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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Carla Interposer for JACK Applications X11 control
  3. * Copyright (C) 2014-2019 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. #include <X11/Xlib.h>
  21. // --------------------------------------------------------------------------------------------------------------------
  22. struct ScopedLibOpen {
  23. void* handle;
  24. long long winId;
  25. ScopedLibOpen() noexcept
  26. : handle(dlopen("libjack.so.0", RTLD_NOW|RTLD_LOCAL)),
  27. winId(-1)
  28. {
  29. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  30. if (const char* const winIdStr = std::getenv("CARLA_FRONTEND_WIN_ID"))
  31. {
  32. CARLA_SAFE_ASSERT_RETURN(winIdStr[0] != '\0',);
  33. winId = std::strtoll(winIdStr, nullptr, 16);
  34. }
  35. }
  36. ~ScopedLibOpen() noexcept
  37. {
  38. if (handle != nullptr)
  39. {
  40. dlclose(handle);
  41. handle = nullptr;
  42. }
  43. }
  44. static const ScopedLibOpen& getInstance() noexcept
  45. {
  46. static const ScopedLibOpen slo;
  47. return slo;
  48. }
  49. CARLA_DECLARE_NON_COPY_STRUCT(ScopedLibOpen);
  50. };
  51. // --------------------------------------------------------------------------------------------------------------------
  52. // Function typedefs
  53. typedef int (*XWindowFunc)(Display*, Window);
  54. typedef int (*XNextEventFunc)(Display*, XEvent*);
  55. // --------------------------------------------------------------------------------------------------------------------
  56. // Current state
  57. typedef enum {
  58. WindowMapNone,
  59. WindowMapNormal,
  60. WindowMapRaised,
  61. WindowMapSubwindows
  62. } WindowMappingType;
  63. static Display* gCurrentlyMappedDisplay = nullptr;
  64. static Window gCurrentlyMappedWindow = 0;
  65. static CarlaInterposedCallback gInterposedCallback = nullptr;
  66. static uint gInterposedSessionManager = LIBJACK_SESSION_MANAGER_NONE;
  67. static uint gInterposedHints = 0x0;
  68. static WindowMappingType gCurrentWindowType = WindowMapNone;
  69. static bool gCurrentWindowMapped = false;
  70. static bool gCurrentWindowVisible = false;
  71. // --------------------------------------------------------------------------------------------------------------------
  72. // Calling the real functions
  73. static int real_XMapWindow(Display* display, Window window)
  74. {
  75. static const XWindowFunc func = (XWindowFunc)::dlsym(RTLD_NEXT, "XMapWindow");
  76. CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
  77. return func(display, window);
  78. }
  79. static int real_XMapRaised(Display* display, Window window)
  80. {
  81. static const XWindowFunc func = (XWindowFunc)::dlsym(RTLD_NEXT, "XMapRaised");
  82. CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
  83. return func(display, window);
  84. }
  85. static int real_XMapSubwindows(Display* display, Window window)
  86. {
  87. static const XWindowFunc func = (XWindowFunc)::dlsym(RTLD_NEXT, "XMapSubwindows");
  88. CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
  89. return func(display, window);
  90. }
  91. static int real_XUnmapWindow(Display* display, Window window)
  92. {
  93. static const XWindowFunc func = (XWindowFunc)::dlsym(RTLD_NEXT, "XUnmapWindow");
  94. CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
  95. return func(display, window);
  96. }
  97. static int real_XNextEvent(Display* display, XEvent* event)
  98. {
  99. static const XNextEventFunc func = (XNextEventFunc)::dlsym(RTLD_NEXT, "XNextEvent");
  100. CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0);
  101. return func(display, event);
  102. }
  103. // --------------------------------------------------------------------------------------------------------------------
  104. // Custom carla window handling
  105. static int carlaWindowMap(Display* const display, const Window window, const WindowMappingType fallbackFnType)
  106. {
  107. const ScopedLibOpen& slo(ScopedLibOpen::getInstance());
  108. for (;;)
  109. {
  110. if (slo.winId < 0)
  111. break;
  112. Atom atom;
  113. int atomFormat;
  114. unsigned char* atomPtrs;
  115. unsigned long numItems, ignored;
  116. const Atom wmWindowType = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
  117. if (XGetWindowProperty(display, window, wmWindowType, 0, ~0L, False, AnyPropertyType,
  118. &atom, &atomFormat, &numItems, &ignored, &atomPtrs) != Success)
  119. {
  120. carla_debug("carlaWindowMap(%p, %lu, %i) - XGetWindowProperty failed", display, window, fallbackFnType);
  121. break;
  122. }
  123. const Atom* const atomValues = (const Atom*)atomPtrs;
  124. bool isMainWindow = (numItems == 0);
  125. for (ulong i=0; i<numItems; ++i)
  126. {
  127. const char* const atomValue(XGetAtomName(display, atomValues[i]));
  128. CARLA_SAFE_ASSERT_CONTINUE(atomValue != nullptr && atomValue[0] != '\0');
  129. if (std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_COMBO" ) == 0 ||
  130. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_DIALOG" ) == 0 ||
  131. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_DND" ) == 0 ||
  132. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_DOCK" ) == 0 ||
  133. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU") == 0 ||
  134. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_MENU" ) == 0 ||
  135. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_NOTIFICATION" ) == 0 ||
  136. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_POPUP_MENU" ) == 0 ||
  137. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_SPLASH" ) == 0 ||
  138. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_TOOLBAR" ) == 0 ||
  139. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_TOOLTIP" ) == 0 ||
  140. std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_UTILITY" ) == 0)
  141. {
  142. isMainWindow = false;
  143. continue;
  144. }
  145. if (std::strcmp(atomValue, "_NET_WM_WINDOW_TYPE_NORMAL") == 0)
  146. {
  147. // window is good, use it if no other types are set
  148. isMainWindow = true;
  149. }
  150. else
  151. {
  152. carla_stdout("=======================================> %s", atomValue);
  153. }
  154. }
  155. if (! isMainWindow)
  156. {
  157. // this has always bothered me...
  158. if (gCurrentlyMappedWindow != 0 && gCurrentWindowMapped && gCurrentWindowVisible)
  159. XSetTransientForHint(display, window, gCurrentlyMappedWindow);
  160. break;
  161. }
  162. Window transientWindow = 0;
  163. if (XGetTransientForHint(display, window, &transientWindow) == Success && transientWindow != 0)
  164. {
  165. carla_stdout("Window has transient set already, ignoring it");
  166. break;
  167. }
  168. // got a new window, we may need to forget last one
  169. if (gCurrentlyMappedDisplay != nullptr && gCurrentlyMappedWindow != 0)
  170. {
  171. // ignore requests against the current mapped window
  172. if (gCurrentlyMappedWindow == window)
  173. return 0;
  174. // we already have a mapped window, with carla visible button on, should be a dialog of sorts..
  175. if (gCurrentWindowMapped && gCurrentWindowVisible)
  176. {
  177. XSetTransientForHint(display, window, gCurrentlyMappedWindow);
  178. break;
  179. }
  180. // ignore empty windows created after the main one
  181. if (numItems == 0)
  182. break;
  183. carla_stdout("NOTICE: XMapWindow now showing previous window");
  184. switch (gCurrentWindowType)
  185. {
  186. case WindowMapNone:
  187. break;
  188. case WindowMapNormal:
  189. real_XMapWindow(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  190. break;
  191. case WindowMapRaised:
  192. real_XMapRaised(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  193. break;
  194. case WindowMapSubwindows:
  195. real_XMapSubwindows(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  196. break;
  197. }
  198. }
  199. gCurrentlyMappedDisplay = display;
  200. gCurrentlyMappedWindow = window;
  201. gCurrentWindowMapped = true;
  202. gCurrentWindowType = fallbackFnType;
  203. if (slo.winId > 0)
  204. XSetTransientForHint(display, window, static_cast<Window>(slo.winId));
  205. if (gCurrentWindowVisible)
  206. {
  207. carla_stdout("JACK application window found, showing it now");
  208. break;
  209. }
  210. gCurrentWindowMapped = false;
  211. carla_stdout("JACK application window found and captured");
  212. return 0;
  213. }
  214. carla_debug("carlaWindowMap(%p, %lu, %i) - not captured", display, window, fallbackFnType);
  215. switch (fallbackFnType)
  216. {
  217. case 1:
  218. return real_XMapWindow(display, window);
  219. case 2:
  220. return real_XMapRaised(display, window);
  221. case 3:
  222. return real_XMapSubwindows(display, window);
  223. default:
  224. return 0;
  225. }
  226. }
  227. // --------------------------------------------------------------------------------------------------------------------
  228. // Our custom X11 functions
  229. CARLA_EXPORT
  230. int XMapWindow(Display* display, Window window)
  231. {
  232. carla_debug("XMapWindow(%p, %lu)", display, window);
  233. return carlaWindowMap(display, window, WindowMapNormal);
  234. }
  235. CARLA_EXPORT
  236. int XMapRaised(Display* display, Window window)
  237. {
  238. carla_debug("XMapRaised(%p, %lu)", display, window);
  239. return carlaWindowMap(display, window, WindowMapRaised);
  240. }
  241. CARLA_EXPORT
  242. int XMapSubwindows(Display* display, Window window)
  243. {
  244. carla_debug("XMapSubwindows(%p, %lu)", display, window);
  245. return carlaWindowMap(display, window, WindowMapSubwindows);
  246. }
  247. CARLA_EXPORT
  248. int XUnmapWindow(Display* display, Window window)
  249. {
  250. carla_debug("XUnmapWindow(%p, %lu)", display, window);
  251. if (gCurrentlyMappedWindow == window)
  252. {
  253. gCurrentlyMappedDisplay = nullptr;
  254. gCurrentlyMappedWindow = 0;
  255. gCurrentWindowType = WindowMapNone;
  256. gCurrentWindowMapped = false;
  257. gCurrentWindowVisible = false;
  258. if (gInterposedCallback != nullptr)
  259. gInterposedCallback(LIBJACK_INTERPOSER_CALLBACK_UI_HIDE, nullptr);
  260. }
  261. return real_XUnmapWindow(display, window);
  262. }
  263. CARLA_EXPORT
  264. int XNextEvent(Display* display, XEvent* event)
  265. {
  266. const int ret = real_XNextEvent(display, event);
  267. if (ret != 0)
  268. return ret;
  269. if (gCurrentlyMappedWindow == 0)
  270. return ret;
  271. if (event->type != ClientMessage)
  272. return ret;
  273. if (event->xclient.window != gCurrentlyMappedWindow)
  274. return ret;
  275. char* const type = XGetAtomName(display, event->xclient.message_type);
  276. CARLA_SAFE_ASSERT_RETURN(type != nullptr, 0);
  277. if (std::strcmp(type, "WM_PROTOCOLS") != 0)
  278. return ret;
  279. if ((Atom)event->xclient.data.l[0] != XInternAtom(display, "WM_DELETE_WINDOW", False))
  280. return ret;
  281. gCurrentWindowVisible = false;
  282. gCurrentWindowMapped = false;
  283. if (gInterposedCallback != nullptr)
  284. gInterposedCallback(LIBJACK_INTERPOSER_CALLBACK_UI_HIDE, nullptr);
  285. event->type = 0;
  286. carla_stdout("XNextEvent close event catched, hiding UI instead");
  287. return real_XUnmapWindow(display, gCurrentlyMappedWindow);
  288. }
  289. // --------------------------------------------------------------------------------------------------------------------
  290. // Full control helper
  291. CARLA_EXPORT
  292. int jack_carla_interposed_action(uint action, uint value, void* ptr)
  293. {
  294. carla_debug("jack_carla_interposed_action(%i, %i, %p)", action, value, ptr);
  295. switch (action)
  296. {
  297. case LIBJACK_INTERPOSER_ACTION_SET_HINTS_AND_CALLBACK:
  298. gInterposedHints = value;
  299. gInterposedCallback = (CarlaInterposedCallback)ptr;
  300. return 1;
  301. case LIBJACK_INTERPOSER_ACTION_SET_SESSION_MANAGER:
  302. gInterposedSessionManager = value;
  303. return 1;
  304. case LIBJACK_INTERPOSER_ACTION_SHOW_HIDE_GUI:
  305. // show gui
  306. if (value != 0)
  307. {
  308. gCurrentWindowVisible = true;
  309. if (gCurrentlyMappedDisplay == nullptr || gCurrentlyMappedWindow == 0)
  310. {
  311. carla_stdout("NOTICE: Interposer show-gui request ignored");
  312. return 0;
  313. }
  314. gCurrentWindowMapped = true;
  315. switch (gCurrentWindowType)
  316. {
  317. case WindowMapNormal:
  318. return real_XMapWindow(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  319. case WindowMapRaised:
  320. return real_XMapRaised(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  321. case WindowMapSubwindows:
  322. return real_XMapSubwindows(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  323. default:
  324. return 0;
  325. }
  326. }
  327. // hide gui
  328. else
  329. {
  330. gCurrentWindowVisible = false;
  331. if (gCurrentlyMappedDisplay == nullptr || gCurrentlyMappedWindow == 0)
  332. {
  333. carla_stdout("NOTICE: Interposer hide-gui request ignored");
  334. return 0;
  335. }
  336. gCurrentWindowMapped = false;
  337. return real_XUnmapWindow(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  338. }
  339. break;
  340. case LIBJACK_INTERPOSER_ACTION_CLOSE_EVERYTHING:
  341. gCurrentWindowType = WindowMapNone;
  342. gCurrentWindowMapped = false;
  343. gCurrentWindowVisible = false;
  344. gCurrentlyMappedDisplay = nullptr;
  345. gCurrentlyMappedWindow = 0;
  346. return 0;
  347. }
  348. return -1;
  349. }
  350. // --------------------------------------------------------------------------------------------------------------------