From 50ac1bcfcb782fb43343d4deb1da5eeaf9314e2a Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 24 Apr 2022 09:10:12 -0700 Subject: [PATCH] Rewrote backlight manager --- backlight/arc_widget.lua | 99 +++++++++++++++++++++++++ backlight/backlight.lua | 133 ++++++++++++++++++++++++++++++++++ backlight/init.lua | 7 ++ bin/init.lua | 25 +------ bin/scripts/backlight | 5 +- binds/system/backlight.lua | 4 +- desktop/init.lua | 6 +- desktop/widgets/backlight.lua | 101 -------------------------- rc.lua | 5 ++ wrapper/backlight.lua | 36 --------- wrapper/init.lua | 1 - 11 files changed, 251 insertions(+), 171 deletions(-) create mode 100644 backlight/arc_widget.lua create mode 100644 backlight/backlight.lua create mode 100644 backlight/init.lua delete mode 100755 desktop/widgets/backlight.lua delete mode 100755 wrapper/backlight.lua diff --git a/backlight/arc_widget.lua b/backlight/arc_widget.lua new file mode 100644 index 0000000..04aa5a8 --- /dev/null +++ b/backlight/arc_widget.lua @@ -0,0 +1,99 @@ +local P = {} + +function P:set_value(value) + -- Set widget value. (0 - 100) + self.arc.value = value + + --[[ + if value > 90 then self.icon.image = beautiful.icons.brightness.i + elseif value > 80 then self.icon.image = beautiful.icons.brightness.h + elseif value > 70 then self.icon.image = beautiful.icons.brightness.g + elseif value > 60 then self.icon.image = beautiful.icons.brightness.f + elseif value > 50 then self.icon.image = beautiful.icons.brightness.e + elseif value > 40 then self.icon.image = beautiful.icons.brightness.d + elseif value > 30 then self.icon.image = beautiful.icons.brightness.c + elseif value > 20 then self.icon.image = beautiful.icons.brightness.b + elseif value <= 10 then self.icon.image = beautiful.icons.brightness.a + end + --]] +end + +function P:set_state(state) + return +end + +function P:set_tooltip(text) + -- Set widget value. (0 - 100) + self.tooltip.text = text +end + +function P:new() + widget = {} + setmetatable(widget, self) + self.__index = self + + widget.icon = wibox.widget { + id = "icon", + image = beautiful.icons.brightness.i, + resize = true, + widget = wibox.widget.imagebox, + } + + widget.arc = wibox.widget { + { + widget.icon, + top = beautiful.dpi(1), + bottom = beautiful.dpi(1), + layout = wibox.container.margin, + }, + max_value = 100, + thickness = beautiful.dpi(4), + start_angle = 4.71238898, -- 2pi*3/4 + --forced_height = beautiful.dpi(16), + --forced_width = beautiful.dpi(16), + colors = {"#27D4CC", "#00446B"}, + bg = "#FFFFFF30", + paddings = beautiful.dpi(2), + widget = wibox.container.arcchart + } + + widget.widget = wibox.widget { + { + { -- Right space + widget = wibox.widget.separator, + color = beautiful.color.transparent, + forced_width = beautiful.dpi(3) + }, + { + widget.arc, + top = beautiful.dpi(2), + bottom = beautiful.dpi(2), + layout = wibox.container.margin, + }, + { -- Left space + widget = wibox.widget.separator, + color = beautiful.color.transparent, + forced_width = beautiful.dpi(3) + }, + layout = wibox.layout.align.horizontal, + }, + layout = wibox.container.background, + } + + widget.tooltip = awful.tooltip { + objects = { widget.widget }, + text = "" + } + + widget.widget:connect_signal("mouse::enter", function(result) + widget.widget.bg = beautiful.color.bar.hover_bg + end) + + widget.widget:connect_signal("mouse::leave", function(result) + widget.widget.bg = beautiful.color.transparent + end) + + return widget +end + +return P diff --git a/backlight/backlight.lua b/backlight/backlight.lua new file mode 100644 index 0000000..b9b1c4b --- /dev/null +++ b/backlight/backlight.lua @@ -0,0 +1,133 @@ +local widget_types = { + arc = require("backlight/arc_widget") +} + +local P = {} + + +--- +-- Internal methods +--- +function P:_get_status() + awful.spawn.easy_async( + script_dir .. "backlight get", + function(stdout, stderr, exitreason, exitcode) + self._ready = true + self.brightness = tonumber(stdout) + self:_update_widget() + end + ) + return true +end + + + +function P:_update_widget() + if (not self._ready) then + self.widget:set_state("error") + self.widget:set_value(self.max_value); + self.widget:set_tooltip("Backlight error"); + return + end + + self.widget:set_value(self.brightness); + self.widget:set_tooltip("Backlight " .. self.brightness .. "u"); + self.widget:set_state("ready") +end + +--- +-- Simple actions +--- +function P:backlight_up() + if (not self._ready) or (self.brightness >= self.max_value) then + return + end + + awful.spawn(script_dir .. "backlight up 5", false) + self.brightness = self.brightness + 5 + if self.brightness > self.max_value then + self.brightness = self.max_value + end + self:_update_widget() +end + +function P:backlight_down() + if (not self._ready) or (self.brightness <= self.min_value) then + return + end + + awful.spawn(script_dir .. "backlight down 5", false) + self.brightness = self.brightness - 5 + if self.brightness < self.min_value then + self.brightness = self.min_value + end + self:_update_widget() +end + +function P:backlight_set(value) + if (not self._ready) then + return + end + + if (value < self.min_value) then + value = self.min_value + end + + if (value > self.max_value) then + value = self.max_value + end + + awful.spawn(script_dir .. "backlight set " .. value, false) + self.brightness = value + self:_update_widget() +end + + +--- +-- Create new volume_interface +--- +function P:new(args) + -- Arguments + b = { + update_interval = args.update_interal or 5, + min_value = args.min_value or 0, + max_value = args.max_value or 100, + widget_type = args.widget_type or "arc", + + + _ready = false, -- is all the information in this class up-to-date? + -- if this is false, ui will show an error and most methods will + -- do nothing. Updated in _get_status() + } + + b.widget = widget_types[b.widget_type]:new() + + b.widget.widget:connect_signal("button::press", + function(_, _, _, button, mods) + -- Scroll up + if (button == 4) then + b:backlight_up() + -- Scroll down + elseif (button == 5) then + b:backlight_down() + end + end + ) + + setmetatable(b, self) + self.__index = self + + -- This timer keeps mute and volume status up-to-date. + b.timer = gears.timer { + timeout = b.update_interval, + call_now = true, + autostart = true, + callback = function() + b:_get_status() + end + } + + return b +end + +return P diff --git a/backlight/init.lua b/backlight/init.lua new file mode 100644 index 0000000..45ab6c3 --- /dev/null +++ b/backlight/init.lua @@ -0,0 +1,7 @@ +local P = require("backlight/backlight") + +local F = function(args) + return P:new(args) +end + +return F diff --git a/bin/init.lua b/bin/init.lua index 8eecb22..4e8e4a8 100755 --- a/bin/init.lua +++ b/bin/init.lua @@ -1,6 +1,3 @@ -dir = configuration_dir .. "bin/" -local script_dir = dir .. "scripts/" - return { system = { @@ -18,26 +15,6 @@ return { }, backlight = { - watch = function(timeout, callback, widget) - awful.widget.watch(script_dir .. "backlight get", timeout, callback, widget) - end, - - get = function(callback) - awful.spawn.easy_async(script_dir .. "backlight get", callback) - end, - - set = function(value) - awful.spawn(script_dir .. "backlight set " .. value, false) - end, - - up = function() - awful.spawn(script_dir .. "backlight up", false) - end, - - down = function() - awful.spawn(script_dir .. "backlight down", false) - end, - redshift = function(temp) awful.spawn("redshift -O " .. tostring(temp), false) end, @@ -59,7 +36,7 @@ return { rofi = { launcher = function() - awful.spawn("rofi -show drun -theme \"" .. dir .. "/rofi/launcher.rasi\"") + awful.spawn("rofi -show drun -theme \"" .. bin_dir .. "/rofi/launcher.rasi\"") end }, diff --git a/bin/scripts/backlight b/bin/scripts/backlight index 63014c8..0ed897f 100755 --- a/bin/scripts/backlight +++ b/bin/scripts/backlight @@ -4,6 +4,7 @@ # backlight get # backlight set [value] # backlight max +# backlight [up|down] [delta] # # Returns: # Set - nothing @@ -19,9 +20,9 @@ function backlight case "set" xbacklight -set $argv[2] case "up" - xbacklight -inc 10 + xbacklight -inc $argv[2] case "down" - xbacklight -dec 10 + xbacklight -dec $argv[2] case "*" echo "Unknown function \"$argv[1]\"" echo "" diff --git a/binds/system/backlight.lua b/binds/system/backlight.lua index e5f8685..027473f 100755 --- a/binds/system/backlight.lua +++ b/binds/system/backlight.lua @@ -1,7 +1,7 @@ return gears.table.join( awful.key( {}, "XF86MonBrightnessUp", function () - wrapper.backlight.up() + bck.backlight_up() end, { description = "Raise brightness", @@ -11,7 +11,7 @@ return gears.table.join( awful.key( {}, "XF86MonBrightnessDown", function () - wrapper.backlight.down() + bck.backlight_down() end, { description = "Lower brightness", diff --git a/desktop/init.lua b/desktop/init.lua index dc6fe33..b869115 100755 --- a/desktop/init.lua +++ b/desktop/init.lua @@ -47,10 +47,6 @@ local desktop = { } --- Load conditional modules -if conf.backlight_enabled then - desktop.widgets.backlight = require("desktop.widgets.backlight") -end if conf.battery_enabled then desktop.widgets.battery = require("desktop.widgets.battery") end @@ -183,7 +179,7 @@ awful.screen.connect_for_each_screen( desktop.widgets.space(8), desktop.widgets.battery, - desktop.widgets.backlight, + bck.widget.widget, vol.widget.widget, desktop.widgets.space(8), diff --git a/desktop/widgets/backlight.lua b/desktop/widgets/backlight.lua deleted file mode 100755 index d474ba1..0000000 --- a/desktop/widgets/backlight.lua +++ /dev/null @@ -1,101 +0,0 @@ -local backlight = {} - - -backlight.icon = wibox.widget { - id = "icon", - image = beautiful.icons.brightness.i, - resize = true, - widget = wibox.widget.imagebox, -} - -backlight.arc = wibox.widget { - { - backlight.icon, - top = beautiful.dpi(1), - bottom = beautiful.dpi(1), - layout = wibox.container.margin, - }, - max_value = 100, - thickness = beautiful.dpi(4), - start_angle = 4.71238898, -- 2pi*3/4 - --forced_height = beautiful.dpi(16), - --forced_width = beautiful.dpi(16), - colors = {"#27D4CC", "#00446B"}, - bg = "#FFFFFF30", - paddings = beautiful.dpi(2), - widget = wibox.container.arcchart -} - - -backlight.widget = wibox.widget { - { - { -- Right space - widget = wibox.widget.separator, - color = beautiful.color.transparent, - forced_width = beautiful.dpi(3) - }, - { -- Main indicator. Can be replaced with backlight.arc - backlight.arc, - top = beautiful.dpi(2), - bottom = beautiful.dpi(2), - layout = wibox.container.margin, - }, - { -- Left space - widget = wibox.widget.separator, - color = beautiful.color.transparent, - forced_width = beautiful.dpi(3) - }, - layout = wibox.layout.align.horizontal, - }, - layout = wibox.container.background, -} - -backlight.widget:connect_signal("mouse::enter", function(result) - backlight.widget.bg = beautiful.color.bar.hover_bg - -end) - -backlight.widget:connect_signal("mouse::leave", function(result) - backlight.widget.bg = beautiful.color.transparent -end) - -backlight.widget:connect_signal("button::press", - function(_, _, _, button, mods) - -- Scroll up - if (button == 4) then - wrapper.backlight.up() - -- Scroll down - elseif (button == 5) then - wrapper.backlight.down() - end - end -) - -backlight.update = function(value) - backlight.arc.value = value - - --[[if value > 90 then backlight.icon.image = beautiful.icons.brightness.i - elseif value > 80 then backlight.icon.image = beautiful.icons.brightness.h - elseif value > 70 then backlight.icon.image = beautiful.icons.brightness.g - elseif value > 60 then backlight.icon.image = beautiful.icons.brightness.f - elseif value > 50 then backlight.icon.image = beautiful.icons.brightness.e - elseif value > 40 then backlight.icon.image = beautiful.icons.brightness.d - elseif value > 30 then backlight.icon.image = beautiful.icons.brightness.c - elseif value > 20 then backlight.icon.image = beautiful.icons.brightness.b - elseif value <= 10 then backlight.icon.image = beautiful.icons.brightness.a end - --]] - -end - - --- Add various hooks -wrapper.backlight.add_hook(backlight.update) -bin.backlight.watch( - 5, - function() - wrapper.backlight.read(backlight.update) - end, - backlight.widget -) - -return backlight.widget diff --git a/rc.lua b/rc.lua index c8afb28..714ad1f 100755 --- a/rc.lua +++ b/rc.lua @@ -9,6 +9,8 @@ menubar = require("menubar") wibox = require("wibox") configuration_dir = gears.filesystem.get_configuration_dir() +bin_dir = configuration_dir .. "bin/" +script_dir = bin_dir .. "scripts/" require("awful.autofocus") @@ -38,6 +40,9 @@ beautiful.init(require("theme")) Volume = require("volume") vol = Volume{pa_sink = conf.pamixer_sink} +Backlight = require("backlight") +bck = Backlight{} + desktop = require("desktop") diff --git a/wrapper/backlight.lua b/wrapper/backlight.lua deleted file mode 100755 index 0b9c85f..0000000 --- a/wrapper/backlight.lua +++ /dev/null @@ -1,36 +0,0 @@ -local backlight = {} - - -backlight.hooks = {} -backlight.add_hook = function(callback) - backlight.hooks[#backlight.hooks + 1] = callback -end - -backlight.exec_hooks = function() - backlight.read(function(status) - for i=1, #backlight.hooks do - backlight.hooks[i](status) - end - end) -end - -backlight.read = function(callback) - bin.backlight.get( - function(stdout, stderr, exitreason, exitcode) - callback(tonumber(stdout)) - end - ) -end - - -backlight.up = function() - bin.backlight.up() - backlight.exec_hooks() -end - -backlight.down = function() - bin.backlight.down() - backlight.exec_hooks() -end - -return backlight diff --git a/wrapper/init.lua b/wrapper/init.lua index 2ec6893..f65a0d7 100755 --- a/wrapper/init.lua +++ b/wrapper/init.lua @@ -1,5 +1,4 @@ return { - backlight = require("wrapper.backlight"), sound = require("wrapper.sound"), ibus = require("wrapper.ibus"), --mdadm = require("wrapper.mdadm")