mRandr

I have 3 display ports: HDMI, DVI, DisplayPort.

They switch names slighty based on when they are 'discovered' at boot.

This fixes that problem, but only if you have HDMI, DVI, DisplayPort .. or edit to accomodate.

this requires you have a aRandr config or manually generated xRandr command line saved in a text file at

~/.screenlayout/mRandr.sh

marcDouglas

#! /usr/bin/env tclsh

puts "Welcome to mRandr"

namespace eval mRandrConfig {
        variable configDir 
        variable configFile
        variable devices

        set configDir ~/.screenlayout/
        set configFile mRandr.sh
        set devices [list]

        proc readConfig { } {
                variable configDir 
                variable configFile
                file mkdir $configDir
                set configFilePath [file join         $configDir $configFile]
                puts "reading in $configFilePath..."
                if {[catch {open $configFilePath r} data]} {
                        puts "Error readoing config file. \[$configFilePath\]"
                }
                while { [gets $data line] >= 0 } {
                        if { [string first xrandr $line] == 0} {
                                set xrandrLine $line
                        }
                }
                while {[set outLocation [string first --output $xrandrLine]] >= 0} {
                        set xrandrLine [string range $xrandrLine $outLocation+9 end]
                        if {[set outLocation [string first --output $xrandrLine]] >= 0} {
                                set tempDevice [string range $xrandrLine 0 $outLocation-2]
                                processDevice $tempDevice
                                set xrandrLine [string range $xrandrLine $outLocation-1 end]
                        } else {
                                processDevice $xrandrLine
                        }
                } 
        }
        proc processDevice { line } {
                variable devices 
                set outLocation2 [string first " " $line]
                set tempDevice [string range $line 0 $outLocation2-1]
                set tempConfig [string range $line $outLocation2+1 end]
                if {[set outLocation [string first --off $line]] < 0} {
                        lappend devices $tempDevice $tempConfig
                }
        }        

        proc devices { } {
                variable devices
                return $devices
        }        
}

namespace eval mRandr {
        variable devices
        variable configOptions
        set configOptions(DisplayPortXOptions) ""
        set configOptions(HDMIXOptions) ""
        set configOptions(DVIXOptions) ""

        proc processConfig { configDevices } {
                set i 0
                foreach {device config} $configDevices {
                        puts "device\[$i\]: $device"
                        puts "config\[$i\]: $config"
                        setDisplayVars $device $config
                        incr i
                }
        }        

        proc run_xRandr { } {
                variable devices
                variable configOptions
                puts -nonewline "Checking xrandr...  "
                if {[catch {set xresults [exec xrandr]} fid]} {
                    puts stderr "xrandr error: $fid"
                    exit 1
                } else {
                        puts "success."
                }
                set xresults [split $xresults "\n"]
                set devices ""
                foreach line $xresults {
                        set sourceScan [scan $line {%s %s}]
                        if {[lindex $sourceScan 1] == "connected"} {
                                append devices "[configureLine [lindex $sourceScan 0]] "
                                puts "Found [lindex $sourceScan 0]"
                        } elseif {[lindex $sourceScan 1] == "disconnected"} {
                                append devices "--output [lindex $sourceScan 0] --off "
                        }
                }        
        }

        proc configureLine { line } {
                variable configOptions
                switch -glob -- $line {
                        "DisplayPort*" {set configLine [subst $configOptions(DisplayPort)]}
                        "HDMI*" {set configLine [subst $configOptions(HDMI)] }
                  "DVI*" {set configLine [subst $configOptions(DVI)]}
                   default    { set configLine ""}
                }
                return $configLine
        }

        proc setDisplayVars { device config } {
                variable configOptions
                switch -glob -- $device {
                        "DisplayPort*" {set configOptions(DisplayPort) "--output \$line $config \$configOptions(DisplayPortXOptions)"}
                        "HDMI*" {set configOptions(HDMI) "--output \$line $config \$configOptions(HDMIXOptions)" }
                  "DVI*" {set configOptions(DVI) "--output \$line $config \$configOptions(DVIXOptions)"}
                }
        }

        proc setHDMIXOptions { newOptions } {
                variable configOptions
                set configOptions(HDMIXOptions) $newOptions
        }

        proc setDisplayPortXOptions { newOptions } {
                variable configOptions
                set configOptions(DisplayPortXOptions) $newOptions
        }        

        proc setDVIXOptions { newOptions } {
                variable configOptions
                set configOptions(DVIXOptions) $newOptions
        }

        proc devices { } {
                variable devices
                return $devices
        }
        proc runCommand { } {
                variable devices
                exec xrandr {*}$devices
        }

}

mRandrConfig::readConfig
mRandr::processConfig [mRandrConfig::devices]
mRandr::setHDMIXOptions "--set underscan on --set \"underscan hborder\" 50 --set \"underscan vborder\" 30"
mRandr::run_xRandr
puts "xrandr [mRandr::devices]"
mRandr::runCommand