Skip to content

Instantly share code, notes, and snippets.

@kbumsik
Last active March 28, 2024 17:12
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save kbumsik/e9717525fec7b6e98524765958044146 to your computer and use it in GitHub Desktop.
Save kbumsik/e9717525fec7b6e98524765958044146 to your computer and use it in GitHub Desktop.
Using iPad as a 2nd monitor on Linux with VNC.
#!/bin/bash
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <k.bumsik@gmail.com> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return. - Bumsik Kim
# ----------------------------------------------------------------------------
# Configuration
WIDTH=1024 # 1368 for iPad Pro
HEIGHT=768 # 1024 for iPad Pro
MODE_NAME="mode_ipad" # Set whatever name you like, you may need to change
# this when you change resolution, or just reboot.
DIS_NAME="VIRTUAL1" # Don't change it unless you know what it is
RANDR_POS="--right-of" # Default position setting for xrandr command
# Parse arguments
while [ "$#" -gt 0 ]; do
case $1 in
-l|--left) RANDR_POS="--left-of" ;;
-r|--right) RANDR_POS="--right-of" ;;
-a|--above) RANDR_POS="--above" ;;
-b|--below) RANDR_POS="--below" ;;
-p|--portrait) TMP=$WIDTH; WIDTH=$HEIGHT; HEIGHT=$TMP
MODE_NAME="$MODE_NAME""_port" ;;
-h|--hidpi) WIDTH=$(($WIDTH * 2)); HEIGHT=$(($HEIGHT * 2))
MODE_NAME="$MODE_NAME""_hidpi" ;;
*) echo "'$1' cannot be a monitor position"; exit 1 ;;
esac
shift
done
# Detect primary display
PRIMARY_DISPLAY=$(xrandr | perl -ne 'print "$1" if /(\w*)\s*connected\s*primary/')
# Add display mode
RANDR_MODE=$(cvt "$WIDTH" "$HEIGHT" 60 | sed '2s/^.*Modeline\s*\".*\"//;2q;d')
xrandr --addmode $DIS_NAME $MODE_NAME 2>/dev/null
# If the mode doesn't exist then make mode and retry
if ! [ $? -eq 0 ]; then
xrandr --newmode $MODE_NAME $RANDR_MODE
xrandr --addmode $DIS_NAME $MODE_NAME
fi
# Show display first
xrandr --output $DIS_NAME --mode $MODE_NAME
# Then move display
sleep 5 # A short delay is needed. Otherwise sometimes the below command is ignored.
xrandr --output $DIS_NAME $RANDR_POS $PRIMARY_DISPLAY
# Cleanup before exit
function finish {
xrandr --output $DIS_NAME --off
xrandr --delmode $DIS_NAME $MODE_NAME
echo "Second monitor disabled."
}
trap finish EXIT
# Get the display's position
CLIP_POS=$(xrandr | perl -ne 'print "$1" if /'$DIS_NAME'\s*connected\s*(\d*x\d*\+\d*\+\d*)/')
echo $CLIP_POS
# Share screen
x11vnc -multiptr -repeat -clip $CLIP_POS
# Possible alternative is x0vncserver but it does not show the mouse cursor.
# x0vncserver -display :0 -geometry $DIS_NAME -overlaymode -passwordfile ~/.vnc/passwd
if ! [ $? -eq 0 ]; then
echo x11vnc failed, did you \'apt-get install x11vnc\'?
fi
@kbumsik
Copy link
Author

kbumsik commented Apr 15, 2018

Visit a blog post for more detail.

Usage

  • Install x11vnc first: sudo apt-get install x11vnc.
  • Change WIDTH and HEIGHT in the script that fits for your tablet.
  • Figure out your IP address using ip addr or ifconfig
  • Run this script. Addtional arguments are self-explanatory. Example arguments:
./ipad_monitor.sh -r    # or --right. Second screen appears right to the primary monitor.
./ipad_monitor.sh -l    # or --left
./ipad_monitor.sh -a    # or --above
./ipad_monitor.sh -b    # or --below
./ipad_monitor.sh -r -h    # or --hidpi. HiDPI mode. It doubles the resolution.
./ipad_monitor.sh -r -p    # or --portrait. Portrait mode.
./ipad_monitor.sh -l -p -h    # Left, portrait mode, and HiDPI mode.
  • Connect your iPad using your favorite VNC app. IP address is : your.ip.addr.ess:5900

@MiguelNdeCarvalho
Copy link

Hey man, I was trying to use this great script but I had this error. Thanks
miguel@miguel-latop:~/Desktop$ ./ipad_monitor.sh -l -p X Error of failed request: BadName (named color or font does not exist) Major opcode of failed request: 140 (RANDR) Minor opcode of failed request: 16 (RRCreateMode) Serial number of failed request: 28 Current serial number in output stream: 28 xrandr: cannot find output "VIRTUAL1" warning: output VIRTUAL1 not found; ignoring warning: output VIRTUAL1 not found; ignoring ./ipad_monitor.sh: 52: ./ipad_monitor.sh: function: not found warning: output VIRTUAL1 not found; ignoring xrandr: cannot find output "VIRTUAL1" Second monitor disabled. ./ipad_monitor.sh: 56: ./ipad_monitor.sh: Syntax error: "}" unexpected

@cdemirsoy
Copy link

You have to answer man!

@TuomoKareoja
Copy link

You could try out the full VrtScreen that takes care of running the scripts for you https://github.com/kbumsik/VirtScreen. You would probably run into same "VIRTUAL1 not found" problem there though. To fix this you need to change VrtScreen configuration files. See here kbumsik/VirtScreen#16. That fixed my problems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment