xcompcap-main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. bool draw_opaque;
  125. double window_check_time = 0.0;
  126. uint32_t width;
  127. uint32_t height;
  128. uint32_t border;
  129. Pixmap pixmap;
  130. GLXPixmap glxpixmap;
  131. gs_texture_t *tex;
  132. gs_texture_t *gltex;
  133. pthread_mutex_t lock;
  134. pthread_mutexattr_t lockattr;
  135. bool show_cursor = true;
  136. bool cursor_outside = false;
  137. xcursor_t *cursor = nullptr;
  138. };
  139. XCompcapMain::XCompcapMain(obs_data_t *settings, obs_source_t *source)
  140. {
  141. p = new XCompcapMain_private;
  142. p->source = source;
  143. obs_enter_graphics();
  144. p->cursor = xcursor_init(xdisp);
  145. obs_leave_graphics();
  146. updateSettings(settings);
  147. }
  148. static void xcc_cleanup(XCompcapMain_private *p);
  149. XCompcapMain::~XCompcapMain()
  150. {
  151. ObsGsContextHolder obsctx;
  152. if (p->tex) {
  153. gs_texture_destroy(p->tex);
  154. p->tex = 0;
  155. }
  156. xcc_cleanup(p);
  157. if (p->cursor) {
  158. xcursor_destroy(p->cursor);
  159. p->cursor = nullptr;
  160. }
  161. delete p;
  162. }
  163. static Window getWindowFromString(std::string wstr)
  164. {
  165. XErrorLock xlock;
  166. if (wstr == "") {
  167. return XCompcap::getTopLevelWindows().front();
  168. }
  169. if (wstr.substr(0, 4) == "root") {
  170. int i = std::stoi("0" + wstr.substr(4));
  171. return RootWindow(xdisp, i);
  172. }
  173. size_t firstMark = wstr.find(WIN_STRING_DIV);
  174. size_t markSize = strlen(WIN_STRING_DIV);
  175. if (firstMark == std::string::npos)
  176. return (Window)std::stol(wstr);
  177. Window wid = 0;
  178. wstr = wstr.substr(firstMark + markSize);
  179. size_t lastMark = wstr.rfind(WIN_STRING_DIV);
  180. std::string wname = wstr.substr(0, lastMark);
  181. std::string wcls = wstr.substr(lastMark + markSize);
  182. Window matchedNameWin = wid;
  183. for (Window cwin: XCompcap::getTopLevelWindows()) {
  184. std::string cwinname = XCompcap::getWindowName(cwin);
  185. std::string ccls = XCompcap::getWindowClass(cwin);
  186. if (cwin == wid && wname == cwinname && wcls == ccls)
  187. return wid;
  188. if (wname == cwinname ||
  189. (!matchedNameWin && !wcls.empty() && wcls == ccls))
  190. matchedNameWin = cwin;
  191. }
  192. return matchedNameWin;
  193. }
  194. static void xcc_cleanup(XCompcapMain_private *p)
  195. {
  196. PLock lock(&p->lock);
  197. XDisplayLock xlock;
  198. if (p->gltex) {
  199. gs_texture_destroy(p->gltex);
  200. p->gltex = 0;
  201. }
  202. if (p->glxpixmap) {
  203. glXDestroyPixmap(xdisp, p->glxpixmap);
  204. p->glxpixmap = 0;
  205. }
  206. if (p->pixmap) {
  207. XFreePixmap(xdisp, p->pixmap);
  208. p->pixmap = 0;
  209. }
  210. if (p->win) {
  211. XCompositeUnredirectWindow(xdisp, p->win,
  212. CompositeRedirectAutomatic);
  213. XSelectInput(xdisp, p->win, 0);
  214. p->win = 0;
  215. }
  216. }
  217. void XCompcapMain::updateSettings(obs_data_t *settings)
  218. {
  219. PLock lock(&p->lock);
  220. XErrorLock xlock;
  221. ObsGsContextHolder obsctx;
  222. blog(LOG_DEBUG, "Settings updating");
  223. Window prevWin = p->win;
  224. xcc_cleanup(p);
  225. if (settings) {
  226. const char *windowName = obs_data_get_string(settings,
  227. "capture_window");
  228. p->windowName = windowName;
  229. p->win = getWindowFromString(windowName);
  230. p->cut_top = obs_data_get_int(settings, "cut_top");
  231. p->cut_left = obs_data_get_int(settings, "cut_left");
  232. p->cut_right = obs_data_get_int(settings, "cut_right");
  233. p->cut_bot = obs_data_get_int(settings, "cut_bot");
  234. p->lockX = obs_data_get_bool(settings, "lock_x");
  235. p->swapRedBlue = obs_data_get_bool(settings, "swap_redblue");
  236. p->show_cursor = obs_data_get_bool(settings, "show_cursor");
  237. p->include_border = obs_data_get_bool(settings, "include_border");
  238. p->exclude_alpha = obs_data_get_bool(settings, "exclude_alpha");
  239. p->draw_opaque = false;
  240. } else {
  241. p->win = prevWin;
  242. }
  243. xlock.resetError();
  244. if (p->win)
  245. XCompositeRedirectWindow(xdisp, p->win,
  246. CompositeRedirectAutomatic);
  247. if (xlock.gotError()) {
  248. blog(LOG_ERROR, "XCompositeRedirectWindow failed: %s",
  249. xlock.getErrorText().c_str());
  250. return;
  251. }
  252. if (p->win)
  253. XSelectInput(xdisp, p->win,
  254. StructureNotifyMask
  255. | ExposureMask
  256. | VisibilityChangeMask);
  257. XSync(xdisp, 0);
  258. XWindowAttributes attr;
  259. if (!p->win || !XGetWindowAttributes(xdisp, p->win, &attr)) {
  260. p->win = 0;
  261. p->width = 0;
  262. p->height = 0;
  263. return;
  264. }
  265. if (p->win && p->cursor && p->show_cursor) {
  266. Window child;
  267. int x, y;
  268. XTranslateCoordinates(xdisp, p->win, attr.root, 0, 0, &x, &y,
  269. &child);
  270. xcursor_offset(p->cursor, x, y);
  271. }
  272. gs_color_format cf = GS_RGBA;
  273. if (p->exclude_alpha) {
  274. cf = GS_BGRX;
  275. }
  276. bool has_alpha = true;
  277. if (attr.depth < 32) {
  278. cf = GS_BGRX;
  279. has_alpha = false;
  280. }
  281. if (cf == GS_BGRX) {
  282. if (settings) {
  283. p->swapRedBlue = !p->swapRedBlue;
  284. }
  285. p->draw_opaque = true;
  286. }
  287. p->border = attr.border_width;
  288. if (p->include_border) {
  289. p->width = attr.width + p->border * 2;
  290. p->height = attr.height + p->border * 2;
  291. } else {
  292. p->width = attr.width;
  293. p->height = attr.height;
  294. }
  295. if (p->cut_top + p->cut_bot < (int)p->height) {
  296. p->cur_cut_top = p->cut_top;
  297. p->cur_cut_bot = p->cut_bot;
  298. } else {
  299. p->cur_cut_top = 0;
  300. p->cur_cut_bot = 0;
  301. }
  302. if (p->cut_left + p->cut_right < (int)p->width) {
  303. p->cur_cut_left = p->cut_left;
  304. p->cur_cut_right = p->cut_right;
  305. } else {
  306. p->cur_cut_left = 0;
  307. p->cur_cut_right = 0;
  308. }
  309. if (p->tex)
  310. gs_texture_destroy(p->tex);
  311. uint8_t *texData = new uint8_t[width() * height() * 4];
  312. memset(texData, 0, width() * height() * 4);
  313. const uint8_t* texDataArr[] = { texData, 0 };
  314. p->tex = gs_texture_create(width(), height(), cf, 1,
  315. texDataArr, 0);
  316. delete[] texData;
  317. if (p->swapRedBlue) {
  318. GLuint tex = *(GLuint*)gs_texture_get_obj(p->tex);
  319. glBindTexture(GL_TEXTURE_2D, tex);
  320. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
  321. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
  322. glBindTexture(GL_TEXTURE_2D, 0);
  323. }
  324. const int attrs_alpha[] =
  325. {
  326. GLX_BIND_TO_TEXTURE_RGBA_EXT, GL_TRUE,
  327. GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
  328. GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
  329. GLX_ALPHA_SIZE, 8,
  330. None
  331. };
  332. const int attrs_no_alpha[] =
  333. {
  334. GLX_BIND_TO_TEXTURE_RGB_EXT, GL_TRUE,
  335. GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
  336. GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
  337. None
  338. };
  339. const int *attrs = has_alpha ? attrs_alpha : attrs_no_alpha;
  340. int nelem = 0;
  341. GLXFBConfig* configs = glXChooseFBConfig(xdisp,
  342. XCompcap::getRootWindowScreen(attr.root),
  343. attrs, &nelem);
  344. if (nelem <= 0) {
  345. blog(LOG_ERROR, "no matching fb config found");
  346. p->win = 0;
  347. p->height = 0;
  348. p->width = 0;
  349. return;
  350. }
  351. GLXFBConfig config;
  352. bool found = false;
  353. for (int i = 0; i < nelem; i++) {
  354. config = configs[i];
  355. XVisualInfo *visual = glXGetVisualFromFBConfig(xdisp, config);
  356. if (attr.visual->visualid == visual->visualid) {
  357. found = true;
  358. XFree(visual);
  359. break;
  360. }
  361. XFree(visual);
  362. }
  363. if (!found) {
  364. config = configs[0];
  365. p->draw_opaque = true;
  366. }
  367. int inverted;
  368. glXGetFBConfigAttrib(xdisp, config, GLX_Y_INVERTED_EXT, &inverted);
  369. p->inverted = inverted != 0;
  370. xlock.resetError();
  371. p->pixmap = XCompositeNameWindowPixmap(xdisp, p->win);
  372. if (xlock.gotError()) {
  373. blog(LOG_ERROR, "XCompositeNameWindowPixmap failed: %s",
  374. xlock.getErrorText().c_str());
  375. p->pixmap = 0;
  376. XFree(configs);
  377. return;
  378. }
  379. const int attribs_alpha[] =
  380. {
  381. GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
  382. GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGBA_EXT,
  383. None
  384. };
  385. const int attribs_no_alpha[] =
  386. {
  387. GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
  388. GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGB_EXT,
  389. None
  390. };
  391. const int *attribs = cf == GS_RGBA ? attribs_alpha : attribs_no_alpha;
  392. p->glxpixmap = glXCreatePixmap(xdisp, config, p->pixmap, attribs);
  393. if (xlock.gotError()) {
  394. blog(LOG_ERROR, "glXCreatePixmap failed: %s",
  395. xlock.getErrorText().c_str());
  396. XFreePixmap(xdisp, p->pixmap);
  397. XFree(configs);
  398. p->pixmap = 0;
  399. p->glxpixmap = 0;
  400. return;
  401. }
  402. XFree(configs);
  403. p->gltex = gs_texture_create(p->width, p->height, cf, 1, 0,
  404. GS_GL_DUMMYTEX);
  405. GLuint gltex = *(GLuint*)gs_texture_get_obj(p->gltex);
  406. glBindTexture(GL_TEXTURE_2D, gltex);
  407. glXBindTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_LEFT_EXT, NULL);
  408. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  409. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  410. if (!p->windowName.empty()) {
  411. blog(LOG_INFO, "[window-capture: '%s'] update settings:\n"
  412. "\ttitle: %s\n"
  413. "\tclass: %s\n"
  414. "\tHas alpha: %s\n"
  415. "\tFound exact GLXFBConfig: %s",
  416. obs_source_get_name(p->source),
  417. XCompcap::getWindowName(p->win).c_str(),
  418. XCompcap::getWindowClass(p->win).c_str(),
  419. has_alpha ? "yes" : "no",
  420. found ? "yes" : "no");
  421. blog(LOG_DEBUG, "\n"
  422. "\tid: %s",
  423. std::to_string((long long)p->win).c_str());
  424. }
  425. }
  426. void XCompcapMain::tick(float seconds)
  427. {
  428. if (!obs_source_showing(p->source))
  429. return;
  430. PLock lock(&p->lock, true);
  431. if (!lock.isLocked())
  432. return;
  433. XCompcap::processEvents();
  434. if (p->win && XCompcap::windowWasReconfigured(p->win)) {
  435. p->window_check_time = FIND_WINDOW_INTERVAL;
  436. p->win = 0;
  437. }
  438. XDisplayLock xlock;
  439. XWindowAttributes attr;
  440. if (!p->win || !XGetWindowAttributes(xdisp, p->win, &attr)) {
  441. p->window_check_time += (double)seconds;
  442. if (p->window_check_time < FIND_WINDOW_INTERVAL)
  443. return;
  444. Window newWin = getWindowFromString(p->windowName);
  445. p->window_check_time = 0.0;
  446. if (newWin && XGetWindowAttributes(xdisp, newWin, &attr)) {
  447. p->win = newWin;
  448. updateSettings(0);
  449. } else {
  450. return;
  451. }
  452. }
  453. if (!p->tex || !p->gltex)
  454. return;
  455. obs_enter_graphics();
  456. if (p->lockX) {
  457. XLockDisplay(xdisp);
  458. XSync(xdisp, 0);
  459. }
  460. if (p->include_border) {
  461. gs_copy_texture_region(
  462. p->tex, 0, 0,
  463. p->gltex,
  464. p->cur_cut_left,
  465. p->cur_cut_top,
  466. width(), height());
  467. } else {
  468. gs_copy_texture_region(
  469. p->tex, 0, 0,
  470. p->gltex,
  471. p->cur_cut_left + p->border,
  472. p->cur_cut_top + p->border,
  473. width(), height());
  474. }
  475. if (p->cursor && p->show_cursor) {
  476. xcursor_tick(p->cursor);
  477. p->cursor_outside =
  478. p->cursor->x < p->cur_cut_left ||
  479. p->cursor->y < p->cur_cut_top ||
  480. p->cursor->x > int(p->width - p->cur_cut_right) ||
  481. p->cursor->y > int(p->height - p->cur_cut_bot);
  482. }
  483. if (p->lockX)
  484. XUnlockDisplay(xdisp);
  485. obs_leave_graphics();
  486. }
  487. void XCompcapMain::render(gs_effect_t *effect)
  488. {
  489. if (!p->win)
  490. return;
  491. PLock lock(&p->lock, true);
  492. if (p->draw_opaque)
  493. effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  494. else
  495. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  496. if (!lock.isLocked() || !p->tex)
  497. return;
  498. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  499. gs_effect_set_texture(image, p->tex);
  500. while (gs_effect_loop(effect, "Draw")) {
  501. gs_draw_sprite(p->tex, 0, 0, 0);
  502. }
  503. if (p->cursor && p->gltex && p->show_cursor && !p->cursor_outside) {
  504. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  505. while (gs_effect_loop(effect, "Draw")) {
  506. xcursor_render(p->cursor);
  507. }
  508. }
  509. }
  510. uint32_t XCompcapMain::width()
  511. {
  512. if (!p->win)
  513. return 0;
  514. return p->width - p->cur_cut_left - p->cur_cut_right;
  515. }
  516. uint32_t XCompcapMain::height()
  517. {
  518. if (!p->win)
  519. return 0;
  520. return p->height - p->cur_cut_bot - p->cur_cut_top;
  521. }