2017-12-09 20:39:46 +01:00
|
|
|
-------------------------------------------------
|
|
|
|
-- Brightness Widget for Awesome Window Manager
|
|
|
|
-- Shows the brightness level of the laptop display
|
|
|
|
-- More details could be found here:
|
|
|
|
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/brightness-widget
|
|
|
|
|
|
|
|
-- @author Pavel Makhov
|
2021-01-16 20:50:54 +01:00
|
|
|
-- @copyright 2021 Pavel Makhov
|
2017-12-09 20:39:46 +01:00
|
|
|
-------------------------------------------------
|
|
|
|
|
2021-01-16 20:50:54 +01:00
|
|
|
local awful = require("awful")
|
2017-01-31 03:38:50 +01:00
|
|
|
local wibox = require("wibox")
|
|
|
|
local watch = require("awful.widget.watch")
|
2018-10-08 17:07:28 +02:00
|
|
|
local spawn = require("awful.spawn")
|
2022-03-22 17:13:05 +01:00
|
|
|
local gfs = require("gears.filesystem")
|
2021-01-16 20:50:54 +01:00
|
|
|
local naughty = require("naughty")
|
2022-01-24 16:20:36 +01:00
|
|
|
local beautiful = require("beautiful")
|
2017-01-31 03:38:50 +01:00
|
|
|
|
2022-03-22 17:13:05 +01:00
|
|
|
local ICON_DIR = gfs.get_configuration_dir() .. "awesome-wm-widgets/brightness-widget/"
|
2021-01-16 20:50:54 +01:00
|
|
|
local get_brightness_cmd
|
2021-04-15 23:01:12 +02:00
|
|
|
local set_brightness_cmd
|
2021-01-16 20:50:54 +01:00
|
|
|
local inc_brightness_cmd
|
|
|
|
local dec_brightness_cmd
|
2017-01-31 03:38:50 +01:00
|
|
|
|
2020-12-06 20:47:40 +01:00
|
|
|
local brightness_widget = {}
|
2019-05-03 03:35:30 +02:00
|
|
|
|
2021-01-16 20:50:54 +01:00
|
|
|
local function show_warning(message)
|
2022-03-22 17:13:05 +01:00
|
|
|
naughty.notify({
|
|
|
|
preset = naughty.config.presets.critical,
|
|
|
|
title = "Brightness Widget",
|
|
|
|
text = message,
|
|
|
|
})
|
2021-01-16 20:50:54 +01:00
|
|
|
end
|
|
|
|
|
2020-12-06 20:47:40 +01:00
|
|
|
local function worker(user_args)
|
2022-04-16 15:00:12 +02:00
|
|
|
local args = user_args or {}
|
2019-05-03 03:35:30 +02:00
|
|
|
|
2021-01-16 20:50:54 +01:00
|
|
|
local type = args.type or 'arc' -- arc or icon_and_text
|
|
|
|
local path_to_icon = args.path_to_icon or ICON_DIR .. 'brightness.svg'
|
2022-01-24 16:20:36 +01:00
|
|
|
local font = args.font or beautiful.font
|
2021-01-16 20:50:54 +01:00
|
|
|
local timeout = args.timeout or 100
|
|
|
|
|
|
|
|
local program = args.program or 'light'
|
|
|
|
local step = args.step or 5
|
2021-04-15 23:01:12 +02:00
|
|
|
local base = args.base or 20
|
2021-04-16 22:32:32 +02:00
|
|
|
local current_level = 0 -- current brightness value
|
2021-04-16 00:03:13 +02:00
|
|
|
local tooltip = args.tooltip or false
|
2022-01-20 23:45:53 +01:00
|
|
|
local percentage = args.percentage or false
|
2022-09-17 11:44:26 +02:00
|
|
|
local rmb_set_max = args.rmb_set_max or false
|
2021-01-16 20:50:54 +01:00
|
|
|
if program == 'light' then
|
2021-04-15 22:57:28 +02:00
|
|
|
get_brightness_cmd = 'light -G'
|
2021-10-16 19:53:27 +02:00
|
|
|
set_brightness_cmd = 'light -S %d' -- <level>
|
2021-04-15 22:57:28 +02:00
|
|
|
inc_brightness_cmd = 'light -A ' .. step
|
|
|
|
dec_brightness_cmd = 'light -U ' .. step
|
2021-01-16 20:50:54 +01:00
|
|
|
elseif program == 'xbacklight' then
|
|
|
|
get_brightness_cmd = 'xbacklight -get'
|
2021-10-16 19:53:27 +02:00
|
|
|
set_brightness_cmd = 'xbacklight -set %d' -- <level>
|
2021-01-16 20:50:54 +01:00
|
|
|
inc_brightness_cmd = 'xbacklight -inc ' .. step
|
|
|
|
dec_brightness_cmd = 'xbacklight -dec ' .. step
|
2021-10-16 19:53:27 +02:00
|
|
|
elseif program == 'brightnessctl' then
|
2022-08-23 10:47:06 +02:00
|
|
|
get_brightness_cmd = "brightnessctl get"
|
|
|
|
set_brightness_cmd = "brightnessctl set %d%%" -- <level>
|
|
|
|
inc_brightness_cmd = "brightnessctl set +" .. step .. "%"
|
|
|
|
dec_brightness_cmd = "brightnessctl set " .. step .. "-%"
|
2021-01-16 20:50:54 +01:00
|
|
|
else
|
|
|
|
show_warning(program .. " command is not supported by the widget")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if type == 'icon_and_text' then
|
|
|
|
brightness_widget.widget = wibox.widget {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
image = path_to_icon,
|
|
|
|
resize = false,
|
|
|
|
widget = wibox.widget.imagebox,
|
|
|
|
},
|
2021-11-28 19:53:23 +01:00
|
|
|
valign = 'center',
|
2021-01-16 20:50:54 +01:00
|
|
|
layout = wibox.container.place
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id = 'txt',
|
|
|
|
font = font,
|
|
|
|
widget = wibox.widget.textbox
|
|
|
|
},
|
|
|
|
spacing = 4,
|
|
|
|
layout = wibox.layout.fixed.horizontal,
|
|
|
|
set_value = function(self, level)
|
2022-01-20 23:45:53 +01:00
|
|
|
local display_level = level
|
|
|
|
if percentage then
|
|
|
|
display_level = display_level .. '%'
|
|
|
|
end
|
|
|
|
self:get_children_by_id('txt')[1]:set_text(display_level)
|
2021-01-16 20:50:54 +01:00
|
|
|
end
|
|
|
|
}
|
|
|
|
elseif type == 'arc' then
|
|
|
|
brightness_widget.widget = wibox.widget {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
image = path_to_icon,
|
|
|
|
resize = true,
|
|
|
|
widget = wibox.widget.imagebox,
|
|
|
|
},
|
2021-11-28 19:53:23 +01:00
|
|
|
valign = 'center',
|
2021-01-16 20:50:54 +01:00
|
|
|
layout = wibox.container.place
|
|
|
|
},
|
|
|
|
max_value = 100,
|
|
|
|
thickness = 2,
|
|
|
|
start_angle = 4.71238898, -- 2pi*3/4
|
|
|
|
forced_height = 18,
|
|
|
|
forced_width = 18,
|
|
|
|
paddings = 2,
|
|
|
|
widget = wibox.container.arcchart,
|
|
|
|
set_value = function(self, level)
|
|
|
|
self:set_value(level)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
else
|
|
|
|
show_warning(type .. " type is not supported by the widget")
|
|
|
|
return
|
|
|
|
|
|
|
|
end
|
2019-05-03 03:35:30 +02:00
|
|
|
|
|
|
|
local update_widget = function(widget, stdout, _, _, _)
|
|
|
|
local brightness_level = tonumber(string.format("%.0f", stdout))
|
2021-04-16 22:32:32 +02:00
|
|
|
current_level = brightness_level
|
2021-01-16 20:50:54 +01:00
|
|
|
widget:set_value(brightness_level)
|
|
|
|
end
|
|
|
|
|
2021-04-15 23:01:12 +02:00
|
|
|
function brightness_widget:set(value)
|
2021-04-16 22:32:32 +02:00
|
|
|
current_level = value
|
2021-10-16 19:53:27 +02:00
|
|
|
spawn.easy_async(string.format(set_brightness_cmd, value), function()
|
2021-04-15 23:01:12 +02:00
|
|
|
spawn.easy_async(get_brightness_cmd, function(out)
|
|
|
|
update_widget(brightness_widget.widget, out)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
2021-04-15 23:38:19 +02:00
|
|
|
local old_level = 0
|
|
|
|
function brightness_widget:toggle()
|
2022-09-17 11:44:26 +02:00
|
|
|
if rmb_set_max then
|
|
|
|
brightness_widget:set(100)
|
2021-04-15 23:38:19 +02:00
|
|
|
else
|
2022-09-17 11:44:26 +02:00
|
|
|
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)
|
2021-04-15 23:38:19 +02:00
|
|
|
end
|
|
|
|
end
|
2021-01-16 20:50:54 +01:00
|
|
|
function brightness_widget:inc()
|
|
|
|
spawn.easy_async(inc_brightness_cmd, function()
|
|
|
|
spawn.easy_async(get_brightness_cmd, function(out)
|
|
|
|
update_widget(brightness_widget.widget, out)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
function brightness_widget:dec()
|
|
|
|
spawn.easy_async(dec_brightness_cmd, function()
|
|
|
|
spawn.easy_async(get_brightness_cmd, function(out)
|
|
|
|
update_widget(brightness_widget.widget, out)
|
|
|
|
end)
|
|
|
|
end)
|
2020-12-06 20:47:40 +01:00
|
|
|
end
|
2019-05-03 03:35:30 +02:00
|
|
|
|
2021-01-16 20:50:54 +01:00
|
|
|
brightness_widget.widget:buttons(
|
|
|
|
awful.util.table.join(
|
2021-04-15 23:01:12 +02:00
|
|
|
awful.button({}, 1, function() brightness_widget:set(base) end),
|
2021-04-15 23:38:19 +02:00
|
|
|
awful.button({}, 3, function() brightness_widget:toggle() end),
|
2021-01-16 20:50:54 +01:00
|
|
|
awful.button({}, 4, function() brightness_widget:inc() end),
|
|
|
|
awful.button({}, 5, function() brightness_widget:dec() end)
|
|
|
|
)
|
|
|
|
)
|
2019-05-03 03:35:30 +02:00
|
|
|
|
2021-01-16 20:50:54 +01:00
|
|
|
watch(get_brightness_cmd, timeout, update_widget, brightness_widget.widget)
|
2019-05-03 03:35:30 +02:00
|
|
|
|
2021-04-16 00:03:13 +02:00
|
|
|
if tooltip then
|
|
|
|
awful.tooltip {
|
|
|
|
objects = { brightness_widget.widget },
|
|
|
|
timer_function = function()
|
2021-04-16 22:32:32 +02:00
|
|
|
return current_level .. " %"
|
2021-04-16 00:03:13 +02:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-01-16 20:50:54 +01:00
|
|
|
return brightness_widget.widget
|
2019-05-03 03:35:30 +02:00
|
|
|
end
|
|
|
|
|
2022-03-22 17:13:05 +01:00
|
|
|
return setmetatable(brightness_widget, {
|
|
|
|
__call = function(_, ...)
|
|
|
|
return worker(...)
|
|
|
|
end,
|
|
|
|
})
|