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.

1199 lines
36KB

  1. /*
  2. * Carla Plugin UI
  3. * Copyright (C) 2014-2018 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. # include "CarlaPluginUI_X11Icon.hpp"
  25. #endif
  26. #ifdef CARLA_OS_MAC
  27. # include "CarlaMacUtils.hpp"
  28. # import <Cocoa/Cocoa.h>
  29. #endif
  30. #ifdef CARLA_OS_WIN
  31. # include <ctime>
  32. #endif
  33. #ifndef CARLA_PLUGIN_UI_CLASS_PREFIX
  34. # error CARLA_PLUGIN_UI_CLASS_PREFIX undefined
  35. #endif
  36. // ---------------------------------------------------------------------------------------------------------------------
  37. // X11
  38. #ifdef HAVE_X11
  39. typedef void (*EventProcPtr)(XEvent* ev);
  40. static const uint X11Key_Escape = 9;
  41. static bool gErrorTriggered = false;
  42. static int temporaryErrorHandler(Display*, XErrorEvent*)
  43. {
  44. gErrorTriggered = true;
  45. return 0;
  46. }
  47. class X11PluginUI : public CarlaPluginUI
  48. {
  49. public:
  50. X11PluginUI(Callback* const cb, const uintptr_t parentId, const bool isResizable) noexcept
  51. : CarlaPluginUI(cb, isResizable),
  52. fDisplay(nullptr),
  53. fHostWindow(0),
  54. fChildWindow(0),
  55. fIsVisible(false),
  56. fFirstShow(true),
  57. fSetSizeCalledAtLeastOnce(false),
  58. fEventProc(nullptr)
  59. {
  60. fDisplay = XOpenDisplay(nullptr);
  61. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  62. const int screen = DefaultScreen(fDisplay);
  63. XSetWindowAttributes attr;
  64. carla_zeroStruct(attr);
  65. attr.border_pixel = 0;
  66. attr.event_mask = KeyPressMask|KeyReleaseMask;
  67. if (fIsResizable)
  68. attr.event_mask |= StructureNotifyMask;
  69. fHostWindow = XCreateWindow(fDisplay, RootWindow(fDisplay, screen),
  70. 0, 0, 300, 300, 0,
  71. DefaultDepth(fDisplay, screen),
  72. InputOutput,
  73. DefaultVisual(fDisplay, screen),
  74. CWBorderPixel|CWEventMask, &attr);
  75. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  76. XGrabKey(fDisplay, X11Key_Escape, AnyModifier, fHostWindow, 1, GrabModeAsync, GrabModeAsync);
  77. Atom wmDelete = XInternAtom(fDisplay, "WM_DELETE_WINDOW", True);
  78. XSetWMProtocols(fDisplay, fHostWindow, &wmDelete, 1);
  79. const pid_t pid = getpid();
  80. const Atom _nwp = XInternAtom(fDisplay, "_NET_WM_PID", False);
  81. XChangeProperty(fDisplay, fHostWindow, _nwp, XA_CARDINAL, 32, PropModeReplace, (const uchar*)&pid, 1);
  82. const Atom _nwi = XInternAtom(fDisplay, "_NET_WM_ICON", False);
  83. XChangeProperty(fDisplay, fHostWindow, _nwi, XA_CARDINAL, 32, PropModeReplace, (const uchar*)sCarlaX11Icon, sCarlaX11IconSize);
  84. if (parentId != 0)
  85. setTransientWinId(parentId);
  86. }
  87. ~X11PluginUI() override
  88. {
  89. CARLA_SAFE_ASSERT(! fIsVisible);
  90. if (fIsVisible)
  91. {
  92. XUnmapWindow(fDisplay, fHostWindow);
  93. fIsVisible = false;
  94. }
  95. if (fHostWindow != 0)
  96. {
  97. XDestroyWindow(fDisplay, fHostWindow);
  98. fHostWindow = 0;
  99. }
  100. if (fDisplay != nullptr)
  101. {
  102. XCloseDisplay(fDisplay);
  103. fDisplay = nullptr;
  104. }
  105. }
  106. void show() override
  107. {
  108. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  109. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  110. if (fFirstShow)
  111. {
  112. if (const Window childWindow = getChildWindow())
  113. {
  114. if (! fSetSizeCalledAtLeastOnce)
  115. {
  116. XSizeHints hints;
  117. carla_zeroStruct(hints);
  118. if (XGetNormalHints(fDisplay, childWindow, &hints) && hints.width > 0 && hints.height > 0)
  119. setSize(hints.width, hints.height, false);
  120. }
  121. const Atom _xevp = XInternAtom(fDisplay, "_XEventProc", False);
  122. gErrorTriggered = false;
  123. const XErrorHandler oldErrorHandler(XSetErrorHandler(temporaryErrorHandler));
  124. Atom actualType;
  125. int actualFormat;
  126. ulong nitems, bytesAfter;
  127. uchar* data = nullptr;
  128. XGetWindowProperty(fDisplay, childWindow, _xevp, 0, 1, False, AnyPropertyType, &actualType, &actualFormat, &nitems, &bytesAfter, &data);
  129. XSetErrorHandler(oldErrorHandler);
  130. if (nitems == 1 && ! gErrorTriggered)
  131. {
  132. fEventProc = *reinterpret_cast<EventProcPtr*>(data);
  133. XMapRaised(fDisplay, childWindow);
  134. }
  135. }
  136. }
  137. fIsVisible = true;
  138. fFirstShow = false;
  139. XMapRaised(fDisplay, fHostWindow);
  140. XFlush(fDisplay);
  141. }
  142. void hide() override
  143. {
  144. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  145. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  146. fIsVisible = false;
  147. XUnmapWindow(fDisplay, fHostWindow);
  148. XFlush(fDisplay);
  149. }
  150. void idle() override
  151. {
  152. // prevent recursion
  153. if (fIsIdling) return;
  154. fIsIdling = true;
  155. for (XEvent event; XPending(fDisplay) > 0;)
  156. {
  157. XNextEvent(fDisplay, &event);
  158. if (! fIsVisible)
  159. continue;
  160. char* type = nullptr;
  161. switch (event.type)
  162. {
  163. case ConfigureNotify:
  164. CARLA_SAFE_ASSERT_CONTINUE(fCallback != nullptr);
  165. CARLA_SAFE_ASSERT_CONTINUE(event.xconfigure.width > 0);
  166. CARLA_SAFE_ASSERT_CONTINUE(event.xconfigure.height > 0);
  167. {
  168. const uint width = static_cast<uint>(event.xconfigure.width);
  169. const uint height = static_cast<uint>(event.xconfigure.height);
  170. if (fChildWindow != 0)
  171. XResizeWindow(fDisplay, fChildWindow, width, height);
  172. fCallback->handlePluginUIResized(width, height);
  173. }
  174. break;
  175. case ClientMessage:
  176. type = XGetAtomName(fDisplay, event.xclient.message_type);
  177. CARLA_SAFE_ASSERT_CONTINUE(type != nullptr);
  178. if (std::strcmp(type, "WM_PROTOCOLS") == 0)
  179. {
  180. fIsVisible = false;
  181. CARLA_SAFE_ASSERT_CONTINUE(fCallback != nullptr);
  182. fCallback->handlePluginUIClosed();
  183. }
  184. break;
  185. case KeyRelease:
  186. if (event.xkey.keycode == X11Key_Escape)
  187. {
  188. fIsVisible = false;
  189. CARLA_SAFE_ASSERT_CONTINUE(fCallback != nullptr);
  190. fCallback->handlePluginUIClosed();
  191. }
  192. break;
  193. }
  194. if (type != nullptr)
  195. XFree(type);
  196. else if (fEventProc != nullptr)
  197. fEventProc(&event);
  198. }
  199. fIsIdling = false;
  200. }
  201. void focus() override
  202. {
  203. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  204. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  205. XRaiseWindow(fDisplay, fHostWindow);
  206. XSetInputFocus(fDisplay, fHostWindow, RevertToPointerRoot, CurrentTime);
  207. XFlush(fDisplay);
  208. }
  209. void setSize(const uint width, const uint height, const bool forceUpdate) override
  210. {
  211. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  212. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  213. fSetSizeCalledAtLeastOnce = true;
  214. XResizeWindow(fDisplay, fHostWindow, width, height);
  215. if (fChildWindow != 0)
  216. XResizeWindow(fDisplay, fChildWindow, width, height);
  217. if (! fIsResizable)
  218. {
  219. XSizeHints sizeHints;
  220. carla_zeroStruct(sizeHints);
  221. sizeHints.flags = PSize|PMinSize|PMaxSize;
  222. sizeHints.width = static_cast<int>(width);
  223. sizeHints.height = static_cast<int>(height);
  224. sizeHints.min_width = static_cast<int>(width);
  225. sizeHints.min_height = static_cast<int>(height);
  226. sizeHints.max_width = static_cast<int>(width);
  227. sizeHints.max_height = static_cast<int>(height);
  228. XSetNormalHints(fDisplay, fHostWindow, &sizeHints);
  229. }
  230. if (forceUpdate)
  231. XFlush(fDisplay);
  232. }
  233. void setTitle(const char* const title) override
  234. {
  235. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  236. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  237. XStoreName(fDisplay, fHostWindow, title);
  238. }
  239. void setTransientWinId(const uintptr_t winId) override
  240. {
  241. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  242. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  243. XSetTransientForHint(fDisplay, fHostWindow, static_cast<Window>(winId));
  244. }
  245. void setChildWindow(void* const winId) override
  246. {
  247. CARLA_SAFE_ASSERT_RETURN(winId != nullptr,);
  248. fChildWindow = (Window)winId;
  249. }
  250. void* getPtr() const noexcept override
  251. {
  252. return (void*)fHostWindow;
  253. }
  254. void* getDisplay() const noexcept override
  255. {
  256. return fDisplay;
  257. }
  258. protected:
  259. Window getChildWindow() const
  260. {
  261. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr, 0);
  262. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0, 0);
  263. Window rootWindow, parentWindow, ret = 0;
  264. Window* childWindows = nullptr;
  265. uint numChildren = 0;
  266. XQueryTree(fDisplay, fHostWindow, &rootWindow, &parentWindow, &childWindows, &numChildren);
  267. if (numChildren > 0 && childWindows != nullptr)
  268. {
  269. ret = childWindows[0];
  270. XFree(childWindows);
  271. }
  272. return ret;
  273. }
  274. private:
  275. Display* fDisplay;
  276. Window fHostWindow;
  277. Window fChildWindow;
  278. bool fIsVisible;
  279. bool fFirstShow;
  280. bool fSetSizeCalledAtLeastOnce;
  281. EventProcPtr fEventProc;
  282. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(X11PluginUI)
  283. };
  284. #endif // HAVE_X11
  285. // ---------------------------------------------------------------------------------------------------------------------
  286. // MacOS / Cocoa
  287. #ifdef CARLA_OS_MAC
  288. #if defined(BUILD_BRIDGE_ALTERNATIVE_ARCH)
  289. # define CarlaPluginWindow CARLA_JOIN_MACRO(CarlaPluginWindowBridgedArch, CARLA_PLUGIN_UI_CLASS_PREFIX)
  290. #elif defined(BUILD_BRIDGE)
  291. # define CarlaPluginWindow CARLA_JOIN_MACRO(CarlaPluginWindowBridged, CARLA_PLUGIN_UI_CLASS_PREFIX)
  292. #else
  293. # define CarlaPluginWindow CARLA_JOIN_MACRO(CarlaPluginWindow, CARLA_PLUGIN_UI_CLASS_PREFIX)
  294. #endif
  295. @interface CarlaPluginWindow : NSWindow
  296. {
  297. @public
  298. CarlaPluginUI::Callback* callback;
  299. }
  300. - (id) initWithContentRect:(NSRect)contentRect
  301. styleMask:(unsigned int)aStyle
  302. backing:(NSBackingStoreType)bufferingType
  303. defer:(BOOL)flag;
  304. - (void) setCallback:(CarlaPluginUI::Callback*)cb;
  305. - (BOOL) canBecomeKeyWindow;
  306. - (BOOL) windowShouldClose:(id)sender;
  307. - (NSSize) windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize;
  308. @end
  309. @implementation CarlaPluginWindow
  310. - (id)initWithContentRect:(NSRect)contentRect
  311. styleMask:(unsigned int)aStyle
  312. backing:(NSBackingStoreType)bufferingType
  313. defer:(BOOL)flag
  314. {
  315. callback = nil;
  316. NSWindow* result = [super initWithContentRect:contentRect
  317. styleMask:(NSClosableWindowMask |
  318. NSTitledWindowMask |
  319. NSResizableWindowMask)
  320. backing:NSBackingStoreBuffered
  321. defer:YES];
  322. [result setIsVisible:NO];
  323. [result setAcceptsMouseMovedEvents:YES];
  324. [result setContentSize:NSMakeSize(18, 100)];
  325. return (CarlaPluginWindow*)result;
  326. // unused
  327. (void)aStyle; (void)bufferingType; (void)flag;
  328. }
  329. - (void)setCallback:(CarlaPluginUI::Callback*)cb
  330. {
  331. callback = cb;
  332. }
  333. - (BOOL)canBecomeKeyWindow
  334. {
  335. return YES;
  336. }
  337. - (BOOL)windowShouldClose:(id)sender
  338. {
  339. if (callback != nil)
  340. callback->handlePluginUIClosed();
  341. return NO;
  342. // unused
  343. (void)sender;
  344. }
  345. - (NSSize) windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize
  346. {
  347. if (callback != nil)
  348. callback->handlePluginUIResized(frameSize.width, frameSize.height);
  349. return frameSize;
  350. // unused
  351. (void)sender;
  352. }
  353. @end
  354. class CocoaPluginUI : public CarlaPluginUI
  355. {
  356. public:
  357. CocoaPluginUI(Callback* const cb, const uintptr_t parentId, const bool isResizable) noexcept
  358. : CarlaPluginUI(cb, isResizable),
  359. fView(nullptr),
  360. fWindow(nullptr)
  361. {
  362. carla_debug("CocoaPluginUI::CocoaPluginUI(%p, " P_UINTPTR, "%s)", cb, parentId, bool2str(isResizable));
  363. const CarlaBackend::AutoNSAutoreleasePool arp;
  364. fView = [[NSView new]retain];
  365. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,)
  366. [fView setHidden:YES];
  367. if (isResizable)
  368. [fView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
  369. fWindow = [[CarlaPluginWindow new]retain];
  370. if (fWindow == nullptr)
  371. {
  372. [fView release];
  373. fView = nullptr;
  374. return;
  375. }
  376. if (! isResizable)
  377. [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
  378. [fWindow setCallback:cb];
  379. [fWindow setContentView:fView];
  380. [fWindow makeFirstResponder:fView];
  381. [fWindow makeKeyAndOrderFront:fWindow];
  382. [fWindow center];
  383. if (parentId != 0)
  384. setTransientWinId(parentId);
  385. }
  386. ~CocoaPluginUI() override
  387. {
  388. carla_debug("CocoaPluginUI::~CocoaPluginUI()");
  389. if (fView == nullptr)
  390. return;
  391. [fWindow close];
  392. [fView release];
  393. [fWindow release];
  394. }
  395. void show() override
  396. {
  397. carla_debug("CocoaPluginUI::show()");
  398. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,);
  399. [fView setHidden:NO];
  400. [fWindow setIsVisible:YES];
  401. }
  402. void hide() override
  403. {
  404. carla_debug("CocoaPluginUI::hide()");
  405. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,);
  406. [fWindow setIsVisible:NO];
  407. [fView setHidden:YES];
  408. }
  409. void idle() override
  410. {
  411. // carla_debug("CocoaPluginUI::idle()");
  412. }
  413. void focus() override
  414. {
  415. carla_debug("CocoaPluginUI::focus()");
  416. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  417. [fWindow makeKeyWindow];
  418. [NSApp activateIgnoringOtherApps:YES];
  419. }
  420. void setSize(const uint width, const uint height, const bool forceUpdate) override
  421. {
  422. carla_debug("CocoaPluginUI::setSize(%u, %u, %s)", width, height, bool2str(forceUpdate));
  423. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  424. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,);
  425. [fView setFrame:NSMakeRect(0, 0, width, height)];
  426. const NSSize size = NSMakeSize(width, height);
  427. [fWindow setContentSize:size];
  428. if (fIsResizable)
  429. {
  430. [fWindow setContentMinSize:NSMakeSize(1, 1)];
  431. [fWindow setContentMaxSize:NSMakeSize(99999, 99999)];
  432. [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:NO];
  433. }
  434. else
  435. {
  436. [fWindow setContentMinSize:size];
  437. [fWindow setContentMaxSize:size];
  438. [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
  439. }
  440. if (forceUpdate)
  441. {
  442. // FIXME, not enough
  443. [fView setNeedsDisplay:YES];
  444. }
  445. }
  446. void setTitle(const char* const title) override
  447. {
  448. carla_debug("CocoaPluginUI::setTitle(\"%s\")", title);
  449. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  450. NSString* titleString = [[NSString alloc]
  451. initWithBytes:title
  452. length:strlen(title)
  453. encoding:NSUTF8StringEncoding];
  454. [fWindow setTitle:titleString];
  455. }
  456. void setTransientWinId(const uintptr_t winId) override
  457. {
  458. carla_debug("CocoaPluginUI::setTransientWinId(" P_UINTPTR ")", winId);
  459. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  460. NSWindow* const parentWindow = [NSApp windowWithWindowNumber:winId];
  461. CARLA_SAFE_ASSERT_RETURN(parentWindow != nullptr,);
  462. [parentWindow addChildWindow:fWindow
  463. ordered:NSWindowAbove];
  464. }
  465. void setChildWindow(void* const window) override
  466. {
  467. carla_debug("CocoaPluginUI::setChildWindow(%p)", window);
  468. CARLA_SAFE_ASSERT_RETURN(window != nullptr,);
  469. }
  470. void* getPtr() const noexcept override
  471. {
  472. carla_debug("CocoaPluginUI::getPtr()");
  473. return (void*)fView;
  474. }
  475. void* getDisplay() const noexcept
  476. {
  477. carla_debug("CocoaPluginUI::getDisplay()");
  478. return (void*)fWindow;
  479. }
  480. private:
  481. NSView* fView;
  482. CarlaPluginWindow* fWindow;
  483. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CocoaPluginUI)
  484. };
  485. #endif // CARLA_OS_MAC
  486. // ---------------------------------------------------------------------------------------------------------------------
  487. // Windows
  488. #ifdef CARLA_OS_WIN
  489. #define PUGL_LOCAL_CLOSE_MSG (WM_USER + 50)
  490. static LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  491. class WindowsPluginUI : public CarlaPluginUI
  492. {
  493. public:
  494. WindowsPluginUI(Callback* const cb, const uintptr_t parentId, const bool isResizable) noexcept
  495. : CarlaPluginUI(cb, isResizable),
  496. fWindow(nullptr),
  497. fParentWindow(nullptr),
  498. fIsVisible(false),
  499. fFirstShow(true)
  500. {
  501. // FIXME
  502. static int wc_count = 0;
  503. char classNameBuf[256];
  504. std::srand((std::time(NULL)));
  505. _snprintf(classNameBuf, sizeof(classNameBuf), "CaWin_%d-%d", std::rand(), ++wc_count);
  506. classNameBuf[sizeof(classNameBuf)-1] = '\0';
  507. const HINSTANCE hInstance = GetModuleHandleA(nullptr);
  508. carla_zeroStruct(fWindowClass);
  509. fWindowClass.style = CS_OWNDC;
  510. fWindowClass.lpfnWndProc = wndProc;
  511. fWindowClass.hInstance = hInstance;
  512. fWindowClass.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
  513. fWindowClass.hCursor = LoadCursor(hInstance, IDC_ARROW);
  514. fWindowClass.lpszClassName = strdup(classNameBuf);
  515. if (!RegisterClass(&fWindowClass)) {
  516. free((void*)fWindowClass.lpszClassName);
  517. return;
  518. }
  519. int winFlags = WS_POPUPWINDOW | WS_CAPTION;
  520. if (isResizable)
  521. winFlags |= WS_SIZEBOX;
  522. fWindow = CreateWindowEx(WS_EX_TOPMOST,
  523. classNameBuf, "Carla Plugin UI", winFlags,
  524. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  525. NULL, NULL, hInstance, NULL);
  526. if (! fWindow) {
  527. UnregisterClass(fWindowClass.lpszClassName, NULL);
  528. free((void*)fWindowClass.lpszClassName);
  529. return;
  530. }
  531. SetWindowLongPtr(fWindow, GWLP_USERDATA, (LONG_PTR)this);
  532. if (parentId != 0)
  533. setTransientWinId(parentId);
  534. }
  535. ~WindowsPluginUI() override
  536. {
  537. CARLA_SAFE_ASSERT(! fIsVisible);
  538. if (fWindow != 0)
  539. {
  540. if (fIsVisible)
  541. ShowWindow(fWindow, SW_HIDE);
  542. DestroyWindow(fWindow);
  543. fWindow = 0;
  544. }
  545. // FIXME
  546. UnregisterClass(fWindowClass.lpszClassName, NULL);
  547. free((void*)fWindowClass.lpszClassName);
  548. }
  549. void show() override
  550. {
  551. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  552. if (fFirstShow)
  553. {
  554. fFirstShow = false;
  555. RECT rectChild, rectParent;
  556. if (fParentWindow != nullptr &&
  557. GetWindowRect(fWindow, &rectChild) &&
  558. GetWindowRect(fParentWindow, &rectParent))
  559. {
  560. SetWindowPos(fWindow, fParentWindow,
  561. rectParent.left + (rectChild.right-rectChild.left)/2,
  562. rectParent.top + (rectChild.bottom-rectChild.top)/2,
  563. 0, 0, SWP_SHOWWINDOW|SWP_NOSIZE);
  564. }
  565. else
  566. {
  567. ShowWindow(fWindow, SW_SHOWNORMAL);
  568. }
  569. }
  570. else
  571. {
  572. ShowWindow(fWindow, SW_RESTORE);
  573. }
  574. fIsVisible = true;
  575. UpdateWindow(fWindow);
  576. }
  577. void hide() override
  578. {
  579. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  580. ShowWindow(fWindow, SW_HIDE);
  581. fIsVisible = false;
  582. UpdateWindow(fWindow);
  583. }
  584. void idle() override
  585. {
  586. if (fIsIdling || fWindow == 0)
  587. return;
  588. MSG msg;
  589. fIsIdling = true;
  590. while (::PeekMessage(&msg, fWindow, 0, 0, PM_REMOVE))
  591. {
  592. switch (msg.message)
  593. {
  594. case WM_QUIT:
  595. case PUGL_LOCAL_CLOSE_MSG:
  596. fIsVisible = false;
  597. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  598. fCallback->handlePluginUIClosed();
  599. break;
  600. }
  601. DispatchMessageA(&msg);
  602. }
  603. fIsIdling = false;
  604. }
  605. LRESULT checkAndHandleMessage(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  606. {
  607. if (fWindow == hwnd)
  608. {
  609. switch (message)
  610. {
  611. case WM_QUIT:
  612. case PUGL_LOCAL_CLOSE_MSG:
  613. fIsVisible = false;
  614. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  615. fCallback->handlePluginUIClosed();
  616. break;
  617. }
  618. }
  619. return DefWindowProcA(hwnd, message, wParam, lParam);
  620. }
  621. void focus() override
  622. {
  623. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  624. SetForegroundWindow(fWindow);
  625. SetActiveWindow(fWindow);
  626. SetFocus(fWindow);
  627. }
  628. void setSize(const uint width, const uint height, const bool forceUpdate) override
  629. {
  630. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  631. const int winFlags = WS_POPUPWINDOW | WS_CAPTION | (fIsResizable ? WS_SIZEBOX : 0x0);
  632. RECT wr = { 0, 0, static_cast<long>(width), static_cast<long>(height) };
  633. AdjustWindowRectEx(&wr, winFlags, FALSE, WS_EX_TOPMOST);
  634. SetWindowPos(fWindow, 0, 0, 0, wr.right-wr.left, wr.bottom-wr.top,
  635. SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
  636. if (forceUpdate)
  637. UpdateWindow(fWindow);
  638. }
  639. void setTitle(const char* const title) override
  640. {
  641. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  642. SetWindowTextA(fWindow, title);
  643. }
  644. void setTransientWinId(const uintptr_t winId) override
  645. {
  646. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  647. fParentWindow = (HWND)winId;
  648. SetWindowLongPtr(fWindow, GWLP_HWNDPARENT, (LONG_PTR)winId);
  649. }
  650. void setChildWindow(void* const winId) override
  651. {
  652. CARLA_SAFE_ASSERT_RETURN(winId != nullptr,);
  653. }
  654. void* getPtr() const noexcept override
  655. {
  656. return (void*)fWindow;
  657. }
  658. void* getDisplay() const noexcept
  659. {
  660. return nullptr;
  661. }
  662. private:
  663. HWND fWindow;
  664. HWND fParentWindow;
  665. WNDCLASS fWindowClass;
  666. bool fIsVisible;
  667. bool fFirstShow;
  668. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(WindowsPluginUI)
  669. };
  670. LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  671. {
  672. switch (message)
  673. {
  674. case WM_CLOSE:
  675. PostMessage(hwnd, PUGL_LOCAL_CLOSE_MSG, wParam, lParam);
  676. return 0;
  677. #if 0
  678. case WM_CREATE:
  679. PostMessage(hwnd, WM_SHOWWINDOW, TRUE, 0);
  680. return 0;
  681. case WM_DESTROY:
  682. return 0;
  683. #endif
  684. default:
  685. if (WindowsPluginUI* const ui = (WindowsPluginUI*)GetWindowLongPtr(hwnd, GWLP_USERDATA))
  686. return ui->checkAndHandleMessage(hwnd, message, wParam, lParam);
  687. return DefWindowProcA(hwnd, message, wParam, lParam);
  688. }
  689. }
  690. #endif // CARLA_OS_WIN
  691. // -----------------------------------------------------
  692. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  693. bool CarlaPluginUI::tryTransientWinIdMatch(const uintptr_t pid, const char* const uiTitle, const uintptr_t winId, const bool centerUI)
  694. {
  695. CARLA_SAFE_ASSERT_RETURN(uiTitle != nullptr && uiTitle[0] != '\0', true);
  696. CARLA_SAFE_ASSERT_RETURN(winId != 0, true);
  697. #if defined(HAVE_X11)
  698. struct ScopedDisplay {
  699. Display* display;
  700. ScopedDisplay() : display(XOpenDisplay(nullptr)) {}
  701. ~ScopedDisplay() { if (display!=nullptr) XCloseDisplay(display); }
  702. // c++ compat stuff
  703. CARLA_PREVENT_HEAP_ALLOCATION
  704. CARLA_DECLARE_NON_COPY_CLASS(ScopedDisplay)
  705. };
  706. struct ScopedFreeData {
  707. union {
  708. char* data;
  709. uchar* udata;
  710. };
  711. ScopedFreeData(char* d) : data(d) {}
  712. ScopedFreeData(uchar* d) : udata(d) {}
  713. ~ScopedFreeData() { XFree(data); }
  714. // c++ compat stuff
  715. CARLA_PREVENT_HEAP_ALLOCATION
  716. CARLA_DECLARE_NON_COPY_CLASS(ScopedFreeData)
  717. };
  718. const ScopedDisplay sd;
  719. CARLA_SAFE_ASSERT_RETURN(sd.display != nullptr, true);
  720. const Window rootWindow(DefaultRootWindow(sd.display));
  721. const Atom _ncl = XInternAtom(sd.display, "_NET_CLIENT_LIST" , False);
  722. const Atom _nwn = XInternAtom(sd.display, "_NET_WM_NAME", False);
  723. const Atom _nwp = XInternAtom(sd.display, "_NET_WM_PID", False);
  724. const Atom utf8 = XInternAtom(sd.display, "UTF8_STRING", True);
  725. Atom actualType;
  726. int actualFormat;
  727. ulong numWindows, bytesAfter;
  728. uchar* data = nullptr;
  729. int status = XGetWindowProperty(sd.display, rootWindow, _ncl, 0L, (~0L), False, AnyPropertyType, &actualType, &actualFormat, &numWindows, &bytesAfter, &data);
  730. CARLA_SAFE_ASSERT_RETURN(data != nullptr, true);
  731. const ScopedFreeData sfd(data);
  732. CARLA_SAFE_ASSERT_RETURN(status == Success, true);
  733. CARLA_SAFE_ASSERT_RETURN(actualFormat == 32, true);
  734. CARLA_SAFE_ASSERT_RETURN(numWindows != 0, true);
  735. Window* windows = (Window*)data;
  736. Window lastGoodWindowPID = 0, lastGoodWindowNameSimple = 0, lastGoodWindowNameUTF8 = 0;
  737. for (ulong i = 0; i < numWindows; i++)
  738. {
  739. const Window window(windows[i]);
  740. CARLA_SAFE_ASSERT_CONTINUE(window != 0);
  741. // ------------------------------------------------
  742. // try using pid
  743. if (pid != 0)
  744. {
  745. ulong pidSize;
  746. uchar* pidData = nullptr;
  747. status = XGetWindowProperty(sd.display, window, _nwp, 0L, (~0L), False, XA_CARDINAL, &actualType, &actualFormat, &pidSize, &bytesAfter, &pidData);
  748. if (pidData != nullptr)
  749. {
  750. const ScopedFreeData sfd2(pidData);
  751. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  752. CARLA_SAFE_ASSERT_CONTINUE(pidSize != 0);
  753. if (*(ulong*)pidData == static_cast<ulong>(pid))
  754. lastGoodWindowPID = window;
  755. }
  756. }
  757. // ------------------------------------------------
  758. // try using name (UTF-8)
  759. ulong nameSize;
  760. uchar* nameData = nullptr;
  761. status = XGetWindowProperty(sd.display, window, _nwn, 0L, (~0L), False, utf8, &actualType, &actualFormat, &nameSize, &bytesAfter, &nameData);
  762. if (nameData != nullptr)
  763. {
  764. const ScopedFreeData sfd2(nameData);
  765. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  766. if (nameSize != 0 && std::strstr((const char*)nameData, uiTitle) != nullptr)
  767. lastGoodWindowNameUTF8 = window;
  768. }
  769. // ------------------------------------------------
  770. // try using name (simple)
  771. char* wmName = nullptr;
  772. status = XFetchName(sd.display, window, &wmName);
  773. if (wmName != nullptr)
  774. {
  775. const ScopedFreeData sfd2(wmName);
  776. CARLA_SAFE_ASSERT_CONTINUE(status != 0);
  777. if (std::strstr(wmName, uiTitle) != nullptr)
  778. lastGoodWindowNameSimple = window;
  779. }
  780. }
  781. if (lastGoodWindowPID == 0 && lastGoodWindowNameSimple == 0 && lastGoodWindowNameUTF8 == 0)
  782. return false;
  783. Window windowToMap;
  784. if (lastGoodWindowPID != 0)
  785. {
  786. if (lastGoodWindowPID == lastGoodWindowNameSimple && lastGoodWindowPID == lastGoodWindowNameUTF8)
  787. {
  788. carla_stdout("Match found using pid, simple and UTF-8 name all at once, nice!");
  789. windowToMap = lastGoodWindowPID;
  790. }
  791. else if (lastGoodWindowPID == lastGoodWindowNameUTF8)
  792. {
  793. carla_stdout("Match found using pid and UTF-8 name");
  794. windowToMap = lastGoodWindowPID;
  795. }
  796. else if (lastGoodWindowPID == lastGoodWindowNameSimple)
  797. {
  798. carla_stdout("Match found using pid and simple name");
  799. windowToMap = lastGoodWindowPID;
  800. }
  801. else
  802. {
  803. carla_stdout("Match found using pid");
  804. windowToMap = lastGoodWindowPID;
  805. }
  806. }
  807. else if (lastGoodWindowNameUTF8 != 0)
  808. {
  809. if (lastGoodWindowNameUTF8 == lastGoodWindowNameSimple)
  810. {
  811. carla_stdout("Match found using simple and UTF-8 name");
  812. windowToMap = lastGoodWindowNameUTF8;
  813. }
  814. else
  815. {
  816. carla_stdout("Match found using simple and UTF-8 name");
  817. windowToMap = lastGoodWindowNameUTF8;
  818. }
  819. }
  820. else
  821. {
  822. carla_stdout("Match found using simple name");
  823. windowToMap = lastGoodWindowNameSimple;
  824. }
  825. const Atom _nwt = XInternAtom(sd.display ,"_NET_WM_STATE", False);
  826. const Atom _nws[2] = {
  827. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_TASKBAR", False),
  828. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_PAGER", False)
  829. };
  830. XChangeProperty(sd.display, windowToMap, _nwt, XA_ATOM, 32, PropModeAppend, (const uchar*)_nws, 2);
  831. const Atom _nwi = XInternAtom(sd.display, "_NET_WM_ICON", False);
  832. XChangeProperty(sd.display, windowToMap, _nwi, XA_CARDINAL, 32, PropModeReplace, (const uchar*)sCarlaX11Icon, sCarlaX11IconSize);
  833. const Window hostWinId((Window)winId);
  834. XSetTransientForHint(sd.display, windowToMap, hostWinId);
  835. if (centerUI && false /* moving the window after being shown isn't pretty... */)
  836. {
  837. int hostX, hostY, pluginX, pluginY;
  838. uint hostWidth, hostHeight, pluginWidth, pluginHeight, border, depth;
  839. Window retWindow;
  840. if (XGetGeometry(sd.display, hostWinId, &retWindow, &hostX, &hostY, &hostWidth, &hostHeight, &border, &depth) != 0 &&
  841. XGetGeometry(sd.display, windowToMap, &retWindow, &pluginX, &pluginY, &pluginWidth, &pluginHeight, &border, &depth) != 0)
  842. {
  843. if (XTranslateCoordinates(sd.display, hostWinId, rootWindow, hostX, hostY, &hostX, &hostY, &retWindow) == True &&
  844. XTranslateCoordinates(sd.display, windowToMap, rootWindow, pluginX, pluginY, &pluginX, &pluginY, &retWindow) == True)
  845. {
  846. const int newX = hostX + int(hostWidth/2 - pluginWidth/2);
  847. const int newY = hostY + int(hostHeight/2 - pluginHeight/2);
  848. XMoveWindow(sd.display, windowToMap, newX, newY);
  849. }
  850. }
  851. }
  852. // focusing the host UI and then the plugin UI forces the WM to repaint the plugin window icon
  853. XRaiseWindow(sd.display, hostWinId);
  854. XSetInputFocus(sd.display, hostWinId, RevertToPointerRoot, CurrentTime);
  855. XRaiseWindow(sd.display, windowToMap);
  856. XSetInputFocus(sd.display, windowToMap, RevertToPointerRoot, CurrentTime);
  857. XFlush(sd.display);
  858. return true;
  859. #endif
  860. #ifdef CARLA_OS_MAC
  861. uint const hints = kCGWindowListOptionOnScreenOnly|kCGWindowListExcludeDesktopElements;
  862. CFArrayRef const windowListRef = CGWindowListCopyWindowInfo(hints, kCGNullWindowID);
  863. const NSArray* const windowList = (const NSArray*)windowListRef;
  864. int windowToMap, windowWithPID = 0, windowWithNameAndPID = 0;
  865. const NSDictionary* entry;
  866. for (entry in windowList)
  867. {
  868. // FIXME: is this needed? is old version safe?
  869. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  870. if ([entry[(id)kCGWindowSharingState] intValue] == kCGWindowSharingNone)
  871. continue;
  872. NSString* const windowName = entry[(id)kCGWindowName];
  873. int const windowNumber = [entry[(id)kCGWindowNumber] intValue];
  874. uintptr_t const windowPID = [entry[(id)kCGWindowOwnerPID] intValue];
  875. #else
  876. if ([[entry objectForKey:(id)kCGWindowSharingState] intValue] == kCGWindowSharingNone)
  877. continue;
  878. NSString* const windowName = [entry objectForKey:(id)kCGWindowName];
  879. int const windowNumber = [[entry objectForKey:(id)kCGWindowNumber] intValue];
  880. uintptr_t const windowPID = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue];
  881. #endif
  882. if (windowPID != pid)
  883. continue;
  884. windowWithPID = windowNumber;
  885. if (windowName != nullptr && std::strcmp([windowName UTF8String], uiTitle) == 0)
  886. windowWithNameAndPID = windowNumber;
  887. }
  888. CFRelease(windowListRef);
  889. if (windowWithNameAndPID != 0)
  890. {
  891. carla_stdout("Match found using pid and name");
  892. windowToMap = windowWithNameAndPID;
  893. }
  894. else if (windowWithPID != 0)
  895. {
  896. carla_stdout("Match found using pid");
  897. windowToMap = windowWithPID;
  898. }
  899. else
  900. {
  901. return false;
  902. }
  903. NSWindow* const parentWindow = [NSApp windowWithWindowNumber:winId];
  904. CARLA_SAFE_ASSERT_RETURN(parentWindow != nullptr, false);
  905. [parentWindow orderWindow:NSWindowBelow
  906. relativeTo:windowToMap];
  907. return true;
  908. #endif
  909. #ifdef CARLA_OS_WIN
  910. if (HWND const childWindow = FindWindowA(nullptr, uiTitle))
  911. {
  912. HWND const parentWindow = (HWND)winId;
  913. SetWindowLongPtr(childWindow, GWLP_HWNDPARENT, (LONG_PTR)parentWindow);
  914. if (centerUI)
  915. {
  916. RECT rectChild, rectParent;
  917. if (GetWindowRect(childWindow, &rectChild) && GetWindowRect(parentWindow, &rectParent))
  918. {
  919. SetWindowPos(childWindow, parentWindow,
  920. rectParent.left + (rectChild.right-rectChild.left)/2,
  921. rectParent.top + (rectChild.bottom-rectChild.top)/2,
  922. 0, 0, SWP_NOSIZE);
  923. }
  924. }
  925. carla_stdout("Match found using window name");
  926. return true;
  927. }
  928. return false;
  929. #endif
  930. // fallback, may be unused
  931. return true;
  932. (void)pid; (void)centerUI;
  933. }
  934. #endif // BUILD_BRIDGE_ALTERNATIVE_ARCH
  935. // -----------------------------------------------------
  936. #ifdef HAVE_X11
  937. CarlaPluginUI* CarlaPluginUI::newX11(Callback* cb, uintptr_t parentId, bool isResizable)
  938. {
  939. return new X11PluginUI(cb, parentId, isResizable);
  940. }
  941. #endif
  942. #ifdef CARLA_OS_MAC
  943. CarlaPluginUI* CarlaPluginUI::newCocoa(Callback* cb, uintptr_t parentId, bool isResizable)
  944. {
  945. return new CocoaPluginUI(cb, parentId, isResizable);
  946. }
  947. #endif
  948. #ifdef CARLA_OS_WIN
  949. CarlaPluginUI* CarlaPluginUI::newWindows(Callback* cb, uintptr_t parentId, bool isResizable)
  950. {
  951. return new WindowsPluginUI(cb, parentId, isResizable);
  952. }
  953. #endif
  954. // -----------------------------------------------------