xcompcap-helper.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #include <X11/Xlib.h>
  2. #include <X11/Xatom.h>
  3. #include <X11/Xutil.h>
  4. #include <X11/extensions/Xcomposite.h>
  5. #include <unordered_set>
  6. #include <pthread.h>
  7. #include <obs-module.h>
  8. #include <util/platform.h>
  9. #include "xcompcap-helper.hpp"
  10. namespace XCompcap
  11. {
  12. static Display* xdisplay = 0;
  13. Display *disp()
  14. {
  15. if (!xdisplay)
  16. xdisplay = XOpenDisplay(NULL);
  17. return xdisplay;
  18. }
  19. void cleanupDisplay()
  20. {
  21. if (!xdisplay)
  22. return;
  23. XCloseDisplay(xdisplay);
  24. xdisplay = 0;
  25. }
  26. static void getAllWindows(Window parent, std::list<Window>& windows)
  27. {
  28. UNUSED_PARAMETER(parent);
  29. UNUSED_PARAMETER(windows);
  30. }
  31. std::list<Window> getAllWindows()
  32. {
  33. std::list<Window> res;
  34. for (int i = 0; i < ScreenCount(disp()); ++i)
  35. getAllWindows(RootWindow(disp(), i), res);
  36. return res;
  37. }
  38. // Specification for checking for ewmh support at
  39. // http://standards.freedesktop.org/wm-spec/wm-spec-latest.html#idm140200472693600
  40. bool ewmhIsSupported()
  41. {
  42. Display *display = disp();
  43. Atom netSupportingWmCheck = XInternAtom(display,
  44. "_NET_SUPPORTING_WM_CHECK", true);
  45. Atom actualType;
  46. int format = 0;
  47. unsigned long num = 0, bytes = 0;
  48. unsigned char *data = NULL;
  49. Window ewmh_window = 0;
  50. int status = XGetWindowProperty(
  51. display,
  52. DefaultRootWindow(display),
  53. netSupportingWmCheck,
  54. 0L,
  55. 1L,
  56. false,
  57. XA_WINDOW,
  58. &actualType,
  59. &format,
  60. &num,
  61. &bytes,
  62. &data);
  63. if (status == Success) {
  64. if (num > 0) {
  65. ewmh_window = ((Window*)data)[0];
  66. }
  67. if (data) {
  68. XFree(data);
  69. data = NULL;
  70. }
  71. }
  72. if (ewmh_window) {
  73. status = XGetWindowProperty(
  74. display,
  75. ewmh_window,
  76. netSupportingWmCheck,
  77. 0L,
  78. 1L,
  79. false,
  80. XA_WINDOW,
  81. &actualType,
  82. &format,
  83. &num,
  84. &bytes,
  85. &data);
  86. if (status != Success || num == 0 ||
  87. ewmh_window != ((Window*)data)[0]) {
  88. ewmh_window = 0;
  89. }
  90. if (status == Success && data) {
  91. XFree(data);
  92. }
  93. }
  94. return ewmh_window != 0;
  95. }
  96. std::list<Window> getTopLevelWindows()
  97. {
  98. std::list<Window> res;
  99. if (!ewmhIsSupported()) {
  100. blog(LOG_WARNING, "Unable to query window list "
  101. "because window manager "
  102. "does not support extended "
  103. "window manager Hints");
  104. return res;
  105. }
  106. Atom netClList = XInternAtom(disp(), "_NET_CLIENT_LIST", true);
  107. Atom actualType;
  108. int format;
  109. unsigned long num, bytes;
  110. Window* data = 0;
  111. for (int i = 0; i < ScreenCount(disp()); ++i) {
  112. Window rootWin = RootWindow(disp(), i);
  113. int status = XGetWindowProperty(
  114. disp(),
  115. rootWin,
  116. netClList,
  117. 0L,
  118. ~0L,
  119. false,
  120. AnyPropertyType,
  121. &actualType,
  122. &format,
  123. &num,
  124. &bytes,
  125. (uint8_t**)&data);
  126. if (status != Success) {
  127. blog(LOG_WARNING, "Failed getting root "
  128. "window properties");
  129. continue;
  130. }
  131. for (unsigned long i = 0; i < num; ++i)
  132. res.push_back(data[i]);
  133. XFree(data);
  134. }
  135. return res;
  136. }
  137. int getRootWindowScreen(Window root)
  138. {
  139. XWindowAttributes attr;
  140. if (!XGetWindowAttributes(disp(), root, &attr))
  141. return DefaultScreen(disp());
  142. return XScreenNumberOfScreen(attr.screen);
  143. }
  144. std::string getWindowAtom(Window win, const char *atom)
  145. {
  146. Atom netWmName = XInternAtom(disp(), atom, false);
  147. int n;
  148. char **list = 0;
  149. XTextProperty tp;
  150. std::string res = "unknown";
  151. XGetTextProperty(disp(), win, &tp, netWmName);
  152. if (!tp.nitems)
  153. XGetWMName(disp(), win, &tp);
  154. if (!tp.nitems)
  155. return "error";
  156. if (tp.encoding == XA_STRING) {
  157. res = (char*)tp.value;
  158. } else {
  159. int ret = XmbTextPropertyToTextList(disp(), &tp, &list,
  160. &n);
  161. if (ret >= Success && n > 0 && *list) {
  162. res = *list;
  163. XFreeStringList(list);
  164. }
  165. }
  166. char *conv = nullptr;
  167. if (os_mbs_to_utf8_ptr(res.c_str(), 0, &conv))
  168. res = conv;
  169. bfree(conv);
  170. XFree(tp.value);
  171. return res;
  172. }
  173. std::string getWindowCommand(Window win)
  174. {
  175. Atom xi = XInternAtom(disp(), "WM_COMMAND", false);
  176. int n;
  177. char **list = 0;
  178. XTextProperty tp;
  179. std::string res = "error";
  180. XGetTextProperty(disp(), win, &tp, xi);
  181. if (!tp.nitems)
  182. return std::string();
  183. if (tp.encoding == XA_STRING) {
  184. res = (char*)tp.value;
  185. } else {
  186. int ret = XmbTextPropertyToTextList(disp(), &tp, &list,
  187. &n);
  188. if (ret >= Success && n > 0 && *list) {
  189. res = *list;
  190. XFreeStringList(list);
  191. }
  192. }
  193. XFree(tp.value);
  194. return res;
  195. }
  196. int getWindowPid(Window win)
  197. {
  198. UNUSED_PARAMETER(win);
  199. return 1234; //TODO
  200. }
  201. static std::unordered_set<Window> changedWindows;
  202. static pthread_mutex_t changeLock = PTHREAD_MUTEX_INITIALIZER;
  203. void processEvents()
  204. {
  205. PLock lock(&changeLock);
  206. XLockDisplay(disp());
  207. while (XEventsQueued(disp(), QueuedAfterReading) > 0) {
  208. XEvent ev;
  209. XNextEvent(disp(), &ev);
  210. if (ev.type == ConfigureNotify)
  211. changedWindows.insert(ev.xconfigure.event);
  212. if (ev.type == MapNotify)
  213. changedWindows.insert(ev.xmap.event);
  214. if (ev.type == Expose)
  215. changedWindows.insert(ev.xexpose.window);
  216. if (ev.type == VisibilityNotify)
  217. changedWindows.insert(ev.xvisibility.window);
  218. if (ev.type == DestroyNotify)
  219. changedWindows.insert(ev.xdestroywindow.event);
  220. }
  221. XUnlockDisplay(disp());
  222. }
  223. bool windowWasReconfigured(Window win)
  224. {
  225. PLock lock(&changeLock);
  226. auto it = changedWindows.find(win);
  227. if (it != changedWindows.end()) {
  228. changedWindows.erase(it);
  229. return true;
  230. }
  231. return false;
  232. }
  233. }
  234. PLock::PLock(pthread_mutex_t* mtx, bool trylock)
  235. :m(mtx)
  236. {
  237. if (trylock)
  238. islock = mtx && pthread_mutex_trylock(mtx) == 0;
  239. else
  240. islock = mtx && pthread_mutex_lock(mtx) == 0;
  241. }
  242. PLock::~PLock()
  243. {
  244. if (islock) {
  245. pthread_mutex_unlock(m);
  246. }
  247. }
  248. bool PLock::isLocked()
  249. {
  250. return islock;
  251. }
  252. void PLock::unlock()
  253. {
  254. if (islock) {
  255. pthread_mutex_unlock(m);
  256. islock = false;
  257. }
  258. }
  259. void PLock::lock()
  260. {
  261. if (!islock) {
  262. pthread_mutex_lock(m);
  263. islock = true;
  264. }
  265. }
  266. static bool* curErrorTarget = 0;
  267. static char curErrorText[200];
  268. static int xerrorlock_handler(Display* disp, XErrorEvent* err)
  269. {
  270. if (curErrorTarget)
  271. *curErrorTarget = true;
  272. XGetErrorText(disp, err->error_code, curErrorText, 200);
  273. return 0;
  274. }
  275. XErrorLock::XErrorLock()
  276. {
  277. goterr = false;
  278. islock = false;
  279. prevhandler = 0;
  280. lock();
  281. }
  282. XErrorLock::~XErrorLock()
  283. {
  284. unlock();
  285. }
  286. bool XErrorLock::isLocked()
  287. {
  288. return islock;
  289. }
  290. void XErrorLock::lock()
  291. {
  292. if (!islock) {
  293. XLockDisplay(XCompcap::disp());
  294. XSync(XCompcap::disp(), 0);
  295. curErrorTarget = &goterr;
  296. curErrorText[0] = 0;
  297. prevhandler = XSetErrorHandler(xerrorlock_handler);
  298. islock = true;
  299. }
  300. }
  301. void XErrorLock::unlock()
  302. {
  303. if (islock) {
  304. XSync(XCompcap::disp(), 0);
  305. curErrorTarget = 0;
  306. XSetErrorHandler(prevhandler);
  307. prevhandler = 0;
  308. XUnlockDisplay(XCompcap::disp());
  309. islock = false;
  310. }
  311. }
  312. bool XErrorLock::gotError()
  313. {
  314. if (!islock)
  315. return false;
  316. XSync(XCompcap::disp(), 0);
  317. bool res = goterr;
  318. goterr = false;
  319. return res;
  320. }
  321. std::string XErrorLock::getErrorText()
  322. {
  323. return curErrorText;
  324. }
  325. void XErrorLock::resetError()
  326. {
  327. if (islock)
  328. XSync(XCompcap::disp(), 0);
  329. goterr = false;
  330. curErrorText[0] = 0;
  331. }
  332. XDisplayLock::XDisplayLock()
  333. {
  334. islock = false;
  335. lock();
  336. }
  337. XDisplayLock::~XDisplayLock()
  338. {
  339. unlock();
  340. }
  341. bool XDisplayLock::isLocked()
  342. {
  343. return islock;
  344. }
  345. void XDisplayLock::lock()
  346. {
  347. if (!islock) {
  348. XLockDisplay(XCompcap::disp());
  349. islock = true;
  350. }
  351. }
  352. void XDisplayLock::unlock()
  353. {
  354. if (islock) {
  355. XSync(XCompcap::disp(), 0);
  356. XUnlockDisplay(XCompcap::disp());
  357. islock = false;
  358. }
  359. }
  360. ObsGsContextHolder::ObsGsContextHolder()
  361. {
  362. obs_enter_graphics();
  363. }
  364. ObsGsContextHolder::~ObsGsContextHolder()
  365. {
  366. obs_leave_graphics();
  367. }