Monday, September 24, 2007

Hotplugging a Secondary Display on Linux

GNU/Linux has been ready for the desktop for quite some time. And yet, when trying to tell the world about how ready Desktop Linux is, using a well prepared Open Office presentation, I am often faced with having to smile and say, oops looks like I need to restart X windows before my computer [Linux] can detect the LCD projector.

Lets face it, there are still some glitches here and there but the FOSS community is working hard at solving those problems, one by one. One such problem has been the inability to plug in an external display such as an external monitor or LCD projector and get it to "just work" without having to restart X Windows. That was until Xorg 7.3 came along!

Ever since I heard of Xorg 7.3 a couple of months earlier, I waited eagerly. Xorg 7.3 was finally released earlier this month. Unfortunately I was too busy to install it (i wanted to update other gentoo packages before I did this). Finally this weekend, I managed to upgrade my system (emerge -avuDN world) and get xorg 7.3 working!

Before I get to the monitor plugging, I'd like to make few comments on my experience doing this on Gentoo. First off, after emerging X org 7.3, X didn't start at all! Turned out that the upgrade process didn't recompile some dependency packages because their version hadn't changed. These are pretty much Gentoo specific issues and your not going to have to worry about it on binary based distros.

The other problem I had was with my synaptic touch pad not working. While trying to figure that out, I remembered that xorg 7.3 is supposed to have INPUT hotplugging and work even without an xorg.conf configuration file. So I renamed /etc/X11/xorg.conf and restarted zapped X (Ctrl+Alt+BS), and everything worked beautifully -- sort of. The synaptic touchpad worked and everything seemed fine, except I was having problems with compiz-fusion, the 3D stuff. I could get the 3D cube to rotate and see the wobbly effect but was unable to see what I was typing in the terminal. I could also not see any icons on certain windows such as of ccsm. It took me about an hour to figure out this was actually a problem with using an auto detected xorg.conf. So in the end, I reverted back to the old xorg.conf and found how to get synaptic working on it (thanks to google of course). Here is how my synaptic configuration on xorg.conf now looks like:


Section "ServerLayout"
Identifier "X.org Configured"
Screen 0 "Screen0" 0 0

#InputDevice "TouchPad" "AlwaysCore" # Old setting
#InputDevice "Mouse0" "CorePointer" # Old setting
InputDevice "TouchPad" "CorePointer" # New for xorg 7.3
InputDevice "Mouse0" "SendCoreEvents"

InputDevice "Keyboard0" "CoreKeyboard"
Option "AIGLX" "true"
EndSection

Section "InputDevice"
Driver "synaptics"
Identifier "TouchPad"
Option "SendCoreEvents"
Option "Protocol" "auto-dev"
Option "SHMConfig" "on"
EndSection



Anyway, so now for the good stuff! I plugged in my 17" monitor to the VGA out of the notebook and waited. The signal not detected sign went away and the screen was pitch black. It was on indefinitely on standby. I zapped X again to restart it and this time got a display on the monitor. Hmmm not the hotplug I had in mind. A bit disappointed I wanted to get to the bottom of this - I mean hotplugging was supposed to be the main feature.

Turns out you can turn on a secondary display without restarting (zapping) X and here is how:

Meet the updated version of xrandr. You should have xrandr 1.2 for this to work.


# xrandr -v
Server reports RandR version 1.2


To get a list of displays available along with its status issue:

# xrandr -q
Screen 0: minimum 320 x 200, current 1280 x 800, maximum 2432 x 864
VGA connected (normal left inverted right x axis y axis)
1152x864 74.8
1024x768 84.9 75.1 70.1 60.0
832x624 74.6
800x600 99.7 84.9 72.2 75.0 60.3
640x480 99.8 84.6 75.0 72.8 60.0
720x400 70.1
640x350 70.1
LVDS connected 1280x800+0+0 (normal left inverted right x axis y axis) 331mm x 207mm
1280x800 60.0*+
1024x768 60.0
800x600 60.3
640x480 59.9
TV disconnected (normal left inverted right x axis y axis)


The output of xrandr -q shows two devices, VGA which represents my VGA out and is also shown to be connected to the monitor and LVDS, my LCD display. A third TV out is shown but is unfortunately not physically available on my notebook model :(

To enable my VGA out display and show an exact copy of whats on my LCD:

xrandr --output VGA --auto


This should automatically pick out the preferred resolution for the monitor and enable it. Alternatively if you want to specify the resolution from one of the modes supported (as given by xrandr -q):

xrandr --output VGA --mode 1024x768


You can turn off the external display by issuing

xrandr --output VGA --off


If you want you can create a VGA out toggle script and assign it to a keyboard short cut.

# vi ~/toggle-vga.sh
#!/bin/bash
XRANDR_OUT=`xrandr -q`
if echo "$XRANDR_OUT"|grep -q 'VGA connected'; then
echo 'Detected VGA connected';
if [ `echo "$XRANDR_OUT"|grep '*'|wc -l` -gt 1 ];then
echo 'Turning off VGA';
xrandr --output VGA --off
else
echo 'Turning on VGA';
xrandr --output VGA --auto
fi
else
echo 'No VGA connected!';
fi


Ok now for some fun stuff with xrandr! RandR was built to rotate the screen so lets try a rotation on the second screen:

# xrandr --output VGA --rotate left
# xrandr --output VGA --rotate right
# xrandr --output VGA --rotate normal


That last line will restore all rotations. Feeling dizzy? If not try these cool tricks:

# xrandr --output VGA --reflect x
# xrandr --output VGA --reflect y
# xrandr --output VGA --reflect xy
# xrandr --output VGA --reflect normal


Finally, wouldn't it be nice to extend your desktop to two displays. Well its now possible without having special dual head monitor settings in xorg.conf and even on an intel card! (with two vga outs of course).

There is a catch - you need to predefine the maximum combined resolution so that X server will pre-allocate that memory. Currently on intel cards, this means not enough memory for AIGLX/Compiz /3D. So I recommend creating a separate xorg.conf file for this purpose.

# vi /etc/xorg.conf
Section "Screen"
Identifier "Screen0"
Monitor "Monitor0"
Device "Card0"
DefaultDepth 24

SubSection "Display"
Viewport 0 0
Depth 24
Virtual 2432 864
EndSubSection
EndSection


# xrandr --output VGA --left-of LVDS

xrandr dual head
Depending on your monitor being to the left, right, above or below of your LCD screen you should use the proper option (see man xrandr).

Thats it for now.

13 comments:

Gaveen said...

Great stuff as usual. Can't wait till Gutsy comes out to try this.

Seriously, I have other things to do than setting up a Gentoo box. :)

Bud said...

Well the word is that your gonna have to wait for a while, like till 8.04 until this gets officially shipped with Ubuntu.

There may be third party debs before that.

Anonymous said...

Not working with...
xrandr --output VGA --rotate inverted ou xrandr --output VGA --reflect xy
... on ubuntu boxes.
Any idea ?

Gustavo said...

Hello!

I think my system is passing through the same problem as yours, but with some additional ones hehe.

First, my hotkeys (like alt+f4) don't work, neither of them. I can't even rotate my cube =/

Second, compiz won't work unless I set the flag LIBGL_ALWAYS_INDIRECT=1 during initialization. That's weird, because I have a GMA950, configured with the latest driver, and DRI is working...

If I start X without xorg.conf, the same error as yours occur. That's because it is needed to set the option "XAANoOffscreenPixmaps" "true" on xorg.conf under the section Device. But I don't know how to set it without the xorg.conf

Have you had any of these problems?
I'm (almost) totally frustrated by upgrading xorg and compiz, because my xorg72+beryl was running like a charm =/

Thanks in advance!

Bud said...

For the first anonymous :) sorry Ubuntu (even 7.10) doesn't have xorg 7.3.

For the second anon.. yes I also have few quirks. The indirect thing is there with intel GMA 950. I used to call the way you did but now use fusion-icon applet.

I too have key binding issues :( The Winkey doesn't work so any short cut which used it is gone. Keyboard layouts r missing on kde (kcontrol), caps lock light doesn't turn on but works.

But I have the cube :) and for others I mapped the winkey to other keys.

I'll let u know if I get it fixed. If not pls let me know!

Gustavo said...

Hey, it's me again :)

I found a workaround for (almost) all my problems hehe

First, for the direct rendering problem with GMA950:
Now when I start compiz, I exec the following command...

LIBGL_ALWAYS_INDIRECT=TRUE compiz --sm-disable --ignore-desktop-hints ccp --indirect-rendering

...and add Option XAANoOffscreenPixmaps" "true" to the Device section in xorg.conf

This way, "glxinfo" returns "Direct Rendering 'yes'" and compiz works.

As for the shortcuts, that's a bug in the keyboard driver. I think it has some incompatibility with KDE. I just disabled keyboard layouts in KDE Control Center, and all the shortcuts started working.

Now the only problem left is with CompizConfig, which is pretty buggy...
There are some options that I can't disable. I click the check box to disable it, and it becomes checked again. 0.õ

Hope it might be helpful =)

Bud said...

thanks for the heads up. I didn't know there was a known compatability issue with kde. Keyboard layout is disabled in kde and the prob is I can't enable it. Most keys work except for the win key which probably has a different scan code on my notebook. May be if I upgrade kde to 3.5.8 it will fix it.

I have another problem which is xvideo does work and I have to use the x11 driver. This used to work under compiz.

ओंकार (Onkar) said...

There is a GNOME applet called grandr which is supposed to be GUI for randr. Have you tried it? Does it work well?

Bud said...

Nope. Haven't tried that.

Will said...

This xrandr is working fine with Kubuntu 8.04, I 'm just wondering if it is possible to alter the touchpad x-y axis orientation to match the screen ?

Will said...

I found this hack on the net. I urge those who would like to have synaptics touchpad axis reorientation to send encouragement to this developer to get it into the official driver release.

Jonathan Kulp said...

Thank you SO MUCH for this posting! Getting a projector to work with my laptop has been making me crazy. It almost seems like cheating to use xrandr because it's so easy and works so perfectly. Incredible. Many thanks for this tip. :D

iPhone Screens said...

thanks shawing/ I like to read it much.