Thursday 26 March 2015

iPad as a 2nd monitor - now on Linux

So after I figured out how to use my iPad as a 2nd monitor on the Mac and Windows, I thought I'd write another post on how to achieve this on Linux. I used Ubuntu Linux as my Linux of choice (until of course, the new Mac) and the post details how I got it to work on Ubuntu.

This is quite a bit more complicated than the extremely easy method that works on the Mac and on Windows.

What works

Screen sharing on the iPad
Mouse, keyboard and clipboard sharing across the laptop and the iPad

What doesn’t

Drag and drop windows from one screen to the other - This does not seem possible to do between X displays

Setup

Step 1: Install the following on Ubuntu

tightvnc - Runs a VNC server on a new X display
x2x - Lets you share the mouse and keyboard across X displays
ipad_charge - Enables charging the iPad in Ubuntu
gnome-session-fallback - Enables a Gnome session in VNC as Unity doesn’t play nicely
xsel - Enables sharing of the clipboard (Credit: nerdopolis @ http://ubuntuforums.org/showthread.php?t=1431913)


Run the following commands to complete the installation.
sudo apt-get install -y tightvncserver x2x gnome-session-fallback xsel
sudo apt-get install -y build-essential libusb-1.0-0 libusb-1.0-0-dev git
git clone https://github.com/mkorenkov/ipad_charge.git
cd ./ipad_charge
make
sudo make install

Step 2: Setup a VNC Password

This provides at least basic security to your main machine, but you should be ok on a home network. You could set up an ssh connection for more security, but this make the rendering of the screen very slow
vncpasswd


Enter a password for the VNC server twice and “no” for a read only password

Step 3: Create the xstartup file

Create a file called xstartup in the $HOME/.vnc folder with the following contents
#!/bin/sh
xrdb $HOME/.Xresources
xsetroot -solid grey
#Open a terminal window in the new X display. Comment the following line if unnecessary
x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
x-window-manager &
# Fix to make GNOME work
export XKL_XMODMAP_DISABLE=1
/etc/X11/Xsession

Step 4: Create a .xsession file

Create a file called .xsession in the $HOME directory with the following contents. This will enable a gnome desktop as Unity does not work over VNC
gnome-session --session=gnome-fallback

Step 5: Install and configure a VNC client on the iPad

Any VNC client will do, but I like this one. It’s free for the basic features which are enough for what we need.
Mocha VNC Lite
Use the following details in the Mocha VNC Lite configuration. The settings may be named different depending on the client you choose. The settings will also be saved, hence from the
VNC server address: As shown when you run the ipad_monitor.sh script
VNC server port: 5901
VNC password: The one you set using the vncpasswd command
Local mouse: Off

Step 6: Additional tips to be more productive

  1. Evince: Default PDF reader that works better than Acrobat reader
  2. Browsers seem to like opening on only one of the displays at a time. I ended up using Chrome on my laptop and Firefox on the VNC display
  3. Synapse: A keystroke search tool to replace Unity Dash
  4. Autohide the top panel to increase available screen real estate
  5. Add an Window Selector to the bottom panel to easily switch windows and increase the bottom size panel to 48 to make touch selection easier
  6. Add a new Gnome panel to the left with Synapse and any other frequently used applications to replace Unity Dash

Usage

Step 1: Create the 2nd screen with keyboard and mouse sharing

Download the ipad_monitor.sh file. Change it to include your network card device (Mine is wlan0 but yours may look like eth0 or eth1) and run it. Enter L (Left) or R (Right) in the command options depending on the side you have placed the iPad. The script needs to be running to enable the monitor.
./ipad_monitor.sh R

Step 2: Share the clipboard

Download the share_clip.sh file. Start a new terminal window & run it with the following command. The script needs to be running to enable clipboard sharing.
./share_clip.sh
Note: autocutsel can also achieve clipboard sharing, but it only worked from server to client for me, and not both ways. The script enables bidirectional clipboard sharing which is a lot more useful. To use autocutsel, add the command autocutsel -s PRIMARY -fork to the ~/.vnc/xstartup file

Step 3: Connect to the laptop using the VNC client

Connect to the server you saved in step 5 above. Click the “Lock” icon in the status bar a the bottom of the screen and then the upside down triangle at the bottom right of the screen to hide the status bar.

Step 4: Enjoy your ultra portable 2nd screen

That’s all. You can now interact on the iPad screen using the keyboard and mouse.


Steps summary

The below steps are for the iPad placed to the right of the laptop.
Ctrl + Alt + T
<path>/ipad_monitor.sh R
Ctrl + Shift + T
<path>/share_clip.sh
Connect from iPad using Mocha VNC. Lock the screen and minimise the status bar.

Scripts

ipad_monitor.sh

#!/bin/bash


if [ "$1" == "L" ]; then
dir="west"
pos="left"
elif [ "$1" == "R" ]; then
dir="east"
pos="right"
else
echo "Incorrect position selected. Defaulting to \"Right\""
dir="east"
pos="right"
fi
chmod +x ~/.vnc/xstartup #Just in case
vncserver -alwaysshared :1 #Start the VNC server
ipad_charge #Allow iPad charging on Linux


#Friendly print of the ip address for the iPad to connect to
INPUT=`ifconfig wlan0 | grep "inet addr"`
SUBSTRING=`echo $INPUT| cut -d' ' -f 2`
echo ""
echo "Ipad Monitor enabled to the $pos of the primary screen. Start VNC client on the iPad and connect to IP $SUBSTRING, port 5901"
echo "Press CTRL+C to disable the iPad monitor"
echo "Run the share_clip.sh script in a new terminal window to enable clipboard sharing"
####
x2x -$dir -to :1 #Share the mouse and keyboard


#Cleanup before exit
function finish {
vncserver -kill :1 #Disable the VNC server
ipad_charge --off #Disable USB charging
echo "Ipad Monitor disabled." #Display the status
}


trap finish EXIT


share_clip.sh (Credit: nerdopolis @ http://ubuntuforums.org/showthread.php?t=1431913)

#! /bin/bash
firstxserver=":0" #1st display is default X display 0
secondxserver=":1" #2nd display has been configured to be at X display 1 while starting the VNC server


#set the variables to the default
echo . | xsel --display $firstxserver -b -i
echo . | xsel --display $secondxserver -b -i
clipboard=.


while [ 1 ]
do
#get the values of the clipboard
firstdislpayclipboard=$(xsel --display $firstxserver -b -o)
seconddislpayclipboard=$(xsel --display $secondxserver -b -o)


#if the first x servers clipboad chages
if [ "$firstdislpayclipboard" != "$clipboard" ]
then


#if it doesnt change to be blank
if [ $(echo $firstdislpayclipboard | grep ^$ -c) -ne 1 ]
then
#set the appropriate variables to be the contents of the first comand
seconddislpayclipboard=$firstdislpayclipboard
clipboard=$firstdislpayclipboard
xsel --display $firstxserver -b -o | xsel --display $secondxserver -b -i
else
#if it is blank set it to be .in case if its because the x server went  down
echo . | xsel --display $firstxserver -b -i
fi


fi


#if the second x servers clipboad chages
if [ "$seconddislpayclipboard" != "$clipboard" ]
then


#if it doesnt change to be blank
if [ $(echo $seconddislpayclipboard | grep ^$ -c) -ne 1 ]
then
#set the appropriate variables to be the contents of the first comand
firstdislpayclipboard=$seconddislpayclipboard
clipboard=$seconddislpayclipboard
xsel --display $secondxserver -b -o | xsel --display $firstxserver -b -i
else
#if it is blank set it to be .in case if its because the x server went  down
echo . | xsel --display $secondxserver -b -i
fi


fi


sleep 1

done

Todo

Enable dragging of windows from the laptop to the iPad screen. This does not seem to do between X displays. It's highly unlikely that I bother figuring this out because the Mac has become my daily usage computer, but you can try out the solutions below.

Workarounds

xpra comes close to achieving sharing of windows. The only challenge is that each program that you want to run needs to be run using the commands. Also, it seems only one client can connect at a time, so it doesn’t seem possible to run multiple programs on 1 X display (100 in the example below) and keep a few windows on the laptop screen and a few on the iPad screen

To share firefox, run on the laptop
xpra start :100 --start-child=firefox

To view the firefox window, run on the laptop or the iPad
xpra attach :100

xdmx is also another program that could help

teleport is yet another program that seems to do this but I can’t understand how to use it

Wednesday 11 March 2015

iPad as a 2nd monitor

I recently got myself a Macbook Air and was blown away by it. This is my first Mac bought to replace my 7 year old workhorse laptop which has now been converted into my home media streaming server (courtesy Serviio).

In just a few days, I noticed how I've moved to using the Mac Air instead of my iPad as my goto computing device. That's when I realised that the killer feature of the Mac Air is its zero startup time - it was like having the convenience of the iPad and the functionality of a full fledged laptop.

That got me thinking that I need to repurpose the iPad and put it to some use, so I thought of making it a 2nd screen for the Mac Air. A little bit of searching the web led me to a number of solutions, some working over Wifi, some using the USB cable. However, all of them were paid solutions, and I wasn't in the mood for spending money on them, just after cleaning out my wallet on the Mac Air.

So a little more effort, and I got a perfectly workable solution, all for free.

Installation

  1. Download and install the following software on the Mac Air
    1. Syphon Virtual Screen - Creates a virtual display. Get it here.
    2. RealVNC Server - Creates a VNC server that allows remote viewing and control of the virtual display. Get it here.
  2. Install the following software on the iPad
    1. RealVNC Viewer - Allows viewing and control of the virtual display from the iPad. Get it here.

Using the virtual screen

  1. Start Syphon Virtual Screen from your applications. Enable it from the menu and set a screen resolution. Since I have an iPad 2, I've set mine to 1024x768
  2. Start VNC Server and select Options->Expert. Change the "Monitor" parameter to "1". This selects the virtual display instead of the default display

  3. Start VNC Viewer on the iPad and connect to the VNC server you just set up. You must be on the same network for this to work.
Voila! Your iPad is now a 2nd monitor for your Mac. The default configuration for this virtual monitor is to the right of the Mac screen and you can drag and drop your windows to it.

The same trick can work for any tablet or even an old laptop. All you need is a VNC client on the tablet or old laptop.

Pro tip: Download and install Spectacle on your Mac for easy keyboard shortcuts to move Windows between the monitors. Get it here.

Notes

  1. In the Syphon Virtual Screen preferences, all you need enabled is "Enable virtual device at startup"
  2. If you would like to change the location of the virtual monitor, you can do that from "System Preferences->Displays->Arrangement". Just drag and drop it wherever it's more convenient
  3. Syphon Virtual Screen does seem to make the Mac laggy, so I only recommend turning it on when you actually are using the virtual monitor
  4. If you want to have a similar setup for Windows, use Zonescreen to create a virtual monitor instead of Syphon Virtual Screen. Get it here


Starting off

This is a blog to document my thoughts, interests and anything else that may catch my attention. Will mostly be thoughts about marketing, music and technology with the occasional digression into other topics