Debian Codegraph @

obvious/lib/hooks/init.lua

raw symbols SCIP
1 ------------------------------------------
2 -- Author: Andrei "Garoth" Thorp --
3 -- Copyright 2009 Andrei "Garoth" Thorp --
4 ------------------------------------------
5 -- Awful Timer Wrapper With Speed Controls
6
7 local pairs = pairs
8 local gears_timer = require('gears').timer
9
10 local timer = {}
11 local hooks = {
12 timer = timer
13 }
14
15 local registry = {}
16
17 -- Register a timer just as you would with awful.hooks.timer.register, but
18 -- with one slight twist: you set your regular speed, your slow
19 -- speed, and then your function.
20 -- @param reg_time Regular speed for the widget
21 -- @param slow_time (Optional) Slow time for the widget
22 -- @param fn The function that the timer should call
23 -- @param descr (Optional) The description of this function
24 function hooks.timer.register(reg_time, slow_time, fn, descr)
25 if not slow_time then slow_time = reg_time * 4 end
26 registry[fn] = {
27 regular=reg_time,
28 slow=slow_time,
29 speed="regular",
30 description=descr or "Undescribed timer",
31 timer=gears_timer({ timeout = 600 })
32 }
33 registry[fn].timer:connect_signal("timeout", fn)
34 -- set_speed() will :stop() again and start with the real timeout
35 registry[fn].timer:start()
36 timer.set_speed(registry[fn].speed, fn)
37 end
38
39 -- Unregister the timer altogether
40 -- Note: It's possible to pause the timer with timer.stop() instead.
41 -- @param fn The function you want to unregister.
42 function hooks.timer.unregister(fn)
43 if not registry[fn] then
44 return
45 end
46 if registry[fn].timer.started then
47 registry[fn].timer:stop()
48 end
49 registry[fn] = nil
50 end
51
52 -- Switch timers between "slow" and "regular" speeds.
53 -- @param speed The speed at which you want the function(s) to run: one of
54 -- "regular" or "slow"
55 -- @param fn (Optional) Function that you want to set to some speed. If not
56 -- not specified, set all timers to the given speed.
57 function hooks.timer.set_speed(speed, fn)
58 if fn then
59 local obj = registry[fn]
60 obj.speed = speed
61 if not registry[fn].timer.started then
62 return
63 end
64 obj.timer:stop()
65 local timeout = nil
66 if speed == "regular" then
67 timeout = obj.regular
68 elseif speed == "slow" then
69 timeout = obj.slow
70 end
71 if timeout then
72 obj.timer.timeout = timeout
73 obj.timer:start()
74 end
75 else
76 for key, value in pairs(registry) do
77 timer.set_speed(speed, key)
78 end
79 end
80 end
81
82 -- Edit a function's speeds.
83 -- @param reg_time Regular speed for the function
84 -- @param slow_time Slow speed for the function
85 -- @param fn Function that you want to alter the speed of
86 function hooks.timer.set_speeds(reg_time, slow_time, fn)
87 registry[fn] = {
88 regular=reg_time,
89 slow=slow_time,
90 speed=registry[fn].speed,
91 timer=registry[fn].timer,
92 }
93 timer.set_speed(registry[fn].speed, fn)
94 end
95
96 -- Returns the speeds for the timer(s).
97 -- @param fn (Optional) Function that you want to know the data for. If not
98 -- specified, this returns a table of all registered timers with their data.
99 -- @return A table with the regular speed, the slow speed, the currently used
100 -- speed, whether the timer is running or not, and the description.
101 -- Note: This function returns a copy of the internal registry, so assigning to
102 -- it doesn't work.
103 function hooks.timer.get_speeds(fn)
104 local copy = {}
105 if fn then
106 for key, value in pairs(registry[fn]) do
107 copy[key] = value
108 end
109 else
110 for key, value in pairs(registry) do
111 local subcopy = {}
112 for subkey, subvalue in pairs(registry[key]) do
113 subcopy[subkey] = subvalue
114 end
115 copy[key] = subcopy
116 end
117 end
118 return copy
119 end
120
121 -- Pause timer(s)
122 -- @param fn (Optional) Function to pause the timer for. If none is specified,
123 -- this pauses all registered timers.
124 function hooks.timer.stop(fn)
125 if fn then
126 registry[fn].timer:stop()
127 else
128 for key, value in pairs(registry) do
129 registry[fn].timer:stop()
130 end
131 end
132 end
133
134 -- Start timer(s)
135 -- @param fn (Optional) Function to start the timer for. If none is specified,
136 -- this starts all registered timers.
137 function hooks.timer.start(fn)
138 if fn then
139 if not registry[fn].timer.started then
140 registry[fn].timer:start()
141 end
142 timer.set_speed(registry[fn].speed, fn)
143 else
144 for key, value in pairs(registry) do
145 if not registry[fn].timer.started then
146 registry[fn].timer:start()
147 end
148 timer.set_speed(registry[key].speed, fn)
149 end
150 end
151 end
152
153 -- Checks whether the given function is registered
154 -- @return boolean true/false of whether the function is registered
155 function hooks.timer.has(fn)
156 if registry[fn] then
157 return true
158 else
159 return false
160 end
161 end
162 return hooks
163 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et