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.

1110 lines
32KB

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