Created
April 4, 2016 12:22
-
-
Save moretea/96873caeb5871744001b9bc3d6464188 to your computer and use it in GitHub Desktop.
Small ruby script to encode screen setups.
This file contains hidden or 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
[maarten@maarten-laptop:~]$ cat `which extscreen` | |
#!/usr/bin/env ruby | |
require 'pathname' | |
require 'digest' | |
BASE_CARD_PATH = "/sys/class/drm/card0-" | |
SETUPS = { | |
"desktop" => { | |
screens: { | |
"HDMI-A-1" => "d17e9077f6e649495c6351b380ff5feeeb2076d49a5a8dcf7825733a0bfe5e03", | |
"DP-2" => "4608940ef02fd1627b7425047b7cf1ad51038d52bbf7bd76b23a5a0769768811" | |
}, | |
command: "xrandr --output LVDS1 --off --output HDMI1 --auto --rotate right --output DP2 --auto --rotate right --right-of HDMI1" | |
}, | |
"woonkamer" => { | |
screens: { | |
"LVDS-1" => "413edd161b6a0af2afb86570904f67240e4586eab8d29f0facb440055d39a9af", | |
"DP-2" => "53453c918410de8e0ef2191c876eb30d7782cade67515fde9cedc971339b290d", | |
}, | |
command: "xrandr --output LVDS1 --auto --output HDMI1 --off --output DP2 --auto --rotate left --right-of LVDS1" | |
}, | |
"laptop" => { | |
screens: { | |
"LVDS-1" =>"413edd161b6a0af2afb86570904f67240e4586eab8d29f0facb440055d39a9af" | |
}, | |
command: "xrandr --output DP2 --off --output HDMI1 --off --output LVDS1 --auto" | |
}, | |
} | |
class Device | |
def initialize(path) | |
@path = Pathname.new(path) | |
end | |
def name | |
@path.to_s.sub(BASE_CARD_PATH,"") | |
end | |
def connected? | |
File.read(@path.join("status")).chomp == "connected" | |
end | |
def raw_edid | |
File.read(@path.join("edid")).chomp | |
end | |
def hex_edid | |
Digest::SHA256.hexdigest(self.raw_edid) | |
end | |
end | |
DEVICES = Dir[BASE_CARD_PATH+"*"].map do |dir| | |
Device.new(dir) | |
end | |
def find_dev_by_name(name) | |
DEVICES.find { |d| d.name == name } | |
end | |
def main | |
verb = ARGV.shift | |
verb = "help" if verb.nil? | |
case verb | |
when "help" | |
puts "USAGE: #{$PROGRAM_NAME} [apply <configuration name> | list | connected]" | |
when "list" | |
verb_list | |
when "apply" | |
verb_apply | |
when "connected" | |
verb_connected | |
else | |
$stderr.puts "Unknown verb #{verb}" | |
exit 1 | |
end | |
end | |
def verb_list | |
require 'pp' | |
pp SETUPS | |
end | |
def verb_connected | |
connected = DEVICES.select { |d| d.connected? } | |
connected.each do |device| | |
p [device.name, device.hex_edid] | |
end | |
end | |
def verb_apply | |
config = ARGV.shift | |
if config.nil? | |
config = find_matching_setup | |
end | |
matching_setup = SETUPS[config] | |
if matching_setup.nil? | |
$stderr.puts("Wrong setup name '#{config}'!") | |
exit 1 | |
end | |
# Apply configuration | |
puts "Applying '#{config}'" | |
system(matching_setup[:command]) | |
end | |
# Detect top down the first matching setup. | |
def find_matching_setup | |
found = SETUPS.find do |setup_name, setup| | |
ok = true | |
setup[:screens].each do |screen_name, edid| | |
if find_dev_by_name(screen_name).hex_edid != edid | |
ok = false | |
end | |
end | |
ok | |
end | |
if found.nil? | |
raise("Could not find a suitable setup!") | |
else | |
found[0] | |
end | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment