summaryrefslogtreecommitdiff
path: root/dot/local/bin/xvnc
blob: 30e92181d5a9efbd5dab609ee4cd67ca51fe51ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/sh

# x11vnc option '-noxrecord' added as workaround for crashing bug
# See https://github.com/LibVNC/x11vnc/issues/61
#
# The option is not be needed with latest x11vnc from source, but we don't rely
# on the remote side being up to date.

direct_connection()
{
    port=$((5900 + local_display))
    ssh -o ControlPath=none -n \
        -L $port:localhost:$port \
        ${remote_port:+-p $remote_port} \
        "$remote_host" \
        "x11vnc -noxrecord -nopw -ncache -ncache_cr -localhost -display ${remote_display}" &
    pid=$!
    sleep 1 # surrender race
    xvncviewer -encodings "copyrect tight zrle hextile" localhost:0
    kill $pid
}

quietly()
{
    "$@" # >/dev/null 2>&1
}

reverse_connection()
{
    d=${DISPLAY%.*}
    d=${d#:}
    port=$((5500 + d))
    xvncviewer -fullscreen -encodings "copyrect tight zrle hextile" -listen &
    pid=$!
    quietly ssh -o ControlPath=none \
            -R $port:localhost:$port \
            ${remote_port:+-p $remote_port} \
            "$remote_host" \
            "x11vnc -noxrecord -q -nopw -localhost -display ${remote_display} -coe localhost:$port -rfbport 0"
    kill $pid
}

share_display()
{
    set -x
    port=5500
    fuser -vk $port/tcp
    ssh -o ControlPath=none \
        -o ExitOnForwardFailure=yes \
        -f \
        -L $port:localhost:$port \
        ${remote_port:+-p $remote_port} \
        "$remote_host" \
        "fuser -vk $port/tcp; DISPLAY=${remote_display} xvncviewer -fullscreen -encodings 'copyrect tight zrle hextile' -listen"
    trap "fuser -vk $port/tcp" INT
    sleep 1
    x11vnc -noxrecord -q -nopw -localhost -display ${local_display} -coe localhost:$port -rfbport 0
}

if [ "$1" = -r ]
then
    shift
    cmd=share_display
else
    cmd=reverse_connection
fi

remote_host=${1:-blackboard}

case "$remote_host" in
    *:*) remote_port=${remote_host##*:}
         remote_host=${remote_host%:*}
         ;;
    *) remote_port=
       ;;
esac
remote_display=${2:-:0}
local_display=${3:-:0}

$cmd