1
0

xshm-input.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. Copyright (C) 2014 by Leonhard Oelke <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <inttypes.h>
  17. #include <xcb/randr.h>
  18. #include <xcb/shm.h>
  19. #include <xcb/xfixes.h>
  20. #include <xcb/xinerama.h>
  21. #include <obs-module.h>
  22. #include <util/dstr.h>
  23. #include "xcursor-xcb.h"
  24. #include "xhelpers.h"
  25. #define XSHM_DATA(voidptr) struct xshm_data *data = voidptr;
  26. #define blog(level, msg, ...) blog(level, "xshm-input: " msg, ##__VA_ARGS__)
  27. #define INVALID_DISPLAY (-1)
  28. struct xshm_data {
  29. obs_source_t *source;
  30. xcb_connection_t *xcb;
  31. xcb_screen_t *xcb_screen;
  32. xcb_shm_t *xshm;
  33. xcb_xcursor_t *cursor;
  34. char *server;
  35. int_fast32_t screen_id;
  36. int_fast32_t x_org;
  37. int_fast32_t y_org;
  38. int_fast32_t width;
  39. int_fast32_t height;
  40. gs_texture_t *texture;
  41. int_fast32_t cut_top;
  42. int_fast32_t cut_left;
  43. int_fast32_t cut_right;
  44. int_fast32_t cut_bot;
  45. int_fast32_t adj_x_org;
  46. int_fast32_t adj_y_org;
  47. int_fast32_t adj_width;
  48. int_fast32_t adj_height;
  49. bool show_cursor;
  50. bool use_xinerama;
  51. bool use_randr;
  52. bool advanced;
  53. };
  54. /**
  55. * Resize the texture
  56. *
  57. * This will automatically create the texture if it does not exist
  58. *
  59. * @note requires to be called within the obs graphics context
  60. */
  61. static inline void xshm_resize_texture(struct xshm_data *data)
  62. {
  63. if (data->texture)
  64. gs_texture_destroy(data->texture);
  65. data->texture = gs_texture_create(data->adj_width, data->adj_height, GS_BGRA, 1, NULL, GS_DYNAMIC);
  66. }
  67. /**
  68. * Check if the xserver supports all the extensions we need
  69. */
  70. static bool xshm_check_extensions(xcb_connection_t *xcb)
  71. {
  72. bool ok = true;
  73. if (!xcb_get_extension_data(xcb, &xcb_shm_id)->present) {
  74. blog(LOG_ERROR, "Missing SHM extension !");
  75. ok = false;
  76. }
  77. if (!xcb_get_extension_data(xcb, &xcb_xinerama_id)->present)
  78. blog(LOG_INFO, "Missing Xinerama extension !");
  79. if (!xcb_get_extension_data(xcb, &xcb_randr_id)->present)
  80. blog(LOG_INFO, "Missing Randr extension !");
  81. return ok;
  82. }
  83. /**
  84. * Update the capture
  85. *
  86. * @return < 0 on error, 0 when size is unchanged, > 1 on size change
  87. */
  88. static int_fast32_t xshm_update_geometry(struct xshm_data *data)
  89. {
  90. int_fast32_t prev_width = data->adj_width;
  91. int_fast32_t prev_height = data->adj_height;
  92. if (data->use_randr) {
  93. if (randr_screen_geo(data->xcb, data->screen_id, &data->x_org, &data->y_org, &data->width,
  94. &data->height, &data->xcb_screen, NULL) < 0) {
  95. return -1;
  96. }
  97. } else if (data->use_xinerama) {
  98. if (xinerama_screen_geo(data->xcb, data->screen_id, &data->x_org, &data->y_org, &data->width,
  99. &data->height) < 0) {
  100. return -1;
  101. }
  102. data->xcb_screen = xcb_get_screen(data->xcb, 0);
  103. } else {
  104. data->x_org = 0;
  105. data->y_org = 0;
  106. if (x11_screen_geo(data->xcb, data->screen_id, &data->width, &data->height) < 0) {
  107. return -1;
  108. }
  109. data->xcb_screen = xcb_get_screen(data->xcb, data->screen_id);
  110. }
  111. if (!data->width || !data->height) {
  112. blog(LOG_ERROR, "Failed to get geometry");
  113. return -1;
  114. }
  115. data->adj_y_org = data->y_org;
  116. data->adj_x_org = data->x_org;
  117. data->adj_height = data->height;
  118. data->adj_width = data->width;
  119. if (data->cut_top != 0) {
  120. if (data->y_org > 0)
  121. data->adj_y_org = data->y_org + data->cut_top;
  122. else
  123. data->adj_y_org = data->cut_top;
  124. data->adj_height = data->adj_height - data->cut_top;
  125. }
  126. if (data->cut_left != 0) {
  127. if (data->x_org > 0)
  128. data->adj_x_org = data->x_org + data->cut_left;
  129. else
  130. data->adj_x_org = data->cut_left;
  131. data->adj_width = data->adj_width - data->cut_left;
  132. }
  133. if (data->cut_right != 0)
  134. data->adj_width = data->adj_width - data->cut_right;
  135. if (data->cut_bot != 0)
  136. data->adj_height = data->adj_height - data->cut_bot;
  137. blog(LOG_INFO, "Geometry %" PRIdFAST32 "x%" PRIdFAST32 " @ %" PRIdFAST32 ",%" PRIdFAST32, data->width,
  138. data->height, data->x_org, data->y_org);
  139. if (prev_width == data->adj_width && prev_height == data->adj_height)
  140. return 0;
  141. return 1;
  142. }
  143. /**
  144. * Returns the name of the plugin
  145. */
  146. static const char *xshm_getname(void *unused)
  147. {
  148. UNUSED_PARAMETER(unused);
  149. return obs_module_text("X11SharedMemoryDisplayInput");
  150. }
  151. /**
  152. * Stop the capture
  153. */
  154. static void xshm_capture_stop(struct xshm_data *data)
  155. {
  156. obs_enter_graphics();
  157. if (data->texture) {
  158. gs_texture_destroy(data->texture);
  159. data->texture = NULL;
  160. }
  161. if (data->cursor) {
  162. xcb_xcursor_destroy(data->cursor);
  163. data->cursor = NULL;
  164. }
  165. obs_leave_graphics();
  166. if (data->xshm) {
  167. xshm_xcb_detach(data->xshm);
  168. data->xshm = NULL;
  169. }
  170. if (data->xcb) {
  171. xcb_disconnect(data->xcb);
  172. data->xcb = NULL;
  173. }
  174. if (data->server) {
  175. bfree(data->server);
  176. data->server = NULL;
  177. }
  178. }
  179. /**
  180. * Start the capture
  181. */
  182. static void xshm_capture_start(struct xshm_data *data)
  183. {
  184. const char *server = (data->advanced && *data->server) ? data->server : NULL;
  185. if (data->screen_id == -1) {
  186. if (data->texture) {
  187. gs_texture_destroy(data->texture);
  188. data->texture = NULL;
  189. }
  190. data->adj_width = 0;
  191. data->adj_height = 0;
  192. return;
  193. }
  194. data->xcb = xcb_connect(server, NULL);
  195. if (!data->xcb || xcb_connection_has_error(data->xcb)) {
  196. blog(LOG_ERROR, "Unable to open X display !");
  197. goto fail;
  198. }
  199. if (!xshm_check_extensions(data->xcb))
  200. goto fail;
  201. data->use_randr = randr_is_active(data->xcb) ? true : false;
  202. data->use_xinerama = xinerama_is_active(data->xcb) ? true : false;
  203. if (xshm_update_geometry(data) < 0) {
  204. blog(LOG_ERROR, "failed to update geometry !");
  205. goto fail;
  206. }
  207. data->xshm = xshm_xcb_attach(data->xcb, data->adj_width, data->adj_height);
  208. if (!data->xshm) {
  209. blog(LOG_ERROR, "failed to attach shm !");
  210. goto fail;
  211. }
  212. data->cursor = xcb_xcursor_init(data->xcb);
  213. xcb_xcursor_offset(data->cursor, data->adj_x_org, data->adj_y_org);
  214. obs_enter_graphics();
  215. xshm_resize_texture(data);
  216. obs_leave_graphics();
  217. return;
  218. fail:
  219. xshm_capture_stop(data);
  220. }
  221. /**
  222. * Update the capture with changed settings
  223. */
  224. static void xshm_update(void *vptr, obs_data_t *settings)
  225. {
  226. XSHM_DATA(vptr);
  227. xshm_capture_stop(data);
  228. data->screen_id = obs_data_get_int(settings, "screen");
  229. data->show_cursor = obs_data_get_bool(settings, "show_cursor");
  230. data->advanced = obs_data_get_bool(settings, "advanced");
  231. data->server = bstrdup(obs_data_get_string(settings, "server"));
  232. data->cut_top = obs_data_get_int(settings, "cut_top");
  233. data->cut_left = obs_data_get_int(settings, "cut_left");
  234. data->cut_right = obs_data_get_int(settings, "cut_right");
  235. data->cut_bot = obs_data_get_int(settings, "cut_bot");
  236. xshm_capture_start(data);
  237. }
  238. /**
  239. * Get the default settings for the capture
  240. */
  241. static void xshm_defaults(obs_data_t *defaults, int ver)
  242. {
  243. obs_data_set_default_int(defaults, "screen", ver == 1 ? 0 : INVALID_DISPLAY);
  244. obs_data_set_default_bool(defaults, "show_cursor", true);
  245. obs_data_set_default_bool(defaults, "advanced", false);
  246. obs_data_set_default_int(defaults, "cut_top", 0);
  247. obs_data_set_default_int(defaults, "cut_left", 0);
  248. obs_data_set_default_int(defaults, "cut_right", 0);
  249. obs_data_set_default_int(defaults, "cut_bot", 0);
  250. }
  251. static void xshm_defaults_v1(obs_data_t *defaults)
  252. {
  253. xshm_defaults(defaults, 1);
  254. }
  255. static void xshm_defaults_v2(obs_data_t *defaults)
  256. {
  257. xshm_defaults(defaults, 2);
  258. }
  259. /**
  260. * Toggle visibility of advanced settings
  261. */
  262. static bool xshm_toggle_advanced(obs_properties_t *props, obs_property_t *p, obs_data_t *settings)
  263. {
  264. UNUSED_PARAMETER(p);
  265. const bool visible = obs_data_get_bool(settings, "advanced");
  266. obs_property_t *server = obs_properties_get(props, "server");
  267. obs_property_set_visible(server, visible);
  268. /* trigger server changed callback so the screen list is refreshed */
  269. obs_property_modified(server, settings);
  270. return true;
  271. }
  272. /**
  273. * The x server was changed
  274. */
  275. static bool xshm_server_changed(obs_properties_t *props, obs_property_t *p, obs_data_t *settings)
  276. {
  277. UNUSED_PARAMETER(p);
  278. bool advanced = obs_data_get_bool(settings, "advanced");
  279. int_fast32_t old_screen = obs_data_get_int(settings, "screen");
  280. const char *server = obs_data_get_string(settings, "server");
  281. obs_property_t *screens = obs_properties_get(props, "screen");
  282. /* we want a real NULL here in case there is no string here */
  283. server = (advanced && *server) ? server : NULL;
  284. obs_property_list_clear(screens);
  285. if (old_screen == INVALID_DISPLAY) {
  286. obs_property_list_add_int(screens, obs_module_text("SelectADisplay"), INVALID_DISPLAY);
  287. obs_property_list_item_disable(screens, 0, true);
  288. }
  289. xcb_connection_t *xcb = xcb_connect(server, NULL);
  290. if (!xcb || xcb_connection_has_error(xcb)) {
  291. obs_property_set_enabled(screens, false);
  292. return true;
  293. }
  294. struct dstr screen_info;
  295. dstr_init(&screen_info);
  296. bool randr = randr_is_active(xcb);
  297. bool xinerama = xinerama_is_active(xcb);
  298. int_fast32_t count =
  299. randr ? randr_screen_count(xcb)
  300. : (xinerama ? xinerama_screen_count(xcb) : xcb_setup_roots_length(xcb_get_setup(xcb)));
  301. for (int_fast32_t i = 0; i < count; ++i) {
  302. char *name;
  303. char name_tmp[20];
  304. int_fast32_t x, y, w, h;
  305. x = y = w = h = 0;
  306. name = NULL;
  307. if (randr)
  308. randr_screen_geo(xcb, i, &x, &y, &w, &h, NULL, &name);
  309. else if (xinerama)
  310. xinerama_screen_geo(xcb, i, &x, &y, &w, &h);
  311. else
  312. x11_screen_geo(xcb, i, &w, &h);
  313. if (name == NULL) {
  314. int ret = snprintf(name_tmp, sizeof(name_tmp), "%" PRIuFAST32, i);
  315. if (ret >= (int)sizeof(name_tmp))
  316. blog(LOG_DEBUG, "linux-capture: A format truncation may have occurred."
  317. " This can be ignored since it is quite improbable.");
  318. name = name_tmp;
  319. }
  320. dstr_printf(&screen_info, "%s (%" PRIuFAST32 "x%" PRIuFAST32 " @ %" PRIuFAST32 ",%" PRIuFAST32 ")",
  321. name, w, h, x, y);
  322. if (name != name_tmp)
  323. free(name);
  324. if (h > 0 && w > 0)
  325. obs_property_list_add_int(screens, screen_info.array, i);
  326. }
  327. /* handle missing screen */
  328. if (old_screen + 1 > count) {
  329. dstr_printf(&screen_info, "Display %" PRIuFAST32 " (not found)", old_screen);
  330. size_t index = obs_property_list_add_int(screens, screen_info.array, old_screen);
  331. obs_property_list_item_disable(screens, index, true);
  332. }
  333. dstr_free(&screen_info);
  334. xcb_disconnect(xcb);
  335. obs_property_set_enabled(screens, true);
  336. return true;
  337. }
  338. /**
  339. * Get the properties for the capture
  340. */
  341. static obs_properties_t *xshm_properties(void *vptr)
  342. {
  343. XSHM_DATA(vptr);
  344. obs_properties_t *props = obs_properties_create();
  345. obs_property_t *prop;
  346. obs_properties_add_list(props, "screen", obs_module_text("Display"), OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  347. obs_properties_add_bool(props, "show_cursor", obs_module_text("CaptureCursor"));
  348. obs_property_t *advanced = obs_properties_add_bool(props, "advanced", obs_module_text("AdvancedSettings"));
  349. prop = obs_properties_add_int(props, "cut_top", obs_module_text("CropTop"), -4096, 4096, 1);
  350. obs_property_int_set_suffix(prop, " px");
  351. prop = obs_properties_add_int(props, "cut_left", obs_module_text("CropLeft"), -4096, 4096, 1);
  352. obs_property_int_set_suffix(prop, " px");
  353. prop = obs_properties_add_int(props, "cut_right", obs_module_text("CropRight"), 0, 4096, 1);
  354. obs_property_int_set_suffix(prop, " px");
  355. prop = obs_properties_add_int(props, "cut_bot", obs_module_text("CropBottom"), 0, 4096, 1);
  356. obs_property_int_set_suffix(prop, " px");
  357. obs_property_t *server = obs_properties_add_text(props, "server", obs_module_text("XServer"), OBS_TEXT_DEFAULT);
  358. obs_property_set_modified_callback(advanced, xshm_toggle_advanced);
  359. obs_property_set_modified_callback(server, xshm_server_changed);
  360. /* trigger server callback to get screen count ... */
  361. obs_data_t *settings = obs_source_get_settings(data->source);
  362. obs_property_modified(server, settings);
  363. obs_data_release(settings);
  364. return props;
  365. }
  366. /**
  367. * Destroy the capture
  368. */
  369. static void xshm_destroy(void *vptr)
  370. {
  371. XSHM_DATA(vptr);
  372. if (!data)
  373. return;
  374. xshm_capture_stop(data);
  375. bfree(data);
  376. }
  377. /**
  378. * Create the capture
  379. */
  380. static void *xshm_create(obs_data_t *settings, obs_source_t *source)
  381. {
  382. struct xshm_data *data = bzalloc(sizeof(struct xshm_data));
  383. data->source = source;
  384. xshm_update(data, settings);
  385. return data;
  386. }
  387. /**
  388. * Prepare the capture data
  389. */
  390. static void xshm_video_tick(void *vptr, float seconds)
  391. {
  392. UNUSED_PARAMETER(seconds);
  393. XSHM_DATA(vptr);
  394. if (!data->texture)
  395. return;
  396. if (!obs_source_showing(data->source))
  397. return;
  398. xcb_shm_get_image_cookie_t img_c;
  399. xcb_shm_get_image_reply_t *img_r;
  400. img_c = xcb_shm_get_image_unchecked(data->xcb, data->xcb_screen->root, data->adj_x_org, data->adj_y_org,
  401. data->adj_width, data->adj_height, ~0, XCB_IMAGE_FORMAT_Z_PIXMAP,
  402. data->xshm->seg, 0);
  403. img_r = xcb_shm_get_image_reply(data->xcb, img_c, NULL);
  404. if (!img_r)
  405. goto exit;
  406. obs_enter_graphics();
  407. gs_texture_set_image(data->texture, (void *)data->xshm->data, data->adj_width * 4, false);
  408. xcb_xcursor_update(data->xcb, data->cursor);
  409. obs_leave_graphics();
  410. exit:
  411. free(img_r);
  412. }
  413. /**
  414. * Render the capture data
  415. */
  416. static void xshm_video_render(void *vptr, gs_effect_t *effect)
  417. {
  418. XSHM_DATA(vptr);
  419. effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  420. if (!data->texture)
  421. return;
  422. const bool linear_srgb = gs_get_linear_srgb();
  423. const bool previous = gs_framebuffer_srgb_enabled();
  424. gs_enable_framebuffer_srgb(linear_srgb);
  425. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  426. if (linear_srgb)
  427. gs_effect_set_texture_srgb(image, data->texture);
  428. else
  429. gs_effect_set_texture(image, data->texture);
  430. while (gs_effect_loop(effect, "Draw")) {
  431. gs_draw_sprite(data->texture, 0, 0, 0);
  432. }
  433. gs_enable_framebuffer_srgb(previous);
  434. if (data->show_cursor) {
  435. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  436. while (gs_effect_loop(effect, "Draw")) {
  437. xcb_xcursor_render(data->cursor);
  438. }
  439. }
  440. }
  441. /**
  442. * Width of the captured data
  443. */
  444. static uint32_t xshm_getwidth(void *vptr)
  445. {
  446. XSHM_DATA(vptr);
  447. return data->adj_width;
  448. }
  449. /**
  450. * Height of the captured data
  451. */
  452. static uint32_t xshm_getheight(void *vptr)
  453. {
  454. XSHM_DATA(vptr);
  455. return data->adj_height;
  456. }
  457. struct obs_source_info xshm_input = {
  458. .id = "xshm_input",
  459. .type = OBS_SOURCE_TYPE_INPUT,
  460. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW | OBS_SOURCE_DO_NOT_DUPLICATE | OBS_SOURCE_SRGB |
  461. OBS_SOURCE_CAP_OBSOLETE,
  462. .get_name = xshm_getname,
  463. .create = xshm_create,
  464. .destroy = xshm_destroy,
  465. .update = xshm_update,
  466. .get_defaults = xshm_defaults_v1,
  467. .get_properties = xshm_properties,
  468. .video_tick = xshm_video_tick,
  469. .video_render = xshm_video_render,
  470. .get_width = xshm_getwidth,
  471. .get_height = xshm_getheight,
  472. .icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE,
  473. };
  474. struct obs_source_info xshm_input_v2 = {
  475. .id = "xshm_input_v2",
  476. .type = OBS_SOURCE_TYPE_INPUT,
  477. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW | OBS_SOURCE_DO_NOT_DUPLICATE | OBS_SOURCE_SRGB,
  478. .get_name = xshm_getname,
  479. .create = xshm_create,
  480. .destroy = xshm_destroy,
  481. .update = xshm_update,
  482. .get_defaults = xshm_defaults_v2,
  483. .get_properties = xshm_properties,
  484. .video_tick = xshm_video_tick,
  485. .video_render = xshm_video_render,
  486. .get_width = xshm_getwidth,
  487. .get_height = xshm_getheight,
  488. .icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE,
  489. };