]> 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:

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