From 642338e57726ad6433d50dd270716154afd04054 Mon Sep 17 00:00:00 2001 From: Rene Kievits Date: Mon, 28 Nov 2022 13:03:27 +0100 Subject: [PATCH] rewritten backlight helper and osd --- awesome/src/bindings/global_keys.lua | 24 +- .../src/modules/brightness/brightness_osd.lua | 216 ++++++++++-------- awesome/src/tools/helpers/backlight.lua | 77 ++++--- 3 files changed, 174 insertions(+), 143 deletions(-) diff --git a/awesome/src/bindings/global_keys.lua b/awesome/src/bindings/global_keys.lua index 1943c6c..8fde800 100644 --- a/awesome/src/bindings/global_keys.lua +++ b/awesome/src/bindings/global_keys.lua @@ -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" } ), diff --git a/awesome/src/modules/brightness/brightness_osd.lua b/awesome/src/modules/brightness/brightness_osd.lua index 35d821b..eb226fc 100644 --- a/awesome/src/modules/brightness/brightness_osd.lua +++ b/awesome/src/modules/brightness/brightness_osd.lua @@ -3,131 +3,147 @@ --------------------------------------- -- 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), - valign = "center", - halign = "center", - resize = false, - id = "icon", - widget = wibox.widget.imagebox - }, - { -- Brightness Bar - { - { - id = "progressbar1", - color = Theme_config.brightness_osd.bar_bg_active, - background_color = Theme_config.brightness_osd.bar_bg, - max_value = 100, - value = 0, - forced_height = dpi(6), - shape = function(cr, width, height) - gears.shape.rounded_bar(cr, width, height, dpi(6)) - end, - widget = wibox.widget.progressbar - }, - id = "progressbar_container2", - halign = "center", + { + { -- Brightness Icon + image = gcolor.recolor_image(icondir .. "brightness-high.svg", Theme_config.brightness_osd.icon_color), valign = "center", - widget = wibox.container.place + halign = "center", + resize = false, + id = "icon", + widget = wibox.widget.imagebox }, - id = "progressbar_container", - width = dpi(240), - heigth = dpi(20), - stragety = "max", - widget = wibox.container.constraint + { -- Brightness Bar + { + { + id = "progressbar1", + color = Theme_config.brightness_osd.bar_bg_active, + background_color = Theme_config.brightness_osd.bar_bg, + max_value = 100, + value = 0, + forced_height = dpi(6), + shape = function(cr, width, height) + gshape.rounded_bar(cr, width, height, dpi(6)) + end, + widget = wibox.widget.progressbar + }, + id = "progressbar_container2", + halign = "center", + valign = "center", + widget = wibox.container.place + }, + id = "progressbar_container", + width = dpi(240), + heigth = dpi(20), + stragety = "max", + widget = wibox.container.constraint + }, + id = "layout1", + spacing = dpi(10), + layout = wibox.layout.fixed.horizontal }, - id = "layout1", - spacing = dpi(10), - layout = wibox.layout.fixed.horizontal + id = "margin", + margins = dpi(10), + widget = wibox.container.margin }, - id = "margin", - margins = dpi(10), - widget = wibox.container.margin + forced_width = dpi(300), + forced_height = dpi(80), + 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 }, - 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 } - - 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 + +function brightness_osd.mt:__call(...) + return brightness_osd.new(...) +end + +return setmetatable(brightness_osd, brightness_osd.mt) diff --git a/awesome/src/tools/helpers/backlight.lua b/awesome/src/tools/helpers/backlight.lua index 1d358f9..ac26467 100644 --- a/awesome/src/tools/helpers/backlight.lua +++ b/awesome/src/tools/helpers/backlight.lua @@ -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