Files
looking-glass-2/host/platform/Linux/capture/XCB/src/xcb.c
T
donny dfe8edfbc2
build / client (Debug, map[cc:clang cxx:clang++], libdecor) (push) Failing after 27s
build / client (Debug, map[cc:gcc cxx:g++], libdecor) (push) Failing after 28s
build / client (Debug, map[cc:clang cxx:clang++], xdg-shell) (push) Failing after 28s
build / client (Debug, map[cc:gcc cxx:g++], xdg-shell) (push) Failing after 21s
build / client (Release, map[cc:clang cxx:clang++], libdecor) (push) Failing after 20s
build / client (Release, map[cc:clang cxx:clang++], xdg-shell) (push) Failing after 21s
build / module (push) Failing after 16s
build / client (Release, map[cc:gcc cxx:g++], xdg-shell) (push) Failing after 19s
build / client (Release, map[cc:gcc cxx:g++], libdecor) (push) Failing after 20s
build / host-linux (push) Failing after 19s
build / host-windows-cross (push) Failing after 17s
build / obs (clang) (push) Failing after 18s
build / docs (push) Failing after 7s
build / obs (gcc) (push) Failing after 20s
build / host-windows-native (push) Canceled after 0s
build / idd (push) Canceled after 0s
Add multi-monitor support via packed atlas frames
Capture every guest display into a single frame and compose it back into
the guest's desktop on the client, so one Looking Glass session can carry
a multi-monitor guest instead of one instance per display.

KVMFR gains a per-frame display layout (version 24). A frame with
displayCount == 0 is a conventional single-display frame and behaves
exactly as before, so existing capture backends are unaffected.

- common: shared display layout and a shelf packer that minimises atlas
  area, plus atlas/desktop coordinate mapping helpers and unit tests
- host: capture interface and host app carry the layout; a layout change
  invalidates the format so clients re-read it
- host/XCB: enumerate RandR monitors and repack the root window into the
  atlas directly in shared memory
- client/EGL: compose the atlas into the presented view as its own pass,
  leaving post processing, HDR, cursor and damage handling unchanged

Also fixes the XCB backend writing a single row per frame instead of the
whole image.
2026-07-28 07:06:32 +00:00

633 lines
17 KiB
C

/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "interface/capture.h"
#include "interface/platform.h"
#include "common/util.h"
#include "common/option.h"
#include "common/debug.h"
#include "common/displays.h"
#include "common/event.h"
#include "common/thread.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <xcb/randr.h>
#include <xcb/shm.h>
#include <xcb/xfixes.h>
#include <sys/ipc.h>
#include <sys/shm.h>
struct xcb
{
bool initialized;
bool stop;
xcb_connection_t * xcb;
xcb_screen_t * xcbScreen;
uint32_t seg;
int shmID;
void * data;
LGEvent * frameEvent;
CaptureGetPointerBuffer getPointerBufferFn;
CapturePostPointerBuffer postPointerBufferFn;
LGThread * pointerThread;
// the X11 root window, which is the whole virtual desktop
unsigned int width;
unsigned int height, dataHeight;
unsigned int pitch;
/* Multi-monitor state. displayCount is zero when capturing the root window
* as a single display, in which case the frame is sent exactly as it always
* has been and none of the atlas fields below are used. */
bool multiMonitor;
unsigned int displayCount;
KVMFRDisplay displays[KVMFR_MAX_DISPLAYS];
// where each display lives in the root window, which packing does not change
unsigned int srcX[KVMFR_MAX_DISPLAYS];
unsigned int srcY[KVMFR_MAX_DISPLAYS];
unsigned int atlasWidth, atlasHeight, atlasPitch;
unsigned int desktopWidth, desktopHeight;
int mouseX, mouseY, mouseHotX, mouseHotY;
bool hasFrame;
xcb_shm_get_image_cookie_t imgC;
xcb_xfixes_get_cursor_image_cookie_t curC;
};
static struct xcb * this = NULL;
static int pointerThread(void * unused);
// forwards
static bool xcb_deinit(void);
// implementation
static const char * xcb_getName(void)
{
return "XCB";
}
static void xcb_initOptions(void)
{
struct Option options[] =
{
{
.module = "xcb",
.name = "multiMonitor",
.description = "Pack all monitors into a single frame",
.type = OPTION_TYPE_BOOL,
.value.x_bool = true
},
{0}
};
option_register(options);
}
/* Enumerate the active RandR monitors and build the packed atlas layout.
*
* On failure the caller falls back to capturing the root window as one
* display, which is always correct, just without per-monitor metadata. */
static bool xcb_setupDisplays(void)
{
this->displayCount = 0;
const xcb_query_extension_reply_t * ext =
xcb_get_extension_data(this->xcb, &xcb_randr_id);
if (!ext || !ext->present)
{
DEBUG_WARN("RandR is unavailable, capturing the root window as one display");
return false;
}
xcb_randr_get_monitors_reply_t * reply = xcb_randr_get_monitors_reply(
this->xcb,
xcb_randr_get_monitors(this->xcb, this->xcbScreen->root, 1),
NULL);
if (!reply)
{
DEBUG_WARN("Failed to query RandR monitors, "
"capturing the root window as one display");
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))
{
const xcb_randr_monitor_info_t * mon = iter.data;
if (mon->width == 0 || mon->height == 0)
continue;
if (count == KVMFR_MAX_DISPLAYS)
{
DEBUG_WARN("More than %d monitors present, ignoring the rest",
KVMFR_MAX_DISPLAYS);
break;
}
LGDisplayInfo * d = &info[count];
memset(d, 0, sizeof(*d));
d->id = count;
d->primary = mon->primary;
d->x = mon->x;
d->y = mon->y;
d->width = mon->width;
d->height = mon->height;
d->rotation = FRAME_ROT_0;
xcb_get_atom_name_reply_t * name = xcb_get_atom_name_reply(this->xcb,
xcb_get_atom_name(this->xcb, mon->name), NULL);
if (name)
{
const int len = min(xcb_get_atom_name_name_length(name),
(int)sizeof(d->name) - 1);
memcpy(d->name, xcb_get_atom_name_name(name), len);
d->name[len] = '\0';
free(name);
}
else
snprintf(d->name, sizeof(d->name), "monitor-%u", count);
++count;
}
free(reply);
if (count < 2)
{
DEBUG_INFO("Only %u monitor present, using single display capture", count);
return false;
}
/* The X11 root window is already the virtual desktop, so a monitor's
* position within it is exactly its RandR position. Record that before
* packing, which renumbers only the destination. */
for(unsigned i = 0; i < count; ++i)
{
this->srcX[i] = info[i].x;
this->srcY[i] = info[i].y;
}
if (!lgDisplaysPack(info, count, 1, this->displays,
&this->atlasWidth, &this->atlasHeight,
&this->desktopWidth, &this->desktopHeight))
{
DEBUG_WARN("Failed to pack %u monitors into an atlas, "
"falling back to single display capture", count);
return false;
}
this->displayCount = count;
this->atlasPitch = this->atlasWidth * 4;
DEBUG_INFO("Multi-monitor : %u monitors, desktop %ux%u, atlas %ux%u",
count, this->desktopWidth, this->desktopHeight,
this->atlasWidth, this->atlasHeight);
for(unsigned i = 0; i < count; ++i)
{
const KVMFRDisplay * d = &this->displays[i];
DEBUG_INFO(" %-16s %ux%u desktop +%d+%d -> atlas +%u+%u%s",
d->name, d->desktopWidth, d->desktopHeight,
d->desktopX, d->desktopY, d->atlasX, d->atlasY,
(d->flags & KVMFR_DISPLAY_FLAG_PRIMARY) ? " (primary)" : "");
}
return true;
}
static bool xcb_create(
CaptureGetPointerBuffer getPointerBufferFn,
CapturePostPointerBuffer postPointerBufferFn,
unsigned frameBuffers
)
{
DEBUG_ASSERT(!this);
this = calloc(1, sizeof(*this));
this->shmID = -1;
this->data = (void *)-1;
this->frameEvent = lgCreateEvent(true, 20);
this->getPointerBufferFn = getPointerBufferFn;
this->postPointerBufferFn = postPointerBufferFn;
if (!this->frameEvent)
{
DEBUG_ERROR("Failed to create the frame event");
free(this);
return false;
}
return true;
}
static bool xcb_init(void * ivshmemBase, unsigned * alignSize)
{
DEBUG_ASSERT(this);
DEBUG_ASSERT(!this->initialized);
lgResetEvent(this->frameEvent);
this->stop = false;
this->xcb = xcb_connect(NULL, NULL);
if (!this->xcb || xcb_connection_has_error(this->xcb))
{
DEBUG_ERROR("Unable to open the X display");
goto fail;
}
if (!xcb_get_extension_data(this->xcb, &xcb_shm_id)->present)
{
DEBUG_ERROR("Missing the SHM extension");
goto fail;
}
xcb_screen_iterator_t iter;
iter = xcb_setup_roots_iterator(xcb_get_setup(this->xcb));
this->xcbScreen = iter.data;
this->width = iter.data->width_in_pixels;
this->height = iter.data->height_in_pixels;
this->pitch = this->width * 4;
DEBUG_INFO("Frame Size : %u x %u", this->width, this->height);
/* Falling back to single display capture is not an error: it is what this
* backend has always done, and it stays correct for one monitor. */
this->multiMonitor = option_get_bool("xcb", "multiMonitor");
if (this->multiMonitor)
xcb_setupDisplays();
this->seg = xcb_generate_id(this->xcb);
const size_t maxFrameSize = this->width * this->height * 4;
this->shmID = shmget(IPC_PRIVATE, maxFrameSize, IPC_CREAT | 0777);
if (this->shmID == -1)
{
DEBUG_ERROR("shmget failed");
goto fail;
}
xcb_shm_attach(this->xcb, this->seg ,this->shmID, false);
this->data = shmat(this->shmID, NULL, 0);
if ((uintptr_t)this->data == -1)
{
DEBUG_ERROR("shmat failed");
goto fail;
}
DEBUG_INFO("Frame Data : 0x%" PRIXPTR, (uintptr_t)this->data);
xcb_query_extension_cookie_t extension_cookie =
xcb_query_extension(this->xcb, strlen("XFIXES"), "XFIXES");
xcb_query_extension_reply_t * extension_reply =
xcb_query_extension_reply(this->xcb, extension_cookie, NULL);
if(!extension_reply)
{
DEBUG_ERROR("Extension \"XFIXES\" isn't available");
free(extension_reply);
goto fail;
}
free(extension_reply);
xcb_xfixes_query_version_cookie_t version_cookie =
xcb_xfixes_query_version(this->xcb, XCB_XFIXES_MAJOR_VERSION, XCB_XFIXES_MINOR_VERSION);
xcb_xfixes_query_version_reply_t * version_reply =
xcb_xfixes_query_version_reply(this->xcb, version_cookie, NULL);
if(!version_reply)
{
DEBUG_ERROR("Extension \"XFIXES\" isn't available");
free(version_reply);
goto fail;
}
free(version_reply);
this->initialized = true;
return true;
fail:
xcb_deinit();
return false;
}
static bool xcb_start(void)
{
this->stop = false;
if (!lgCreateThread("XCBPointer", pointerThread, NULL, &this->pointerThread))
{
DEBUG_ERROR("Failed to create the XCBPointer thread");
return false;
}
return true;
}
static void xcb_stop(void)
{
this->stop = true;
if(this->pointerThread)
{
lgJoinThread(this->pointerThread, NULL);
this->pointerThread = NULL;
}
}
static bool xcb_deinit(void)
{
DEBUG_ASSERT(this);
if ((uintptr_t)this->data != -1)
{
shmdt(this->data);
this->data = (void *)-1;
}
if (this->shmID != -1)
{
shmctl(this->shmID, IPC_RMID, NULL);
this->shmID = -1;
}
if (this->xcb)
{
xcb_disconnect(this->xcb);
this->xcb = NULL;
}
this->initialized = false;
return true;
}
static void xcb_free(void)
{
lgFreeEvent(this->frameEvent);
free(this);
this = NULL;
}
static CaptureResult xcb_capture(
unsigned frameBufferIndex,
FrameBuffer * frame)
{
DEBUG_ASSERT(this);
DEBUG_ASSERT(this->initialized);
if (!this->hasFrame)
{
this->imgC = xcb_shm_get_image_unchecked(
this->xcb,
this->xcbScreen->root,
0, 0,
this->width,
this->height,
~0,
XCB_IMAGE_FORMAT_Z_PIXMAP,
this->seg,
0);
this->hasFrame = true;
lgSignalEvent(this->frameEvent);
}
return CAPTURE_RESULT_OK;
}
static CaptureResult xcb_waitFrame(
unsigned frameBufferIndex,
CaptureFrame * frame,
const size_t maxFrameSize)
{
lgWaitEvent(this->frameEvent, TIMEOUT_INFINITE);
if (this->displayCount)
{
/* Multi-monitor: the frame is the packed atlas, while the screen size
* describes the guest's virtual desktop so the client can lay the
* monitors out as the guest has them. */
const unsigned int maxHeight = maxFrameSize / this->atlasPitch;
this->dataHeight = min(maxHeight, this->atlasHeight);
frame->screenWidth = this->desktopWidth;
frame->screenHeight = this->desktopHeight;
frame->dataWidth = this->atlasWidth;
frame->dataHeight = this->dataHeight;
frame->frameWidth = this->atlasWidth;
frame->frameHeight = this->atlasHeight;
frame->truncated = maxHeight < this->atlasHeight;
frame->pitch = this->atlasPitch;
frame->stride = this->atlasWidth;
frame->format = CAPTURE_FMT_BGRA;
frame->rotation = CAPTURE_ROT_0;
frame->displayCount = this->displayCount;
memcpy(frame->displays, this->displays,
this->displayCount * sizeof(*this->displays));
return CAPTURE_RESULT_OK;
}
const unsigned int maxHeight = maxFrameSize / this->pitch;
this->dataHeight = min(maxHeight, this->height);
frame->screenWidth = this->width;
frame->screenHeight = this->height;
frame->dataWidth = this->width;
frame->dataHeight = this->dataHeight;
frame->frameWidth = this->width;
frame->frameHeight = this->height;
frame->truncated = maxHeight < this->height;
frame->pitch = this->pitch;
frame->stride = this->width;
frame->format = CAPTURE_FMT_BGRA;
frame->rotation = CAPTURE_ROT_0;
frame->displayCount = 0;
return CAPTURE_RESULT_OK;
}
/* Copy each monitor out of the captured root window into its slot in the
* packed atlas, writing directly into the shared memory frame so no
* intermediate buffer is needed.
*
* The write pointer is advanced once at the end rather than per row: the
* atlas is not filled in a contiguous top-to-bottom order, so a client
* waiting on the pointer must not be told a region is ready before it is. */
static void xcb_packDisplays(FrameBuffer * frame)
{
uint8_t * dst = framebuffer_get_data(frame);
const uint8_t * src = this->data;
for(unsigned i = 0; i < this->displayCount; ++i)
{
const KVMFRDisplay * d = &this->displays[i];
// clamp to the captured root window, which a monitor should never exceed
unsigned width = d->atlasWidth;
unsigned rows = d->atlasHeight;
if (this->srcX[i] >= this->width || this->srcY[i] >= this->height)
continue;
if (this->srcX[i] + width > this->width)
width = this->width - this->srcX[i];
if (this->srcY[i] + rows > this->height)
rows = this->height - this->srcY[i];
// clamp to what fits in the frame, so a small ivshmem truncates safely
if (d->atlasY >= this->dataHeight)
continue;
if (d->atlasY + rows > this->dataHeight)
rows = this->dataHeight - d->atlasY;
const size_t bytes = (size_t)width * 4;
for(unsigned y = 0; y < rows; ++y)
memcpy(
dst + (size_t)(d->atlasY + y) * this->atlasPitch +
(size_t)d->atlasX * 4,
src + (size_t)(this->srcY[i] + y) * this->pitch +
(size_t)this->srcX[i] * 4,
bytes);
}
framebuffer_set_write_ptr(frame,
(size_t)this->atlasPitch * this->dataHeight);
}
static CaptureResult xcb_getFrame(
unsigned frameBufferIndex,
FrameBuffer * frame,
const size_t maxFrameSize)
{
DEBUG_ASSERT(this);
DEBUG_ASSERT(this->initialized);
xcb_shm_get_image_reply_t * img;
img = xcb_shm_get_image_reply(this->xcb, this->imgC, NULL);
if (!img)
{
DEBUG_ERROR("Failed to get image reply");
return CAPTURE_RESULT_ERROR;
}
if (this->displayCount)
xcb_packDisplays(frame);
else
framebuffer_write(frame, this->data,
(size_t)this->pitch * this->dataHeight);
free(img);
this->hasFrame = false;
return CAPTURE_RESULT_OK;
}
static int pointerThread(void * unused)
{
while (!this->stop)
{
if (this->stop)
break;
CapturePointer pointer = { 0 };
this->curC = xcb_xfixes_get_cursor_image_unchecked(this->xcb);
void * data;
uint32_t size;
if (!this->getPointerBufferFn(&data, &size))
{
DEBUG_WARN("failed to get a pointer buffer");
continue;
}
xcb_xfixes_get_cursor_image_reply_t * curReply;
curReply = xcb_xfixes_get_cursor_image_reply(this->xcb, this->curC, NULL);
if (curReply == NULL)
{
DEBUG_WARN("Failed to get cursor reply");
continue;
}
if(curReply->xhot != this->mouseHotX || curReply->yhot != this->mouseHotY)
{
pointer.shapeUpdate = true;
this->mouseHotX = curReply->xhot;
this->mouseHotY = curReply->yhot;
uint32_t * src = xcb_xfixes_get_cursor_image_cursor_image(curReply);
uint32_t * dst = (uint32_t *) (data);
memcpy(dst, src, curReply->width * curReply->height * sizeof(uint32_t));
}
else
pointer.shapeUpdate = false;
if(curReply->x != this->mouseX || curReply->y != this->mouseY)
{
pointer.positionUpdate = true;
this->mouseX = curReply->x;
this->mouseY = curReply->y;
}
else
pointer.positionUpdate = false;
if(pointer.positionUpdate || pointer.shapeUpdate)
{
pointer.hx = curReply->xhot;
pointer.hy = curReply->yhot;
pointer.visible = true;
pointer.x = curReply->x - curReply->xhot;
pointer.y = curReply->y - curReply->yhot;
pointer.format = CAPTURE_FMT_COLOR;
pointer.width = curReply->width;
pointer.height = curReply->height;
pointer.pitch = curReply->width * 4;
this->postPointerBufferFn(&pointer);
}
free(curReply);
usleep(1000);
}
return 0;
}
struct CaptureInterface Capture_XCB =
{
.shortName = "XCB",
.asyncCapture = true,
.initOptions = xcb_initOptions,
.getName = xcb_getName,
.create = xcb_create,
.init = xcb_init,
.start = xcb_start,
.stop = xcb_stop,
.deinit = xcb_deinit,
.free = xcb_free,
.capture = xcb_capture,
.waitFrame = xcb_waitFrame,
.getFrame = xcb_getFrame
};