vicious/init.lua

242 lines
5.5 KiB
Lua
Raw Normal View History

2009-11-04 23:39:38 +01:00
---------------------------------------------------
-- Vicious widgets for the awesome window manager
2009-11-04 23:39:38 +01:00
---------------------------------------------------
2009-09-29 22:33:19 +02:00
-- Licensed under the GNU General Public License v2
2010-01-02 21:21:54 +01:00
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
-- * (c) 2009, Lucas de Vries <lucas@glacicle.com>
2009-11-04 23:39:38 +01:00
---------------------------------------------------
-- {{{ Setup environment
local type = type
local pairs = pairs
local tonumber = tonumber
local capi = { timer = timer }
local os = { time = os.time }
local table = {
insert = table.insert,
remove = table.remove
}
2009-09-29 22:46:33 +02:00
require("vicious.helpers")
require("vicious.widgets")
-- Vicious: widgets for the awesome window manager
module("vicious")
-- Initialize tables
local timers = {}
local registered = {}
local widget_cache = {}
-- }}}
-- {{{ Local functions
-- {{{ Update a widget
local function update(widget, reg, disablecache)
-- Check if there are any equal widgets
if reg == nil then
for w, i in pairs(registered) do
if w == widget then
for _, r in pairs(i) do
update(w, r, disablecache)
end
end
end
return
end
local t = os.time()
local data = {}
-- Check for chached output newer than the last update
if widget_cache[reg.wtype] ~= nil then
local c = widget_cache[reg.wtype]
if (c.time == nil or c.time <= t-reg.timer) or disablecache then
c.time, c.data = t, reg.wtype(reg.format, reg.warg)
end
data = c.data
else
data = reg.wtype and reg.wtype(reg.format, reg.warg)
end
if type(data) == "table" then
if type(reg.format) == "string" then
data = helpers.format(reg.format, data)
elseif type(reg.format) == "function" then
data = reg.format(widget, data)
end
end
if widget.add_value ~= nil then
widget:add_value(tonumber(data) and tonumber(data)/100)
elseif widget.set_value ~= nil then
widget:set_value(tonumber(data) and tonumber(data)/100)
else
widget.text = data
end
return data
end
-- }}}
-- {{{ Register from reg object
local function regregister(reg)
if not reg.running then
if registered[reg.widget] == nil then
registered[reg.widget] = {}
table.insert(registered[reg.widget], reg)
else
2009-09-14 17:25:23 +02:00
local already = false
for w, i in pairs(registered) do
if w == reg.widget then
2009-08-08 17:42:28 +02:00
for _, v in pairs(i) do
if v == reg then
already = true
break
end
end
if already then
break
end
end
end
if not already then
table.insert(registered[reg.widget], reg)
end
end
2009-09-14 17:25:23 +02:00
-- Start the timer
if reg.timer > 0 then
timers[reg.update] = {
timer = capi.timer({ timeout = reg.timer })
}
timers[reg.update].timer:add_signal("timeout", reg.update)
timers[reg.update].timer:start()
end
-- Initial update
reg.update()
reg.running = true
end
end
-- }}}
-- }}}
-- {{{ Global functions
-- {{{ Register a widget
function register(widget, wtype, format, timer, warg)
local reg = {}
local widget = widget
-- Set properties
reg.wtype = wtype
reg.format = format
reg.timer = timer
reg.warg = warg
reg.widget = widget
-- Update function
reg.update = function ()
update(widget, reg)
end
-- Default to 2s timer
if reg.timer == nil then
reg.timer = 2
end
-- Register a reg object
regregister(reg)
-- Return a reg object for reuse
return reg
end
-- }}}
2009-09-14 17:25:23 +02:00
-- {{{ Unregister a widget
function unregister(widget, keep, reg)
if reg == nil then
for w, i in pairs(registered) do
if w == widget then
2009-08-08 17:42:28 +02:00
for _, v in pairs(i) do
reg = unregister(w, keep, v)
end
end
end
return reg
end
if not keep then
for w, i in pairs(registered) do
if w == widget then
for k, v in pairs(i) do
if v == reg then
table.remove(registered[w], k)
end
end
end
end
end
2009-09-14 17:25:23 +02:00
-- Stop the timer
if timers[reg.update].timer.started then
timers[reg.update].timer:stop()
end
reg.running = false
return reg
end
-- }}}
-- {{{ Enable caching of a widget type
function cache(wtype)
if wtype ~= nil then
if widget_cache[wtype] == nil then
widget_cache[wtype] = {}
end
end
end
-- }}}
-- {{{ Force update of widgets
function force(wtable)
if type(wtable) == "table" then
for _, w in pairs(wtable) do
update(w, nil, true)
end
end
end
-- }}}
-- {{{ Suspend all widgets
function suspend()
for w, i in pairs(registered) do
2009-08-08 17:42:28 +02:00
for _, v in pairs(i) do
unregister(w, true, v)
end
end
end
-- }}}
-- {{{ Activate a widget
function activate(widget)
for w, i in pairs(registered) do
if widget == nil or w == widget then
2009-08-08 17:42:28 +02:00
for _, v in pairs(i) do
regregister(v)
end
end
end
end
-- }}}
-- }}}