Debian Codegraph @

tyrannical/init.lua

raw symbols SCIP
1 local setmetatable = setmetatable
2 local print , pairs = print , pairs
3 local ipairs , type = ipairs , type
4 local string , unpack= string , unpack or table.unpack
5 local awful = require("awful")
6
7 local capi,sn_callback = {client = client, tag = tag, awesome = awesome,
8 screen = screen, mouse = mouse}, awful.spawn.snid_buffer or {}
9
10 -------------------------------INIT------------------------------
11
12 local module,c_rules,tags_hash,settings,fallbacks = {},{class={},instance={}},{},{tag={},client={}},{}
13
14 ----------------------TYRANNICAL LOGIC--------------------------
15
16 --Called when a tag is selected/unselected
17 local function on_selected_change(tag,data)
18 if data and data.exec_once and tag.selected then
19 for _,v in ipairs(type(data.exec_once) == "string" and {data.exec_once} or data.exec_once) do
20 awful.spawn.single_instance(v)
21 end
22 end
23 end
24
25 local function get_class(c)
26 return awful.client.property.get(c, "overwrite_class") or c.class or "N/A"
27 end
28
29 local function get_screen_idx(s)
30 return type(s) == "number" and s or s.index
31 end
32
33 local function scr_exists(s)
34 local t = type(s)
35 return (t == "number" and s > 0 and s <= capi.screen.count())
36 or t == "screen" or (t == "table" and s.workarea)
37 end
38
39 --Load tags, this cannot be undone
40 local function load_tags(tyrannical_tags)
41 for k,v in ipairs(tyrannical_tags) do
42 if v.init ~= false then
43 if type(v.screen) == "table" then
44 local screens = v.screen --TODO remove
45 for k2,v2 in pairs(screens) do
46 if (type(v2) == "number" and v2 or v2.index) <= capi.screen.count() then
47 v.screen = v2 --TODO remove
48 awful.tag.add(v.name,v,{screen = v2}).is_template = true
49 end
50 end
51 v.screen = screens --TODO remove
52 elseif (v.screen and (type(v.screen) == "number" and v.screen or v.screen.index) or 1) <= capi.screen.count() then
53 awful.tag.add(v.name,v).is_template = true
54 end
55 elseif v.volatile == nil then
56 v.volatile = true
57 end
58 for _,prop in ipairs {"class","instance"} do
59 if v[prop] and c_rules[prop] then
60 -- for low in (function() local i=0; return function() i=i+1; return prop[i] and prop[i]:lower() end end)() do --TODO fix
61 for i=1,#v[prop] do
62 local low = string.lower(v[prop][i])
63 local tmp = c_rules[prop][low] or {tags={},properties={}}
64 tmp.tags[#tmp.tags+1] = awful.util.table.hasitem(tmp.tags,v) == nil and v or nil --Avoid duplicates
65 c_rules[prop][low] = tmp
66 end
67 end
68 end
69 tags_hash[v.name or "N/A"] = v
70 end
71 end
72
73 --Load property
74 local function load_property(name,property)
75 for k2,v2 in pairs(property) do --TODO make an iterator?
76 local key_type = type(k2)
77 local low = string.lower(key_type == "number" and v2 or k2)
78 c_rules.class[low] = c_rules.class[low] or {name=low,tags={},properties={}}
79 c_rules.class[low].properties[name] = key_type == "number" and true or v2
80 end
81 end
82
83 local function has_selected(tags, screen)
84 if #tags == 0 then return false end
85
86 for _, t in ipairs(screen.selected_tags) do
87 if awful.util.table.hasitem(tags, t) then return true end
88 end
89
90 return false
91 end
92
93 function awful.rules.delayed_properties.master(c, value)
94 if not value then return end
95
96 awful.client.setmaster(c)
97
98 -- Some smarter layouts may implement this directly
99 c.master = true
100 end
101
102 function awful.rules.delayed_properties.slave(c, value)
103 if not value then return end
104
105 awful.client.setslave(c)
106
107 -- Some smarter layouts may implement this directly
108 c.slave = true
109 end
110
111 function module.focus_client(c,properties)
112
113 if (((not c.transient_for) or (c.transient_for==capi.client.focus) or (not settings.block_children_focus_stealing)) and (not c.no_autofocus)) then
114 local tags = c:tags()
115
116 if #tags > 0 and not has_selected(tags, c.screen) and not tags[1].no_focus_stealing_in then
117 local t = c:tags()[1]
118 if t.no_tag_deselect then
119 t.selected = true
120 else
121 t:view_only()
122 end
123 end
124
125 capi.client.focus = c
126 c:raise()
127 return true
128 end
129 end
130
131 --Apply all properties
132 local function apply_properties(c, props, callbacks)
133
134 local force_intrusive = settings.force_odd_as_intrusive
135 and c.type ~= "normal"
136
137 local is_intrusive = force_intrusive
138 or type(props.intrusive) == "function" and props.intrusive(c)
139 or props.intrusive
140
141 if props.tag or props.tags or props.new_tag then
142 is_intrusive = false
143 end
144
145 local has_tag = props.tag or props.new_tag or props.tags
146 --Check if the client should be added to an existing tag (or tags)
147 if (not has_tag) and is_intrusive then
148 local tag = capi.mouse.screen.selected_tag
149 or capi.mouse.screen.tags[1]
150
151 if tag and not tag.selected then
152 tag:view_only()
153 end
154
155 if tag then --Can be false if there is no tags
156 props.tag, props.tags, props.intrusive = tag, nil, false
157 end
158 end
159
160 awful.rules.execute(c, props, callbacks)
161 end
162
163 local function select_screen(tag)
164 local s
165
166 -- If there is a table of screen, check if it contains the mouse one
167 if type(tag.screen) =="table" and tag.screen[1] then
168 for k, ss in ipairs(tag.screen) do
169 ss = type(ss) == "number" and ss <= capi.screen.count() and ss or nil
170 if ss and capi.screen[ss] == awful.screen.focused() then
171 s = ss
172 end
173 end
174 s = s or capi.screen[tag.screen[1]]
175 else
176 s = scr_exists(tag.screen) and capi.screen[tag.screen] or nil
177 end
178
179 -- If the tag.force_screen is set, then obey
180 if (tag.force_screen and s) or (s and settings.favor_focused == false) then
181 return s
182 end
183
184 -- By default, Tyrannical prefer to use the focused screen to place new tags
185 -- This override some other settings, but is more pleasant to use.
186 return awful.screen.focused()
187 end
188
189 --Match client
190 local function match_client(c, forced_tags, hints)
191 -- Don't prevent tags from being drag and dropped between screens
192 if hints and hints.reason == "screen" then
193 c:tags {c.screen.selected_tag}
194 return true
195 end
196
197 if (not c) or #c:tags() > 0 then return end
198
199 local props = c.startup_id and sn_callback[tostring(c.startup_id)] or {}
200
201 local low_i = string.lower(c.instance or "N/A")
202 local low_c = string.lower(get_class(c))
203 local tags = props.tags or {props.tag}
204
205 local rules = c_rules.instance[low_i] or c_rules.class[low_c]
206
207 if #tags == 0 and c.transient_for and (capi.mouse.screen or (rules and rules.properties.intrusive_popup)) then
208 c.sticky = c.transient_for.sticky or false
209 c:tags(awful.util.table.join(c.transient_for:tags(),(rules and rules.properties.intrusive_popup) and c.screen.selected_tags))
210 return module.focus_client(c,props)
211 elseif forced_tags then
212 return module.focus_client(c,props)
213 elseif rules then
214 --Add to matches
215 local tags_src,fav_scr,c_src,mouse_s = {},false,c.screen,capi.mouse.screen
216 for j=1,#(#tags == 0 and rules.tags or {}) do
217 local tag,cache = rules.tags[j],rules.tags[j].screen
218 tag.instances = tag.instances or setmetatable({}, { __mode = 'v' })
219
220 tag.screen = select_screen(tag)
221
222 match = tag.instances[get_screen_idx(tag.screen)]
223 tag.screen = tag.screen and get_screen_idx(tag.screen) or nil
224 local max_clients = match and (type(match.max_clients) == "function" and match.max_clients(c,match) or match.max_clients) or 999
225 if (not match and not (fav_scr == true and mouse_s ~= tag.screen)) or (max_clients <= #match:clients()) then
226 local t = awful.tag.add(tag.name,tag)
227 t.volatile = match and (max_clients ~= nil) or tag.volatile
228 t.is_template = true
229 end
230 tags_src[tag.screen],fav_scr = tags_src[tag.screen] or {},fav_scr or (tag.screen == mouse_s) --Reset if a better screen is found
231 tags_src[tag.screen][#tags_src[get_screen_idx(tag.screen)]+1] = tag.instances[get_screen_idx(tag.screen)]
232 tag.screen = cache
233 end
234 for k,t in ipairs(tags_src[mouse_s] or tags_src[c_src] or select(2,next(tags_src)) or awful.util.table.join(unpack(tags_src))) do
235 tags[#tags+1] = t.locked ~= true and t.activated and t or nil --Do not add to locked tags
236 end
237 c.screen = tags[1] and tags[1].screen or c_src
238 if #tags > 0 then
239 c:tags(tags)
240 return module.focus_client(c,props)
241 end
242 end
243
244 --Add to the current tag if not exclusive
245 local cur_tag = c.screen.selected_tag
246 if cur_tag and cur_tag.exclusive ~= true and cur_tag.locked ~= true then
247 c:tags({cur_tag})
248 return module.focus_client(c,props)
249 end
250
251 --Add to the fallback tags
252 if #c:tags((function(arr) for k,v in ipairs(fallbacks) do
253 arr[#arr+1]=v.screen == c.screen and v or nil
254 end; return arr
255 end)({})) > 0 then -- Select the first fallback tag if the current tag isn't a fallback
256 return module.focus_client(c,props)
257 end
258 --Last resort, create a new tag
259 c_rules.class[low_c] = c_rules.class[low_c] or {tags={},properties={}}
260 local tmp,tag = c_rules.class[low_c],awful.tag.add(get_class(c),{name=get_class(c),onetimer=true,volatile=true,exclusive=true,screen=(c.screen.index <= capi.screen.count())
261 and c.screen or capi.screen.primary or capi.screen[1],layout=settings.tag.layout or settings.default_layout or awful.layout.suit.max})
262 tmp.tags[#tmp.tags+1] = {name=get_class(c),instances = setmetatable({[get_screen_idx(c.screen)]=tag}, { __mode = 'v' }),volatile=true,screen=c.screen,exclusive=true}
263 c:tags({tag})
264 return module.focus_client(c,props)
265 end
266
267 capi.client.disconnect_signal("request::tag", awful.ewmh.tag)
268 capi.client.connect_signal("request::tag", match_client)
269
270 capi.client.connect_signal("untagged", function (c, t)
271 if t.volatile == true and #t:clients() == 0 then
272 local rules = c_rules.class[string.lower(get_class(c))]
273 c_rules.class[string.lower(get_class(c))] = (t.onetimer ~= true or c.class == nil) and rules or nil --Prevent "last resort tags" from persisting
274 for j=1,#(rules and rules.tags or {}) do
275 rules.tags[j].instances[get_screen_idx(c.screen)] = rules.tags[j].instances[get_screen_idx(c.screen)] ~= t and rules.tags[j].instances[get_screen_idx(c.screen)] or nil
276 end
277 -- awful.tag.delete(t)
278 end
279 end)
280
281 awful.tag._add = awful.tag.add
282
283 awful.tag.add = function(tag,props,override)
284 props.screen,props.instances = props.screen or capi.mouse.screen,props.instances or setmetatable({}, { __mode = 'v' })
285 props.master_width_factor,props.layout = props.master_width_factor or settings.tag.master_width_factor or settings.master_width_factor,props.layout or settings.default_layout or awful.layout.max
286 local t = awful.tag._add(tag,awful.util.table.join(settings.tag,props,override))
287 fallbacks[#fallbacks+1] = props.fallback and t or nil
288 t:connect_signal("property::selected", function(t) on_selected_change(t,props or {}) end)
289 t.selected = props.selected or false
290 props.instances[get_screen_idx(props.screen)] = t
291 return t
292 end
293
294 capi.tag.connect_signal("property::fallback",function(t)
295 fallbacks[awful.util.table.hasitem(fallbacks, t) or (#fallbacks+1)] = t.fallback and t or nil
296 end)
297
298 local function contain_screen(tab, s)
299 for _, v in ipairs(tab) do
300 if type(v) ~= "number" or v <= capi.screen.count() then
301 v = capi.screen[v]
302 if v == s then
303 return true
304 end
305 end
306 end
307
308 return false
309 end
310
311 -- Add init tags to newly connected screens
312 awful.screen.connect_for_each_screen(function(s)
313 for _, def in pairs(tags_hash) do
314 if def.init then
315 if def.screen and (type(def.screen) == "table" and contain_screen(def.screen, s))
316 or (type(def.screen) == "number" and def.screen <= capi.screen.count())
317 or (type(def.screen) == "screen") then
318 local real_s = def.screen
319 def.screen = s
320 awful.tag.add(def.name,def).is_template = true
321 def.screen = real_s
322 end
323 end
324 end
325
326 -- Restore old tags to their original screen
327 for ss in capi.screen do
328 for _, t in ipairs(ss.tags) do
329 if t.saved_from == s.index then
330 t.screen = s
331 end
332 end
333 end
334 end)
335
336 -- Handle events such as screen being removed
337 capi.tag.connect_signal("request::screen", function(t)
338 -- Only salvage used tags
339 if #t:clients() > 0 then
340 -- If the same class of tag exist on another screen, use that
341 if t.is_template and t.instances then
342 local new_tag
343 for k, t in pairs(t.instances) do
344 if k ~= t.screen.index and k <= capi.screen.count() then
345 new_tag = capi.screen[k]
346 break
347 end
348 end
349
350 if new_tag then
351 for _, c in ipairs(t:clients()) do
352 c:tags{new_tag}--TODO batch this to if a client is on 2 tags, it doesn't get removed from the other
353 end
354
355 -- The tag will be deleted by awful.tag
356 return
357 end
358 end
359
360 local new_screen = capi.screen.primary or awful.screen.focused() --TODO be smarter
361
362 -- In case the screen comes back, save the old index
363 t.saved_from = t.screen.index
364
365 -- Move the tag to an existing screen
366 t.selected = false
367 t.screen = new_screen
368 end
369 end)
370
371 local function apply_tyrannical_rules(c, props, callbacks)
372 -- In case the class is overwriten
373 local low_c = props.overwrite_class or string.lower(get_class(c))
374 local low_i = string.lower(c.instance or "N/A")
375
376 -- Add Tyrannical properties
377 local props_src = (c_rules.instance[low_i]
378 or c_rules.class[low_c] or {}).properties
379 or {}
380
381 awful.util.table.crush(props,props_src)
382 end
383
384 awful.rules.add_rule_source(
385 "tyrannical", apply_tyrannical_rules, {"snid", "awful.spawn"}, {"awful.rules"}
386 )
387
388 capi.client.disconnect_signal("request::activate",awful.ewmh.activate)
389 capi.client.connect_signal("request::activate",function(c,reason)
390 -- Always grant those request as it probably mean that it is a modal dialog
391 if c.transient_for and capi.client.focus == c.transient_for then
392 capi.client.focus = c
393 c:raise()
394 -- If it is not modal, then use the normal code path
395 elseif reason == "rule" or reason == "rules" or reason == "ewmh" then
396 module.focus_client(c)
397 -- Tyrannical doesn't have enough information, grant the request
398 else
399 capi.client.focus = c
400 c:raise()
401 end
402 end)
403
404 --------------------------OBJECT GEARS---------------------------
405 local getter = {properties = setmetatable({}, {__newindex = function(table,k,v) load_property(k,v) end}),
406 settings = settings, tags_by_name = tags_hash, sn_callback = sn_callback}
407 local setter = {tags = load_tags}
408
409 return setmetatable(module,{__index=function(t,k) return getter[k] end,__newindex=function(t,k,v) if setter[k] then return setter[k](v) end end})