diff --git a/client/renderers/EGL/desktop.c b/client/renderers/EGL/desktop.c index 9847939b..ac7bc49d 100644 --- a/client/renderers/EGL/desktop.c +++ b/client/renderers/EGL/desktop.c @@ -410,7 +410,10 @@ bool egl_desktopSetup(EGL_Desktop * desktop, const LG_RendererFormat format) { if (egl_composeSetup(desktop->compose, &desktop->format)) { - egl_composeGetSize(desktop->compose, &desktop->width, &desktop->height); + unsigned int viewWidth, viewHeight; + egl_composeGetSize(desktop->compose, &viewWidth, &viewHeight); + desktop->width = viewWidth; + desktop->height = viewHeight; desktop->composeActive = true; DEBUG_INFO("Composing %u displays from a %ux%u atlas into a %ux%u view", diff --git a/client/src/config.c b/client/src/config.c index a90daa44..44de7a87 100644 --- a/client/src/config.c +++ b/client/src/config.c @@ -96,6 +96,14 @@ static struct Option options[] = .type = OPTION_TYPE_INT, .value.x_int = 1000 }, + { + .module = "app", + .name = "display", + .description = "Which guest display to show when the guest has several: " + "`all` for the whole desktop, or an index or output name", + .type = OPTION_TYPE_STRING, + .value.x_string = "all", + }, { .module = "app", .name = "allowDMA", diff --git a/client/src/main.c b/client/src/main.c index 4e8b938b..5f02f117 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -46,6 +46,7 @@ #include "common/locking.h" #include "common/event.h" #include "common/ivshmem.h" +#include "common/option.h" #include "common/time.h" #include "common/version.h" #include "common/paths.h" @@ -557,6 +558,74 @@ int main_cursorThread(void * unused) return 0; } +/* Work out which part of the guest's virtual desktop to present, from the + * `app:display` option. + * + * The default, `all`, shows the whole desktop with the monitors arranged as + * the guest has them. Naming a single display instead shows just that one, + * which is how several client instances can each take one guest monitor: they + * all subscribe to the same frames and each crops its own. + * + * An unrecognised selection falls back to the whole desktop rather than + * failing, so a stale config cannot leave the user with a blank window. */ +static void configureDisplayView(LG_RendererFormat * format, + const KVMFRFrame * frame) +{ + format->displayCount = frame->displayCount; + memcpy(format->displays, frame->displays, + frame->displayCount * sizeof(*frame->displays)); + + // the whole virtual desktop, which the host reports as the screen size + format->viewX = 0; + format->viewY = 0; + format->viewWidth = frame->screenWidth; + format->viewHeight = frame->screenHeight; + + if (!frame->displayCount) + return; + + const char * select = option_get_string("app", "display"); + if (!select || !*select || strcasecmp(select, "all") == 0) + { + DEBUG_INFO("Showing all %u guest displays (%ux%u virtual desktop)", + frame->displayCount, frame->screenWidth, frame->screenHeight); + return; + } + + const KVMFRDisplay * chosen = NULL; + + // an index into the display list, as reported by the host + char * end; + const long index = strtol(select, &end, 10); + if (*select && !*end && index >= 0 && index < (long)frame->displayCount) + chosen = &frame->displays[index]; + else + for(unsigned i = 0; i < frame->displayCount; ++i) + if (strcasecmp(frame->displays[i].name, select) == 0) + { + chosen = &frame->displays[i]; + break; + } + + if (!chosen) + { + DEBUG_WARN("No guest display matches `%s`, showing the whole desktop", + select); + for(unsigned i = 0; i < frame->displayCount; ++i) + DEBUG_WARN(" available: %u or `%s`", i, frame->displays[i].name); + return; + } + + format->viewX = chosen->desktopX; + format->viewY = chosen->desktopY; + format->viewWidth = chosen->desktopWidth; + format->viewHeight = chosen->desktopHeight; + + DEBUG_INFO("Showing guest display `%s` (%ux%u at +%d+%d)", + chosen->name, chosen->desktopWidth, chosen->desktopHeight, + chosen->desktopX, chosen->desktopY); +} + int main_frameThread(void * unused) { struct DMAFrameInfo @@ -668,6 +737,8 @@ int main_frameThread(void * unused) lgrFormat.sdrWhiteLevel = frame->sdrWhiteLevel ? frame->sdrWhiteLevel : KVMFR_SDR_WHITE_LEVEL_DEFAULT; + configureDisplayView(&lgrFormat, frame); + if (lgrFormat.hdrMetadata) { memcpy(lgrFormat.hdrDisplayPrimary, frame->hdrDisplayPrimary, sizeof(lgrFormat.hdrDisplayPrimary)); @@ -788,11 +859,27 @@ int main_frameThread(void * unused) renderQueue_surfaceFormat(lgrFormat, rendererSupportsNativeHDR); LG_UNLOCK(g_state.lgrLock); - g_state.srcSize.x = lgrFormat.screenWidth; - g_state.srcSize.y = lgrFormat.screenHeight; + /* With a multi-display guest what reaches the screen is the composed + * view, not the packed frame, so both the source size used for input + * mapping and the auto-resize target must come from the view. */ + g_state.displayCount = lgrFormat.displayCount; + memcpy(g_state.displays, lgrFormat.displays, + lgrFormat.displayCount * sizeof(*lgrFormat.displays)); + g_state.viewX = lgrFormat.viewX; + g_state.viewY = lgrFormat.viewY; + + const unsigned int viewW = lgrFormat.displayCount ? + lgrFormat.viewWidth : lgrFormat.screenWidth; + const unsigned int viewH = lgrFormat.displayCount ? + lgrFormat.viewHeight : lgrFormat.screenHeight; + + g_state.srcSize.x = viewW; + g_state.srcSize.y = viewH; g_state.haveSrcSize = true; if (g_params.autoResize) - g_state.ds->setWindowSize(lgrFormat.frameWidth, lgrFormat.frameHeight); + g_state.ds->setWindowSize( + lgrFormat.displayCount ? viewW : lgrFormat.frameWidth, + lgrFormat.displayCount ? viewH : lgrFormat.frameHeight); core_updatePositionInfo(); } diff --git a/client/src/util.c b/client/src/util.c index 36ac3e85..d50ab6fd 100644 --- a/client/src/util.c +++ b/client/src/util.c @@ -120,10 +120,14 @@ bool util_guestCurToLocal(struct DoublePoint *local) if (!g_cursor.guest.valid || !g_state.posInfoValid) return false; + /* The guest reports its cursor in virtual desktop coordinates. When only + * part of that desktop is on show, shift into the coordinates of the + * presented view. Both offsets are zero unless a single display of a + * multi-display guest has been selected. */ const struct DoublePoint point = { - .x = g_cursor.guest.x + g_cursor.guest.hx, - .y = g_cursor.guest.y + g_cursor.guest.hy + .x = g_cursor.guest.x + g_cursor.guest.hx - g_state.viewX, + .y = g_cursor.guest.y + g_cursor.guest.hy - g_state.viewY }; switch((g_state.rotate + g_params.winRotate) % LG_ROTATE_MAX) @@ -190,6 +194,11 @@ void util_localCurToGuest(struct DoublePoint *guest) default: DEBUG_UNREACHABLE(); } + + /* the guest addresses its cursor across the whole virtual desktop, so undo + * the shift applied when only part of that desktop is presented */ + guest->x += g_state.viewX; + guest->y += g_state.viewY; } void util_rotatePoint(struct DoublePoint *point)