lain/widget/contrib/redshift.lua

60 lines
1.8 KiB
Lua
Raw Normal View History

2014-05-27 15:54:37 +02:00
--[[
Licensed under GNU General Public License v2
* (c) 2017, Luca CPZ
* (c) 2014, blueluke <http://github.com/blueluke>
2014-05-27 15:54:37 +02:00
--]]
local requirePrefix = tostring(...):match(".*lain") or ""
if requirePrefix then
requirePrefix = requirePrefix .. "."
end
local async = require(requirePrefix .. ".helpers").async
2017-01-22 01:15:57 +01:00
local awful = require("awful")
local execute = os.execute
local type = type
2014-05-27 15:54:37 +02:00
2015-07-03 12:17:31 +02:00
-- Redshift
2017-02-08 14:15:48 +01:00
-- lain.widget.contrib.redshift
2017-01-22 01:15:57 +01:00
local redshift = { active = false, pid = nil }
2020-11-29 22:48:32 +01:00
function redshift.start()
2017-01-22 01:15:57 +01:00
execute("pkill redshift")
awful.spawn.with_shell("redshift -x") -- clear adjustments
redshift.pid = awful.spawn.with_shell("redshift")
redshift.active = true
if type(redshift.update_fun) == "function" then
redshift.update_fun(redshift.active)
2014-05-27 15:54:37 +02:00
end
end
2020-11-29 22:48:32 +01:00
function redshift.toggle()
async({ awful.util.shell, "-c", string.format("ps -p %d -o pid=", redshift.pid) }, function(f)
2017-01-22 01:15:57 +01:00
if f and #f > 0 then -- redshift is running
-- Sending -USR1 toggles redshift (See project website)
execute("pkill -USR1 redshift")
redshift.active = not redshift.active
else -- not started or killed, (re)start it
2020-11-29 22:48:32 +01:00
redshift.start()
2017-01-22 01:15:57 +01:00
end
redshift.update_fun(redshift.active)
end)
2014-05-27 15:54:37 +02:00
end
-- Attach to a widget
-- Provides a button which toggles redshift on/off on click
2015-07-03 12:17:31 +02:00
-- @param widget: Widget to attach to.
2017-01-22 01:15:57 +01:00
-- @param fun: Function to be run each time redshift is toggled (optional).
2015-07-03 12:17:31 +02:00
-- Use it to update widget text or icons on status change.
2020-11-29 22:48:32 +01:00
function redshift.attach(widget, fun)
2017-01-22 01:15:57 +01:00
redshift.update_fun = fun or function() end
2020-11-29 22:48:32 +01:00
if not redshift.pid then redshift.start() end
if widget then
2020-11-29 22:48:32 +01:00
widget:buttons(awful.util.table.join(awful.button({}, 1, function () redshift.toggle() end)))
end
2014-05-27 15:54:37 +02:00
end
2017-01-22 01:15:57 +01:00
return redshift