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.

323 lines
9.5KB

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