summaryrefslogtreecommitdiff
path: root/brightness-widget/brightness.lua
diff options
context:
space:
mode:
Diffstat (limited to 'brightness-widget/brightness.lua')
-rw-r--r--brightness-widget/brightness.lua48
1 files changed, 45 insertions, 3 deletions
diff --git a/brightness-widget/brightness.lua b/brightness-widget/brightness.lua
index 2afb38a..b66b1f0 100644
--- a/brightness-widget/brightness.lua
+++ b/brightness-widget/brightness.lua
@@ -16,6 +16,7 @@ local naughty = require("naughty")
local ICON_DIR = os.getenv("HOME") .. '/.config/awesome/awesome-wm-widgets/brightness-widget/'
local get_brightness_cmd
+local set_brightness_cmd
local inc_brightness_cmd
local dec_brightness_cmd
@@ -39,12 +40,17 @@ local function worker(user_args)
local program = args.program or 'light'
local step = args.step or 5
+ local base = args.base or 20
+ local current_level = 0 -- current brightness value
+ local tooltip = args.tooltip or false
if program == 'light' then
- get_brightness_cmd = 'sh -c "light -G"'
- inc_brightness_cmd = 'sh -c "light -A ' .. step .. '"'
- dec_brightness_cmd = 'sh -c "light -U ' .. step .. '"'
+ get_brightness_cmd = 'light -G'
+ set_brightness_cmd = 'light -S ' -- <level>
+ inc_brightness_cmd = 'light -A ' .. step
+ dec_brightness_cmd = 'light -U ' .. step
elseif program == 'xbacklight' then
get_brightness_cmd = 'xbacklight -get'
+ set_brightness_cmd = 'xbacklight -set ' -- <level>
inc_brightness_cmd = 'xbacklight -inc ' .. step
dec_brightness_cmd = 'xbacklight -dec ' .. step
else
@@ -104,9 +110,34 @@ local function worker(user_args)
local update_widget = function(widget, stdout, _, _, _)
local brightness_level = tonumber(string.format("%.0f", stdout))
+ current_level = brightness_level
widget:set_value(brightness_level)
end
+ function brightness_widget:set(value)
+ current_level = value
+ spawn.easy_async(set_brightness_cmd .. value, function()
+ spawn.easy_async(get_brightness_cmd, function(out)
+ update_widget(brightness_widget.widget, out)
+ end)
+ end)
+ end
+ local old_level = 0
+ function brightness_widget:toggle()
+ if old_level < 0.1 then
+ -- avoid toggling between '0' and 'almost 0'
+ old_level = 1
+ end
+ if current_level < 0.1 then
+ -- restore previous level
+ current_level = old_level
+ else
+ -- save current brightness for later
+ old_level = current_level
+ current_level = 0
+ end
+ brightness_widget:set(current_level)
+ end
function brightness_widget:inc()
spawn.easy_async(inc_brightness_cmd, function()
spawn.easy_async(get_brightness_cmd, function(out)
@@ -124,6 +155,8 @@ local function worker(user_args)
brightness_widget.widget:buttons(
awful.util.table.join(
+ awful.button({}, 1, function() brightness_widget:set(base) end),
+ awful.button({}, 3, function() brightness_widget:toggle() end),
awful.button({}, 4, function() brightness_widget:inc() end),
awful.button({}, 5, function() brightness_widget:dec() end)
)
@@ -131,6 +164,15 @@ local function worker(user_args)
watch(get_brightness_cmd, timeout, update_widget, brightness_widget.widget)
+ if tooltip then
+ awful.tooltip {
+ objects = { brightness_widget.widget },
+ timer_function = function()
+ return current_level .. " %"
+ end,
+ }
+ end
+
return brightness_widget.widget
end