xshm-input.c 14 KB

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