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