0

I am using awesome WM and I want to run a lua function after a client is created/deleted. Specifically, I want to change the name of a tag to the name of one of the clients that are on the tag.

I do this with a timer, but I think the best way to do this would be to register a callback function to awesomeWM that it will invoke when a client is created/removed.

Are there some hooks/callbacks that I can implement to tell awesome to do this for me?

---------------------------------------------UPDATE----------------------------------------

I tried using the signals, but i cant find the correct signal that changes calls my function AFTER the window is created and attached to the tag. I tried this with manage/unmanage tagged/untagged, and tag.new, etc, but no one helps.

Any ideas?

here is the code:

override_name_char = "<"
function tag_name_from_client(c)
  if string.match(c.name, "Mozilla Firefox") then
    return "Firefox"
  end

  if string.match(c.name, "Sublime Text") then
    return "Sublime"
  end

  if string.match(c.name, "/bin/bash") then
    return "Shell"
  end

  return ""
end


function tag_name_from_tag(tag)
   if tag.name:match(override_name_char) then
      return tag.name
   end
   for _, c in pairs(tag:clients()) do
     return "  "..tostring(awful.tag.getidx(tag)).." "..tag_name_from_client(c)
   end
   return tostring(awful.tag.getidx(tag))
end

function refresh_tag_name()
  for s = 1, screen.count() do
      for _,tag in pairs(awful.tag.gettags(s)) do
         tag.name = tag_name_from_tag(tag)
      end
  end
end

client.connect_signal("tagged", refresh_tag_name)
client.connect_signal("untagged", refresh_tag_name)

--tag_timer = timer({timeout = 0.5})
--tag_timer:connect_signal("timeout", function()
   --refresh_tag_name()
--end)
--tag_timer:start()

Thanks in advance for any help regarding this.

deller
  • 490
  • 3
  • 12
  • [Signals](https://awesomewm.org/wiki/Signals) and [`connect_signal`](https://awesomewm.org/doc/api/modules/awesome.html#connect_signal) are _probably_ what you're looking for, but Awesome's documentation is pretty questionable in some parts. – Oka Sep 03 '16 at 16:11

2 Answers2

0

One of possible ways for v3.5.6, try this in your rc.lua

local naughty = require("naughty")
client.connect_signal("manage", function (c)
    --filter client by class name
    if c.class:lower() == "gedit" then
        -- do things on client start
        naughty.notify({text = "Gedit launched!"})

        -- add exit signal for this client
        c:connect_signal("unmanage", function() naughty.notify({text = "Gedit closed!"}) end)
    end
end)
Worron
  • 181
  • 3
0
  • "A new client is created" is the manage signal.
  • "A new client was destroyed" is the unmanage signal.

So, something like the following (untested):

local function choose_name_for_tag(t)
  for _, c in ipairs(t:clients() do
    return "has client: " .. tostring(c.name or "unknown")
  end
  return "has no clients"
end

local function update_state()
  for _, t in pairs(root.tags()) do
    t.name = choose_name_for_tag(t)
  end
end
client.connect_signal("manage", update_state)
client.connect_signal("unmanage", update_state)
tag.connect_signal("tagged", function(t)
  t.name = choose_name_for_tag(t)
end)
tag.connect_signal("untagged", function(t)
  t.name = choose_name_for_tag(t)
end)
Uli Schlachter
  • 9,337
  • 1
  • 23
  • 39
  • this does not capture when creating a new window. I create a new window (Mod4+enter) and it doesnt seem to change the name of the tag. I posted the code to my question – deller Sep 06 '16 at 06:53
  • Your code from the answer should be correct (I fixed the code in my answer). The only problem that I can see is that your code only looks at the first client on a tag. Could it be that you have multiple clients on the same tag and you want a specific one to decide about the name of the tag? Also, what's your awesome version? This code will not work with awesome 3.4. – Uli Schlachter Sep 06 '16 at 08:12
  • The solution does not work when creating a new window (ex: Mod4 + enter). The client isnt yet created when the function is run. Any ideas? – deller Sep 07 '16 at 07:13
  • A brute-force fix would be to start a timer with a 0 seconds timeout, so: client.connect_signal("manage", function() local t = timer{timeout=0} t:connect_signal("timeout", function() t:stop() update_state()end) t:start() end) – Uli Schlachter Sep 07 '16 at 07:59