awesomewm/classes/backlight/backlight.lua

132 lines
2.6 KiB
Lua

local widget_types = {
arc = require("classes/backlight/arc_widget")
}
local P = {
name = "backlight"
}
P.__index = P
---
-- Internal methods
---
function P:_get_status()
awful.spawn.easy_async(
"xbacklight -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 " .. math.floor(self.brightness) .. "%");
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("xbacklight -inc 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("xbacklight -dec 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("xbacklight -set " .. value, false)
self.brightness = value
self:_update_widget()
end
---
-- Create new
---
function P:new(args)
-- Arguments
local b = {
update_interval = args.update_interal or 5,
min_value = args.min_value or 1,
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()
}
setmetatable(b, self)
b._widget = widget_types[b.widget_type]:new()
b.widget = b._widget.widget
b.widget:connect_signal("button::press",
function(_, _, _, button, mods)
if (button == 4) then -- Scroll up
b:backlight_up()
elseif (button == 5) then -- Scroll down
b:backlight_down()
end
end
)
-- 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