How to use your Android Tablet as a second screen on Linux





Being a researcher sometimes means travelling a lot and you do not have a second monitor. So, I was using my Tablet and suddenly I thought: "What if I can use my Tablet as a second monitor?". After a little search on the magic world of  internet, I found out how to do that on Linux OS (my favourite operating system!).    

First of all we should install Android Debug Bridge (adb):
sudo apt-get install android-tools-adb
and your Tablet must run a version of Android equal or greater than 5.0.

Then you should create the file /usr/share/X11/xorg.conf.d/20-virtual.conf and copy and paste the following:
Section "Device"
Identifier "nvidiagpu0"
Driver "nvidia"
Option "VirtualHeads" "1"
EndSection

After you checked your GPU information, you can change the Identifier and the Driver accordingly. Usually the following command does the job:
sudo lshw -numeric -C display

Create a file create_android_screen.sh paste the following script.
#!/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 "nvidiagpu0"
# Driver "nvidia"
# Option "VirtualHeads" "1"
#EndSection
#### If you use AMD or Nvidia, change the Identifier and Driver options to match your GPU.
W=1280 # Virtual display width
H=800 # Virtual display height
O=VIRTUAL1 # The name of the virtual display (check using xrandr)
P=LVDS1 # 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
adb reverse tcp:5901 tcp:5901 # only for Android equal or grater than 5.0
x11vnc -localhost -rfbauth /home/${USER}/.vnc/passwd -rfbport 5901 -clip ${W}x${H}+${PW}+0 -noxdamage
# When the session ends, turn off the virtual display
xrandr --output $O --off
xrandr --delmode $O ${W}x${H}_60.00
xrandr --rmmode ${W}x${H}_60.00

Set the parameters W and H as the width and the height of yout Tablet's screen resolution, respectively. also remember to set a password for accessing your vnc server (x11vnc).

Then, on your Android Tablet:

Then, connect via USB your Tablet to your PC, run the script create_android_screen.sh and access your VNC server by the Viewer App with localhost address and 5901 as a port number.

Detailed information about the meaning of the commands used in the script and their setting can be found at the link: https://blog.8bitbuddhism.com/2019/12/01/how-to-use-your-android-tablet-as-second-monitor/. I strongly suggest visiting this blog because it is a serious and interesting source of inspiration for coders. Thank you 8bitbuddhism!

Comments

  1. Took like ~10 min to get it running on Debian 10. Thank you very much :D

    ReplyDelete

Post a Comment

Popular Posts