Skip to content

Instantly share code, notes, and snippets.

@binarynoise
Created August 29, 2022 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binarynoise/0be82483f99db89d5ef59a940bd182c0 to your computer and use it in GitHub Desktop.
Save binarynoise/0be82483f99db89d5ef59a940bd182c0 to your computer and use it in GitHub Desktop.
Virtual Display VNC-Server
#!/bin/bash
# https://github.com/dianariyanto/virtual-display-linux/
# https://bbs.archlinux.org/viewtopic.php?id=180904
# https://unix.stackexchange.com/questions/378373/add-virtual-output-to-xorg
set -euo pipefail
set -x
resolution="1920x1080"
screen="HDMI-2"
echo "Configuring monitor $screen to $resolution"
if ! ( xrandr | grep -Pzo ".*$screen'.*\n(\s+.*\n)+" | grep -E "\s$resolution\s" ); then
if ! ( xrandr | grep -E "\s$resolution\s" ); then
xrandr --newmode $resolution $(cvt ${resolution/x/ } 60.02 | grep "^Modeline" | sed -E 's/Modeline\s+"[^"]+"\s+//')
fi
xrandr --addmode $screen $resolution
fi
xrandr --output $screen --mode $resolution --right-of eDP-1
echo "started display $screen to $resolution"
trap "xrandr --output $screen --off &" 0 1 3 6 14 15
killall /usr/bin/x0vncserver || true
echo "starting x0vncserver"
x0vncserver \
-rfbauth ~/.vnc/passwd \
-AcceptKeyEvents=off -AcceptPointerEvents=off -AcceptSetDesktopSize=off \
-Geometry=$resolution+1920+0 \
# -FrameRate 15 \
xrandr --output $screen --off
@binarynoise
Copy link
Author

In Line 15 you need to replace the frame rate and in line 19 the name of your actual monitor

@SSUPII
Copy link

SSUPII commented Aug 29, 2022

I tried using this script, on Debian 11 and setting the right options. On my end what it does is flash my screen twice, add an xrandr mode to a disconnected port and creating a ghost VNC server nobody can connect to (only after changing the x0vncserver call).

Output, before x0vncserver call is changed:

+ resolution=1920x1080
+ screen=HDMI-2
+ echo 'Configuring monitor HDMI-2 to 1920x1080'
Configuring monitor HDMI-2 to 1920x1080
+ xrandr
+ grep -Pzo '.*HDMI-2'\''.*\n(\s+.*\n)+'
+ grep -E '\s1920x1080\s'
+ xrandr
+ grep -E '\s1920x1080\s'
   1920x1080     60.02*+  60.01    59.97    59.96    59.93  
   1920x1080     60.02  
+ xrandr --addmode HDMI-2 1920x1080
+ xrandr --output HDMI-2 --mode 1920x1080 --right-of eDP-1
+ echo 'started display HDMI-2 to 1920x1080'
started display HDMI-2 to 1920x1080
+ trap 'xrandr --output HDMI-2 --off &' 0 1 3 6 14 15
+ killall x0vncserver
x0vncserver: nessun processo trovato
+ true
+ echo 'starting x0vncserver'
starting x0vncserver
+ x0vncserver -rfbauth /home/sergio/.vnc/passwd -AcceptKeyEvents=off -AcceptPointerEvents=off -AcceptSetDesktopSize=off -Geometry=1920x1080+1920+0
x0vncserver: Option -AcceptKeyEvents: Option AcceptKeyEvents can only be set to true or false!

x0vncserver usage:

  Help can be found in x0vncserver(1), or via usage of
     -help                   if specified, dumps this help message.
     -h                      is an alias for help.
     -?                      is a***the entirity of the arguments list***
     
+ xrandr --output HDMI-2 --off

@binarynoise
Copy link
Author

what it does is flash my screen twice

That's probably xrandr --output HDMI-2 --mode 1920x1080 --right-of eDP-1 and xrandr --output HDMI-2 --mode 1920x1080 --right-of eDP-1, turning the second monitor on and off.

x0vncserver: Option -AcceptKeyEvents: Option AcceptKeyEvents can only be set to true or false!

Looks like on Debian you need to replace off with false, on arch it accepts many variants.

Output, before x0vncserver call is changed:

What is the output afterwards?

@SSUPII
Copy link

SSUPII commented Aug 29, 2022

What is the output afterwards?

Exactly the same until starting x0vncserver

+ x0vncserver -rfbauth /home/sergio/.vnc/passwd -AcceptKeyEvents=false -AcceptPointerEvents=false -AcceptSetDesktopSize=false -Geometry=1920x1080+1920+0

New X0tigervnc server 'debian:0 (sergio)' on port 5900 for display :0.
Use xtigervncviewer -SecurityTypes VncAuth -passwd /home/sergio/.vnc/passwd :0 to connect to the VNC server.

+ xrandr --output HDMI-2 --off
+ xrandr --output HDMI-2 --off

Connecting via a secondary device will give a time out.

Connecting locally will give a
CConn: unable to connect to socket: Connessione rifiutata (111)

@SSUPII
Copy link

SSUPII commented Aug 29, 2022

I added --localhost no to the tigervnc arguments and this got the VNC server to be visible, but the client will be immediately stopped as soon as an external device or the same device will try to connect to it with a CConn: End of stream.

Every other attempt to connect from any device will fail with the same error listed in the message before (the server died?).

@SSUPII
Copy link

SSUPII commented Aug 29, 2022

Removing +1920+0 from line 19 fixes the VNC server from crashing (but of course, it is just a mirror of the main screen). The X server is not drawing anything at all over the bounds of the main screen, as the system is still seeing only 1 screen.

@binarynoise
Copy link
Author

Mmh. Seems it doesn't work for you. What does your vnc client display if you remove the geometry from the commandline when the virtual display is active?

@SSUPII
Copy link

SSUPII commented Aug 29, 2022

Mmh. Seems it doesn't work for you. What does your vnc client display if you remove the geometry from the commandline when the virtual display is active?

My main screen only.

The problem is the virtual screen is never active. I never see it appear in my XFCE4 screen settings, nor can I move past my main screen boundaries.
It will appear as disconnected in xrandr but with an available video mode.

@binarynoise
Copy link
Author

I never see it appear in my XFCE4 screen settings

I use cinnamon, but it won't appear in its display settings either. I can move windows over to the virtual monitor though, and in arandr (like a gui for xrandr).

Maybe XFCE does some things different so it won't work. You maybe need a dummy HDMI plug

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