2021-06-05 21:26:18 -04:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2022-01-05 19:42:46 +11:00
|
|
|
* Copyright © 2017-2022 The Looking Glass Authors
|
2021-06-05 21:26:18 -04:00
|
|
|
* 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
|
|
|
|
*/
|
2019-03-02 11:59:03 +11:00
|
|
|
|
2019-05-09 19:07:23 +10:00
|
|
|
#include "interface/capture.h"
|
|
|
|
#include "interface/platform.h"
|
2022-05-04 12:23:20 +10:00
|
|
|
#include "common/util.h"
|
2021-10-30 14:14:18 -04:00
|
|
|
#include "common/option.h"
|
2019-05-09 19:07:23 +10:00
|
|
|
#include "common/debug.h"
|
2020-01-02 23:34:35 +11:00
|
|
|
#include "common/event.h"
|
2021-10-30 14:19:17 -04:00
|
|
|
#include "common/thread.h"
|
2019-03-02 20:33:45 +11:00
|
|
|
#include <string.h>
|
2019-03-02 11:59:03 +11:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <inttypes.h>
|
2021-10-30 14:19:17 -04:00
|
|
|
#include <unistd.h>
|
2019-03-02 11:59:03 +11:00
|
|
|
#include <xcb/shm.h>
|
|
|
|
#include <xcb/xfixes.h>
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
|
|
|
|
struct xcb
|
|
|
|
{
|
2021-10-30 14:19:17 -04:00
|
|
|
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;
|
2019-03-02 11:59:03 +11:00
|
|
|
|
|
|
|
unsigned int width;
|
|
|
|
unsigned int height;
|
|
|
|
|
2021-10-30 14:19:17 -04:00
|
|
|
int mouseX, mouseY, mouseHotX, mouseHotY;
|
|
|
|
|
2019-03-02 20:33:45 +11:00
|
|
|
bool hasFrame;
|
2019-03-02 11:59:03 +11:00
|
|
|
xcb_shm_get_image_cookie_t imgC;
|
|
|
|
xcb_xfixes_get_cursor_image_cookie_t curC;
|
|
|
|
};
|
|
|
|
|
2021-08-20 02:22:17 -04:00
|
|
|
static struct xcb * this = NULL;
|
2019-03-02 11:59:03 +11:00
|
|
|
|
2021-10-30 14:19:17 -04:00
|
|
|
static int pointerThread(void * unused);
|
|
|
|
|
2019-03-02 11:59:03 +11:00
|
|
|
// forwards
|
|
|
|
|
2022-02-20 11:43:36 -05:00
|
|
|
static bool xcb_deinit(void);
|
2019-03-02 11:59:03 +11:00
|
|
|
|
|
|
|
// implementation
|
|
|
|
|
2021-01-14 01:05:26 -05:00
|
|
|
static const char * xcb_getName(void)
|
2019-03-02 11:59:03 +11:00
|
|
|
{
|
|
|
|
return "XCB";
|
|
|
|
}
|
|
|
|
|
2021-10-30 14:14:18 -04:00
|
|
|
static void xcb_initOptions(void)
|
|
|
|
{
|
|
|
|
struct Option options[] =
|
|
|
|
{
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
|
|
|
option_register(options);
|
|
|
|
}
|
|
|
|
|
2021-01-14 04:59:02 -05:00
|
|
|
static bool xcb_create(CaptureGetPointerBuffer getPointerBufferFn, CapturePostPointerBuffer postPointerBufferFn)
|
2019-03-02 11:59:03 +11:00
|
|
|
{
|
2021-08-13 19:30:41 -04:00
|
|
|
DEBUG_ASSERT(!this);
|
2021-08-15 18:56:42 -04:00
|
|
|
this = calloc(1, sizeof(*this));
|
2019-03-04 17:08:49 +11:00
|
|
|
this->shmID = -1;
|
|
|
|
this->data = (void *)-1;
|
2020-01-02 23:34:35 +11:00
|
|
|
this->frameEvent = lgCreateEvent(true, 20);
|
2019-03-04 17:08:49 +11:00
|
|
|
|
2021-10-30 14:19:17 -04:00
|
|
|
this->getPointerBufferFn = getPointerBufferFn;
|
|
|
|
this->postPointerBufferFn = postPointerBufferFn;
|
|
|
|
|
2019-03-04 17:08:49 +11:00
|
|
|
if (!this->frameEvent)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create the frame event");
|
|
|
|
free(this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-02 11:59:03 +11:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:05:26 -05:00
|
|
|
static bool xcb_init(void)
|
2019-03-02 11:59:03 +11:00
|
|
|
{
|
2021-08-13 19:30:41 -04:00
|
|
|
DEBUG_ASSERT(this);
|
|
|
|
DEBUG_ASSERT(!this->initialized);
|
2019-03-02 11:59:03 +11:00
|
|
|
|
2020-01-02 23:34:35 +11:00
|
|
|
lgResetEvent(this->frameEvent);
|
2019-03-04 17:08:49 +11:00
|
|
|
|
2021-10-30 14:14:18 -04:00
|
|
|
this->stop = false;
|
2019-03-02 11:59:03 +11:00
|
|
|
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;
|
|
|
|
DEBUG_INFO("Frame Size : %u x %u", this->width, this->height);
|
|
|
|
|
|
|
|
this->seg = xcb_generate_id(this->xcb);
|
2021-06-12 19:10:32 +10:00
|
|
|
const size_t maxFrameSize = this->width * this->height * 4;
|
|
|
|
this->shmID = shmget(IPC_PRIVATE, maxFrameSize, IPC_CREAT | 0777);
|
2019-03-02 11:59:03 +11:00
|
|
|
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);
|
|
|
|
|
2021-10-30 14:19:17 -04:00
|
|
|
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);
|
|
|
|
|
2022-03-07 13:43:59 -05:00
|
|
|
this->initialized = true;
|
|
|
|
return true;
|
|
|
|
fail:
|
|
|
|
xcb_deinit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool xcb_start(void)
|
|
|
|
{
|
|
|
|
this->stop = false;
|
|
|
|
|
2021-10-30 14:19:17 -04:00
|
|
|
if (!lgCreateThread("XCBPointer", pointerThread, NULL, &this->pointerThread))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create the XCBPointer thread");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-02 11:59:03 +11:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-30 14:14:18 -04:00
|
|
|
static void xcb_stop(void)
|
|
|
|
{
|
|
|
|
this->stop = true;
|
|
|
|
|
|
|
|
if(this->pointerThread)
|
|
|
|
{
|
|
|
|
lgJoinThread(this->pointerThread, NULL);
|
|
|
|
this->pointerThread = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:05:26 -05:00
|
|
|
static bool xcb_deinit(void)
|
2019-03-02 11:59:03 +11:00
|
|
|
{
|
2021-08-13 19:30:41 -04:00
|
|
|
DEBUG_ASSERT(this);
|
2019-03-02 11:59:03 +11:00
|
|
|
|
|
|
|
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;
|
2021-10-30 14:16:18 -04:00
|
|
|
return true;
|
2019-03-02 11:59:03 +11:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:05:26 -05:00
|
|
|
static void xcb_free(void)
|
2019-03-02 11:59:03 +11:00
|
|
|
{
|
2020-01-02 23:34:35 +11:00
|
|
|
lgFreeEvent(this->frameEvent);
|
2019-03-02 11:59:03 +11:00
|
|
|
free(this);
|
|
|
|
this = NULL;
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:05:26 -05:00
|
|
|
static CaptureResult xcb_capture(void)
|
2019-03-02 11:59:03 +11:00
|
|
|
{
|
2021-08-13 19:30:41 -04:00
|
|
|
DEBUG_ASSERT(this);
|
|
|
|
DEBUG_ASSERT(this->initialized);
|
2019-03-02 11:59:03 +11:00
|
|
|
|
2019-03-02 20:33:45 +11:00
|
|
|
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;
|
2020-01-02 23:34:35 +11:00
|
|
|
lgSignalEvent(this->frameEvent);
|
2019-03-02 20:33:45 +11:00
|
|
|
}
|
|
|
|
|
2019-03-02 11:59:03 +11:00
|
|
|
return CAPTURE_RESULT_OK;
|
|
|
|
}
|
|
|
|
|
2021-06-12 19:10:32 +10:00
|
|
|
static CaptureResult xcb_waitFrame(CaptureFrame * frame,
|
|
|
|
const size_t maxFrameSize)
|
2020-01-02 23:34:35 +11:00
|
|
|
{
|
|
|
|
lgWaitEvent(this->frameEvent, TIMEOUT_INFINITE);
|
|
|
|
|
2021-06-12 19:10:32 +10:00
|
|
|
const unsigned int maxHeight = maxFrameSize / (this->width * 4);
|
|
|
|
|
2022-05-04 10:58:19 +10:00
|
|
|
frame->screenWidth = this->width;
|
|
|
|
frame->screenHeight = this->height;
|
|
|
|
frame->frameWidth = this->width;
|
2022-05-04 12:23:20 +10:00
|
|
|
frame->frameHeight = min(maxHeight, this->height);
|
|
|
|
frame->truncated = maxHeight < this->height;
|
2022-05-04 10:58:19 +10:00
|
|
|
frame->pitch = this->width * 4;
|
|
|
|
frame->stride = this->width;
|
|
|
|
frame->format = CAPTURE_FMT_BGRA;
|
|
|
|
frame->rotation = CAPTURE_ROT_0;
|
2020-01-02 23:34:35 +11:00
|
|
|
|
|
|
|
return CAPTURE_RESULT_OK;
|
|
|
|
}
|
|
|
|
|
2021-07-11 19:18:59 -04:00
|
|
|
static CaptureResult xcb_getFrame(FrameBuffer * frame,
|
|
|
|
const unsigned int height, int frameIndex)
|
2019-03-02 20:33:45 +11:00
|
|
|
{
|
2021-08-13 19:30:41 -04:00
|
|
|
DEBUG_ASSERT(this);
|
|
|
|
DEBUG_ASSERT(this->initialized);
|
2019-03-02 20:33:45 +11:00
|
|
|
|
|
|
|
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");
|
2019-05-09 19:07:23 +10:00
|
|
|
return CAPTURE_RESULT_ERROR;
|
2019-03-02 20:33:45 +11:00
|
|
|
}
|
|
|
|
|
2021-06-12 19:10:32 +10:00
|
|
|
framebuffer_write(frame, this->data, this->width * height * 4);
|
2019-03-02 20:33:45 +11:00
|
|
|
free(img);
|
2019-03-04 17:08:49 +11:00
|
|
|
|
2019-03-02 20:33:45 +11:00
|
|
|
this->hasFrame = false;
|
2019-05-09 19:07:23 +10:00
|
|
|
return CAPTURE_RESULT_OK;
|
2019-03-02 20:33:45 +11:00
|
|
|
}
|
|
|
|
|
2021-10-30 14:19:17 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-02 11:59:03 +11:00
|
|
|
struct CaptureInterface Capture_XCB =
|
|
|
|
{
|
2021-02-26 23:35:20 -05:00
|
|
|
.shortName = "XCB",
|
2021-07-12 16:53:46 +10:00
|
|
|
.asyncCapture = true,
|
2021-10-30 14:14:18 -04:00
|
|
|
.initOptions = xcb_initOptions,
|
2019-03-02 11:59:03 +11:00
|
|
|
.getName = xcb_getName,
|
|
|
|
.create = xcb_create,
|
|
|
|
.init = xcb_init,
|
2022-03-07 13:43:59 -05:00
|
|
|
.start = xcb_start,
|
2021-10-30 14:14:18 -04:00
|
|
|
.stop = xcb_stop,
|
2019-03-02 11:59:03 +11:00
|
|
|
.deinit = xcb_deinit,
|
|
|
|
.free = xcb_free,
|
2019-03-02 20:33:45 +11:00
|
|
|
.capture = xcb_capture,
|
2020-01-02 23:34:35 +11:00
|
|
|
.waitFrame = xcb_waitFrame,
|
2021-01-14 05:02:48 -05:00
|
|
|
.getFrame = xcb_getFrame
|
2021-01-18 13:53:29 +11:00
|
|
|
};
|