xshm-input.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. sprintf(name_tmp, "%" PRIuFAST32, i);
  304. name = name_tmp;
  305. }
  306. dstr_printf(&screen_info,
  307. "Screen %s (%" PRIuFAST32 "x%" PRIuFAST32
  308. " @ %" PRIuFAST32 ",%" PRIuFAST32 ")",
  309. name, w, h, x, y);
  310. if (name != name_tmp)
  311. free(name);
  312. if (h > 0 && w > 0)
  313. obs_property_list_add_int(screens, screen_info.array,
  314. i);
  315. }
  316. /* handle missing screen */
  317. if (old_screen + 1 > count) {
  318. dstr_printf(&screen_info, "Screen %" PRIuFAST32 " (not found)",
  319. old_screen);
  320. size_t index = obs_property_list_add_int(
  321. screens, screen_info.array, old_screen);
  322. obs_property_list_item_disable(screens, index, true);
  323. }
  324. dstr_free(&screen_info);
  325. xcb_disconnect(xcb);
  326. obs_property_set_enabled(screens, true);
  327. return true;
  328. }
  329. /**
  330. * Get the properties for the capture
  331. */
  332. static obs_properties_t *xshm_properties(void *vptr)
  333. {
  334. XSHM_DATA(vptr);
  335. obs_properties_t *props = obs_properties_create();
  336. obs_property_t *prop;
  337. obs_properties_add_list(props, "screen", obs_module_text("Screen"),
  338. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  339. obs_properties_add_bool(props, "show_cursor",
  340. obs_module_text("CaptureCursor"));
  341. obs_property_t *advanced = obs_properties_add_bool(
  342. props, "advanced", obs_module_text("AdvancedSettings"));
  343. prop = obs_properties_add_int(
  344. props, "cut_top", obs_module_text("CropTop"), -4096, 4096, 1);
  345. obs_property_int_set_suffix(prop, " px");
  346. prop = obs_properties_add_int(
  347. props, "cut_left", obs_module_text("CropLeft"), -4096, 4096, 1);
  348. obs_property_int_set_suffix(prop, " px");
  349. prop = obs_properties_add_int(props, "cut_right",
  350. obs_module_text("CropRight"), 0, 4096, 1);
  351. obs_property_int_set_suffix(prop, " px");
  352. prop = obs_properties_add_int(
  353. props, "cut_bot", obs_module_text("CropBottom"), 0, 4096, 1);
  354. obs_property_int_set_suffix(prop, " px");
  355. obs_property_t *server = obs_properties_add_text(
  356. props, "server", obs_module_text("XServer"), OBS_TEXT_DEFAULT);
  357. obs_property_set_modified_callback(advanced, xshm_toggle_advanced);
  358. obs_property_set_modified_callback(server, xshm_server_changed);
  359. /* trigger server callback to get screen count ... */
  360. obs_data_t *settings = obs_source_get_settings(data->source);
  361. obs_property_modified(server, settings);
  362. obs_data_release(settings);
  363. return props;
  364. }
  365. /**
  366. * Destroy the capture
  367. */
  368. static void xshm_destroy(void *vptr)
  369. {
  370. XSHM_DATA(vptr);
  371. if (!data)
  372. return;
  373. xshm_capture_stop(data);
  374. bfree(data);
  375. }
  376. /**
  377. * Create the capture
  378. */
  379. static void *xshm_create(obs_data_t *settings, obs_source_t *source)
  380. {
  381. struct xshm_data *data = bzalloc(sizeof(struct xshm_data));
  382. data->source = source;
  383. xshm_update(data, settings);
  384. return data;
  385. }
  386. /**
  387. * Prepare the capture data
  388. */
  389. static void xshm_video_tick(void *vptr, float seconds)
  390. {
  391. UNUSED_PARAMETER(seconds);
  392. XSHM_DATA(vptr);
  393. if (!data->texture)
  394. return;
  395. if (!obs_source_showing(data->source))
  396. return;
  397. xcb_shm_get_image_cookie_t img_c;
  398. xcb_shm_get_image_reply_t *img_r;
  399. img_c = xcb_shm_get_image_unchecked(data->xcb, data->xcb_screen->root,
  400. data->adj_x_org, data->adj_y_org,
  401. data->adj_width, data->adj_height,
  402. ~0, XCB_IMAGE_FORMAT_Z_PIXMAP,
  403. data->xshm->seg, 0);
  404. img_r = xcb_shm_get_image_reply(data->xcb, img_c, NULL);
  405. if (!img_r)
  406. goto exit;
  407. obs_enter_graphics();
  408. gs_texture_set_image(data->texture, (void *)data->xshm->data,
  409. data->adj_width * 4, false);
  410. xcb_xcursor_update(data->xcb, data->cursor);
  411. obs_leave_graphics();
  412. exit:
  413. free(img_r);
  414. }
  415. /**
  416. * Render the capture data
  417. */
  418. static void xshm_video_render(void *vptr, gs_effect_t *effect)
  419. {
  420. XSHM_DATA(vptr);
  421. effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  422. if (!data->texture)
  423. return;
  424. const bool linear_srgb = gs_get_linear_srgb();
  425. const bool previous = gs_framebuffer_srgb_enabled();
  426. gs_enable_framebuffer_srgb(linear_srgb);
  427. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  428. if (linear_srgb)
  429. gs_effect_set_texture_srgb(image, data->texture);
  430. else
  431. gs_effect_set_texture(image, data->texture);
  432. while (gs_effect_loop(effect, "Draw")) {
  433. gs_draw_sprite(data->texture, 0, 0, 0);
  434. }
  435. gs_enable_framebuffer_srgb(previous);
  436. if (data->show_cursor) {
  437. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  438. while (gs_effect_loop(effect, "Draw")) {
  439. xcb_xcursor_render(data->cursor);
  440. }
  441. }
  442. }
  443. /**
  444. * Width of the captured data
  445. */
  446. static uint32_t xshm_getwidth(void *vptr)
  447. {
  448. XSHM_DATA(vptr);
  449. return data->adj_width;
  450. }
  451. /**
  452. * Height of the captured data
  453. */
  454. static uint32_t xshm_getheight(void *vptr)
  455. {
  456. XSHM_DATA(vptr);
  457. return data->adj_height;
  458. }
  459. struct obs_source_info xshm_input = {
  460. .id = "xshm_input",
  461. .type = OBS_SOURCE_TYPE_INPUT,
  462. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  463. OBS_SOURCE_DO_NOT_DUPLICATE | OBS_SOURCE_SRGB,
  464. .get_name = xshm_getname,
  465. .create = xshm_create,
  466. .destroy = xshm_destroy,
  467. .update = xshm_update,
  468. .get_defaults = xshm_defaults,
  469. .get_properties = xshm_properties,
  470. .video_tick = xshm_video_tick,
  471. .video_render = xshm_video_render,
  472. .get_width = xshm_getwidth,
  473. .get_height = xshm_getheight,
  474. .icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE,
  475. };