2021-01-23 23:04:22 +01:00
|
|
|
local awful = require("awful")
|
2021-04-08 20:17:10 +02:00
|
|
|
local gears = require("gears")
|
2021-01-23 23:04:22 +01:00
|
|
|
|
|
|
|
local _client = {}
|
|
|
|
|
|
|
|
--- Turn off passed client
|
|
|
|
-- Remove current tag from window's tags
|
|
|
|
--
|
2021-04-08 20:17:10 +02:00
|
|
|
-- @param c A client
|
2021-08-26 19:31:27 +02:00
|
|
|
function _client.turn_off(c, current_tag)
|
2021-08-27 20:01:22 +02:00
|
|
|
if current_tag == nil then
|
|
|
|
current_tag = c.screen.selected_tag
|
|
|
|
end
|
2021-01-23 23:04:22 +01:00
|
|
|
local ctags = {}
|
|
|
|
for k, tag in pairs(c:tags()) do
|
2021-08-27 20:01:22 +02:00
|
|
|
if tag ~= current_tag then
|
|
|
|
table.insert(ctags, tag)
|
|
|
|
end
|
2021-01-23 23:04:22 +01:00
|
|
|
end
|
|
|
|
c:tags(ctags)
|
2021-09-14 02:22:34 +02:00
|
|
|
c.sticky = false
|
2021-01-23 23:04:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Turn on passed client (add current tag to window's tags)
|
|
|
|
--
|
|
|
|
-- @param c A client
|
|
|
|
function _client.turn_on(c)
|
2021-04-16 21:46:42 +02:00
|
|
|
local current_tag = c.screen.selected_tag
|
2021-08-27 20:01:22 +02:00
|
|
|
ctags = { current_tag }
|
2021-01-23 23:04:22 +01:00
|
|
|
for k, tag in pairs(c:tags()) do
|
2021-08-27 20:01:22 +02:00
|
|
|
if tag ~= current_tag then
|
|
|
|
table.insert(ctags, tag)
|
|
|
|
end
|
2021-01-23 23:04:22 +01:00
|
|
|
end
|
|
|
|
c:tags(ctags)
|
|
|
|
c:raise()
|
|
|
|
client.focus = c
|
|
|
|
end
|
|
|
|
|
2021-03-15 13:50:25 +01:00
|
|
|
--- Sync two clients
|
|
|
|
--
|
|
|
|
-- @param to_c The client to which to write all properties
|
|
|
|
-- @param from_c The client from which to read all properties
|
|
|
|
function _client.sync(to_c, from_c)
|
2021-08-27 20:01:22 +02:00
|
|
|
if not from_c or not to_c then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not from_c.valid or not to_c.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if from_c.modal then
|
|
|
|
return
|
|
|
|
end
|
2021-03-15 13:50:25 +01:00
|
|
|
to_c.floating = from_c.floating
|
|
|
|
to_c.maximized = from_c.maximized
|
|
|
|
to_c.above = from_c.above
|
|
|
|
to_c.below = from_c.below
|
|
|
|
to_c:geometry(from_c:geometry())
|
|
|
|
-- TODO: Should also copy over the position in a tiling layout
|
|
|
|
end
|
2021-01-23 23:04:22 +01:00
|
|
|
|
2021-04-08 20:17:10 +02:00
|
|
|
--- Checks whether the passed client is a childprocess of a given process ID
|
|
|
|
--
|
|
|
|
-- @param c A client
|
|
|
|
-- @param pid The process ID
|
|
|
|
-- @return True if the passed client is a childprocess of the given PID otherwise false
|
|
|
|
function _client.is_child_of(c, pid)
|
2021-08-27 20:01:22 +02:00
|
|
|
-- io.popen is normally discouraged. Should probably be changed
|
|
|
|
if not c or not c.valid then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
if tostring(c.pid) == tostring(pid) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
local pid_cmd = [[pstree -T -p -a -s ]]
|
|
|
|
.. tostring(c.pid)
|
|
|
|
.. [[ | sed '2q;d' | grep -o '[0-9]*$' | tr -d '\n']]
|
2021-04-08 20:17:10 +02:00
|
|
|
local handle = io.popen(pid_cmd)
|
|
|
|
local parent_pid = handle:read("*a")
|
|
|
|
handle:close()
|
2021-08-27 20:01:22 +02:00
|
|
|
return tostring(parent_pid) == tostring(pid)
|
|
|
|
or tostring(parent_pid) == tostring(c.pid)
|
2021-04-08 20:17:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Finds all clients that satisfy the passed rule
|
|
|
|
--
|
|
|
|
-- @param rule The rule to be searched for
|
2021-07-27 19:54:29 +02:00
|
|
|
-- @retrun A list of clients that match the given rule
|
2021-04-08 20:17:10 +02:00
|
|
|
function _client.find(rule)
|
2021-08-27 20:01:22 +02:00
|
|
|
local function matcher(c)
|
|
|
|
return awful.rules.match(c, rule)
|
|
|
|
end
|
2021-04-08 20:17:10 +02:00
|
|
|
local clients = client.get()
|
|
|
|
local findex = gears.table.hasitem(clients, client.focus) or 1
|
|
|
|
local start = gears.math.cycle(#clients, findex + 1)
|
|
|
|
|
|
|
|
local matches = {}
|
|
|
|
for c in awful.client.iterate(matcher, start) do
|
|
|
|
matches[#matches + 1] = c
|
|
|
|
end
|
|
|
|
|
|
|
|
return matches
|
|
|
|
end
|
|
|
|
|
2021-07-27 19:54:29 +02:00
|
|
|
--- Gets the next client by direction from the focused one
|
2021-08-27 20:01:22 +02:00
|
|
|
--
|
2021-07-27 19:54:29 +02:00
|
|
|
-- @param direction it the direction as a string ("up", "down", "left" or "right")
|
|
|
|
-- @retrun the client in the given direction starting at the currently focused one, nil otherwise
|
|
|
|
function _client.get_by_direction(direction)
|
|
|
|
local sel = client.focus
|
2021-08-27 20:01:22 +02:00
|
|
|
if not sel then
|
|
|
|
return nil
|
|
|
|
end
|
2021-07-27 19:54:29 +02:00
|
|
|
local cltbl = sel.screen:get_clients()
|
|
|
|
local geomtbl = {}
|
|
|
|
for i, cl in ipairs(cltbl) do
|
|
|
|
geomtbl[i] = cl:geometry()
|
|
|
|
end
|
2021-08-27 20:01:22 +02:00
|
|
|
local target = gears.geometry.rectangle.get_in_direction(
|
|
|
|
direction,
|
|
|
|
geomtbl,
|
|
|
|
sel:geometry()
|
|
|
|
)
|
2021-07-27 19:54:29 +02:00
|
|
|
return cltbl[target]
|
|
|
|
end
|
|
|
|
|
2021-01-23 23:04:22 +01:00
|
|
|
return _client
|