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.

1503 lines
47KB

  1. /*
  2. * Carla Plugin UI
  3. * Copyright (C) 2014-2022 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 <pthread.h>
  21. # include <sys/types.h>
  22. # include <X11/Xatom.h>
  23. # include <X11/Xlib.h>
  24. # include <X11/Xutil.h>
  25. # include "CarlaPluginUI_X11Icon.hpp"
  26. #endif
  27. #ifdef CARLA_OS_MAC
  28. # include "CarlaMacUtils.hpp"
  29. # import <Cocoa/Cocoa.h>
  30. #endif
  31. #ifdef CARLA_OS_WIN
  32. # include <ctime>
  33. # include "water/common.hpp"
  34. #endif
  35. #ifndef CARLA_PLUGIN_UI_CLASS_PREFIX
  36. # error CARLA_PLUGIN_UI_CLASS_PREFIX undefined
  37. #endif
  38. // ---------------------------------------------------------------------------------------------------------------------
  39. // X11
  40. #ifdef HAVE_X11
  41. typedef void (*EventProcPtr)(XEvent* ev);
  42. static const uint X11Key_Escape = 9;
  43. static bool gErrorTriggered = false;
  44. # if defined(__GNUC__) && (__GNUC__ >= 5) && ! defined(__clang__)
  45. # pragma GCC diagnostic push
  46. # pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
  47. # endif
  48. static pthread_mutex_t gErrorMutex = PTHREAD_MUTEX_INITIALIZER;
  49. # if defined(__GNUC__) && (__GNUC__ >= 5) && ! defined(__clang__)
  50. # pragma GCC diagnostic pop
  51. # endif
  52. static int temporaryErrorHandler(Display*, XErrorEvent*)
  53. {
  54. gErrorTriggered = true;
  55. return 0;
  56. }
  57. class X11PluginUI : public CarlaPluginUI
  58. {
  59. public:
  60. X11PluginUI(Callback* const cb, const uintptr_t parentId,
  61. const bool isStandalone, const bool isResizable, const bool canMonitorChildren) noexcept
  62. : CarlaPluginUI(cb, isStandalone, isResizable),
  63. fDisplay(nullptr),
  64. fHostWindow(0),
  65. fChildWindow(0),
  66. fChildWindowConfigured(false),
  67. fChildWindowMonitoring(isResizable || canMonitorChildren),
  68. fIsVisible(false),
  69. fFirstShow(true),
  70. fSetSizeCalledAtLeastOnce(false),
  71. fEventProc(nullptr)
  72. {
  73. fDisplay = XOpenDisplay(nullptr);
  74. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  75. const int screen = DefaultScreen(fDisplay);
  76. XSetWindowAttributes attr;
  77. carla_zeroStruct(attr);
  78. attr.event_mask = KeyPressMask|KeyReleaseMask|FocusChangeMask;
  79. if (fChildWindowMonitoring)
  80. attr.event_mask |= StructureNotifyMask|SubstructureNotifyMask;
  81. fHostWindow = XCreateWindow(fDisplay, RootWindow(fDisplay, screen),
  82. 0, 0, 300, 300, 0,
  83. DefaultDepth(fDisplay, screen),
  84. InputOutput,
  85. DefaultVisual(fDisplay, screen),
  86. CWBorderPixel|CWEventMask, &attr);
  87. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  88. XGrabKey(fDisplay, X11Key_Escape, AnyModifier, fHostWindow, 1, GrabModeAsync, GrabModeAsync);
  89. Atom wmDelete = XInternAtom(fDisplay, "WM_DELETE_WINDOW", True);
  90. XSetWMProtocols(fDisplay, fHostWindow, &wmDelete, 1);
  91. const pid_t pid = getpid();
  92. const Atom _nwp = XInternAtom(fDisplay, "_NET_WM_PID", False);
  93. XChangeProperty(fDisplay, fHostWindow, _nwp, XA_CARDINAL, 32, PropModeReplace, (const uchar*)&pid, 1);
  94. const Atom _nwi = XInternAtom(fDisplay, "_NET_WM_ICON", False);
  95. XChangeProperty(fDisplay, fHostWindow, _nwi, XA_CARDINAL, 32, PropModeReplace, (const uchar*)sCarlaX11Icon, sCarlaX11IconSize);
  96. const Atom _wt = XInternAtom(fDisplay, "_NET_WM_WINDOW_TYPE", False);
  97. // Setting the window to both dialog and normal will produce a decorated floating dialog
  98. // Order is important: DIALOG needs to come before NORMAL
  99. const Atom _wts[2] = {
  100. XInternAtom(fDisplay, "_NET_WM_WINDOW_TYPE_DIALOG", False),
  101. XInternAtom(fDisplay, "_NET_WM_WINDOW_TYPE_NORMAL", False)
  102. };
  103. XChangeProperty(fDisplay, fHostWindow, _wt, XA_ATOM, 32, PropModeReplace, (const uchar*)&_wts, 2);
  104. if (parentId != 0)
  105. setTransientWinId(parentId);
  106. }
  107. ~X11PluginUI() override
  108. {
  109. CARLA_SAFE_ASSERT(! fIsVisible);
  110. if (fDisplay == nullptr)
  111. return;
  112. if (fIsVisible)
  113. {
  114. XUnmapWindow(fDisplay, fHostWindow);
  115. fIsVisible = false;
  116. }
  117. if (fHostWindow != 0)
  118. {
  119. XDestroyWindow(fDisplay, fHostWindow);
  120. fHostWindow = 0;
  121. }
  122. XCloseDisplay(fDisplay);
  123. fDisplay = nullptr;
  124. }
  125. void show() override
  126. {
  127. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  128. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  129. if (fFirstShow)
  130. {
  131. if (const Window childWindow = getChildWindow())
  132. {
  133. if (! fSetSizeCalledAtLeastOnce)
  134. {
  135. int width = 0;
  136. int height = 0;
  137. XWindowAttributes attrs;
  138. carla_zeroStruct(attrs);
  139. pthread_mutex_lock(&gErrorMutex);
  140. const XErrorHandler oldErrorHandler = XSetErrorHandler(temporaryErrorHandler);
  141. gErrorTriggered = false;
  142. if (XGetWindowAttributes(fDisplay, childWindow, &attrs))
  143. {
  144. width = attrs.width;
  145. height = attrs.height;
  146. }
  147. XSetErrorHandler(oldErrorHandler);
  148. pthread_mutex_unlock(&gErrorMutex);
  149. if (width == 0 && height == 0)
  150. {
  151. XSizeHints sizeHints;
  152. carla_zeroStruct(sizeHints);
  153. if (XGetNormalHints(fDisplay, childWindow, &sizeHints))
  154. {
  155. if (sizeHints.flags & PSize)
  156. {
  157. width = sizeHints.width;
  158. height = sizeHints.height;
  159. }
  160. else if (sizeHints.flags & PBaseSize)
  161. {
  162. width = sizeHints.base_width;
  163. height = sizeHints.base_height;
  164. }
  165. }
  166. }
  167. if (width > 1 && height > 1)
  168. setSize(static_cast<uint>(width), static_cast<uint>(height), false);
  169. }
  170. const Atom _xevp = XInternAtom(fDisplay, "_XEventProc", False);
  171. pthread_mutex_lock(&gErrorMutex);
  172. const XErrorHandler oldErrorHandler(XSetErrorHandler(temporaryErrorHandler));
  173. gErrorTriggered = false;
  174. Atom actualType;
  175. int actualFormat;
  176. ulong nitems, bytesAfter;
  177. uchar* data = nullptr;
  178. XGetWindowProperty(fDisplay, childWindow, _xevp, 0, 1, False, AnyPropertyType,
  179. &actualType, &actualFormat, &nitems, &bytesAfter, &data);
  180. XSetErrorHandler(oldErrorHandler);
  181. pthread_mutex_unlock(&gErrorMutex);
  182. if (nitems == 1 && ! gErrorTriggered)
  183. {
  184. fEventProc = *reinterpret_cast<EventProcPtr*>(data);
  185. XMapRaised(fDisplay, childWindow);
  186. }
  187. }
  188. }
  189. fIsVisible = true;
  190. fFirstShow = false;
  191. XMapRaised(fDisplay, fHostWindow);
  192. XSync(fDisplay, False);
  193. }
  194. void hide() override
  195. {
  196. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  197. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  198. fIsVisible = false;
  199. XUnmapWindow(fDisplay, fHostWindow);
  200. XFlush(fDisplay);
  201. }
  202. void idle() override
  203. {
  204. // prevent recursion
  205. if (fIsIdling) return;
  206. int nextWidth = 0;
  207. int nextHeight = 0;
  208. fIsIdling = true;
  209. for (XEvent event; XPending(fDisplay) > 0;)
  210. {
  211. XNextEvent(fDisplay, &event);
  212. if (! fIsVisible)
  213. continue;
  214. char* type = nullptr;
  215. switch (event.type)
  216. {
  217. case ConfigureNotify:
  218. CARLA_SAFE_ASSERT_CONTINUE(fCallback != nullptr);
  219. CARLA_SAFE_ASSERT_CONTINUE(event.xconfigure.width > 0);
  220. CARLA_SAFE_ASSERT_CONTINUE(event.xconfigure.height > 0);
  221. if (event.xconfigure.window == fHostWindow)
  222. {
  223. const uint width = static_cast<uint>(event.xconfigure.width);
  224. const uint height = static_cast<uint>(event.xconfigure.height);
  225. if (fChildWindow != 0)
  226. {
  227. if (! fChildWindowConfigured)
  228. {
  229. pthread_mutex_lock(&gErrorMutex);
  230. const XErrorHandler oldErrorHandler = XSetErrorHandler(temporaryErrorHandler);
  231. gErrorTriggered = false;
  232. XSizeHints sizeHints;
  233. carla_zeroStruct(sizeHints);
  234. if (XGetNormalHints(fDisplay, fChildWindow, &sizeHints) && !gErrorTriggered)
  235. {
  236. XSetNormalHints(fDisplay, fHostWindow, &sizeHints);
  237. }
  238. else
  239. {
  240. carla_stdout("Caught errors while accessing child window");
  241. fChildWindow = 0;
  242. }
  243. fChildWindowConfigured = true;
  244. XSetErrorHandler(oldErrorHandler);
  245. pthread_mutex_unlock(&gErrorMutex);
  246. }
  247. if (fChildWindow != 0)
  248. XResizeWindow(fDisplay, fChildWindow, width, height);
  249. }
  250. fCallback->handlePluginUIResized(width, height);
  251. }
  252. else if (fChildWindowMonitoring && event.xconfigure.window == fChildWindow && fChildWindow != 0)
  253. {
  254. nextWidth = event.xconfigure.width;
  255. nextHeight = event.xconfigure.height;
  256. }
  257. break;
  258. case ClientMessage:
  259. type = XGetAtomName(fDisplay, event.xclient.message_type);
  260. CARLA_SAFE_ASSERT_CONTINUE(type != nullptr);
  261. if (std::strcmp(type, "WM_PROTOCOLS") == 0)
  262. {
  263. fIsVisible = false;
  264. CARLA_SAFE_ASSERT_CONTINUE(fCallback != nullptr);
  265. fCallback->handlePluginUIClosed();
  266. }
  267. break;
  268. case KeyRelease:
  269. if (event.xkey.keycode == X11Key_Escape)
  270. {
  271. fIsVisible = false;
  272. CARLA_SAFE_ASSERT_CONTINUE(fCallback != nullptr);
  273. fCallback->handlePluginUIClosed();
  274. }
  275. break;
  276. case FocusIn:
  277. if (fChildWindow == 0)
  278. fChildWindow = getChildWindow();
  279. if (fChildWindow != 0)
  280. {
  281. XWindowAttributes wa;
  282. carla_zeroStruct(wa);
  283. if (XGetWindowAttributes(fDisplay, fChildWindow, &wa) && wa.map_state == IsViewable)
  284. XSetInputFocus(fDisplay, fChildWindow, RevertToPointerRoot, CurrentTime);
  285. }
  286. break;
  287. }
  288. if (type != nullptr)
  289. XFree(type);
  290. else if (fEventProc != nullptr && event.type != FocusIn && event.type != FocusOut)
  291. fEventProc(&event);
  292. }
  293. if (nextWidth != 0 && nextHeight != 0 && fChildWindow != 0)
  294. {
  295. XSizeHints sizeHints;
  296. carla_zeroStruct(sizeHints);
  297. if (XGetNormalHints(fDisplay, fChildWindow, &sizeHints))
  298. XSetNormalHints(fDisplay, fHostWindow, &sizeHints);
  299. XResizeWindow(fDisplay, fHostWindow, static_cast<uint>(nextWidth), static_cast<uint>(nextHeight));
  300. XFlush(fDisplay);
  301. }
  302. fIsIdling = false;
  303. }
  304. void focus() override
  305. {
  306. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  307. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  308. XWindowAttributes wa;
  309. carla_zeroStruct(wa);
  310. CARLA_SAFE_ASSERT_RETURN(XGetWindowAttributes(fDisplay, fHostWindow, &wa),);
  311. if (wa.map_state == IsViewable)
  312. {
  313. XRaiseWindow(fDisplay, fHostWindow);
  314. XSetInputFocus(fDisplay, fHostWindow, RevertToPointerRoot, CurrentTime);
  315. XSync(fDisplay, False);
  316. }
  317. }
  318. void setSize(const uint width, const uint height, const bool forceUpdate) override
  319. {
  320. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  321. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  322. fSetSizeCalledAtLeastOnce = true;
  323. XResizeWindow(fDisplay, fHostWindow, width, height);
  324. if (fChildWindow != 0)
  325. XResizeWindow(fDisplay, fChildWindow, width, height);
  326. if (! fIsResizable)
  327. {
  328. XSizeHints sizeHints;
  329. carla_zeroStruct(sizeHints);
  330. sizeHints.flags = PSize|PMinSize|PMaxSize;
  331. sizeHints.width = static_cast<int>(width);
  332. sizeHints.height = static_cast<int>(height);
  333. sizeHints.min_width = static_cast<int>(width);
  334. sizeHints.min_height = static_cast<int>(height);
  335. sizeHints.max_width = static_cast<int>(width);
  336. sizeHints.max_height = static_cast<int>(height);
  337. XSetNormalHints(fDisplay, fHostWindow, &sizeHints);
  338. }
  339. if (forceUpdate)
  340. XSync(fDisplay, False);
  341. }
  342. void setTitle(const char* const title) override
  343. {
  344. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  345. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  346. XStoreName(fDisplay, fHostWindow, title);
  347. }
  348. void setTransientWinId(const uintptr_t winId) override
  349. {
  350. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr,);
  351. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0,);
  352. XSetTransientForHint(fDisplay, fHostWindow, static_cast<Window>(winId));
  353. }
  354. void setChildWindow(void* const winId) override
  355. {
  356. CARLA_SAFE_ASSERT_RETURN(winId != nullptr,);
  357. fChildWindow = (Window)winId;
  358. }
  359. void* getPtr() const noexcept override
  360. {
  361. return (void*)fHostWindow;
  362. }
  363. void* getDisplay() const noexcept override
  364. {
  365. return fDisplay;
  366. }
  367. protected:
  368. Window getChildWindow() const
  369. {
  370. CARLA_SAFE_ASSERT_RETURN(fDisplay != nullptr, 0);
  371. CARLA_SAFE_ASSERT_RETURN(fHostWindow != 0, 0);
  372. Window rootWindow, parentWindow, ret = 0;
  373. Window* childWindows = nullptr;
  374. uint numChildren = 0;
  375. XQueryTree(fDisplay, fHostWindow, &rootWindow, &parentWindow, &childWindows, &numChildren);
  376. if (numChildren > 0 && childWindows != nullptr)
  377. {
  378. ret = childWindows[0];
  379. XFree(childWindows);
  380. }
  381. return ret;
  382. }
  383. private:
  384. Display* fDisplay;
  385. Window fHostWindow;
  386. Window fChildWindow;
  387. bool fChildWindowConfigured;
  388. bool fChildWindowMonitoring;
  389. bool fIsVisible;
  390. bool fFirstShow;
  391. bool fSetSizeCalledAtLeastOnce;
  392. EventProcPtr fEventProc;
  393. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(X11PluginUI)
  394. };
  395. #endif // HAVE_X11
  396. // ---------------------------------------------------------------------------------------------------------------------
  397. // MacOS / Cocoa
  398. #ifdef CARLA_OS_MAC
  399. #if defined(BUILD_BRIDGE_ALTERNATIVE_ARCH)
  400. # define CarlaPluginWindow CARLA_JOIN_MACRO3(CarlaPluginWindowBridgedArch, CARLA_BACKEND_NAMESPACE, CARLA_PLUGIN_UI_CLASS_PREFIX)
  401. # define CarlaPluginWindowDelegate CARLA_JOIN_MACRO3(CarlaPluginWindowDelegateBridgedArch, CARLA_BACKEND_NAMESPACE, CARLA_PLUGIN_UI_CLASS_PREFIX)
  402. #elif defined(BUILD_BRIDGE)
  403. # define CarlaPluginWindow CARLA_JOIN_MACRO3(CarlaPluginWindowBridged, CARLA_BACKEND_NAMESPACE, CARLA_PLUGIN_UI_CLASS_PREFIX)
  404. # define CarlaPluginWindowDelegate CARLA_JOIN_MACRO3(CarlaPluginWindowDelegateBridged, CARLA_BACKEND_NAMESPACE, CARLA_PLUGIN_UI_CLASS_PREFIX)
  405. #else
  406. # define CarlaPluginWindow CARLA_JOIN_MACRO3(CarlaPluginWindow, CARLA_BACKEND_NAMESPACE, CARLA_PLUGIN_UI_CLASS_PREFIX)
  407. # define CarlaPluginWindowDelegate CARLA_JOIN_MACRO3(CarlaPluginWindowDelegate, CARLA_BACKEND_NAMESPACE, CARLA_PLUGIN_UI_CLASS_PREFIX)
  408. #endif
  409. @interface CarlaPluginWindow : NSWindow
  410. - (id) initWithContentRect:(NSRect)contentRect
  411. styleMask:(unsigned int)aStyle
  412. backing:(NSBackingStoreType)bufferingType
  413. defer:(BOOL)flag;
  414. - (BOOL) canBecomeKeyWindow;
  415. - (BOOL) canBecomeMainWindow;
  416. @end
  417. @implementation CarlaPluginWindow
  418. - (id)initWithContentRect:(NSRect)contentRect
  419. styleMask:(unsigned int)aStyle
  420. backing:(NSBackingStoreType)bufferingType
  421. defer:(BOOL)flag
  422. {
  423. NSWindow* result = [super initWithContentRect:contentRect
  424. styleMask:aStyle
  425. backing:bufferingType
  426. defer:flag];
  427. [result setAcceptsMouseMovedEvents:YES];
  428. return (CarlaPluginWindow*)result;
  429. // unused
  430. (void)flag;
  431. }
  432. - (BOOL)canBecomeKeyWindow
  433. {
  434. return YES;
  435. }
  436. - (BOOL)canBecomeMainWindow
  437. {
  438. return NO;
  439. }
  440. @end
  441. @interface CarlaPluginWindowDelegate : NSObject<NSWindowDelegate>
  442. {
  443. CarlaPluginUI::Callback* callback;
  444. CarlaPluginWindow* window;
  445. }
  446. - (instancetype)initWithWindowAndCallback:(CarlaPluginWindow*)window
  447. callback:(CarlaPluginUI::Callback*)callback2;
  448. - (BOOL)windowShouldClose:(id)sender;
  449. - (NSSize)windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize;
  450. @end
  451. @implementation CarlaPluginWindowDelegate
  452. - (instancetype)initWithWindowAndCallback:(CarlaPluginWindow*)window2
  453. callback:(CarlaPluginUI::Callback*)callback2
  454. {
  455. if ((self = [super init]))
  456. {
  457. callback = callback2;
  458. window = window2;
  459. }
  460. return self;
  461. }
  462. - (BOOL)windowShouldClose:(id)sender
  463. {
  464. if (callback != nil)
  465. callback->handlePluginUIClosed();
  466. return NO;
  467. // unused
  468. (void)sender;
  469. }
  470. - (NSSize)windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize
  471. {
  472. for (NSView* subview in [[window contentView] subviews])
  473. {
  474. const NSSize minSize = [subview fittingSize];
  475. if (frameSize.width < minSize.width)
  476. frameSize.width = minSize.width;
  477. if (frameSize.height < minSize.height)
  478. frameSize.height = minSize.height;
  479. break;
  480. }
  481. return frameSize;
  482. }
  483. /*
  484. - (void)windowDidResize:(NSWindow*)sender
  485. {
  486. carla_stdout("window did resize %p %f %f", sender, [window frame].size.width, [window frame].size.height);
  487. const NSSize size = [window frame].size;
  488. NSView* const view = [window contentView];
  489. for (NSView* subview in [view subviews])
  490. {
  491. [subview setFrameSize:size];
  492. break;
  493. }
  494. }
  495. */
  496. @end
  497. class CocoaPluginUI : public CarlaPluginUI
  498. {
  499. public:
  500. CocoaPluginUI(Callback* const callback, const uintptr_t parentId, const bool isStandalone, const bool isResizable) noexcept
  501. : CarlaPluginUI(callback, isStandalone, isResizable),
  502. fView(nullptr),
  503. fParentWindow(nullptr),
  504. fWindow(nullptr)
  505. {
  506. carla_debug("CocoaPluginUI::CocoaPluginUI(%p, " P_UINTPTR, "%s)", callback, parentId, bool2str(isResizable));
  507. const CARLA_BACKEND_NAMESPACE::AutoNSAutoreleasePool arp;
  508. [NSApplication sharedApplication];
  509. fView = [[NSView new]retain];
  510. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,)
  511. uint style = NSClosableWindowMask | NSTitledWindowMask;
  512. /*
  513. if (isResizable)
  514. style |= NSResizableWindowMask;
  515. */
  516. const NSRect frame = NSMakeRect(0, 0, 100, 100);
  517. fWindow = [[[CarlaPluginWindow alloc]
  518. initWithContentRect:frame
  519. styleMask:style
  520. backing:NSBackingStoreBuffered
  521. defer:NO
  522. ] retain];
  523. if (fWindow == nullptr)
  524. {
  525. [fView release];
  526. fView = nullptr;
  527. return;
  528. }
  529. ((NSWindow*)fWindow).delegate = [[[CarlaPluginWindowDelegate alloc]
  530. initWithWindowAndCallback:fWindow
  531. callback:callback] retain];
  532. /*
  533. if (isResizable)
  534. {
  535. [fView setAutoresizingMask:(NSViewWidthSizable |
  536. NSViewHeightSizable |
  537. NSViewMinXMargin |
  538. NSViewMaxXMargin |
  539. NSViewMinYMargin |
  540. NSViewMaxYMargin)];
  541. [fView setAutoresizesSubviews:YES];
  542. }
  543. else
  544. */
  545. {
  546. [fView setAutoresizingMask:NSViewNotSizable];
  547. [fView setAutoresizesSubviews:NO];
  548. [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
  549. }
  550. [fWindow setContentView:fView];
  551. [fWindow makeFirstResponder:fView];
  552. [fView setHidden:NO];
  553. if (parentId != 0)
  554. setTransientWinId(parentId);
  555. }
  556. ~CocoaPluginUI() override
  557. {
  558. carla_debug("CocoaPluginUI::~CocoaPluginUI()");
  559. if (fView == nullptr)
  560. return;
  561. [fView setHidden:YES];
  562. [fView removeFromSuperview];
  563. [fWindow close];
  564. [fView release];
  565. [fWindow release];
  566. }
  567. void show() override
  568. {
  569. carla_debug("CocoaPluginUI::show()");
  570. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,);
  571. if (fParentWindow != nullptr)
  572. {
  573. [fParentWindow addChildWindow:fWindow
  574. ordered:NSWindowAbove];
  575. }
  576. else
  577. {
  578. [fWindow setIsVisible:YES];
  579. }
  580. }
  581. void hide() override
  582. {
  583. carla_debug("CocoaPluginUI::hide()");
  584. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,);
  585. [fWindow setIsVisible:NO];
  586. if (fParentWindow != nullptr)
  587. [fParentWindow removeChildWindow:fWindow];
  588. }
  589. void idle() override
  590. {
  591. // carla_debug("CocoaPluginUI::idle()");
  592. for (NSView* subview in [fView subviews])
  593. {
  594. const NSSize viewSize = [fView frame].size;
  595. const NSSize subviewSize = [subview frame].size;
  596. if (viewSize.width != subviewSize.width || viewSize.height != subviewSize.height)
  597. {
  598. [fView setFrameSize:subviewSize];
  599. [fWindow setContentSize:subviewSize];
  600. }
  601. break;
  602. }
  603. }
  604. void focus() override
  605. {
  606. carla_debug("CocoaPluginUI::focus()");
  607. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  608. [fWindow makeKeyAndOrderFront:fWindow];
  609. [fWindow orderFrontRegardless];
  610. [NSApp activateIgnoringOtherApps:YES];
  611. }
  612. void setSize(const uint width, const uint height, const bool forceUpdate) override
  613. {
  614. carla_debug("CocoaPluginUI::setSize(%u, %u, %s)", width, height, bool2str(forceUpdate));
  615. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  616. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,);
  617. const NSSize size = NSMakeSize(width, height);
  618. [fView setFrameSize:size];
  619. [fWindow setContentSize:size];
  620. // this is needed for a few plugins
  621. if (forceUpdate)
  622. {
  623. for (NSView* subview in [fView subviews])
  624. {
  625. [subview setFrame:[fView frame]];
  626. break;
  627. }
  628. }
  629. /*
  630. if (fIsResizable)
  631. {
  632. [fWindow setContentMinSize:NSMakeSize(1, 1)];
  633. [fWindow setContentMaxSize:NSMakeSize(99999, 99999)];
  634. }
  635. else
  636. {
  637. [fWindow setContentMinSize:size];
  638. [fWindow setContentMaxSize:size];
  639. }
  640. */
  641. if (forceUpdate)
  642. {
  643. // FIXME, not enough
  644. [fView setNeedsDisplay:YES];
  645. }
  646. }
  647. void setTitle(const char* const title) override
  648. {
  649. carla_debug("CocoaPluginUI::setTitle(\"%s\")", title);
  650. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  651. NSString* titleString = [[NSString alloc]
  652. initWithBytes:title
  653. length:strlen(title)
  654. encoding:NSUTF8StringEncoding];
  655. [fWindow setTitle:titleString];
  656. }
  657. void setTransientWinId(const uintptr_t winId) override
  658. {
  659. carla_debug("CocoaPluginUI::setTransientWinId(" P_UINTPTR ")", winId);
  660. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  661. NSWindow* const parentWindow = [NSApp windowWithWindowNumber:winId];
  662. CARLA_SAFE_ASSERT_RETURN(parentWindow != nullptr,);
  663. fParentWindow = parentWindow;
  664. if ([fWindow isVisible])
  665. [fParentWindow addChildWindow:fWindow
  666. ordered:NSWindowAbove];
  667. }
  668. void setChildWindow(void* const childWindow) override
  669. {
  670. carla_debug("CocoaPluginUI::setChildWindow(%p)", childWindow);
  671. CARLA_SAFE_ASSERT_RETURN(childWindow != nullptr,);
  672. NSView* const view = (NSView*)childWindow;
  673. const NSRect frame = [view frame];
  674. [fWindow setContentSize:frame.size];
  675. [fView setFrame:frame];
  676. [fView setNeedsDisplay:YES];
  677. }
  678. void* getPtr() const noexcept override
  679. {
  680. carla_debug("CocoaPluginUI::getPtr()");
  681. return (void*)fView;
  682. }
  683. void* getDisplay() const noexcept
  684. {
  685. carla_debug("CocoaPluginUI::getDisplay()");
  686. return (void*)fWindow;
  687. }
  688. private:
  689. NSView* fView;
  690. NSWindow* fParentWindow;
  691. CarlaPluginWindow* fWindow;
  692. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CocoaPluginUI)
  693. };
  694. #endif // CARLA_OS_MAC
  695. // ---------------------------------------------------------------------------------------------------------------------
  696. // Windows
  697. #ifdef CARLA_OS_WIN
  698. #define CARLA_LOCAL_CLOSE_MSG (WM_USER + 50)
  699. static LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  700. class WindowsPluginUI : public CarlaPluginUI
  701. {
  702. public:
  703. WindowsPluginUI(Callback* const cb, const uintptr_t parentId, const bool isStandalone, const bool isResizable) noexcept
  704. : CarlaPluginUI(cb, isStandalone, isResizable),
  705. fWindow(nullptr),
  706. fChildWindow(nullptr),
  707. fParentWindow(nullptr),
  708. fIsVisible(false),
  709. fFirstShow(true)
  710. {
  711. // FIXME
  712. static int wc_count = 0;
  713. char classNameBuf[32];
  714. std::srand((std::time(nullptr)));
  715. std::snprintf(classNameBuf, 32, "CarlaWin-%d-%d", ++wc_count, std::rand());
  716. classNameBuf[31] = '\0';
  717. const HINSTANCE hInstance = water::getCurrentModuleInstanceHandle();
  718. carla_zeroStruct(fWindowClass);
  719. fWindowClass.style = CS_OWNDC;
  720. fWindowClass.lpfnWndProc = wndProc;
  721. fWindowClass.hInstance = hInstance;
  722. fWindowClass.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
  723. fWindowClass.hCursor = LoadCursor(hInstance, IDC_ARROW);
  724. fWindowClass.lpszClassName = strdup(classNameBuf);
  725. if (!RegisterClass(&fWindowClass)) {
  726. free((void*)fWindowClass.lpszClassName);
  727. return;
  728. }
  729. int winFlags = WS_POPUPWINDOW | WS_CAPTION;
  730. if (isResizable)
  731. winFlags |= WS_SIZEBOX;
  732. #ifdef BUILDING_CARLA_FOR_WINE
  733. const uint winType = WS_EX_DLGMODALFRAME;
  734. const HWND parent = nullptr;
  735. #else
  736. const uint winType = WS_EX_TOOLWINDOW;
  737. const HWND parent = (HWND)parentId;
  738. #endif
  739. fWindow = CreateWindowEx(winType,
  740. classNameBuf, "Carla Plugin UI", winFlags,
  741. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  742. parent, nullptr,
  743. hInstance, nullptr);
  744. if (fWindow == nullptr)
  745. {
  746. const DWORD errorCode = ::GetLastError();
  747. carla_stderr2("CreateWindowEx failed with error code 0x%x, class name was '%s'",
  748. errorCode, fWindowClass.lpszClassName);
  749. UnregisterClass(fWindowClass.lpszClassName, nullptr);
  750. free((void*)fWindowClass.lpszClassName);
  751. return;
  752. }
  753. SetWindowLongPtr(fWindow, GWLP_USERDATA, (LONG_PTR)this);
  754. #ifndef BUILDING_CARLA_FOR_WINE
  755. if (parentId != 0)
  756. setTransientWinId(parentId);
  757. #endif
  758. return;
  759. // maybe unused
  760. (void)parentId;
  761. }
  762. ~WindowsPluginUI() override
  763. {
  764. CARLA_SAFE_ASSERT(! fIsVisible);
  765. if (fWindow != 0)
  766. {
  767. if (fIsVisible)
  768. ShowWindow(fWindow, SW_HIDE);
  769. DestroyWindow(fWindow);
  770. fWindow = 0;
  771. }
  772. // FIXME
  773. UnregisterClass(fWindowClass.lpszClassName, nullptr);
  774. free((void*)fWindowClass.lpszClassName);
  775. }
  776. void show() override
  777. {
  778. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  779. if (fFirstShow)
  780. {
  781. fFirstShow = false;
  782. RECT rectChild, rectParent;
  783. if (fChildWindow != nullptr && GetWindowRect(fChildWindow, &rectChild))
  784. setSize(rectChild.right - rectChild.left, rectChild.bottom - rectChild.top, false);
  785. if (fParentWindow != nullptr &&
  786. GetWindowRect(fWindow, &rectChild) &&
  787. GetWindowRect(fParentWindow, &rectParent))
  788. {
  789. SetWindowPos(fWindow, fParentWindow,
  790. rectParent.left + (rectChild.right-rectChild.left)/2,
  791. rectParent.top + (rectChild.bottom-rectChild.top)/2,
  792. 0, 0, SWP_SHOWWINDOW|SWP_NOSIZE);
  793. }
  794. else
  795. {
  796. ShowWindow(fWindow, SW_SHOWNORMAL);
  797. }
  798. }
  799. else
  800. {
  801. ShowWindow(fWindow, SW_RESTORE);
  802. }
  803. fIsVisible = true;
  804. UpdateWindow(fWindow);
  805. }
  806. void hide() override
  807. {
  808. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  809. ShowWindow(fWindow, SW_HIDE);
  810. fIsVisible = false;
  811. UpdateWindow(fWindow);
  812. }
  813. void idle() override
  814. {
  815. if (fIsIdling || fWindow == nullptr)
  816. return;
  817. MSG msg;
  818. fIsIdling = true;
  819. while (::PeekMessage(&msg, fWindow, 0, 0, PM_REMOVE))
  820. {
  821. switch (msg.message)
  822. {
  823. case WM_QUIT:
  824. case CARLA_LOCAL_CLOSE_MSG:
  825. fIsVisible = false;
  826. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  827. fCallback->handlePluginUIClosed();
  828. break;
  829. }
  830. DispatchMessageA(&msg);
  831. }
  832. fIsIdling = false;
  833. }
  834. LRESULT checkAndHandleMessage(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  835. {
  836. if (fWindow == hwnd)
  837. {
  838. switch (message)
  839. {
  840. case WM_SIZE:
  841. if (fChildWindow != nullptr)
  842. {
  843. RECT rect;
  844. GetClientRect(fWindow, &rect);
  845. SetWindowPos(fChildWindow, 0, 0, 0, rect.right, rect.bottom,
  846. SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
  847. }
  848. break;
  849. case WM_QUIT:
  850. case CARLA_LOCAL_CLOSE_MSG:
  851. fIsVisible = false;
  852. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  853. fCallback->handlePluginUIClosed();
  854. break;
  855. }
  856. }
  857. return DefWindowProcA(hwnd, message, wParam, lParam);
  858. }
  859. void focus() override
  860. {
  861. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  862. SetForegroundWindow(fWindow);
  863. SetActiveWindow(fWindow);
  864. SetFocus(fWindow);
  865. }
  866. void setSize(const uint width, const uint height, const bool forceUpdate) override
  867. {
  868. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  869. const int winFlags = WS_POPUPWINDOW | WS_CAPTION | (fIsResizable ? WS_SIZEBOX : 0x0);
  870. RECT wr = { 0, 0, static_cast<long>(width), static_cast<long>(height) };
  871. AdjustWindowRectEx(&wr, winFlags, FALSE, WS_EX_TOPMOST);
  872. SetWindowPos(fWindow, 0, 0, 0, wr.right-wr.left, wr.bottom-wr.top,
  873. SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
  874. if (forceUpdate)
  875. UpdateWindow(fWindow);
  876. }
  877. void setTitle(const char* const title) override
  878. {
  879. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  880. SetWindowTextA(fWindow, title);
  881. }
  882. void setTransientWinId(const uintptr_t winId) override
  883. {
  884. CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,);
  885. fParentWindow = (HWND)winId;
  886. SetWindowLongPtr(fWindow, GWLP_HWNDPARENT, (LONG_PTR)winId);
  887. }
  888. void setChildWindow(void* const winId) override
  889. {
  890. CARLA_SAFE_ASSERT_RETURN(winId != nullptr,);
  891. fChildWindow = (HWND)winId;
  892. }
  893. void* getPtr() const noexcept override
  894. {
  895. return (void*)fWindow;
  896. }
  897. void* getDisplay() const noexcept
  898. {
  899. return nullptr;
  900. }
  901. private:
  902. HWND fWindow;
  903. HWND fChildWindow;
  904. HWND fParentWindow;
  905. WNDCLASS fWindowClass;
  906. bool fIsVisible;
  907. bool fFirstShow;
  908. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(WindowsPluginUI)
  909. };
  910. LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  911. {
  912. switch (message)
  913. {
  914. case WM_CLOSE:
  915. PostMessage(hwnd, CARLA_LOCAL_CLOSE_MSG, wParam, lParam);
  916. return 0;
  917. #if 0
  918. case WM_CREATE:
  919. PostMessage(hwnd, WM_SHOWWINDOW, TRUE, 0);
  920. return 0;
  921. case WM_DESTROY:
  922. return 0;
  923. #endif
  924. default:
  925. if (WindowsPluginUI* const ui = (WindowsPluginUI*)GetWindowLongPtr(hwnd, GWLP_USERDATA))
  926. return ui->checkAndHandleMessage(hwnd, message, wParam, lParam);
  927. return DefWindowProcA(hwnd, message, wParam, lParam);
  928. }
  929. }
  930. #endif // CARLA_OS_WIN
  931. // -----------------------------------------------------
  932. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  933. bool CarlaPluginUI::tryTransientWinIdMatch(const uintptr_t pid, const char* const uiTitle, const uintptr_t winId, const bool centerUI)
  934. {
  935. CARLA_SAFE_ASSERT_RETURN(uiTitle != nullptr && uiTitle[0] != '\0', true);
  936. CARLA_SAFE_ASSERT_RETURN(winId != 0, true);
  937. #if defined(HAVE_X11)
  938. struct ScopedDisplay {
  939. Display* display;
  940. ScopedDisplay() : display(XOpenDisplay(nullptr)) {}
  941. ~ScopedDisplay() { if (display!=nullptr) XCloseDisplay(display); }
  942. // c++ compat stuff
  943. CARLA_PREVENT_HEAP_ALLOCATION
  944. CARLA_DECLARE_NON_COPYABLE(ScopedDisplay)
  945. };
  946. struct ScopedFreeData {
  947. union {
  948. char* data;
  949. uchar* udata;
  950. };
  951. ScopedFreeData(char* d) : data(d) {}
  952. ScopedFreeData(uchar* d) : udata(d) {}
  953. ~ScopedFreeData() { XFree(data); }
  954. // c++ compat stuff
  955. CARLA_PREVENT_HEAP_ALLOCATION
  956. CARLA_DECLARE_NON_COPYABLE(ScopedFreeData)
  957. };
  958. const ScopedDisplay sd;
  959. CARLA_SAFE_ASSERT_RETURN(sd.display != nullptr, true);
  960. const Window rootWindow(DefaultRootWindow(sd.display));
  961. const Atom _ncl = XInternAtom(sd.display, "_NET_CLIENT_LIST" , False);
  962. const Atom _nwn = XInternAtom(sd.display, "_NET_WM_NAME", False);
  963. const Atom _nwp = XInternAtom(sd.display, "_NET_WM_PID", False);
  964. const Atom utf8 = XInternAtom(sd.display, "UTF8_STRING", True);
  965. Atom actualType;
  966. int actualFormat;
  967. ulong numWindows, bytesAfter;
  968. uchar* data = nullptr;
  969. int status = XGetWindowProperty(sd.display, rootWindow, _ncl, 0L, (~0L), False, AnyPropertyType, &actualType, &actualFormat, &numWindows, &bytesAfter, &data);
  970. CARLA_SAFE_ASSERT_RETURN(data != nullptr, true);
  971. const ScopedFreeData sfd(data);
  972. CARLA_SAFE_ASSERT_RETURN(status == Success, true);
  973. CARLA_SAFE_ASSERT_RETURN(actualFormat == 32, true);
  974. CARLA_SAFE_ASSERT_RETURN(numWindows != 0, true);
  975. Window* windows = (Window*)data;
  976. Window lastGoodWindowPID = 0, lastGoodWindowNameSimple = 0, lastGoodWindowNameUTF8 = 0;
  977. for (ulong i = 0; i < numWindows; i++)
  978. {
  979. const Window window(windows[i]);
  980. CARLA_SAFE_ASSERT_CONTINUE(window != 0);
  981. // ------------------------------------------------
  982. // try using pid
  983. if (pid != 0)
  984. {
  985. ulong pidSize;
  986. uchar* pidData = nullptr;
  987. status = XGetWindowProperty(sd.display, window, _nwp, 0L, (~0L), False, XA_CARDINAL, &actualType, &actualFormat, &pidSize, &bytesAfter, &pidData);
  988. if (pidData != nullptr)
  989. {
  990. const ScopedFreeData sfd2(pidData);
  991. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  992. CARLA_SAFE_ASSERT_CONTINUE(pidSize != 0);
  993. if (*(ulong*)pidData == static_cast<ulong>(pid))
  994. lastGoodWindowPID = window;
  995. }
  996. }
  997. // ------------------------------------------------
  998. // try using name (UTF-8)
  999. ulong nameSize;
  1000. uchar* nameData = nullptr;
  1001. status = XGetWindowProperty(sd.display, window, _nwn, 0L, (~0L), False, utf8, &actualType, &actualFormat, &nameSize, &bytesAfter, &nameData);
  1002. if (nameData != nullptr)
  1003. {
  1004. const ScopedFreeData sfd2(nameData);
  1005. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  1006. if (nameSize != 0 && std::strstr((const char*)nameData, uiTitle) != nullptr)
  1007. lastGoodWindowNameUTF8 = window;
  1008. }
  1009. // ------------------------------------------------
  1010. // try using name (simple)
  1011. char* wmName = nullptr;
  1012. status = XFetchName(sd.display, window, &wmName);
  1013. if (wmName != nullptr)
  1014. {
  1015. const ScopedFreeData sfd2(wmName);
  1016. CARLA_SAFE_ASSERT_CONTINUE(status != 0);
  1017. if (std::strstr(wmName, uiTitle) != nullptr)
  1018. lastGoodWindowNameSimple = window;
  1019. }
  1020. }
  1021. if (lastGoodWindowPID == 0 && lastGoodWindowNameSimple == 0 && lastGoodWindowNameUTF8 == 0)
  1022. return false;
  1023. Window windowToMap;
  1024. if (lastGoodWindowPID != 0)
  1025. {
  1026. if (lastGoodWindowPID == lastGoodWindowNameSimple && lastGoodWindowPID == lastGoodWindowNameUTF8)
  1027. {
  1028. carla_stdout("Match found using pid, simple and UTF-8 name all at once, nice!");
  1029. windowToMap = lastGoodWindowPID;
  1030. }
  1031. else if (lastGoodWindowPID == lastGoodWindowNameUTF8)
  1032. {
  1033. carla_stdout("Match found using pid and UTF-8 name");
  1034. windowToMap = lastGoodWindowPID;
  1035. }
  1036. else if (lastGoodWindowPID == lastGoodWindowNameSimple)
  1037. {
  1038. carla_stdout("Match found using pid and simple name");
  1039. windowToMap = lastGoodWindowPID;
  1040. }
  1041. else if (lastGoodWindowNameUTF8 != 0)
  1042. {
  1043. if (lastGoodWindowNameUTF8 == lastGoodWindowNameSimple)
  1044. {
  1045. carla_stdout("Match found using simple and UTF-8 name (ignoring pid)");
  1046. windowToMap = lastGoodWindowNameUTF8;
  1047. }
  1048. else
  1049. {
  1050. carla_stdout("Match found using UTF-8 name (ignoring pid)");
  1051. windowToMap = lastGoodWindowNameUTF8;
  1052. }
  1053. }
  1054. else
  1055. {
  1056. carla_stdout("Match found using pid");
  1057. windowToMap = lastGoodWindowPID;
  1058. }
  1059. }
  1060. else if (lastGoodWindowNameUTF8 != 0)
  1061. {
  1062. if (lastGoodWindowNameUTF8 == lastGoodWindowNameSimple)
  1063. {
  1064. carla_stdout("Match found using simple and UTF-8 name");
  1065. windowToMap = lastGoodWindowNameUTF8;
  1066. }
  1067. else
  1068. {
  1069. carla_stdout("Match found using UTF-8 name");
  1070. windowToMap = lastGoodWindowNameUTF8;
  1071. }
  1072. }
  1073. else
  1074. {
  1075. carla_stdout("Match found using simple name");
  1076. windowToMap = lastGoodWindowNameSimple;
  1077. }
  1078. const Atom _nwt = XInternAtom(sd.display ,"_NET_WM_STATE", False);
  1079. const Atom _nws[2] = {
  1080. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_TASKBAR", False),
  1081. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_PAGER", False)
  1082. };
  1083. XChangeProperty(sd.display, windowToMap, _nwt, XA_ATOM, 32, PropModeAppend, (const uchar*)_nws, 2);
  1084. const Atom _nwi = XInternAtom(sd.display, "_NET_WM_ICON", False);
  1085. XChangeProperty(sd.display, windowToMap, _nwi, XA_CARDINAL, 32, PropModeReplace, (const uchar*)sCarlaX11Icon, sCarlaX11IconSize);
  1086. const Window hostWinId((Window)winId);
  1087. XSetTransientForHint(sd.display, windowToMap, hostWinId);
  1088. if (centerUI && false /* moving the window after being shown isn't pretty... */)
  1089. {
  1090. int hostX, hostY, pluginX, pluginY;
  1091. uint hostWidth, hostHeight, pluginWidth, pluginHeight, border, depth;
  1092. Window retWindow;
  1093. if (XGetGeometry(sd.display, hostWinId, &retWindow, &hostX, &hostY, &hostWidth, &hostHeight, &border, &depth) != 0 &&
  1094. XGetGeometry(sd.display, windowToMap, &retWindow, &pluginX, &pluginY, &pluginWidth, &pluginHeight, &border, &depth) != 0)
  1095. {
  1096. if (XTranslateCoordinates(sd.display, hostWinId, rootWindow, hostX, hostY, &hostX, &hostY, &retWindow) == True &&
  1097. XTranslateCoordinates(sd.display, windowToMap, rootWindow, pluginX, pluginY, &pluginX, &pluginY, &retWindow) == True)
  1098. {
  1099. const int newX = hostX + int(hostWidth/2 - pluginWidth/2);
  1100. const int newY = hostY + int(hostHeight/2 - pluginHeight/2);
  1101. XMoveWindow(sd.display, windowToMap, newX, newY);
  1102. }
  1103. }
  1104. }
  1105. // focusing the host UI and then the plugin UI forces the WM to repaint the plugin window icon
  1106. XRaiseWindow(sd.display, hostWinId);
  1107. XSetInputFocus(sd.display, hostWinId, RevertToPointerRoot, CurrentTime);
  1108. XRaiseWindow(sd.display, windowToMap);
  1109. XSetInputFocus(sd.display, windowToMap, RevertToPointerRoot, CurrentTime);
  1110. XFlush(sd.display);
  1111. return true;
  1112. #endif
  1113. #ifdef CARLA_OS_MAC
  1114. uint const hints = kCGWindowListOptionOnScreenOnly|kCGWindowListExcludeDesktopElements;
  1115. CFArrayRef const windowListRef = CGWindowListCopyWindowInfo(hints, kCGNullWindowID);
  1116. const NSArray* const windowList = (const NSArray*)windowListRef;
  1117. int windowToMap, windowWithPID = 0, windowWithNameAndPID = 0;
  1118. const NSDictionary* entry;
  1119. for (entry in windowList)
  1120. {
  1121. // FIXME: is this needed? is old version safe?
  1122. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  1123. if ([entry[(id)kCGWindowSharingState] intValue] == kCGWindowSharingNone)
  1124. continue;
  1125. NSString* const windowName = entry[(id)kCGWindowName];
  1126. int const windowNumber = [entry[(id)kCGWindowNumber] intValue];
  1127. uintptr_t const windowPID = [entry[(id)kCGWindowOwnerPID] intValue];
  1128. #else
  1129. if ([[entry objectForKey:(id)kCGWindowSharingState] intValue] == kCGWindowSharingNone)
  1130. continue;
  1131. NSString* const windowName = [entry objectForKey:(id)kCGWindowName];
  1132. int const windowNumber = [[entry objectForKey:(id)kCGWindowNumber] intValue];
  1133. uintptr_t const windowPID = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue];
  1134. #endif
  1135. if (windowPID != pid)
  1136. continue;
  1137. windowWithPID = windowNumber;
  1138. if (windowName != nullptr && std::strcmp([windowName UTF8String], uiTitle) == 0)
  1139. windowWithNameAndPID = windowNumber;
  1140. }
  1141. CFRelease(windowListRef);
  1142. if (windowWithNameAndPID != 0)
  1143. {
  1144. carla_stdout("Match found using pid and name");
  1145. windowToMap = windowWithNameAndPID;
  1146. }
  1147. else if (windowWithPID != 0)
  1148. {
  1149. carla_stdout("Match found using pid");
  1150. windowToMap = windowWithPID;
  1151. }
  1152. else
  1153. {
  1154. return false;
  1155. }
  1156. NSWindow* const parentWindow = [NSApp windowWithWindowNumber:winId];
  1157. CARLA_SAFE_ASSERT_RETURN(parentWindow != nullptr, false);
  1158. [parentWindow orderWindow:NSWindowBelow
  1159. relativeTo:windowToMap];
  1160. return true;
  1161. #endif
  1162. #ifdef CARLA_OS_WIN
  1163. if (HWND const childWindow = FindWindowA(nullptr, uiTitle))
  1164. {
  1165. HWND const parentWindow = (HWND)winId;
  1166. SetWindowLongPtr(childWindow, GWLP_HWNDPARENT, (LONG_PTR)parentWindow);
  1167. if (centerUI)
  1168. {
  1169. RECT rectChild, rectParent;
  1170. if (GetWindowRect(childWindow, &rectChild) && GetWindowRect(parentWindow, &rectParent))
  1171. {
  1172. SetWindowPos(childWindow, parentWindow,
  1173. rectParent.left + (rectChild.right-rectChild.left)/2,
  1174. rectParent.top + (rectChild.bottom-rectChild.top)/2,
  1175. 0, 0, SWP_NOSIZE);
  1176. }
  1177. }
  1178. carla_stdout("Match found using window name");
  1179. return true;
  1180. }
  1181. return false;
  1182. #endif
  1183. // fallback, may be unused
  1184. return true;
  1185. (void)pid; (void)centerUI;
  1186. }
  1187. #endif // BUILD_BRIDGE_ALTERNATIVE_ARCH
  1188. // -----------------------------------------------------
  1189. #ifdef HAVE_X11
  1190. CarlaPluginUI* CarlaPluginUI::newX11(Callback* const cb,
  1191. const uintptr_t parentId,
  1192. const bool isStandalone,
  1193. const bool isResizable,
  1194. const bool isLV2)
  1195. {
  1196. return new X11PluginUI(cb, parentId, isStandalone, isResizable, isLV2);
  1197. }
  1198. #endif
  1199. #ifdef CARLA_OS_MAC
  1200. CarlaPluginUI* CarlaPluginUI::newCocoa(Callback* const cb,
  1201. const uintptr_t parentId,
  1202. const bool isStandalone,
  1203. const bool isResizable)
  1204. {
  1205. return new CocoaPluginUI(cb, parentId, isStandalone, isResizable);
  1206. }
  1207. #endif
  1208. #ifdef CARLA_OS_WIN
  1209. CarlaPluginUI* CarlaPluginUI::newWindows(Callback* const cb,
  1210. const uintptr_t parentId,
  1211. const bool isStandalone,
  1212. const bool isResizable)
  1213. {
  1214. return new WindowsPluginUI(cb, parentId, isStandalone, isResizable);
  1215. }
  1216. #endif
  1217. // -----------------------------------------------------