Debian Codegraph @

vicious/init.lua

raw symbols SCIP
1 -- Vicious module initialization
2 -- Copyright (C) 2009 Lucas de Vries <[email protected]>
3 -- Copyright (C) 2009-2013 Adrian C. (anrxc) <[email protected]>
4 -- Copyright (C) 2011-2017 Joerg Thalheim <[email protected]>
5 -- Copyright (C) 2012 Arvydas Sidorenko <[email protected]>
6 -- Copyright (C) 2013 Dodo <[email protected]>
7 -- Copyright (C) 2014 blastmaster <[email protected]>
8 -- Copyright (C) 2015,2019 Daniel Hahler <[email protected]>
9 -- Copyright (C) 2017 James Reed <[email protected]>
10 -- Copyright (C) 2017 getzze <[email protected]>
11 -- Copyright (C) 2017 mutlusun <[email protected]>
12 -- Copyright (C) 2018 Beniamin Kalinowski <[email protected]>
13 -- Copyright (C) 2018,2020 Nguyễn Gia Phong <[email protected]g>
14 -- Copyright (C) 2022 Constantin Piber <[email protected]>
15 --
16 -- This file is part of Vicious.
17 --
18 -- Vicious is free software: you can redistribute it and/or modify
19 -- it under the terms of the GNU General Public License as
20 -- published by the Free Software Foundation, either version 2 of the
21 -- License, or (at your option) any later version.
22 --
23 -- Vicious is distributed in the hope that it will be useful,
24 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
25 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 -- GNU General Public License for more details.
27 --
28 -- You should have received a copy of the GNU General Public License
29 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
30
31 -- {{{ Setup environment
32 local type = type
33 local pairs = pairs
34 local tonumber = tonumber
35 local timer = type(timer) == "table" and timer or require("gears.timer")
36 local os = { time = os.time }
37 local table = {
38 insert = table.insert,
39 remove = table.remove
40 }
41 local helpers = require("vicious.helpers")
42 local dstatus, debug = pcall(require, "gears.debug")
43 local stderr = io.stderr
44 local warn
45 if dstatus then
46 warn = debug.print_warning
47 else
48 warn = function (msg) stderr:write("Warning (vicious): ", msg, "\n") end
49 end
50
51 -- Vicious: widgets for the awesome window manager
52 local vicious = {}
53 vicious.widgets = require("vicious.widgets")
54 --vicious.contrib = require("vicious.contrib")
55
56 -- Initialize tables
57 local timers = {}
58 local registered = {}
59 local widget_cache = {}
60 -- }}}
61
62
63 -- {{{ Local functions
64 -- {{{ Update a widget
65 local function update(widget, reg, disablecache)
66 -- Check if there are any equal widgets
67 if reg == nil then
68 for w, i in pairs(registered) do
69 if w == widget then
70 for _, r in pairs(i) do
71 update(w, r, disablecache)
72 end
73 end
74 end
75
76 return
77 end
78
79 local update_time = os.time()
80
81 local function format_data(data)
82 local ret
83 if type(data) == "table" then
84 local escaped_data = {}
85 for k, v in pairs(data) do
86 if type(v) == "string" then
87 escaped_data[k] = helpers.escape(v)
88 else
89 escaped_data[k] = v
90 end
91 end
92
93 if type(reg.format) == "string" then
94 ret = helpers.format(reg.format, escaped_data)
95 elseif type(reg.format) == "function" then
96 ret = reg.format(widget, escaped_data)
97 end
98 end
99 return ret or data
100 end
101
102 local function topercent(e) return tonumber(e) and tonumber(e) / 100 end
103
104 local function update_value(data)
105 local fmtd_data = format_data(data)
106 if widget.add_value ~= nil then
107 if widget.get_stack ~= nil and widget:get_stack() then
108 for idx, _ in ipairs(widget:get_stack_colors()) do
109 if fmtd_data[idx] then
110 widget:add_value(topercent(fmtd_data[idx]), idx)
111 end
112 end
113 else
114 widget:add_value(topercent(fmtd_data))
115 end
116 elseif widget.set_value ~= nil then
117 widget:set_value(topercent(fmtd_data))
118 elseif widget.set_markup ~= nil then
119 widget:set_markup(fmtd_data)
120 else
121 widget.text = fmtd_data
122 end
123 end
124
125 local function update_cache(data, t, cache)
126 -- Update cache
127 if t and cache then
128 cache.time, cache.data = t, data
129 end
130 end
131
132 -- Check for cached output newer than the last update
133 local c = widget_cache[reg.wtype]
134 if c and update_time < c.time + reg.timeout and not disablecache then
135 update_value(c.data)
136 elseif reg.wtype then
137 if type(reg.wtype) == "table" and reg.wtype.async then
138 if not reg.lock then
139 reg.lock = true
140 return reg.wtype.async(reg.format,
141 reg.warg,
142 function(data)
143 update_cache(data, update_time, c)
144 local status, res = pcall(update_value, data)
145 if not status then warn(res) end
146 reg.lock=false
147 end)
148 end
149 else
150 local data = reg.wtype(reg.format, reg.warg)
151 update_cache(data, update_time, c)
152 update_value(data)
153 end
154 end
155 end
156 -- }}}
157
158 -- {{{ Register from reg object
159 local function regregister(reg)
160 if not reg.running then
161 if registered[reg.widget] == nil then
162 registered[reg.widget] = {}
163 table.insert(registered[reg.widget], reg)
164 else
165 local already = false
166
167 for w, i in pairs(registered) do
168 if w == reg.widget then
169 for _, v in pairs(i) do
170 if v == reg then
171 already = true
172 break
173 end
174 end
175
176 if already then
177 break
178 end
179 end
180 end
181
182 if not already then
183 table.insert(registered[reg.widget], reg)
184 end
185 end
186
187 -- Start the timer
188 if reg.timeout > 0 then
189 local tm = timers[reg.timeout] and timers[reg.timeout].timer
190 tm = tm or timer({ timeout = reg.timeout })
191 if tm.connect_signal then
192 tm:connect_signal("timeout", reg.update)
193 else
194 tm:add_signal("timeout", reg.update)
195 end
196 if not timers[reg.timeout] then
197 timers[reg.timeout] = { timer = tm, refs = 1 }
198 else
199 timers[reg.timeout].refs = timers[reg.timeout].refs + 1
200 end
201 if not tm.started then
202 tm:start()
203 end
204 -- Initial update
205 reg.update()
206 end
207 reg.running = true
208 end
209 end
210 -- }}}
211 -- }}}
212
213
214 -- {{{ Global functions
215 -- {{{ Register a widget
216 function vicious.register(widget, wtype, format, timeout, warg)
217 local reg = {
218 -- Set properties
219 wtype = wtype,
220 lock = false,
221 format = format,
222 timeout = timeout or 2,
223 warg = warg,
224 widget = widget,
225 }
226 reg.timer = timeout -- For backward compatibility.
227
228 -- Set functions
229 function reg.update()
230 update(widget, reg)
231 end
232
233 -- Register a reg object
234 regregister(reg)
235
236 -- Return a reg object for reuse
237 return reg
238 end
239 -- }}}
240
241 -- {{{ Unregister a widget
242 function vicious.unregister(widget, keep, reg)
243 if reg == nil then
244 for w, i in pairs(registered) do
245 if w == widget then
246 for _, v in pairs(i) do
247 reg = vicious.unregister(w, keep, v)
248 end
249 end
250 end
251
252 return reg
253 end
254
255 if not keep then
256 for w, i in pairs(registered) do
257 if w == widget then
258 for k, v in pairs(i) do
259 if v == reg then
260 table.remove(registered[w], k)
261 end
262 end
263 end
264 end
265 end
266
267 if not reg.running then
268 return reg
269 end
270
271 -- Disconnect from timer
272 local tm = timers[reg.timeout]
273 if tm.timer.disconnect_signal then
274 tm.timer:disconnect_signal("timeout", reg.update)
275 else
276 tm.timer:remove_signal("timeout", reg.update)
277 end
278 reg.running = false
279 -- Stop the timer
280 tm.refs = tm.refs - 1
281 if tm.refs == 0 and tm.timer.started then
282 tm.timer:stop()
283 end
284
285 return reg
286 end
287 -- }}}
288
289 -- {{{ Enable caching of a widget type
290 function vicious.cache(wtype)
291 if wtype ~= nil then
292 if widget_cache[wtype] == nil then
293 widget_cache[wtype] = { data = nil, time = 0 }
294 end
295 end
296 end
297 -- }}}
298
299 -- {{{ Force update of widgets
300 function vicious.force(wtable)
301 if type(wtable) == "table" then
302 for _, w in pairs(wtable) do
303 update(w, nil, true)
304 end
305 end
306 end
307 -- }}}
308
309 -- {{{ Suspend all widgets
310 function vicious.suspend()
311 for w, i in pairs(registered) do
312 for _, v in pairs(i) do
313 vicious.unregister(w, true, v)
314 end
315 end
316 end
317 -- }}}
318
319 -- {{{ Activate a widget
320 function vicious.activate(widget)
321 for w, i in pairs(registered) do
322 if widget == nil or w == widget then
323 for _, v in pairs(i) do
324 regregister(v)
325 end
326 end
327 end
328 end
329 -- }}}
330
331 -- {{{ Get formatted data from a synchronous widget type
332 function vicious.call(wtype, format, warg)
333 if wtype.async ~= nil then return nil end
334
335 local data = wtype(format, warg)
336 if type(format) == "string" then
337 return helpers.format(format, data)
338 elseif type(format) == "function" then
339 return format(wtype, data)
340 end
341 end
342 -- }}}
343
344 -- {{{ Get formatted data from an asynchronous widget type
345 function vicious.call_async(wtype, format, warg, callback)
346 if wtype.async == nil then
347 callback()
348 return
349 end
350
351 wtype.async(
352 format, warg,
353 function (data)
354 if type(format) == "string" then
355 callback(helpers.format(format, data))
356 elseif type(format) == "function" then
357 callback(format(wtype, data))
358 else
359 callback()
360 end
361 end)
362 end
363 -- }}}
364
365 return vicious
366 -- }}}