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.

CarlaPluginUI.cpp 14KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Carla Plugin UI
  3. * Copyright (C) 2014 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 "CarlaPluginUI.hpp"
  18. #ifdef HAVE_X11
  19. # include <sys/types.h>
  20. # include <X11/Xatom.h>
  21. # include <X11/Xlib.h>
  22. # include <X11/Xutil.h>
  23. #endif
  24. #ifdef HAVE_X11
  25. # include "CarlaPluginUI_X11Icon.hpp"
  26. // -----------------------------------------------------
  27. // X11
  28. typedef void (*EventProcPtr)(XEvent* ev);
  29. static const int X11Key_Escape = 9;
  30. static bool gErrorTriggered = false;
  31. static int temporaryErrorHandler(Display*, XErrorEvent*)
  32. {
  33. gErrorTriggered = true;
  34. return 0;
  35. }
  36. class X11PluginUi : public CarlaPluginUi
  37. {
  38. public:
  39. X11PluginUi(CloseCallback* const cb, const uintptr_t parentId) noexcept
  40. : CarlaPluginUi(cb),
  41. fDisplay(nullptr),
  42. fWindow(0),
  43. fIsVisible(false),
  44. fFirstShow(true),
  45. fEventProc(nullptr)
  46. {
  47. fDisplay = XOpenDisplay(nullptr);
  48. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  49. const int screen = DefaultScreen(fDisplay);
  50. XSetWindowAttributes attr;
  51. carla_zeroStruct<XSetWindowAttributes>(attr);
  52. attr.border_pixel = 0;
  53. attr.event_mask = KeyPressMask|KeyReleaseMask;
  54. fWindow = XCreateWindow(fDisplay, RootWindow(fDisplay, screen),
  55. 0, 0, 300, 300, 0,
  56. DefaultDepth(fDisplay, screen),
  57. InputOutput,
  58. DefaultVisual(fDisplay, screen),
  59. CWBorderPixel|CWEventMask, &attr);
  60. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  61. XGrabKey(fDisplay, X11Key_Escape, AnyModifier, fWindow, 1, GrabModeAsync, GrabModeAsync);
  62. Atom wmDelete = XInternAtom(fDisplay, "WM_DELETE_WINDOW", True);
  63. XSetWMProtocols(fDisplay, fWindow, &wmDelete, 1);
  64. const pid_t pid = getpid();
  65. const Atom _nwp = XInternAtom(fDisplay, "_NET_WM_PID", False);
  66. XChangeProperty(fDisplay, fWindow, _nwp, XA_CARDINAL, 32, PropModeReplace, (const uchar*)&pid, 1);
  67. const Atom _nwi = XInternAtom(fDisplay, "_NET_WM_ICON", False);
  68. XChangeProperty(fDisplay, fWindow, _nwi, XA_CARDINAL, 32, PropModeReplace, (const uchar*)sCarlaX11Icon, sCarlaX11IconSize);
  69. if (parentId != 0)
  70. setTransientWinId(parentId);
  71. }
  72. ~X11PluginUi() override
  73. {
  74. CARLA_SAFE_ASSERT(! fIsVisible);
  75. if (fIsVisible)
  76. {
  77. XUnmapWindow(fDisplay, fWindow);
  78. fIsVisible = false;
  79. }
  80. if (fWindow != 0)
  81. {
  82. XDestroyWindow(fDisplay, fWindow);
  83. fWindow = 0;
  84. }
  85. if (fDisplay != nullptr)
  86. {
  87. XCloseDisplay(fDisplay);
  88. fDisplay = nullptr;
  89. }
  90. }
  91. void show() override
  92. {
  93. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  94. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  95. if (fFirstShow)
  96. {
  97. if (const Window childWindow = getChildWindow())
  98. {
  99. const Atom _xevp = XInternAtom(fDisplay, "_XEventProc", False);
  100. gErrorTriggered = false;
  101. const XErrorHandler oldErrorHandler(XSetErrorHandler(temporaryErrorHandler));
  102. Atom actualType;
  103. int actualFormat;
  104. ulong nitems, bytesAfter;
  105. uchar* data = nullptr;
  106. XGetWindowProperty(fDisplay, childWindow, _xevp, 0, 1, False, AnyPropertyType, &actualType, &actualFormat, &nitems, &bytesAfter, &data);
  107. XSetErrorHandler(oldErrorHandler);
  108. if (nitems == 1 && ! gErrorTriggered)
  109. {
  110. fEventProc = *reinterpret_cast<EventProcPtr*>(data);
  111. XMapRaised(fDisplay, childWindow);
  112. }
  113. }
  114. }
  115. fIsVisible = true;
  116. fFirstShow = false;
  117. XMapRaised(fDisplay, fWindow);
  118. XFlush(fDisplay);
  119. }
  120. void hide() override
  121. {
  122. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  123. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  124. fIsVisible = false;
  125. XUnmapWindow(fDisplay, fWindow);
  126. XFlush(fDisplay);
  127. }
  128. void idle() override
  129. {
  130. // prevent recursion
  131. if (fIsIdling) return;
  132. fIsIdling = true;
  133. for (XEvent event; XPending(fDisplay) > 0;)
  134. {
  135. XNextEvent(fDisplay, &event);
  136. if (! fIsVisible)
  137. continue;
  138. char* type = nullptr;
  139. switch (event.type)
  140. {
  141. case ClientMessage:
  142. type = XGetAtomName(fDisplay, event.xclient.message_type);
  143. CARLA_SAFE_ASSERT_CONTINUE(type != nullptr);
  144. if (std::strcmp(type, "WM_PROTOCOLS") == 0)
  145. {
  146. fIsVisible = false;
  147. CARLA_SAFE_ASSERT_CONTINUE(fCallback != nullptr);
  148. fCallback->handlePluginUiClosed();
  149. }
  150. break;
  151. case KeyRelease:
  152. if (event.xkey.keycode == X11Key_Escape)
  153. {
  154. fIsVisible = false;
  155. CARLA_SAFE_ASSERT_CONTINUE(fCallback != nullptr);
  156. fCallback->handlePluginUiClosed();
  157. }
  158. break;
  159. }
  160. if (type != nullptr)
  161. XFree(type);
  162. else if (fEventProc != nullptr)
  163. fEventProc(&event);
  164. }
  165. fIsIdling = false;
  166. }
  167. void focus() override
  168. {
  169. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  170. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  171. XRaiseWindow(fDisplay, fWindow);
  172. XSetInputFocus(fDisplay, fWindow, RevertToPointerRoot, CurrentTime);
  173. XFlush(fDisplay);
  174. }
  175. void setSize(const uint width, const uint height, const bool forceUpdate) override
  176. {
  177. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  178. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  179. XResizeWindow(fDisplay, fWindow, width, height);
  180. XSizeHints sizeHints;
  181. carla_zeroStruct<XSizeHints>(sizeHints);
  182. sizeHints.flags = PSize|PMinSize|PMaxSize;
  183. sizeHints.width = static_cast<int>(width);
  184. sizeHints.height = static_cast<int>(height);
  185. sizeHints.min_width = static_cast<int>(width);
  186. sizeHints.min_height = static_cast<int>(height);
  187. sizeHints.max_width = static_cast<int>(width);
  188. sizeHints.max_height = static_cast<int>(height);
  189. XSetNormalHints(fDisplay, fWindow, &sizeHints);
  190. if (forceUpdate)
  191. XFlush(fDisplay);
  192. }
  193. void setTitle(const char* const title) override
  194. {
  195. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  196. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  197. XStoreName(fDisplay, fWindow, title);
  198. }
  199. void setTransientWinId(const uintptr_t winId) override
  200. {
  201. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  202. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  203. XSetTransientForHint(fDisplay, fWindow, static_cast<Window>(winId));
  204. }
  205. void* getPtr() const noexcept
  206. {
  207. return (void*)fWindow;
  208. }
  209. void* getDisplay() const noexcept
  210. {
  211. return fDisplay;
  212. }
  213. protected:
  214. Window getChildWindow() const
  215. {
  216. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr, 0);
  217. CARLA_SAFE_ASSERT_RETURN(fWindow != 0, 0);
  218. Window rootWindow, parentWindow, ret = 0;
  219. Window* childWindows = nullptr;
  220. uint numChildren = 0;
  221. XQueryTree(fDisplay, fWindow, &rootWindow, &parentWindow, &childWindows, &numChildren);
  222. if (numChildren > 0 && childWindows != nullptr)
  223. {
  224. ret = childWindows[0];
  225. XFree(childWindows);
  226. }
  227. return ret;
  228. }
  229. private:
  230. Display* fDisplay;
  231. Window fWindow;
  232. bool fIsVisible;
  233. bool fFirstShow;
  234. EventProcPtr fEventProc;
  235. };
  236. #endif
  237. // -----------------------------------------------------
  238. bool CarlaPluginUi::tryTransientWinIdMatch(const uintptr_t pid, const char* const uiTitle, const uintptr_t winId)
  239. {
  240. CARLA_SAFE_ASSERT_RETURN(uiTitle != nullptr && uiTitle[0] != '\0', true);
  241. CARLA_SAFE_ASSERT_RETURN(winId != 0, true);
  242. #if defined(CARLA_OS_MAC)
  243. return true;
  244. (void)pid;
  245. #elif defined(CARLA_OS_WIN)
  246. return true;
  247. (void)pid;
  248. #elif defined(HAVE_X11)
  249. struct ScopedDisplay {
  250. Display* display;
  251. ScopedDisplay() : display(XOpenDisplay(nullptr)) {}
  252. ~ScopedDisplay() { if (display!=nullptr) XCloseDisplay(display); }
  253. };
  254. struct ScopedFreeData {
  255. union {
  256. char* data;
  257. uchar* udata;
  258. };
  259. ScopedFreeData(char* d) : data(d) {}
  260. ScopedFreeData(uchar* d) : udata(d) {}
  261. ~ScopedFreeData() { XFree(data); }
  262. };
  263. const ScopedDisplay sd;
  264. CARLA_SAFE_ASSERT_RETURN(sd.display != nullptr, true);
  265. const Atom _ncl = XInternAtom(sd.display, "_NET_CLIENT_LIST" , False);
  266. const Atom _nwn = XInternAtom(sd.display, "_NET_WM_NAME", False);
  267. const Atom _nwp = XInternAtom(sd.display, "_NET_WM_PID", False);
  268. const Atom utf8 = XInternAtom(sd.display, "UTF8_STRING", True);
  269. Atom actualType;
  270. int actualFormat;
  271. ulong numWindows, bytesAfter;
  272. uchar* data = nullptr;
  273. int status = XGetWindowProperty(sd.display, DefaultRootWindow(sd.display), _ncl, 0L, (~0L), False, AnyPropertyType, &actualType, &actualFormat, &numWindows, &bytesAfter, &data);
  274. CARLA_SAFE_ASSERT_RETURN(data != nullptr, true);
  275. const ScopedFreeData sfd(data);
  276. CARLA_SAFE_ASSERT_RETURN(status == Success, true);
  277. CARLA_SAFE_ASSERT_RETURN(actualFormat == 32, true);
  278. CARLA_SAFE_ASSERT_RETURN(numWindows != 0, true);
  279. Window* windows = (Window*)data;
  280. Window lastGoodWindow = 0;
  281. for (ulong i = 0; i < numWindows; i++)
  282. {
  283. const Window window(windows[i]);
  284. CARLA_SAFE_ASSERT_CONTINUE(window != 0);
  285. // ------------------------------------------------
  286. // try using pid
  287. if (pid != 0)
  288. {
  289. ulong pidSize;
  290. uchar* pidData = nullptr;
  291. status = XGetWindowProperty(sd.display, window, _nwp, 0L, (~0L), False, XA_CARDINAL, &actualType, &actualFormat, &pidSize, &bytesAfter, &pidData);
  292. if (pidData != nullptr)
  293. {
  294. const ScopedFreeData sfd2(pidData);
  295. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  296. CARLA_SAFE_ASSERT_CONTINUE(pidSize != 0);
  297. if (*(ulong*)pidData == static_cast<ulong>(pid))
  298. {
  299. CARLA_SAFE_ASSERT_RETURN(lastGoodWindow == window || lastGoodWindow == 0, true);
  300. lastGoodWindow = window;
  301. carla_stdout("Match found using pid");
  302. break;
  303. }
  304. }
  305. }
  306. // ------------------------------------------------
  307. // try using name (UTF-8)
  308. ulong nameSize;
  309. uchar* nameData = nullptr;
  310. status = XGetWindowProperty(sd.display, window, _nwn, 0L, (~0L), False, utf8, &actualType, &actualFormat, &nameSize, &bytesAfter, &nameData);
  311. if (nameData != nullptr)
  312. {
  313. const ScopedFreeData sfd2(nameData);
  314. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  315. CARLA_SAFE_ASSERT_CONTINUE(nameSize != 0);
  316. if (std::strstr((const char*)nameData, uiTitle) != nullptr)
  317. {
  318. CARLA_SAFE_ASSERT_RETURN(lastGoodWindow == window || lastGoodWindow == 0, true);
  319. lastGoodWindow = window;
  320. carla_stdout("Match found using UTF-8 name");
  321. }
  322. }
  323. // ------------------------------------------------
  324. // try using name (simple)
  325. char* wmName = nullptr;
  326. status = XFetchName(sd.display, window, &wmName);
  327. if (wmName != nullptr)
  328. {
  329. const ScopedFreeData sfd2(wmName);
  330. CARLA_SAFE_ASSERT_CONTINUE(status != 0);
  331. if (std::strstr(wmName, uiTitle) != nullptr)
  332. {
  333. CARLA_SAFE_ASSERT_RETURN(lastGoodWindow == window || lastGoodWindow == 0, true);
  334. lastGoodWindow = window;
  335. carla_stdout("Match found using simple name");
  336. }
  337. }
  338. }
  339. if (lastGoodWindow == 0)
  340. return false;
  341. const Atom _nwt = XInternAtom(sd.display ,"_NET_WM_STATE", False);
  342. const Atom _nws[2] = {
  343. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_TASKBAR", False),
  344. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_PAGER", False)
  345. };
  346. XChangeProperty(sd.display, lastGoodWindow, _nwt, XA_ATOM, 32, PropModeAppend, (const uchar*)_nws, 2);
  347. const Atom _nwi = XInternAtom(sd.display, "_NET_WM_ICON", False);
  348. XChangeProperty(sd.display, lastGoodWindow, _nwi, XA_CARDINAL, 32, PropModeReplace, (const uchar*)sCarlaX11Icon, sCarlaX11IconSize);
  349. XSetTransientForHint(sd.display, lastGoodWindow, (Window)winId);
  350. XRaiseWindow(sd.display, lastGoodWindow);
  351. XSetInputFocus(sd.display, lastGoodWindow, RevertToPointerRoot, CurrentTime);
  352. XFlush(sd.display);
  353. return true;
  354. #else
  355. return true;
  356. (void)pid;
  357. #endif
  358. }
  359. // -----------------------------------------------------
  360. #ifdef CARLA_OS_MAC
  361. CarlaPluginUi* CarlaPluginUi::newCocoa(CloseCallback*, uintptr_t)
  362. {
  363. //return new CocoaPluginUi(cb, parentId);
  364. return nullptr;
  365. }
  366. #endif
  367. #ifdef CARLA_OS_WIN
  368. CarlaPluginUi* CarlaPluginUi::newWindows(CloseCallback*, uintptr_t)
  369. {
  370. //return new WindowsPluginUi(cb, parentId);
  371. return nullptr;
  372. }
  373. #endif
  374. #ifdef HAVE_X11
  375. CarlaPluginUi* CarlaPluginUi::newX11(CloseCallback* cb, uintptr_t parentId)
  376. {
  377. return new X11PluginUi(cb, parentId);
  378. }
  379. #endif
  380. // -----------------------------------------------------