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.

1168 lines
34KB

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