Version 0 of mRandr

Updated 2019-02-24 16:42:01 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

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 {
        proc run { } {
                initialize
                processResults
                devices
                runCommand
        }
        proc initialize { } {
                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 "sucess."
                }
                set xresults [split $xresults "\n"]
                set devices ""
        }
        proc processResults { } {
                variable devices
                variable xresults
                foreach line $xresults {
                        set sourceScan [scan $line {%s %s %s %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"}
                        "HDMI*" {set configOptions(HDMI) "--output \$line $config" }
                  "DVI*" {set configOptions(DVI) "--output \$line $config"}
                }
        }

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

        proc setConfigDevices { nConfigDevices } {
                variable configDevices
                set configDevices $nConfigDevices
        }

        proc setConfigDevicesOff { nConfigDevicesOff } {
                variable configDevicesOff
                set configDevicesOff $nConfigDevicesOff
        }
        
        proc devices { } {
                variable devices
                puts $devices
                return $devices
        }
        proc runCommand { } {
                variable devices
                exec xrandr {*}$devices
        }

}
mRandrConfig::run
mRandr::setConfigDevices [mRandrConfig::devices]
mRandr::processConfig
mRandr::run