4 X11_FORK="${X11_FORK:-0}"
7 DEBUG="${DEBUG:-/dev/stderr}"
15 printe(){ printf "$@" >&2; }
20 X11_UNIX="/tmp/.X11-unix/X${X11_UNIX%.*}"
23 X11_HOST=${DISPLAY%:*}
24 X11_PORT=${DISPLAY#*:}
25 X11_PORT=$((6000 + ${X11_PORT%.*}))
29 if [ "$X11_FORK" = 1 ]; then
31 debug "Forking into hexdump\n"
32 stdbuf -o0 hexdump -ve '3/1 " %u" /1 " %u\n"' |"$_EXEC"
35 elif [ "$X11_FORK" = 2 ]; then
36 debug "Reassigning file handles\n"
42 elif [ "$X11_HOST" -a "$X11_PORT" ]; then
45 debug 'Using `ncat` for TCP socket %s:%i\n' "$X11_HOST" "$X11_PORT"
46 if ! ncat "$X11_HOST" "$X11_PORT" -e "$_EXEC"; then
48 printe '`ncat` could not connect to TCP socket %s:%i\n' "$X11_HOST" "$X11_PORT"
53 elif [ "$X11_UNIX" ]; then
56 debug 'Using `ncat` for UNIX socket %s\n' "$X11_UNIX"
57 if ! ncat -U "$X11_UNIX" -e "$_EXEC"; then
59 printe '`ncat` could not connect to UNIX socket %s\n' "$X11_UNIX"
65 printe 'No DISPLAY specified, set a DISPLAY variable to connect to a server.'
70 debug 'Connected to X11-Server\n'
79 local string='' chars="$1"
80 while [ "$chars" -gt 0 ]; do
81 string="$string ${x11_additional_data%% *}"
82 x11_additional_data="${x11_additional_data#* }"
83 chars="$((chars - 1))"
85 printf "$(printf '\\%o' $string)";
87 # CARD16(){ printf "%i\n" $((256 * $1 + $2)); }
88 # CARD32(){ printf "%i\n" $((16777216 * $1 + 65536 * $2 + 256 * $3 + $4)); }
90 x11_additional_data(){
91 # Argument: Number of 4-Octet (32bit) data words
92 # Return: list of decimal representation of octets
93 local data l words="$1"
94 while [ $words -gt 0 ]; do
103 local data chars m1 m2 m3 m4;
106 while [ $# -gt 0 ]; do case $1 in
108 m1="${data%% *}" data="${data#* }"
113 m1="${data%% *}" data="${data#* }"
114 m2="${data%% *}" data="${data#* }"
115 printf "%i " $((256 * m1 + m2));
118 CARD32|VALUE|TIMESTAMP)
119 m1="${data%% *}" data="${data#* }"
120 m2="${data%% *}" data="${data#* }"
121 m3="${data%% *}" data="${data#* }"
122 m4="${data%% *}" data="${data#* }"
123 printf "%i " $((16777216 * m1 + 65536 * m2 + 256 * m3 + m4));
126 STRING8) # Argument: Number of characters
127 chars="${2:-0}" m1='' m2=''
128 while [ $chars -gt 0 ]; do
133 printf "$(printf '\\%o' $m1)\n";
136 pad) # Argument: Number of octets, discards octets without output
138 while [ $chars -gt 0 ]; do
148 debug 'Unknown value type: %s\n' "$1"
153 printf '%s\n' "$data"
157 local m1 m2 m3 m4 m5 m6 m7 m8 data
159 printf 'B~\000\013\000\000\000\000\000\000~~' >&4
164 X11_VERSION="$((256 * m3 + m4)).$((256 * m5 + m6))"
165 data="$(x11_additional_data "$((256 * m7 + m8))")"
169 debug 'X11: Setup Failed\nProtocol Version: %s\n' "$X11_VERSION"
170 debug '%s\n' "$(printf '%i ' $data |x11_read_values STRING8 $m2)"
174 debug 'X11: Setup Authenticate\n'
175 debug '%s\n' "$(printf '%i ' $data |x11_read_values STRING8 $m2)"
179 debug 'X11: Setup Success\nProtocol Version: %s\n' "$X11_VERSION"
180 { read X11_RELEASE X11_RID_BASE X11_RID_MASK X11_MBUF_SIZE \
181 m1 X11_MAX_RQ_LENGTH \
182 m2 m3 X11_IMAGE_BO X11_BITMAP_BO \
183 X11_SCANLINE_UNIT X11_SCANLINE_PAD \
184 X11_MIN_KEYCODE X11_MAX_KEYCODE \
187 $( printf '%i ' $data |x11_read_values \
188 CARD32 CARD32 CARD32 CARD32 \
190 CARD8 CARD8 CARD8 CARD8 \
196 debug "Number of Screens: %i\n" $m2
197 { read X11_VENDOR; read data; } <<-EOF
198 $( printf '%i ' $data |x11_read_values STRING8 $m1 pad $(( (4 - m1 % 4) % 4 )) )
200 debug "Vendor (%i): %s\n" "$m1" "$X11_VENDOR"