xcomposite-input.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. #include <obs-module.h>
  2. #include <obs-nix-platform.h>
  3. #include <glad/glad.h>
  4. #include <X11/Xutil.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. gs_texture_t *gltex;
  53. pthread_mutex_t lock;
  54. bool show_cursor;
  55. bool cursor_outside;
  56. xcb_xcursor_t *cursor;
  57. };
  58. static void xcompcap_update(void *data, obs_data_t *settings);
  59. xcb_atom_t get_atom(xcb_connection_t *conn, const char *name)
  60. {
  61. xcb_intern_atom_cookie_t atom_c =
  62. xcb_intern_atom(conn, 1, strlen(name), name);
  63. xcb_intern_atom_reply_t *atom_r =
  64. xcb_intern_atom_reply(conn, atom_c, NULL);
  65. xcb_atom_t a = atom_r->atom;
  66. free(atom_r);
  67. return a;
  68. }
  69. void xcomp_gather_atoms(xcb_connection_t *conn)
  70. {
  71. ATOM_UTF8_STRING = get_atom(conn, "UTF8_STRING");
  72. ATOM_STRING = get_atom(conn, "STRING");
  73. ATOM_TEXT = get_atom(conn, "TEXT");
  74. ATOM_COMPOUND_TEXT = get_atom(conn, "COMPOUND_TEXT");
  75. ATOM_WM_NAME = get_atom(conn, "WM_NAME");
  76. ATOM_WM_CLASS = get_atom(conn, "WM_CLASS");
  77. ATOM__NET_WM_NAME = get_atom(conn, "_NET_WM_NAME");
  78. ATOM__NET_SUPPORTING_WM_CHECK =
  79. get_atom(conn, "_NET_SUPPORTING_WM_CHECK");
  80. ATOM__NET_CLIENT_LIST = get_atom(conn, "_NET_CLIENT_LIST");
  81. }
  82. bool xcomp_window_exists(xcb_connection_t *conn, xcb_window_t win)
  83. {
  84. xcb_generic_error_t *err = NULL;
  85. xcb_get_window_attributes_cookie_t attr_cookie =
  86. xcb_get_window_attributes(conn, win);
  87. xcb_get_window_attributes_reply_t *attr =
  88. xcb_get_window_attributes_reply(conn, attr_cookie, &err);
  89. bool exists = err == NULL && attr->map_state == XCB_MAP_STATE_VIEWABLE;
  90. free(attr);
  91. return exists;
  92. }
  93. xcb_get_property_reply_t *xcomp_property_sync(xcb_connection_t *conn,
  94. xcb_window_t win, xcb_atom_t atom)
  95. {
  96. if (atom == XCB_ATOM_NONE)
  97. return NULL;
  98. xcb_generic_error_t *err = NULL;
  99. // Read properties up to 4096*4 bytes
  100. xcb_get_property_cookie_t prop_cookie =
  101. xcb_get_property(conn, 0, win, atom, 0, 0, 4096);
  102. xcb_get_property_reply_t *prop =
  103. xcb_get_property_reply(conn, prop_cookie, &err);
  104. if (err != NULL || xcb_get_property_value_length(prop) == 0) {
  105. free(prop);
  106. return NULL;
  107. }
  108. return prop;
  109. }
  110. // See ICCCM https://www.x.org/releases/X11R7.6/doc/xorg-docs/specs/ICCCM/icccm.html#text_properties
  111. // for more info on the galactic brained string types used in Xorg.
  112. struct dstr xcomp_window_name(xcb_connection_t *conn, Display *disp,
  113. xcb_window_t win)
  114. {
  115. struct dstr ret = {0};
  116. xcb_get_property_reply_t *name =
  117. xcomp_property_sync(conn, win, ATOM__NET_WM_NAME);
  118. if (name) {
  119. // Guaranteed to be UTF8_STRING.
  120. const char *data = (const char *)xcb_get_property_value(name);
  121. dstr_ncopy(&ret, data, xcb_get_property_value_length(name));
  122. free(name);
  123. return ret;
  124. }
  125. name = xcomp_property_sync(conn, win, ATOM_WM_NAME);
  126. if (!name) {
  127. goto fail;
  128. }
  129. char *data = (char *)xcb_get_property_value(name);
  130. if (name->type == ATOM_UTF8_STRING) {
  131. dstr_ncopy(&ret, data, xcb_get_property_value_length(name));
  132. free(name);
  133. return ret;
  134. }
  135. if (name->type == ATOM_STRING) { // Latin-1, safe enough
  136. dstr_ncopy(&ret, data, xcb_get_property_value_length(name));
  137. free(name);
  138. return ret;
  139. }
  140. if (name->type == ATOM_TEXT) { // Default charset
  141. char *utf8;
  142. if (!os_mbs_to_utf8_ptr(
  143. data, xcb_get_property_value_length(name), &utf8)) {
  144. free(name);
  145. goto fail;
  146. }
  147. free(name);
  148. dstr_init_move_array(&ret, utf8);
  149. return ret;
  150. }
  151. if (name->type ==
  152. ATOM_COMPOUND_TEXT) { // LibX11 is the only decoder for these.
  153. XTextProperty xname = {
  154. (unsigned char *)data, name->type,
  155. 8, // 8 by definition.
  156. 1, // Only decode the first element of string arrays.
  157. };
  158. char **list;
  159. int len = 0;
  160. if (XmbTextPropertyToTextList(disp, &xname, &list, &len) <
  161. Success ||
  162. !list || len < 1) {
  163. free(name);
  164. goto fail;
  165. }
  166. char *utf8;
  167. if (!os_mbs_to_utf8_ptr(list[0], 0, &utf8)) {
  168. XFreeStringList(list);
  169. free(name);
  170. goto fail;
  171. }
  172. dstr_init_move_array(&ret, utf8);
  173. XFreeStringList(list);
  174. free(name);
  175. return ret;
  176. }
  177. fail:
  178. dstr_copy(&ret, "unknown");
  179. return ret;
  180. }
  181. struct dstr xcomp_window_class(xcb_connection_t *conn, xcb_window_t win)
  182. {
  183. struct dstr ret = {0};
  184. dstr_copy(&ret, "unknown");
  185. xcb_get_property_reply_t *cls =
  186. xcomp_property_sync(conn, win, ATOM_WM_CLASS);
  187. if (!cls)
  188. return ret;
  189. // WM_CLASS is formatted differently from other strings, it's two null terminated strings.
  190. // Since we want the first one, let's just let copy run strlen.
  191. dstr_copy(&ret, (const char *)xcb_get_property_value(cls));
  192. free(cls);
  193. return ret;
  194. }
  195. // Specification for checking for ewmh support at
  196. // http://standards.freedesktop.org/wm-spec/wm-spec-latest.html#idm140200472693600
  197. bool xcomp_check_ewmh(xcb_connection_t *conn, xcb_window_t root)
  198. {
  199. xcb_get_property_reply_t *check =
  200. xcomp_property_sync(conn, root, ATOM__NET_SUPPORTING_WM_CHECK);
  201. if (!check)
  202. return false;
  203. xcb_window_t ewmh_window =
  204. ((xcb_window_t *)xcb_get_property_value(check))[0];
  205. free(check);
  206. xcb_get_property_reply_t *check2 = xcomp_property_sync(
  207. conn, ewmh_window, ATOM__NET_SUPPORTING_WM_CHECK);
  208. if (!check2)
  209. return false;
  210. free(check2);
  211. return true;
  212. }
  213. struct darray xcomp_top_level_windows(xcb_connection_t *conn)
  214. {
  215. DARRAY(xcb_window_t) res = {0};
  216. // EWMH top level window listing is not supported.
  217. if (ATOM__NET_CLIENT_LIST == XCB_ATOM_NONE)
  218. return res.da;
  219. xcb_screen_iterator_t screen_iter =
  220. xcb_setup_roots_iterator(xcb_get_setup(conn));
  221. for (; screen_iter.rem > 0; xcb_screen_next(&screen_iter)) {
  222. xcb_generic_error_t *err = NULL;
  223. // Read properties up to 4096*4 bytes
  224. xcb_get_property_cookie_t cl_list_cookie =
  225. xcb_get_property(conn, 0, screen_iter.data->root,
  226. ATOM__NET_CLIENT_LIST, 0, 0, 4096);
  227. xcb_get_property_reply_t *cl_list =
  228. xcb_get_property_reply(conn, cl_list_cookie, &err);
  229. if (err != NULL) {
  230. goto done;
  231. }
  232. uint32_t len = xcb_get_property_value_length(cl_list) /
  233. sizeof(xcb_window_t);
  234. for (uint32_t i = 0; i < len; i++)
  235. da_push_back(res,
  236. &(((xcb_window_t *)xcb_get_property_value(
  237. cl_list))[i]));
  238. done:
  239. free(cl_list);
  240. }
  241. return res.da;
  242. }
  243. xcb_window_t xcomp_find_window(xcb_connection_t *conn, Display *disp,
  244. const char *str)
  245. {
  246. xcb_window_t ret = 0;
  247. DARRAY(xcb_window_t) tlw = {0};
  248. tlw.da = xcomp_top_level_windows(conn);
  249. if (!str || strlen(str) == 0) {
  250. if (tlw.num > 0)
  251. ret = *(xcb_window_t *)darray_item(sizeof(xcb_window_t),
  252. &tlw.da, 0);
  253. goto cleanup1;
  254. }
  255. size_t markSize = strlen(WIN_STRING_DIV);
  256. const char *firstMark = strstr(str, WIN_STRING_DIV);
  257. const char *secondMark =
  258. firstMark ? strstr(firstMark + markSize, WIN_STRING_DIV) : NULL;
  259. const char *strEnd = str + strlen(str);
  260. const char *secondStr = firstMark + markSize;
  261. const char *thirdStr = secondMark + markSize;
  262. // wstr only consists of the window-id
  263. if (!firstMark)
  264. return (xcb_window_t)atol(str);
  265. // wstr also contains window-name and window-class
  266. char *wname = bzalloc(secondMark - secondStr + 1);
  267. char *wcls = bzalloc(strEnd - thirdStr + 1);
  268. memcpy(wname, secondStr, secondMark - secondStr);
  269. memcpy(wcls, thirdStr, strEnd - thirdStr);
  270. xcb_window_t wid = (xcb_window_t)strtol(str, NULL, 10);
  271. // first try to find a match by the window-id
  272. if (da_find(tlw, &wid, 0) != DARRAY_INVALID) {
  273. ret = wid;
  274. goto cleanup2;
  275. }
  276. // then try to find a match by name & class
  277. for (size_t i = 0; i < tlw.num; i++) {
  278. xcb_window_t cwin = *(xcb_window_t *)darray_item(
  279. sizeof(xcb_window_t), &tlw.da, i);
  280. struct dstr cwname = xcomp_window_name(conn, disp, cwin);
  281. struct dstr cwcls = xcomp_window_class(conn, cwin);
  282. bool found = (strcmp(wname, cwname.array) == 0 &&
  283. strcmp(wcls, cwcls.array));
  284. dstr_free(&cwname);
  285. dstr_free(&cwcls);
  286. if (found) {
  287. ret = cwin;
  288. goto cleanup2;
  289. }
  290. }
  291. blog(LOG_DEBUG,
  292. "Did not find new window id for Name '%s' or Class '%s'", wname,
  293. wcls);
  294. cleanup2:
  295. bfree(wname);
  296. bfree(wcls);
  297. cleanup1:
  298. da_free(tlw);
  299. return wid;
  300. }
  301. void xcomp_cleanup_pixmap(Display *disp, struct xcompcap *s)
  302. {
  303. if (s->gltex) {
  304. gs_texture_destroy(s->gltex);
  305. s->gltex = 0;
  306. }
  307. if (s->pixmap) {
  308. XFreePixmap(disp, s->pixmap);
  309. s->pixmap = 0;
  310. }
  311. }
  312. static enum gs_color_format gs_format_from_tex()
  313. {
  314. GLint iformat = 0;
  315. // consider GL_ARB_internalformat_query
  316. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT,
  317. &iformat);
  318. // These formats are known to be wrong on Intel platforms. We intentionally
  319. // use swapped internal formats here to preserve historic behavior which
  320. // swapped colors accidentally and because D3D11 would not support a
  321. // GS_RGBX format.
  322. switch (iformat) {
  323. case GL_RGB:
  324. return GS_BGRX_UNORM;
  325. case GL_RGBA:
  326. return GS_RGBA_UNORM;
  327. default:
  328. return GS_RGBA_UNORM;
  329. }
  330. }
  331. void xcomp_create_pixmap(xcb_connection_t *conn, struct xcompcap *s,
  332. int log_level)
  333. {
  334. if (!s->win)
  335. return;
  336. xcb_generic_error_t *err = NULL;
  337. xcb_get_geometry_cookie_t geom_cookie = xcb_get_geometry(conn, s->win);
  338. xcb_get_geometry_reply_t *geom =
  339. xcb_get_geometry_reply(conn, geom_cookie, &err);
  340. if (err != NULL) {
  341. return;
  342. }
  343. s->border = s->include_border ? geom->border_width : 0;
  344. s->width = geom->width;
  345. s->height = geom->height;
  346. // We don't have an alpha channel, but may have garbage in the texture.
  347. int32_t depth = geom->depth;
  348. if (depth != 32) {
  349. s->exclude_alpha = true;
  350. }
  351. xcb_window_t root = geom->root;
  352. free(geom);
  353. uint32_t vert_borders = s->crop_top + s->crop_bot + 2 * s->border;
  354. uint32_t hori_borders = s->crop_left + s->crop_right + 2 * s->border;
  355. // Skip 0 sized textures.
  356. if (vert_borders > s->height || hori_borders > s->width)
  357. return;
  358. s->pixmap = xcb_generate_id(conn);
  359. xcb_void_cookie_t name_cookie =
  360. xcb_composite_name_window_pixmap(conn, s->win, s->pixmap);
  361. err = NULL;
  362. if ((err = xcb_request_check(conn, name_cookie)) != NULL) {
  363. blog(log_level, "xcb_composite_name_window_pixmap failed");
  364. s->pixmap = 0;
  365. return;
  366. }
  367. s->gltex = gs_texture_create_from_pixmap(s->width, s->height,
  368. GS_BGRA_UNORM, GL_TEXTURE_2D,
  369. (void *)s->pixmap);
  370. }
  371. struct reg_item {
  372. struct xcompcap *src;
  373. xcb_window_t win;
  374. };
  375. static DARRAY(struct reg_item) watcher_registry = {0};
  376. static pthread_mutex_t watcher_lock = PTHREAD_MUTEX_INITIALIZER;
  377. void watcher_register(xcb_connection_t *conn, struct xcompcap *s)
  378. {
  379. pthread_mutex_lock(&watcher_lock);
  380. if (xcomp_window_exists(conn, s->win)) {
  381. // Subscribe to Events
  382. uint32_t vals[1] = {StructureNotifyMask | ExposureMask |
  383. VisibilityChangeMask};
  384. xcb_change_window_attributes(conn, s->win, XCB_CW_EVENT_MASK,
  385. vals);
  386. xcb_composite_redirect_window(conn, s->win,
  387. XCB_COMPOSITE_REDIRECT_AUTOMATIC);
  388. da_push_back(watcher_registry, (&(struct reg_item){s, s->win}));
  389. }
  390. pthread_mutex_unlock(&watcher_lock);
  391. }
  392. void watcher_unregister(xcb_connection_t *conn, struct xcompcap *s)
  393. {
  394. pthread_mutex_lock(&watcher_lock);
  395. size_t idx = DARRAY_INVALID;
  396. xcb_window_t win = 0;
  397. for (size_t i = 0; i < watcher_registry.num; i++) {
  398. struct reg_item *item = (struct reg_item *)darray_item(
  399. sizeof(struct reg_item), &watcher_registry.da, i);
  400. if (item->src == s) {
  401. idx = i;
  402. win = item->win;
  403. break;
  404. }
  405. }
  406. if (idx == DARRAY_INVALID)
  407. goto done;
  408. da_erase(watcher_registry, idx);
  409. // Check if there are still sources listening for the same window.
  410. bool windowInUse = false;
  411. for (size_t i = 0; i < watcher_registry.num; i++) {
  412. struct reg_item *item = (struct reg_item *)darray_item(
  413. sizeof(struct reg_item), &watcher_registry.da, i);
  414. if (item->win == win) {
  415. windowInUse = true;
  416. break;
  417. }
  418. }
  419. if (!windowInUse && xcomp_window_exists(conn, s->win)) {
  420. // Last source released, stop listening for events.
  421. uint32_t vals[1] = {0};
  422. xcb_change_window_attributes(conn, win, XCB_CW_EVENT_MASK,
  423. vals);
  424. }
  425. done:
  426. pthread_mutex_unlock(&watcher_lock);
  427. }
  428. void watcher_process(xcb_generic_event_t *ev)
  429. {
  430. if (!ev)
  431. return;
  432. pthread_mutex_lock(&watcher_lock);
  433. xcb_window_t win = 0;
  434. switch (ev->response_type & ~0x80) {
  435. case XCB_CONFIGURE_NOTIFY:
  436. win = ((xcb_configure_notify_event_t *)ev)->event;
  437. break;
  438. case XCB_MAP_NOTIFY:
  439. win = ((xcb_map_notify_event_t *)ev)->event;
  440. break;
  441. case XCB_EXPOSE:
  442. win = ((xcb_expose_event_t *)ev)->window;
  443. break;
  444. case XCB_VISIBILITY_NOTIFY:
  445. win = ((xcb_visibility_notify_event_t *)ev)->window;
  446. break;
  447. case XCB_DESTROY_NOTIFY:
  448. win = ((xcb_destroy_notify_event_t *)ev)->event;
  449. break;
  450. };
  451. if (win != 0) {
  452. for (size_t i = 0; i < watcher_registry.num; i++) {
  453. struct reg_item *item = (struct reg_item *)darray_item(
  454. sizeof(struct reg_item), &watcher_registry.da,
  455. i);
  456. if (item->win == win) {
  457. item->src->window_changed = true;
  458. }
  459. }
  460. }
  461. pthread_mutex_unlock(&watcher_lock);
  462. }
  463. void watcher_unload()
  464. {
  465. da_free(watcher_registry);
  466. }
  467. static uint32_t xcompcap_get_width(void *data)
  468. {
  469. struct xcompcap *s = (struct xcompcap *)data;
  470. if (!s->gltex)
  471. return 0;
  472. int32_t border = s->crop_left + s->crop_right + 2 * s->border;
  473. int32_t width = s->width - border;
  474. return width < 0 ? 0 : width;
  475. }
  476. static uint32_t xcompcap_get_height(void *data)
  477. {
  478. struct xcompcap *s = (struct xcompcap *)data;
  479. if (!s->gltex)
  480. return 0;
  481. int32_t border = s->crop_bot + s->crop_top + 2 * s->border;
  482. int32_t height = s->height - border;
  483. return height < 0 ? 0 : height;
  484. }
  485. static void *xcompcap_create(obs_data_t *settings, obs_source_t *source)
  486. {
  487. struct xcompcap *s =
  488. (struct xcompcap *)bzalloc(sizeof(struct xcompcap));
  489. pthread_mutex_init(&s->lock, NULL);
  490. s->show_cursor = true;
  491. s->source = source;
  492. obs_enter_graphics();
  493. s->cursor = xcb_xcursor_init(conn);
  494. obs_leave_graphics();
  495. xcompcap_update(s, settings);
  496. return s;
  497. }
  498. static void xcompcap_destroy(void *data)
  499. {
  500. struct xcompcap *s = (struct xcompcap *)data;
  501. obs_enter_graphics();
  502. pthread_mutex_lock(&s->lock);
  503. watcher_unregister(conn, s);
  504. xcomp_cleanup_pixmap(disp, s);
  505. if (s->cursor)
  506. xcb_xcursor_destroy(s->cursor);
  507. pthread_mutex_unlock(&s->lock);
  508. obs_leave_graphics();
  509. pthread_mutex_destroy(&s->lock);
  510. bfree(s);
  511. }
  512. static void xcompcap_video_tick(void *data, float seconds)
  513. {
  514. struct xcompcap *s = (struct xcompcap *)data;
  515. if (!obs_source_showing(s->source))
  516. return;
  517. obs_enter_graphics();
  518. pthread_mutex_lock(&s->lock);
  519. xcb_generic_event_t *event;
  520. while ((event = xcb_poll_for_queued_event(conn)))
  521. watcher_process(event);
  522. // Reacquire window after interval or immediately if reconfigured.
  523. s->window_check_time += seconds;
  524. bool window_lost = !xcomp_window_exists(conn, s->win);
  525. if ((window_lost && s->window_check_time > FIND_WINDOW_INTERVAL) ||
  526. s->window_changed) {
  527. watcher_unregister(conn, s);
  528. s->window_changed = false;
  529. s->window_check_time = 0.0;
  530. s->win = xcomp_find_window(conn, disp, s->windowName);
  531. watcher_register(conn, s);
  532. xcomp_cleanup_pixmap(disp, s);
  533. // Avoid excessive logging. We expect this to fail while windows are
  534. // minimized or on offscreen workspaces or already captured on NVIDIA.
  535. xcomp_create_pixmap(conn, s, LOG_DEBUG);
  536. xcb_xcursor_offset_win(conn, s->cursor, s->win);
  537. xcb_xcursor_offset(s->cursor, s->cursor->x_org + s->crop_left,
  538. s->cursor->y_org + s->crop_top);
  539. }
  540. if (!s->gltex)
  541. goto done;
  542. if (xcompcap_get_height(s) == 0 || xcompcap_get_width(s) == 0)
  543. goto done;
  544. if (s->show_cursor) {
  545. xcb_xcursor_update(conn, s->cursor);
  546. s->cursor_outside = s->cursor->x < 0 || s->cursor->y < 0 ||
  547. s->cursor->x > (int)xcompcap_get_width(s) ||
  548. s->cursor->y > (int)xcompcap_get_height(s);
  549. }
  550. done:
  551. pthread_mutex_unlock(&s->lock);
  552. obs_leave_graphics();
  553. }
  554. static void xcompcap_video_render(void *data, gs_effect_t *effect)
  555. {
  556. gs_eparam_t *image; // Placate C++ goto rules.
  557. struct xcompcap *s = (struct xcompcap *)data;
  558. pthread_mutex_lock(&s->lock);
  559. if (!s->gltex)
  560. goto done;
  561. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  562. if (s->exclude_alpha)
  563. effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  564. image = gs_effect_get_param_by_name(effect, "image");
  565. gs_effect_set_texture(image, s->gltex);
  566. while (gs_effect_loop(effect, "Draw")) {
  567. gs_draw_sprite_subregion(s->gltex, 0, s->crop_left, s->crop_top,
  568. xcompcap_get_width(s),
  569. xcompcap_get_height(s));
  570. }
  571. if (s->gltex && s->show_cursor && !s->cursor_outside) {
  572. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  573. while (gs_effect_loop(effect, "Draw")) {
  574. xcb_xcursor_render(s->cursor);
  575. }
  576. }
  577. done:
  578. pthread_mutex_unlock(&s->lock);
  579. }
  580. struct WindowInfo {
  581. struct dstr name_lower;
  582. struct dstr name;
  583. struct dstr desc;
  584. };
  585. static int cmp_wi(const void *a, const void *b)
  586. {
  587. struct WindowInfo *awi = (struct WindowInfo *)a;
  588. struct WindowInfo *bwi = (struct WindowInfo *)b;
  589. return strcmp(awi->name_lower.array, bwi->name_lower.array);
  590. }
  591. static obs_properties_t *xcompcap_props(void *unused)
  592. {
  593. UNUSED_PARAMETER(unused);
  594. obs_properties_t *props = obs_properties_create();
  595. obs_property_t *wins = obs_properties_add_list(
  596. props, "capture_window", obs_module_text("Window"),
  597. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  598. DARRAY(struct WindowInfo) window_strings = {0};
  599. struct darray windows = xcomp_top_level_windows(conn);
  600. for (size_t w = 0; w < windows.num; w++) {
  601. xcb_window_t win = *(xcb_window_t *)darray_item(
  602. sizeof(xcb_window_t), &windows, w);
  603. struct dstr name = xcomp_window_name(conn, disp, win);
  604. struct dstr cls = xcomp_window_class(conn, win);
  605. struct dstr desc = {0};
  606. dstr_printf(&desc, "%d" WIN_STRING_DIV "%s" WIN_STRING_DIV "%s",
  607. win, name.array, cls.array);
  608. dstr_free(&cls);
  609. struct dstr name_lower;
  610. dstr_init_copy_dstr(&name_lower, &name);
  611. dstr_to_lower(&name_lower);
  612. da_push_back(window_strings,
  613. (&(struct WindowInfo){name_lower, name, desc}));
  614. }
  615. darray_free(&windows);
  616. qsort(window_strings.array, window_strings.num,
  617. sizeof(struct WindowInfo), cmp_wi);
  618. for (size_t i = 0; i < window_strings.num; i++) {
  619. struct WindowInfo *s = (struct WindowInfo *)darray_item(
  620. sizeof(struct WindowInfo), &window_strings.da, i);
  621. obs_property_list_add_string(wins, s->name.array,
  622. s->desc.array);
  623. dstr_free(&s->name_lower);
  624. dstr_free(&s->name);
  625. dstr_free(&s->desc);
  626. }
  627. da_free(window_strings);
  628. obs_properties_add_int(props, "cut_top", obs_module_text("CropTop"), 0,
  629. 4096, 1);
  630. obs_properties_add_int(props, "cut_left", obs_module_text("CropLeft"),
  631. 0, 4096, 1);
  632. obs_properties_add_int(props, "cut_right", obs_module_text("CropRight"),
  633. 0, 4096, 1);
  634. obs_properties_add_int(props, "cut_bot", obs_module_text("CropBottom"),
  635. 0, 4096, 1);
  636. obs_properties_add_bool(props, "swap_redblue",
  637. obs_module_text("SwapRedBlue"));
  638. obs_properties_add_bool(props, "show_cursor",
  639. obs_module_text("CaptureCursor"));
  640. obs_properties_add_bool(props, "include_border",
  641. obs_module_text("IncludeXBorder"));
  642. obs_properties_add_bool(props, "exclude_alpha",
  643. obs_module_text("ExcludeAlpha"));
  644. return props;
  645. }
  646. static void xcompcap_defaults(obs_data_t *settings)
  647. {
  648. obs_data_set_default_string(settings, "capture_window", "");
  649. obs_data_set_default_int(settings, "cut_top", 0);
  650. obs_data_set_default_int(settings, "cut_left", 0);
  651. obs_data_set_default_int(settings, "cut_right", 0);
  652. obs_data_set_default_int(settings, "cut_bot", 0);
  653. obs_data_set_default_bool(settings, "swap_redblue", false);
  654. obs_data_set_default_bool(settings, "show_cursor", true);
  655. obs_data_set_default_bool(settings, "include_border", false);
  656. obs_data_set_default_bool(settings, "exclude_alpha", false);
  657. }
  658. static void xcompcap_update(void *data, obs_data_t *settings)
  659. {
  660. struct xcompcap *s = (struct xcompcap *)data;
  661. obs_enter_graphics();
  662. pthread_mutex_lock(&s->lock);
  663. s->crop_top = obs_data_get_int(settings, "cut_top");
  664. s->crop_left = obs_data_get_int(settings, "cut_left");
  665. s->crop_right = obs_data_get_int(settings, "cut_right");
  666. s->crop_bot = obs_data_get_int(settings, "cut_bot");
  667. s->swapRedBlue = obs_data_get_bool(settings, "swap_redblue");
  668. s->show_cursor = obs_data_get_bool(settings, "show_cursor");
  669. s->include_border = obs_data_get_bool(settings, "include_border");
  670. s->exclude_alpha = obs_data_get_bool(settings, "exclude_alpha");
  671. s->windowName = obs_data_get_string(settings, "capture_window");
  672. s->win = xcomp_find_window(conn, disp, s->windowName);
  673. if (s->win && s->windowName) {
  674. struct dstr wname = xcomp_window_name(conn, disp, s->win);
  675. struct dstr wcls = xcomp_window_class(conn, s->win);
  676. blog(LOG_INFO,
  677. "[window-capture: '%s'] update settings:\n"
  678. "\ttitle: %s\n"
  679. "\tclass: %s\n",
  680. obs_source_get_name(s->source), wname.array, wcls.array);
  681. dstr_free(&wname);
  682. dstr_free(&wcls);
  683. }
  684. watcher_register(conn, s);
  685. xcomp_cleanup_pixmap(disp, s);
  686. xcomp_create_pixmap(conn, s, LOG_ERROR);
  687. xcb_xcursor_offset_win(conn, s->cursor, s->win);
  688. xcb_xcursor_offset(s->cursor, s->cursor->x_org + s->crop_left,
  689. s->cursor->y_org + s->crop_top);
  690. pthread_mutex_unlock(&s->lock);
  691. obs_leave_graphics();
  692. }
  693. static const char *xcompcap_getname(void *data)
  694. {
  695. UNUSED_PARAMETER(data);
  696. return obs_module_text("XCCapture");
  697. }
  698. void xcomposite_load(void)
  699. {
  700. disp = XOpenDisplay(NULL);
  701. conn = XGetXCBConnection(disp);
  702. if (xcb_connection_has_error(conn)) {
  703. blog(LOG_ERROR, "failed opening display");
  704. return;
  705. }
  706. const xcb_query_extension_reply_t *xcomp_ext =
  707. xcb_get_extension_data(conn, &xcb_composite_id);
  708. if (!xcomp_ext->present) {
  709. blog(LOG_ERROR, "Xcomposite extension not supported");
  710. return;
  711. }
  712. xcb_composite_query_version_cookie_t version_cookie =
  713. xcb_composite_query_version(conn, 0, 2);
  714. xcb_composite_query_version_reply_t *version =
  715. xcb_composite_query_version_reply(conn, version_cookie, NULL);
  716. if (version->major_version == 0 && version->minor_version < 2) {
  717. blog(LOG_ERROR, "Xcomposite extension is too old: %d.%d < 0.2",
  718. version->major_version, version->minor_version);
  719. free(version);
  720. return;
  721. }
  722. free(version);
  723. // Must be done before other helpers called.
  724. xcomp_gather_atoms(conn);
  725. xcb_screen_t *screen_default =
  726. xcb_get_screen(conn, DefaultScreen(disp));
  727. if (!screen_default || !xcomp_check_ewmh(conn, screen_default->root)) {
  728. blog(LOG_ERROR,
  729. "window manager does not support Extended Window Manager Hints (EWMH).\nXComposite capture disabled.");
  730. return;
  731. }
  732. struct obs_source_info sinfo = {
  733. .id = "xcomposite_input",
  734. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  735. OBS_SOURCE_DO_NOT_DUPLICATE,
  736. .get_name = xcompcap_getname,
  737. .create = xcompcap_create,
  738. .destroy = xcompcap_destroy,
  739. .get_properties = xcompcap_props,
  740. .get_defaults = xcompcap_defaults,
  741. .update = xcompcap_update,
  742. .video_tick = xcompcap_video_tick,
  743. .video_render = xcompcap_video_render,
  744. .get_width = xcompcap_get_width,
  745. .get_height = xcompcap_get_height,
  746. .icon_type = OBS_ICON_TYPE_WINDOW_CAPTURE,
  747. };
  748. obs_register_source(&sinfo);
  749. }
  750. void xcomposite_unload(void)
  751. {
  752. XCloseDisplay(disp);
  753. disp = NULL;
  754. conn = NULL;
  755. watcher_unload();
  756. }