The i3 FAQ has migrated to https://www.reddit.com/r/i3wm/. All content here is read-only.
Ask Your Question
0

Conditional Monitor Cofiguration

asked 2015-08-11 15:31:17 +0000

SilverDiamond gravatar image

I use Dell Sputnik for development with i3wm. When I travel, I use the laptop monitor, but while in the office, I connect a desktop monitor. Below is my configuration when the desktop monitor connected.

~$ xrandr
    Screen 0: minimum 8 x 8, current 2560 x 1440, maximum 32767 x 32767
eDP1 connected primary (normal left inverted right x axis y axis)
   1920x1080      60.0 +   59.9     48.0  
   1680x1050      60.0     59.9  
   1600x1024      60.2  
   1400x1050      60.0  
   1280x1024      60.0  
   1440x900       59.9  
   1280x960       60.0  
   1360x768       59.8     60.0  
   1152x864       60.0  
   1024x768       60.0  
   800x600        60.3     56.2  
   640x480        59.9  
DP1 connected 2560x1440+0+0 (normal left inverted right x axis y axis) 597mm x 336mm
   2560x1440      60.0*+
   1920x1200      59.9  
   1920x1080      60.0     60.0     50.0     59.9     24.0     24.0  
   1920x1080i     60.1     50.0     60.0  
   1600x1200      60.0  
   1680x1050      60.0  
   1280x1024      75.0     60.0  
   1280x800       59.8  
   1152x864       75.0  
   1280x720       60.0     50.0     59.9  
   1024x768       75.1     60.0  
   800x600        75.0     60.3  
   720x576        50.0  
   720x480        60.0     59.9  
   640x480        75.0     60.0     59.9  
   720x400        70.1  
HDMI1 disconnected (normal left inverted right x axis y axis)
VIRTUAL1 disconnected (normal left inverted right x axis y axis)

Is it possible to have the system turn off the laptop monitor when the desktop monitor is connected? I know that I can enter xrandr --output DP1 --off to do it by hand, but is it possible to have the system do it automatically? Thanks...

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
0

answered 2015-08-11 17:51:41 +0000

gurkensalat gravatar image

updated 2015-08-12 15:50:32 +0000

Name my script to e.g. "xrandr_daemon" and let it run on startup. I haven't test this to its full extend yet, but it should work.

#!/bin/bash

onConnection() {
    echo onConnection
    xrandr --output eDP1 --auto
    xrandr --output DP1 --off
}
onDisconnection() {
    echo onDisconnection
    xrandr --output eDP1 --off
    xrandr --output DP1 --auto
}

#########################

statefile="`mktemp`"

quit() {
    rm "$statefile"
    exit 1
}
trap quit SIGINT SIGTERM

getstate() {
    state="`xrandr -q | wc -l`"
}
savestate() {
    echo "$state" > "$statefile"
}
getstate
savestate

xev -root -event randr | grep --line-buffered XRROutputChangeNotifyEvent | \
while IFS= read -r line; do
    getstate
    old="`cat "$statefile"`"
    if [ "$state" -gt "$old" ]; then
        onConnection
    elif [ "$state" -lt "$old" ]; then
        onDisconnection
    fi
    savestate
done

EDIT: What works is

# onConnection
xrandr --output eDP1 --auto
xrandr --output DP1 --primary --left-of eDP1
# onDisconnection
xrandr --output eDP1 --off

i3 seems to log out automatically as soon as both monitors are set off (xrandr ... --off) which makes the original script unusable...

EDIT2: The next release with this commit will fix this (EDIT3: tested already)

edit flag offensive delete link more

Comments

I tried it and got the errors below. Also, when I unplugged the monitor, the system logged out.

SilverDiamond gravatar imageSilverDiamond ( 2015-08-11 18:16:17 +0000 )edit
0

answered 2015-08-11 18:14:59 +0000

SilverDiamond gravatar image

Thank you @gurkensalat, I tried it and go the following errors:

~/.i3$ ./xrandr_deamon.sh 
onDisconnection
onDisconnection
XIO:  fatal IO error 11 (Resource temporarily unavailable) on X server ":0"
      after 29 requests (29 known processed) with 0 events remaining.
Can't open display :0
Can't open display :0
~/.i3$ Invalid MIT-MAGIC-COOKIE-1 keyCan't open display :0
Traceback (most recent call last):
  File "./connect_screen.py", line 33, in <module>
    xr2 = count_screens(get(["xrandr"]))
  File "./connect_screen.py", line 21, in get
    def get(cmd): return subprocess.check_output(cmd).decode("utf-8")
  File "/usr/lib/python3.4/subprocess.py", line 620, in check_output
    raise CalledProcessError(retcode, process.args, output=output)
subprocess.CalledProcessError: Command '['xrandr']' returned non-zero exit status 1
edit flag offensive delete link more

Comments

This is strange... Playing around with 2 monitors turned on works fine (--left-of / disabling one), but if only 1 is enabled and then this one disconnects, running xrandr crashes my X server... EDIT: i3 seems to automatically exit if all the monitors are not in use

gurkensalat gravatar imagegurkensalat ( 2015-08-11 19:32:52 +0000 )edit
0

answered 2015-08-11 17:51:55 +0000

i3convert gravatar image

You could subscribe to i3 events. The output event means that the configuration of screens, outputs, etc. has changed. You could then run a script to check what outputs are available and modify the xrandr configuration.

edit flag offensive delete link more

Comments

Wow, this looks awesome, is there an event that detects plugging/unplugging of the screen?

SilverDiamond gravatar imageSilverDiamond ( 2015-08-11 17:56:53 +0000 )edit

Question Tools

1 follower

Stats

Asked: 2015-08-11 15:31:17 +0000

Seen: 224 times

Last updated: Aug 12