xshm-input.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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,
  66. GS_BGRA, 1, NULL, GS_DYNAMIC);
  67. }
  68. /**
  69. * Check if the xserver supports all the extensions we need
  70. */
  71. static bool xshm_check_extensions(xcb_connection_t *xcb)
  72. {
  73. bool ok = true;
  74. if (!xcb_get_extension_data(xcb, &xcb_shm_id)->present) {
  75. blog(LOG_ERROR, "Missing SHM extension !");
  76. ok = false;
  77. }
  78. if (!xcb_get_extension_data(xcb, &xcb_xinerama_id)->present)
  79. blog(LOG_INFO, "Missing Xinerama extension !");
  80. if (!xcb_get_extension_data(xcb, &xcb_randr_id)->present)
  81. blog(LOG_INFO, "Missing Randr extension !");
  82. return ok;
  83. }
  84. /**
  85. * Update the capture
  86. *
  87. * @return < 0 on error, 0 when size is unchanged, > 1 on size change
  88. */
  89. static int_fast32_t xshm_update_geometry(struct xshm_data *data)
  90. {
  91. int_fast32_t prev_width = data->adj_width;
  92. int_fast32_t prev_height = data->adj_height;
  93. if (data->use_randr) {
  94. if (randr_screen_geo(data->xcb, data->screen_id, &data->x_org,
  95. &data->y_org, &data->width, &data->height,
  96. &data->xcb_screen, NULL) < 0) {
  97. return -1;
  98. }
  99. } else if (data->use_xinerama) {
  100. if (xinerama_screen_geo(data->xcb, data->screen_id,
  101. &data->x_org, &data->y_org,
  102. &data->width, &data->height) < 0) {
  103. return -1;
  104. }
  105. data->xcb_screen = xcb_get_screen(data->xcb, 0);
  106. } else {
  107. data->x_org = 0;
  108. data->y_org = 0;
  109. if (x11_screen_geo(data->xcb, data->screen_id, &data->width,
  110. &data->height) < 0) {
  111. return -1;
  112. }
  113. data->xcb_screen = xcb_get_screen(data->xcb, data->screen_id);
  114. }
  115. if (!data->width || !data->height) {
  116. blog(LOG_ERROR, "Failed to get geometry");
  117. return -1;
  118. }
  119. data->adj_y_org = data->y_org;
  120. data->adj_x_org = data->x_org;
  121. data->adj_height = data->height;
  122. data->adj_width = data->width;
  123. if (data->cut_top != 0) {
  124. if (data->y_org > 0)
  125. data->adj_y_org = data->y_org + data->cut_top;
  126. else
  127. data->adj_y_org = data->cut_top;
  128. data->adj_height = data->adj_height - data->cut_top;
  129. }
  130. if (data->cut_left != 0) {
  131. if (data->x_org > 0)
  132. data->adj_x_org = data->x_org + data->cut_left;
  133. else
  134. data->adj_x_org = data->cut_left;
  135. data->adj_width = data->adj_width - data->cut_left;
  136. }
  137. if (data->cut_right != 0)
  138. data->adj_width = data->adj_width - data->cut_right;
  139. if (data->cut_bot != 0)
  140. data->adj_height = data->adj_height - data->cut_bot;
  141. blog(LOG_INFO,
  142. "Geometry %" PRIdFAST32 "x%" PRIdFAST32 " @ %" PRIdFAST32
  143. ",%" PRIdFAST32,
  144. data->width, data->height, data->x_org, data->y_org);
  145. if (prev_width == data->adj_width && prev_height == data->adj_height)
  146. return 0;
  147. return 1;
  148. }
  149. /**
  150. * Returns the name of the plugin
  151. */
  152. static const char *xshm_getname(void *unused)
  153. {
  154. UNUSED_PARAMETER(unused);
  155. return obs_module_text("X11SharedMemoryDisplayInput");
  156. }
  157. /**
  158. * Stop the capture
  159. */
  160. static void xshm_capture_stop(struct xshm_data *data)
  161. {
  162. obs_enter_graphics();
  163. if (data->texture) {
  164. gs_texture_destroy(data->texture);
  165. data->texture = NULL;
  166. }
  167. if (data->cursor) {
  168. xcb_xcursor_destroy(data->cursor);
  169. data->cursor = NULL;
  170. }
  171. obs_leave_graphics();
  172. if (data->xshm) {
  173. xshm_xcb_detach(data->xshm);
  174. data->xshm = NULL;
  175. }
  176. if (data->xcb) {
  177. xcb_disconnect(data->xcb);
  178. data->xcb = NULL;
  179. }
  180. if (data->server) {
  181. bfree(data->server);
  182. data->server = NULL;
  183. }
  184. }
  185. /**
  186. * Start the capture
  187. */
  188. static void xshm_capture_start(struct xshm_data *data)
  189. {
  190. const char *server = (data->advanced && *data->server) ? data->server
  191. : NULL;
  192. if (data->screen_id == -1) {
  193. if (data->texture) {
  194. gs_texture_destroy(data->texture);
  195. data->texture = NULL;
  196. }
  197. data->adj_width = 0;
  198. data->adj_height = 0;
  199. return;
  200. }
  201. data->xcb = xcb_connect(server, NULL);
  202. if (!data->xcb || xcb_connection_has_error(data->xcb)) {
  203. blog(LOG_ERROR, "Unable to open X display !");
  204. goto fail;
  205. }
  206. if (!xshm_check_extensions(data->xcb))
  207. goto fail;
  208. data->use_randr = randr_is_active(data->xcb) ? true : false;
  209. data->use_xinerama = xinerama_is_active(data->xcb) ? true : false;
  210. if (xshm_update_geometry(data) < 0) {
  211. blog(LOG_ERROR, "failed to update geometry !");
  212. goto fail;
  213. }
  214. data->xshm =
  215. xshm_xcb_attach(data->xcb, data->adj_width, data->adj_height);
  216. if (!data->xshm) {
  217. blog(LOG_ERROR, "failed to attach shm !");
  218. goto fail;
  219. }
  220. data->cursor = xcb_xcursor_init(data->xcb);
  221. xcb_xcursor_offset(data->cursor, data->adj_x_org, data->adj_y_org);
  222. obs_enter_graphics();
  223. xshm_resize_texture(data);
  224. obs_leave_graphics();
  225. return;
  226. fail:
  227. xshm_capture_stop(data);
  228. }
  229. /**
  230. * Update the capture with changed settings
  231. */
  232. static void xshm_update(void *vptr, obs_data_t *settings)
  233. {
  234. XSHM_DATA(vptr);
  235. xshm_capture_stop(data);
  236. data->screen_id = obs_data_get_int(settings, "screen");
  237. data->show_cursor = obs_data_get_bool(settings, "show_cursor");
  238. data->advanced = obs_data_get_bool(settings, "advanced");
  239. data->server = bstrdup(obs_data_get_string(settings, "server"));
  240. data->cut_top = obs_data_get_int(settings, "cut_top");
  241. data->cut_left = obs_data_get_int(settings, "cut_left");
  242. data->cut_right = obs_data_get_int(settings, "cut_right");
  243. data->cut_bot = obs_data_get_int(settings, "cut_bot");
  244. xshm_capture_start(data);
  245. }
  246. /**
  247. * Get the default settings for the capture
  248. */
  249. static void xshm_defaults(obs_data_t *defaults, int ver)
  250. {
  251. obs_data_set_default_int(defaults, "screen",
  252. ver == 1 ? 0 : INVALID_DISPLAY);
  253. obs_data_set_default_bool(defaults, "show_cursor", true);
  254. obs_data_set_default_bool(defaults, "advanced", false);
  255. obs_data_set_default_int(defaults, "cut_top", 0);
  256. obs_data_set_default_int(defaults, "cut_left", 0);
  257. obs_data_set_default_int(defaults, "cut_right", 0);
  258. obs_data_set_default_int(defaults, "cut_bot", 0);
  259. }
  260. static void xshm_defaults_v1(obs_data_t *defaults)
  261. {
  262. xshm_defaults(defaults, 1);
  263. }
  264. static void xshm_defaults_v2(obs_data_t *defaults)
  265. {
  266. xshm_defaults(defaults, 2);
  267. }
  268. /**
  269. * Toggle visibility of advanced settings
  270. */
  271. static bool xshm_toggle_advanced(obs_properties_t *props, obs_property_t *p,
  272. obs_data_t *settings)
  273. {
  274. UNUSED_PARAMETER(p);
  275. const bool visible = obs_data_get_bool(settings, "advanced");
  276. obs_property_t *server = obs_properties_get(props, "server");
  277. obs_property_set_visible(server, visible);
  278. /* trigger server changed callback so the screen list is refreshed */
  279. obs_property_modified(server, settings);
  280. return true;
  281. }
  282. /**
  283. * The x server was changed
  284. */
  285. static bool xshm_server_changed(obs_properties_t *props, obs_property_t *p,
  286. obs_data_t *settings)
  287. {
  288. UNUSED_PARAMETER(p);
  289. bool advanced = obs_data_get_bool(settings, "advanced");
  290. int_fast32_t old_screen = obs_data_get_int(settings, "screen");
  291. const char *server = obs_data_get_string(settings, "server");
  292. obs_property_t *screens = obs_properties_get(props, "screen");
  293. /* we want a real NULL here in case there is no string here */
  294. server = (advanced && *server) ? server : NULL;
  295. obs_property_list_clear(screens);
  296. if (old_screen == INVALID_DISPLAY) {
  297. obs_property_list_add_int(screens,
  298. obs_module_text("SelectADisplay"),
  299. INVALID_DISPLAY);
  300. obs_property_list_item_disable(screens, 0, true);
  301. }
  302. xcb_connection_t *xcb = xcb_connect(server, NULL);
  303. if (!xcb || xcb_connection_has_error(xcb)) {
  304. obs_property_set_enabled(screens, false);
  305. return true;
  306. }
  307. struct dstr screen_info;
  308. dstr_init(&screen_info);
  309. bool randr = randr_is_active(xcb);
  310. bool xinerama = xinerama_is_active(xcb);
  311. int_fast32_t count =
  312. randr ? randr_screen_count(xcb)
  313. : (xinerama ? xinerama_screen_count(xcb)
  314. : xcb_setup_roots_length(xcb_get_setup(xcb)));
  315. for (int_fast32_t i = 0; i < count; ++i) {
  316. char *name;
  317. char name_tmp[20];
  318. int_fast32_t x, y, w, h;
  319. x = y = w = h = 0;
  320. name = NULL;
  321. if (randr)
  322. randr_screen_geo(xcb, i, &x, &y, &w, &h, NULL, &name);
  323. else if (xinerama)
  324. xinerama_screen_geo(xcb, i, &x, &y, &w, &h);
  325. else
  326. x11_screen_geo(xcb, i, &w, &h);
  327. if (name == NULL) {
  328. int ret = snprintf(name_tmp, sizeof(name_tmp),
  329. "%" PRIuFAST32, i);
  330. if (ret >= (int)sizeof(name_tmp))
  331. blog(LOG_DEBUG,
  332. "linux-capture: A format truncation may have occurred."
  333. " This can be ignored since it is quite improbable.");
  334. name = name_tmp;
  335. }
  336. dstr_printf(&screen_info,
  337. "%s (%" PRIuFAST32 "x%" PRIuFAST32 " @ %" PRIuFAST32
  338. ",%" PRIuFAST32 ")",
  339. name, w, h, x, y);
  340. if (name != name_tmp)
  341. free(name);
  342. if (h > 0 && w > 0)
  343. obs_property_list_add_int(screens, screen_info.array,
  344. i);
  345. }
  346. /* handle missing screen */
  347. if (old_screen + 1 > count) {
  348. dstr_printf(&screen_info, "Display %" PRIuFAST32 " (not found)",
  349. old_screen);
  350. size_t index = obs_property_list_add_int(
  351. screens, screen_info.array, old_screen);
  352. obs_property_list_item_disable(screens, index, true);
  353. }
  354. dstr_free(&screen_info);
  355. xcb_disconnect(xcb);
  356. obs_property_set_enabled(screens, true);
  357. return true;
  358. }
  359. /**
  360. * Get the properties for the capture
  361. */
  362. static obs_properties_t *xshm_properties(void *vptr)
  363. {
  364. XSHM_DATA(vptr);
  365. obs_properties_t *props = obs_properties_create();
  366. obs_property_t *prop;
  367. obs_properties_add_list(props, "screen", obs_module_text("Display"),
  368. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  369. obs_properties_add_bool(props, "show_cursor",
  370. obs_module_text("CaptureCursor"));
  371. obs_property_t *advanced = obs_properties_add_bool(
  372. props, "advanced", obs_module_text("AdvancedSettings"));
  373. prop = obs_properties_add_int(
  374. props, "cut_top", obs_module_text("CropTop"), -4096, 4096, 1);
  375. obs_property_int_set_suffix(prop, " px");
  376. prop = obs_properties_add_int(
  377. props, "cut_left", obs_module_text("CropLeft"), -4096, 4096, 1);
  378. obs_property_int_set_suffix(prop, " px");
  379. prop = obs_properties_add_int(props, "cut_right",
  380. obs_module_text("CropRight"), 0, 4096, 1);
  381. obs_property_int_set_suffix(prop, " px");
  382. prop = obs_properties_add_int(
  383. props, "cut_bot", obs_module_text("CropBottom"), 0, 4096, 1);
  384. obs_property_int_set_suffix(prop, " px");
  385. obs_property_t *server = obs_properties_add_text(
  386. props, "server", obs_module_text("XServer"), OBS_TEXT_DEFAULT);
  387. obs_property_set_modified_callback(advanced, xshm_toggle_advanced);
  388. obs_property_set_modified_callback(server, xshm_server_changed);
  389. /* trigger server callback to get screen count ... */
  390. obs_data_t *settings = obs_source_get_settings(data->source);
  391. obs_property_modified(server, settings);
  392. obs_data_release(settings);
  393. return props;
  394. }
  395. /**
  396. * Destroy the capture
  397. */
  398. static void xshm_destroy(void *vptr)
  399. {
  400. XSHM_DATA(vptr);
  401. if (!data)
  402. return;
  403. xshm_capture_stop(data);
  404. bfree(data);
  405. }
  406. /**
  407. * Create the capture
  408. */
  409. static void *xshm_create(obs_data_t *settings, obs_source_t *source)
  410. {
  411. struct xshm_data *data = bzalloc(sizeof(struct xshm_data));
  412. data->source = source;
  413. xshm_update(data, settings);
  414. return data;
  415. }
  416. /**
  417. * Prepare the capture data
  418. */
  419. static void xshm_video_tick(void *vptr, float seconds)
  420. {
  421. UNUSED_PARAMETER(seconds);
  422. XSHM_DATA(vptr);
  423. if (!data->texture)
  424. return;
  425. if (!obs_source_showing(data->source))
  426. return;
  427. xcb_shm_get_image_cookie_t img_c;
  428. xcb_shm_get_image_reply_t *img_r;
  429. img_c = xcb_shm_get_image_unchecked(data->xcb, data->xcb_screen->root,
  430. data->adj_x_org, data->adj_y_org,
  431. data->adj_width, data->adj_height,
  432. ~0, XCB_IMAGE_FORMAT_Z_PIXMAP,
  433. data->xshm->seg, 0);
  434. img_r = xcb_shm_get_image_reply(data->xcb, img_c, NULL);
  435. if (!img_r)
  436. goto exit;
  437. obs_enter_graphics();
  438. gs_texture_set_image(data->texture, (void *)data->xshm->data,
  439. data->adj_width * 4, false);
  440. xcb_xcursor_update(data->xcb, data->cursor);
  441. obs_leave_graphics();
  442. exit:
  443. free(img_r);
  444. }
  445. /**
  446. * Render the capture data
  447. */
  448. static void xshm_video_render(void *vptr, gs_effect_t *effect)
  449. {
  450. XSHM_DATA(vptr);
  451. effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  452. if (!data->texture)
  453. return;
  454. const bool linear_srgb = gs_get_linear_srgb();
  455. const bool previous = gs_framebuffer_srgb_enabled();
  456. gs_enable_framebuffer_srgb(linear_srgb);
  457. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  458. if (linear_srgb)
  459. gs_effect_set_texture_srgb(image, data->texture);
  460. else
  461. gs_effect_set_texture(image, data->texture);
  462. while (gs_effect_loop(effect, "Draw")) {
  463. gs_draw_sprite(data->texture, 0, 0, 0);
  464. }
  465. gs_enable_framebuffer_srgb(previous);
  466. if (data->show_cursor) {
  467. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  468. while (gs_effect_loop(effect, "Draw")) {
  469. xcb_xcursor_render(data->cursor);
  470. }
  471. }
  472. }
  473. /**
  474. * Width of the captured data
  475. */
  476. static uint32_t xshm_getwidth(void *vptr)
  477. {
  478. XSHM_DATA(vptr);
  479. return data->adj_width;
  480. }
  481. /**
  482. * Height of the captured data
  483. */
  484. static uint32_t xshm_getheight(void *vptr)
  485. {
  486. XSHM_DATA(vptr);
  487. return data->adj_height;
  488. }
  489. struct obs_source_info xshm_input = {
  490. .id = "xshm_input",
  491. .type = OBS_SOURCE_TYPE_INPUT,
  492. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  493. OBS_SOURCE_DO_NOT_DUPLICATE | OBS_SOURCE_SRGB |
  494. OBS_SOURCE_CAP_OBSOLETE,
  495. .get_name = xshm_getname,
  496. .create = xshm_create,
  497. .destroy = xshm_destroy,
  498. .update = xshm_update,
  499. .get_defaults = xshm_defaults_v1,
  500. .get_properties = xshm_properties,
  501. .video_tick = xshm_video_tick,
  502. .video_render = xshm_video_render,
  503. .get_width = xshm_getwidth,
  504. .get_height = xshm_getheight,
  505. .icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE,
  506. };
  507. struct obs_source_info xshm_input_v2 = {
  508. .id = "xshm_input_v2",
  509. .type = OBS_SOURCE_TYPE_INPUT,
  510. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  511. OBS_SOURCE_DO_NOT_DUPLICATE | OBS_SOURCE_SRGB,
  512. .get_name = xshm_getname,
  513. .create = xshm_create,
  514. .destroy = xshm_destroy,
  515. .update = xshm_update,
  516. .get_defaults = xshm_defaults_v2,
  517. .get_properties = xshm_properties,
  518. .video_tick = xshm_video_tick,
  519. .video_render = xshm_video_render,
  520. .get_width = xshm_getwidth,
  521. .get_height = xshm_getheight,
  522. .icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE,
  523. };