diff --git a/host/platform/Linux/capture/XCB/src/xcb.c b/host/platform/Linux/capture/XCB/src/xcb.c index 1e2f5fa1..4dc5d49d 100644 --- a/host/platform/Linux/capture/XCB/src/xcb.c +++ b/host/platform/Linux/capture/XCB/src/xcb.c @@ -102,6 +102,14 @@ static void xcb_initOptions(void) .type = OPTION_TYPE_BOOL, .value.x_bool = true }, + { + .module = "xcb", + .name = "monitors", + .description = "Override monitor detection with a manual layout, " + "e.g. `1920x1080+0+0,1920x1080+1920+0`", + .type = OPTION_TYPE_STRING, + .value.x_string = NULL + }, {0} }; @@ -112,10 +120,84 @@ static void xcb_initOptions(void) * * On failure the caller falls back to capturing the root window as one * display, which is always correct, just without per-monitor metadata. */ +/* Parse a manual monitor layout, `WxH+X+Y` separated by commas. + * + * Nested and virtual X servers such as Xvfb and Xephyr accept + * `xrandr --setmonitor` but ignore it, so there is no way to describe a + * multi-monitor desktop to them. This makes such a server usable as a capture + * source for testing, and covers any X server without RandR monitor objects. + * + * Returns the number parsed, or zero if the layout is malformed. */ +static unsigned xcb_parseMonitorLayout(const char * spec, LGDisplayInfo * info) +{ + unsigned count = 0; + + for(const char * p = spec; *p; ) + { + int w, h, x, y, consumed = 0; + if (sscanf(p, "%dx%d+%d+%d%n", &w, &h, &x, &y, &consumed) != 4 || + consumed == 0) + { + DEBUG_ERROR("Invalid monitor layout at `%s`, expected WxH+X+Y", p); + return 0; + } + + if (w <= 0 || h <= 0) + { + DEBUG_ERROR("Monitor %u has a non-positive size (%dx%d)", count, w, h); + return 0; + } + + if (count == KVMFR_MAX_DISPLAYS) + { + DEBUG_ERROR("The layout describes more than %d monitors", + KVMFR_MAX_DISPLAYS); + return 0; + } + + LGDisplayInfo * d = &info[count]; + memset(d, 0, sizeof(*d)); + d->id = count; + d->primary = (count == 0); + d->x = x; + d->y = y; + d->width = w; + d->height = h; + d->rotation = FRAME_ROT_0; + snprintf(d->name, sizeof(d->name), "MANUAL-%u", count); + ++count; + + p += consumed; + if (*p == ',') + ++p; + else if (*p) + { + DEBUG_ERROR("Unexpected `%s` in the monitor layout", p); + return 0; + } + } + + return count; +} + static bool xcb_setupDisplays(void) { this->displayCount = 0; + LGDisplayInfo info[KVMFR_MAX_DISPLAYS]; + unsigned count = 0; + + const char * manual = option_get_string("xcb", "monitors"); + if (manual && *manual) + { + count = xcb_parseMonitorLayout(manual, info); + if (!count) + return false; + + DEBUG_INFO("Using the manually specified monitor layout"); + goto haveDisplays; + } + const xcb_query_extension_reply_t * ext = xcb_get_extension_data(this->xcb, &xcb_randr_id); if (!ext || !ext->present) @@ -135,9 +217,6 @@ static bool xcb_setupDisplays(void) return false; } - LGDisplayInfo info[KVMFR_MAX_DISPLAYS]; - unsigned count = 0; - xcb_randr_monitor_info_iterator_t iter = xcb_randr_get_monitors_monitors_iterator(reply); for(; iter.rem; xcb_randr_monitor_info_next(&iter)) @@ -181,6 +260,7 @@ static bool xcb_setupDisplays(void) } free(reply); +haveDisplays: if (count < 2) { DEBUG_INFO("Only %u monitor present, using single display capture", count); diff --git a/host/tests/demo-multimon.sh b/host/tests/demo-multimon.sh new file mode 100755 index 00000000..55be0872 --- /dev/null +++ b/host/tests/demo-multimon.sh @@ -0,0 +1,115 @@ +#!/bin/bash +# A visible end to end demonstration of multi-monitor Looking Glass. +# +# Runs a virtual X server holding three "monitors" side by side, puts real +# applications on each, captures it with the host, and renders the composed +# result through the real client. The client's window is captured to a PNG. +# +# Everything runs locally, so this shows the whole pipeline without a VM. +# +# Usage: demo-multimon.sh [work-dir] + +set -u + +HOST="${1:?host binary required}" +CLIENT="${2:?client binary required}" +WORK="${3:-/tmp/lg-demo}" + +GUEST=:97 # the "guest" desktop being captured +VIEW=:96 # where the client renders, so it can be captured +SHM=/dev/shm/lg-demo + +MON_W=1280 +MON_H=800 +DESK_W=$((MON_W * 3)) + +cleanup() { + for p in ${PIDS:-}; do kill "$p" 2>/dev/null; done + pkill -f "Xvfb $GUEST" 2>/dev/null + pkill -f "Xvfb $VIEW" 2>/dev/null + rm -f "$SHM" +} +trap cleanup EXIT +PIDS="" + +mkdir -p "$WORK" + +echo "== guest desktop: ${DESK_W}x${MON_H}, three ${MON_W}x${MON_H} monitors ==" +Xvfb $GUEST -screen 0 ${DESK_W}x${MON_H}x24 > "$WORK/xvfb-guest.log" 2>&1 & +PIDS="$PIDS $!" +sleep 2 + +# a distinct background per monitor region so the split is obvious +convert -size ${MON_W}x${MON_H} gradient:'#20304a'-'#0a0f18' \ + -size ${MON_W}x${MON_H} gradient:'#1e4436'-'#08160f' \ + -size ${MON_W}x${MON_H} gradient:'#4a2030'-'#18080f' \ + +append "$WORK/bg.png" 2>/dev/null +DISPLAY=$GUEST display -window root "$WORK/bg.png" 2>/dev/null & +PIDS="$PIDS $!" +sleep 1 + +echo "== launching applications onto each monitor ==" +# one clock per monitor, plus some moving content, positioned per region +DISPLAY=$GUEST xclock -digital -update 1 -bg '#101820' -fg '#7fd6ff' \ + -geometry 300x80+80+60 > /dev/null 2>&1 & PIDS="$PIDS $!" +DISPLAY=$GUEST xeyes -geometry 260x260+120+220 > /dev/null 2>&1 & PIDS="$PIDS $!" + +DISPLAY=$GUEST xclock -analog -update 1 -bg '#0d1f18' -fg '#7fffc4' \ + -geometry 260x260+$((MON_W + 120))+150 > /dev/null 2>&1 & PIDS="$PIDS $!" +DISPLAY=$GUEST xterm -geometry 44x12+$((MON_W + 80))+480 \ + -bg '#08160f' -fg '#7fffc4' -e "while :; do date; sleep 1; done" \ + > /dev/null 2>&1 & PIDS="$PIDS $!" + +DISPLAY=$GUEST xclock -digital -update 1 -bg '#200d16' -fg '#ff9fc4' \ + -geometry 300x80+$((MON_W * 2 + 80))+60 > /dev/null 2>&1 & PIDS="$PIDS $!" +DISPLAY=$GUEST xeyes -geometry 260x260+$((MON_W * 2 + 120))+220 \ + > /dev/null 2>&1 & PIDS="$PIDS $!" +sleep 3 + +echo "== starting the host ==" +rm -f "$SHM" +truncate -s 256M "$SHM" +DISPLAY=$GUEST "$HOST" app:capture=XCB app:shmFile="$SHM" \ + "xcb:monitors=${MON_W}x${MON_H}+0+0,${MON_W}x${MON_H}+${MON_W}+0,${MON_W}x${MON_H}+$((MON_W * 2))+0" \ + > "$WORK/host.log" 2>&1 & +PIDS="$PIDS $!" +sleep 3 + +grep -E "Multi-monitor|-> atlas" "$WORK/host.log" || { + echo "FAIL: host did not report a multi-monitor layout" + tail -20 "$WORK/host.log"; exit 1; } + +echo +echo "== starting the client ==" +Xvfb $VIEW -screen 0 $((DESK_W / 2 + 40))x$((MON_H / 2 + 40))x24 \ + > "$WORK/xvfb-view.log" 2>&1 & +PIDS="$PIDS $!" +sleep 2 + +env -u WAYLAND_DISPLAY DISPLAY=$VIEW "$CLIENT" \ + -f "$SHM" -s \ + win:size=$((DESK_W / 2))x$((MON_H / 2)) win:autoResize=no \ + win:borderless=yes win:position=0x0 app:display=all \ + > "$WORK/client.log" 2>&1 & +PIDS="$PIDS $!" +sleep 8 + +grep -E "Composing|Showing" "$WORK/client.log" || { + echo "FAIL: client did not compose" + tail -20 "$WORK/client.log"; exit 1; } + +echo +echo "== capturing what the client is showing ==" +DISPLAY=$VIEW import -window root "$WORK/client-view.png" 2>/dev/null +convert "$WORK/client-view.png" -crop $((DESK_W / 2))x$((MON_H / 2))+0+0 \ + +repage "$WORK/composed.png" + +# the raw guest desktop, for comparison +DISPLAY=$GUEST import -window root -resize $((DESK_W / 2))x \ + "$WORK/guest.png" 2>/dev/null + +echo +echo " guest desktop : $WORK/guest.png" +echo " client output : $WORK/composed.png" +echo +echo "DEMO COMPLETE"