2014-05-27 15:54:37 +02:00
|
|
|
--[[
|
2017-09-04 12:43:00 +02:00
|
|
|
|
|
|
|
Licensed under GNU General Public License v2
|
2018-11-28 20:43:04 +01:00
|
|
|
* (c) 2017, Luca CPZ
|
2017-09-04 12:43:00 +02:00
|
|
|
* (c) 2014, blueluke <http://github.com/blueluke>
|
|
|
|
|
2014-05-27 15:54:37 +02:00
|
|
|
--]]
|
|
|
|
|
2017-01-22 01:15:57 +01:00
|
|
|
local async = require("lain.helpers").async
|
|
|
|
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()
|
2017-01-23 23:03:13 +01:00
|
|
|
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
|
2017-01-23 20:58:19 +01:00
|
|
|
if widget then
|
2020-11-29 22:48:32 +01:00
|
|
|
widget:buttons(awful.util.table.join(awful.button({}, 1, function () redshift.toggle() end)))
|
2017-01-23 20:58:19 +01:00
|
|
|
end
|
2014-05-27 15:54:37 +02:00
|
|
|
end
|
|
|
|
|
2017-01-22 01:15:57 +01:00
|
|
|
return redshift
|