AwesomeWM

<< Back to wiki homepage

Table of contents:

Helpful functions

Keep mouse while changing screens

There is no real support for it, so you need to go manual and save the current location + re-apply it after focus change:

local function switch_focus_without_mouse(c, dir)
    -- Get the mouse position
    mouselocation_x = mouse.coords().x
    mouselocation_y = mouse.coords().y
    awful.client.focus.global_bydirection(dir, c, true)
    -- Keep the mouse position
    mouse.coords {
        x = mouselocation_x,
        y = mouselocation_y
    }
end

Quickly put a client on top (e.g. zoom/teams calls etc.)

This sets it on top + sticky so client always hangs there. Also reverts on second run.

local function float_toggle(c)
    awful.client.floating.toggle()
    if c.floating then
        c.ontop=true
        c.sticky=true
        c.skip_taskbar=true
        c.width=533
        c.height=860
        awful.placement.top_right(client.focus)
    else
        c.ontop=false
        c.sticky=false
        c.skip_taskbar=false
    end
end

Suspend a client (SIGSTOP)

Suspends and puts client on color. Reverts on second run.

local function suspend_toggle(c)
    awful.spawn.easy_async(
    "ps -q " .. c.pid .. " -o state --no-headers",
    function(stdout, stderr, reason, exit_code)
        if stdout == "T\n" then
        -- suspended, let's wake up
        c.border_color = beautiful.border_normal
        c.border_width = beautiful.border_width
        c.opacity = 1
        naughty.notify { text = "waking up client: " .. c.class .. " (" .. c.pid .. ")"}
        awful.spawn("kill -18 " .. c.pid)
        else
        -- working, let's suspend
        c.border_color = '#ff0000'
        c.border_width = 10
        c.opacity = 0.8
        naughty.notify { text = "suspending client: " .. c.class .. " (" .. c.pid .. ")"}
        awful.spawn("kill -19 " .. c.pid)
        end
    end
    )
end

Restart awesomewm on docking multiple monitors (timer example)

This is a dirty workaround instead of using screen.connect_signal("added" etc. because if you're plugging/removing multiple monitors at once (e.g. docking station), you'll have weird issues due to multiple very-close restarts.

docked = false
if screen:count() > 1 then
    docked = true
end

local function check_available_screens()
    if ( docked and screen:count() == 1 ) or ( not docked and screen:count() == 2 ) then
        print(">>>>>> Detected docking change, restarting awesomewm")
        awesome.restart()
    end
end

restart_timer = gears.timer {
    timeout = 3,
    autostart = true,
    callback = function()
        check_available_screens()
    end
}

psi_timer = gears.timer {
    timeout = 15,
    autostart = true,
    callback = function()
        psi_widget:check()
    end
}

Set wallpaper on a screen (randomly from a folder)

Accepts screen object as input. You can bind it to a timer if you want it to change from time to time.

-- Needed helper function
function get_randomseed(b, m, r)
  urand = assert (io.open ('/dev/urandom', 'rb'))
  b = b or 4
  m = m or 256
  r = r or urand
  local n, s = 0, r:read (b)

  for i = 1, s:len () do
    n = m * n + s:byte (i)
  end

  return n
end

function set_wallpaper(s)
  -- choose random wallpaper
  awful.spawn.easy_async(
    "find /my/wallpaper/folder -not -path 'phone*' -type f",
    function(stdout, stderr, reason, exit_code)
      if exit_code == 0 then
        local wallpapers = {}
        for wp in stdout:gmatch("[^\r\n]+") do
          table.insert(wallpapers, wp)
        end
        -- lua is the shittiest language ever
        -- you need to set seed again, or the "random" will always return same
        math.randomseed(get_randomseed())
        gears.wallpaper.maximized(wallpapers[math.random(#wallpapers)], s)
      else
        naughty.notify({text = "Wallpaper assign error: " .. stderr})
      end
    end)
end

Keep the tags after restart

function save_current_tag()
    active_tags = {}
    for s in screen do
        os.remove("/home/gurkan/.awesome-last-ws")
        for _, tagobj in pairs(s.selected_tags) do
            table.insert(active_tags, tagobj)
        end
    end
    local f = assert(io.open("/home/gurkan/.awesome-last-ws", "a+"))
    for _, tagobj in pairs(active_tags) do
        f:write(tagobj.name, "\n")
    end
    f:close()
end

function load_last_active_tag()
    tag_list = my_utils.read_lines_from("/home/gurkan/.awesome-last-ws")
    if next(tag_list) ~= nil then
        local previous_tags = {}
        for _, tag_name in pairs(tag_list) do
            local t = awful.tag.find_by_name(nil, tag_name)
            table.insert(previous_tags, t)
        end
        awful.tag.viewnone()
        awful.tag.viewmore(previous_tags)
    end
end

awesome.connect_signal("exit", function (c)
  -- We are about to exit / restart awesome, save our last used tag
  save_current_tag()
end)

And add load_tast_active_tag() function at the end of your rc.lua

Escape from mouse

Make some clients run away from mouse to other side of the screen (e.g. marked, sticky, whatever):

client.connect_signal("mouse::enter", function (c)
  if c.ontop and c.sticky and c.skip_taskbar and c.marked then -- < change this to your case
    c.opacity = 0.9
    -- Run away from mouse, to the other side of the screen
    if c.x > (c.screen.geometry.x + c.screen.geometry.width - 600) then
            c:relative_move(-(c.screen.geometry.width - c.width), 0, 0, 0)
        else
            c:relative_move((c.screen.geometry.width - c.width), 0, 0, 0)
        end
  end
end)