]> git.madduck.net Git - etc/awesome.git/blob - widgets/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:

pulsebar: corrected colors typo; closes #251
[etc/awesome.git] / widgets / contrib / redshift.lua
1
2 --[[
3                                                         
4      Licensed under GNU General Public License v2       
5       * (c) 2014, blueluke <http://github.com/blueluke> 
6                                                         
7 --]]
8
9 local awful        = require("awful")
10 local os           = os
11 local spawn        = awful.util.spawn_with_shell
12
13 local setmetatable = setmetatable
14
15 -- Redshift
16 -- lain.widgets.contrib.redshift
17 local redshift = {}
18
19 local attached    = false           -- true if attached to a widget
20 local active      = false           -- true if redshift is active
21 local running     = false           -- true if redshift was initialized
22 local update_fnct = function() end  -- Function that is run each time redshift is toggled. See redshift:attach().
23
24 local function init()
25     -- As there is no way to determine if redshift was previously
26     -- toggled off (i.e Awesome on-the-fly restart), kill redshift to make sure
27     os.execute("pkill redshift")
28     -- Remove existing color adjustment
29     spawn("redshift -x")
30     -- (Re)start redshift
31     spawn("redshift")
32     running = true
33     active = true
34 end
35
36 function redshift:toggle()
37     if running then
38         -- Sending -USR1 toggles redshift (See project website)
39         os.execute("pkill -USR1 redshift")
40         active = not active
41     else
42         init()
43     end
44     update_fnct()
45 end
46
47 function redshift:off()
48     if running and active then
49         redshift:toggle()
50     end
51 end
52
53 function redshift:on()
54     if not active then
55         redshift:toggle()
56     end
57 end
58
59 function redshift:is_active()
60     return active
61 end
62
63 -- Attach to a widget
64 -- Provides a button which toggles redshift on/off on click
65 -- @param widget:  Widget to attach to.
66 -- @param fnct:    Function to be run each time redshift is toggled (optional).
67 --                 Use it to update widget text or icons on status change.
68 function redshift:attach(widget, fnct)
69     update_fnct  = fnct or function() end
70     if not attached then
71         init()
72         attached = true
73         update_fnct()
74     end
75     widget:buttons(awful.util.table.join( awful.button({}, 1, function () redshift:toggle() end) ))
76 end
77
78 return setmetatable(redshift, { _call = function(_, ...) return create(...) end })