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.

506 lines
16KB

  1. /*
  2. * Carla Interposer for JACK Applications X11 control
  3. * Copyright (C) 2014-2022 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. carla_debug("carlaWindowMap(%p, %lu, %i) - not main window, ignoring", display, window, fallbackFnType);
  177. // this has always bothered me...
  178. if (gCurrentlyMappedWindow != 0 && gCurrentWindowMapped && gCurrentWindowVisible)
  179. XSetTransientForHint(display, window, gCurrentlyMappedWindow);
  180. break;
  181. }
  182. Window transientWindow = 0;
  183. if (XGetTransientForHint(display, window, &transientWindow) == Success && transientWindow != 0)
  184. {
  185. carla_stdout("Window has transient set already, ignoring it");
  186. break;
  187. }
  188. // got a new window, we may need to forget last one
  189. if (gCurrentlyMappedDisplay != nullptr && gCurrentlyMappedWindow != 0)
  190. {
  191. // ignore requests against the current mapped window
  192. if (gCurrentlyMappedWindow == window)
  193. {
  194. carla_debug("carlaWindowMap(%p, %lu, %i) - asked to show window, ignoring it",
  195. display, window, fallbackFnType);
  196. return 0;
  197. }
  198. // we already have a mapped window, with carla visible button on, should be a dialog of sorts..
  199. if (gCurrentWindowMapped && gCurrentWindowVisible)
  200. {
  201. XSetTransientForHint(display, window, gCurrentlyMappedWindow);
  202. break;
  203. }
  204. // ignore empty windows created after the main one
  205. if (numItems == 0)
  206. {
  207. carla_debug("carlaWindowMap(%p, %lu, %i) - ignoring empty window", display, window, fallbackFnType);
  208. break;
  209. }
  210. carla_stdout("NOTICE: XMapWindow now showing previous window");
  211. switch (gCurrentWindowType)
  212. {
  213. case WindowMapNone:
  214. break;
  215. case WindowMapNormal:
  216. real_XMapWindow(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  217. break;
  218. case WindowMapRaised:
  219. real_XMapRaised(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  220. break;
  221. case WindowMapSubwindows:
  222. real_XMapSubwindows(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  223. break;
  224. }
  225. }
  226. gCurrentlyMappedDisplay = display;
  227. gCurrentlyMappedWindow = window;
  228. gCurrentWindowMapped = true;
  229. gCurrentWindowType = fallbackFnType;
  230. if (slo.winId > 0)
  231. XSetTransientForHint(display, window, static_cast<Window>(slo.winId));
  232. if (gCurrentWindowVisible)
  233. {
  234. carla_stdout("JACK application window found, showing it now");
  235. break;
  236. }
  237. gCurrentWindowMapped = false;
  238. carla_stdout("JACK application window found and captured");
  239. if (gInterposedSessionManager == LIBJACK_SESSION_MANAGER_NSM && gSupportsOptionalGui)
  240. break;
  241. return 0;
  242. }
  243. carla_debug("carlaWindowMap(%p, %lu, %i) - not captured", display, window, fallbackFnType);
  244. switch (fallbackFnType)
  245. {
  246. case WindowMapNormal:
  247. return real_XMapWindow(display, window);
  248. case WindowMapRaised:
  249. return real_XMapRaised(display, window);
  250. case WindowMapSubwindows:
  251. return real_XMapSubwindows(display, window);
  252. default:
  253. return 0;
  254. }
  255. }
  256. static int carlaWindowUnmap(Display* const display, const Window window, const WindowUnmappingType fallbackFnType)
  257. {
  258. if (gCurrentlyMappedWindow == window)
  259. {
  260. carla_stdout("NOTICE: now hiding previous window");
  261. gCurrentlyMappedDisplay = nullptr;
  262. gCurrentlyMappedWindow = 0;
  263. gCurrentWindowType = WindowMapNone;
  264. gCurrentWindowMapped = false;
  265. gCurrentWindowVisible = false;
  266. if (gInterposedCallback != nullptr)
  267. gInterposedCallback(LIBJACK_INTERPOSER_CALLBACK_UI_HIDE, nullptr);
  268. }
  269. else
  270. {
  271. carla_debug("carlaWindowUnmap(%p, %lu, %i) - not captured", display, window, fallbackFnType);
  272. }
  273. switch (fallbackFnType)
  274. {
  275. case WindowUnmapNormal:
  276. return real_XUnmapWindow(display, window);
  277. case WindowUnmapDestroy:
  278. return real_XDestroyWindow(display, window);
  279. default:
  280. return 0;
  281. }
  282. }
  283. // --------------------------------------------------------------------------------------------------------------------
  284. // Our custom X11 functions
  285. CARLA_PLUGIN_EXPORT
  286. int XMapWindow(Display* display, Window window)
  287. {
  288. carla_debug("XMapWindow(%p, %lu)", display, window);
  289. return carlaWindowMap(display, window, WindowMapNormal);
  290. }
  291. CARLA_PLUGIN_EXPORT
  292. int XMapRaised(Display* display, Window window)
  293. {
  294. carla_debug("XMapRaised(%p, %lu)", display, window);
  295. return carlaWindowMap(display, window, WindowMapRaised);
  296. }
  297. CARLA_PLUGIN_EXPORT
  298. int XMapSubwindows(Display* display, Window window)
  299. {
  300. carla_debug("XMapSubwindows(%p, %lu)", display, window);
  301. return carlaWindowMap(display, window, WindowMapSubwindows);
  302. }
  303. CARLA_PLUGIN_EXPORT
  304. int XUnmapWindow(Display* display, Window window)
  305. {
  306. carla_debug("XUnmapWindow(%p, %lu)", display, window);
  307. return carlaWindowUnmap(display, window, WindowUnmapNormal);
  308. }
  309. CARLA_PLUGIN_EXPORT
  310. int XDestroyWindow(Display* display, Window window)
  311. {
  312. carla_debug("XDestroyWindow(%p, %lu)", display, window);
  313. return carlaWindowUnmap(display, window, WindowUnmapDestroy);
  314. }
  315. CARLA_PLUGIN_EXPORT
  316. int XNextEvent(Display* display, XEvent* event)
  317. {
  318. const int ret = real_XNextEvent(display, event);
  319. if ((gInterposedHints & LIBJACK_FLAG_CONTROL_WINDOW) == 0x0)
  320. return ret;
  321. if (gInterposedSessionManager == LIBJACK_SESSION_MANAGER_NSM && gSupportsOptionalGui)
  322. return ret;
  323. if (ret != 0)
  324. return ret;
  325. if (gCurrentlyMappedWindow == 0)
  326. return ret;
  327. if (event->type != ClientMessage)
  328. return ret;
  329. if (event->xclient.window != gCurrentlyMappedWindow)
  330. return ret;
  331. char* const type = XGetAtomName(display, event->xclient.message_type);
  332. CARLA_SAFE_ASSERT_RETURN(type != nullptr, 0);
  333. if (std::strcmp(type, "WM_PROTOCOLS") != 0)
  334. return ret;
  335. if ((Atom)event->xclient.data.l[0] != XInternAtom(display, "WM_DELETE_WINDOW", False))
  336. return ret;
  337. gCurrentWindowVisible = false;
  338. gCurrentWindowMapped = false;
  339. if (gInterposedCallback != nullptr)
  340. gInterposedCallback(LIBJACK_INTERPOSER_CALLBACK_UI_HIDE, nullptr);
  341. event->type = 0;
  342. carla_stdout("XNextEvent close event caught, hiding UI instead");
  343. return real_XUnmapWindow(display, gCurrentlyMappedWindow);
  344. }
  345. #endif // HAVE_X11
  346. // --------------------------------------------------------------------------------------------------------------------
  347. // Full control helper
  348. CARLA_PLUGIN_EXPORT
  349. int jack_carla_interposed_action(uint action, uint value, void* ptr)
  350. {
  351. carla_debug("jack_carla_interposed_action(%i, %i, %p)", action, value, ptr);
  352. switch (action)
  353. {
  354. case LIBJACK_INTERPOSER_ACTION_SET_HINTS_AND_CALLBACK:
  355. gInterposedHints = value;
  356. gInterposedCallback = (CarlaInterposedCallback)ptr;
  357. return 1;
  358. case LIBJACK_INTERPOSER_ACTION_SET_SESSION_MANAGER:
  359. gInterposedSessionManager = value;
  360. return 1;
  361. case LIBJACK_INTERPOSER_ACTION_SHOW_HIDE_GUI:
  362. #ifdef HAVE_X11
  363. gSupportsOptionalGui = false;
  364. #endif
  365. // show gui
  366. if (value != 0)
  367. {
  368. #ifdef HAVE_X11
  369. gCurrentWindowVisible = true;
  370. if (gCurrentlyMappedDisplay == nullptr || gCurrentlyMappedWindow == 0)
  371. #endif
  372. {
  373. carla_stdout("NOTICE: Interposer show-gui request ignored");
  374. return 0;
  375. }
  376. #ifdef HAVE_X11
  377. gCurrentWindowMapped = true;
  378. switch (gCurrentWindowType)
  379. {
  380. case WindowMapNormal:
  381. return real_XMapWindow(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  382. case WindowMapRaised:
  383. return real_XMapRaised(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  384. case WindowMapSubwindows:
  385. return real_XMapSubwindows(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  386. default:
  387. return 0;
  388. }
  389. #endif
  390. }
  391. // hide gui
  392. else
  393. {
  394. #ifdef HAVE_X11
  395. gCurrentWindowVisible = false;
  396. if (gCurrentlyMappedDisplay == nullptr || gCurrentlyMappedWindow == 0)
  397. #endif
  398. {
  399. carla_stdout("NOTICE: Interposer hide-gui request ignored");
  400. return 0;
  401. }
  402. #ifdef HAVE_X11
  403. gCurrentWindowMapped = false;
  404. return real_XUnmapWindow(gCurrentlyMappedDisplay, gCurrentlyMappedWindow);
  405. #endif
  406. }
  407. break;
  408. case LIBJACK_INTERPOSER_ACTION_CLOSE_EVERYTHING:
  409. gCurrentWindowType = WindowMapNone;
  410. gCurrentWindowMapped = false;
  411. gCurrentWindowVisible = false;
  412. #ifdef HAVE_X11
  413. gCurrentlyMappedDisplay = nullptr;
  414. gCurrentlyMappedWindow = 0;
  415. #endif
  416. return 0;
  417. }
  418. return -1;
  419. }
  420. // --------------------------------------------------------------------------------------------------------------------