xcompcap-helper.cpp 7.2 KB

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