xshm-input.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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/shm.h>
  18. #include <xcb/xfixes.h>
  19. #include <xcb/xinerama.h>
  20. #include <obs-module.h>
  21. #include <util/dstr.h>
  22. #include "xcursor-xcb.h"
  23. #include "xhelpers.h"
  24. #define XSHM_DATA(voidptr) struct xshm_data *data = voidptr;
  25. #define blog(level, msg, ...) blog(level, "xshm-input: " msg, ##__VA_ARGS__)
  26. struct xshm_data {
  27. obs_source_t *source;
  28. xcb_connection_t *xcb;
  29. xcb_screen_t *xcb_screen;
  30. xcb_shm_t *xshm;
  31. xcb_xcursor_t *cursor;
  32. char *server;
  33. uint_fast32_t screen_id;
  34. int_fast32_t x_org;
  35. int_fast32_t y_org;
  36. int_fast32_t width;
  37. int_fast32_t height;
  38. gs_texture_t *texture;
  39. bool show_cursor;
  40. bool use_xinerama;
  41. bool advanced;
  42. };
  43. /**
  44. * Resize the texture
  45. *
  46. * This will automatically create the texture if it does not exist
  47. *
  48. * @note requires to be called within the obs graphics context
  49. */
  50. static inline void xshm_resize_texture(struct xshm_data *data)
  51. {
  52. if (data->texture)
  53. gs_texture_destroy(data->texture);
  54. data->texture = gs_texture_create(data->width, data->height,
  55. GS_BGRA, 1, NULL, GS_DYNAMIC);
  56. }
  57. /**
  58. * Check if the xserver supports all the extensions we need
  59. */
  60. static bool xshm_check_extensions(xcb_connection_t *xcb)
  61. {
  62. bool ok = true;
  63. if (!xcb_get_extension_data(xcb, &xcb_shm_id)->present) {
  64. blog(LOG_ERROR, "Missing SHM extension !");
  65. ok = false;
  66. }
  67. if (!xcb_get_extension_data(xcb, &xcb_xinerama_id)->present)
  68. blog(LOG_INFO, "Missing Xinerama extension !");
  69. return ok;
  70. }
  71. /**
  72. * Update the capture
  73. *
  74. * @return < 0 on error, 0 when size is unchanged, > 1 on size change
  75. */
  76. static int_fast32_t xshm_update_geometry(struct xshm_data *data)
  77. {
  78. int_fast32_t old_width = data->width;
  79. int_fast32_t old_height = data->height;
  80. if (data->use_xinerama) {
  81. if (xinerama_screen_geo(data->xcb, data->screen_id,
  82. &data->x_org, &data->y_org,
  83. &data->width, &data->height) < 0) {
  84. return -1;
  85. }
  86. data->xcb_screen = xcb_get_screen(data->xcb, 0);
  87. }
  88. else {
  89. data->x_org = 0;
  90. data->y_org = 0;
  91. if (x11_screen_geo(data->xcb, data->screen_id,
  92. &data->width, &data->height) < 0) {
  93. return -1;
  94. }
  95. data->xcb_screen = xcb_get_screen(data->xcb, data->screen_id);
  96. }
  97. if (!data->width || !data->height) {
  98. blog(LOG_ERROR, "Failed to get geometry");
  99. return -1;
  100. }
  101. blog(LOG_INFO, "Geometry %"PRIdFAST32"x%"PRIdFAST32
  102. " @ %"PRIdFAST32",%"PRIdFAST32,
  103. data->width, data->height, data->x_org, data->y_org);
  104. if (old_width == data->width && old_height == data->height)
  105. return 0;
  106. return 1;
  107. }
  108. /**
  109. * Returns the name of the plugin
  110. */
  111. static const char* xshm_getname(void)
  112. {
  113. return obs_module_text("X11SharedMemoryScreenInput");
  114. }
  115. /**
  116. * Stop the capture
  117. */
  118. static void xshm_capture_stop(struct xshm_data *data)
  119. {
  120. obs_enter_graphics();
  121. if (data->texture) {
  122. gs_texture_destroy(data->texture);
  123. data->texture = NULL;
  124. }
  125. if (data->cursor) {
  126. xcb_xcursor_destroy(data->cursor);
  127. data->cursor = NULL;
  128. }
  129. obs_leave_graphics();
  130. if (data->xshm) {
  131. xshm_xcb_detach(data->xshm);
  132. data->xshm = NULL;
  133. }
  134. if (data->xcb) {
  135. xcb_disconnect(data->xcb);
  136. data->xcb = NULL;
  137. }
  138. if (data->server) {
  139. bfree(data->server);
  140. data->server = NULL;
  141. }
  142. }
  143. /**
  144. * Start the capture
  145. */
  146. static void xshm_capture_start(struct xshm_data *data)
  147. {
  148. const char *server = (data->advanced && *data->server)
  149. ? data->server : NULL;
  150. data->xcb = xcb_connect(server, NULL);
  151. if (!data->xcb || xcb_connection_has_error(data->xcb)) {
  152. blog(LOG_ERROR, "Unable to open X display !");
  153. goto fail;
  154. }
  155. if (!xshm_check_extensions(data->xcb))
  156. goto fail;
  157. data->use_xinerama = xinerama_is_active(data->xcb) ? true : false;
  158. if (xshm_update_geometry(data) < 0) {
  159. blog(LOG_ERROR, "failed to update geometry !");
  160. goto fail;
  161. }
  162. data->xshm = xshm_xcb_attach(data->xcb, data->width, data->height);
  163. if (!data->xshm) {
  164. blog(LOG_ERROR, "failed to attach shm !");
  165. goto fail;
  166. }
  167. data->cursor = xcb_xcursor_init(data->xcb);
  168. xcb_xcursor_offset(data->cursor, data->x_org, data->y_org);
  169. obs_enter_graphics();
  170. xshm_resize_texture(data);
  171. obs_leave_graphics();
  172. return;
  173. fail:
  174. xshm_capture_stop(data);
  175. }
  176. /**
  177. * Update the capture with changed settings
  178. */
  179. static void xshm_update(void *vptr, obs_data_t *settings)
  180. {
  181. XSHM_DATA(vptr);
  182. xshm_capture_stop(data);
  183. data->screen_id = obs_data_get_int(settings, "screen");
  184. data->show_cursor = obs_data_get_bool(settings, "show_cursor");
  185. data->advanced = obs_data_get_bool(settings, "advanced");
  186. data->server = bstrdup(obs_data_get_string(settings, "server"));
  187. xshm_capture_start(data);
  188. }
  189. /**
  190. * Get the default settings for the capture
  191. */
  192. static void xshm_defaults(obs_data_t *defaults)
  193. {
  194. obs_data_set_default_int(defaults, "screen", 0);
  195. obs_data_set_default_bool(defaults, "show_cursor", true);
  196. obs_data_set_default_bool(defaults, "advanced", false);
  197. }
  198. /**
  199. * Toggle visibility of advanced settings
  200. */
  201. static bool xshm_toggle_advanced(obs_properties_t *props,
  202. obs_property_t *p, obs_data_t *settings)
  203. {
  204. UNUSED_PARAMETER(p);
  205. const bool visible = obs_data_get_bool(settings, "advanced");
  206. obs_property_t *server = obs_properties_get(props, "server");
  207. obs_property_set_visible(server, visible);
  208. /* trigger server changed callback so the screen list is refreshed */
  209. obs_property_modified(server, settings);
  210. return true;
  211. }
  212. /**
  213. * The x server was changed
  214. */
  215. static bool xshm_server_changed(obs_properties_t *props,
  216. obs_property_t *p, obs_data_t *settings)
  217. {
  218. UNUSED_PARAMETER(p);
  219. bool advanced = obs_data_get_bool(settings, "advanced");
  220. int_fast32_t old_screen = obs_data_get_int(settings, "screen");
  221. const char *server = obs_data_get_string(settings, "server");
  222. obs_property_t *screens = obs_properties_get(props, "screen");
  223. /* we want a real NULL here in case there is no string here */
  224. server = (advanced && *server) ? server : NULL;
  225. obs_property_list_clear(screens);
  226. xcb_connection_t *xcb = xcb_connect(server, NULL);
  227. if (!xcb || xcb_connection_has_error(xcb)) {
  228. obs_property_set_enabled(screens, false);
  229. return true;
  230. }
  231. struct dstr screen_info;
  232. dstr_init(&screen_info);
  233. bool xinerama = xinerama_is_active(xcb);
  234. int_fast32_t count = (xinerama) ?
  235. xinerama_screen_count(xcb) :
  236. xcb_setup_roots_length(xcb_get_setup(xcb));
  237. for (int_fast32_t i = 0; i < count; ++i) {
  238. int_fast32_t x, y, w, h;
  239. x = y = w = h = 0;
  240. if (xinerama)
  241. xinerama_screen_geo(xcb, i, &x, &y, &w, &h);
  242. else
  243. x11_screen_geo(xcb, i, &w, &h);
  244. dstr_printf(&screen_info, "Screen %"PRIuFAST32" (%"PRIuFAST32
  245. "x%"PRIuFAST32" @ %"PRIuFAST32
  246. ",%"PRIuFAST32")", i, w, h, x, y);
  247. obs_property_list_add_int(screens, screen_info.array, i);
  248. }
  249. /* handle missing screen */
  250. if (old_screen + 1 > count) {
  251. dstr_printf(&screen_info, "Screen %"PRIuFAST32" (not found)",
  252. old_screen);
  253. size_t index = obs_property_list_add_int(screens,
  254. screen_info.array, old_screen);
  255. obs_property_list_item_disable(screens, index, true);
  256. }
  257. dstr_free(&screen_info);
  258. xcb_disconnect(xcb);
  259. obs_property_set_enabled(screens, true);
  260. return true;
  261. }
  262. /**
  263. * Get the properties for the capture
  264. */
  265. static obs_properties_t *xshm_properties(void *vptr)
  266. {
  267. XSHM_DATA(vptr);
  268. obs_properties_t *props = obs_properties_create();
  269. obs_properties_add_list(props, "screen", obs_module_text("Screen"),
  270. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  271. obs_properties_add_bool(props, "show_cursor",
  272. obs_module_text("CaptureCursor"));
  273. obs_property_t *advanced = obs_properties_add_bool(props, "advanced",
  274. obs_module_text("AdvancedSettings"));
  275. obs_property_t *server = obs_properties_add_text(props, "server",
  276. obs_module_text("XServer"), OBS_TEXT_DEFAULT);
  277. obs_property_set_modified_callback(advanced, xshm_toggle_advanced);
  278. obs_property_set_modified_callback(server, xshm_server_changed);
  279. /* trigger server callback to get screen count ... */
  280. obs_data_t *settings = obs_source_get_settings(data->source);
  281. obs_property_modified(server, settings);
  282. obs_data_release(settings);
  283. return props;
  284. }
  285. /**
  286. * Destroy the capture
  287. */
  288. static void xshm_destroy(void *vptr)
  289. {
  290. XSHM_DATA(vptr);
  291. if (!data)
  292. return;
  293. xshm_capture_stop(data);
  294. bfree(data);
  295. }
  296. /**
  297. * Create the capture
  298. */
  299. static void *xshm_create(obs_data_t *settings, obs_source_t *source)
  300. {
  301. struct xshm_data *data = bzalloc(sizeof(struct xshm_data));
  302. data->source = source;
  303. xshm_update(data, settings);
  304. return data;
  305. }
  306. /**
  307. * Prepare the capture data
  308. */
  309. static void xshm_video_tick(void *vptr, float seconds)
  310. {
  311. UNUSED_PARAMETER(seconds);
  312. XSHM_DATA(vptr);
  313. if (!data->texture)
  314. return;
  315. if (!obs_source_showing(data->source))
  316. return;
  317. xcb_shm_get_image_cookie_t img_c;
  318. xcb_shm_get_image_reply_t *img_r;
  319. xcb_xfixes_get_cursor_image_cookie_t cur_c;
  320. xcb_xfixes_get_cursor_image_reply_t *cur_r;
  321. img_c = xcb_shm_get_image_unchecked(data->xcb, data->xcb_screen->root,
  322. data->x_org, data->y_org, data->width, data->height,
  323. ~0, XCB_IMAGE_FORMAT_Z_PIXMAP, data->xshm->seg, 0);
  324. cur_c = xcb_xfixes_get_cursor_image_unchecked(data->xcb);
  325. img_r = xcb_shm_get_image_reply(data->xcb, img_c, NULL);
  326. cur_r = xcb_xfixes_get_cursor_image_reply(data->xcb, cur_c, NULL);
  327. if (!img_r)
  328. goto exit;
  329. obs_enter_graphics();
  330. gs_texture_set_image(data->texture, (void *) data->xshm->data,
  331. data->width * 4, false);
  332. xcb_xcursor_update(data->cursor, cur_r);
  333. obs_leave_graphics();
  334. exit:
  335. free(img_r);
  336. free(cur_r);
  337. }
  338. /**
  339. * Render the capture data
  340. */
  341. static void xshm_video_render(void *vptr, gs_effect_t *effect)
  342. {
  343. XSHM_DATA(vptr);
  344. if (!data->texture)
  345. return;
  346. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  347. gs_effect_set_texture(image, data->texture);
  348. gs_enable_blending(false);
  349. gs_draw_sprite(data->texture, 0, 0, 0);
  350. if (data->show_cursor)
  351. xcb_xcursor_render(data->cursor);
  352. gs_reset_blend_state();
  353. }
  354. /**
  355. * Width of the captured data
  356. */
  357. static uint32_t xshm_getwidth(void *vptr)
  358. {
  359. XSHM_DATA(vptr);
  360. return data->width;
  361. }
  362. /**
  363. * Height of the captured data
  364. */
  365. static uint32_t xshm_getheight(void *vptr)
  366. {
  367. XSHM_DATA(vptr);
  368. return data->height;
  369. }
  370. struct obs_source_info xshm_input = {
  371. .id = "xshm_input",
  372. .type = OBS_SOURCE_TYPE_INPUT,
  373. .output_flags = OBS_SOURCE_VIDEO,
  374. .get_name = xshm_getname,
  375. .create = xshm_create,
  376. .destroy = xshm_destroy,
  377. .update = xshm_update,
  378. .get_defaults = xshm_defaults,
  379. .get_properties = xshm_properties,
  380. .video_tick = xshm_video_tick,
  381. .video_render = xshm_video_render,
  382. .get_width = xshm_getwidth,
  383. .get_height = xshm_getheight
  384. };