AwesomeWM client created/removed callback

隐身守侯 提交于 2019-12-12 03:13:29

问题


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.


回答1:


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)



回答2:


  • "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)


来源:https://stackoverflow.com/questions/39306126/awesomewm-client-created-removed-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!