Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash script to use an Android device as a second monitor for Linux. See https://blog.8bitbuddhism.com/2019/12/01/how-to-use-your-android-tablet-as-second-monitor/ for detailed instructions.
#!/bin/bash
# Make sure your Android device is plugged in and accessible over adb.
#### Remember to enable virtual displays in xorg by adding the following to your configuration (e.g. /usr/share/X11/xorg.conf.d/20-virtual.conf)
# Section "Device"
# Identifier "intelgpu0"
# Driver "intel"
# Option "VirtualHeads" "1"
#EndSection
#### If you use AMD or Nvidia, change the Identifier and Driver options to match your GPU.
W=1920 # Virtual display width
H=1080 # Virtual display height
O=VIRTUAL1 # The name of the virtual display (check using xrandr)
P=eDP1 # The name of your physical display (check using xrandr)
PW='$(xrandr --current | grep \* | awk '{print $1;}' | cut -d x -f 1)'
# Create the virtual display
gtf $W $H 60 | sed '3q;d' | sed 's/Modeline//g' | xargs xrandr --newmode
gtf $W $H 60 | sed '3q;d' | sed 's/Modeline//g' | awk '{print $1;}' | sed 's/^.\(.*\).$/\1/' | xargs xrandr --addmode $O
gtf $W $H 60 | sed '3q;d' | sed 's/Modeline//g' | awk '{print $1;}' | sed 's/^.\(.*\).$/\1/' | xargs xrandr --output $O --right-of $P --mode
# Forward the VNC port to your device and start a VNC session
adb reverse tcp:5900 tcp:5900
x11vnc -localhost -clip ${W}x${H}+${PW}+0
# When the session ends, turn off the virtual display
xrandr --output $O --off
@dusansimic

This comment has been minimized.

Copy link

@dusansimic dusansimic commented Apr 7, 2020

Hi. "VirtualHeads" option is Intel driver specific and it can't be used with other drivers like amdgpu or nvidia. Meaning this will work only on computers that are running Intel graphics.

@marnunez

This comment has been minimized.

Copy link

@marnunez marnunez commented Jul 11, 2020

Has anyone found a nvidia graphics alternative for this?

@iamferreirajp

This comment has been minimized.

Copy link

@iamferreirajp iamferreirajp commented Sep 14, 2020

Would be great to find and nvidia alternative

@balkce

This comment has been minimized.

Copy link

@balkce balkce commented Oct 14, 2020

+1 to find a way to do this for nvidia drivers

@Dimfred

This comment has been minimized.

Copy link

@Dimfred Dimfred commented Dec 2, 2020

+1 for nvidia

@hovo1990

This comment has been minimized.

Copy link

@hovo1990 hovo1990 commented Jan 28, 2021

+1 for nvidia

@sascha1337

This comment has been minimized.

Copy link

@sascha1337 sascha1337 commented Mar 13, 2021

+1 for nvidia, wen?

@andrej-korshikov

This comment has been minimized.

Copy link

@andrej-korshikov andrej-korshikov commented May 10, 2021

NVidia driver doesn't support virtual screens. And, mentioned "intel" driver is legacy one, current intel's "modesetting" driver doesn't support virtual screens too. Please, read the following materials for possible solutions:
StackExchange: Unable to add a VIRTUAL display to Xorg
ArchLinux Wiki: Extreme Multihead

@cb1986ster

This comment has been minimized.

Copy link

@cb1986ster cb1986ster commented Jul 17, 2021

Hi,
I use this modified version of the script (nvidia1070):

#!/bin/bash

# Make sure your Android device is plugged in and accessible over adb.
W=800      # Virtual display width
H=1280      # Virtual display height
O=DVI-I-1-1  # The name of the virtual display (check using xrandr)
P=DP-0      # The name of your physical display (check using xrandr)
PW=`xrandr --current | grep \* | awk '{print $1;}' | cut -d x -f 1`

sudo modprobe evdi initial_device_count=2
xrandr --setprovideroutputsource 1 0 --setprovideroutputsource 2 0

# Create the virtual display
# gtf $W $H 60 | sed '3q;d' | sed 's/Modeline//g' | xargs xrandr --newmode
xrandr --newmode `gtf $W $H 60 | grep -v '#' | sed 's/Modeline//g' | xargs`
# gtf $W $H 60 | sed '3q;d' | sed 's/Modeline//g' | awk '{print $1;}' | sed 's/^.\(.*\).$/\1/' | xargs xrandr --addmode $O
xrandr --addmode ${O} ${W}x${H}_60.00
# gtf $W $H 60 | sed '3q;d' | sed 's/Modeline//g' | awk '{print $1;}' | sed 's/^.\(.*\).$/\1/' | xargs xrandr --output $O --right-of $P --mode
xrandr --output $O --right-of $P --mode ${W}x${H}_60.00
    
# Forward the VNC port to your device and start a VNC session
adb reverse tcp:5900 tcp:5900
x11vnc -clip ${W}x${H}+${PW}+0

# When the session ends, turn off the virtual display
xrandr --output $O --off

sudo rmmod evdi

It use evdi (# apt install evdi-dkms) to create virtual display instead of intel features. No X server required, but mouse cursor is blinking. Any suggestions how to fix this?

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