xcomposite-input.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. #include <obs-module.h>
  2. #include <obs-nix-platform.h>
  3. #include <glad/glad.h>
  4. #include <glad/glad_glx.h>
  5. #include <X11/Xlib-xcb.h>
  6. #include <xcb/xcb.h>
  7. #include <xcb/composite.h>
  8. #include <pthread.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stdint.h>
  12. #include <stdbool.h>
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include "xhelpers.h"
  16. #include "xcursor-xcb.h"
  17. #include "xcomposite-input.h"
  18. #include <util/platform.h>
  19. #include <util/dstr.h>
  20. #include <util/darray.h>
  21. #define WIN_STRING_DIV "\r\n"
  22. #define FIND_WINDOW_INTERVAL 0.5
  23. static Display *disp = NULL;
  24. static xcb_connection_t *conn = NULL;
  25. // Atoms used throughout our plugin
  26. xcb_atom_t ATOM_UTF8_STRING;
  27. xcb_atom_t ATOM_STRING;
  28. xcb_atom_t ATOM_TEXT;
  29. xcb_atom_t ATOM_COMPOUND_TEXT;
  30. xcb_atom_t ATOM_WM_NAME;
  31. xcb_atom_t ATOM_WM_CLASS;
  32. xcb_atom_t ATOM__NET_WM_NAME;
  33. xcb_atom_t ATOM__NET_SUPPORTING_WM_CHECK;
  34. xcb_atom_t ATOM__NET_CLIENT_LIST;
  35. struct xcompcap {
  36. obs_source_t *source;
  37. const char *windowName;
  38. xcb_window_t win;
  39. int crop_top;
  40. int crop_left;
  41. int crop_right;
  42. int crop_bot;
  43. bool swapRedBlue;
  44. bool include_border;
  45. bool exclude_alpha;
  46. float window_check_time;
  47. bool window_changed;
  48. uint32_t width;
  49. uint32_t height;
  50. uint32_t border;
  51. Pixmap pixmap;
  52. GLXPixmap glxpixmap;
  53. gs_texture_t *gltex;
  54. pthread_mutex_t lock;
  55. bool show_cursor;
  56. bool cursor_outside;
  57. xcb_xcursor_t *cursor;
  58. // strict_binding determines whether we rebind the GLX Pixmap on every
  59. // tick. "Strict binding" is the correct mode of operation, according to
  60. // GLX_EXT_texture_from_pixmap. However certain drivers exhibit poor
  61. // performance when this is done, so setting this to false allows working
  62. // around it.
  63. bool strict_binding;
  64. bool egl;
  65. };
  66. static void xcompcap_update(void *data, obs_data_t *settings);
  67. xcb_atom_t get_atom(xcb_connection_t *conn, const char *name)
  68. {
  69. xcb_intern_atom_cookie_t atom_c =
  70. xcb_intern_atom(conn, 1, strlen(name), name);
  71. xcb_intern_atom_reply_t *atom_r =
  72. xcb_intern_atom_reply(conn, atom_c, NULL);
  73. xcb_atom_t a = atom_r->atom;
  74. free(atom_r);
  75. return a;
  76. }
  77. void xcomp_gather_atoms(xcb_connection_t *conn)
  78. {
  79. ATOM_UTF8_STRING = get_atom(conn, "UTF8_STRING");
  80. ATOM_STRING = get_atom(conn, "STRING");
  81. ATOM_TEXT = get_atom(conn, "TEXT");
  82. ATOM_COMPOUND_TEXT = get_atom(conn, "COMPOUND_TEXT");
  83. ATOM_WM_NAME = get_atom(conn, "WM_NAME");
  84. ATOM_WM_CLASS = get_atom(conn, "WM_CLASS");
  85. ATOM__NET_WM_NAME = get_atom(conn, "_NET_WM_NAME");
  86. ATOM__NET_SUPPORTING_WM_CHECK =
  87. get_atom(conn, "_NET_SUPPORTING_WM_CHECK");
  88. ATOM__NET_CLIENT_LIST = get_atom(conn, "_NET_CLIENT_LIST");
  89. }
  90. bool xcomp_window_exists(xcb_connection_t *conn, xcb_window_t win)
  91. {
  92. xcb_generic_error_t *err = NULL;
  93. xcb_get_window_attributes_cookie_t attr_cookie =
  94. xcb_get_window_attributes(conn, win);
  95. xcb_get_window_attributes_reply_t *attr =
  96. xcb_get_window_attributes_reply(conn, attr_cookie, &err);
  97. bool exists = err == NULL && attr->map_state == XCB_MAP_STATE_VIEWABLE;
  98. free(attr);
  99. return exists;
  100. }
  101. xcb_get_property_reply_t *xcomp_property_sync(xcb_connection_t *conn,
  102. xcb_window_t win, xcb_atom_t atom)
  103. {
  104. if (atom == XCB_ATOM_NONE)
  105. return NULL;
  106. xcb_generic_error_t *err = NULL;
  107. // Read properties up to 4096*4 bytes
  108. xcb_get_property_cookie_t prop_cookie =
  109. xcb_get_property(conn, 0, win, atom, 0, 0, 4096);
  110. xcb_get_property_reply_t *prop =
  111. xcb_get_property_reply(conn, prop_cookie, &err);
  112. if (err != NULL || xcb_get_property_value_length(prop) == 0) {
  113. free(prop);
  114. return NULL;
  115. }
  116. return prop;
  117. }
  118. // See ICCCM https://www.x.org/releases/X11R7.6/doc/xorg-docs/specs/ICCCM/icccm.html#text_properties
  119. // for more info on the galactic brained string types used in Xorg.
  120. struct dstr xcomp_window_name(xcb_connection_t *conn, Display *disp,
  121. xcb_window_t win)
  122. {
  123. struct dstr ret = {0};
  124. xcb_get_property_reply_t *name =
  125. xcomp_property_sync(conn, win, ATOM__NET_WM_NAME);
  126. if (name) {
  127. // Guaranteed to be UTF8_STRING.
  128. const char *data = (const char *)xcb_get_property_value(name);
  129. dstr_ncopy(&ret, data, xcb_get_property_value_length(name));
  130. free(name);
  131. return ret;
  132. }
  133. name = xcomp_property_sync(conn, win, ATOM_WM_NAME);
  134. if (!name) {
  135. goto fail;
  136. }
  137. char *data = (char *)xcb_get_property_value(name);
  138. if (name->type == ATOM_UTF8_STRING) {
  139. dstr_ncopy(&ret, data, xcb_get_property_value_length(name));
  140. free(name);
  141. return ret;
  142. }
  143. if (name->type == ATOM_STRING) { // Latin-1, safe enough
  144. dstr_ncopy(&ret, data, xcb_get_property_value_length(name));
  145. free(name);
  146. return ret;
  147. }
  148. if (name->type == ATOM_TEXT) { // Default charset
  149. char *utf8;
  150. if (!os_mbs_to_utf8_ptr(
  151. data, xcb_get_property_value_length(name), &utf8)) {
  152. free(name);
  153. goto fail;
  154. }
  155. free(name);
  156. dstr_init_move_array(&ret, utf8);
  157. return ret;
  158. }
  159. if (name->type ==
  160. ATOM_COMPOUND_TEXT) { // LibX11 is the only decoder for these.
  161. XTextProperty xname = {
  162. (unsigned char *)data, name->type,
  163. 8, // 8 by definition.
  164. 1, // Only decode the first element of string arrays.
  165. };
  166. char **list;
  167. int len = 0;
  168. if (XmbTextPropertyToTextList(disp, &xname, &list, &len) <
  169. Success ||
  170. !list || len < 1) {
  171. free(name);
  172. goto fail;
  173. }
  174. char *utf8;
  175. if (!os_mbs_to_utf8_ptr(list[0], 0, &utf8)) {
  176. XFreeStringList(list);
  177. free(name);
  178. goto fail;
  179. }
  180. dstr_init_move_array(&ret, utf8);
  181. XFreeStringList(list);
  182. free(name);
  183. return ret;
  184. }
  185. fail:
  186. dstr_copy(&ret, "unknown");
  187. return ret;
  188. }
  189. struct dstr xcomp_window_class(xcb_connection_t *conn, xcb_window_t win)
  190. {
  191. struct dstr ret = {0};
  192. dstr_copy(&ret, "unknown");
  193. xcb_get_property_reply_t *cls =
  194. xcomp_property_sync(conn, win, ATOM_WM_CLASS);
  195. if (!cls)
  196. return ret;
  197. // WM_CLASS is formatted differently from other strings, it's two null terminated strings.
  198. // Since we want the first one, let's just let copy run strlen.
  199. dstr_copy(&ret, (const char *)xcb_get_property_value(cls));
  200. free(cls);
  201. return ret;
  202. }
  203. // Specification for checking for ewmh support at
  204. // http://standards.freedesktop.org/wm-spec/wm-spec-latest.html#idm140200472693600
  205. bool xcomp_check_ewmh(xcb_connection_t *conn, xcb_window_t root)
  206. {
  207. xcb_get_property_reply_t *check =
  208. xcomp_property_sync(conn, root, ATOM__NET_SUPPORTING_WM_CHECK);
  209. if (!check)
  210. return false;
  211. xcb_window_t ewmh_window =
  212. ((xcb_window_t *)xcb_get_property_value(check))[0];
  213. free(check);
  214. xcb_get_property_reply_t *check2 = xcomp_property_sync(
  215. conn, ewmh_window, ATOM__NET_SUPPORTING_WM_CHECK);
  216. if (!check2)
  217. return false;
  218. free(check2);
  219. return true;
  220. }
  221. struct darray xcomp_top_level_windows(xcb_connection_t *conn)
  222. {
  223. DARRAY(xcb_window_t) res = {0};
  224. // EWMH top level window listing is not supported.
  225. if (ATOM__NET_CLIENT_LIST == XCB_ATOM_NONE)
  226. return res.da;
  227. xcb_screen_iterator_t screen_iter =
  228. xcb_setup_roots_iterator(xcb_get_setup(conn));
  229. for (; screen_iter.rem > 0; xcb_screen_next(&screen_iter)) {
  230. xcb_generic_error_t *err = NULL;
  231. // Read properties up to 4096*4 bytes
  232. xcb_get_property_cookie_t cl_list_cookie =
  233. xcb_get_property(conn, 0, screen_iter.data->root,
  234. ATOM__NET_CLIENT_LIST, 0, 0, 4096);
  235. xcb_get_property_reply_t *cl_list =
  236. xcb_get_property_reply(conn, cl_list_cookie, &err);
  237. if (err != NULL) {
  238. goto done;
  239. }
  240. uint32_t len = xcb_get_property_value_length(cl_list) /
  241. sizeof(xcb_window_t);
  242. for (uint32_t i = 0; i < len; i++)
  243. da_push_back(res,
  244. &(((xcb_window_t *)xcb_get_property_value(
  245. cl_list))[i]));
  246. done:
  247. free(cl_list);
  248. }
  249. return res.da;
  250. }
  251. xcb_window_t xcomp_find_window(xcb_connection_t *conn, Display *disp,
  252. const char *str)
  253. {
  254. xcb_window_t ret = 0;
  255. DARRAY(xcb_window_t) tlw = {0};
  256. tlw.da = xcomp_top_level_windows(conn);
  257. if (!str || strlen(str) == 0) {
  258. if (tlw.num > 0)
  259. ret = *(xcb_window_t *)darray_item(sizeof(xcb_window_t),
  260. &tlw.da, 0);
  261. goto cleanup1;
  262. }
  263. size_t markSize = strlen(WIN_STRING_DIV);
  264. const char *firstMark = strstr(str, WIN_STRING_DIV);
  265. const char *secondMark =
  266. firstMark ? strstr(firstMark + markSize, WIN_STRING_DIV) : NULL;
  267. const char *strEnd = str + strlen(str);
  268. const char *secondStr = firstMark + markSize;
  269. const char *thirdStr = secondMark + markSize;
  270. // wstr only consists of the window-id
  271. if (!firstMark)
  272. return (xcb_window_t)atol(str);
  273. // wstr also contains window-name and window-class
  274. char *wname = bzalloc(secondMark - secondStr + 1);
  275. char *wcls = bzalloc(strEnd - thirdStr + 1);
  276. memcpy(wname, secondStr, secondMark - secondStr);
  277. memcpy(wcls, thirdStr, strEnd - thirdStr);
  278. xcb_window_t wid = (xcb_window_t)strtol(str, NULL, 10);
  279. // first try to find a match by the window-id
  280. if (da_find(tlw, &wid, 0) != DARRAY_INVALID) {
  281. ret = wid;
  282. goto cleanup2;
  283. }
  284. // then try to find a match by name & class
  285. for (size_t i = 0; i < tlw.num; i++) {
  286. xcb_window_t cwin = *(xcb_window_t *)darray_item(
  287. sizeof(xcb_window_t), &tlw.da, i);
  288. struct dstr cwname = xcomp_window_name(conn, disp, cwin);
  289. struct dstr cwcls = xcomp_window_class(conn, cwin);
  290. bool found = (strcmp(wname, cwname.array) == 0 &&
  291. strcmp(wcls, cwcls.array));
  292. dstr_free(&cwname);
  293. dstr_free(&cwcls);
  294. if (found) {
  295. ret = cwin;
  296. goto cleanup2;
  297. }
  298. }
  299. blog(LOG_DEBUG,
  300. "Did not find new window id for Name '%s' or Class '%s'", wname,
  301. wcls);
  302. cleanup2:
  303. bfree(wname);
  304. bfree(wcls);
  305. cleanup1:
  306. da_free(tlw);
  307. return wid;
  308. }
  309. void xcomp_cleanup_pixmap(Display *disp, struct xcompcap *s)
  310. {
  311. if (s->gltex) {
  312. GLuint gltex = *(GLuint *)gs_texture_get_obj(s->gltex);
  313. glBindTexture(GL_TEXTURE_2D, gltex);
  314. if (s->glxpixmap) {
  315. if (s->strict_binding) {
  316. glXReleaseTexImageEXT(disp, s->glxpixmap,
  317. GLX_FRONT_EXT);
  318. }
  319. glXDestroyPixmap(disp, s->glxpixmap);
  320. s->glxpixmap = 0;
  321. }
  322. gs_texture_destroy(s->gltex);
  323. s->gltex = 0;
  324. }
  325. if (s->pixmap) {
  326. XFreePixmap(disp, s->pixmap);
  327. s->pixmap = 0;
  328. }
  329. }
  330. static enum gs_color_format gs_format_from_tex()
  331. {
  332. GLint iformat = 0;
  333. // consider GL_ARB_internalformat_query
  334. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT,
  335. &iformat);
  336. // These formats are known to be wrong on Intel platforms. We intentionally
  337. // use swapped internal formats here to preserve historic behavior which
  338. // swapped colors accidentally and because D3D11 would not support a
  339. // GS_RGBX format.
  340. switch (iformat) {
  341. case GL_RGB:
  342. return GS_BGRX_UNORM;
  343. case GL_RGBA:
  344. return GS_RGBA_UNORM;
  345. default:
  346. return GS_RGBA_UNORM;
  347. }
  348. }
  349. // These declarations are from libobs-opengl/gl-subsystem.h because we need to
  350. // handle GLX modifying textures outside libobs.
  351. struct fb_info;
  352. struct gs_texture {
  353. gs_device_t *device;
  354. enum gs_texture_type type;
  355. enum gs_color_format format;
  356. GLenum gl_format;
  357. GLenum gl_target;
  358. GLenum gl_internal_format;
  359. GLenum gl_type;
  360. GLuint texture;
  361. uint32_t levels;
  362. bool is_dynamic;
  363. bool is_render_target;
  364. bool is_dummy;
  365. bool gen_mipmaps;
  366. gs_samplerstate_t *cur_sampler;
  367. struct fbo_info *fbo;
  368. };
  369. // End shitty hack.
  370. // XErrorHandler for glXCreatePixmap calls.
  371. static bool pixmap_err = false;
  372. static char pixmap_err_text[200];
  373. static int catch_pixmap_errors(Display *display, XErrorEvent *err)
  374. {
  375. pixmap_err = true;
  376. memset(pixmap_err_text, 0, 200);
  377. XGetErrorText(display, err->error_code, pixmap_err_text, 200);
  378. return 0;
  379. }
  380. void xcomp_create_pixmap(xcb_connection_t *conn, Display *disp,
  381. struct xcompcap *s, int log_level)
  382. {
  383. if (!s->win)
  384. return;
  385. xcb_generic_error_t *err = NULL;
  386. xcb_get_geometry_cookie_t geom_cookie = xcb_get_geometry(conn, s->win);
  387. xcb_get_geometry_reply_t *geom =
  388. xcb_get_geometry_reply(conn, geom_cookie, &err);
  389. if (err != NULL) {
  390. return;
  391. }
  392. s->border = s->include_border ? geom->border_width : 0;
  393. s->width = geom->width;
  394. s->height = geom->height;
  395. // We don't have an alpha channel, but may have garbage in the texture.
  396. int32_t depth = geom->depth;
  397. if (depth != 32) {
  398. s->exclude_alpha = true;
  399. }
  400. xcb_window_t root = geom->root;
  401. free(geom);
  402. uint32_t vert_borders = s->crop_top + s->crop_bot + 2 * s->border;
  403. uint32_t hori_borders = s->crop_left + s->crop_right + 2 * s->border;
  404. // Skip 0 sized textures.
  405. if (vert_borders > s->height || hori_borders > s->width)
  406. return;
  407. s->pixmap = xcb_generate_id(conn);
  408. xcb_void_cookie_t name_cookie =
  409. xcb_composite_name_window_pixmap(conn, s->win, s->pixmap);
  410. err = NULL;
  411. if ((err = xcb_request_check(conn, name_cookie)) != NULL) {
  412. blog(log_level, "xcb_composite_name_window_pixmap failed");
  413. s->pixmap = 0;
  414. return;
  415. }
  416. if (s->egl) {
  417. s->gltex = gs_texture_create_from_pixmap(s->width, s->height,
  418. GS_BGRA_UNORM,
  419. GL_TEXTURE_2D,
  420. (void *)s->pixmap);
  421. } else {
  422. const int config_attrs[] = {GLX_BIND_TO_TEXTURE_RGBA_EXT,
  423. GL_TRUE,
  424. GLX_DRAWABLE_TYPE,
  425. GLX_PIXMAP_BIT,
  426. GLX_BIND_TO_TEXTURE_TARGETS_EXT,
  427. GLX_TEXTURE_2D_BIT_EXT,
  428. GLX_DOUBLEBUFFER,
  429. GL_FALSE,
  430. None};
  431. int nelem = 0;
  432. GLXFBConfig *configs = glXChooseFBConfig(
  433. disp, xcb_get_screen_for_root(conn, root), config_attrs,
  434. &nelem);
  435. bool found = false;
  436. GLXFBConfig config;
  437. for (int i = 0; i < nelem; i++) {
  438. config = configs[i];
  439. XVisualInfo *visual =
  440. glXGetVisualFromFBConfig(disp, config);
  441. if (!visual)
  442. continue;
  443. found = depth == visual->depth;
  444. XFree(visual);
  445. if (found)
  446. break;
  447. }
  448. XFree(configs);
  449. if (!found) {
  450. blog(log_level, "no matching fb config found");
  451. s->pixmap = 0;
  452. return;
  453. }
  454. // Should be consistent format with config we are using. Since we searched on RGBA let's use RGBA here.
  455. const int pixmap_attrs[] = {GLX_TEXTURE_TARGET_EXT,
  456. GLX_TEXTURE_2D_EXT,
  457. GLX_TEXTURE_FORMAT_EXT,
  458. GLX_TEXTURE_FORMAT_RGBA_EXT, None};
  459. // Try very hard to capture errors in glXCreatePixmap for NVIDIA drivers
  460. // where only one pixmap can be bound in GLX at a time.
  461. pixmap_err = false;
  462. XErrorHandler prev = XSetErrorHandler(catch_pixmap_errors);
  463. s->glxpixmap =
  464. glXCreatePixmap(disp, config, s->pixmap, pixmap_attrs);
  465. XSync(disp, false);
  466. s->gltex = gs_texture_create(s->width, s->height, GS_RGBA_UNORM,
  467. 1, 0, GS_GL_DUMMYTEX);
  468. GLuint gltex = *(GLuint *)gs_texture_get_obj(s->gltex);
  469. glBindTexture(GL_TEXTURE_2D, gltex);
  470. // Not respecting a captured glXCreatePixmap error will result in Xorg closing our connection.
  471. if (!pixmap_err)
  472. glXBindTexImageEXT(disp, s->glxpixmap, GLX_FRONT_EXT,
  473. NULL);
  474. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  475. GL_LINEAR);
  476. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  477. GL_LINEAR);
  478. // glxBindTexImageEXT might modify the textures format.
  479. enum gs_color_format format = gs_format_from_tex();
  480. // Check if texture is invalid... because X11 hates us.
  481. int w;
  482. int h;
  483. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
  484. &w);
  485. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
  486. &h);
  487. glBindTexture(GL_TEXTURE_2D, 0);
  488. // We must sync OBS texture format based on any glxBindTexImageEXT changes.
  489. s->gltex->format = format;
  490. XSync(disp, false);
  491. if (pixmap_err || (uint32_t)w < s->width ||
  492. (uint32_t)h < s->height) {
  493. blog(log_level, "glXCreatePixmap failed: %s",
  494. pixmap_err_text);
  495. glXDestroyPixmap(disp, s->glxpixmap);
  496. XFreePixmap(disp, s->pixmap);
  497. gs_texture_destroy(s->gltex);
  498. s->pixmap = 0;
  499. s->glxpixmap = 0;
  500. s->gltex = 0;
  501. XSetErrorHandler(prev);
  502. return;
  503. }
  504. XSetErrorHandler(prev);
  505. }
  506. }
  507. struct reg_item {
  508. struct xcompcap *src;
  509. xcb_window_t win;
  510. };
  511. static DARRAY(struct reg_item) watcher_registry = {0};
  512. static pthread_mutex_t watcher_lock = PTHREAD_MUTEX_INITIALIZER;
  513. void watcher_register(xcb_connection_t *conn, struct xcompcap *s)
  514. {
  515. pthread_mutex_lock(&watcher_lock);
  516. if (xcomp_window_exists(conn, s->win)) {
  517. // Subscribe to Events
  518. uint32_t vals[1] = {StructureNotifyMask | ExposureMask |
  519. VisibilityChangeMask};
  520. xcb_change_window_attributes(conn, s->win, XCB_CW_EVENT_MASK,
  521. vals);
  522. xcb_composite_redirect_window(conn, s->win,
  523. XCB_COMPOSITE_REDIRECT_AUTOMATIC);
  524. da_push_back(watcher_registry, (&(struct reg_item){s, s->win}));
  525. }
  526. pthread_mutex_unlock(&watcher_lock);
  527. }
  528. void watcher_unregister(xcb_connection_t *conn, struct xcompcap *s)
  529. {
  530. pthread_mutex_lock(&watcher_lock);
  531. size_t idx = DARRAY_INVALID;
  532. xcb_window_t win = 0;
  533. for (size_t i = 0; i < watcher_registry.num; i++) {
  534. struct reg_item *item = (struct reg_item *)darray_item(
  535. sizeof(struct reg_item), &watcher_registry.da, i);
  536. if (item->src == s) {
  537. idx = i;
  538. win = item->win;
  539. break;
  540. }
  541. }
  542. if (idx == DARRAY_INVALID)
  543. goto done;
  544. da_erase(watcher_registry, idx);
  545. // Check if there are still sources listening for the same window.
  546. bool windowInUse = false;
  547. for (size_t i = 0; i < watcher_registry.num; i++) {
  548. struct reg_item *item = (struct reg_item *)darray_item(
  549. sizeof(struct reg_item), &watcher_registry.da, i);
  550. if (item->win == win) {
  551. windowInUse = true;
  552. break;
  553. }
  554. }
  555. if (!windowInUse && xcomp_window_exists(conn, s->win)) {
  556. // Last source released, stop listening for events.
  557. uint32_t vals[1] = {0};
  558. xcb_change_window_attributes(conn, win, XCB_CW_EVENT_MASK,
  559. vals);
  560. }
  561. done:
  562. pthread_mutex_unlock(&watcher_lock);
  563. }
  564. void watcher_process(xcb_generic_event_t *ev)
  565. {
  566. if (!ev)
  567. return;
  568. pthread_mutex_lock(&watcher_lock);
  569. xcb_window_t win = 0;
  570. switch (ev->response_type & ~0x80) {
  571. case XCB_CONFIGURE_NOTIFY:
  572. win = ((xcb_configure_notify_event_t *)ev)->event;
  573. break;
  574. case XCB_MAP_NOTIFY:
  575. win = ((xcb_map_notify_event_t *)ev)->event;
  576. break;
  577. case XCB_EXPOSE:
  578. win = ((xcb_expose_event_t *)ev)->window;
  579. break;
  580. case XCB_VISIBILITY_NOTIFY:
  581. win = ((xcb_visibility_notify_event_t *)ev)->window;
  582. break;
  583. case XCB_DESTROY_NOTIFY:
  584. win = ((xcb_destroy_notify_event_t *)ev)->event;
  585. break;
  586. };
  587. if (win != 0) {
  588. for (size_t i = 0; i < watcher_registry.num; i++) {
  589. struct reg_item *item = (struct reg_item *)darray_item(
  590. sizeof(struct reg_item), &watcher_registry.da,
  591. i);
  592. if (item->win == win) {
  593. item->src->window_changed = true;
  594. }
  595. }
  596. }
  597. pthread_mutex_unlock(&watcher_lock);
  598. }
  599. void watcher_unload()
  600. {
  601. da_free(watcher_registry);
  602. }
  603. static uint32_t xcompcap_get_width(void *data)
  604. {
  605. struct xcompcap *s = (struct xcompcap *)data;
  606. if (!s->gltex)
  607. return 0;
  608. int32_t border = s->crop_left + s->crop_right + 2 * s->border;
  609. int32_t width = s->width - border;
  610. return width < 0 ? 0 : width;
  611. }
  612. static uint32_t xcompcap_get_height(void *data)
  613. {
  614. struct xcompcap *s = (struct xcompcap *)data;
  615. if (!s->gltex)
  616. return 0;
  617. int32_t border = s->crop_bot + s->crop_top + 2 * s->border;
  618. int32_t height = s->height - border;
  619. return height < 0 ? 0 : height;
  620. }
  621. static void *xcompcap_create(obs_data_t *settings, obs_source_t *source)
  622. {
  623. struct xcompcap *s =
  624. (struct xcompcap *)bzalloc(sizeof(struct xcompcap));
  625. pthread_mutex_init(&s->lock, NULL);
  626. s->show_cursor = true;
  627. s->strict_binding = true;
  628. s->source = source;
  629. enum obs_nix_platform_type platform = obs_get_nix_platform();
  630. s->egl = platform == OBS_NIX_PLATFORM_X11_EGL;
  631. if (s->egl)
  632. s->strict_binding = false;
  633. obs_enter_graphics();
  634. if (strcmp(glGetString(GL_VENDOR), "NVIDIA Corporation") == 0) {
  635. // Pixmap binds are extremely slow on NVIDIA cards.
  636. // See: https://github.com/obsproject/obs-studio/issues/5685
  637. s->strict_binding = false;
  638. }
  639. s->cursor = xcb_xcursor_init(conn);
  640. obs_leave_graphics();
  641. xcompcap_update(s, settings);
  642. return s;
  643. }
  644. static void xcompcap_destroy(void *data)
  645. {
  646. struct xcompcap *s = (struct xcompcap *)data;
  647. obs_enter_graphics();
  648. pthread_mutex_lock(&s->lock);
  649. watcher_unregister(conn, s);
  650. xcomp_cleanup_pixmap(disp, s);
  651. if (s->cursor)
  652. xcb_xcursor_destroy(s->cursor);
  653. pthread_mutex_unlock(&s->lock);
  654. obs_leave_graphics();
  655. pthread_mutex_destroy(&s->lock);
  656. bfree(s);
  657. }
  658. static void xcompcap_video_tick(void *data, float seconds)
  659. {
  660. struct xcompcap *s = (struct xcompcap *)data;
  661. if (!obs_source_showing(s->source))
  662. return;
  663. obs_enter_graphics();
  664. pthread_mutex_lock(&s->lock);
  665. xcb_generic_event_t *event;
  666. while ((event = xcb_poll_for_queued_event(conn)))
  667. watcher_process(event);
  668. // Reacquire window after interval or immediately if reconfigured.
  669. s->window_check_time += seconds;
  670. bool window_lost = !xcomp_window_exists(conn, s->win);
  671. if ((window_lost && s->window_check_time > FIND_WINDOW_INTERVAL) ||
  672. s->window_changed) {
  673. watcher_unregister(conn, s);
  674. s->window_changed = false;
  675. s->window_check_time = 0.0;
  676. s->win = xcomp_find_window(conn, disp, s->windowName);
  677. watcher_register(conn, s);
  678. xcomp_cleanup_pixmap(disp, s);
  679. // Avoid excessive logging. We expect this to fail while windows are
  680. // minimized or on offscreen workspaces or already captured on NVIDIA.
  681. xcomp_create_pixmap(conn, disp, s, LOG_DEBUG);
  682. xcb_xcursor_offset_win(conn, s->cursor, s->win);
  683. xcb_xcursor_offset(s->cursor, s->cursor->x_org + s->crop_left,
  684. s->cursor->y_org + s->crop_top);
  685. }
  686. if (!s->gltex)
  687. goto done;
  688. if (xcompcap_get_height(s) == 0 || xcompcap_get_width(s) == 0)
  689. goto done;
  690. glBindTexture(GL_TEXTURE_2D, *(GLuint *)gs_texture_get_obj(s->gltex));
  691. if (s->strict_binding && s->glxpixmap) {
  692. glXReleaseTexImageEXT(disp, s->glxpixmap, GLX_FRONT_EXT);
  693. glXBindTexImageEXT(disp, s->glxpixmap, GLX_FRONT_EXT, NULL);
  694. }
  695. glBindTexture(GL_TEXTURE_2D, 0);
  696. if (s->show_cursor) {
  697. xcb_xcursor_update(conn, s->cursor);
  698. s->cursor_outside = s->cursor->x < 0 || s->cursor->y < 0 ||
  699. s->cursor->x > (int)xcompcap_get_width(s) ||
  700. s->cursor->y > (int)xcompcap_get_height(s);
  701. }
  702. done:
  703. pthread_mutex_unlock(&s->lock);
  704. obs_leave_graphics();
  705. }
  706. static void xcompcap_video_render(void *data, gs_effect_t *effect)
  707. {
  708. gs_eparam_t *image; // Placate C++ goto rules.
  709. struct xcompcap *s = (struct xcompcap *)data;
  710. pthread_mutex_lock(&s->lock);
  711. if (!s->gltex)
  712. goto done;
  713. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  714. if (s->exclude_alpha)
  715. effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  716. image = gs_effect_get_param_by_name(effect, "image");
  717. gs_effect_set_texture(image, s->gltex);
  718. while (gs_effect_loop(effect, "Draw")) {
  719. gs_draw_sprite_subregion(s->gltex, 0, s->crop_left, s->crop_top,
  720. xcompcap_get_width(s),
  721. xcompcap_get_height(s));
  722. }
  723. if (s->gltex && s->show_cursor && !s->cursor_outside) {
  724. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  725. while (gs_effect_loop(effect, "Draw")) {
  726. xcb_xcursor_render(s->cursor);
  727. }
  728. }
  729. done:
  730. pthread_mutex_unlock(&s->lock);
  731. }
  732. struct WindowInfo {
  733. struct dstr name_lower;
  734. struct dstr name;
  735. struct dstr desc;
  736. };
  737. static int cmp_wi(const void *a, const void *b)
  738. {
  739. struct WindowInfo *awi = (struct WindowInfo *)a;
  740. struct WindowInfo *bwi = (struct WindowInfo *)b;
  741. return strcmp(awi->name_lower.array, bwi->name_lower.array);
  742. }
  743. static obs_properties_t *xcompcap_props(void *unused)
  744. {
  745. UNUSED_PARAMETER(unused);
  746. obs_properties_t *props = obs_properties_create();
  747. obs_property_t *wins = obs_properties_add_list(
  748. props, "capture_window", obs_module_text("Window"),
  749. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  750. DARRAY(struct WindowInfo) window_strings = {0};
  751. struct darray windows = xcomp_top_level_windows(conn);
  752. for (size_t w = 0; w < windows.num; w++) {
  753. xcb_window_t win = *(xcb_window_t *)darray_item(
  754. sizeof(xcb_window_t), &windows, w);
  755. struct dstr name = xcomp_window_name(conn, disp, win);
  756. struct dstr cls = xcomp_window_class(conn, win);
  757. struct dstr desc = {0};
  758. dstr_printf(&desc, "%d" WIN_STRING_DIV "%s" WIN_STRING_DIV "%s",
  759. win, name.array, cls.array);
  760. dstr_free(&cls);
  761. struct dstr name_lower;
  762. dstr_init_copy_dstr(&name_lower, &name);
  763. dstr_to_lower(&name_lower);
  764. da_push_back(window_strings,
  765. (&(struct WindowInfo){name_lower, name, desc}));
  766. }
  767. darray_free(&windows);
  768. qsort(window_strings.array, window_strings.num,
  769. sizeof(struct WindowInfo), cmp_wi);
  770. for (size_t i = 0; i < window_strings.num; i++) {
  771. struct WindowInfo *s = (struct WindowInfo *)darray_item(
  772. sizeof(struct WindowInfo), &window_strings.da, i);
  773. obs_property_list_add_string(wins, s->name.array,
  774. s->desc.array);
  775. dstr_free(&s->name_lower);
  776. dstr_free(&s->name);
  777. dstr_free(&s->desc);
  778. }
  779. da_free(window_strings);
  780. obs_properties_add_int(props, "cut_top", obs_module_text("CropTop"), 0,
  781. 4096, 1);
  782. obs_properties_add_int(props, "cut_left", obs_module_text("CropLeft"),
  783. 0, 4096, 1);
  784. obs_properties_add_int(props, "cut_right", obs_module_text("CropRight"),
  785. 0, 4096, 1);
  786. obs_properties_add_int(props, "cut_bot", obs_module_text("CropBottom"),
  787. 0, 4096, 1);
  788. obs_properties_add_bool(props, "swap_redblue",
  789. obs_module_text("SwapRedBlue"));
  790. obs_properties_add_bool(props, "show_cursor",
  791. obs_module_text("CaptureCursor"));
  792. obs_properties_add_bool(props, "include_border",
  793. obs_module_text("IncludeXBorder"));
  794. obs_properties_add_bool(props, "exclude_alpha",
  795. obs_module_text("ExcludeAlpha"));
  796. return props;
  797. }
  798. static void xcompcap_defaults(obs_data_t *settings)
  799. {
  800. obs_data_set_default_string(settings, "capture_window", "");
  801. obs_data_set_default_int(settings, "cut_top", 0);
  802. obs_data_set_default_int(settings, "cut_left", 0);
  803. obs_data_set_default_int(settings, "cut_right", 0);
  804. obs_data_set_default_int(settings, "cut_bot", 0);
  805. obs_data_set_default_bool(settings, "swap_redblue", false);
  806. obs_data_set_default_bool(settings, "show_cursor", true);
  807. obs_data_set_default_bool(settings, "include_border", false);
  808. obs_data_set_default_bool(settings, "exclude_alpha", false);
  809. }
  810. static void xcompcap_update(void *data, obs_data_t *settings)
  811. {
  812. struct xcompcap *s = (struct xcompcap *)data;
  813. obs_enter_graphics();
  814. pthread_mutex_lock(&s->lock);
  815. s->crop_top = obs_data_get_int(settings, "cut_top");
  816. s->crop_left = obs_data_get_int(settings, "cut_left");
  817. s->crop_right = obs_data_get_int(settings, "cut_right");
  818. s->crop_bot = obs_data_get_int(settings, "cut_bot");
  819. s->swapRedBlue = obs_data_get_bool(settings, "swap_redblue");
  820. s->show_cursor = obs_data_get_bool(settings, "show_cursor");
  821. s->include_border = obs_data_get_bool(settings, "include_border");
  822. s->exclude_alpha = obs_data_get_bool(settings, "exclude_alpha");
  823. s->windowName = obs_data_get_string(settings, "capture_window");
  824. s->win = xcomp_find_window(conn, disp, s->windowName);
  825. if (s->win && s->windowName) {
  826. struct dstr wname = xcomp_window_name(conn, disp, s->win);
  827. struct dstr wcls = xcomp_window_class(conn, s->win);
  828. blog(LOG_INFO,
  829. "[window-capture: '%s'] update settings:\n"
  830. "\ttitle: %s\n"
  831. "\tclass: %s\n",
  832. obs_source_get_name(s->source), wname.array, wcls.array);
  833. dstr_free(&wname);
  834. dstr_free(&wcls);
  835. }
  836. watcher_register(conn, s);
  837. xcomp_cleanup_pixmap(disp, s);
  838. xcomp_create_pixmap(conn, disp, s, LOG_ERROR);
  839. xcb_xcursor_offset_win(conn, s->cursor, s->win);
  840. xcb_xcursor_offset(s->cursor, s->cursor->x_org + s->crop_left,
  841. s->cursor->y_org + s->crop_top);
  842. pthread_mutex_unlock(&s->lock);
  843. obs_leave_graphics();
  844. }
  845. static const char *xcompcap_getname(void *data)
  846. {
  847. UNUSED_PARAMETER(data);
  848. return obs_module_text("XCCapture");
  849. }
  850. void xcomposite_load(void)
  851. {
  852. disp = XOpenDisplay(NULL);
  853. conn = XGetXCBConnection(disp);
  854. if (xcb_connection_has_error(conn)) {
  855. blog(LOG_ERROR, "failed opening display");
  856. return;
  857. }
  858. const xcb_query_extension_reply_t *xcomp_ext =
  859. xcb_get_extension_data(conn, &xcb_composite_id);
  860. if (!xcomp_ext->present) {
  861. blog(LOG_ERROR, "Xcomposite extension not supported");
  862. return;
  863. }
  864. xcb_composite_query_version_cookie_t version_cookie =
  865. xcb_composite_query_version(conn, 0, 2);
  866. xcb_composite_query_version_reply_t *version =
  867. xcb_composite_query_version_reply(conn, version_cookie, NULL);
  868. if (version->major_version == 0 && version->minor_version < 2) {
  869. blog(LOG_ERROR, "Xcomposite extension is too old: %d.%d < 0.2",
  870. version->major_version, version->minor_version);
  871. free(version);
  872. return;
  873. }
  874. free(version);
  875. // Must be done before other helpers called.
  876. xcomp_gather_atoms(conn);
  877. xcb_screen_t *screen_default =
  878. xcb_get_screen(conn, DefaultScreen(disp));
  879. if (!screen_default || !xcomp_check_ewmh(conn, screen_default->root)) {
  880. blog(LOG_ERROR,
  881. "window manager does not support Extended Window Manager Hints (EWMH).\nXComposite capture disabled.");
  882. return;
  883. }
  884. struct obs_source_info sinfo = {
  885. .id = "xcomposite_input",
  886. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  887. OBS_SOURCE_DO_NOT_DUPLICATE,
  888. .get_name = xcompcap_getname,
  889. .create = xcompcap_create,
  890. .destroy = xcompcap_destroy,
  891. .get_properties = xcompcap_props,
  892. .get_defaults = xcompcap_defaults,
  893. .update = xcompcap_update,
  894. .video_tick = xcompcap_video_tick,
  895. .video_render = xcompcap_video_render,
  896. .get_width = xcompcap_get_width,
  897. .get_height = xcompcap_get_height,
  898. .icon_type = OBS_ICON_TYPE_WINDOW_CAPTURE,
  899. };
  900. obs_register_source(&sinfo);
  901. }
  902. void xcomposite_unload(void)
  903. {
  904. XCloseDisplay(disp);
  905. disp = NULL;
  906. conn = NULL;
  907. watcher_unload();
  908. }