2017-01-27 02:26:40 +01:00
|
|
|
--- Separating Multiple Monitor functions as a separeted module (taken from awesome wiki)
|
|
|
|
|
2019-06-26 13:29:33 +02:00
|
|
|
local gtable = require("gears.table")
|
|
|
|
local spawn = require("awful.spawn")
|
|
|
|
local naughty = require("naughty")
|
2017-01-27 02:26:40 +01:00
|
|
|
|
|
|
|
-- A path to a fancy icon
|
2017-01-27 20:34:26 +01:00
|
|
|
local icon_path = ""
|
2017-01-27 02:26:40 +01:00
|
|
|
|
|
|
|
-- Get active outputs
|
|
|
|
local function outputs()
|
|
|
|
local outputs = {}
|
|
|
|
local xrandr = io.popen("xrandr -q --current")
|
|
|
|
|
|
|
|
if xrandr then
|
|
|
|
for line in xrandr:lines() do
|
|
|
|
local output = line:match("^([%w-]+) connected ")
|
|
|
|
if output then
|
|
|
|
outputs[#outputs + 1] = output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
xrandr:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
return outputs
|
|
|
|
end
|
|
|
|
|
|
|
|
local function arrange(out)
|
|
|
|
-- We need to enumerate all permutations of horizontal outputs.
|
|
|
|
|
|
|
|
local choices = {}
|
|
|
|
local previous = { {} }
|
|
|
|
for i = 1, #out do
|
|
|
|
-- Find all permutation of length `i`: we take the permutation
|
|
|
|
-- of length `i-1` and for each of them, we create new
|
|
|
|
-- permutations by adding each output at the end of it if it is
|
|
|
|
-- not already present.
|
|
|
|
local new = {}
|
|
|
|
for _, p in pairs(previous) do
|
|
|
|
for _, o in pairs(out) do
|
2019-06-26 13:26:43 +02:00
|
|
|
if not gtable.hasitem(p, o) then
|
|
|
|
new[#new + 1] = gtable.join(p, {o})
|
2017-01-27 02:26:40 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-06-26 13:26:43 +02:00
|
|
|
choices = gtable.join(choices, new)
|
2017-01-27 02:26:40 +01:00
|
|
|
previous = new
|
|
|
|
end
|
|
|
|
|
|
|
|
return choices
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Build available choices
|
|
|
|
local function menu()
|
|
|
|
local menu = {}
|
|
|
|
local out = outputs()
|
|
|
|
local choices = arrange(out)
|
|
|
|
|
|
|
|
for _, choice in pairs(choices) do
|
|
|
|
local cmd = "xrandr"
|
|
|
|
-- Enabled outputs
|
|
|
|
for i, o in pairs(choice) do
|
|
|
|
cmd = cmd .. " --output " .. o .. " --auto"
|
|
|
|
if i > 1 then
|
|
|
|
cmd = cmd .. " --right-of " .. choice[i-1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- Disabled outputs
|
|
|
|
for _, o in pairs(out) do
|
2019-06-26 13:26:43 +02:00
|
|
|
if not gtable.hasitem(choice, o) then
|
2017-01-27 02:26:40 +01:00
|
|
|
cmd = cmd .. " --output " .. o .. " --off"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local label = ""
|
|
|
|
if #choice == 1 then
|
|
|
|
label = 'Only <span weight="bold">' .. choice[1] .. '</span>'
|
|
|
|
else
|
|
|
|
for i, o in pairs(choice) do
|
|
|
|
if i > 1 then label = label .. " + " end
|
|
|
|
label = label .. '<span weight="bold">' .. o .. '</span>'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-28 17:57:21 +01:00
|
|
|
menu[#menu + 1] = { label, cmd }
|
2017-01-27 02:26:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return menu
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Display xrandr notifications from choices
|
2017-01-28 18:11:20 +01:00
|
|
|
local state = { cid = nil }
|
|
|
|
|
|
|
|
local function naughty_destroy_callback(reason)
|
|
|
|
if reason == naughty.notificationClosedReason.expired or
|
|
|
|
reason == naughty.notificationClosedReason.dismissedByUser then
|
|
|
|
local action = state.index and state.menu[state.index - 1][2]
|
|
|
|
if action then
|
2019-06-26 13:29:33 +02:00
|
|
|
spawn(action, false)
|
2017-01-28 18:11:20 +01:00
|
|
|
state.index = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-01-27 02:26:40 +01:00
|
|
|
|
2017-01-28 18:11:20 +01:00
|
|
|
local function xrandr()
|
2017-01-27 02:26:40 +01:00
|
|
|
-- Build the list of choices
|
|
|
|
if not state.index then
|
|
|
|
state.menu = menu()
|
|
|
|
state.index = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Select one and display the appropriate notification
|
2017-01-28 17:57:21 +01:00
|
|
|
local label, action
|
2017-01-27 02:26:40 +01:00
|
|
|
local next = state.menu[state.index]
|
|
|
|
state.index = state.index + 1
|
|
|
|
|
|
|
|
if not next then
|
2017-01-28 17:57:21 +01:00
|
|
|
label = "Keep the current configuration"
|
2017-01-27 20:34:26 +01:00
|
|
|
state.index = nil
|
2017-01-27 02:26:40 +01:00
|
|
|
else
|
2019-06-26 13:29:42 +02:00
|
|
|
label, action = next[1], next[2]
|
2017-01-27 02:26:40 +01:00
|
|
|
end
|
|
|
|
state.cid = naughty.notify({ text = label,
|
2017-01-28 17:57:21 +01:00
|
|
|
icon = icon_path,
|
2017-01-27 02:26:40 +01:00
|
|
|
timeout = 4,
|
|
|
|
screen = mouse.screen,
|
2017-01-28 18:11:20 +01:00
|
|
|
replaces_id = state.cid,
|
|
|
|
destroy = naughty_destroy_callback}).id
|
2017-01-27 02:26:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
outputs = outputs,
|
|
|
|
arrange = arrange,
|
|
|
|
menu = menu,
|
|
|
|
xrandr = xrandr
|
|
|
|
}
|