]> git.madduck.net Git - etc/awesome.git/blob - widget/contrib/redshift.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge pull request #420 from sim590/unpack-lua51-compat
[etc/awesome.git] / 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