You are not logged in.

#1 2013-10-21 00:41:53

originalsurfmex
Member
Registered: 2008-11-13
Posts: 150

looking for an elegant dual monitor auto-configure solution (SOLVED)

I am currently using XFCE becuase Gnome and KDE are both too heavy for my weak little laptop with intel onboard graphics.  The number one thing I miss is auto-detect and configuration when I plug or unplug my monitor.

I can't find anything that works automatically in an elegant way, the only thing I can do is poll the system every second.  There appears to be a way to make a udev rule but that is a little bit out of my depth.

My current solution is below, does anybody have a suggestion for something more elegant?  It would be awesome if there was something in the AUR.

#!/bin/bash

#inspired of: 
#   http://unix.stackexchange.com/questions/4489/a-tool-for-automatically-applying-randr-configuration-when-external-display-is-p
#   http://ozlabs.org/~jk/docs/mergefb/

dmode="$(cat /sys/class/drm/card0-VGA-1/status)"                        
export DISPLAY=:0                                                       
export XAUTHORITY=~/.Xauthority  

# actual script
while true
do
    if [ "${dmode}" = disconnected ]; then                                  
         /usr/bin/xrandr --auto                                              
    elif [ "${dmode}" = connected ];then                                    
         /usr/bin/xrandr --output VGA1 --auto --right-of LVDS1               
    else /usr/bin/xrandr --auto                                             

    fi

    sleep 1s
done

-----

SOLVED using inotify see below and thanks for all the help

#!/bin/bash

#inspired of: 
#   http://unix.stackexchange.com/questions/4489/a-tool-for-automatically-applying-randr-configuration-when-external-display-is-p
#   http://ozlabs.org/~jk/docs/mergefb/
#   http://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes/181543#181543
export MONITOR2=/sys/class/drm/card0-VGA-1/status

while inotifywait -e modify,create,delete,open,close,close_write,access $MONITOR2;

dmode="$(cat $MONITOR2)"

do
    if [ "${dmode}" = disconnected ]; then
         /usr/bin/xrandr --auto
         echo "${dmode}"
    elif [ "${dmode}" = connected ];then
         /usr/bin/xrandr --output VGA1 --auto --right-of LVDS1
         echo "${dmode}"
    else /usr/bin/xrandr --auto
         echo "${dmode}"
    fi
done

Last edited by originalsurfmex (2013-10-28 20:51:45)

Offline

#2 2013-10-21 00:47:23

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,535
Website

Re: looking for an elegant dual monitor auto-configure solution (SOLVED)

That script doesn't actuall work, does it?  That will evaluate the cat command only once, so "dmode" will always be set to what it was when the script was started.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2013-10-21 23:37:05

originalsurfmex
Member
Registered: 2008-11-13
Posts: 150

Re: looking for an elegant dual monitor auto-configure solution (SOLVED)

LOL - yes it works.  It checks every second for the state of the dmode.  I just unplugged my monitor and plugged it back in - the screens reconfigured themselves as expected.

Offline

#4 2013-10-23 02:00:34

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

Re: looking for an elegant dual monitor auto-configure solution (SOLVED)

Is this script really running all the time or are you triggering it somehow? Trilby is right, as written above it shouldn't work.

I just bind the monitor logic to a shortcut key.


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#5 2013-10-24 23:11:42

originalsurfmex
Member
Registered: 2008-11-13
Posts: 150

Re: looking for an elegant dual monitor auto-configure solution (SOLVED)

According to this site - http://www.cyberciti.biz/faq/bash-infinite-loop/ - it is a script set to run as an infinite loop.  I guess we could call it a ghetto example of polling.

Monitor auto-configuration is one of the few things that I do not want to mess with by hand.

So far I run the script and leave it running in a tmux session, or by running it in the background.  Examples:

$>sh autodualmonitor_polling.sh
$>sh ~/.screenlayout/autodualmonitor_polling.sh &

HTOP shows it as using:

RES 1768 
VIRT 13504 
SHR 1328
CPU% 0.0
MEM% 0.0

Without the script running I have no auto configuration of my monitors, with the script running I have auto configuration.  Sometimes there is a 1-2 second delay, but that must just be due to the polling aspect of it.

Last edited by originalsurfmex (2013-10-24 23:26:41)

Offline

#6 2013-10-24 23:54:46

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,535
Website

Re: looking for an elegant dual monitor auto-configure solution (SOLVED)

We're not questioning the loop.  But that doesn't evaluate the cat every time through the loop.  I wouldn't beleive it worked  at all except that what is in the dmode variable is actually irrelevant.  Your script is equivalent to running the following:

while true; do xrandr --auto; sleep 1; done

I'd look into a udev rule, or if not that, an inotify script that watches the file that you cat at the beginning of your script.

Last edited by Trilby (2013-10-24 23:56:11)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2013-10-25 03:04:11

originalsurfmex
Member
Registered: 2008-11-13
Posts: 150

Re: looking for an elegant dual monitor auto-configure solution (SOLVED)

Yup, you guys are completely right.  I tried changing out the variables to dmode and it turns out that as long as dmode = connected the script worked.  This meant that the script only worked as long as my second monitor was connected before running it, which is of course unnacceptable.

I have adjusted the script so that it works as intended.  Thanks very much for catching that it was broken.  I don't know how to use inotify or udev rules yet, if you have any suggestions please let me know.  Once I get some more time I'll try and learn those two methods of managing events.

Here is the newly minted script:

#!/bin/bash

while true
dmode="$(cat /sys/class/drm/card0-VGA-1/status)"
do

    if [ "${dmode}" = disconnected ]; then
         /usr/bin/xrandr --auto
         echo "${dmode}"
    elif [ "${dmode}" = connected ];then
         /usr/bin/xrandr --output VGA1 --auto --right-of LVDS1
         echo "${dmode}"
    else /usr/bin/xrandr --auto
         echo "${dmode}"
    fi

    sleep 1s
done

Offline

#8 2013-10-25 03:07:31

originalsurfmex
Member
Registered: 2008-11-13
Posts: 150

Re: looking for an elegant dual monitor auto-configure solution (SOLVED)

Thanks very much for pointing me to inotify.  This is exactly the type of solution I was looking for!  I wasn't too excited about creating some udev tool.  Here is the script using inotify:

#!/bin/bash

#inspired of: 
#   http://unix.stackexchange.com/questions/4489/a-tool-for-automatically-applying-randr-configuration-when-external-display-is-p
#   http://ozlabs.org/~jk/docs/mergefb/
#   http://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes/181543#181543
export MONITOR2=/sys/class/drm/card0-VGA-1/status

while inotifywait -e modify,create,delete,open,close,close_write,access $MONITOR2;

dmode="$(cat $MONITOR2)"

do
    if [ "${dmode}" = disconnected ]; then
         /usr/bin/xrandr --auto
         echo "${dmode}"
    elif [ "${dmode}" = connected ];then
         /usr/bin/xrandr --output VGA1 --auto --right-of LVDS1
         echo "${dmode}"
    else /usr/bin/xrandr --auto
         echo "${dmode}"
    fi
done

I am marking this as solved.  If you have more critique or suggestions I'd be open to improve the script.

Offline

#9 2013-10-28 16:55:36

SuperRaWR
Member
From: Sylvan Lake, AB
Registered: 2011-09-18
Posts: 9
Website

Re: looking for an elegant dual monitor auto-configure solution (SOLVED)

Rather than having a constantly polling script, why not create a udev rule to execute your script when it detects a monitor being plugged in?

I have the following in /etc/udev/rules.d/95-monitor-hotplug.rules

KERNEL=="card0", ACTION=="change" , RUN+="/usr/bin/autodetectscreenlayout"

You may have to use udevadm monitor in order to find the device that changes when you connect your monitors, mine happened to be named "card0".  My script "autodetectscreenlayout" is similar to what you have except there is no need to have it looping in the background.

Last edited by SuperRaWR (2013-10-28 16:56:22)

Offline

#10 2013-10-28 20:45:44

originalsurfmex
Member
Registered: 2008-11-13
Posts: 150

Re: looking for an elegant dual monitor auto-configure solution (SOLVED)

SuperRaWR,

Thanks very much for the udev suggestion, that was what I was looking for initially and I'm going to try that next.

I'd like to clarify that my current script posted above doesn't loop in the background, instead it uses the service inotifywait see the following:

while inotifywait -e modify,create,delete,open,close,close_write,access $MONITOR2;

So the new script does not use polling anymore.

Would you mind sharing your 'autodetectscreenlayout' script?

Last edited by originalsurfmex (2013-10-28 20:46:36)

Offline

Board footer

Powered by FluxBB