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.

316 lines
9.2KB

  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. #include "CarlaHost.h"
  19. #ifdef HAVE_X11
  20. # include <X11/Xatom.h>
  21. # include <X11/Xlib.h>
  22. # include <X11/Xutil.h>
  23. #endif
  24. #ifdef HAVE_X11
  25. // -----------------------------------------------------
  26. // X11
  27. class X11PluginUi : public CarlaPluginUi
  28. {
  29. public:
  30. X11PluginUi(CloseCallback* cb) noexcept
  31. : CarlaPluginUi(cb),
  32. fDisplay(nullptr),
  33. fWindow(0)
  34. {
  35. fDisplay = XOpenDisplay(0);
  36. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  37. const int screen = DefaultScreen(fDisplay);
  38. XSetWindowAttributes attr;
  39. carla_zeroStruct<XSetWindowAttributes>(attr);
  40. fWindow = XCreateWindow(fDisplay, RootWindow(fDisplay, screen),
  41. 0, 0, 300, 300, 0,
  42. DefaultDepth(fDisplay, screen),
  43. InputOutput,
  44. DefaultVisual(fDisplay, screen),
  45. CWBorderPixel | CWColormap | CWEventMask, &attr);
  46. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  47. Atom wmDelete = XInternAtom(fDisplay, "WM_DELETE_WINDOW", True);
  48. XSetWMProtocols(fDisplay, fWindow, &wmDelete, 1);
  49. if (const uintptr_t transientId = carla_standalone_get_transient_win_id())
  50. setTransientWinId(transientId);
  51. }
  52. ~X11PluginUi() override
  53. {
  54. if (fWindow != 0)
  55. {
  56. XDestroyWindow(fDisplay, fWindow);
  57. fWindow = 0;
  58. }
  59. if (fDisplay != nullptr)
  60. {
  61. XCloseDisplay(fDisplay);
  62. fDisplay = nullptr;
  63. }
  64. }
  65. void show() override
  66. {
  67. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  68. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  69. XMapRaised(fDisplay, fWindow);
  70. XFlush(fDisplay);
  71. }
  72. void hide() override
  73. {
  74. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  75. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  76. XUnmapWindow(fDisplay, fWindow);
  77. XFlush(fDisplay);
  78. }
  79. void idle() override
  80. {
  81. for (XEvent event; XPending(fDisplay) > 0;)
  82. {
  83. XNextEvent(fDisplay, &event);
  84. switch (event.type)
  85. {
  86. case ClientMessage:
  87. if (std::strcmp(XGetAtomName(fDisplay, event.xclient.message_type), "WM_PROTOCOLS") == 0)
  88. {
  89. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  90. fCallback->handlePluginUiClosed();
  91. }
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. }
  98. void focus() override
  99. {
  100. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  101. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  102. XRaiseWindow(fDisplay, fWindow);
  103. XSetInputFocus(fDisplay, fWindow, RevertToPointerRoot, CurrentTime);
  104. XFlush(fDisplay);
  105. }
  106. void setSize(const uint width, const uint height, const bool forceUpdate) override
  107. {
  108. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  109. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  110. XResizeWindow(fDisplay, fWindow, width, height);
  111. XSizeHints sizeHints;
  112. carla_zeroStruct<XSizeHints>(sizeHints);
  113. sizeHints.flags = PMinSize|PMaxSize;
  114. sizeHints.min_width = static_cast<int>(width);
  115. sizeHints.min_height = static_cast<int>(height);
  116. sizeHints.max_width = static_cast<int>(width);
  117. sizeHints.max_height = static_cast<int>(height);
  118. XSetNormalHints(fDisplay, fWindow, &sizeHints);
  119. if (forceUpdate)
  120. XFlush(fDisplay);
  121. }
  122. void setTitle(const char* const title) override
  123. {
  124. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  125. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  126. XStoreName(fDisplay, fWindow, title);
  127. }
  128. void setTransientWinId(const uintptr_t winId) override
  129. {
  130. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  131. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  132. XSetTransientForHint(fDisplay, fWindow, (Window)winId);
  133. }
  134. void* getPtr() const noexcept
  135. {
  136. return (void*)fWindow;
  137. }
  138. private:
  139. Display* fDisplay;
  140. Window fWindow;
  141. };
  142. #endif
  143. // -----------------------------------------------------
  144. bool CarlaPluginUi::tryTransientWinIdMatch(const ulong pid, const char* const uiTitle, const uintptr_t winId)
  145. {
  146. CARLA_SAFE_ASSERT_RETURN(uiTitle != nullptr && uiTitle[0] != '\0', true);
  147. CARLA_SAFE_ASSERT_RETURN(winId != 0, true);
  148. #if defined(CARLA_OS_MAC)
  149. #elif defined(CARLA_OS_WIN)
  150. #elif defined(HAVE_X11)
  151. struct ScopedDisplay {
  152. Display* display;
  153. ScopedDisplay() : display(XOpenDisplay(0)) {}
  154. ~ScopedDisplay() { if (display!=nullptr) XCloseDisplay(display); }
  155. };
  156. struct ScopedFreeData {
  157. uchar* data;
  158. ScopedFreeData(uchar* d) : data(d) {}
  159. ~ScopedFreeData() { XFree(data); }
  160. };
  161. const ScopedDisplay sd;
  162. CARLA_SAFE_ASSERT_RETURN(sd.display != nullptr, true);
  163. Atom _ncl = XInternAtom(sd.display, "_NET_CLIENT_LIST" , True);
  164. Atom _nwn = XInternAtom(sd.display, "_NET_WM_NAME", True);
  165. Atom _nwp = XInternAtom(sd.display, "_NET_WM_PID", True);
  166. Atom utf8 = XInternAtom(sd.display, "UTF8_STRING", True);
  167. Atom actualType;
  168. int actualFormat;
  169. unsigned long numWindows, bytesAfter;
  170. unsigned char* data = nullptr;
  171. int status = XGetWindowProperty(sd.display, DefaultRootWindow(sd.display), _ncl, 0L, (~0L), False, AnyPropertyType, &actualType, &actualFormat, &numWindows, &bytesAfter, &data);
  172. CARLA_SAFE_ASSERT_RETURN(data != nullptr, true);
  173. const ScopedFreeData sfd(data);
  174. CARLA_SAFE_ASSERT_RETURN(status == Success, true);
  175. CARLA_SAFE_ASSERT_RETURN(actualFormat == 32, true);
  176. CARLA_SAFE_ASSERT_RETURN(numWindows != 0, true);
  177. Window* windows = (Window*)data;
  178. Window lastGoodWindow = 0;
  179. for (ulong i = 0; i < numWindows; i++)
  180. {
  181. const Window window(windows[i]);
  182. CARLA_SAFE_ASSERT_CONTINUE(window != 0);
  183. // ------------------------------------------------
  184. // try using pid
  185. if (pid != 0)
  186. {
  187. unsigned long pidSize;
  188. unsigned char* pidData = nullptr;
  189. status = XGetWindowProperty(sd.display, window, _nwp, 0L, (~0L), False, XA_CARDINAL, &actualType, &actualFormat, &pidSize, &bytesAfter, &pidData);
  190. if (pidData != nullptr)
  191. {
  192. const ScopedFreeData sfd2(pidData);
  193. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  194. CARLA_SAFE_ASSERT_CONTINUE(pidSize != 0);
  195. if (*(ulong*)pidData == pid)
  196. {
  197. CARLA_SAFE_ASSERT_RETURN(lastGoodWindow == window || lastGoodWindow == 0, true);
  198. lastGoodWindow = window;
  199. carla_stdout("Match found using pid");
  200. break;
  201. }
  202. }
  203. }
  204. // ------------------------------------------------
  205. // try using name
  206. unsigned long nameSize;
  207. unsigned char* nameData = nullptr;
  208. status = XGetWindowProperty(sd.display, window, _nwn, 0L, (~0L), False, utf8, &actualType, &actualFormat, &nameSize, &bytesAfter, &nameData);
  209. if (nameData != nullptr)
  210. {
  211. const ScopedFreeData sfd2(nameData);
  212. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  213. CARLA_SAFE_ASSERT_CONTINUE(nameSize != 0);
  214. if (std::strstr((const char*)nameData, uiTitle) != nullptr)
  215. {
  216. CARLA_SAFE_ASSERT_RETURN(lastGoodWindow == window || lastGoodWindow == 0, true);
  217. lastGoodWindow = window;
  218. carla_stdout("Match found using name");
  219. }
  220. }
  221. }
  222. if (lastGoodWindow == 0)
  223. return false;
  224. Atom _nwt = XInternAtom(sd.display ,"_NET_WM_STATE", True);
  225. Atom _nws[2];
  226. _nws[0] = XInternAtom(sd.display, "_NET_WM_STATE_SKIP_TASKBAR", True);
  227. _nws[1] = XInternAtom(sd.display, "_NET_WM_STATE_SKIP_PAGER", True);
  228. XChangeProperty(sd.display, lastGoodWindow, _nwt, XA_ATOM, 32, PropModeAppend, (uchar*)_nws, 2);
  229. XSetTransientForHint(sd.display, lastGoodWindow, (Window)winId);
  230. XFlush(sd.display);
  231. return true;
  232. #endif
  233. }
  234. // -----------------------------------------------------
  235. #ifdef CARLA_OS_MAC
  236. CarlaPluginUi* CarlaPluginUi::newCocoa(CloseCallback* cb)
  237. {
  238. return new CocoaPluginUi(cb);
  239. }
  240. #endif
  241. #ifdef CARLA_OS_WIN
  242. CarlaPluginUi* CarlaPluginUi::newWindows(CloseCallback* cb)
  243. {
  244. return new WindowsPluginUi(cb);
  245. }
  246. #endif
  247. #ifdef HAVE_X11
  248. CarlaPluginUi* CarlaPluginUi::newX11(CloseCallback* cb)
  249. {
  250. return new X11PluginUi(cb);
  251. }
  252. #endif
  253. // -----------------------------------------------------