tyrannical/init.lua

354 lines
15 KiB
Lua
Raw Normal View History

2013-02-20 18:11:17 +01:00
local setmetatable = setmetatable
local print , pairs = print , pairs
local ipairs , type = ipairs , type
local string , unpack= string , unpack
2013-02-20 18:11:17 +01:00
local awful = require("awful")
require("tyrannical.extra.legacy")
2013-02-20 18:11:17 +01:00
local capi,sn_callback = {client = client, tag = tag, awesome = awesome,
screen = screen, mouse = mouse},awful.spawn and awful.spawn.snid_buffer or {}
2013-02-20 18:11:17 +01:00
-------------------------------INIT------------------------------
2016-06-13 05:49:28 +02:00
local module,c_rules,tags_hash,settings,fallbacks,prop = {},{class={},instance={}},{},{tag={},client={}},{},awful.tag.getproperty
2013-02-20 18:11:17 +01:00
----------------------TYRANNICAL LOGIC--------------------------
2013-02-20 18:11:17 +01:00
--Called when a tag is selected/unselected
local function on_selected_change(tag,data)
if data and data.exec_once and tag.selected then
for _,v in ipairs(type(data.exec_once) == "string" and {data.exec_once} or data.exec_once) do
awful.spawn.with_shell("ps -ef | grep -v grep | grep '" .. v .. "' > /dev/null || (" .. v .. ")")
end
end
end
local function get_class(c)
return awful.client.property.get(c, "overwrite_class") or c.class or "N/A"
end
2016-04-12 08:48:51 +02:00
local function get_screen_idx(s)
return type(s) == "number" and s or s.index
end
local function scr_exists(s)
local t = type(s)
2016-08-01 19:11:34 +02:00
return (t == "number" and s > 0 and s <= capi.screen.count())
or t == "screen" or (t == "table" and s.workarea)
end
2013-02-20 18:11:17 +01:00
--Load tags, this cannot be undone
local function load_tags(tyrannical_tags)
for k,v in ipairs(tyrannical_tags) do
2013-02-20 18:11:17 +01:00
if v.init ~= false then
if type(v.screen) == "table" then
local screens = v.screen --TODO remove
for k2,v2 in pairs(screens) do
2016-04-12 08:48:51 +02:00
if (type(v2) == "number" and v2 or v2.index) <= capi.screen.count() then
v.screen = v2 --TODO remove
awful.tag.add(v.name,v,{screen = v2}).is_template = true
2013-02-20 18:11:17 +01:00
end
end
v.screen = screens --TODO remove
2016-04-12 08:48:51 +02:00
elseif (v.screen and (type(v.screen) == "number" and v.screen or v.screen.index) or 1) <= capi.screen.count() then
awful.tag.add(v.name,v).is_template = true
2013-02-20 18:11:17 +01:00
end
elseif v.volatile == nil then
v.volatile = true
end
for _,prop in ipairs {"class","instance"} do
if v[prop] and c_rules[prop] then
-- for low in (function() local i=0; return function() i=i+1; return prop[i] and prop[i]:lower() end end)() do --TODO fix
for i=1,#v[prop] do
local low = string.lower(v[prop][i])
local tmp = c_rules[prop][low] or {tags={},properties={}}
tmp.tags[#tmp.tags+1] = awful.util.table.hasitem(tmp.tags,v) == nil and v or nil --Avoid duplicates
c_rules[prop][low] = tmp
end
end
end
tags_hash[v.name or "N/A"] = v
2013-02-20 18:11:17 +01:00
end
end
--Load property
local function load_property(name,property)
for k2,v2 in pairs(property) do --TODO make an iterator?
2013-02-20 18:11:17 +01:00
local key_type = type(k2)
local low = string.lower(key_type == "number" and v2 or k2)
c_rules.class[low] = c_rules.class[low] or {name=low,tags={},properties={}}
c_rules.class[low].properties[name] = key_type == "number" and true or v2
2013-02-20 18:11:17 +01:00
end
end
--Check all focus policies then change focus (Awesome 3.5.3+)
function module.focus_client(c,properties)
local properties = properties or (c_rules.instance[string.lower(c.instance or "N/A")] or {}).properties or (c_rules.class[string.lower(get_class(c))] or {}).properties or {}
if (((not c.transient_for) or (c.transient_for==capi.client.focus) or (not settings.block_children_focus_stealing)) and (not properties.no_autofocus)) then
2016-04-12 08:48:51 +02:00
if not awful.util.table.hasitem(c:tags(), (c.screen or capi.screen[1]).selected_tag) and (not prop(c:tags()[1],"no_focus_stealing_in")) then
c:tags()[1]:view_only()
end
capi.client.focus = c
c:raise()
return true
end
end
--Apply all properties
local function apply_properties(c,override,normal)
if not override and not normal then return nil,{} end
local props,ret = awful.util.table.join(settings.client,normal or {},override,
override.callback and override.callback(c) or (normal and normal.callback and normal.callback(c)) or {}),nil
--Set all 'c.something' properties, --TODO maybe eventually move to awful.rules.execute
2014-04-19 05:04:13 +02:00
for k,_ in pairs(props) do
c[k] = props[k]
end
--Center client
2014-04-19 05:04:13 +02:00
if props.centered == true then
awful.placement.centered(c, nil)
end
2014-05-01 22:33:34 +02:00
--Set slave or master
if props.slave == true or props.master == true then
awful.client["set"..(props.slave and "slave" or "master")](c, true)
end
2014-09-29 06:07:16 +02:00
--Check if the client should be added to an existing tag (or tags)
if props.new_tag then
ret = c:tags({awful.tag.add(type(props.new_tag)=="table" and props.new_tag.name or c.class,type(props.new_tag)=="table" and props.new_tag or {screen=c.screen or 1})})
elseif props.tag then
ret = c:tags(type(props.tag) == "function" and props.tag(c) or (type(props.tag) == "table" and props.tag or { props.tag }))
elseif props.intrusive == true or (settings.force_odd_as_intrusive and c.type ~= "normal") then
2016-04-12 08:48:51 +02:00
local tag = c.screen.selected_tag or c.screen.tags[1]:view_only() or c.screen.selected_tag
if tag then --Can be false if there is no tags
ret = c:tags({tag})
end
end
return ret,props
end
local function select_screen(tag)
local s
-- If there is a table of screen, check if it contains the mouse one
if type(tag.screen) =="table" and tag.screen[1] then
for k, ss in ipairs(tag.screen) do
ss = type(ss) == "number" and ss <= capi.screen.count() and ss or nil
if ss and capi.screen[ss] == awful.screen.focused() then
s = ss
end
end
s = s or capi.screen[tag.screen[1]]
else
s = scr_exists(tag.screen) and capi.screen[tag.screen] or nil
end
-- If the tag.force_screen is set, then obey
if (tag.force_screen and s) or (s and settings.favor_focused == false) then
return s
end
-- By default, Tyrannical prefer to use the focused screen to place new tags
-- This override some other settings, but is more pleasant to use.
return awful.screen.focused()
end
2013-02-20 18:11:17 +01:00
--Match client
local function match_client(c, startup)
if not c then return end
2014-03-30 07:21:24 +02:00
local startup = startup == nil and capi.awesome.startup or startup
local props = c.startup_id and sn_callback[tostring(c.startup_id)] or {}
local low_i = string.lower(c.instance or "N/A")
local low_c = string.lower(get_class(c))
local tags = props.tags or {props.tag}
local rules = c_rules.instance[low_i] or c_rules.class[low_c]
local forced_tags,props = apply_properties(c,props,rules and rules.properties)
if #tags == 0 and c.transient_for and (capi.mouse.screen or (rules and rules.properties.intrusive_popup)) then
c.sticky = c.transient_for.sticky or false
2016-04-12 08:48:51 +02:00
c:tags(awful.util.table.join(c.transient_for:tags(),(rules and rules.properties.intrusive_popup) and c.screen.selected_tags))
return module.focus_client(c,props)
elseif forced_tags then
return module.focus_client(c,props)
elseif rules then
2013-02-20 18:11:17 +01:00
--Add to matches
local tags_src,fav_scr,c_src,mouse_s = {},false,c.screen,capi.mouse.screen
for j=1,#(#tags == 0 and rules.tags or {}) do
local tag,cache = rules.tags[j],rules.tags[j].screen
tag.instances = tag.instances or setmetatable({}, { __mode = 'v' })
tag.screen = select_screen(tag)
match = tag.instances[get_screen_idx(tag.screen)]
2016-04-12 08:48:51 +02:00
tag.screen = tag.screen and get_screen_idx(tag.screen) or nil
local max_clients = match and (type(prop(match,"max_clients")) == "function" and prop(match,"max_clients")(c,match) or prop(match,"max_clients")) or 999
if (not match and not (fav_scr == true and mouse_s ~= tag.screen)) or (max_clients <= #match:clients()) then
local t = awful.tag.add(tag.name,tag)
t.volatile = match and (max_clients ~= nil) or tag.volatile
t.is_template = true
2013-02-20 18:11:17 +01:00
end
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
2016-04-12 08:48:51 +02:00
tags_src[tag.screen][#tags_src[get_screen_idx(tag.screen)]+1] = tag.instances[get_screen_idx(tag.screen)]
tag.screen = cache
2013-02-20 18:11:17 +01:00
end
2014-04-19 04:47:30 +02:00
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
tags[#tags+1] = prop(t,"locked") ~= true and t.activated and t or nil --Do not add to locked tags
2014-04-19 04:47:30 +02:00
end
2016-04-12 08:48:51 +02:00
c.screen = tags[1] and tags[1].screen or c_src
2014-04-19 04:47:30 +02:00
if #tags > 0 then
2013-02-20 18:11:17 +01:00
c:tags(tags)
return module.focus_client(c,props)
2013-02-20 18:11:17 +01:00
end
end
2013-02-20 18:11:17 +01:00
--Add to the current tag if not exclusive
2016-04-12 08:48:51 +02:00
local cur_tag = c.screen.selected_tag
if cur_tag and cur_tag.exclusive ~= true and cur_tag.locked ~= true then
c:tags({cur_tag})
return module.focus_client(c,props)
2013-03-09 07:47:42 +01:00
end
2014-04-13 08:00:59 +02:00
--Add to the fallback tags
if #c:tags((function(arr) for k,v in ipairs(fallbacks) do
2016-04-12 08:48:51 +02:00
arr[#arr+1]=v.screen == c.screen and v or nil
2014-04-13 08:00:59 +02:00
end; return arr
2014-04-16 02:46:27 +02:00
end)({})) > 0 then -- Select the first fallback tag if the current tag isn't a fallback
return module.focus_client(c,props)
2014-04-16 02:46:27 +02:00
end
2013-02-20 18:11:17 +01:00
--Last resort, create a new tag
c_rules.class[low_c] = c_rules.class[low_c] or {tags={},properties={}}
2016-04-12 08:48:51 +02:00
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())
and c.screen or capi.screen.primary or capi.screen[1],layout=settings.tag.layout or settings.default_layout or awful.layout.suit.max})
2016-06-13 21:08:30 +02:00
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}
2013-02-20 18:11:17 +01:00
c:tags({tag})
return module.focus_client(c,props)
2013-02-20 18:11:17 +01:00
end
capi.client.connect_signal("request::tag", match_client)
2013-02-20 18:11:17 +01:00
capi.client.connect_signal("untagged", function (c, t)
2014-04-19 05:04:13 +02:00
if prop(t,"volatile") == true and #t:clients() == 0 then
local rules = c_rules.class[string.lower(get_class(c))]
c_rules.class[string.lower(get_class(c))] = (prop(t,"onetimer") ~= true or c.class == nil) and rules or nil --Prevent "last resort tags" from persisting
2013-02-20 18:11:17 +01:00
for j=1,#(rules and rules.tags or {}) do
2016-06-13 21:08:30 +02:00
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
2013-02-20 18:11:17 +01:00
end
-- awful.tag.delete(t)
2013-02-20 18:11:17 +01:00
end
end)
awful.tag.withcurrent,awful.tag._add = function(c, startup)
2013-02-20 18:11:17 +01:00
local tags,old_tags = {},c:tags()
--Safety to prevent
for k, t in ipairs(old_tags) do
2016-04-12 08:48:51 +02:00
tags[#tags+1] = (t.screen == c.screen) and t or nil
2013-02-20 18:11:17 +01:00
end
--Necessary when dragging clients
if startup == nil and old_tags[1] and old_tags[1].screen ~= c.screen then --nil != false
2016-04-12 08:48:51 +02:00
local sellist = c.screen.selected_tags
2013-02-20 18:11:17 +01:00
if #sellist > 0 then --Use already selected tag
tags = sellist
else --Select a tag
match_client(c, startup)
end
end
c:tags(tags)
end,awful.tag.add
2016-04-12 08:48:51 +02:00
awful.tag.add = function(tag,props,override)
props.screen,props.instances = props.screen or capi.mouse.screen,props.instances or setmetatable({}, { __mode = 'v' })
props.mwfact,props.layout = props.mwfact or settings.tag.mwfact or settings.mwfact,props.layout or settings.default_layout or awful.layout.max
local t = awful.tag._add(tag,awful.util.table.join(settings.tag,props,override))
2014-04-13 08:00:59 +02:00
fallbacks[#fallbacks+1] = props.fallback and t or nil
t:connect_signal("property::selected", function(t) on_selected_change(t,props or {}) end)
2013-05-24 17:22:36 +02:00
t.selected = props.selected or false
2016-06-13 21:08:30 +02:00
props.instances[get_screen_idx(props.screen)] = t
return t
end
2014-05-03 01:13:27 +02:00
capi.tag.connect_signal("property::fallback",function(t)
fallbacks[awful.util.table.hasitem(fallbacks, t) or (#fallbacks+1)] = prop(t,"fallback") and t or nil
end)
local is_init = setmetatable({}, { __mode = 'k' })
local function contain_screen(tab, s)
for _, v in ipairs(tab) do
if type(v) ~= "number" or v <= capi.screen.count() then
v = capi.screen[v]
if v == s then
return true
end
end
end
return false
end
-- Add init tags to newly connected screens
awful.screen.connect_for_each_screen(function(s) --TODO remove the load code and use this
if is_init[s] then return end
for _, def in pairs(tags_hash) do
if def.init then
if def.screen and (type(def.screen) == "table" and contain_screen(def.screen, s))
or (type(def.screen) == "number" and def.screen <= capi.screen.count())
or (type(def.screen) == "screen") then
local real_s = def.screen
def.screen = s
awful.tag.add(def.name,def).is_template = true
def.screen = real_s
end
end
end
-- Restore old tags to their original screen
for ss in capi.screen do
for _, t in ipairs(ss.tags) do
if t.saved_from == s.index then
t.screen = s
end
end
end
end)
-- Handle events such as screen being removed
capi.tag.connect_signal("request::screen", function(t)
-- Only salvage used tags
if #t:clients() > 0 then
-- If the same class of tag exist on another screen, use that
if t.is_template and t.instances then
local new_tag
for k, t in pairs(t.instances) do
if k ~= t.screen.index and k <= capi.screen.count() then
new_tag = capi.screen[k]
break
end
end
if new_tag then
for _, c in ipairs(t:clients()) do
c:tags{new_tag}--TODO batch this to if a client is on 2 tags, it doesn't get removed from the other
end
-- The tag will be deleted by awful.tag
return
end
end
local new_screen = capi.screen.primary or awful.screen.focused() --TODO be smarter
-- In case the screen comes back, save the old index
t.saved_from = t.screen.index
-- Move the tag to an existing screen
t.selected = false
t.screen = new_screen
end
end)
2013-02-20 18:11:17 +01:00
--------------------------OBJECT GEARS---------------------------
local getter = {properties = setmetatable({}, {__newindex = function(table,k,v) load_property(k,v) end}),
2013-10-11 06:08:58 +02:00
settings = settings, tags_by_name = tags_hash, sn_callback = sn_callback}
local setter = {tags = load_tags}
2013-02-20 18:11:17 +01:00
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})