rewritten backlight helper and osd

This commit is contained in:
Rene Kievits
2022-11-28 13:03:27 +01:00
parent d63ffa1171
commit 642338e577
3 changed files with 174 additions and 143 deletions

View File

@@ -1,11 +1,15 @@
-- Awesome Libs
-- Awesome libs
local gears = require("gears")
local awful = require("awful")
local hotkeys_popup = require("awful.hotkeys_popup")
local ruled = require("ruled")
-- Third party libs
local json = require("src.lib.json-lua.json-lua")
-- Own libs
local backlight_helper = require("src.tools.helpers.backlight")
local capi = {
awesome = awesome,
mousegrabber = mousegrabber,
@@ -251,14 +255,7 @@ return gears.table.join(
{},
"XF86MonBrightnessUp",
function(c)
awful.spawn.easy_async_with_shell(
"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
)
backlight_helper.brightness_increase()
end,
{ description = "Raise backlight brightness", group = "System" }
),
@@ -266,14 +263,7 @@ return gears.table.join(
{},
"XF86MonBrightnessDown",
function(c)
awful.spawn.easy_async_with_shell(
"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
)
backlight_helper.brightness_decrease()
end,
{ description = "Lower backlight brightness", group = "System" }
),

View File

@@ -3,26 +3,54 @@
---------------------------------------
-- Awesome Libs
local awful = require("awful")
local aplacement = require("awful.placement")
local apopup = require("awful.popup")
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 backlight_helper = require("src.tools.helpers.backlight")
local capi = {
awesome = awesome,
mouse = mouse,
}
-- 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
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",
halign = "center",
resize = false,
@@ -39,7 +67,7 @@ return function(s)
value = 0,
forced_height = dpi(6),
shape = function(cr, width, height)
gears.shape.rounded_bar(cr, width, height, dpi(6))
gshape.rounded_bar(cr, width, height, dpi(6))
end,
widget = wibox.widget.progressbar
},
@@ -64,70 +92,58 @@ return function(s)
},
forced_width = dpi(300),
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_width = Theme_config.brightness_osd.border_width,
fg = Theme_config.brightness_osd.fg,
bg = Theme_config.brightness_osd.bg,
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,
stretch = false,
visible = false,
screen = s,
placement = function(c) awful.placement.bottom_left(c, { margins = dpi(20) }) end,
screen = args.screen,
placement = function(c) aplacement.bottom_left(c, { margins = dpi(20) }) end,
shape = function(cr, width, height)
gears.shape.rounded_rect(cr, width, height, dpi(14))
gshape.rounded_rect(cr, width, height, dpi(14))
end
}
local hide_brightness_osd = gears.timer {
timeout = 2,
autostart = true,
gtable.crush(osd, brightness_osd, 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()
brightness_container.visible = false
osd.visible = false
end
}
end
brightness_container:setup {
brightness_osd_widget,
layout = wibox.layout.fixed.horizontal
}
function brightness_osd.mt:__call(...)
return brightness_osd.new(...)
end
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
return setmetatable(brightness_osd, brightness_osd.mt)

View File

@@ -1,32 +1,57 @@
local awful = require("awful")
local aspawn = require("awful.spawn")
local capi = {
awesome = awesome,
}
BACKLIGHT_MAX_BRIGHTNESS = 0
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
)
local backlight = {}
capi.awesome.connect_signal(
"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
)
backlight.device = ""
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