Skip to content

Instantly share code, notes, and snippets.

@khalid151
Created June 18, 2020 23:27
Show Gist options
  • Save khalid151/af7704926274294b5896ee1fed468b34 to your computer and use it in GitHub Desktop.
Save khalid151/af7704926274294b5896ee1fed468b34 to your computer and use it in GitHub Desktop.
Give AwesomeWM DWM-like window swallowing
-- These are changes required to enable DWM-like window swallowing.
-- The code is some spaghetti mess but.. it works (TM)
-- First, a callback must be added to all clients to find PPID of the window, which
-- refers to terminal window PID.
-- The behaviour I've noticed when launching applications from within zsh shell,
-- the PPID of the launched window is actually the PID of the zsh instance.
-- So I had to get PPID of it, to get the PID of terminal.
awful.rules.rules = {
rule = { },
properties = {
-- add these extra properties:
swallow = true, -- Enable for all windows by default
callback = function(c)
if c.pid then
-- Get PPID of window, which is zsh -> outpu1
awful.spawn.easy_async("ps -oppid= -p" .. c.pid, function(output1)
-- Get PID of terminal, PPID of zsh -> output2
awful.spawn.easy_async("ps -oppid= -p" .. output1, function(output2)
-- PPID stored as extra property for clients
c.ppid = tonumber(output2)
end)
end)
end
end,
},
-- Add this property to help determine used terminal emulators
rule = { class = "Termite" },
properties = {
terminal = true,
}
}
-- Keep track of terminal PIDs in a table
local running_terminals = { }
function awful.client.object.set_terminal(c, set)
-- Add the terminal to list of running terminals
c.is_terminal = value
if set then
running_terminals[tostring(c.pid)] = c
end
end
-- A small function to calculate the index in layout, to restore later
local find_client_relative_index = function(c)
local index_table = awful.client.idx(c)
if index_table.col == 0 then
return index_table.col + index_table.idx
else
return index_table.idx - index_table.num
end
end
-- Check new clients if the should be swallowed or not
client.connect_signal("manage", function(c)
if c.swallow and c.type == "normal" then
-- Since setting PPID as property for clients is asynchronous,
-- trying to swallow window can happen too soon. So using a timer here.
gears.timer {
autostart = true,
timeout = 0.06,
single_shot = true,
callback = function()
-- Get the terminal that matches the PPID of the newly-managed client,
-- if the PPID exists within the running_terminals table, then
-- the terminal should be swallowed.
local term = running_terminals[tostring(c.ppid)]
if term then
c:geometry(term:geometry()) -- Set the client geometry as the terminal's
term:swap(c) -- Swap the position within the layout
term.hidden = true -- Hide terminal from taskbar and minimize it
-- Keep track of indices
term.relative_index = find_client_relative_index(c)
c:connect_signal("swapped", function(_c)
term.relative_index = find_client_relative_index(_c)
end)
-- When client is destoryed, restore the terminal
c:connect_signal("unmanage", function(_c)
term:geometry(_c:geometry())
term.hidden = false
term:activate{}
awful.client.swap.byidx(term.relative_index, term)
end)
end
end
}
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment