1
0

xcompcap-main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 progpath = XCompcap::getWindowCommand(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 + progpath);
  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. struct XCompcapMain_private
  88. {
  89. XCompcapMain_private()
  90. :win(0)
  91. ,cut_top(0), cur_cut_top(0)
  92. ,cut_left(0), cur_cut_left(0)
  93. ,cut_right(0), cur_cut_right(0)
  94. ,cut_bot(0), cur_cut_bot(0)
  95. ,inverted(false)
  96. ,width(0),height(0)
  97. ,pixmap(0)
  98. ,glxpixmap(0)
  99. ,tex(0)
  100. ,gltex(0)
  101. {
  102. pthread_mutexattr_init(&lockattr);
  103. pthread_mutexattr_settype(&lockattr, PTHREAD_MUTEX_RECURSIVE);
  104. pthread_mutex_init(&lock, &lockattr);
  105. }
  106. ~XCompcapMain_private()
  107. {
  108. pthread_mutex_destroy(&lock);
  109. pthread_mutexattr_destroy(&lockattr);
  110. }
  111. obs_source_t *source;
  112. std::string windowName;
  113. Window win;
  114. int cut_top, cur_cut_top;
  115. int cut_left, cur_cut_left;
  116. int cut_right, cur_cut_right;
  117. int cut_bot, cur_cut_bot;
  118. bool inverted;
  119. bool swapRedBlue;
  120. bool lockX;
  121. bool include_border;
  122. bool exclude_alpha;
  123. uint32_t width;
  124. uint32_t height;
  125. uint32_t border;
  126. Pixmap pixmap;
  127. GLXPixmap glxpixmap;
  128. gs_texture_t *tex;
  129. gs_texture_t *gltex;
  130. pthread_mutex_t lock;
  131. pthread_mutexattr_t lockattr;
  132. bool show_cursor = true;
  133. bool cursor_outside = false;
  134. xcursor_t *cursor = nullptr;
  135. };
  136. XCompcapMain::XCompcapMain(obs_data_t *settings, obs_source_t *source)
  137. {
  138. p = new XCompcapMain_private;
  139. p->source = source;
  140. obs_enter_graphics();
  141. p->cursor = xcursor_init(xdisp);
  142. obs_leave_graphics();
  143. updateSettings(settings);
  144. }
  145. static void xcc_cleanup(XCompcapMain_private *p);
  146. XCompcapMain::~XCompcapMain()
  147. {
  148. ObsGsContextHolder obsctx;
  149. if (p->tex) {
  150. gs_texture_destroy(p->tex);
  151. p->tex = 0;
  152. }
  153. xcc_cleanup(p);
  154. if (p->cursor) {
  155. xcursor_destroy(p->cursor);
  156. p->cursor = nullptr;
  157. }
  158. delete p;
  159. }
  160. static Window getWindowFromString(std::string wstr)
  161. {
  162. if (wstr == "") {
  163. return XCompcap::getTopLevelWindows().front();
  164. }
  165. if (wstr.substr(0, 4) == "root") {
  166. int i = std::stoi("0" + wstr.substr(4));
  167. return RootWindow(xdisp, i);
  168. }
  169. size_t firstMark = wstr.find(WIN_STRING_DIV);
  170. if (firstMark == std::string::npos)
  171. return (Window)std::stol(wstr);
  172. std::string widstr = wstr.substr(0, firstMark);
  173. Window wid = (Window)std::stol(widstr);
  174. wstr = wstr.substr(firstMark + strlen(WIN_STRING_DIV));
  175. size_t lastMark = wstr.rfind(WIN_STRING_DIV);
  176. std::string wname = wstr.substr(0, lastMark);
  177. Window matchedNameWin = wid;
  178. for (Window cwin: XCompcap::getTopLevelWindows()) {
  179. std::string cwinname = XCompcap::getWindowName(cwin);
  180. if (cwin == wid && wname == cwinname)
  181. return wid;
  182. if (wname == cwinname)
  183. matchedNameWin = cwin;
  184. }
  185. return matchedNameWin;
  186. }
  187. static void xcc_cleanup(XCompcapMain_private *p)
  188. {
  189. PLock lock(&p->lock);
  190. XErrorLock xlock;
  191. if (p->gltex) {
  192. gs_texture_destroy(p->gltex);
  193. p->gltex = 0;
  194. }
  195. if (p->glxpixmap) {
  196. glXDestroyPixmap(xdisp, p->glxpixmap);
  197. p->glxpixmap = 0;
  198. }
  199. if (p->pixmap) {
  200. XFreePixmap(xdisp, p->pixmap);
  201. p->pixmap = 0;
  202. }
  203. if (p->win) {
  204. XCompositeUnredirectWindow(xdisp, p->win,
  205. CompositeRedirectAutomatic);
  206. XSelectInput(xdisp, p->win, 0);
  207. p->win = 0;
  208. }
  209. }
  210. void XCompcapMain::updateSettings(obs_data_t *settings)
  211. {
  212. PLock lock(&p->lock);
  213. XErrorLock xlock;
  214. ObsGsContextHolder obsctx;
  215. blog(LOG_DEBUG, "Settings updating");
  216. Window prevWin = p->win;
  217. xcc_cleanup(p);
  218. if (settings) {
  219. const char *windowName = obs_data_get_string(settings,
  220. "capture_window");
  221. p->windowName = windowName;
  222. p->win = getWindowFromString(windowName);
  223. p->cut_top = obs_data_get_int(settings, "cut_top");
  224. p->cut_left = obs_data_get_int(settings, "cut_left");
  225. p->cut_right = obs_data_get_int(settings, "cut_right");
  226. p->cut_bot = obs_data_get_int(settings, "cut_bot");
  227. p->lockX = obs_data_get_bool(settings, "lock_x");
  228. p->swapRedBlue = obs_data_get_bool(settings, "swap_redblue");
  229. p->show_cursor = obs_data_get_bool(settings, "show_cursor");
  230. p->include_border = obs_data_get_bool(settings, "include_border");
  231. p->exclude_alpha = obs_data_get_bool(settings, "exclude_alpha");
  232. } else {
  233. p->win = prevWin;
  234. }
  235. xlock.resetError();
  236. XCompositeRedirectWindow(xdisp, p->win, CompositeRedirectAutomatic);
  237. if (xlock.gotError()) {
  238. blog(LOG_ERROR, "XCompositeRedirectWindow failed: %s",
  239. xlock.getErrorText().c_str());
  240. return;
  241. }
  242. XSelectInput(xdisp, p->win, StructureNotifyMask | ExposureMask);
  243. XSync(xdisp, 0);
  244. XWindowAttributes attr;
  245. if (!XGetWindowAttributes(xdisp, p->win, &attr)) {
  246. p->win = 0;
  247. p->width = 0;
  248. p->height = 0;
  249. return;
  250. }
  251. if (p->cursor && p->show_cursor) {
  252. Window child;
  253. int x, y;
  254. XTranslateCoordinates(xdisp, p->win, attr.root, 0, 0, &x, &y,
  255. &child);
  256. xcursor_offset(p->cursor, x, y);
  257. }
  258. gs_color_format cf = GS_RGBA;
  259. if (p->exclude_alpha) {
  260. cf = GS_BGRX;
  261. }
  262. p->border = attr.border_width;
  263. if (p->include_border) {
  264. p->width = attr.width + p->border * 2;
  265. p->height = attr.height + p->border * 2;
  266. } else {
  267. p->width = attr.width;
  268. p->height = attr.height;
  269. }
  270. if (p->cut_top + p->cut_bot < (int)p->height) {
  271. p->cur_cut_top = p->cut_top;
  272. p->cur_cut_bot = p->cut_bot;
  273. } else {
  274. p->cur_cut_top = 0;
  275. p->cur_cut_bot = 0;
  276. }
  277. if (p->cut_left + p->cut_right < (int)p->width) {
  278. p->cur_cut_left = p->cut_left;
  279. p->cur_cut_right = p->cut_right;
  280. } else {
  281. p->cur_cut_left = 0;
  282. p->cur_cut_right = 0;
  283. }
  284. if (p->tex)
  285. gs_texture_destroy(p->tex);
  286. uint8_t *texData = new uint8_t[width() * height() * 4];
  287. memset(texData, 0, width() * height() * 4);
  288. const uint8_t* texDataArr[] = { texData, 0 };
  289. p->tex = gs_texture_create(width(), height(), cf, 1,
  290. texDataArr, 0);
  291. delete[] texData;
  292. if (p->swapRedBlue) {
  293. GLuint tex = *(GLuint*)gs_texture_get_obj(p->tex);
  294. glBindTexture(GL_TEXTURE_2D, tex);
  295. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
  296. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
  297. glBindTexture(GL_TEXTURE_2D, 0);
  298. }
  299. const int attrs[] =
  300. {
  301. GLX_BIND_TO_TEXTURE_RGBA_EXT, GL_TRUE,
  302. GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
  303. GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
  304. GLX_DOUBLEBUFFER, GL_FALSE,
  305. None
  306. };
  307. int nelem = 0;
  308. GLXFBConfig* configs = glXChooseFBConfig(xdisp,
  309. XCompcap::getRootWindowScreen(attr.root),
  310. attrs, &nelem);
  311. if (nelem <= 0) {
  312. blog(LOG_ERROR, "no matching fb config found");
  313. p->win = 0;
  314. p->height = 0;
  315. p->width = 0;
  316. return;
  317. }
  318. glXGetFBConfigAttrib(xdisp, configs[0], GLX_Y_INVERTED_EXT, &nelem);
  319. p->inverted = nelem != 0;
  320. xlock.resetError();
  321. p->pixmap = XCompositeNameWindowPixmap(xdisp, p->win);
  322. if (xlock.gotError()) {
  323. blog(LOG_ERROR, "XCompositeNameWindowPixmap failed: %s",
  324. xlock.getErrorText().c_str());
  325. p->pixmap = 0;
  326. XFree(configs);
  327. return;
  328. }
  329. const int attribs[] =
  330. {
  331. GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
  332. GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGBA_EXT,
  333. None
  334. };
  335. p->glxpixmap = glXCreatePixmap(xdisp, configs[0], p->pixmap, attribs);
  336. if (xlock.gotError()) {
  337. blog(LOG_ERROR, "glXCreatePixmap failed: %s",
  338. xlock.getErrorText().c_str());
  339. XFreePixmap(xdisp, p->pixmap);
  340. XFree(configs);
  341. p->pixmap = 0;
  342. p->glxpixmap = 0;
  343. return;
  344. }
  345. XFree(configs);
  346. p->gltex = gs_texture_create(p->width, p->height, cf, 1, 0,
  347. GS_GL_DUMMYTEX);
  348. GLuint gltex = *(GLuint*)gs_texture_get_obj(p->gltex);
  349. glBindTexture(GL_TEXTURE_2D, gltex);
  350. glXBindTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_LEFT_EXT, NULL);
  351. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  352. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  353. }
  354. void XCompcapMain::tick(float seconds)
  355. {
  356. UNUSED_PARAMETER(seconds);
  357. if (!obs_source_showing(p->source))
  358. return;
  359. PLock lock(&p->lock, true);
  360. if (!lock.isLocked())
  361. return;
  362. XCompcap::processEvents();
  363. if (XCompcap::windowWasReconfigured(p->win))
  364. updateSettings(0);
  365. XErrorLock xlock;
  366. xlock.resetError();
  367. XWindowAttributes attr;
  368. if (!XGetWindowAttributes(xdisp, p->win, &attr)) {
  369. Window newWin = getWindowFromString(p->windowName);
  370. if (XGetWindowAttributes(xdisp, newWin, &attr)) {
  371. p->win = newWin;
  372. updateSettings(0);
  373. }
  374. return;
  375. }
  376. if (!p->tex || !p->gltex)
  377. return;
  378. obs_enter_graphics();
  379. if (p->lockX) {
  380. XLockDisplay(xdisp);
  381. XSync(xdisp, 0);
  382. }
  383. if (p->include_border) {
  384. gs_copy_texture_region(
  385. p->tex, 0, 0,
  386. p->gltex,
  387. p->cur_cut_left,
  388. p->cur_cut_top,
  389. width(), height());
  390. } else {
  391. gs_copy_texture_region(
  392. p->tex, 0, 0,
  393. p->gltex,
  394. p->cur_cut_left + p->border,
  395. p->cur_cut_top + p->border,
  396. width(), height());
  397. }
  398. if (p->cursor && p->show_cursor) {
  399. xcursor_tick(p->cursor);
  400. p->cursor_outside =
  401. p->cursor->x < p->cur_cut_left ||
  402. p->cursor->y < p->cur_cut_top ||
  403. p->cursor->x > int(p->width - p->cur_cut_right) ||
  404. p->cursor->y > int(p->height - p->cur_cut_bot);
  405. }
  406. if (p->lockX)
  407. XUnlockDisplay(xdisp);
  408. obs_leave_graphics();
  409. }
  410. void XCompcapMain::render(gs_effect_t *effect)
  411. {
  412. PLock lock(&p->lock, true);
  413. if (!p->win)
  414. return;
  415. effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  416. if (!lock.isLocked() || !p->tex)
  417. return;
  418. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  419. gs_effect_set_texture(image, p->tex);
  420. while (gs_effect_loop(effect, "Draw")) {
  421. gs_draw_sprite(p->tex, 0, 0, 0);
  422. }
  423. if (p->cursor && p->gltex && p->show_cursor && !p->cursor_outside) {
  424. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  425. while (gs_effect_loop(effect, "Draw")) {
  426. xcursor_render(p->cursor);
  427. }
  428. }
  429. }
  430. uint32_t XCompcapMain::width()
  431. {
  432. return p->width - p->cur_cut_left - p->cur_cut_right;
  433. }
  434. uint32_t XCompcapMain::height()
  435. {
  436. return p->height - p->cur_cut_bot - p->cur_cut_top;
  437. }