From 3c62f2d59a7319aef2eedbe0d2c6cc8bb9f87f03 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 12 Feb 2023 21:56:06 +0000 Subject: rc: show notification on screen backlight changes --- rc.lua | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/rc.lua b/rc.lua index 2bf47ca..28eb8e8 100644 --- a/rc.lua +++ b/rc.lua @@ -9,6 +9,8 @@ local beautiful = require("beautiful") -- Notification library local naughty = require("naughty") +local gio = require("lgi").Gio + -- local modules local bindings = require("bindings") local commondefs = require("commondefs") @@ -233,3 +235,39 @@ do end) end end + +-- show notification on display brightness changes +local function brightness_stdout(line) + local action, device = string.match(line, '^KERNEL%[%d+.%d*%]%s+(%a+)%s+([^%s]+)%s+%(backlight%)$') + if action ~= 'change' then + return + end + + local function readnum(name) + local file = gio.File.new_for_path('/sys/' .. device .. '/' .. name) + local contents = file:load_contents(nil) + + if contents then + contents = string.gsub(contents, "%s$", "") + return string.len(contents) > 0 and tonumber(contents) or nil + end + + return nil + end + + local brightness = readnum('brightness') + local max_brightness = readnum('max_brightness') + + if brightness and max_brightness then + utils.notify_singleton(wsp, 'brightness', + { title = 'Brightness', + text = tostring(100 * brightness // max_brightness) .. '%'}) + end + + return nil +end +local function brightness_exit(reason, code) + utils.warn('brightness', 'udevadm monitor exited: %s/%d', reason, code) +end +awful.spawn.with_line_callback('udevadm monitor --kernel --subsystem-match=backlight', + { stdout = brightness_stdout, exit = brightness_exit }) -- cgit v1.2.3