1
0

xshm-input.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 <obs-module.h>
  20. #include <util/dstr.h>
  21. #include "xcursor.h"
  22. #include "xhelpers.h"
  23. #define XSHM_DATA(voidptr) struct xshm_data *data = voidptr;
  24. #define blog(level, msg, ...) blog(level, "xshm-input: " msg, ##__VA_ARGS__)
  25. struct xshm_data {
  26. /** The source object */
  27. obs_source_t *source;
  28. /** Xlib display object */
  29. Display *dpy;
  30. /** Xlib screen object */
  31. Screen *screen;
  32. /** user setting - the server name to capture from */
  33. char *server;
  34. /** user setting - the id of the screen that should be captured */
  35. uint_fast32_t screen_id;
  36. /** root coordinates for the capture */
  37. int_fast32_t x_org, y_org;
  38. /** size for the capture */
  39. int_fast32_t width, height;
  40. /** shared memory management object */
  41. xshm_t *xshm;
  42. /** the texture used to display the capture */
  43. gs_texture_t *texture;
  44. /** cursor object for displaying the server */
  45. xcursor_t *cursor;
  46. /** user setting - if cursor should be displayed */
  47. bool show_cursor;
  48. /** set if xinerama is available and active on the screen */
  49. bool use_xinerama;
  50. /** user setting - if advanced settings should be displayed */
  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->width, data->height,
  65. GS_BGRA, 1, NULL, GS_DYNAMIC);
  66. }
  67. /**
  68. * Update the capture
  69. *
  70. * @return < 0 on error, 0 when size is unchanged, > 1 on size change
  71. */
  72. static int_fast32_t xshm_update_geometry(struct xshm_data *data)
  73. {
  74. int_fast32_t old_width = data->width;
  75. int_fast32_t old_height = data->height;
  76. if (data->use_xinerama) {
  77. if (xinerama_screen_geo(data->dpy, data->screen_id,
  78. &data->x_org, &data->y_org,
  79. &data->width, &data->height) < 0) {
  80. return -1;
  81. }
  82. data->screen = XDefaultScreenOfDisplay(data->dpy);
  83. }
  84. else {
  85. data->x_org = 0;
  86. data->y_org = 0;
  87. if (x11_screen_geo(data->dpy, data->screen_id,
  88. &data->width, &data->height) < 0) {
  89. return -1;
  90. }
  91. data->screen = XScreenOfDisplay(data->dpy, data->screen_id);
  92. }
  93. if (!data->width || !data->height) {
  94. blog(LOG_ERROR, "Failed to get geometry");
  95. return -1;
  96. }
  97. blog(LOG_INFO, "Geometry %"PRIdFAST32"x%"PRIdFAST32
  98. " @ %"PRIdFAST32",%"PRIdFAST32,
  99. data->width, data->height, data->x_org, data->y_org);
  100. if (old_width == data->width && old_height == data->height)
  101. return 0;
  102. return 1;
  103. }
  104. /**
  105. * Returns the name of the plugin
  106. */
  107. static const char* xshm_getname(void)
  108. {
  109. return obs_module_text("X11SharedMemoryScreenInput");
  110. }
  111. /**
  112. * Stop the capture
  113. */
  114. static void xshm_capture_stop(struct xshm_data *data)
  115. {
  116. obs_enter_graphics();
  117. if (data->texture) {
  118. gs_texture_destroy(data->texture);
  119. data->texture = NULL;
  120. }
  121. if (data->cursor) {
  122. xcursor_destroy(data->cursor);
  123. data->cursor = NULL;
  124. }
  125. obs_leave_graphics();
  126. if (data->xshm) {
  127. xshm_detach(data->xshm);
  128. data->xshm = NULL;
  129. }
  130. if (data->dpy) {
  131. XSync(data->dpy, true);
  132. XCloseDisplay(data->dpy);
  133. data->dpy = NULL;
  134. }
  135. if (data->server) {
  136. bfree(data->server);
  137. data->server = NULL;
  138. }
  139. }
  140. /**
  141. * Start the capture
  142. */
  143. static void xshm_capture_start(struct xshm_data *data)
  144. {
  145. const char *server = (data->advanced && *data->server)
  146. ? data->server : NULL;
  147. data->dpy = XOpenDisplay(server);
  148. if (!data->dpy) {
  149. blog(LOG_ERROR, "Unable to open X display !");
  150. goto fail;
  151. }
  152. if (!XShmQueryExtension(data->dpy)) {
  153. blog(LOG_ERROR, "XShm extension not found !");
  154. goto fail;
  155. }
  156. data->use_xinerama = xinerama_is_active(data->dpy) ? true : false;
  157. if (xshm_update_geometry(data) < 0) {
  158. blog(LOG_ERROR, "failed to update geometry !");
  159. goto fail;
  160. }
  161. data->xshm = xshm_attach(data->dpy, data->screen,
  162. data->width, data->height);
  163. if (!data->xshm) {
  164. blog(LOG_ERROR, "failed to attach shm !");
  165. goto fail;
  166. }
  167. obs_enter_graphics();
  168. data->cursor = xcursor_init(data->dpy);
  169. xcursor_offset(data->cursor, data->x_org, data->y_org);
  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. Display *dpy = XOpenDisplay(server);
  227. if (!dpy) {
  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(dpy);
  234. int_fast32_t count = (xinerama) ?
  235. xinerama_screen_count(dpy) : XScreenCount(dpy);
  236. for (int_fast32_t i = 0; i < count; ++i) {
  237. int_fast32_t x, y, w, h;
  238. x = y = w = h = 0;
  239. if (xinerama)
  240. xinerama_screen_geo(dpy, i, &x, &y, &w, &h);
  241. else
  242. x11_screen_geo(dpy, i, &w, &h);
  243. dstr_printf(&screen_info, "Screen %"PRIuFAST32" (%"PRIuFAST32
  244. "x%"PRIuFAST32" @ %"PRIuFAST32
  245. ",%"PRIuFAST32")", i, w, h, x, y);
  246. obs_property_list_add_int(screens, screen_info.array, i);
  247. }
  248. /* handle missing screen */
  249. if (old_screen + 1 > count) {
  250. dstr_printf(&screen_info, "Screen %"PRIuFAST32" (not found)",
  251. old_screen);
  252. size_t index = obs_property_list_add_int(screens,
  253. screen_info.array, old_screen);
  254. obs_property_list_item_disable(screens, index, true);
  255. }
  256. dstr_free(&screen_info);
  257. XCloseDisplay(dpy);
  258. obs_property_set_enabled(screens, true);
  259. return true;
  260. }
  261. /**
  262. * Get the properties for the capture
  263. */
  264. static obs_properties_t *xshm_properties(void *vptr)
  265. {
  266. XSHM_DATA(vptr);
  267. obs_properties_t *props = obs_properties_create();
  268. obs_properties_add_list(props, "screen", obs_module_text("Screen"),
  269. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  270. obs_properties_add_bool(props, "show_cursor",
  271. obs_module_text("CaptureCursor"));
  272. obs_property_t *advanced = obs_properties_add_bool(props, "advanced",
  273. obs_module_text("AdvancedSettings"));
  274. obs_property_t *server = obs_properties_add_text(props, "server",
  275. obs_module_text("XServer"), OBS_TEXT_DEFAULT);
  276. obs_property_set_modified_callback(advanced, xshm_toggle_advanced);
  277. obs_property_set_modified_callback(server, xshm_server_changed);
  278. /* trigger server callback to get screen count ... */
  279. obs_data_t *settings = obs_source_get_settings(data->source);
  280. obs_property_modified(server, settings);
  281. obs_data_release(settings);
  282. return props;
  283. }
  284. /**
  285. * Destroy the capture
  286. */
  287. static void xshm_destroy(void *vptr)
  288. {
  289. XSHM_DATA(vptr);
  290. if (!data)
  291. return;
  292. xshm_capture_stop(data);
  293. bfree(data);
  294. }
  295. /**
  296. * Create the capture
  297. */
  298. static void *xshm_create(obs_data_t *settings, obs_source_t *source)
  299. {
  300. struct xshm_data *data = bzalloc(sizeof(struct xshm_data));
  301. data->source = source;
  302. xshm_update(data, settings);
  303. return data;
  304. }
  305. /**
  306. * Prepare the capture data
  307. */
  308. static void xshm_video_tick(void *vptr, float seconds)
  309. {
  310. UNUSED_PARAMETER(seconds);
  311. XSHM_DATA(vptr);
  312. if (!data->texture)
  313. return;
  314. obs_enter_graphics();
  315. XShmGetImage(data->dpy, XRootWindowOfScreen(data->screen),
  316. data->xshm->image, data->x_org, data->y_org, AllPlanes);
  317. gs_texture_set_image(data->texture, (void *) data->xshm->image->data,
  318. data->width * 4, false);
  319. xcursor_tick(data->cursor);
  320. obs_leave_graphics();
  321. }
  322. /**
  323. * Render the capture data
  324. */
  325. static void xshm_video_render(void *vptr, gs_effect_t *effect)
  326. {
  327. XSHM_DATA(vptr);
  328. if (!data->texture)
  329. return;
  330. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  331. gs_effect_set_texture(image, data->texture);
  332. gs_enable_blending(false);
  333. gs_draw_sprite(data->texture, 0, 0, 0);
  334. if (data->show_cursor)
  335. xcursor_render(data->cursor);
  336. gs_reset_blend_state();
  337. }
  338. /**
  339. * Width of the captured data
  340. */
  341. static uint32_t xshm_getwidth(void *vptr)
  342. {
  343. XSHM_DATA(vptr);
  344. return data->width;
  345. }
  346. /**
  347. * Height of the captured data
  348. */
  349. static uint32_t xshm_getheight(void *vptr)
  350. {
  351. XSHM_DATA(vptr);
  352. return data->height;
  353. }
  354. struct obs_source_info xshm_input = {
  355. .id = "xshm_input",
  356. .type = OBS_SOURCE_TYPE_INPUT,
  357. .output_flags = OBS_SOURCE_VIDEO,
  358. .get_name = xshm_getname,
  359. .create = xshm_create,
  360. .destroy = xshm_destroy,
  361. .update = xshm_update,
  362. .get_defaults = xshm_defaults,
  363. .get_properties = xshm_properties,
  364. .video_tick = xshm_video_tick,
  365. .video_render = xshm_video_render,
  366. .get_width = xshm_getwidth,
  367. .get_height = xshm_getheight
  368. };