This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxrandr.lua
241 lines (219 loc) · 7.57 KB
/
xrandr.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
local edid = require('foggy.edid')
local cmd
local status, cmd_fun = pcall(function()
local awful = require('awful')
return awful.util.spawn_with_shell
end)
if status then
cmd = cmd_fun
else
cmd = print
end
local xrandr = {
_NAME = "foggy.xrandr",
actions = {}
}
local function printf(format, ...)
print(string.format(format, ...))
end
local function parse_transformations(text, assume_normal)
local rot = { normal = (assume_normal or false), left = false, right = false, inverted = false}
local refl = { x = false, y = false, normal = (assume_normal or false) }
for word in text:gmatch("(%w+)") do
for k, _v in pairs(rot) do
if k == word then rot[k] = true end
end
for k, _v in pairs(refl) do
if k == word:lower() then refl[k] = true end
end
end
return { rotations = rot, reflections = refl }
end
function xrandr.info(fp)
local info = { screens = {}, outputs = {} }
local current_output
local last_property
local pats = {
['^Screen (%d+): minimum (%d+) x (%d+), current (%d+) x (%d+), maximum (%d+) x (%d+)$'] = function(matches)
-- X screens. Usually just one, when used with Xinerama
info.screens[tonumber(matches[1])] = {
minimum = { tonumber(matches[2]), tonumber(matches[3]) },
resolution = { tonumber(matches[4]), tonumber(matches[5]) },
maximum = { tonumber(matches[6]), tonumber(matches[7]) }
}
end,
['^([-%a%d]+) connected ([%S]-)%s*(%d+)x(%d+)+(%d+)+(%d+)%s*(.-)%(([%a%s]+)%) (%d+)mm x (%d+)mm$'] = function(matches)
-- Match connected and active outputs
current_output = {
name = matches[1],
resolution = { tonumber(matches[3]), tonumber(matches[4]) },
offset = { tonumber(matches[5]), tonumber(matches[6]) },
transformations = parse_transformations(matches[7]),
available_transformations = parse_transformations(matches[8], false),
physical_size = { tonumber(matches[9]), tonumber(matches[10]) },
connected = true,
on = true,
primary = (matches[2] == 'primary'),
modes = {},
properties = {},
edid = ''
}
info.outputs[matches[1]] = current_output
end,
['^([-%a%d]+) connected %(([%a%s]+)%)$'] = function(matches)
-- DVI-1 connected (normal left inverted right x axis y axis)
-- Match outputs that are connected but disabled
current_output = {
name = matches[1],
available_transformations = parse_transformations(matches[2], false),
transformations = parse_transformations(''),
modes = {},
connected = true,
on = false,
properties = {},
edid = ''
}
info.outputs[matches[1]] = current_output
end,
['^([-%a%d]+) disconnected .*%(([%a%s]+)%)$'] = function(matches)
-- Match disconnected outputs
current_output = {
available_transformations = parse_transformations(matches[2], false),
connected = false, on = false,
properties = {},
edid = ''
}
info.outputs[matches[1]] = current_output
end,
['^%s%s%s(%d+)x(%d+)%s+(.+)$'] = function(matches)
-- Match modelines. Only care about resolution and refresh.
local w = tonumber(matches[1])
local h = tonumber(matches[2])
for refresh, symbols in matches[3]:gmatch('([%d.]+)(..)') do
local mode = { w, h, math.floor(tonumber(refresh)) }
local modes = current_output.modes
modes[#modes + 1] = mode
if symbols:find('%*') then
current_output.current_mode = mode
end
if symbols:find('%+') then
current_output.default_mode = mode
end
end
end,
['^\t(.+):%s+(.+)%s+$'] = function(matches)
-- Match properties, which are rather freeform
last_property = matches[1]
local properties = current_output.properties
properties[last_property] = { value = matches[2] }
end,
['^\t\tsupported:%s+(.+)$'] = function(matches)
-- Match supported property values, freeform but comma separated
if last_property ~= nil then
local prop = current_output.properties[last_property]
local supported = { }
for word in matches[1]:gmatch('([^,]+),?%s?') do
supported[#supported + 1] = word
end
prop.supported = supported
end
end,
['^\t\t(' .. string.rep('[0-9a-f]', 32) .. ')$'] = function(matches)
-- Match EDID block
current_output.edid = current_output.edid .. matches[1]
end,
['^\t\trange:%s+%((%d+), (%d+)%)$'] = function(matches)
-- Match ranged property values, e.g. brightness
if last_property ~= nil then
local prop = current_output.properties[last_property]
local range = { tonumber(matches[1]), tonumber(matches[2]) }
prop.range = range
end
end
}
fp = fp or io.popen('xrandr --query --prop', 'r')
for line in fp:lines() do
for pat, func in pairs(pats) do
local res
res = {line:find(pat)}
if #res > 0 then
table.remove(res, 1)
table.remove(res, 1)
func(res)
break
end
end
end
return info
end
function xrandr.actions.set_mode(name, mode)
cmd(string.format('xrandr --output %s --mode %dx%d --rate %d', name, mode[1], mode[2], mode[3]))
end
function xrandr.actions.auto_mode(name)
cmd(string.format('xrandr --output %s --auto', name))
end
function xrandr.actions.off(name)
cmd(string.format('xrandr --output %s --off', name))
end
function xrandr.actions.set_rotate(name, rot)
cmd(string.format('xrandr --output %s --rotate %s', name, rot))
end
function xrandr.actions.set_reflect(name, refl)
cmd(string.format('xrandr --output %s --reflect %s', name, refl))
end
function xrandr.actions.set_relative_pos(name, relation, other)
cmd(string.format('xrandr --output %s --%s %s', name, relation, other))
end
function xrandr.actions.set_primary(name)
cmd(string.format('xrandr --output %s --primary', name))
end
function xrandr.actions.set_property(name, prop, value)
cmd(string.format("xrandr --output %s --set '%s' '%s'", name, prop, value))
end
function xrandr.actions.set_backlight(name, value)
xrandr.actions.set_property(name, 'BACKLIGHT', value)
end
function xrandr.actions.identify_outputs(timeout)
timeout = timeout or 3
local wibox = require("wibox")
local awful = require("awful")
for name, output in pairs(xrandr.info().outputs) do
if output.connected and output.on then
local textbox = wibox.widget.textbox()
local box = wibox({fg = '#ffffff', bg = '#0000007f'})
local layout = wibox.layout.fixed.horizontal()
textbox:set_font('sans 36')
local text
if output.edid ~= "" then
local monitor_name = edid.monitor_name(output.edid)
if monitor_name then
text = name .. "\n" .. edid.monitor_name(output.edid)
else
text = name
end
else
text = name
end
textbox:set_markup(text)
textbox:set_align("center")
layout:add(textbox)
box:set_widget(layout)
local screen = awful.screen.getbycoord(output.offset[1] + 1, output.offset[2] + 1)
local w, h = textbox:get_preferred_size(screen)
local xoff = (output.resolution[1] - w) / 2
local yoff = (output.resolution[2] - h) / 2
box:geometry({x = output.offset[1] + xoff, y = output.offset[2] + yoff, width=w, height=h})
box.ontop = true
box.visible = true
local tm = timer({timeout=timeout})
tm:connect_signal('timeout', function()
box.visible = false
tm:stop()
box = nil
tm = nil
end)
tm:start()
end
end
end
return xrandr