2013-02-20 18:11:17 +01:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local print , pairs = print , pairs
|
|
|
|
local ipairs , type = ipairs , type
|
|
|
|
local string = string
|
|
|
|
local awful = require("awful")
|
|
|
|
|
|
|
|
local capi = {client = client , tag = tag ,
|
|
|
|
screen = screen , mouse = mouse }
|
|
|
|
|
2013-02-27 17:38:30 +01:00
|
|
|
local module = {}
|
2013-02-20 18:11:17 +01:00
|
|
|
|
|
|
|
-------------------------------INIT------------------------------
|
|
|
|
|
2013-03-05 22:17:13 +01:00
|
|
|
local signals = {
|
2013-03-08 06:33:34 +01:00
|
|
|
"property::exclusive" , "property::init" , "property::volatile" ,
|
2013-03-16 07:01:12 +01:00
|
|
|
"property::focus_new" , "property::instances" , "property::match" ,
|
2013-03-08 06:33:34 +01:00
|
|
|
"property::class" , "property::spawn" , "property::position" ,
|
|
|
|
"property::force_screen" , "property::max_clients", "property::exec_once",
|
2013-03-09 07:27:06 +01:00
|
|
|
"property::clone_on" , "property::clone_of"
|
2013-03-05 22:17:13 +01:00
|
|
|
}
|
|
|
|
for _,sig in ipairs(signals) do
|
2013-02-20 18:11:17 +01:00
|
|
|
capi.tag.add_signal(sig)
|
|
|
|
end
|
|
|
|
|
|
|
|
-------------------------------DATA------------------------------
|
|
|
|
|
2013-05-17 07:46:29 +02:00
|
|
|
local class_client,matches_client,tags_hash = {},{},{}
|
2013-02-20 18:11:17 +01:00
|
|
|
|
|
|
|
--------------------------TYRANIC LOGIC--------------------------
|
|
|
|
|
2013-03-08 06:33:34 +01:00
|
|
|
--Called when a tag is selected/unselected
|
|
|
|
local function on_selected_change(tag,data)
|
2013-05-16 21:19:52 +02:00
|
|
|
if data and data.exec_once and tag.selected and not data._init_done then
|
2013-03-08 06:33:34 +01:00
|
|
|
for k,v in ipairs(type(data.exec_once) == "string" and {data.exec_once} or data.exec_once) do
|
|
|
|
awful.util.spawn(v, false)
|
|
|
|
end
|
|
|
|
data._init_done = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-20 18:11:17 +01:00
|
|
|
--Turn tags -> matches into matches -> tags
|
2013-03-22 03:10:21 +01:00
|
|
|
local function fill_tyrannical(tab_in,tab_out,value)
|
2013-02-20 18:11:17 +01:00
|
|
|
if tab_in and tab_out then
|
|
|
|
for i=1,#tab_in do
|
|
|
|
local low = string.lower(tab_in[i])
|
|
|
|
local tmp = tab_out[low] or {tags={},properties={}}
|
2013-03-08 06:33:34 +01:00
|
|
|
value.instances= value.instances or {}
|
2013-02-20 18:11:17 +01:00
|
|
|
tmp.tags[#tmp.tags+1] = value
|
|
|
|
tab_out[low] = tmp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--Load tags, this cannot be undone
|
2013-03-22 03:10:21 +01:00
|
|
|
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
|
|
|
|
v.instances = {}
|
|
|
|
local stype = type(v.screen)
|
|
|
|
if stype == "table" then
|
|
|
|
for k2,v2 in pairs(v.screen) do
|
|
|
|
if v2 <= capi.screen.count() then
|
|
|
|
v.screen = v2
|
|
|
|
v.instances[v2] = awful.tag.add(v.name,v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif (v.screen or 1) <= capi.screen.count() then
|
|
|
|
v.instances[v.screen or 1] = awful.tag.add(v.name,v)
|
|
|
|
end
|
|
|
|
elseif v.volatile == nil then
|
|
|
|
v.volatile = true
|
|
|
|
end
|
2013-03-22 03:10:21 +01:00
|
|
|
fill_tyrannical(v.class,class_client,v)
|
|
|
|
fill_tyrannical(v.match,matches_client,v)
|
2013-05-17 07:46:29 +02:00
|
|
|
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
|
|
|
|
local key_type = type(k2)
|
|
|
|
local low = string.lower(key_type == "number" and v2 or k2)
|
|
|
|
class_client[low] = class_client[low] or {name=low,tags={},properties={}}
|
|
|
|
class_client[low].properties[name] = key_type == "number" and true or v2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--Match client
|
|
|
|
local function match_client(c, startup)
|
|
|
|
if not c then return end
|
|
|
|
local low = string.lower(c.class or "N/A")
|
|
|
|
local rules = class_client[low]
|
|
|
|
if rules then
|
|
|
|
--Force floating state if necessary
|
|
|
|
if rules.properties.floating ~= nil then
|
|
|
|
awful.client.floating.set(c, rules.properties.floating)
|
|
|
|
end
|
2013-02-28 02:42:48 +01:00
|
|
|
--Center client
|
|
|
|
if rules.properties.centered == true then
|
|
|
|
awful.placement.centered(c, nil)
|
|
|
|
end
|
2013-02-28 02:53:04 +01:00
|
|
|
--Focus new client
|
2013-03-16 07:01:12 +01:00
|
|
|
if rules.properties.focus_new ~= false then
|
2013-03-22 03:10:21 +01:00
|
|
|
capi.client.focus = c
|
2013-02-28 02:53:04 +01:00
|
|
|
end
|
2013-02-20 18:11:17 +01:00
|
|
|
--Set other properties
|
|
|
|
for k,v in pairs(rules.properties) do
|
|
|
|
c[k] = v
|
|
|
|
end
|
|
|
|
--Add to the current tag if the client is intrusive, ignore exclusive
|
|
|
|
if rules.properties.intrusive == true then
|
|
|
|
local tag = awful.tag.selected(c.screen)
|
2013-03-15 04:27:44 +01:00
|
|
|
if not tag then
|
|
|
|
awful.tag.viewonly(awful.tag.gettags(c.screen)[1])
|
|
|
|
end
|
|
|
|
tag = awful.tag.selected(c.screen)
|
|
|
|
if tag then --Can be false if there is no tags
|
2013-02-20 18:11:17 +01:00
|
|
|
c:tags({tag})
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
--TODO pre_match
|
|
|
|
--Add to matches
|
|
|
|
local tags = {}
|
|
|
|
for j=1,#(rules.tags or {}) do
|
|
|
|
local tag_tmp = rules.tags[j]
|
|
|
|
if not tag_tmp.instances[c.screen or 1] then
|
|
|
|
local cache = tag_tmp.screen
|
|
|
|
tag_tmp.screen = tag_tmp.force_screen == true and tag_tmp.screen or c.screen
|
2013-03-09 07:27:06 +01:00
|
|
|
tag_tmp.screen = (tag_tmp.screen <= capi.screen.count()) and tag_tmp.screen or 1
|
2013-02-20 18:11:17 +01:00
|
|
|
c.screen = tag_tmp.screen
|
2013-03-09 07:27:06 +01:00
|
|
|
tag_tmp.instances[(c.screen <= capi.screen.count()) and tag_tmp.screen or 1] = awful.tag.add(tag_tmp.name,tag_tmp)
|
2013-02-20 18:11:17 +01:00
|
|
|
tag_tmp.screen = cache
|
|
|
|
end
|
2013-03-09 07:27:06 +01:00
|
|
|
tags[#tags+1] = tag_tmp.instances[(c.screen <= capi.screen.count()) and c.screen or 1]
|
2013-02-20 18:11:17 +01:00
|
|
|
end
|
|
|
|
if #tags > 0 then
|
|
|
|
c:tags(tags)
|
2013-03-16 07:01:12 +01:00
|
|
|
if awful.tag.getproperty(tags[1],"focus_new") ~= false then
|
2013-02-20 18:11:17 +01:00
|
|
|
awful.tag.viewonly(tags[1])
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
--TODO post_match
|
|
|
|
end
|
|
|
|
--Add to the current tag if not exclusive
|
2013-03-09 07:47:42 +01:00
|
|
|
local cur_tag = awful.tag.selected(c.screen)
|
|
|
|
if awful.tag.getproperty(cur_tag,"exclusive") ~= true then
|
|
|
|
return c:tags({cur_tag})
|
|
|
|
end
|
2013-02-20 18:11:17 +01:00
|
|
|
--Last resort, create a new tag
|
|
|
|
class_client[low] = class_client[low] or {tags={},properties={}}
|
2013-03-15 04:27:44 +01:00
|
|
|
local tmp,tag = class_client[low],awful.tag.add(c.class,{name=c.class,volatile=true,screen=(c.screen <= capi.screen.count()) and c.screen or 1,layout=awful.layout.suit.max})
|
2013-03-09 07:27:06 +01:00
|
|
|
tmp.tags[#tmp.tags+1] = {name=c.class,instances = {[c.screen]=tag},volatile=true,screen=c.screen}
|
2013-02-20 18:11:17 +01:00
|
|
|
c:tags({tag})
|
2013-03-22 03:10:21 +01:00
|
|
|
if awful.tag.getproperty(tag,"focus_on_new") ~= false then
|
|
|
|
awful.tag.viewonly(tag)
|
|
|
|
end
|
2013-02-20 18:11:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
capi.client.connect_signal("manage", match_client)
|
|
|
|
|
|
|
|
capi.client.connect_signal("untagged", function (c, t)
|
|
|
|
if awful.tag.getproperty(t,"volatile") == true and #t:clients() == 0 then
|
|
|
|
local rules = class_client[string.lower(c.class or "N/A")]
|
|
|
|
for j=1,#(rules and rules.tags or {}) do
|
|
|
|
rules.tags[j].instances[c.screen] = rules.tags[j].instances[c.screen] ~= t and rules.tags[j].instances[c.screen] or nil
|
|
|
|
end
|
|
|
|
awful.tag.delete(t)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- awful.tag.withcurrent = function() end --Disable automatic tag insertion
|
2013-03-08 06:33:34 +01:00
|
|
|
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
|
|
|
|
if awful.tag.getscreen(t) == c.screen then
|
|
|
|
tags[#tags+1] = t
|
|
|
|
end
|
|
|
|
end
|
|
|
|
--Necessary when dragging clients
|
|
|
|
if startup == nil and old_tags[1] and old_tags[1].screen ~= c.screen then --nil != false
|
|
|
|
local sellist = awful.tag.selectedlist(c.screen)
|
|
|
|
if #sellist > 0 then --Use already selected tag
|
|
|
|
tags = sellist
|
|
|
|
else --Select a tag
|
|
|
|
match_client(c, startup)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
c:tags(tags)
|
2013-03-08 06:33:34 +01:00
|
|
|
end,awful.tag.add
|
|
|
|
|
2013-03-09 07:27:06 +01:00
|
|
|
awful.tag.add,awful.tag._setscreen = function(tag,props)
|
2013-03-08 06:33:34 +01:00
|
|
|
local t = awful.tag._add(tag,props)
|
2013-03-09 07:27:06 +01:00
|
|
|
if awful.tag.getproperty(t,"clone_on") and awful.tag.getproperty(t,"clone_on") ~= t.screen then
|
2013-03-15 04:27:44 +01:00
|
|
|
local t3 = awful.tag._add(tag,{screen = awful.tag.getproperty(t,"clone_on"), clone_of = t,icon=awful.tag.geticon(t)})
|
2013-03-09 07:27:06 +01:00
|
|
|
--TODO prevent clients from being added to the clone
|
|
|
|
end
|
2013-05-17 07:21:04 +02:00
|
|
|
t.selected = props.selected or false
|
2013-05-16 21:19:52 +02:00
|
|
|
t:connect_signal("property::selected", function(t) on_selected_change(t,props or {}) end)
|
2013-03-08 06:33:34 +01:00
|
|
|
return t
|
2013-03-09 07:27:06 +01:00
|
|
|
end,awful.tag.setscreen
|
|
|
|
|
|
|
|
awful.tag.setscreen,awful.tag._viewonly = function(tag,screen) --Why this isn't by default...
|
|
|
|
awful.tag.history.restore(tag.screen,1)
|
|
|
|
awful.tag._setscreen(tag,screen)
|
|
|
|
for k,c in ipairs(tag:clients()) do
|
|
|
|
c.screen = screen or 1 --Move all clients
|
|
|
|
end
|
|
|
|
end,awful.tag.viewonly
|
|
|
|
|
|
|
|
awful.tag.viewonly = function(t)
|
|
|
|
awful.tag._viewonly(t)
|
|
|
|
if awful.tag.getproperty(t,"clone_of") then
|
|
|
|
awful.tag.swap(t,awful.tag.getproperty(t,"clone_of"))
|
|
|
|
awful.tag.viewonly(awful.tag.getproperty(t,"clone_of"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
awful.tag.swap = function(tag1,tag2)
|
|
|
|
local idx1,idx2,scr2 = awful.tag.getidx(tag1),awful.tag.getidx(tag2),awful.tag.getscreen(tag2)
|
|
|
|
awful.tag.setscreen(tag2,awful.tag.getscreen(tag1))
|
|
|
|
awful.tag.move(idx1,tag2)
|
|
|
|
awful.tag.setscreen(tag1,scr2)
|
|
|
|
awful.tag.move(idx2,tag1)
|
2013-02-20 18:11:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--------------------------OBJECT GEARS---------------------------
|
|
|
|
local properties = {}
|
|
|
|
setmetatable(properties, {__newindex = function(table,k,v) load_property(k,v) end})
|
|
|
|
|
|
|
|
local function getter (table, key)
|
|
|
|
if key == "properties" then
|
|
|
|
return properties
|
2013-05-17 07:46:29 +02:00
|
|
|
elseif key == "tags_by_name" then
|
|
|
|
return tags_hash
|
2013-02-20 18:11:17 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
local function setter (table, key,value)
|
|
|
|
if key == "tags" then
|
|
|
|
load_tags(value)
|
|
|
|
elseif key == "properties" then
|
|
|
|
properties = value
|
|
|
|
setmetatable(properties, {__newindex = function(table,k,v) load_property(k,v) end})
|
2013-03-22 03:10:21 +01:00
|
|
|
for k,v in pairs(tyrannical_properties) do
|
2013-02-20 18:11:17 +01:00
|
|
|
load_property(k,v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-27 17:38:30 +01:00
|
|
|
return setmetatable(module, { __call = function(_, ...) return end , __index = getter, __newindex = setter})
|