Linux – Focus on application when changing workspaces in awesome-wm

awesome-wmlinuxlua

I am using archlinux in combination with awesome-wm for quite a while now. I still haven't figured out how to automatically grab the focus on an application when changing workspace through Mod+number.

For example, I have firefox running on workspace 2 and some terminals on workspace 1. If I am in workspace 2 and press Mod+1 to go to workspace 1, no terminal is focussed. I can ofcourse click on a terminal to grab focus, or press Mod+j or Mod+k, but I would like this to happen automatically.

How can I make this possible? Thanks in advance!

Best Answer

I figured it out so I will answer my own question. This might not be the best or most efficient way, but it gets the job done. This question might fit more on stackoverflow since it is more focussed on programming.

I wrote this function, which grabs focus on the first client on screen (except for Conky, the system monitor I use).

-- Grab focus on first client on screen
function grab_focus()
    local all_clients = client.get()
    for i, c in pairs(all_clients) do
        if c:isvisible() and c.class ~= "Conky" then
            client.focus = c
        end
    end
end

I now call this function in rc.lua where the numbered keys are mapped:

-- Bind all key numbers to tags.
...
if tags[screen][i] then
    awful.tag.viewonly(tags[screen][i])
    grab_focus()
end
Related Question