xshm-input.c 11 KB

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