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.

1544 lines
48KB

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