-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiscreen.lua
148 lines (132 loc) · 3.66 KB
/
multiscreen.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
-- Get active outputs
local awful=require("awful")
local naughty=require("naughty")
local function outputs()
local outputs = {}
local xrandr = io.popen("xrandr -q")
if xrandr then
for line in xrandr:lines() do
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 the way to combinate output. We assume
-- we want only an horizontal layout.
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
if not awful.util.table.hasitem(p, o) then
new[#new + 1] = awful.util.table.join(p, {o})
end
end
end
choices = awful.util.table.join(choices, new)
previous = new
end
return choices
end
local function myarrange(choices)
mychoices={}
for _, p in pairs(choices) do
if #p ==1 or p[1]=="LVDS1" then
mychoices=awful.util.table.join(mychoices, {p})
end
end
return mychoices
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
if not awful.util.table.hasitem(choice, o) then
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
menu[#menu + 1] = { label,
cmd,
"/usr/share/icons/Faenza/devices/32/display.png"}
end
return menu
end
-- Display xrandr notifications from choices
local state = { iterator = nil,
timer = nil,
cid = nil }
function xrandr()
-- Stop any previous timer
if state.timer then
state.timer:stop()
state.timer = nil
end
-- Build the list of choices
if not state.iterator then
state.iterator = awful.util.table.iterate(menu(),
function() return true end)
end
-- Select one and display the appropriate notification
local next = state.iterator()
local label, action, icon
if not next then
label, icon = "Keep the current configuration", "/usr/share/icons/Faenza/devices/32/display.png"
state.iterator = nil
else
label, action, icon = unpack(next)
end
state.cid = naughty.notify({ text = label,
icon = icon,
timeout = 4,
screen = mouse.screen, -- Important, not all screens may be visible
--font = "Free Sans 12",
height = 80,
width = 200,
replaces_id = state.cid }).id
-- Setup the timer
state.timer = timer { timeout = 4 }
state.timer:connect_signal("timeout",
function()
state.timer:stop()
state.timer = nil
state.iterator = nil
if action then
awful.util.spawn(action, false)
end
end)
state.timer:start()
end
--config.keys.global = awful.util.table.join(
--config.keys.global,
--awful.key({}, "XF86Display", xrandr))