xshm-input.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 *unused)
  112. {
  113. UNUSED_PARAMETER(unused);
  114. return obs_module_text("X11SharedMemoryScreenInput");
  115. }
  116. /**
  117. * Stop the capture
  118. */
  119. static void xshm_capture_stop(struct xshm_data *data)
  120. {
  121. obs_enter_graphics();
  122. if (data->texture) {
  123. gs_texture_destroy(data->texture);
  124. data->texture = NULL;
  125. }
  126. if (data->cursor) {
  127. xcb_xcursor_destroy(data->cursor);
  128. data->cursor = NULL;
  129. }
  130. obs_leave_graphics();
  131. if (data->xshm) {
  132. xshm_xcb_detach(data->xshm);
  133. data->xshm = NULL;
  134. }
  135. if (data->xcb) {
  136. xcb_disconnect(data->xcb);
  137. data->xcb = NULL;
  138. }
  139. if (data->server) {
  140. bfree(data->server);
  141. data->server = NULL;
  142. }
  143. }
  144. /**
  145. * Start the capture
  146. */
  147. static void xshm_capture_start(struct xshm_data *data)
  148. {
  149. const char *server = (data->advanced && *data->server)
  150. ? data->server : NULL;
  151. data->xcb = xcb_connect(server, NULL);
  152. if (!data->xcb || xcb_connection_has_error(data->xcb)) {
  153. blog(LOG_ERROR, "Unable to open X display !");
  154. goto fail;
  155. }
  156. if (!xshm_check_extensions(data->xcb))
  157. goto fail;
  158. data->use_xinerama = xinerama_is_active(data->xcb) ? true : false;
  159. if (xshm_update_geometry(data) < 0) {
  160. blog(LOG_ERROR, "failed to update geometry !");
  161. goto fail;
  162. }
  163. data->xshm = xshm_xcb_attach(data->xcb, data->width, data->height);
  164. if (!data->xshm) {
  165. blog(LOG_ERROR, "failed to attach shm !");
  166. goto fail;
  167. }
  168. data->cursor = xcb_xcursor_init(data->xcb);
  169. xcb_xcursor_offset(data->cursor, data->x_org, data->y_org);
  170. obs_enter_graphics();
  171. xshm_resize_texture(data);
  172. obs_leave_graphics();
  173. return;
  174. fail:
  175. xshm_capture_stop(data);
  176. }
  177. /**
  178. * Update the capture with changed settings
  179. */
  180. static void xshm_update(void *vptr, obs_data_t *settings)
  181. {
  182. XSHM_DATA(vptr);
  183. xshm_capture_stop(data);
  184. data->screen_id = obs_data_get_int(settings, "screen");
  185. data->show_cursor = obs_data_get_bool(settings, "show_cursor");
  186. data->advanced = obs_data_get_bool(settings, "advanced");
  187. data->server = bstrdup(obs_data_get_string(settings, "server"));
  188. xshm_capture_start(data);
  189. }
  190. /**
  191. * Get the default settings for the capture
  192. */
  193. static void xshm_defaults(obs_data_t *defaults)
  194. {
  195. obs_data_set_default_int(defaults, "screen", 0);
  196. obs_data_set_default_bool(defaults, "show_cursor", true);
  197. obs_data_set_default_bool(defaults, "advanced", false);
  198. }
  199. /**
  200. * Toggle visibility of advanced settings
  201. */
  202. static bool xshm_toggle_advanced(obs_properties_t *props,
  203. obs_property_t *p, obs_data_t *settings)
  204. {
  205. UNUSED_PARAMETER(p);
  206. const bool visible = obs_data_get_bool(settings, "advanced");
  207. obs_property_t *server = obs_properties_get(props, "server");
  208. obs_property_set_visible(server, visible);
  209. /* trigger server changed callback so the screen list is refreshed */
  210. obs_property_modified(server, settings);
  211. return true;
  212. }
  213. /**
  214. * The x server was changed
  215. */
  216. static bool xshm_server_changed(obs_properties_t *props,
  217. obs_property_t *p, obs_data_t *settings)
  218. {
  219. UNUSED_PARAMETER(p);
  220. bool advanced = obs_data_get_bool(settings, "advanced");
  221. int_fast32_t old_screen = obs_data_get_int(settings, "screen");
  222. const char *server = obs_data_get_string(settings, "server");
  223. obs_property_t *screens = obs_properties_get(props, "screen");
  224. /* we want a real NULL here in case there is no string here */
  225. server = (advanced && *server) ? server : NULL;
  226. obs_property_list_clear(screens);
  227. xcb_connection_t *xcb = xcb_connect(server, NULL);
  228. if (!xcb || xcb_connection_has_error(xcb)) {
  229. obs_property_set_enabled(screens, false);
  230. return true;
  231. }
  232. struct dstr screen_info;
  233. dstr_init(&screen_info);
  234. bool xinerama = xinerama_is_active(xcb);
  235. int_fast32_t count = (xinerama) ?
  236. xinerama_screen_count(xcb) :
  237. xcb_setup_roots_length(xcb_get_setup(xcb));
  238. for (int_fast32_t i = 0; i < count; ++i) {
  239. int_fast32_t x, y, w, h;
  240. x = y = w = h = 0;
  241. if (xinerama)
  242. xinerama_screen_geo(xcb, i, &x, &y, &w, &h);
  243. else
  244. x11_screen_geo(xcb, i, &w, &h);
  245. dstr_printf(&screen_info, "Screen %"PRIuFAST32" (%"PRIuFAST32
  246. "x%"PRIuFAST32" @ %"PRIuFAST32
  247. ",%"PRIuFAST32")", i, w, h, x, y);
  248. obs_property_list_add_int(screens, screen_info.array, i);
  249. }
  250. /* handle missing screen */
  251. if (old_screen + 1 > count) {
  252. dstr_printf(&screen_info, "Screen %"PRIuFAST32" (not found)",
  253. old_screen);
  254. size_t index = obs_property_list_add_int(screens,
  255. screen_info.array, old_screen);
  256. obs_property_list_item_disable(screens, index, true);
  257. }
  258. dstr_free(&screen_info);
  259. xcb_disconnect(xcb);
  260. obs_property_set_enabled(screens, true);
  261. return true;
  262. }
  263. /**
  264. * Get the properties for the capture
  265. */
  266. static obs_properties_t *xshm_properties(void *vptr)
  267. {
  268. XSHM_DATA(vptr);
  269. obs_properties_t *props = obs_properties_create();
  270. obs_properties_add_list(props, "screen", obs_module_text("Screen"),
  271. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  272. obs_properties_add_bool(props, "show_cursor",
  273. obs_module_text("CaptureCursor"));
  274. obs_property_t *advanced = obs_properties_add_bool(props, "advanced",
  275. obs_module_text("AdvancedSettings"));
  276. obs_property_t *server = obs_properties_add_text(props, "server",
  277. obs_module_text("XServer"), OBS_TEXT_DEFAULT);
  278. obs_property_set_modified_callback(advanced, xshm_toggle_advanced);
  279. obs_property_set_modified_callback(server, xshm_server_changed);
  280. /* trigger server callback to get screen count ... */
  281. obs_data_t *settings = obs_source_get_settings(data->source);
  282. obs_property_modified(server, settings);
  283. obs_data_release(settings);
  284. return props;
  285. }
  286. /**
  287. * Destroy the capture
  288. */
  289. static void xshm_destroy(void *vptr)
  290. {
  291. XSHM_DATA(vptr);
  292. if (!data)
  293. return;
  294. xshm_capture_stop(data);
  295. bfree(data);
  296. }
  297. /**
  298. * Create the capture
  299. */
  300. static void *xshm_create(obs_data_t *settings, obs_source_t *source)
  301. {
  302. struct xshm_data *data = bzalloc(sizeof(struct xshm_data));
  303. data->source = source;
  304. xshm_update(data, settings);
  305. return data;
  306. }
  307. /**
  308. * Prepare the capture data
  309. */
  310. static void xshm_video_tick(void *vptr, float seconds)
  311. {
  312. UNUSED_PARAMETER(seconds);
  313. XSHM_DATA(vptr);
  314. if (!data->texture)
  315. return;
  316. if (!obs_source_showing(data->source))
  317. return;
  318. xcb_shm_get_image_cookie_t img_c;
  319. xcb_shm_get_image_reply_t *img_r;
  320. xcb_xfixes_get_cursor_image_cookie_t cur_c;
  321. xcb_xfixes_get_cursor_image_reply_t *cur_r;
  322. img_c = xcb_shm_get_image_unchecked(data->xcb, data->xcb_screen->root,
  323. data->x_org, data->y_org, data->width, data->height,
  324. ~0, XCB_IMAGE_FORMAT_Z_PIXMAP, data->xshm->seg, 0);
  325. cur_c = xcb_xfixes_get_cursor_image_unchecked(data->xcb);
  326. img_r = xcb_shm_get_image_reply(data->xcb, img_c, NULL);
  327. cur_r = xcb_xfixes_get_cursor_image_reply(data->xcb, cur_c, NULL);
  328. if (!img_r)
  329. goto exit;
  330. obs_enter_graphics();
  331. gs_texture_set_image(data->texture, (void *) data->xshm->data,
  332. data->width * 4, false);
  333. xcb_xcursor_update(data->cursor, cur_r);
  334. obs_leave_graphics();
  335. exit:
  336. free(img_r);
  337. free(cur_r);
  338. }
  339. /**
  340. * Render the capture data
  341. */
  342. static void xshm_video_render(void *vptr, gs_effect_t *effect)
  343. {
  344. XSHM_DATA(vptr);
  345. effect = obs_get_opaque_effect();
  346. if (!data->texture)
  347. return;
  348. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  349. gs_effect_set_texture(image, data->texture);
  350. while (gs_effect_loop(effect, "Draw")) {
  351. gs_draw_sprite(data->texture, 0, 0, 0);
  352. }
  353. if (data->show_cursor) {
  354. effect = obs_get_default_effect();
  355. while (gs_effect_loop(effect, "Draw")) {
  356. xcb_xcursor_render(data->cursor);
  357. }
  358. }
  359. }
  360. /**
  361. * Width of the captured data
  362. */
  363. static uint32_t xshm_getwidth(void *vptr)
  364. {
  365. XSHM_DATA(vptr);
  366. return data->width;
  367. }
  368. /**
  369. * Height of the captured data
  370. */
  371. static uint32_t xshm_getheight(void *vptr)
  372. {
  373. XSHM_DATA(vptr);
  374. return data->height;
  375. }
  376. struct obs_source_info xshm_input = {
  377. .id = "xshm_input",
  378. .type = OBS_SOURCE_TYPE_INPUT,
  379. .output_flags = OBS_SOURCE_VIDEO |
  380. OBS_SOURCE_CUSTOM_DRAW,
  381. .get_name = xshm_getname,
  382. .create = xshm_create,
  383. .destroy = xshm_destroy,
  384. .update = xshm_update,
  385. .get_defaults = xshm_defaults,
  386. .get_properties = xshm_properties,
  387. .video_tick = xshm_video_tick,
  388. .video_render = xshm_video_render,
  389. .get_width = xshm_getwidth,
  390. .get_height = xshm_getheight
  391. };