Actualizacion maquina principal
[dotfiles/.git] / .config / awesome / lain / widget / contrib / redshift.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2017, Luca CPZ
5       * (c) 2014, blueluke <http://github.com/blueluke>
6
7 --]]
8
9 local async   = require("lain.helpers").async
10 local awful   = require("awful")
11 local execute = os.execute
12 local type    = type
13
14 -- Redshift
15 -- lain.widget.contrib.redshift
16 local redshift = { active = false, pid = nil }
17
18 function redshift:start()
19     execute("pkill redshift")
20     awful.spawn.with_shell("redshift -x") -- clear adjustments
21     redshift.pid = awful.spawn.with_shell("redshift")
22     redshift.active = true
23     if type(redshift.update_fun) == "function" then
24         redshift.update_fun(redshift.active)
25     end
26 end
27
28 function redshift:toggle()
29     async({ awful.util.shell, "-c", string.format("ps -p %d -o pid=", redshift.pid) }, function(f)
30         if f and #f > 0 then -- redshift is running
31             -- Sending -USR1 toggles redshift (See project website)
32             execute("pkill -USR1 redshift")
33             redshift.active = not redshift.active
34         else -- not started or killed, (re)start it
35             redshift:start()
36         end
37         redshift.update_fun(redshift.active)
38     end)
39 end
40
41 -- Attach to a widget
42 -- Provides a button which toggles redshift on/off on click
43 -- @param widget:  Widget to attach to.
44 -- @param fun:     Function to be run each time redshift is toggled (optional).
45 --                 Use it to update widget text or icons on status change.
46 function redshift:attach(widget, fun)
47     redshift.update_fun = fun or function() end
48     if not redshift.pid then redshift:start() end
49     if widget then
50         widget:buttons(awful.util.table.join(awful.button({}, 1, function () redshift:toggle() end)))
51     end
52 end
53
54 return redshift