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.

934 lines
27KB

  1. /*
  2. * Carla Plugin UI
  3. * Copyright (C) 2014-2017 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 "CarlaJuceUtils.hpp"
  18. #include "CarlaPluginUI.hpp"
  19. #ifdef HAVE_X11
  20. # include <sys/types.h>
  21. # include <X11/Xatom.h>
  22. # include <X11/Xlib.h>
  23. # include <X11/Xutil.h>
  24. #endif
  25. #ifdef CARLA_OS_MAC
  26. # import <Cocoa/Cocoa.h>
  27. #endif
  28. #ifdef CARLA_OS_WIN
  29. # include <ctime>
  30. #endif
  31. // ---------------------------------------------------------------------------------------------------------------------
  32. // X11
  33. #ifdef HAVE_X11
  34. # include "CarlaPluginUI_X11Icon.hpp"
  35. typedef void (*EventProcPtr)(XEvent* ev);
  36. static const uint X11Key_Escape = 9;
  37. static bool gErrorTriggered = false;
  38. static int temporaryErrorHandler(Display*, XErrorEvent*)
  39. {
  40. gErrorTriggered = true;
  41. return 0;
  42. }
  43. class X11PluginUI : public CarlaPluginUI
  44. {
  45. public:
  46. X11PluginUI(CloseCallback* const cb, const uintptr_t parentId, const bool isResizable) noexcept
  47. : CarlaPluginUI(cb, isResizable),
  48. fDisplay(nullptr),
  49. fWindow(0),
  50. fIsVisible(false),
  51. fFirstShow(true),
  52. fEventProc(nullptr)
  53. {
  54. fDisplay = XOpenDisplay(nullptr);
  55. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  56. const int screen = DefaultScreen(fDisplay);
  57. XSetWindowAttributes attr;
  58. carla_zeroStruct(attr);
  59. attr.border_pixel = 0;
  60. attr.event_mask = KeyPressMask|KeyReleaseMask;
  61. if (fIsResizable)
  62. attr.event_mask |= StructureNotifyMask;
  63. fWindow = XCreateWindow(fDisplay, RootWindow(fDisplay, screen),
  64. 0, 0, 300, 300, 0,
  65. DefaultDepth(fDisplay, screen),
  66. InputOutput,
  67. DefaultVisual(fDisplay, screen),
  68. CWBorderPixel|CWEventMask, &attr);
  69. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  70. XGrabKey(fDisplay, X11Key_Escape, AnyModifier, fWindow, 1, GrabModeAsync, GrabModeAsync);
  71. Atom wmDelete = XInternAtom(fDisplay, "WM_DELETE_WINDOW", True);
  72. XSetWMProtocols(fDisplay, fWindow, &wmDelete, 1);
  73. const pid_t pid = getpid();
  74. const Atom _nwp = XInternAtom(fDisplay, "_NET_WM_PID", False);
  75. XChangeProperty(fDisplay, fWindow, _nwp, XA_CARDINAL, 32, PropModeReplace, (const uchar*)&pid, 1);
  76. const Atom _nwi = XInternAtom(fDisplay, "_NET_WM_ICON", False);
  77. XChangeProperty(fDisplay, fWindow, _nwi, XA_CARDINAL, 32, PropModeReplace, (const uchar*)sCarlaX11Icon, sCarlaX11IconSize);
  78. if (parentId != 0)
  79. setTransientWinId(parentId);
  80. }
  81. ~X11PluginUI() override
  82. {
  83. CARLA_SAFE_ASSERT(! fIsVisible);
  84. if (fIsVisible)
  85. {
  86. XUnmapWindow(fDisplay, fWindow);
  87. fIsVisible = false;
  88. }
  89. if (fWindow != 0)
  90. {
  91. XDestroyWindow(fDisplay, fWindow);
  92. fWindow = 0;
  93. }
  94. if (fDisplay != nullptr)
  95. {
  96. XCloseDisplay(fDisplay);
  97. fDisplay = nullptr;
  98. }
  99. }
  100. void show() override
  101. {
  102. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  103. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  104. if (fFirstShow)
  105. {
  106. if (const Window childWindow = getChildWindow())
  107. {
  108. const Atom _xevp = XInternAtom(fDisplay, "_XEventProc", False);
  109. gErrorTriggered = false;
  110. const XErrorHandler oldErrorHandler(XSetErrorHandler(temporaryErrorHandler));
  111. Atom actualType;
  112. int actualFormat;
  113. ulong nitems, bytesAfter;
  114. uchar* data = nullptr;
  115. XGetWindowProperty(fDisplay, childWindow, _xevp, 0, 1, False, AnyPropertyType, &actualType, &actualFormat, &nitems, &bytesAfter, &data);
  116. XSetErrorHandler(oldErrorHandler);
  117. if (nitems == 1 && ! gErrorTriggered)
  118. {
  119. fEventProc = *reinterpret_cast<EventProcPtr*>(data);
  120. XMapRaised(fDisplay, childWindow);
  121. }
  122. }
  123. }
  124. fIsVisible = true;
  125. fFirstShow = false;
  126. XMapRaised(fDisplay, fWindow);
  127. XFlush(fDisplay);
  128. }
  129. void hide() override
  130. {
  131. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  132. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  133. fIsVisible = false;
  134. XUnmapWindow(fDisplay, fWindow);
  135. XFlush(fDisplay);
  136. }
  137. void idle() override
  138. {
  139. // prevent recursion
  140. if (fIsIdling) return;
  141. fIsIdling = true;
  142. for (XEvent event; XPending(fDisplay) > 0;)
  143. {
  144. XNextEvent(fDisplay, &event);
  145. if (! fIsVisible)
  146. continue;
  147. char* type = nullptr;
  148. switch (event.type)
  149. {
  150. case ConfigureNotify:
  151. CARLA_SAFE_ASSERT_CONTINUE(fCallback != nullptr);
  152. CARLA_SAFE_ASSERT_CONTINUE(event.xconfigure.width > 0);
  153. CARLA_SAFE_ASSERT_CONTINUE(event.xconfigure.height > 0);
  154. fCallback->handlePluginUIResized(static_cast<uint>(event.xconfigure.width),
  155. static_cast<uint>(event.xconfigure.height));
  156. break;
  157. case ClientMessage:
  158. type = XGetAtomName(fDisplay, event.xclient.message_type);
  159. CARLA_SAFE_ASSERT_CONTINUE(type != nullptr);
  160. if (std::strcmp(type, "WM_PROTOCOLS") == 0)
  161. {
  162. fIsVisible = false;
  163. CARLA_SAFE_ASSERT_CONTINUE(fCallback != nullptr);
  164. fCallback->handlePluginUIClosed();
  165. }
  166. break;
  167. case KeyRelease:
  168. if (event.xkey.keycode == X11Key_Escape)
  169. {
  170. fIsVisible = false;
  171. CARLA_SAFE_ASSERT_CONTINUE(fCallback != nullptr);
  172. fCallback->handlePluginUIClosed();
  173. }
  174. break;
  175. }
  176. if (type != nullptr)
  177. XFree(type);
  178. else if (fEventProc != nullptr)
  179. fEventProc(&event);
  180. }
  181. fIsIdling = false;
  182. }
  183. void focus() override
  184. {
  185. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  186. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  187. XRaiseWindow(fDisplay, fWindow);
  188. XSetInputFocus(fDisplay, fWindow, RevertToPointerRoot, CurrentTime);
  189. XFlush(fDisplay);
  190. }
  191. void setSize(const uint width, const uint height, const bool forceUpdate) override
  192. {
  193. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  194. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  195. XResizeWindow(fDisplay, fWindow, width, height);
  196. if (! fIsResizable)
  197. {
  198. XSizeHints sizeHints;
  199. carla_zeroStruct(sizeHints);
  200. sizeHints.flags = PSize|PMinSize|PMaxSize;
  201. sizeHints.width = static_cast<int>(width);
  202. sizeHints.height = static_cast<int>(height);
  203. sizeHints.min_width = static_cast<int>(width);
  204. sizeHints.min_height = static_cast<int>(height);
  205. sizeHints.max_width = static_cast<int>(width);
  206. sizeHints.max_height = static_cast<int>(height);
  207. XSetNormalHints(fDisplay, fWindow, &sizeHints);
  208. }
  209. if (forceUpdate)
  210. XFlush(fDisplay);
  211. }
  212. void setTitle(const char* const title) override
  213. {
  214. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  215. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  216. XStoreName(fDisplay, fWindow, title);
  217. }
  218. void setTransientWinId(const uintptr_t winId) override
  219. {
  220. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  221. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  222. XSetTransientForHint(fDisplay, fWindow, static_cast<Window>(winId));
  223. }
  224. void* getPtr() const noexcept override
  225. {
  226. return (void*)fWindow;
  227. }
  228. void* getDisplay() const noexcept override
  229. {
  230. return fDisplay;
  231. }
  232. protected:
  233. Window getChildWindow() const
  234. {
  235. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr, 0);
  236. CARLA_SAFE_ASSERT_RETURN(fWindow != 0, 0);
  237. Window rootWindow, parentWindow, ret = 0;
  238. Window* childWindows = nullptr;
  239. uint numChildren = 0;
  240. XQueryTree(fDisplay, fWindow, &rootWindow, &parentWindow, &childWindows, &numChildren);
  241. if (numChildren > 0 && childWindows != nullptr)
  242. {
  243. ret = childWindows[0];
  244. XFree(childWindows);
  245. }
  246. return ret;
  247. }
  248. private:
  249. Display* fDisplay;
  250. Window fWindow;
  251. bool fIsVisible;
  252. bool fFirstShow;
  253. EventProcPtr fEventProc;
  254. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(X11PluginUI)
  255. };
  256. #endif // HAVE_X11
  257. // ---------------------------------------------------------------------------------------------------------------------
  258. // MacOS / Cocoa
  259. #ifdef CARLA_OS_MAC
  260. class CocoaPluginUI : public CarlaPluginUI
  261. {
  262. public:
  263. CocoaPluginUI(CloseCallback* const cb, const uintptr_t parentId, const bool isResizable) noexcept
  264. : CarlaPluginUI(cb, isResizable),
  265. fView(nullptr),
  266. fWindow(0)
  267. {
  268. [NSAutoreleasePool new];
  269. [NSApplication sharedApplication];
  270. fView = [NSView new];
  271. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,)
  272. if (isResizable)
  273. [fView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
  274. fWindow = [[NSWindow new]retain];
  275. if (fWindow == 0)
  276. {
  277. [fView release];
  278. fView = nullptr;
  279. return;
  280. }
  281. [fWindow setIsVisible:NO];
  282. [fWindow setContentView:fView];
  283. [fWindow makeFirstResponder:fView];
  284. [fWindow makeKeyAndOrderFront:fWindow];
  285. [NSApp activateIgnoringOtherApps:YES];
  286. [fWindow center];
  287. #if 0
  288. uint styleMask = NSClosableWindowMask | NSTitledWindowMask;
  289. if (isResizable)
  290. styleMask |= NSResizableWindowMask;
  291. fWindow = [NSWindow initWithContentRect:contentRect
  292. styleMask:flags
  293. backing:NSBackingStoreBuffered defer:NO];
  294. #endif
  295. }
  296. ~CocoaPluginUI() override
  297. {
  298. if (fView == nullptr)
  299. return;
  300. [fWindow close];
  301. [fView release];
  302. [fWindow release];
  303. }
  304. void show() override
  305. {
  306. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  307. [fWindow setIsVisible:YES];
  308. }
  309. void hide() override
  310. {
  311. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  312. [fWindow setIsVisible:NO];
  313. }
  314. void idle() override
  315. {
  316. }
  317. void focus() override
  318. {
  319. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  320. // TODO
  321. }
  322. void setSize(const uint width, const uint height, const bool forceUpdate) override
  323. {
  324. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  325. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,);
  326. [fView setFrame:NSMakeRect(0, 0, width, height)];
  327. const NSSize size = NSMakeSize(width, height);
  328. [fWindow setContentSize:size];
  329. #if 0
  330. if (fResizable)
  331. {
  332. [fWindow setContentMinSize:NSMakeSize(1, 1)];
  333. [fWindow setContentMaxSize:NSMakeSize(99999, 99999)];
  334. [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:NO];
  335. }
  336. else
  337. {
  338. [fWindow setContentMinSize:size];
  339. [fWindow setContentMaxSize:size];
  340. [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
  341. }
  342. #endif
  343. }
  344. void setTitle(const char* const title) override
  345. {
  346. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  347. NSString* titleString = [[NSString alloc]
  348. initWithBytes:title
  349. length:strlen(title)
  350. encoding:NSUTF8StringEncoding];
  351. [fWindow setTitle:titleString];
  352. }
  353. void setTransientWinId(const uintptr_t winId) override
  354. {
  355. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  356. // TODO
  357. }
  358. void* getPtr() const noexcept override
  359. {
  360. return (void*)fView;
  361. }
  362. void* getDisplay() const noexcept
  363. {
  364. return (void*)fWindow;
  365. }
  366. private:
  367. NSView* fView;
  368. id fWindow;
  369. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CocoaPluginUI)
  370. };
  371. #endif // CARLA_OS_MAC
  372. // ---------------------------------------------------------------------------------------------------------------------
  373. // Windows
  374. #ifdef CARLA_OS_WIN
  375. #define PUGL_LOCAL_CLOSE_MSG (WM_USER + 50)
  376. static LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  377. class WindowsPluginUI : public CarlaPluginUI
  378. {
  379. public:
  380. WindowsPluginUI(CloseCallback* const cb, const uintptr_t parentId, const bool isResizable) noexcept
  381. : CarlaPluginUI(cb, isResizable),
  382. fWindow(0),
  383. fIsVisible(false),
  384. fFirstShow(true)
  385. {
  386. // FIXME
  387. static int wc_count = 0;
  388. char classNameBuf[256];
  389. std::srand((std::time(NULL)));
  390. _snprintf(classNameBuf, sizeof(classNameBuf), "CaWin_%d-%d", std::rand(), ++wc_count);
  391. classNameBuf[sizeof(classNameBuf)-1] = '\0';
  392. const HINSTANCE hInstance = GetModuleHandleA(nullptr);
  393. carla_zeroStruct(fWindowClass);
  394. fWindowClass.style = CS_OWNDC;
  395. fWindowClass.lpfnWndProc = wndProc;
  396. fWindowClass.hInstance = hInstance;
  397. fWindowClass.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
  398. fWindowClass.hCursor = LoadCursor(hInstance, IDC_ARROW);
  399. fWindowClass.lpszClassName = strdup(classNameBuf);
  400. if (!RegisterClass(&fWindowClass)) {
  401. free((void*)fWindowClass.lpszClassName);
  402. return;
  403. }
  404. int winFlags = WS_POPUPWINDOW | WS_CAPTION;
  405. if (isResizable)
  406. winFlags |= WS_SIZEBOX;
  407. fWindow = CreateWindowEx(WS_EX_TOPMOST,
  408. classNameBuf, "Carla Plugin UI", winFlags,
  409. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  410. NULL, NULL, hInstance, NULL);
  411. if (! fWindow) {
  412. UnregisterClass(fWindowClass.lpszClassName, NULL);
  413. free((void*)fWindowClass.lpszClassName);
  414. return;
  415. }
  416. SetWindowLongPtr(fWindow, GWLP_USERDATA, (LONG_PTR)this);
  417. }
  418. ~WindowsPluginUI() override
  419. {
  420. CARLA_SAFE_ASSERT(! fIsVisible);
  421. if (fWindow != 0)
  422. {
  423. if (fIsVisible)
  424. ShowWindow(fWindow, SW_HIDE);
  425. DestroyWindow(fWindow);
  426. fWindow = 0;
  427. }
  428. // FIXME
  429. UnregisterClass(fWindowClass.lpszClassName, NULL);
  430. free((void*)fWindowClass.lpszClassName);
  431. }
  432. void show() override
  433. {
  434. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  435. ShowWindow(fWindow, fFirstShow ? SW_SHOWNORMAL : SW_RESTORE);
  436. fIsVisible = true;
  437. fFirstShow = false;
  438. UpdateWindow(fWindow);
  439. }
  440. void hide() override
  441. {
  442. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  443. ShowWindow(fWindow, SW_HIDE);
  444. fIsVisible = false;
  445. UpdateWindow(fWindow);
  446. }
  447. void idle() override
  448. {
  449. if (fIsIdling || fWindow == 0)
  450. return;
  451. MSG msg;
  452. fIsIdling = true;
  453. while (::PeekMessage(&msg, fWindow, 0, 0, PM_REMOVE))
  454. {
  455. switch (msg.message)
  456. {
  457. case WM_QUIT:
  458. case PUGL_LOCAL_CLOSE_MSG:
  459. fIsVisible = false;
  460. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  461. fCallback->handlePluginUIClosed();
  462. break;
  463. }
  464. DispatchMessageA(&msg);
  465. }
  466. fIsIdling = false;
  467. }
  468. LRESULT checkAndHandleMessage(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  469. {
  470. if (fWindow == hwnd)
  471. {
  472. switch (message)
  473. {
  474. case WM_QUIT:
  475. case PUGL_LOCAL_CLOSE_MSG:
  476. fIsVisible = false;
  477. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  478. fCallback->handlePluginUIClosed();
  479. break;
  480. }
  481. }
  482. return DefWindowProcA(hwnd, message, wParam, lParam);
  483. }
  484. void focus() override
  485. {
  486. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  487. SetForegroundWindow(fWindow);
  488. SetActiveWindow(fWindow);
  489. SetFocus(fWindow);
  490. }
  491. void setSize(const uint width, const uint height, const bool forceUpdate) override
  492. {
  493. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  494. const int winFlags = WS_POPUPWINDOW | WS_CAPTION | (fIsResizable ? WS_SIZEBOX : 0x0);
  495. RECT wr = { 0, 0, static_cast<long>(width), static_cast<long>(height) };
  496. AdjustWindowRectEx(&wr, winFlags, FALSE, WS_EX_TOPMOST);
  497. SetWindowPos(fWindow, 0, 0, 0, wr.right-wr.left, wr.bottom-wr.top,
  498. SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
  499. if (forceUpdate)
  500. UpdateWindow(fWindow);
  501. }
  502. void setTitle(const char* const title) override
  503. {
  504. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  505. SetWindowTextA(fWindow, title);
  506. }
  507. void setTransientWinId(const uintptr_t winId) override
  508. {
  509. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  510. // TODO
  511. }
  512. void* getPtr() const noexcept override
  513. {
  514. return (void*)fWindow;
  515. }
  516. void* getDisplay() const noexcept
  517. {
  518. return nullptr;
  519. }
  520. private:
  521. HWND fWindow;
  522. WNDCLASS fWindowClass;
  523. bool fIsVisible;
  524. bool fFirstShow;
  525. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(WindowsPluginUI)
  526. };
  527. LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  528. {
  529. switch (message)
  530. {
  531. case WM_CLOSE:
  532. PostMessage(hwnd, PUGL_LOCAL_CLOSE_MSG, wParam, lParam);
  533. return 0;
  534. #if 0
  535. case WM_CREATE:
  536. PostMessage(hwnd, WM_SHOWWINDOW, TRUE, 0);
  537. return 0;
  538. case WM_DESTROY:
  539. return 0;
  540. #endif
  541. default:
  542. if (WindowsPluginUI* const ui = (WindowsPluginUI*)GetWindowLongPtr(hwnd, GWLP_USERDATA))
  543. return ui->checkAndHandleMessage(hwnd, message, wParam, lParam);
  544. return DefWindowProcA(hwnd, message, wParam, lParam);
  545. }
  546. }
  547. #endif // CARLA_OS_WIN
  548. // -----------------------------------------------------
  549. bool CarlaPluginUI::tryTransientWinIdMatch(const uintptr_t pid, const char* const uiTitle, const uintptr_t winId, const bool centerUI)
  550. {
  551. CARLA_SAFE_ASSERT_RETURN(uiTitle != nullptr && uiTitle[0] != '\0', true);
  552. CARLA_SAFE_ASSERT_RETURN(winId != 0, true);
  553. #if defined(HAVE_X11)
  554. struct ScopedDisplay {
  555. Display* display;
  556. ScopedDisplay() : display(XOpenDisplay(nullptr)) {}
  557. ~ScopedDisplay() { if (display!=nullptr) XCloseDisplay(display); }
  558. // c++ compat stuff
  559. CARLA_PREVENT_HEAP_ALLOCATION
  560. CARLA_DECLARE_NON_COPY_CLASS(ScopedDisplay)
  561. };
  562. struct ScopedFreeData {
  563. union {
  564. char* data;
  565. uchar* udata;
  566. };
  567. ScopedFreeData(char* d) : data(d) {}
  568. ScopedFreeData(uchar* d) : udata(d) {}
  569. ~ScopedFreeData() { XFree(data); }
  570. // c++ compat stuff
  571. CARLA_PREVENT_HEAP_ALLOCATION
  572. CARLA_DECLARE_NON_COPY_CLASS(ScopedFreeData)
  573. };
  574. const ScopedDisplay sd;
  575. CARLA_SAFE_ASSERT_RETURN(sd.display != nullptr, true);
  576. const Window rootWindow(DefaultRootWindow(sd.display));
  577. const Atom _ncl = XInternAtom(sd.display, "_NET_CLIENT_LIST" , False);
  578. const Atom _nwn = XInternAtom(sd.display, "_NET_WM_NAME", False);
  579. const Atom _nwp = XInternAtom(sd.display, "_NET_WM_PID", False);
  580. const Atom utf8 = XInternAtom(sd.display, "UTF8_STRING", True);
  581. Atom actualType;
  582. int actualFormat;
  583. ulong numWindows, bytesAfter;
  584. uchar* data = nullptr;
  585. int status = XGetWindowProperty(sd.display, rootWindow, _ncl, 0L, (~0L), False, AnyPropertyType, &actualType, &actualFormat, &numWindows, &bytesAfter, &data);
  586. CARLA_SAFE_ASSERT_RETURN(data != nullptr, true);
  587. const ScopedFreeData sfd(data);
  588. CARLA_SAFE_ASSERT_RETURN(status == Success, true);
  589. CARLA_SAFE_ASSERT_RETURN(actualFormat == 32, true);
  590. CARLA_SAFE_ASSERT_RETURN(numWindows != 0, true);
  591. Window* windows = (Window*)data;
  592. Window lastGoodWindowPID = 0, lastGoodWindowNameSimple = 0, lastGoodWindowNameUTF8 = 0;
  593. for (ulong i = 0; i < numWindows; i++)
  594. {
  595. const Window window(windows[i]);
  596. CARLA_SAFE_ASSERT_CONTINUE(window != 0);
  597. // ------------------------------------------------
  598. // try using pid
  599. if (pid != 0)
  600. {
  601. ulong pidSize;
  602. uchar* pidData = nullptr;
  603. status = XGetWindowProperty(sd.display, window, _nwp, 0L, (~0L), False, XA_CARDINAL, &actualType, &actualFormat, &pidSize, &bytesAfter, &pidData);
  604. if (pidData != nullptr)
  605. {
  606. const ScopedFreeData sfd2(pidData);
  607. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  608. CARLA_SAFE_ASSERT_CONTINUE(pidSize != 0);
  609. if (*(ulong*)pidData == static_cast<ulong>(pid))
  610. lastGoodWindowPID = window;
  611. }
  612. }
  613. // ------------------------------------------------
  614. // try using name (UTF-8)
  615. ulong nameSize;
  616. uchar* nameData = nullptr;
  617. status = XGetWindowProperty(sd.display, window, _nwn, 0L, (~0L), False, utf8, &actualType, &actualFormat, &nameSize, &bytesAfter, &nameData);
  618. if (nameData != nullptr)
  619. {
  620. const ScopedFreeData sfd2(nameData);
  621. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  622. if (nameSize != 0 && std::strstr((const char*)nameData, uiTitle) != nullptr)
  623. lastGoodWindowNameUTF8 = window;
  624. }
  625. // ------------------------------------------------
  626. // try using name (simple)
  627. char* wmName = nullptr;
  628. status = XFetchName(sd.display, window, &wmName);
  629. if (wmName != nullptr)
  630. {
  631. const ScopedFreeData sfd2(wmName);
  632. CARLA_SAFE_ASSERT_CONTINUE(status != 0);
  633. if (std::strstr(wmName, uiTitle) != nullptr)
  634. lastGoodWindowNameSimple = window;
  635. }
  636. }
  637. if (lastGoodWindowPID == 0 && lastGoodWindowNameSimple == 0 && lastGoodWindowNameUTF8 == 0)
  638. return false;
  639. Window windowToMap;
  640. if (lastGoodWindowPID != 0)
  641. {
  642. if (lastGoodWindowPID == lastGoodWindowNameSimple && lastGoodWindowPID == lastGoodWindowNameUTF8)
  643. {
  644. carla_stdout("Match found using pid, simple and UTF-8 name all at once, nice!");
  645. windowToMap = lastGoodWindowPID;
  646. }
  647. else if (lastGoodWindowPID == lastGoodWindowNameUTF8)
  648. {
  649. carla_stdout("Match found using pid and UTF-8 name");
  650. windowToMap = lastGoodWindowPID;
  651. }
  652. else if (lastGoodWindowPID == lastGoodWindowNameSimple)
  653. {
  654. carla_stdout("Match found using pid and simple name");
  655. windowToMap = lastGoodWindowPID;
  656. }
  657. else
  658. {
  659. carla_stdout("Match found using pid");
  660. windowToMap = lastGoodWindowPID;
  661. }
  662. }
  663. else if (lastGoodWindowNameUTF8 != 0)
  664. {
  665. if (lastGoodWindowNameUTF8 == lastGoodWindowNameSimple)
  666. {
  667. carla_stdout("Match found using simple and UTF-8 name");
  668. windowToMap = lastGoodWindowNameUTF8;
  669. }
  670. else
  671. {
  672. carla_stdout("Match found using simple and UTF-8 name");
  673. windowToMap = lastGoodWindowNameUTF8;
  674. }
  675. }
  676. else
  677. {
  678. carla_stdout("Match found using simple name");
  679. windowToMap = lastGoodWindowNameSimple;
  680. }
  681. const Atom _nwt = XInternAtom(sd.display ,"_NET_WM_STATE", False);
  682. const Atom _nws[2] = {
  683. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_TASKBAR", False),
  684. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_PAGER", False)
  685. };
  686. XChangeProperty(sd.display, windowToMap, _nwt, XA_ATOM, 32, PropModeAppend, (const uchar*)_nws, 2);
  687. const Atom _nwi = XInternAtom(sd.display, "_NET_WM_ICON", False);
  688. XChangeProperty(sd.display, windowToMap, _nwi, XA_CARDINAL, 32, PropModeReplace, (const uchar*)sCarlaX11Icon, sCarlaX11IconSize);
  689. const Window hostWinId((Window)winId);
  690. XSetTransientForHint(sd.display, windowToMap, hostWinId);
  691. if (centerUI && false /* moving the window after being shown isn't pretty... */)
  692. {
  693. int hostX, hostY, pluginX, pluginY;
  694. uint hostWidth, hostHeight, pluginWidth, pluginHeight, border, depth;
  695. Window retWindow;
  696. if (XGetGeometry(sd.display, hostWinId, &retWindow, &hostX, &hostY, &hostWidth, &hostHeight, &border, &depth) != 0 &&
  697. XGetGeometry(sd.display, windowToMap, &retWindow, &pluginX, &pluginY, &pluginWidth, &pluginHeight, &border, &depth) != 0)
  698. {
  699. if (XTranslateCoordinates(sd.display, hostWinId, rootWindow, hostX, hostY, &hostX, &hostY, &retWindow) == True &&
  700. XTranslateCoordinates(sd.display, windowToMap, rootWindow, pluginX, pluginY, &pluginX, &pluginY, &retWindow) == True)
  701. {
  702. const int newX = hostX + int(hostWidth/2 - pluginWidth/2);
  703. const int newY = hostY + int(hostHeight/2 - pluginHeight/2);
  704. XMoveWindow(sd.display, windowToMap, newX, newY);
  705. }
  706. }
  707. }
  708. // focusing the host UI and then the plugin UI forces the WM to repaint the plugin window icon
  709. XRaiseWindow(sd.display, hostWinId);
  710. XSetInputFocus(sd.display, hostWinId, RevertToPointerRoot, CurrentTime);
  711. XRaiseWindow(sd.display, windowToMap);
  712. XSetInputFocus(sd.display, windowToMap, RevertToPointerRoot, CurrentTime);
  713. XFlush(sd.display);
  714. return true;
  715. #else
  716. return true;
  717. (void)pid; (void)centerUI;
  718. #endif
  719. }
  720. // -----------------------------------------------------
  721. #ifdef HAVE_X11
  722. CarlaPluginUI* CarlaPluginUI::newX11(CloseCallback* cb, uintptr_t parentId, bool isResizable)
  723. {
  724. return new X11PluginUI(cb, parentId, isResizable);
  725. }
  726. #endif
  727. #ifdef CARLA_OS_MAC
  728. CarlaPluginUI* CarlaPluginUI::newCocoa(CloseCallback* cb, uintptr_t parentId, bool isResizable)
  729. {
  730. return new CocoaPluginUI(cb, parentId, isResizable);
  731. }
  732. #endif
  733. #ifdef CARLA_OS_WIN
  734. CarlaPluginUI* CarlaPluginUI::newWindows(CloseCallback* cb, uintptr_t parentId, bool isResizable)
  735. {
  736. return new WindowsPluginUI(cb, parentId, isResizable);
  737. }
  738. #endif
  739. // -----------------------------------------------------