Sunshine master
Self-hosted game stream host for Moonlight.
x11grab.cpp File Reference

Definitions for x11 capture. More...

#include <fstream>
#include <thread>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/Xrandr.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <xcb/shm.h>
#include <xcb/xfixes.h>
#include "cuda.h"
#include "graphics.h"
#include "misc.h"
#include "src/config.h"
#include "src/globals.h"
#include "src/logging.h"
#include "src/platform/common.h"
#include "src/task_pool.h"
#include "src/video.h"
#include "vaapi.h"
#include "x11grab.h"
Include dependency graph for x11grab.cpp:

Classes

struct  platf::shm_attr_t
 X11 shared-memory image dimensions and identifiers. More...
 
class  platf::shm_data_t
 RAII wrapper that detaches mapped SysV shared memory. More...
 
class  platf::shm_id_t
 RAII wrapper that removes a SysV shared-memory segment. More...
 
struct  platf::shm_img_t
 X11 shared-memory image and segment ownership. More...
 
struct  platf::x11_attr_t
 X11 display, window, and attribute handles for capture. More...
 
struct  platf::x11_img_t
 X11 image wrapper used by the software capture path. More...
 

Macros

#define _FN(x, ret, args)
 Macro for FN.
 

Typedefs

typedef int(* platf::x11::CloseDisplay_fn) (Display *display)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef xcb_connection_t *(* platf::xcb::connect_fn) (const char *displayname, int *screenp)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef int(* platf::xcb::connection_has_error_fn) (xcb_connection_t *c)
 Function pointer type for the dynamically loaded X11 entry point.

 
using platf::crtc_info_t = util::dyn_safe_ptr<_XRRCrtcInfo, &x11::rr::FreeCrtcInfo>
 XRandR CRTC info pointer released with XRRFreeCrtcInfo.
 
typedef void(* platf::xcb::disconnect_fn) (xcb_connection_t *c)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef int(* platf::x11::Free_fn) (void *data)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef void(* platf::x11::rr::FreeCrtcInfo_fn) (XRRCrtcInfo *crtcInfo)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef void(* platf::x11::rr::FreeOutputInfo_fn) (XRROutputInfo *outputInfo)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef void(* platf::x11::rr::FreeScreenResources_fn) (XRRScreenResources *resources)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef std::uint32_t(* platf::xcb::generate_id_fn) (xcb_connection_t *c)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef xcb_query_extension_reply_t *(* platf::xcb::get_extension_data_fn) (xcb_connection_t *c, xcb_extension_t *ext)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef xcb_setup_t *(* platf::xcb::get_setup_fn) (xcb_connection_t *c)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef XRRCrtcInfo *(* platf::x11::rr::GetCrtcInfo_fn) (Display *dpy, XRRScreenResources *resources, RRCrtc crtc)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef XFixesCursorImage *(* platf::x11::fix::GetCursorImage_fn) (Display *dpy)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef XImage *(* platf::x11::GetImage_fn) (Display *display, Drawable d, int x, int y, unsigned int width, unsigned int height, unsigned long plane_mask, int format)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef XRROutputInfo *(* platf::x11::rr::GetOutputInfo_fn) (Display *dpy, XRRScreenResources *resources, RROutput output)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef XRRScreenResources *(* platf::x11::rr::GetScreenResources_fn) (Display *dpy, Window window)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef Status(* platf::x11::GetWindowAttributes_fn) (Display *display, Window w, XWindowAttributes *window_attributes_return)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef Status(* platf::x11::InitThreads_fn) (void)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef Display *(* platf::x11::OpenDisplay_fn) (_Xconst char *display_name)
 Function pointer type for the dynamically loaded X11 entry point.

 
using platf::output_info_t = util::dyn_safe_ptr<_XRROutputInfo, &x11::rr::FreeOutputInfo>
 XRandR output info pointer released with XRRFreeOutputInfo.
 
using platf::screen_res_t = util::dyn_safe_ptr<_XRRScreenResources, &x11::rr::FreeScreenResources>
 XRandR screen resources pointer released with XRRFreeScreenResources.
 
typedef xcb_screen_iterator_t(* platf::xcb::setup_roots_iterator_fn) (const xcb_setup_t *R)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef xcb_void_cookie_t(* platf::xcb::shm_attach_fn) (xcb_connection_t *c, xcb_shm_seg_t shmseg, uint32_t shmid, uint8_t read_only)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef xcb_shm_get_image_reply_t *(* platf::xcb::shm_get_image_reply_fn) (xcb_connection_t *c, xcb_shm_get_image_cookie_t cookie, xcb_generic_error_t **e)
 Function pointer type for the dynamically loaded X11 entry point.

 
typedef xcb_shm_get_image_cookie_t(* platf::xcb::shm_get_image_unchecked_fn) (xcb_connection_t *c, xcb_drawable_t drawable, int16_t x, int16_t y, uint16_t width, uint16_t height, uint32_t plane_mask, uint8_t format, xcb_shm_seg_t shmseg, uint32_t offset)
 Function pointer type for the dynamically loaded X11 entry point.

 
using platf::xcb_connect_t = util::dyn_safe_ptr<xcb_connection_t, &xcb::disconnect>
 XCB connection pointer released with xcb_disconnect.
 
using platf::xcb_img_t = util::c_ptr<xcb_shm_get_image_reply_t>
 XCB image pointer released with xcb_image_destroy.
 
using platf::xcursor_t = util::safe_ptr<XFixesCursorImage, freeX>
 XFixes cursor image pointer released with XFree.
 
using platf::ximg_t = util::safe_ptr<XImage, freeImage>
 XImage pointer released with XDestroyImage.
 

Functions

void platf::x11::freeCursorCtx (cursor_ctx_t::pointer ctx)
 Release cursor context resources.
 
void platf::x11::freeDisplay (_XDisplay *xdisplay)
 Release display resources.
 
void platf::freeImage (XImage *)
 Release image resources.
 
void platf::freeX (XFixesCursorImage *)
 Release x resources.
 
int platf::xcb::init ()
 Initialize XFixes cursor tracking for an X11 display.
 
int platf::xcb::init_shm ()
 Initialize shared-memory support for X11 capture.
 
int platf::load_x11 ()
 Load X11 entry points used by the X11 capture backend.
 
int platf::load_xcb ()
 Load XCB entry points used by the X11 capture backend.
 
xdisplay_t platf::x11::make_display ()
 Open and initialize the display connection used for capture.
 
std::shared_ptr< display_tplatf::x11_display (platf::mem_type_e hwdevice_type, const std::string &display_name, const ::video::config_t &config)
 Create an X11 display capture backend.
 
std::vector< std::string > platf::x11_display_names ()
 Enumerate display names accepted by the X11 backend.
 

Detailed Description

Definitions for x11 capture.

Macro Definition Documentation

◆ _FN

#define _FN ( x,
ret,
args )
Value:
\
typedef ret(*x##_fn) args; \
\
static x##_fn x
Functions for handling command line arguments.
Definition entry_handler.cpp:37

Macro for FN.

Typedef Documentation

◆ CloseDisplay_fn

typedef int(* platf::x11::CloseDisplay_fn) (Display *display)

Function pointer type for the dynamically loaded X11 entry point.

◆ connect_fn

typedef xcb_connection_t *(* platf::xcb::connect_fn) (const char *displayname, int *screenp)

Function pointer type for the dynamically loaded X11 entry point.

◆ connection_has_error_fn

typedef int(* platf::xcb::connection_has_error_fn) (xcb_connection_t *c)

Function pointer type for the dynamically loaded X11 entry point.

◆ disconnect_fn

typedef void(* platf::xcb::disconnect_fn) (xcb_connection_t *c)

Function pointer type for the dynamically loaded X11 entry point.

◆ Free_fn

typedef int(* platf::x11::Free_fn) (void *data)

Function pointer type for the dynamically loaded X11 entry point.

◆ FreeCrtcInfo_fn

typedef void(* platf::x11::rr::FreeCrtcInfo_fn) (XRRCrtcInfo *crtcInfo)

Function pointer type for the dynamically loaded X11 entry point.

◆ FreeOutputInfo_fn

typedef void(* platf::x11::rr::FreeOutputInfo_fn) (XRROutputInfo *outputInfo)

Function pointer type for the dynamically loaded X11 entry point.

◆ FreeScreenResources_fn

typedef void(* platf::x11::rr::FreeScreenResources_fn) (XRRScreenResources *resources)

Function pointer type for the dynamically loaded X11 entry point.

◆ generate_id_fn

typedef std::uint32_t(* platf::xcb::generate_id_fn) (xcb_connection_t *c)

Function pointer type for the dynamically loaded X11 entry point.

◆ get_extension_data_fn

typedef xcb_query_extension_reply_t *(* platf::xcb::get_extension_data_fn) (xcb_connection_t *c, xcb_extension_t *ext)

Function pointer type for the dynamically loaded X11 entry point.

◆ get_setup_fn

typedef xcb_setup_t *(* platf::xcb::get_setup_fn) (xcb_connection_t *c)

Function pointer type for the dynamically loaded X11 entry point.

◆ GetCrtcInfo_fn

typedef XRRCrtcInfo *(* platf::x11::rr::GetCrtcInfo_fn) (Display *dpy, XRRScreenResources *resources, RRCrtc crtc)

Function pointer type for the dynamically loaded X11 entry point.

◆ GetCursorImage_fn

typedef XFixesCursorImage *(* platf::x11::fix::GetCursorImage_fn) (Display *dpy)

Function pointer type for the dynamically loaded X11 entry point.

◆ GetImage_fn

typedef XImage *(* platf::x11::GetImage_fn) (Display *display, Drawable d, int x, int y, unsigned int width, unsigned int height, unsigned long plane_mask, int format)

Function pointer type for the dynamically loaded X11 entry point.

◆ GetOutputInfo_fn

typedef XRROutputInfo *(* platf::x11::rr::GetOutputInfo_fn) (Display *dpy, XRRScreenResources *resources, RROutput output)

Function pointer type for the dynamically loaded X11 entry point.

◆ GetScreenResources_fn

typedef XRRScreenResources *(* platf::x11::rr::GetScreenResources_fn) (Display *dpy, Window window)

Function pointer type for the dynamically loaded X11 entry point.

◆ GetWindowAttributes_fn

typedef Status(* platf::x11::GetWindowAttributes_fn) (Display *display, Window w, XWindowAttributes *window_attributes_return)

Function pointer type for the dynamically loaded X11 entry point.

◆ InitThreads_fn

typedef Status(* platf::x11::InitThreads_fn) (void)

Function pointer type for the dynamically loaded X11 entry point.

◆ OpenDisplay_fn

typedef Display *(* platf::x11::OpenDisplay_fn) (_Xconst char *display_name)

Function pointer type for the dynamically loaded X11 entry point.

◆ setup_roots_iterator_fn

typedef xcb_screen_iterator_t(* platf::xcb::setup_roots_iterator_fn) (const xcb_setup_t *R)

Function pointer type for the dynamically loaded X11 entry point.

◆ shm_attach_fn

typedef xcb_void_cookie_t(* platf::xcb::shm_attach_fn) (xcb_connection_t *c, xcb_shm_seg_t shmseg, uint32_t shmid, uint8_t read_only)

Function pointer type for the dynamically loaded X11 entry point.

◆ shm_get_image_reply_fn

typedef xcb_shm_get_image_reply_t *(* platf::xcb::shm_get_image_reply_fn) (xcb_connection_t *c, xcb_shm_get_image_cookie_t cookie, xcb_generic_error_t **e)

Function pointer type for the dynamically loaded X11 entry point.

◆ shm_get_image_unchecked_fn

typedef xcb_shm_get_image_cookie_t(* platf::xcb::shm_get_image_unchecked_fn) (xcb_connection_t *c, xcb_drawable_t drawable, int16_t x, int16_t y, uint16_t width, uint16_t height, uint32_t plane_mask, uint8_t format, xcb_shm_seg_t shmseg, uint32_t offset)

Function pointer type for the dynamically loaded X11 entry point.

Function Documentation

◆ freeCursorCtx()

void platf::x11::freeCursorCtx ( cursor_ctx_t::pointer ctx)

Release cursor context resources.

Parameters
ctxNative context object used by the operation or callback.

◆ freeDisplay()

void platf::x11::freeDisplay ( _XDisplay * xdisplay)

Release display resources.

Parameters
xdisplayX11 display connection.

◆ freeImage()

void platf::freeImage ( XImage * p)

Release image resources.

Parameters
pPointer passed to the deleter or conversion helper.

◆ freeX()

void platf::freeX ( XFixesCursorImage * p)

Release x resources.

Parameters
pPointer passed to the deleter or conversion helper.

◆ init()

int platf::xcb::init ( )

Initialize XFixes cursor tracking for an X11 display.

Returns
0 on success; nonzero or negative platform status on failure.

◆ init_shm()

int platf::xcb::init_shm ( )

Initialize shared-memory support for X11 capture.

Returns
0 when XCB shared-memory functions are loaded; nonzero otherwise.

◆ load_x11()

int platf::load_x11 ( )

Load X11 entry points used by the X11 capture backend.

Returns
0 when required X11 symbols are loaded; nonzero otherwise.

◆ load_xcb()

int platf::load_xcb ( )

Load XCB entry points used by the X11 capture backend.

Returns
0 when required XCB symbols are loaded; nonzero otherwise.

◆ make_display()

xdisplay_t platf::x11::make_display ( )

Open and initialize the display connection used for capture.

Returns
Constructed display object.

◆ x11_display()

std::shared_ptr< display_t > platf::x11_display ( platf::mem_type_e hwdevice_type,
const std::string & display_name,
const ::video::config_t & config )

Create an X11 display capture backend.

Parameters
hwdevice_typeHardware device type requested for capture or encode.
display_nameDisplay name.
configConfiguration values to apply.
Returns
X11 display backend, or nullptr when initialization fails.

◆ x11_display_names()

std::vector< std::string > platf::x11_display_names ( )

Enumerate display names accepted by the X11 backend.

Returns
X11 display names, or an empty list when X11 probing fails.