Version 3 of mRandr

Updated 2019-02-24 17:38:09 by marcDouglas

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 devices1 
                variable configs 
                variable devicesOff 
                variable configDir 
                variable configFile

        proc run { } {
                initialize
                readConfig
        }

        proc initialize { } {
                variable devices1 
                variable configs 
                variable devicesOff 
                variable configDir 
                variable configFile
                set configDir ~/.screenlayout/
                set configFile mRandr.sh
                set devices [list]
                set devicesOff [list]
        }
        proc readConfig { } {
                variable devices1 
                variable configs 
                variable devicesOff 
                variable configDir 
                variable configFile
                file mkdir $configDir
                set configFilePath [file join         $configDir $configFile]
                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]
                #        puts "xrandrLine:$xrandrLine"
                        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 
                variable configs 
                variable devicesOff
                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
                        lappend configs $tempConfig
                } else {
                        lappend devicesOff $tempDevice $tempConfig
                }
        
        }        

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

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

        proc run_xRandr { } {
                variable devices
                variable xresults
                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 processConfig { configDevices } {
                set i 0
                foreach {device config} $configDevices {
                        puts "device\[$i\]: $device"
                        puts "config\[$i\]: $config"
                        setDisplayVars $device $config
                        incr i
                }
        }        
        proc devices { } {
                variable devices
                return $devices
        }
        proc runCommand { } {
                variable devices
                exec xrandr {*}$devices
        }

}
mRandrConfig::run
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