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

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