Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

interposer-jack-x11.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * Carla Interposer for JACK Applications X11 control
  3. * Copyright (C) 2014-2020 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaLibJackHints.h"
  18. #include "CarlaUtils.hpp"
  19. #include <dlfcn.h>
  20. #ifdef HAVE_X11
  21. # include <X11/Xlib.h>
  22. #endif
  23. // --------------------------------------------------------------------------------------------------------------------
  24. struct ScopedLibOpen {
  25. void* handle;
  26. long long winId;
  27. ScopedLibOpen() noexcept
  28. : handle(dlopen("libjack.so.0", RTLD_NOW|RTLD_LOCAL)),
  29. winId(-1)
  30. {
  31. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  32. if (const char* const winIdStr = std::getenv("CARLA_FRONTEND_WIN_ID"))
  33. {
  34. CARLA_SAFE_ASSERT_RETURN(winIdStr[0] != '\0',);
  35. winId = std::strtoll(winIdStr, nullptr, 16);
  36. }
  37. }
  38. ~ScopedLibOpen() noexcept
  39. {
  40. if (handle != nullptr)
  41. {
  42. dlclose(handle);
  43. handle = nullptr;
  44. }
  45. }
  46. static const ScopedLibOpen& getInstance() noexcept
  47. {
  48. static const ScopedLibOpen slo;
  49. return slo;
  50. }
  51. CARLA_DECLARE_NON_COPY_STRUCT(ScopedLibOpen);
  52. };
  53. // --------------------------------------------------------------------------------------------------------------------
  54. // Current state
  55. typedef enum {
  56. WindowMapNone,
  57. WindowMapNormal,
  58. WindowMapRaised,
  59. WindowMapSubwindows
  60. } WindowMappingType;
  61. typedef enum {
  62. WindowUnmapNone,
  63. WindowUnmapNormal,
  64. WindowUnmapDestroy
  65. } WindowUnmappingType;
  66. #ifdef HAVE_X11
  67. static Display* gCurrentlyMappedDisplay = nullptr;
  68. static Window gCurrentlyMappedWindow = 0;
  69. static bool gSupportsOptionalGui = true;
  70. #endif
  71. static CarlaInterposedCallback gInterposedCallback = nullptr;
  72. static uint gInterposedSessionManager = LIBJACK_SESSION_MANAGER_NONE;
  73. static uint gInterposedHints = 0x0;
  74. static WindowMappingType gCurrentWindowType = WindowMapNone;
  75. static bool gCurrentWindowMapped = false;
  76. static bool gCurrentWindowVisible = false;
  77. #ifdef HAVE_X11
  78. // --------------------------------------------------------------------------------------------------------------------
  79. // Function typedefs
  80. typedef int (*XWindowFunc)(Display*, Window);
  81. typedef int (*XNextEventFunc)(Display*, XEvent*);
  82. // --------------------------------------------------------------------------------------------------------------------
  83. // Calling the real X11 functions
  84. static int real_XMapWindow(Display* display, Window window)
  85. {
  86. static const XWindowFunc func = (XWindowFunc)::dlsym(RTLD_NEXT, "XMapWindow");
  87. CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
  88. return func(display, window);
  89. }
  90. static int real_XMapRaised(Display* display, Window window)
  91. {
  92. static const XWindowFunc func = (XWindowFunc)::dlsym(RTLD_NEXT, "XMapRaised");
  93. CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
  94. return func(display, window);
  95. }
  96. static int real_XMapSubwindows(Display* display, Window window)
  97. {
  98. static const XWindowFunc func = (XWindowFunc)::dlsym(RTLD_NEXT, "XMapSubwindows");
  99. CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
  100. return func(display, window);
  101. }
  102. static int real_XUnmapWindow(Display* display, Window window)
  103. {
  104. static const XWindowFunc func = (XWindowFunc)::dlsym(RTLD_NEXT, "XUnmapWindow");
  105. CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
  106. return func(display, window);
  107. }
  108. static int real_XDestroyWindow(Display* display, Window window)
  109. {
  110. static const XWindowFunc func = (XWindowFunc)::dlsym(RTLD_NEXT, "XDestroyWindow");
  111. CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
  112. return func(display, window);
  113. }
  114. static int real_XNextEvent(Display* display, XEvent* event)
  115. {
  116. static const XNextEventFunc func = (XNextEventFunc)::dlsym(RTLD_NEXT, "XNextEvent");
  117. CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
  118. return func(display, event);
  119. }
  120. // --------------------------------------------------------------------------------------------------------------------
  121. // Custom carla window handling
  122. static int carlaWindowMap(Display* const display, const Window window, const WindowMappingType fallbackFnType)
  123. {
  124. const ScopedLibOpen& slo(ScopedLibOpen::getInstance());
  125. for (;;)
  126. {
  127. if (slo.winId < 0)
  128. break;
  129. if ((gInterposedHints & LIBJACK_FLAG_CONTROL_WINDOW) == 0x0)
  130. break;
  131. Atom atom;
  132. int atomFormat;
  133. unsigned char* atomPtrs;
  134. unsigned long numItems, ignored;
  135. const Atom wmWindowType = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
  136. if (XGetWindowProperty(display, window, wmWindowType, 0, ~0L, False, AnyPropertyType,
  137. &atom, &atomFormat, &numItems, &ignored, &atomPtrs) != Success)
  138. {
  139. carla_debug("carlaWindowMap(%p, %lu, %i) - XGetWindowProperty failed", display, window, fallbackFnType);
  140. break;
  141. }
  142. const Atom* const atomValues = (const Atom*)atomPtrs;
  143. bool isMainWindow = (numItems == 0);
  144. for (ulong i=0; i<numItems; ++i)
  145. {
  146. const char* const atomValue(XGetAtomName(display, atomValues[i]));
  147. CARLA_SAFE_ASSERT_CONTINUE(atomValue != nullptr && atomValue[0] != '\0');
  148. if (std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_COMBO" ) == 0 ||
  149. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_DIALOG" ) == 0 ||
  150. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_DND" ) == 0 ||
  151. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_DOCK" ) == 0 ||
  152. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU") == 0 ||
  153. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_MENU" ) == 0 ||
  154. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_NOTIFICATION" ) == 0 ||
  155. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_POPUP_MENU" ) == 0 ||
  156. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_SPLASH" ) == 0 ||
  157. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_TOOLBAR" ) == 0 ||
  158. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_TOOLTIP" ) == 0 ||
  159. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_UTILITY" ) == 0)
  160. {
  161. isMainWindow = false;
  162. continue;
  163. }
  164. if (std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_NORMAL") == 0)
  165. {
  166. // window is good, use it if no other types are set
  167. isMainWindow = true;
  168. }
  169. else
  170. {
  171. carla_stdout("=======================================> %s", atomValue);
  172. }
  173. }
  174. if (! isMainWindow)
  175. {
  176. // this has always bothered me...
  177. if (gCurrentlyMappedWindow != 0 && gCurrentWindowMapped && gCurrentWindowVisible)
  178. XSetTransientForHint(display, window, gCurrentlyMappedWindow);
  179. break;
  180. }
  181. Window transientWindow = 0;
  182. if (XGetTransientForHint(display, window, &transientWindow) == Success && transientWindow != 0)
  183. {
  184. carla_stdout("Window has transient set already, ignoring it");
  185. break;
  186. }
  187. // got a new window, we may need to forget last one
  188. if (gCurrentlyMappedDisplay != nullptr && gCurrentlyMappedWindow != 0)
  189. {
  190. // ignore requests against the current mapped window
  191. if (gCurrentlyMappedWindow == window)
  192. return 0;
  193. if (gInterposedSessionManager != LIBJACK_SESSION_MANAGER_NSM || ! gSupportsOptionalGui)
  194. return 0;
  195. // we already have a mapped window, with carla visible button on, should be a dialog of sorts..
  196. if (gCurrentWindowMapped && gCurrentWindowVisible)
  197. {
  198. XSetTransientForHint(display, window, gCurrentlyMappedWindow);
  199. break;
  200. }
  201. // ignore empty windows created after the main one
  202. if (numItems == 0)
  203. break;
  204. carla_stdout("NOTICE: XMapWindow now showing previous window");
  205. switch (gCurrentWindowType)
  206. {
  207. case WindowMapNone:
  208. break;
  209. case WindowMapNormal:
  210. real_XMapWindow(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  211. break;
  212. case WindowMapRaised:
  213. real_XMapRaised(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  214. break;
  215. case WindowMapSubwindows:
  216. real_XMapSubwindows(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  217. break;
  218. }
  219. }
  220. gCurrentlyMappedDisplay = display;
  221. gCurrentlyMappedWindow = window;
  222. gCurrentWindowMapped = true;
  223. gCurrentWindowType = fallbackFnType;
  224. if (slo.winId > 0)
  225. XSetTransientForHint(display, window, static_cast<Window>(slo.winId));
  226. if (gCurrentWindowVisible)
  227. {
  228. carla_stdout("JACK application window found, showing it now");
  229. break;
  230. }
  231. gCurrentWindowMapped = false;
  232. carla_stdout("JACK application window found and captured");
  233. if (gInterposedSessionManager == LIBJACK_SESSION_MANAGER_NSM && gSupportsOptionalGui)
  234. break;
  235. return 0;
  236. }
  237. carla_debug("carlaWindowMap(%p, %lu, %i) - not captured", display, window, fallbackFnType);
  238. switch (fallbackFnType)
  239. {
  240. case WindowMapNormal:
  241. return real_XMapWindow(display, window);
  242. case WindowMapRaised:
  243. return real_XMapRaised(display, window);
  244. case WindowMapSubwindows:
  245. return real_XMapSubwindows(display, window);
  246. default:
  247. return 0;
  248. }
  249. }
  250. static int carlaWindowUnmap(Display* const display, const Window window, const WindowUnmappingType fallbackFnType)
  251. {
  252. if (gCurrentlyMappedWindow == window)
  253. {
  254. carla_stdout("NOTICE: now hiding previous window");
  255. gCurrentlyMappedDisplay = nullptr;
  256. gCurrentlyMappedWindow = 0;
  257. gCurrentWindowType = WindowMapNone;
  258. gCurrentWindowMapped = false;
  259. gCurrentWindowVisible = false;
  260. if (gInterposedCallback != nullptr)
  261. gInterposedCallback(LIBJACK_INTERPOSER_CALLBACK_UI_HIDE, nullptr);
  262. }
  263. else
  264. {
  265. carla_debug("carlaWindowUnmap(%p, %lu, %i) - not captured", display, window, fallbackFnType);
  266. }
  267. switch (fallbackFnType)
  268. {
  269. case WindowUnmapNormal:
  270. return real_XUnmapWindow(display, window);
  271. case WindowUnmapDestroy:
  272. return real_XDestroyWindow(display, window);
  273. default:
  274. return 0;
  275. }
  276. }
  277. // --------------------------------------------------------------------------------------------------------------------
  278. // Our custom X11 functions
  279. CARLA_EXPORT
  280. int XMapWindow(Display* display, Window window)
  281. {
  282. carla_debug("XMapWindow(%p, %lu)", display, window);
  283. return carlaWindowMap(display, window, WindowMapNormal);
  284. }
  285. CARLA_EXPORT
  286. int XMapRaised(Display* display, Window window)
  287. {
  288. carla_debug("XMapRaised(%p, %lu)", display, window);
  289. return carlaWindowMap(display, window, WindowMapRaised);
  290. }
  291. CARLA_EXPORT
  292. int XMapSubwindows(Display* display, Window window)
  293. {
  294. carla_debug("XMapSubwindows(%p, %lu)", display, window);
  295. return carlaWindowMap(display, window, WindowMapSubwindows);
  296. }
  297. CARLA_EXPORT
  298. int XUnmapWindow(Display* display, Window window)
  299. {
  300. carla_debug("XUnmapWindow(%p, %lu)", display, window);
  301. return carlaWindowUnmap(display, window, WindowUnmapNormal);
  302. }
  303. CARLA_EXPORT
  304. int XDestroyWindow(Display* display, Window window)
  305. {
  306. carla_debug("XDestroyWindow(%p, %lu)", display, window);
  307. return carlaWindowUnmap(display, window, WindowUnmapDestroy);
  308. }
  309. CARLA_EXPORT
  310. int XNextEvent(Display* display, XEvent* event)
  311. {
  312. const int ret = real_XNextEvent(display, event);
  313. if ((gInterposedHints & LIBJACK_FLAG_CONTROL_WINDOW) == 0x0)
  314. return ret;
  315. if (gInterposedSessionManager == LIBJACK_SESSION_MANAGER_NSM && gSupportsOptionalGui)
  316. return ret;
  317. if (ret != 0)
  318. return ret;
  319. if (gCurrentlyMappedWindow == 0)
  320. return ret;
  321. if (event->type != ClientMessage)
  322. return ret;
  323. if (event->xclient.window != gCurrentlyMappedWindow)
  324. return ret;
  325. char* const type = XGetAtomName(display, event->xclient.message_type);
  326. CARLA_SAFE_ASSERT_RETURN(type != nullptr, 0);
  327. if (std::strcmp(type, "WM_PROTOCOLS") != 0)
  328. return ret;
  329. if ((Atom)event->xclient.data.l[0] != XInternAtom(display, "WM_DELETE_WINDOW", False))
  330. return ret;
  331. gCurrentWindowVisible = false;
  332. gCurrentWindowMapped = false;
  333. if (gInterposedCallback != nullptr)
  334. gInterposedCallback(LIBJACK_INTERPOSER_CALLBACK_UI_HIDE, nullptr);
  335. event->type = 0;
  336. carla_stdout("XNextEvent close event caught, hiding UI instead");
  337. return real_XUnmapWindow(display, gCurrentlyMappedWindow);
  338. }
  339. #endif // HAVE_X11
  340. // --------------------------------------------------------------------------------------------------------------------
  341. // Full control helper
  342. CARLA_EXPORT
  343. int jack_carla_interposed_action(uint action, uint value, void* ptr)
  344. {
  345. carla_debug("jack_carla_interposed_action(%i, %i, %p)", action, value, ptr);
  346. switch (action)
  347. {
  348. case LIBJACK_INTERPOSER_ACTION_SET_HINTS_AND_CALLBACK:
  349. gInterposedHints = value;
  350. gInterposedCallback = (CarlaInterposedCallback)ptr;
  351. return 1;
  352. case LIBJACK_INTERPOSER_ACTION_SET_SESSION_MANAGER:
  353. gInterposedSessionManager = value;
  354. return 1;
  355. case LIBJACK_INTERPOSER_ACTION_SHOW_HIDE_GUI:
  356. #ifdef HAVE_X11
  357. gSupportsOptionalGui = false;
  358. #endif
  359. // show gui
  360. if (value != 0)
  361. {
  362. #ifdef HAVE_X11
  363. gCurrentWindowVisible = true;
  364. if (gCurrentlyMappedDisplay == nullptr || gCurrentlyMappedWindow == 0)
  365. #endif
  366. {
  367. carla_stdout("NOTICE: Interposer show-gui request ignored");
  368. return 0;
  369. }
  370. #ifdef HAVE_X11
  371. gCurrentWindowMapped = true;
  372. switch (gCurrentWindowType)
  373. {
  374. case WindowMapNormal:
  375. return real_XMapWindow(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  376. case WindowMapRaised:
  377. return real_XMapRaised(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  378. case WindowMapSubwindows:
  379. return real_XMapSubwindows(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  380. default:
  381. return 0;
  382. }
  383. #endif
  384. }
  385. // hide gui
  386. else
  387. {
  388. #ifdef HAVE_X11
  389. gCurrentWindowVisible = false;
  390. if (gCurrentlyMappedDisplay == nullptr || gCurrentlyMappedWindow == 0)
  391. #endif
  392. {
  393. carla_stdout("NOTICE: Interposer hide-gui request ignored");
  394. return 0;
  395. }
  396. #ifdef HAVE_X11
  397. gCurrentWindowMapped = false;
  398. return real_XUnmapWindow(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  399. #endif
  400. }
  401. break;
  402. case LIBJACK_INTERPOSER_ACTION_CLOSE_EVERYTHING:
  403. gCurrentWindowType = WindowMapNone;
  404. gCurrentWindowMapped = false;
  405. gCurrentWindowVisible = false;
  406. #ifdef HAVE_X11
  407. gCurrentlyMappedDisplay = nullptr;
  408. gCurrentlyMappedWindow = 0;
  409. #endif
  410. return 0;
  411. }
  412. return -1;
  413. }
  414. // --------------------------------------------------------------------------------------------------------------------