Procházet zdrojové kódy

linux-capture: Add xcb helper functions for shm

Add new helper functions managing the shm segment with xcb to xhelpers.
fryshorts před 10 roky
rodič
revize
e7cdb837aa

+ 40 - 0
plugins/linux-capture/xhelpers.c

@@ -148,3 +148,43 @@ void xshm_detach(xshm_t *xshm)
 
 	bfree(xshm);
 }
+
+xcb_shm_t* xshm_xcb_attach(xcb_connection_t *xcb, const int w, const int h)
+{
+	if (!xcb)
+		return NULL;
+
+	xcb_shm_t *shm = bzalloc(sizeof(xcb_shm_t));
+	shm->xcb       = xcb;
+	shm->seg       = xcb_generate_id(shm->xcb);
+
+	shm->shmid = shmget(IPC_PRIVATE, w * h * 4, IPC_CREAT | 0777);
+	if (shm->shmid == -1)
+		goto fail;
+
+	xcb_shm_attach(shm->xcb, shm->seg, shm->shmid, false);
+
+	shm->data = shmat(shm->shmid, NULL, 0);
+
+	return shm;
+fail:
+	xshm_xcb_detach(shm);
+	return NULL;
+}
+
+void xshm_xcb_detach(xcb_shm_t *shm)
+{
+	if (!shm)
+		return;
+
+	xcb_shm_detach(shm->xcb, shm->seg);
+
+	if ((char *) shm->data != (char *) -1)
+		shmdt(shm->data);
+
+	if (shm->shmid != -1)
+		shmctl(shm->shmid, IPC_RMID, NULL);
+
+	bfree(shm);
+}
+

+ 24 - 0
plugins/linux-capture/xhelpers.h

@@ -23,6 +23,7 @@ extern "C" {
 
 #include <X11/Xlib.h>
 #include <X11/extensions/XShm.h>
+#include <xcb/shm.h>
 #include <obs.h>
 
 typedef struct {
@@ -32,6 +33,13 @@ typedef struct {
 	bool attached;
 } xshm_t;
 
+typedef struct {
+	xcb_connection_t *xcb;
+	xcb_shm_seg_t    seg;
+	int              shmid;
+	uint8_t          *data;
+} xcb_shm_t;
+
 /**
  * Check for Xinerama extension
  *
@@ -96,6 +104,22 @@ xshm_t *xshm_attach(Display *dpy, Screen *screen,
  */
 void xshm_detach(xshm_t *xshm);
 
+/**
+ * Attach a shared memory segment to the X-Server
+ *
+ * @param xcb xcb connection
+ * @param w width of the captured screen
+ * @param h height of the captured screen
+ *
+ * @return NULL on error
+ */
+xcb_shm_t *xshm_xcb_attach(xcb_connection_t *xcb, const int w, const int h);
+
+/**
+ * Detach a shared memory segment
+ */
+void xshm_xcb_detach(xcb_shm_t *shm);
+
 #ifdef __cplusplus
 }
 #endif