-
-
Save hishamhm/b948ef9e6e8df30d3e3ab8aacff4a80d to your computer and use it in GitHub Desktop.
xrandr.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local abstk = require("abstk") | |
--abstk.set_mode("curses") | |
local mm = require("mm") | |
local pd = io.popen("xrandr", "r") | |
local state = "screen" | |
local data = {} | |
local curscr = nil | |
local curmon = nil | |
local parser | |
parser = { | |
screen = function(line) | |
local s_nr, minx, miny, curx, cury, maxx, maxy = line:match("^Screen (%d+): minimum (%d+) x (%d+), current (%d+) x (%d+), maximum (%d+) x (%d+)") | |
if s_nr then | |
curscr = { | |
id = s_nr, | |
min = { x = minx, y = miny }, | |
cur = { x = curx, y = cury }, | |
max = { x = maxx, y = maxy }, | |
} | |
data[s_nr] = curscr | |
state = "monitor" | |
end | |
end, | |
monitor = function(line) | |
if line:match("^Screen ") then | |
state = "init" | |
return parser.init(line) | |
end | |
local m_id, connected, rest = line:match("^([^ ]+) (d?i?s?connected)(.*)$") | |
if m_id then | |
local w, h, x, y = rest:match("(%d+)x(%d+)%+(%d+)%+(%d+)") | |
if not w then | |
w, h, x, y = 0, 0, 0, 0 | |
end | |
curmon = { | |
id = m_id, | |
w = w, h = h, x = x, y = y | |
} | |
curscr[m_id] = curmon | |
table.insert(curscr, curmon) | |
state = "resolution" | |
end | |
end, | |
resolution = function(line) | |
local w, h, freqs = line:match("^ (%d+)x(%d+)%s+(.*)$") | |
if w then | |
local key = w.."x"..h | |
local res = { | |
id = key, | |
sortkey = tonumber(w) * tonumber(h) * 1000 + #curmon, | |
w = w, h = h, | |
frequencies = {} | |
} | |
curmon[w.."x"..h] = res | |
table.insert(curmon, res) | |
for hz, cur, pref in freqs:gmatch("([%d.]+)(%*?)(%+?)") do | |
table.insert(res.frequencies, { hz = hz, cur = cur == "*", pref = pref == "+" }) | |
if cur == "*" then | |
res.freq = hz | |
end | |
end | |
else | |
state = "monitor" | |
return parser[state](line) | |
end | |
end, | |
} | |
for line in pd:lines() do | |
parser[state](line) | |
end | |
local scr = abstk.new_screen("XRandr setup") | |
local screens = {} | |
for id, _ in pairs(data) do | |
table.insert(screens, id) | |
end | |
scr:create_selector("screens", "Screens", screens, 1, "Logical screens") | |
local monitors = {} | |
local cur_screen = data[screens[1]] | |
for _, mon in ipairs(cur_screen) do | |
table.insert(monitors, mon.id) | |
end | |
--mm(data) | |
local function get_resolutions(idx) | |
local resolutions = {} | |
local cur_monitor = cur_screen[monitors[idx]] | |
--mm(monitors) | |
for _, res in ipairs(cur_monitor) do | |
table.insert(resolutions, res.id) | |
end | |
return resolutions | |
end | |
scr:create_selector("monitors", "Monitors", monitors, 1, "Monitor connections", | |
function(_, idx) | |
scr:set_value("resolutions", get_resolutions(idx)) | |
end | |
) | |
scr:create_selector("resolutions", "Available resolutions", get_resolutions(1), 1) | |
scr:add_button("apply", "Apply", "Set the resolution", function() | |
local mon = scr:get_value("monitors") | |
local res = scr:get_value("resolutions") | |
scr:show_message_box("xrandr will attempt to change your resolution. If it fails, press Esc.") | |
os.execute("xrandr --output "..mon.." --mode "..res) | |
local answer = scr:show_message_box("Are you happy?", "YES_NO") | |
if answer ~= "YES" then | |
os.execute("xrandr --output "..mon.." --mode 1920x1080") -- FIXME | |
end | |
end) | |
scr:add_button("off", "Off", "Turn monitor off", function() | |
local mon = scr:get_value("monitors") | |
os.execute("xrandr --output "..mon.." --off") | |
end) | |
scr:run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment