xcompcap-main.cpp 12 KB

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