rewritten backlight helper and osd
This commit is contained in:
@@ -1,11 +1,15 @@
|
|||||||
-- Awesome Libs
|
-- Awesome libs
|
||||||
local gears = require("gears")
|
local gears = require("gears")
|
||||||
local awful = require("awful")
|
local awful = require("awful")
|
||||||
local hotkeys_popup = require("awful.hotkeys_popup")
|
local hotkeys_popup = require("awful.hotkeys_popup")
|
||||||
local ruled = require("ruled")
|
local ruled = require("ruled")
|
||||||
|
|
||||||
|
-- Third party libs
|
||||||
local json = require("src.lib.json-lua.json-lua")
|
local json = require("src.lib.json-lua.json-lua")
|
||||||
|
|
||||||
|
-- Own libs
|
||||||
|
local backlight_helper = require("src.tools.helpers.backlight")
|
||||||
|
|
||||||
local capi = {
|
local capi = {
|
||||||
awesome = awesome,
|
awesome = awesome,
|
||||||
mousegrabber = mousegrabber,
|
mousegrabber = mousegrabber,
|
||||||
@@ -251,14 +255,7 @@ return gears.table.join(
|
|||||||
{},
|
{},
|
||||||
"XF86MonBrightnessUp",
|
"XF86MonBrightnessUp",
|
||||||
function(c)
|
function(c)
|
||||||
awful.spawn.easy_async_with_shell(
|
backlight_helper.brightness_increase()
|
||||||
"pkexec xfpm-power-backlight-helper --get-brightness",
|
|
||||||
function(stdout)
|
|
||||||
awful.spawn(gears.filesystem.get_configuration_dir() ..
|
|
||||||
"src/scripts/backlight.sh set " .. tostring(tonumber(stdout) + BACKLIGHT_SEPS))
|
|
||||||
capi.awesome.emit_signal("brightness::update")
|
|
||||||
end
|
|
||||||
)
|
|
||||||
end,
|
end,
|
||||||
{ description = "Raise backlight brightness", group = "System" }
|
{ description = "Raise backlight brightness", group = "System" }
|
||||||
),
|
),
|
||||||
@@ -266,14 +263,7 @@ return gears.table.join(
|
|||||||
{},
|
{},
|
||||||
"XF86MonBrightnessDown",
|
"XF86MonBrightnessDown",
|
||||||
function(c)
|
function(c)
|
||||||
awful.spawn.easy_async_with_shell(
|
backlight_helper.brightness_decrease()
|
||||||
"pkexec xfpm-power-backlight-helper --get-brightness",
|
|
||||||
function(stdout)
|
|
||||||
awful.spawn(gears.filesystem.get_configuration_dir() ..
|
|
||||||
"src/scripts/backlight.sh set " .. tostring(tonumber(stdout) - BACKLIGHT_SEPS))
|
|
||||||
capi.awesome.emit_signal("brightness::update")
|
|
||||||
end
|
|
||||||
)
|
|
||||||
end,
|
end,
|
||||||
{ description = "Lower backlight brightness", group = "System" }
|
{ description = "Lower backlight brightness", group = "System" }
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -3,26 +3,54 @@
|
|||||||
---------------------------------------
|
---------------------------------------
|
||||||
|
|
||||||
-- Awesome Libs
|
-- Awesome Libs
|
||||||
local awful = require("awful")
|
local aplacement = require("awful.placement")
|
||||||
|
local apopup = require("awful.popup")
|
||||||
local dpi = require("beautiful").xresources.apply_dpi
|
local dpi = require("beautiful").xresources.apply_dpi
|
||||||
local gears = require("gears")
|
local gcolor = require("gears.color")
|
||||||
|
local gfilesystem = require("gears.filesystem")
|
||||||
|
local gtable = require("gears.table")
|
||||||
|
local gshape = require("gears.shape")
|
||||||
|
local gtimer = require("gears.timer")
|
||||||
local wibox = require("wibox")
|
local wibox = require("wibox")
|
||||||
|
|
||||||
|
local backlight_helper = require("src.tools.helpers.backlight")
|
||||||
|
|
||||||
local capi = {
|
local capi = {
|
||||||
awesome = awesome,
|
awesome = awesome,
|
||||||
mouse = mouse,
|
mouse = mouse,
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Icon directory path
|
-- Icon directory path
|
||||||
local icondir = gears.filesystem.get_configuration_dir() .. "src/assets/icons/brightness/"
|
local icondir = gfilesystem.get_configuration_dir() .. "src/assets/icons/brightness/"
|
||||||
|
|
||||||
return function(s)
|
local brightness_osd = { mt = {} }
|
||||||
|
|
||||||
local brightness_osd_widget = wibox.widget {
|
-- Hide the brightness_osd after 3 seconds
|
||||||
|
function brightness_osd:hide()
|
||||||
|
self.timer:stop(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Rerun the timer
|
||||||
|
function brightness_osd:rerun()
|
||||||
|
self.visible = true
|
||||||
|
self.timer:again(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Show the brightness_osd for 3 seconds
|
||||||
|
function brightness_osd:show()
|
||||||
|
self.visible = true
|
||||||
|
self.timer:start(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
function brightness_osd.new(args)
|
||||||
|
args = args or {}
|
||||||
|
|
||||||
|
local osd = apopup {
|
||||||
|
widget = {
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
{ -- Brightness Icon
|
{ -- Brightness Icon
|
||||||
image = gears.color.recolor_image(icondir .. "brightness-high.svg", Theme_config.brightness_osd.icon_color),
|
image = gcolor.recolor_image(icondir .. "brightness-high.svg", Theme_config.brightness_osd.icon_color),
|
||||||
valign = "center",
|
valign = "center",
|
||||||
halign = "center",
|
halign = "center",
|
||||||
resize = false,
|
resize = false,
|
||||||
@@ -39,7 +67,7 @@ return function(s)
|
|||||||
value = 0,
|
value = 0,
|
||||||
forced_height = dpi(6),
|
forced_height = dpi(6),
|
||||||
shape = function(cr, width, height)
|
shape = function(cr, width, height)
|
||||||
gears.shape.rounded_bar(cr, width, height, dpi(6))
|
gshape.rounded_bar(cr, width, height, dpi(6))
|
||||||
end,
|
end,
|
||||||
widget = wibox.widget.progressbar
|
widget = wibox.widget.progressbar
|
||||||
},
|
},
|
||||||
@@ -64,70 +92,58 @@ return function(s)
|
|||||||
},
|
},
|
||||||
forced_width = dpi(300),
|
forced_width = dpi(300),
|
||||||
forced_height = dpi(80),
|
forced_height = dpi(80),
|
||||||
shape = function(cr, width, height)
|
|
||||||
gears.shape.rounded_rect(cr, width, height, dpi(12))
|
|
||||||
end,
|
|
||||||
border_color = Theme_config.brightness_osd.border_color,
|
border_color = Theme_config.brightness_osd.border_color,
|
||||||
border_width = Theme_config.brightness_osd.border_width,
|
border_width = Theme_config.brightness_osd.border_width,
|
||||||
fg = Theme_config.brightness_osd.fg,
|
fg = Theme_config.brightness_osd.fg,
|
||||||
bg = Theme_config.brightness_osd.bg,
|
bg = Theme_config.brightness_osd.bg,
|
||||||
widget = wibox.container.background
|
widget = wibox.container.background
|
||||||
}
|
},
|
||||||
|
|
||||||
capi.awesome.connect_signal(
|
|
||||||
"brightness::get",
|
|
||||||
function(brightness)
|
|
||||||
brightness_osd_widget:get_children_by_id("progressbar1")[1].value = brightness
|
|
||||||
|
|
||||||
local icon = icondir .. "brightness"
|
|
||||||
if brightness >= 0 and brightness < 34 then
|
|
||||||
icon = icon .. "-low"
|
|
||||||
elseif brightness >= 34 and brightness < 67 then
|
|
||||||
icon = icon .. "-medium"
|
|
||||||
elseif brightness >= 67 then
|
|
||||||
icon = icon .. "-high"
|
|
||||||
end
|
|
||||||
brightness_osd_widget:get_children_by_id("icon")[1]:set_image(gears.color.recolor_image(icon .. ".svg",
|
|
||||||
Theme_config.brightness_osd.icon_color))
|
|
||||||
end
|
|
||||||
)
|
|
||||||
|
|
||||||
local brightness_container = awful.popup {
|
|
||||||
widget = {},
|
|
||||||
ontop = true,
|
ontop = true,
|
||||||
stretch = false,
|
stretch = false,
|
||||||
visible = false,
|
visible = false,
|
||||||
screen = s,
|
screen = args.screen,
|
||||||
placement = function(c) awful.placement.bottom_left(c, { margins = dpi(20) }) end,
|
placement = function(c) aplacement.bottom_left(c, { margins = dpi(20) }) end,
|
||||||
shape = function(cr, width, height)
|
shape = function(cr, width, height)
|
||||||
gears.shape.rounded_rect(cr, width, height, dpi(14))
|
gshape.rounded_rect(cr, width, height, dpi(14))
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
local hide_brightness_osd = gears.timer {
|
gtable.crush(osd, brightness_osd, true)
|
||||||
timeout = 2,
|
|
||||||
autostart = true,
|
-- Called when the brightness changes, updates the brightness osd and icon
|
||||||
|
capi.awesome.connect_signal("brightness::changed", function(brightness)
|
||||||
|
if not capi.mouse.screen == args.screen then return end
|
||||||
|
assert(type(brightness) == "number", "brightness must be a number")
|
||||||
|
|
||||||
|
brightness = (brightness - 0) / ((backlight_helper.brightness_max or 24000) - 0) * 100
|
||||||
|
osd.widget:get_children_by_id("progressbar1")[1].value = brightness
|
||||||
|
|
||||||
|
local icon = icondir .. "brightness"
|
||||||
|
if brightness >= 0 and brightness < 34 then
|
||||||
|
icon = icon .. "-low.svg"
|
||||||
|
elseif brightness >= 34 and brightness < 67 then
|
||||||
|
icon = icon .. "-medium.svg"
|
||||||
|
elseif brightness >= 67 then
|
||||||
|
icon = icon .. "-high.svg"
|
||||||
|
end
|
||||||
|
|
||||||
|
osd:rerun(true)
|
||||||
|
osd.widget:get_children_by_id("icon")[1]:set_image(gcolor.recolor_image(icon,
|
||||||
|
Theme_config.brightness_osd.icon_color))
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- osd timer
|
||||||
|
osd.timer = gtimer {
|
||||||
|
timeout = 3,
|
||||||
|
single_shot = true,
|
||||||
callback = function()
|
callback = function()
|
||||||
brightness_container.visible = false
|
osd.visible = false
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
brightness_container:setup {
|
|
||||||
brightness_osd_widget,
|
|
||||||
layout = wibox.layout.fixed.horizontal
|
|
||||||
}
|
|
||||||
|
|
||||||
capi.awesome.connect_signal(
|
|
||||||
"brightness::rerun",
|
|
||||||
function()
|
|
||||||
if capi.mouse.screen == s then
|
|
||||||
brightness_container.visible = true
|
|
||||||
if hide_brightness_osd.started then
|
|
||||||
hide_brightness_osd:again()
|
|
||||||
else
|
|
||||||
hide_brightness_osd:start()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function brightness_osd.mt:__call(...)
|
||||||
|
return brightness_osd.new(...)
|
||||||
|
end
|
||||||
|
|
||||||
|
return setmetatable(brightness_osd, brightness_osd.mt)
|
||||||
|
|||||||
@@ -1,32 +1,57 @@
|
|||||||
local awful = require("awful")
|
local aspawn = require("awful.spawn")
|
||||||
|
|
||||||
local capi = {
|
local capi = {
|
||||||
awesome = awesome,
|
awesome = awesome,
|
||||||
}
|
}
|
||||||
|
|
||||||
BACKLIGHT_MAX_BRIGHTNESS = 0
|
local backlight = {}
|
||||||
BACKLIGHT_SEPS = 0
|
|
||||||
awful.spawn.easy_async_with_shell(
|
|
||||||
"pkexec xfpm-power-backlight-helper --get-max-brightness",
|
|
||||||
function(stdout)
|
|
||||||
BACKLIGHT_MAX_BRIGHTNESS = tonumber(stdout)
|
|
||||||
BACKLIGHT_SEPS = BACKLIGHT_MAX_BRIGHTNESS / 100 * 2
|
|
||||||
BACKLIGHT_SEPS = math.floor(BACKLIGHT_SEPS)
|
|
||||||
end
|
|
||||||
)
|
|
||||||
|
|
||||||
capi.awesome.connect_signal(
|
backlight.device = ""
|
||||||
"brightness::update",
|
|
||||||
function()
|
|
||||||
awful.spawn.easy_async_with_shell(
|
|
||||||
"pkexec xfpm-power-backlight-helper --get-brightness",
|
|
||||||
function(value)
|
|
||||||
capi.awesome.emit_signal("brightness::get",
|
|
||||||
math.floor((tonumber(value) - 1) / (BACKLIGHT_MAX_BRIGHTNESS - 1) * 100))
|
|
||||||
capi.awesome.emit_signal("brightness::rerun")
|
|
||||||
end
|
|
||||||
)
|
|
||||||
end
|
|
||||||
)
|
|
||||||
|
|
||||||
capi.awesome.emit_signal("brightness::update")
|
backlight.max_brightness = 1
|
||||||
|
|
||||||
|
-- Init the backlight device and get the max brightness
|
||||||
|
aspawn.easy_async_with_shell("ls /sys/class/backlight/", function(stdout)
|
||||||
|
backlight.device = stdout:gsub("%s+", "")
|
||||||
|
aspawn.easy_async_with_shell("cat /sys/class/backlight/" .. backlight.device .. "/max_brightness", function(stdout)
|
||||||
|
backlight.max_brightness = tonumber(stdout:gsub("\n", "") or 0)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
|
||||||
|
function backlight.brightness_get()
|
||||||
|
aspawn.easy_async_with_shell("cat /sys/class/backlight/" .. backlight.device .. "/brightness", function(stdout)
|
||||||
|
capi.awesome.emit_signal("brightness::get", tonumber(stdout))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
function backlight.brightness_get_percent()
|
||||||
|
aspawn.easy_async_with_shell("cat /sys/class/backlight/" .. backlight.device .. "/brightness", function(stdout)
|
||||||
|
capi.awesome.emit_signal("brightness::get_percent",
|
||||||
|
math.floor((tonumber(stdout) / backlight.max_brightness * 100) + 0.5))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
function backlight.brightness_set(value)
|
||||||
|
if value < 0 or value > (backlight.max_brightness or 24000) then return end
|
||||||
|
aspawn.with_shell("echo " .. math.floor(value) .. " > /sys/class/backlight/" .. backlight.device .. "/brightness")
|
||||||
|
end
|
||||||
|
|
||||||
|
function backlight.brightness_increase()
|
||||||
|
aspawn.easy_async_with_shell("cat /sys/class/backlight/" .. backlight.device .. "/brightness", function(stdout)
|
||||||
|
local new_value = tonumber(stdout:gsub("\n", "") or 0) +
|
||||||
|
(backlight.max_brightness / 100 * User_config.brightness_step)
|
||||||
|
backlight.brightness_set(new_value)
|
||||||
|
capi.awesome.emit_signal("brightness::changed", new_value)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
function backlight.brightness_decrease()
|
||||||
|
aspawn.easy_async_with_shell("cat /sys/class/backlight/" .. backlight.device .. "/brightness", function(stdout)
|
||||||
|
local new_value = tonumber(stdout:gsub("\n", "") or 0) -
|
||||||
|
(backlight.max_brightness / 100 * User_config.brightness_step)
|
||||||
|
backlight.brightness_set(new_value)
|
||||||
|
capi.awesome.emit_signal("brightness::changed", new_value)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
return backlight
|
||||||
|
|||||||
Reference in New Issue
Block a user