xcompcap-helper.cpp 6.9 KB

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