Skip to content

Instantly share code, notes, and snippets.

@nimag42
Last active June 6, 2022 09:45
Show Gist options
  • Save nimag42/464cd3d527a7fd8e20d1419a0bfb0cbf to your computer and use it in GitHub Desktop.
Save nimag42/464cd3d527a7fd8e20d1419a0bfb0cbf to your computer and use it in GitHub Desktop.
AwesomeWM binding to configure positions of Monitors
-- This snippet is a code I wrote to configure the position of monitors with AwesomeWM
-- Usage :
-- Press your Display button (or whatever you binded it to)
-- Popup will show the name of a connected monitor
-- Press an arrow key or Display button to set this monitor in this direction relatively to your main monitor, (display for same position)
-- It will continue until you have configured all connected monitors
-- the name of your main screen as shown in xrandr
main_screen = "eDP1"
-- Helper function to conf a screen
function conf_screen(screen, mode, pos)
if screen == nil then cmd = "xrandr --auto"
else
cmd = "xrandr --output " .. screen .. " --mode " .. mode .. " --" .. pos .. " " .. main_screen
end
awful.spawn(cmd)
end
-------------------------------
-- Add this in your globalkeys
awful.key({}, "XF86Display",
function ()
-- Command to get list of other screens and best mode
-- hack to handle when monitors mode are not sorted, like my TV does.
local get_screens = "xrandr | awk '/ connected/{screen=$1;a=1;next}/connected/{a=0}a{print screen \" \" $1}' | sort -k2n | tac | sort -u -k1,1 | grep -v " .. main_screen
awful.spawn.easy_async_with_shell(get_screens,
function(stdout, stderr, reason, exit_code)
-- Process the results
screens = {}
for i in stdout:gmatch("[^\r\n]+") do
a, b = i:match("(%S+) (%S+)")
table.insert(screens, {a, b})
end
-- If no screens, reset conf
if #screens == 0 then conf_screen(); return end
-- For each screens, ask position
local idx = 1
local notif = naughty.notify { text = "Position for " .. screens[idx][1] }
local grabber
grabber = awful.keygrabber.run(function(mod, key, event)
if event == "release" then return end
if key == "XF86Display" then conf_screen(screens[idx][1], screens[idx][2], "same-as")
elseif key == 'Up' then conf_screen(screens[idx][1], screens[idx][2], "above")
elseif key == 'Down' then conf_screen(screens[idx][1], screens[idx][2], "below")
elseif key == 'Right' then conf_screen(screens[idx][1], screens[idx][2], "right-of")
elseif key == 'Left' then conf_screen(screens[idx][1], screens[idx][2], "left-of")
else return
end
if idx < #screens then
idx = idx + 1
naughty.destroy(notif)
notif = naughty.notify { text = "Position for " .. screens[idx][1] }
else
naughty.destroy(notif)
awful.keygrabber.stop(grabber)
end
end)
end)
end,
{description = "Display management auto", group = "function keys"})
@Shardj
Copy link

Shardj commented Jun 6, 2022

Where's that top bit of the code meant to go?

main_screen = "eDP1"

-- Helper function to conf a screen
function conf_screen(screen, mode, pos)
   if screen == nil then cmd = "xrandr --auto"
   else
      cmd = "xrandr --output " .. screen .. " --mode " .. mode .. " --" .. pos .. " " .. main_screen
   end
   awful.spawn(cmd)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment