Friday, April 24, 2015

Android tablet as a second Ubuntu screen with xrandr and x11vnc

Let's say you have a laptop, an Android tablet and you're working on debugging something.  Wouldn't it be nice to have some extra screen real estate?  The second answer here seemed to be going in the right direction... only one gets stuck moving the mouse into the extended screen area.

So here's a recipe that works (on Ubuntu 14.10 at least). Let's say the actual laptop screen is 1366x768 as in my case; and we want to create another screen of the same resolution:

$ sudo apt-get install x11vnc
$ sudo xrandr --fb 2732x768 --output LVDS1 --panning 2732x768+0+0/2732x768+0+0
$ sleep 3 # wait a moment
$ sudo xrandr --fb 2732x768 --output LVDS1 --panning 1366x768+0+0/2732x768+0+0

Explanation: xrandr (X Resize and Rotate) sets the screen size. To make it think the screen is bigger without doing strange things we first tell it to make a panning setup. With panning the section before the / is the area in which that screen can pan around, after the / is the very important tracking area in which the mouse can move.

Now we can use x11vnc with a clip

sudo x11vnc -clip 1366x768+1367+0 -nocursorshape -nocursorpos

We need to use -nocursorshape and -nocursorpos so the cursor is not dealt with by VNC; the cursor is directly painted on as part of the image.



Now you can take your pick of Android VNC apps and then point it at your laptop's IP address:

The new right half of the screen
Now there's only two and a bit problems left: VNC is not encrypted; so it should run over an SSH tunnel/VPN or something of the like.  Another problem can be lousy WiFi; I'm hoping to kill two bids with one stone by making this run using port forwarding on over the USB cable.

Thanks to this AskUbuntu post.

Monday, April 13, 2015

Ubuntu 14.04 'headless' LTS with Jenkins-CI for Android emulation

So you want one of these magic cheap cloud servers to do handle unit testing and building your Android app?  Makes sense if you have a cross platform approach; the tests need to run automatically on each platform.  And some of us need to do more customization than the millions of cloudy IDEs let us do (like install an Ubuntu package even)

Problem 1: Amazon AWS (and the like) servers are virtual themselves and performance will be awful.  So bad in fact that Cordova's default timeouts will be exceeded.  So you need a real server; in fact with many providers (e.g. Hetzner) they are cheaper for the same performance than what Amazon provides.

Problem 2: Google can make an OS that's on a billion devices.  What they apparently cannot do is maintain documentation on installing an SDK and it's actual requirements.

Problem 3: Ubuntu minimal (what you normally get with a cloud server) and the Android emulator do not play ball.  You will get strange meaningless error messages like can't load swrast.  glxinfo will segfault.

Solution

Take an Ubuntu 14.04 real dedicated server, add the full ubuntu desktop, add a few extra packages, then use VNC to login exactly as you would 'normally'

$ apt-get update && apt-get install ubuntu-desktop
$ apt-get install x11vnc
$ adduser <someusername>
$ init 6


...Wait for server to restart with the lightdm desktop manager running, login as root, and then...

$ x11vnc -xkb -noxrecord -noxfixes -noxdamage -display :0 -auth /var/run/lightdm/root/:0 -usepw

Now use a VNC client to connect to display 0 using the public IP address of the server: you should be able to connect and see the 'normal' ubuntu login.  You can now login with the username created before.


Note: VNC itself does not use encryption.  You'll need to use a VPN like OpenVPN for that.

Now use another SSH connection to install what's needed for Jenkins:
$ apt-get install openjdk-7-jdk
$ wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
$ echo 'deb http://pkg.jenkins-ci.org/debian binary/' > /etc/apt/sources.list.d/jenkins.list
$ apt-get update && apt-get install jenkins
$ nano /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf

Add a line so you can login with a graphical desktop as jenkins :
greeter-show-manual-login=true


Restart lightdm to enable the change (this will cut your VNC session):
$ service lightdm restart


Re-run the VNC service:
$ x11vnc -xkb -noxrecord -noxfixes -noxdamage -display :0 -auth /var/run/lightdm/root/:0 -usepw

Login to the server as jenkins using vnc now and use firefox to download the android development toolkit as you would on a normal laptop/desktop.  Before you run it (as root); add the packages Android needs:

$ dpkg --add-architecture i386
$ apt-get update
$ apt-get install libncurses5:i386 libstdc++6:i386 zlib1g:i386
$ #install these packages that google didn't tell you about to avoid no such file or directory mystery error message
$ apt-get install libc6:i386 libstdc++6:i386 lib32z1

Add Jenkins to the KVM group needed to run android vms at a reasonable speed:
$ adduser jenkins libvirtd
$ adduser jenkins kvm

Now use the android command to install the components as normal and android avd to setup virtual devices.  I recommend not using super screen resolutions:


Now for continuous integration purposes; you'll need to make sure the system is logged in and add this to your scripts before they need something that launches a window:

export DISPLAY=:0 && export XAUTHORITY=/home/$USER/.Xauthority

References + Thanks: