xcompcap-main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. #include <glad/glad.h>
  2. #include <glad/glad_glx.h>
  3. #include <X11/Xlib.h>
  4. #include <X11/extensions/Xcomposite.h>
  5. #include <pthread.h>
  6. #include <vector>
  7. #include <obs-module.h>
  8. #include <graphics/vec4.h>
  9. #include <util/platform.h>
  10. #include "xcompcap-main.hpp"
  11. #include "xcompcap-helper.hpp"
  12. #include "xcursor.h"
  13. #define xdisp (XCompcap::disp())
  14. #define WIN_STRING_DIV "\r\n"
  15. bool XCompcapMain::init()
  16. {
  17. if (!xdisp) {
  18. blog(LOG_ERROR, "failed opening display");
  19. return false;
  20. }
  21. int eventBase, errorBase;
  22. if (!XCompositeQueryExtension(xdisp, &eventBase, &errorBase)) {
  23. blog(LOG_ERROR, "Xcomposite extension not supported");
  24. return false;
  25. }
  26. int major = 0, minor = 2;
  27. XCompositeQueryVersion(xdisp, &major, &minor);
  28. if (major == 0 && minor < 2) {
  29. blog(LOG_ERROR, "Xcomposite extension is too old: %d.%d < 0.2",
  30. major, minor);
  31. return false;
  32. }
  33. return true;
  34. }
  35. void XCompcapMain::deinit()
  36. {
  37. XCompcap::cleanupDisplay();
  38. }
  39. obs_properties_t *XCompcapMain::properties()
  40. {
  41. obs_properties_t *props = obs_properties_create();
  42. obs_property_t *wins = obs_properties_add_list(props, "capture_window",
  43. obs_module_text("Window"),
  44. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  45. for (Window win: XCompcap::getTopLevelWindows()) {
  46. std::string wname = XCompcap::getWindowName(win);
  47. std::string cls = XCompcap::getWindowClass(win);
  48. std::string winid = std::to_string((long long)win);
  49. std::string desc =
  50. (winid + WIN_STRING_DIV + wname +
  51. WIN_STRING_DIV + cls);
  52. obs_property_list_add_string(wins, wname.c_str(),
  53. desc.c_str());
  54. }
  55. obs_properties_add_int(props, "cut_top", obs_module_text("CropTop"),
  56. 0, 4096, 1);
  57. obs_properties_add_int(props, "cut_left", obs_module_text("CropLeft"),
  58. 0, 4096, 1);
  59. obs_properties_add_int(props, "cut_right", obs_module_text("CropRight"),
  60. 0, 4096, 1);
  61. obs_properties_add_int(props, "cut_bot", obs_module_text("CropBottom"),
  62. 0, 4096, 1);
  63. obs_properties_add_bool(props, "swap_redblue",
  64. obs_module_text("SwapRedBlue"));
  65. obs_properties_add_bool(props, "lock_x", obs_module_text("LockX"));
  66. obs_properties_add_bool(props, "show_cursor",
  67. obs_module_text("CaptureCursor"));
  68. obs_properties_add_bool(props, "include_border",
  69. obs_module_text("IncludeXBorder"));
  70. obs_properties_add_bool(props, "exclude_alpha",
  71. obs_module_text("ExcludeAlpha"));
  72. return props;
  73. }
  74. void XCompcapMain::defaults(obs_data_t *settings)
  75. {
  76. obs_data_set_default_string(settings, "capture_window", "");
  77. obs_data_set_default_int(settings, "cut_top", 0);
  78. obs_data_set_default_int(settings, "cut_left", 0);
  79. obs_data_set_default_int(settings, "cut_right", 0);
  80. obs_data_set_default_int(settings, "cut_bot", 0);
  81. obs_data_set_default_bool(settings, "swap_redblue", false);
  82. obs_data_set_default_bool(settings, "lock_x", false);
  83. obs_data_set_default_bool(settings, "show_cursor", true);
  84. obs_data_set_default_bool(settings, "include_border", false);
  85. obs_data_set_default_bool(settings, "exclude_alpha", false);
  86. }
  87. #define FIND_WINDOW_INTERVAL 2.0
  88. struct XCompcapMain_private
  89. {
  90. XCompcapMain_private()
  91. :win(0)
  92. ,cut_top(0), cur_cut_top(0)
  93. ,cut_left(0), cur_cut_left(0)
  94. ,cut_right(0), cur_cut_right(0)
  95. ,cut_bot(0), cur_cut_bot(0)
  96. ,inverted(false)
  97. ,width(0),height(0)
  98. ,pixmap(0)
  99. ,glxpixmap(0)
  100. ,tex(0)
  101. ,gltex(0)
  102. {
  103. pthread_mutexattr_init(&lockattr);
  104. pthread_mutexattr_settype(&lockattr, PTHREAD_MUTEX_RECURSIVE);
  105. pthread_mutex_init(&lock, &lockattr);
  106. }
  107. ~XCompcapMain_private()
  108. {
  109. pthread_mutex_destroy(&lock);
  110. pthread_mutexattr_destroy(&lockattr);
  111. }
  112. obs_source_t *source;
  113. std::string windowName;
  114. Window win = 0;
  115. int cut_top, cur_cut_top;
  116. int cut_left, cur_cut_left;
  117. int cut_right, cur_cut_right;
  118. int cut_bot, cur_cut_bot;
  119. bool inverted;
  120. bool swapRedBlue;
  121. bool lockX;
  122. bool include_border;
  123. bool exclude_alpha;
  124. double window_check_time = 0.0;
  125. uint32_t width;
  126. uint32_t height;
  127. uint32_t border;
  128. Pixmap pixmap;
  129. GLXPixmap glxpixmap;
  130. gs_texture_t *tex;
  131. gs_texture_t *gltex;
  132. pthread_mutex_t lock;
  133. pthread_mutexattr_t lockattr;
  134. bool show_cursor = true;
  135. bool cursor_outside = false;
  136. xcursor_t *cursor = nullptr;
  137. };
  138. XCompcapMain::XCompcapMain(obs_data_t *settings, obs_source_t *source)
  139. {
  140. p = new XCompcapMain_private;
  141. p->source = source;
  142. obs_enter_graphics();
  143. p->cursor = xcursor_init(xdisp);
  144. obs_leave_graphics();
  145. updateSettings(settings);
  146. }
  147. static void xcc_cleanup(XCompcapMain_private *p);
  148. XCompcapMain::~XCompcapMain()
  149. {
  150. ObsGsContextHolder obsctx;
  151. if (p->tex) {
  152. gs_texture_destroy(p->tex);
  153. p->tex = 0;
  154. }
  155. xcc_cleanup(p);
  156. if (p->cursor) {
  157. xcursor_destroy(p->cursor);
  158. p->cursor = nullptr;
  159. }
  160. delete p;
  161. }
  162. static Window getWindowFromString(std::string wstr)
  163. {
  164. XErrorLock xlock;
  165. if (wstr == "") {
  166. return XCompcap::getTopLevelWindows().front();
  167. }
  168. if (wstr.substr(0, 4) == "root") {
  169. int i = std::stoi("0" + wstr.substr(4));
  170. return RootWindow(xdisp, i);
  171. }
  172. size_t firstMark = wstr.find(WIN_STRING_DIV);
  173. size_t markSize = strlen(WIN_STRING_DIV);
  174. if (firstMark == std::string::npos)
  175. return (Window)std::stol(wstr);
  176. Window wid = 0;
  177. wstr = wstr.substr(firstMark + markSize);
  178. size_t lastMark = wstr.rfind(WIN_STRING_DIV);
  179. std::string wname = wstr.substr(0, lastMark);
  180. std::string wcls = wstr.substr(lastMark + markSize);
  181. Window matchedNameWin = wid;
  182. for (Window cwin: XCompcap::getTopLevelWindows()) {
  183. std::string cwinname = XCompcap::getWindowName(cwin);
  184. std::string ccls = XCompcap::getWindowClass(cwin);
  185. if (cwin == wid && wname == cwinname && wcls == ccls)
  186. return wid;
  187. if (wname == cwinname ||
  188. (!matchedNameWin && !wcls.empty() && wcls == ccls))
  189. matchedNameWin = cwin;
  190. }
  191. return matchedNameWin;
  192. }
  193. static void xcc_cleanup(XCompcapMain_private *p)
  194. {
  195. PLock lock(&p->lock);
  196. XDisplayLock xlock;
  197. if (p->gltex) {
  198. gs_texture_destroy(p->gltex);
  199. p->gltex = 0;
  200. }
  201. if (p->glxpixmap) {
  202. glXDestroyPixmap(xdisp, p->glxpixmap);
  203. p->glxpixmap = 0;
  204. }
  205. if (p->pixmap) {
  206. XFreePixmap(xdisp, p->pixmap);
  207. p->pixmap = 0;
  208. }
  209. if (p->win) {
  210. XCompositeUnredirectWindow(xdisp, p->win,
  211. CompositeRedirectAutomatic);
  212. XSelectInput(xdisp, p->win, 0);
  213. p->win = 0;
  214. }
  215. }
  216. void XCompcapMain::updateSettings(obs_data_t *settings)
  217. {
  218. PLock lock(&p->lock);
  219. XErrorLock xlock;
  220. ObsGsContextHolder obsctx;
  221. blog(LOG_DEBUG, "Settings updating");
  222. Window prevWin = p->win;
  223. xcc_cleanup(p);
  224. if (settings) {
  225. const char *windowName = obs_data_get_string(settings,
  226. "capture_window");
  227. p->windowName = windowName;
  228. p->win = getWindowFromString(windowName);
  229. p->cut_top = obs_data_get_int(settings, "cut_top");
  230. p->cut_left = obs_data_get_int(settings, "cut_left");
  231. p->cut_right = obs_data_get_int(settings, "cut_right");
  232. p->cut_bot = obs_data_get_int(settings, "cut_bot");
  233. p->lockX = obs_data_get_bool(settings, "lock_x");
  234. p->swapRedBlue = obs_data_get_bool(settings, "swap_redblue");
  235. p->show_cursor = obs_data_get_bool(settings, "show_cursor");
  236. p->include_border = obs_data_get_bool(settings, "include_border");
  237. p->exclude_alpha = obs_data_get_bool(settings, "exclude_alpha");
  238. } else {
  239. p->win = prevWin;
  240. }
  241. xlock.resetError();
  242. if (p->win)
  243. XCompositeRedirectWindow(xdisp, p->win,
  244. CompositeRedirectAutomatic);
  245. if (xlock.gotError()) {
  246. blog(LOG_ERROR, "XCompositeRedirectWindow failed: %s",
  247. xlock.getErrorText().c_str());
  248. return;
  249. }
  250. if (p->win)
  251. XSelectInput(xdisp, p->win,
  252. StructureNotifyMask
  253. | ExposureMask
  254. | VisibilityChangeMask);
  255. XSync(xdisp, 0);
  256. XWindowAttributes attr;
  257. if (!p->win || !XGetWindowAttributes(xdisp, p->win, &attr)) {
  258. p->win = 0;
  259. p->width = 0;
  260. p->height = 0;
  261. return;
  262. }
  263. if (p->win && p->cursor && p->show_cursor) {
  264. Window child;
  265. int x, y;
  266. XTranslateCoordinates(xdisp, p->win, attr.root, 0, 0, &x, &y,
  267. &child);
  268. xcursor_offset(p->cursor, x, y);
  269. }
  270. gs_color_format cf = GS_RGBA;
  271. if (p->exclude_alpha) {
  272. cf = GS_BGRX;
  273. }
  274. p->border = attr.border_width;
  275. if (p->include_border) {
  276. p->width = attr.width + p->border * 2;
  277. p->height = attr.height + p->border * 2;
  278. } else {
  279. p->width = attr.width;
  280. p->height = attr.height;
  281. }
  282. if (p->cut_top + p->cut_bot < (int)p->height) {
  283. p->cur_cut_top = p->cut_top;
  284. p->cur_cut_bot = p->cut_bot;
  285. } else {
  286. p->cur_cut_top = 0;
  287. p->cur_cut_bot = 0;
  288. }
  289. if (p->cut_left + p->cut_right < (int)p->width) {
  290. p->cur_cut_left = p->cut_left;
  291. p->cur_cut_right = p->cut_right;
  292. } else {
  293. p->cur_cut_left = 0;
  294. p->cur_cut_right = 0;
  295. }
  296. if (p->tex)
  297. gs_texture_destroy(p->tex);
  298. uint8_t *texData = new uint8_t[width() * height() * 4];
  299. memset(texData, 0, width() * height() * 4);
  300. const uint8_t* texDataArr[] = { texData, 0 };
  301. p->tex = gs_texture_create(width(), height(), cf, 1,
  302. texDataArr, 0);
  303. delete[] texData;
  304. if (p->swapRedBlue) {
  305. GLuint tex = *(GLuint*)gs_texture_get_obj(p->tex);
  306. glBindTexture(GL_TEXTURE_2D, tex);
  307. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
  308. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
  309. glBindTexture(GL_TEXTURE_2D, 0);
  310. }
  311. const int attrs[] =
  312. {
  313. GLX_BIND_TO_TEXTURE_RGBA_EXT, GL_TRUE,
  314. GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
  315. GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
  316. GLX_ALPHA_SIZE, 8,
  317. GLX_DOUBLEBUFFER, GL_FALSE,
  318. None
  319. };
  320. int nelem = 0;
  321. GLXFBConfig* configs = glXChooseFBConfig(xdisp,
  322. XCompcap::getRootWindowScreen(attr.root),
  323. attrs, &nelem);
  324. if (nelem <= 0) {
  325. blog(LOG_ERROR, "no matching fb config found");
  326. p->win = 0;
  327. p->height = 0;
  328. p->width = 0;
  329. return;
  330. }
  331. glXGetFBConfigAttrib(xdisp, configs[0], GLX_Y_INVERTED_EXT, &nelem);
  332. p->inverted = nelem != 0;
  333. xlock.resetError();
  334. p->pixmap = XCompositeNameWindowPixmap(xdisp, p->win);
  335. if (xlock.gotError()) {
  336. blog(LOG_ERROR, "XCompositeNameWindowPixmap failed: %s",
  337. xlock.getErrorText().c_str());
  338. p->pixmap = 0;
  339. XFree(configs);
  340. return;
  341. }
  342. const int attribs[] =
  343. {
  344. GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
  345. GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGBA_EXT,
  346. None
  347. };
  348. p->glxpixmap = glXCreatePixmap(xdisp, configs[0], p->pixmap, attribs);
  349. if (xlock.gotError()) {
  350. blog(LOG_ERROR, "glXCreatePixmap failed: %s",
  351. xlock.getErrorText().c_str());
  352. XFreePixmap(xdisp, p->pixmap);
  353. XFree(configs);
  354. p->pixmap = 0;
  355. p->glxpixmap = 0;
  356. return;
  357. }
  358. XFree(configs);
  359. p->gltex = gs_texture_create(p->width, p->height, cf, 1, 0,
  360. GS_GL_DUMMYTEX);
  361. GLuint gltex = *(GLuint*)gs_texture_get_obj(p->gltex);
  362. glBindTexture(GL_TEXTURE_2D, gltex);
  363. glXBindTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_LEFT_EXT, NULL);
  364. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  365. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  366. if (!p->windowName.empty()) {
  367. blog(LOG_INFO, "[window-capture: '%s'] update settings:\n"
  368. "\ttitle: %s\n"
  369. "\tclass: %s",
  370. obs_source_get_name(p->source),
  371. XCompcap::getWindowName(p->win).c_str(),
  372. XCompcap::getWindowClass(p->win).c_str());
  373. blog(LOG_DEBUG, "\n"
  374. "\tid: %s",
  375. std::to_string((long long)p->win).c_str());
  376. }
  377. }
  378. void XCompcapMain::tick(float seconds)
  379. {
  380. if (!obs_source_showing(p->source))
  381. return;
  382. PLock lock(&p->lock, true);
  383. if (!lock.isLocked())
  384. return;
  385. XCompcap::processEvents();
  386. if (p->win && XCompcap::windowWasReconfigured(p->win)) {
  387. p->window_check_time = FIND_WINDOW_INTERVAL;
  388. p->win = 0;
  389. }
  390. XDisplayLock xlock;
  391. XWindowAttributes attr;
  392. if (!p->win || !XGetWindowAttributes(xdisp, p->win, &attr)) {
  393. p->window_check_time += (double)seconds;
  394. if (p->window_check_time < FIND_WINDOW_INTERVAL)
  395. return;
  396. Window newWin = getWindowFromString(p->windowName);
  397. p->window_check_time = 0.0;
  398. if (newWin && XGetWindowAttributes(xdisp, newWin, &attr)) {
  399. p->win = newWin;
  400. updateSettings(0);
  401. } else {
  402. return;
  403. }
  404. }
  405. if (!p->tex || !p->gltex)
  406. return;
  407. obs_enter_graphics();
  408. if (p->lockX) {
  409. XLockDisplay(xdisp);
  410. XSync(xdisp, 0);
  411. }
  412. if (p->include_border) {
  413. gs_copy_texture_region(
  414. p->tex, 0, 0,
  415. p->gltex,
  416. p->cur_cut_left,
  417. p->cur_cut_top,
  418. width(), height());
  419. } else {
  420. gs_copy_texture_region(
  421. p->tex, 0, 0,
  422. p->gltex,
  423. p->cur_cut_left + p->border,
  424. p->cur_cut_top + p->border,
  425. width(), height());
  426. }
  427. if (p->cursor && p->show_cursor) {
  428. xcursor_tick(p->cursor);
  429. p->cursor_outside =
  430. p->cursor->x < p->cur_cut_left ||
  431. p->cursor->y < p->cur_cut_top ||
  432. p->cursor->x > int(p->width - p->cur_cut_right) ||
  433. p->cursor->y > int(p->height - p->cur_cut_bot);
  434. }
  435. if (p->lockX)
  436. XUnlockDisplay(xdisp);
  437. obs_leave_graphics();
  438. }
  439. void XCompcapMain::render(gs_effect_t *effect)
  440. {
  441. if (!p->win)
  442. return;
  443. PLock lock(&p->lock, true);
  444. effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  445. if (!lock.isLocked() || !p->tex)
  446. return;
  447. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  448. gs_effect_set_texture(image, p->tex);
  449. while (gs_effect_loop(effect, "Draw")) {
  450. gs_draw_sprite(p->tex, 0, 0, 0);
  451. }
  452. if (p->cursor && p->gltex && p->show_cursor && !p->cursor_outside) {
  453. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  454. while (gs_effect_loop(effect, "Draw")) {
  455. xcursor_render(p->cursor);
  456. }
  457. }
  458. }
  459. uint32_t XCompcapMain::width()
  460. {
  461. if (!p->win)
  462. return 0;
  463. return p->width - p->cur_cut_left - p->cur_cut_right;
  464. }
  465. uint32_t XCompcapMain::height()
  466. {
  467. if (!p->win)
  468. return 0;
  469. return p->height - p->cur_cut_bot - p->cur_cut_top;
  470. }