xcompcap-main.cpp 12 KB

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