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.

1054 lines
30KB

  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(0),
  470. fIsVisible(false),
  471. fFirstShow(true)
  472. {
  473. // FIXME
  474. static int wc_count = 0;
  475. char classNameBuf[256];
  476. std::srand((std::time(NULL)));
  477. _snprintf(classNameBuf, sizeof(classNameBuf), "CaWin_%d-%d", std::rand(), ++wc_count);
  478. classNameBuf[sizeof(classNameBuf)-1] = '\0';
  479. const HINSTANCE hInstance = GetModuleHandleA(nullptr);
  480. carla_zeroStruct(fWindowClass);
  481. fWindowClass.style = CS_OWNDC;
  482. fWindowClass.lpfnWndProc = wndProc;
  483. fWindowClass.hInstance = hInstance;
  484. fWindowClass.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
  485. fWindowClass.hCursor = LoadCursor(hInstance, IDC_ARROW);
  486. fWindowClass.lpszClassName = strdup(classNameBuf);
  487. if (!RegisterClass(&fWindowClass)) {
  488. free((void*)fWindowClass.lpszClassName);
  489. return;
  490. }
  491. int winFlags = WS_POPUPWINDOW | WS_CAPTION;
  492. if (isResizable)
  493. winFlags |= WS_SIZEBOX;
  494. fWindow = CreateWindowEx(WS_EX_TOPMOST,
  495. classNameBuf, "Carla Plugin UI", winFlags,
  496. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  497. NULL, NULL, hInstance, NULL);
  498. if (! fWindow) {
  499. UnregisterClass(fWindowClass.lpszClassName, NULL);
  500. free((void*)fWindowClass.lpszClassName);
  501. return;
  502. }
  503. SetWindowLongPtr(fWindow, GWLP_USERDATA, (LONG_PTR)this);
  504. if (parentId != 0)
  505. setTransientWinId(parentId);
  506. }
  507. ~WindowsPluginUI() override
  508. {
  509. CARLA_SAFE_ASSERT(! fIsVisible);
  510. if (fWindow != 0)
  511. {
  512. if (fIsVisible)
  513. ShowWindow(fWindow, SW_HIDE);
  514. DestroyWindow(fWindow);
  515. fWindow = 0;
  516. }
  517. // FIXME
  518. UnregisterClass(fWindowClass.lpszClassName, NULL);
  519. free((void*)fWindowClass.lpszClassName);
  520. }
  521. void show() override
  522. {
  523. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  524. ShowWindow(fWindow, fFirstShow ? SW_SHOWNORMAL : SW_RESTORE);
  525. fIsVisible = true;
  526. fFirstShow = false;
  527. UpdateWindow(fWindow);
  528. }
  529. void hide() override
  530. {
  531. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  532. ShowWindow(fWindow, SW_HIDE);
  533. fIsVisible = false;
  534. UpdateWindow(fWindow);
  535. }
  536. void idle() override
  537. {
  538. if (fIsIdling || fWindow == 0)
  539. return;
  540. MSG msg;
  541. fIsIdling = true;
  542. while (::PeekMessage(&msg, fWindow, 0, 0, PM_REMOVE))
  543. {
  544. switch (msg.message)
  545. {
  546. case WM_QUIT:
  547. case PUGL_LOCAL_CLOSE_MSG:
  548. fIsVisible = false;
  549. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  550. fCallback->handlePluginUIClosed();
  551. break;
  552. }
  553. DispatchMessageA(&msg);
  554. }
  555. fIsIdling = false;
  556. }
  557. LRESULT checkAndHandleMessage(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  558. {
  559. if (fWindow == hwnd)
  560. {
  561. switch (message)
  562. {
  563. case WM_QUIT:
  564. case PUGL_LOCAL_CLOSE_MSG:
  565. fIsVisible = false;
  566. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  567. fCallback->handlePluginUIClosed();
  568. break;
  569. }
  570. }
  571. return DefWindowProcA(hwnd, message, wParam, lParam);
  572. }
  573. void focus() override
  574. {
  575. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  576. SetForegroundWindow(fWindow);
  577. SetActiveWindow(fWindow);
  578. SetFocus(fWindow);
  579. }
  580. void setSize(const uint width, const uint height, const bool forceUpdate) override
  581. {
  582. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  583. const int winFlags = WS_POPUPWINDOW | WS_CAPTION | (fIsResizable ? WS_SIZEBOX : 0x0);
  584. RECT wr = { 0, 0, static_cast<long>(width), static_cast<long>(height) };
  585. AdjustWindowRectEx(&wr, winFlags, FALSE, WS_EX_TOPMOST);
  586. SetWindowPos(fWindow, 0, 0, 0, wr.right-wr.left, wr.bottom-wr.top,
  587. SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
  588. if (forceUpdate)
  589. UpdateWindow(fWindow);
  590. }
  591. void setTitle(const char* const title) override
  592. {
  593. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  594. SetWindowTextA(fWindow, title);
  595. }
  596. void setTransientWinId(const uintptr_t winId) override
  597. {
  598. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  599. SetWindowLongPtr(fWindow, GWLP_HWNDPARENT, (LONG_PTR)winId);
  600. }
  601. void setChildWindow(void* const winId) override
  602. {
  603. CARLA_SAFE_ASSERT_RETURN(winId != nullptr,);
  604. }
  605. void* getPtr() const noexcept override
  606. {
  607. return (void*)fWindow;
  608. }
  609. void* getDisplay() const noexcept
  610. {
  611. return nullptr;
  612. }
  613. private:
  614. HWND fWindow;
  615. WNDCLASS fWindowClass;
  616. bool fIsVisible;
  617. bool fFirstShow;
  618. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(WindowsPluginUI)
  619. };
  620. LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  621. {
  622. switch (message)
  623. {
  624. case WM_CLOSE:
  625. PostMessage(hwnd, PUGL_LOCAL_CLOSE_MSG, wParam, lParam);
  626. return 0;
  627. #if 0
  628. case WM_CREATE:
  629. PostMessage(hwnd, WM_SHOWWINDOW, TRUE, 0);
  630. return 0;
  631. case WM_DESTROY:
  632. return 0;
  633. #endif
  634. default:
  635. if (WindowsPluginUI* const ui = (WindowsPluginUI*)GetWindowLongPtr(hwnd, GWLP_USERDATA))
  636. return ui->checkAndHandleMessage(hwnd, message, wParam, lParam);
  637. return DefWindowProcA(hwnd, message, wParam, lParam);
  638. }
  639. }
  640. #endif // CARLA_OS_WIN
  641. // -----------------------------------------------------
  642. bool CarlaPluginUI::tryTransientWinIdMatch(const uintptr_t pid, const char* const uiTitle, const uintptr_t winId, const bool centerUI)
  643. {
  644. CARLA_SAFE_ASSERT_RETURN(uiTitle != nullptr && uiTitle[0] != '\0', true);
  645. CARLA_SAFE_ASSERT_RETURN(winId != 0, true);
  646. #if defined(HAVE_X11)
  647. struct ScopedDisplay {
  648. Display* display;
  649. ScopedDisplay() : display(XOpenDisplay(nullptr)) {}
  650. ~ScopedDisplay() { if (display!=nullptr) XCloseDisplay(display); }
  651. // c++ compat stuff
  652. CARLA_PREVENT_HEAP_ALLOCATION
  653. CARLA_DECLARE_NON_COPY_CLASS(ScopedDisplay)
  654. };
  655. struct ScopedFreeData {
  656. union {
  657. char* data;
  658. uchar* udata;
  659. };
  660. ScopedFreeData(char* d) : data(d) {}
  661. ScopedFreeData(uchar* d) : udata(d) {}
  662. ~ScopedFreeData() { XFree(data); }
  663. // c++ compat stuff
  664. CARLA_PREVENT_HEAP_ALLOCATION
  665. CARLA_DECLARE_NON_COPY_CLASS(ScopedFreeData)
  666. };
  667. const ScopedDisplay sd;
  668. CARLA_SAFE_ASSERT_RETURN(sd.display != nullptr, true);
  669. const Window rootWindow(DefaultRootWindow(sd.display));
  670. const Atom _ncl = XInternAtom(sd.display, "_NET_CLIENT_LIST" , False);
  671. const Atom _nwn = XInternAtom(sd.display, "_NET_WM_NAME", False);
  672. const Atom _nwp = XInternAtom(sd.display, "_NET_WM_PID", False);
  673. const Atom utf8 = XInternAtom(sd.display, "UTF8_STRING", True);
  674. Atom actualType;
  675. int actualFormat;
  676. ulong numWindows, bytesAfter;
  677. uchar* data = nullptr;
  678. int status = XGetWindowProperty(sd.display, rootWindow, _ncl, 0L, (~0L), False, AnyPropertyType, &actualType, &actualFormat, &numWindows, &bytesAfter, &data);
  679. CARLA_SAFE_ASSERT_RETURN(data != nullptr, true);
  680. const ScopedFreeData sfd(data);
  681. CARLA_SAFE_ASSERT_RETURN(status == Success, true);
  682. CARLA_SAFE_ASSERT_RETURN(actualFormat == 32, true);
  683. CARLA_SAFE_ASSERT_RETURN(numWindows != 0, true);
  684. Window* windows = (Window*)data;
  685. Window lastGoodWindowPID = 0, lastGoodWindowNameSimple = 0, lastGoodWindowNameUTF8 = 0;
  686. for (ulong i = 0; i < numWindows; i++)
  687. {
  688. const Window window(windows[i]);
  689. CARLA_SAFE_ASSERT_CONTINUE(window != 0);
  690. // ------------------------------------------------
  691. // try using pid
  692. if (pid != 0)
  693. {
  694. ulong pidSize;
  695. uchar* pidData = nullptr;
  696. status = XGetWindowProperty(sd.display, window, _nwp, 0L, (~0L), False, XA_CARDINAL, &actualType, &actualFormat, &pidSize, &bytesAfter, &pidData);
  697. if (pidData != nullptr)
  698. {
  699. const ScopedFreeData sfd2(pidData);
  700. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  701. CARLA_SAFE_ASSERT_CONTINUE(pidSize != 0);
  702. if (*(ulong*)pidData == static_cast<ulong>(pid))
  703. lastGoodWindowPID = window;
  704. }
  705. }
  706. // ------------------------------------------------
  707. // try using name (UTF-8)
  708. ulong nameSize;
  709. uchar* nameData = nullptr;
  710. status = XGetWindowProperty(sd.display, window, _nwn, 0L, (~0L), False, utf8, &actualType, &actualFormat, &nameSize, &bytesAfter, &nameData);
  711. if (nameData != nullptr)
  712. {
  713. const ScopedFreeData sfd2(nameData);
  714. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  715. if (nameSize != 0 && std::strstr((const char*)nameData, uiTitle) != nullptr)
  716. lastGoodWindowNameUTF8 = window;
  717. }
  718. // ------------------------------------------------
  719. // try using name (simple)
  720. char* wmName = nullptr;
  721. status = XFetchName(sd.display, window, &wmName);
  722. if (wmName != nullptr)
  723. {
  724. const ScopedFreeData sfd2(wmName);
  725. CARLA_SAFE_ASSERT_CONTINUE(status != 0);
  726. if (std::strstr(wmName, uiTitle) != nullptr)
  727. lastGoodWindowNameSimple = window;
  728. }
  729. }
  730. if (lastGoodWindowPID == 0 && lastGoodWindowNameSimple == 0 && lastGoodWindowNameUTF8 == 0)
  731. return false;
  732. Window windowToMap;
  733. if (lastGoodWindowPID != 0)
  734. {
  735. if (lastGoodWindowPID == lastGoodWindowNameSimple && lastGoodWindowPID == lastGoodWindowNameUTF8)
  736. {
  737. carla_stdout("Match found using pid, simple and UTF-8 name all at once, nice!");
  738. windowToMap = lastGoodWindowPID;
  739. }
  740. else if (lastGoodWindowPID == lastGoodWindowNameUTF8)
  741. {
  742. carla_stdout("Match found using pid and UTF-8 name");
  743. windowToMap = lastGoodWindowPID;
  744. }
  745. else if (lastGoodWindowPID == lastGoodWindowNameSimple)
  746. {
  747. carla_stdout("Match found using pid and simple name");
  748. windowToMap = lastGoodWindowPID;
  749. }
  750. else
  751. {
  752. carla_stdout("Match found using pid");
  753. windowToMap = lastGoodWindowPID;
  754. }
  755. }
  756. else if (lastGoodWindowNameUTF8 != 0)
  757. {
  758. if (lastGoodWindowNameUTF8 == lastGoodWindowNameSimple)
  759. {
  760. carla_stdout("Match found using simple and UTF-8 name");
  761. windowToMap = lastGoodWindowNameUTF8;
  762. }
  763. else
  764. {
  765. carla_stdout("Match found using simple and UTF-8 name");
  766. windowToMap = lastGoodWindowNameUTF8;
  767. }
  768. }
  769. else
  770. {
  771. carla_stdout("Match found using simple name");
  772. windowToMap = lastGoodWindowNameSimple;
  773. }
  774. const Atom _nwt = XInternAtom(sd.display ,"_NET_WM_STATE", False);
  775. const Atom _nws[2] = {
  776. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_TASKBAR", False),
  777. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_PAGER", False)
  778. };
  779. XChangeProperty(sd.display, windowToMap, _nwt, XA_ATOM, 32, PropModeAppend, (const uchar*)_nws, 2);
  780. const Atom _nwi = XInternAtom(sd.display, "_NET_WM_ICON", False);
  781. XChangeProperty(sd.display, windowToMap, _nwi, XA_CARDINAL, 32, PropModeReplace, (const uchar*)sCarlaX11Icon, sCarlaX11IconSize);
  782. const Window hostWinId((Window)winId);
  783. XSetTransientForHint(sd.display, windowToMap, hostWinId);
  784. if (centerUI && false /* moving the window after being shown isn't pretty... */)
  785. {
  786. int hostX, hostY, pluginX, pluginY;
  787. uint hostWidth, hostHeight, pluginWidth, pluginHeight, border, depth;
  788. Window retWindow;
  789. if (XGetGeometry(sd.display, hostWinId, &retWindow, &hostX, &hostY, &hostWidth, &hostHeight, &border, &depth) != 0 &&
  790. XGetGeometry(sd.display, windowToMap, &retWindow, &pluginX, &pluginY, &pluginWidth, &pluginHeight, &border, &depth) != 0)
  791. {
  792. if (XTranslateCoordinates(sd.display, hostWinId, rootWindow, hostX, hostY, &hostX, &hostY, &retWindow) == True &&
  793. XTranslateCoordinates(sd.display, windowToMap, rootWindow, pluginX, pluginY, &pluginX, &pluginY, &retWindow) == True)
  794. {
  795. const int newX = hostX + int(hostWidth/2 - pluginWidth/2);
  796. const int newY = hostY + int(hostHeight/2 - pluginHeight/2);
  797. XMoveWindow(sd.display, windowToMap, newX, newY);
  798. }
  799. }
  800. }
  801. // focusing the host UI and then the plugin UI forces the WM to repaint the plugin window icon
  802. XRaiseWindow(sd.display, hostWinId);
  803. XSetInputFocus(sd.display, hostWinId, RevertToPointerRoot, CurrentTime);
  804. XRaiseWindow(sd.display, windowToMap);
  805. XSetInputFocus(sd.display, windowToMap, RevertToPointerRoot, CurrentTime);
  806. XFlush(sd.display);
  807. return true;
  808. #else
  809. return true;
  810. (void)pid; (void)centerUI;
  811. #endif
  812. }
  813. // -----------------------------------------------------
  814. #ifdef HAVE_X11
  815. CarlaPluginUI* CarlaPluginUI::newX11(Callback* cb, uintptr_t parentId, bool isResizable)
  816. {
  817. return new X11PluginUI(cb, parentId, isResizable);
  818. }
  819. #endif
  820. #ifdef CARLA_OS_MAC
  821. CarlaPluginUI* CarlaPluginUI::newCocoa(Callback* cb, uintptr_t parentId, bool isResizable)
  822. {
  823. return new CocoaPluginUI(cb, parentId, isResizable);
  824. }
  825. #endif
  826. #ifdef CARLA_OS_WIN
  827. CarlaPluginUI* CarlaPluginUI::newWindows(Callback* cb, uintptr_t parentId, bool isResizable)
  828. {
  829. return new WindowsPluginUI(cb, parentId, isResizable);
  830. }
  831. #endif
  832. // -----------------------------------------------------