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.

1220 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) acceptsFirstResponder;
  306. - (BOOL) canBecomeKeyWindow;
  307. - (BOOL) canBecomeMainWindow;
  308. - (BOOL) windowShouldClose:(id)sender;
  309. - (NSSize) windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize;
  310. @end
  311. @implementation CarlaPluginWindow
  312. - (id)initWithContentRect:(NSRect)contentRect
  313. styleMask:(unsigned int)aStyle
  314. backing:(NSBackingStoreType)bufferingType
  315. defer:(BOOL)flag
  316. {
  317. callback = nil;
  318. NSWindow* result = [super initWithContentRect:contentRect
  319. styleMask:(NSClosableWindowMask |
  320. NSTitledWindowMask |
  321. NSResizableWindowMask)
  322. backing:NSBackingStoreBuffered
  323. defer:YES];
  324. [result setIsVisible:NO];
  325. [result setAcceptsMouseMovedEvents:YES];
  326. [result setContentSize:NSMakeSize(18, 100)];
  327. return (CarlaPluginWindow*)result;
  328. // unused
  329. (void)aStyle; (void)bufferingType; (void)flag;
  330. }
  331. - (void)setCallback:(CarlaPluginUI::Callback*)cb
  332. {
  333. callback = cb;
  334. }
  335. - (BOOL)acceptsFirstResponder
  336. {
  337. return YES;
  338. }
  339. - (BOOL)canBecomeKeyWindow
  340. {
  341. return YES;
  342. }
  343. - (BOOL)canBecomeMainWindow
  344. {
  345. return NO;
  346. }
  347. - (BOOL)windowShouldClose:(id)sender
  348. {
  349. if (callback != nil)
  350. callback->handlePluginUIClosed();
  351. return YES;
  352. // unused
  353. (void)sender;
  354. }
  355. - (NSSize) windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize
  356. {
  357. if (callback != nil)
  358. callback->handlePluginUIResized(frameSize.width, frameSize.height);
  359. return frameSize;
  360. // unused
  361. (void)sender;
  362. }
  363. @end
  364. class CocoaPluginUI : public CarlaPluginUI
  365. {
  366. public:
  367. CocoaPluginUI(Callback* const cb, const uintptr_t parentId, const bool isResizable) noexcept
  368. : CarlaPluginUI(cb, isResizable),
  369. fView(nullptr),
  370. fWindow(nullptr)
  371. {
  372. carla_debug("CocoaPluginUI::CocoaPluginUI(%p, " P_UINTPTR, "%s)", cb, parentId, bool2str(isResizable));
  373. const CarlaBackend::AutoNSAutoreleasePool arp;
  374. fView = [[NSView new]retain];
  375. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,)
  376. [fView setHidden:YES];
  377. if (isResizable)
  378. [fView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
  379. fWindow = [[CarlaPluginWindow new]retain];
  380. if (fWindow == nullptr)
  381. {
  382. [fView release];
  383. fView = nullptr;
  384. return;
  385. }
  386. if (! isResizable)
  387. [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
  388. [fWindow setCallback:cb];
  389. [fWindow setContentView:fView];
  390. [fWindow makeFirstResponder:fView];
  391. [fWindow makeKeyAndOrderFront:fWindow];
  392. [fWindow center];
  393. if (parentId != 0)
  394. setTransientWinId(parentId);
  395. }
  396. ~CocoaPluginUI() override
  397. {
  398. carla_debug("CocoaPluginUI::~CocoaPluginUI()");
  399. if (fView == nullptr)
  400. return;
  401. [fView removeFromSuperview];
  402. [fWindow close];
  403. [fView release];
  404. [fWindow release];
  405. }
  406. void show() override
  407. {
  408. carla_debug("CocoaPluginUI::show()");
  409. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,);
  410. [fView setHidden:NO];
  411. [fWindow setIsVisible:YES];
  412. }
  413. void hide() override
  414. {
  415. carla_debug("CocoaPluginUI::hide()");
  416. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,);
  417. [fWindow setIsVisible:NO];
  418. [fView setHidden:YES];
  419. }
  420. void idle() override
  421. {
  422. // carla_debug("CocoaPluginUI::idle()");
  423. }
  424. void focus() override
  425. {
  426. carla_debug("CocoaPluginUI::focus()");
  427. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  428. [fWindow makeKeyWindow];
  429. [NSApp activateIgnoringOtherApps:YES];
  430. }
  431. void setSize(const uint width, const uint height, const bool forceUpdate) override
  432. {
  433. carla_debug("CocoaPluginUI::setSize(%u, %u, %s)", width, height, bool2str(forceUpdate));
  434. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  435. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,);
  436. [fView setFrame:NSMakeRect(0, 0, width, height)];
  437. const NSSize size = NSMakeSize(width, height);
  438. [fWindow setContentSize:size];
  439. if (fIsResizable)
  440. {
  441. [fWindow setContentMinSize:NSMakeSize(1, 1)];
  442. [fWindow setContentMaxSize:NSMakeSize(99999, 99999)];
  443. [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:NO];
  444. }
  445. else
  446. {
  447. [fWindow setContentMinSize:size];
  448. [fWindow setContentMaxSize:size];
  449. [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
  450. }
  451. if (forceUpdate)
  452. {
  453. // FIXME, not enough
  454. [fView setNeedsDisplay:YES];
  455. }
  456. }
  457. void setTitle(const char* const title) override
  458. {
  459. carla_debug("CocoaPluginUI::setTitle(\"%s\")", title);
  460. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  461. NSString* titleString = [[NSString alloc]
  462. initWithBytes:title
  463. length:strlen(title)
  464. encoding:NSUTF8StringEncoding];
  465. [fWindow setTitle:titleString];
  466. }
  467. void setTransientWinId(const uintptr_t winId) override
  468. {
  469. carla_debug("CocoaPluginUI::setTransientWinId(" P_UINTPTR ")", winId);
  470. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  471. NSWindow* const parentWindow = [NSApp windowWithWindowNumber:winId];
  472. CARLA_SAFE_ASSERT_RETURN(parentWindow != nullptr,);
  473. [parentWindow addChildWindow:fWindow
  474. ordered:NSWindowAbove];
  475. }
  476. void setChildWindow(void* const window) override
  477. {
  478. carla_debug("CocoaPluginUI::setChildWindow(%p)", window);
  479. CARLA_SAFE_ASSERT_RETURN(window != nullptr,);
  480. }
  481. void* getPtr() const noexcept override
  482. {
  483. carla_debug("CocoaPluginUI::getPtr()");
  484. return (void*)fView;
  485. }
  486. void* getDisplay() const noexcept
  487. {
  488. carla_debug("CocoaPluginUI::getDisplay()");
  489. return (void*)fWindow;
  490. }
  491. private:
  492. NSView* fView;
  493. CarlaPluginWindow* fWindow;
  494. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CocoaPluginUI)
  495. };
  496. #endif // CARLA_OS_MAC
  497. // ---------------------------------------------------------------------------------------------------------------------
  498. // Windows
  499. #ifdef CARLA_OS_WIN
  500. #define PUGL_LOCAL_CLOSE_MSG (WM_USER + 50)
  501. static LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  502. class WindowsPluginUI : public CarlaPluginUI
  503. {
  504. public:
  505. WindowsPluginUI(Callback* const cb, const uintptr_t parentId, const bool isResizable) noexcept
  506. : CarlaPluginUI(cb, isResizable),
  507. fWindow(nullptr),
  508. fChildWindow(nullptr),
  509. fParentWindow(nullptr),
  510. fIsVisible(false),
  511. fFirstShow(true)
  512. {
  513. // FIXME
  514. static int wc_count = 0;
  515. char classNameBuf[256];
  516. std::srand((std::time(NULL)));
  517. _snprintf(classNameBuf, sizeof(classNameBuf), "CaWin_%d-%d", std::rand(), ++wc_count);
  518. classNameBuf[sizeof(classNameBuf)-1] = '\0';
  519. const HINSTANCE hInstance = GetModuleHandleA(nullptr);
  520. carla_zeroStruct(fWindowClass);
  521. fWindowClass.style = CS_OWNDC;
  522. fWindowClass.lpfnWndProc = wndProc;
  523. fWindowClass.hInstance = hInstance;
  524. fWindowClass.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
  525. fWindowClass.hCursor = LoadCursor(hInstance, IDC_ARROW);
  526. fWindowClass.lpszClassName = strdup(classNameBuf);
  527. if (!RegisterClass(&fWindowClass)) {
  528. free((void*)fWindowClass.lpszClassName);
  529. return;
  530. }
  531. int winFlags = WS_POPUPWINDOW | WS_CAPTION;
  532. if (isResizable)
  533. {
  534. // not supported right now
  535. // winFlags |= WS_SIZEBOX;
  536. }
  537. fWindow = CreateWindowEx(WS_EX_TOPMOST,
  538. classNameBuf, "Carla Plugin UI", winFlags,
  539. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  540. NULL, NULL, hInstance, NULL);
  541. if (! fWindow) {
  542. UnregisterClass(fWindowClass.lpszClassName, NULL);
  543. free((void*)fWindowClass.lpszClassName);
  544. return;
  545. }
  546. SetWindowLongPtr(fWindow, GWLP_USERDATA, (LONG_PTR)this);
  547. if (parentId != 0)
  548. setTransientWinId(parentId);
  549. }
  550. ~WindowsPluginUI() override
  551. {
  552. CARLA_SAFE_ASSERT(! fIsVisible);
  553. if (fWindow != 0)
  554. {
  555. if (fIsVisible)
  556. ShowWindow(fWindow, SW_HIDE);
  557. DestroyWindow(fWindow);
  558. fWindow = 0;
  559. }
  560. // FIXME
  561. UnregisterClass(fWindowClass.lpszClassName, NULL);
  562. free((void*)fWindowClass.lpszClassName);
  563. }
  564. void show() override
  565. {
  566. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  567. if (fFirstShow)
  568. {
  569. fFirstShow = false;
  570. RECT rectChild, rectParent;
  571. if (fParentWindow != nullptr &&
  572. GetWindowRect(fWindow, &rectChild) &&
  573. GetWindowRect(fParentWindow, &rectParent))
  574. {
  575. SetWindowPos(fWindow, fParentWindow,
  576. rectParent.left + (rectChild.right-rectChild.left)/2,
  577. rectParent.top + (rectChild.bottom-rectChild.top)/2,
  578. 0, 0, SWP_SHOWWINDOW|SWP_NOSIZE);
  579. }
  580. else
  581. {
  582. ShowWindow(fWindow, SW_SHOWNORMAL);
  583. }
  584. }
  585. else
  586. {
  587. ShowWindow(fWindow, SW_RESTORE);
  588. }
  589. fIsVisible = true;
  590. UpdateWindow(fWindow);
  591. }
  592. void hide() override
  593. {
  594. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  595. ShowWindow(fWindow, SW_HIDE);
  596. fIsVisible = false;
  597. UpdateWindow(fWindow);
  598. }
  599. void idle() override
  600. {
  601. if (fIsIdling || fWindow == 0)
  602. return;
  603. MSG msg;
  604. fIsIdling = true;
  605. while (::PeekMessage(&msg, fWindow, 0, 0, PM_REMOVE))
  606. {
  607. switch (msg.message)
  608. {
  609. case WM_QUIT:
  610. case PUGL_LOCAL_CLOSE_MSG:
  611. fIsVisible = false;
  612. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  613. fCallback->handlePluginUIClosed();
  614. break;
  615. }
  616. DispatchMessageA(&msg);
  617. }
  618. fIsIdling = false;
  619. }
  620. LRESULT checkAndHandleMessage(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  621. {
  622. if (fWindow == hwnd)
  623. {
  624. switch (message)
  625. {
  626. case WM_QUIT:
  627. case PUGL_LOCAL_CLOSE_MSG:
  628. fIsVisible = false;
  629. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  630. fCallback->handlePluginUIClosed();
  631. break;
  632. }
  633. }
  634. return DefWindowProcA(hwnd, message, wParam, lParam);
  635. }
  636. void focus() override
  637. {
  638. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  639. SetForegroundWindow(fWindow);
  640. SetActiveWindow(fWindow);
  641. SetFocus(fWindow);
  642. }
  643. void setSize(const uint width, const uint height, const bool forceUpdate) override
  644. {
  645. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  646. const int winFlags = WS_POPUPWINDOW | WS_CAPTION | (fIsResizable ? WS_SIZEBOX : 0x0);
  647. RECT wr = { 0, 0, static_cast<long>(width), static_cast<long>(height) };
  648. AdjustWindowRectEx(&wr, winFlags, FALSE, WS_EX_TOPMOST);
  649. SetWindowPos(fWindow, 0, 0, 0, wr.right-wr.left, wr.bottom-wr.top,
  650. SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
  651. if (forceUpdate)
  652. UpdateWindow(fWindow);
  653. }
  654. void setTitle(const char* const title) override
  655. {
  656. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  657. SetWindowTextA(fWindow, title);
  658. }
  659. void setTransientWinId(const uintptr_t winId) override
  660. {
  661. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  662. fParentWindow = (HWND)winId;
  663. SetWindowLongPtr(fWindow, GWLP_HWNDPARENT, (LONG_PTR)winId);
  664. }
  665. void setChildWindow(void* const winId) override
  666. {
  667. CARLA_SAFE_ASSERT_RETURN(winId != nullptr,);
  668. fChildWindow = (HWND)fChildWindow;
  669. }
  670. void* getPtr() const noexcept override
  671. {
  672. return (void*)fWindow;
  673. }
  674. void* getDisplay() const noexcept
  675. {
  676. return nullptr;
  677. }
  678. private:
  679. HWND fWindow;
  680. HWND fChildWindow;
  681. HWND fParentWindow;
  682. WNDCLASS fWindowClass;
  683. bool fIsVisible;
  684. bool fFirstShow;
  685. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(WindowsPluginUI)
  686. };
  687. LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  688. {
  689. switch (message)
  690. {
  691. case WM_CLOSE:
  692. PostMessage(hwnd, PUGL_LOCAL_CLOSE_MSG, wParam, lParam);
  693. return 0;
  694. #if 0
  695. case WM_CREATE:
  696. PostMessage(hwnd, WM_SHOWWINDOW, TRUE, 0);
  697. return 0;
  698. case WM_DESTROY:
  699. return 0;
  700. #endif
  701. default:
  702. if (WindowsPluginUI* const ui = (WindowsPluginUI*)GetWindowLongPtr(hwnd, GWLP_USERDATA))
  703. return ui->checkAndHandleMessage(hwnd, message, wParam, lParam);
  704. return DefWindowProcA(hwnd, message, wParam, lParam);
  705. }
  706. }
  707. #endif // CARLA_OS_WIN
  708. // -----------------------------------------------------
  709. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  710. bool CarlaPluginUI::tryTransientWinIdMatch(const uintptr_t pid, const char* const uiTitle, const uintptr_t winId, const bool centerUI)
  711. {
  712. CARLA_SAFE_ASSERT_RETURN(uiTitle != nullptr && uiTitle[0] != '\0', true);
  713. CARLA_SAFE_ASSERT_RETURN(winId != 0, true);
  714. #if defined(HAVE_X11)
  715. struct ScopedDisplay {
  716. Display* display;
  717. ScopedDisplay() : display(XOpenDisplay(nullptr)) {}
  718. ~ScopedDisplay() { if (display!=nullptr) XCloseDisplay(display); }
  719. // c++ compat stuff
  720. CARLA_PREVENT_HEAP_ALLOCATION
  721. CARLA_DECLARE_NON_COPY_CLASS(ScopedDisplay)
  722. };
  723. struct ScopedFreeData {
  724. union {
  725. char* data;
  726. uchar* udata;
  727. };
  728. ScopedFreeData(char* d) : data(d) {}
  729. ScopedFreeData(uchar* d) : udata(d) {}
  730. ~ScopedFreeData() { XFree(data); }
  731. // c++ compat stuff
  732. CARLA_PREVENT_HEAP_ALLOCATION
  733. CARLA_DECLARE_NON_COPY_CLASS(ScopedFreeData)
  734. };
  735. const ScopedDisplay sd;
  736. CARLA_SAFE_ASSERT_RETURN(sd.display != nullptr, true);
  737. const Window rootWindow(DefaultRootWindow(sd.display));
  738. const Atom _ncl = XInternAtom(sd.display, "_NET_CLIENT_LIST" , False);
  739. const Atom _nwn = XInternAtom(sd.display, "_NET_WM_NAME", False);
  740. const Atom _nwp = XInternAtom(sd.display, "_NET_WM_PID", False);
  741. const Atom utf8 = XInternAtom(sd.display, "UTF8_STRING", True);
  742. Atom actualType;
  743. int actualFormat;
  744. ulong numWindows, bytesAfter;
  745. uchar* data = nullptr;
  746. int status = XGetWindowProperty(sd.display, rootWindow, _ncl, 0L, (~0L), False, AnyPropertyType, &actualType, &actualFormat, &numWindows, &bytesAfter, &data);
  747. CARLA_SAFE_ASSERT_RETURN(data != nullptr, true);
  748. const ScopedFreeData sfd(data);
  749. CARLA_SAFE_ASSERT_RETURN(status == Success, true);
  750. CARLA_SAFE_ASSERT_RETURN(actualFormat == 32, true);
  751. CARLA_SAFE_ASSERT_RETURN(numWindows != 0, true);
  752. Window* windows = (Window*)data;
  753. Window lastGoodWindowPID = 0, lastGoodWindowNameSimple = 0, lastGoodWindowNameUTF8 = 0;
  754. for (ulong i = 0; i < numWindows; i++)
  755. {
  756. const Window window(windows[i]);
  757. CARLA_SAFE_ASSERT_CONTINUE(window != 0);
  758. // ------------------------------------------------
  759. // try using pid
  760. if (pid != 0)
  761. {
  762. ulong pidSize;
  763. uchar* pidData = nullptr;
  764. status = XGetWindowProperty(sd.display, window, _nwp, 0L, (~0L), False, XA_CARDINAL, &actualType, &actualFormat, &pidSize, &bytesAfter, &pidData);
  765. if (pidData != nullptr)
  766. {
  767. const ScopedFreeData sfd2(pidData);
  768. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  769. CARLA_SAFE_ASSERT_CONTINUE(pidSize != 0);
  770. if (*(ulong*)pidData == static_cast<ulong>(pid))
  771. lastGoodWindowPID = window;
  772. }
  773. }
  774. // ------------------------------------------------
  775. // try using name (UTF-8)
  776. ulong nameSize;
  777. uchar* nameData = nullptr;
  778. status = XGetWindowProperty(sd.display, window, _nwn, 0L, (~0L), False, utf8, &actualType, &actualFormat, &nameSize, &bytesAfter, &nameData);
  779. if (nameData != nullptr)
  780. {
  781. const ScopedFreeData sfd2(nameData);
  782. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  783. if (nameSize != 0 && std::strstr((const char*)nameData, uiTitle) != nullptr)
  784. lastGoodWindowNameUTF8 = window;
  785. }
  786. // ------------------------------------------------
  787. // try using name (simple)
  788. char* wmName = nullptr;
  789. status = XFetchName(sd.display, window, &wmName);
  790. if (wmName != nullptr)
  791. {
  792. const ScopedFreeData sfd2(wmName);
  793. CARLA_SAFE_ASSERT_CONTINUE(status != 0);
  794. if (std::strstr(wmName, uiTitle) != nullptr)
  795. lastGoodWindowNameSimple = window;
  796. }
  797. }
  798. if (lastGoodWindowPID == 0 && lastGoodWindowNameSimple == 0 && lastGoodWindowNameUTF8 == 0)
  799. return false;
  800. Window windowToMap;
  801. if (lastGoodWindowPID != 0)
  802. {
  803. if (lastGoodWindowPID == lastGoodWindowNameSimple && lastGoodWindowPID == lastGoodWindowNameUTF8)
  804. {
  805. carla_stdout("Match found using pid, simple and UTF-8 name all at once, nice!");
  806. windowToMap = lastGoodWindowPID;
  807. }
  808. else if (lastGoodWindowPID == lastGoodWindowNameUTF8)
  809. {
  810. carla_stdout("Match found using pid and UTF-8 name");
  811. windowToMap = lastGoodWindowPID;
  812. }
  813. else if (lastGoodWindowPID == lastGoodWindowNameSimple)
  814. {
  815. carla_stdout("Match found using pid and simple name");
  816. windowToMap = lastGoodWindowPID;
  817. }
  818. else
  819. {
  820. carla_stdout("Match found using pid");
  821. windowToMap = lastGoodWindowPID;
  822. }
  823. }
  824. else if (lastGoodWindowNameUTF8 != 0)
  825. {
  826. if (lastGoodWindowNameUTF8 == lastGoodWindowNameSimple)
  827. {
  828. carla_stdout("Match found using simple and UTF-8 name");
  829. windowToMap = lastGoodWindowNameUTF8;
  830. }
  831. else
  832. {
  833. carla_stdout("Match found using simple and UTF-8 name");
  834. windowToMap = lastGoodWindowNameUTF8;
  835. }
  836. }
  837. else
  838. {
  839. carla_stdout("Match found using simple name");
  840. windowToMap = lastGoodWindowNameSimple;
  841. }
  842. const Atom _nwt = XInternAtom(sd.display ,"_NET_WM_STATE", False);
  843. const Atom _nws[2] = {
  844. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_TASKBAR", False),
  845. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_PAGER", False)
  846. };
  847. XChangeProperty(sd.display, windowToMap, _nwt, XA_ATOM, 32, PropModeAppend, (const uchar*)_nws, 2);
  848. const Atom _nwi = XInternAtom(sd.display, "_NET_WM_ICON", False);
  849. XChangeProperty(sd.display, windowToMap, _nwi, XA_CARDINAL, 32, PropModeReplace, (const uchar*)sCarlaX11Icon, sCarlaX11IconSize);
  850. const Window hostWinId((Window)winId);
  851. XSetTransientForHint(sd.display, windowToMap, hostWinId);
  852. if (centerUI && false /* moving the window after being shown isn't pretty... */)
  853. {
  854. int hostX, hostY, pluginX, pluginY;
  855. uint hostWidth, hostHeight, pluginWidth, pluginHeight, border, depth;
  856. Window retWindow;
  857. if (XGetGeometry(sd.display, hostWinId, &retWindow, &hostX, &hostY, &hostWidth, &hostHeight, &border, &depth) != 0 &&
  858. XGetGeometry(sd.display, windowToMap, &retWindow, &pluginX, &pluginY, &pluginWidth, &pluginHeight, &border, &depth) != 0)
  859. {
  860. if (XTranslateCoordinates(sd.display, hostWinId, rootWindow, hostX, hostY, &hostX, &hostY, &retWindow) == True &&
  861. XTranslateCoordinates(sd.display, windowToMap, rootWindow, pluginX, pluginY, &pluginX, &pluginY, &retWindow) == True)
  862. {
  863. const int newX = hostX + int(hostWidth/2 - pluginWidth/2);
  864. const int newY = hostY + int(hostHeight/2 - pluginHeight/2);
  865. XMoveWindow(sd.display, windowToMap, newX, newY);
  866. }
  867. }
  868. }
  869. // focusing the host UI and then the plugin UI forces the WM to repaint the plugin window icon
  870. XRaiseWindow(sd.display, hostWinId);
  871. XSetInputFocus(sd.display, hostWinId, RevertToPointerRoot, CurrentTime);
  872. XRaiseWindow(sd.display, windowToMap);
  873. XSetInputFocus(sd.display, windowToMap, RevertToPointerRoot, CurrentTime);
  874. XFlush(sd.display);
  875. return true;
  876. #endif
  877. #ifdef CARLA_OS_MAC
  878. uint const hints = kCGWindowListOptionOnScreenOnly|kCGWindowListExcludeDesktopElements;
  879. CFArrayRef const windowListRef = CGWindowListCopyWindowInfo(hints, kCGNullWindowID);
  880. const NSArray* const windowList = (const NSArray*)windowListRef;
  881. int windowToMap, windowWithPID = 0, windowWithNameAndPID = 0;
  882. const NSDictionary* entry;
  883. for (entry in windowList)
  884. {
  885. // FIXME: is this needed? is old version safe?
  886. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  887. if ([entry[(id)kCGWindowSharingState] intValue] == kCGWindowSharingNone)
  888. continue;
  889. NSString* const windowName = entry[(id)kCGWindowName];
  890. int const windowNumber = [entry[(id)kCGWindowNumber] intValue];
  891. uintptr_t const windowPID = [entry[(id)kCGWindowOwnerPID] intValue];
  892. #else
  893. if ([[entry objectForKey:(id)kCGWindowSharingState] intValue] == kCGWindowSharingNone)
  894. continue;
  895. NSString* const windowName = [entry objectForKey:(id)kCGWindowName];
  896. int const windowNumber = [[entry objectForKey:(id)kCGWindowNumber] intValue];
  897. uintptr_t const windowPID = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue];
  898. #endif
  899. if (windowPID != pid)
  900. continue;
  901. windowWithPID = windowNumber;
  902. if (windowName != nullptr && std::strcmp([windowName UTF8String], uiTitle) == 0)
  903. windowWithNameAndPID = windowNumber;
  904. }
  905. CFRelease(windowListRef);
  906. if (windowWithNameAndPID != 0)
  907. {
  908. carla_stdout("Match found using pid and name");
  909. windowToMap = windowWithNameAndPID;
  910. }
  911. else if (windowWithPID != 0)
  912. {
  913. carla_stdout("Match found using pid");
  914. windowToMap = windowWithPID;
  915. }
  916. else
  917. {
  918. return false;
  919. }
  920. NSWindow* const parentWindow = [NSApp windowWithWindowNumber:winId];
  921. CARLA_SAFE_ASSERT_RETURN(parentWindow != nullptr, false);
  922. [parentWindow orderWindow:NSWindowBelow
  923. relativeTo:windowToMap];
  924. return true;
  925. #endif
  926. #ifdef CARLA_OS_WIN
  927. if (HWND const childWindow = FindWindowA(nullptr, uiTitle))
  928. {
  929. HWND const parentWindow = (HWND)winId;
  930. SetWindowLongPtr(childWindow, GWLP_HWNDPARENT, (LONG_PTR)parentWindow);
  931. if (centerUI)
  932. {
  933. RECT rectChild, rectParent;
  934. if (GetWindowRect(childWindow, &rectChild) && GetWindowRect(parentWindow, &rectParent))
  935. {
  936. SetWindowPos(childWindow, parentWindow,
  937. rectParent.left + (rectChild.right-rectChild.left)/2,
  938. rectParent.top + (rectChild.bottom-rectChild.top)/2,
  939. 0, 0, SWP_NOSIZE);
  940. }
  941. }
  942. carla_stdout("Match found using window name");
  943. return true;
  944. }
  945. return false;
  946. #endif
  947. // fallback, may be unused
  948. return true;
  949. (void)pid; (void)centerUI;
  950. }
  951. #endif // BUILD_BRIDGE_ALTERNATIVE_ARCH
  952. // -----------------------------------------------------
  953. #ifdef HAVE_X11
  954. CarlaPluginUI* CarlaPluginUI::newX11(Callback* cb, uintptr_t parentId, bool isResizable)
  955. {
  956. return new X11PluginUI(cb, parentId, isResizable);
  957. }
  958. #endif
  959. #ifdef CARLA_OS_MAC
  960. CarlaPluginUI* CarlaPluginUI::newCocoa(Callback* cb, uintptr_t parentId, bool isResizable)
  961. {
  962. return new CocoaPluginUI(cb, parentId, isResizable);
  963. }
  964. #endif
  965. #ifdef CARLA_OS_WIN
  966. CarlaPluginUI* CarlaPluginUI::newWindows(Callback* cb, uintptr_t parentId, bool isResizable)
  967. {
  968. return new WindowsPluginUI(cb, parentId, isResizable);
  969. }
  970. #endif
  971. // -----------------------------------------------------