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.

1061 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. fParentId(parentId)
  353. {
  354. [NSAutoreleasePool new];
  355. [NSApplication sharedApplication];
  356. fView = [NSView new];
  357. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,)
  358. if (isResizable)
  359. [fView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
  360. fWindow = [[CarlaPluginWindow new]retain];
  361. if (fWindow == 0)
  362. {
  363. [fView release];
  364. fView = nullptr;
  365. return;
  366. }
  367. if (! isResizable)
  368. [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
  369. [fWindow setup:cb view:fView];
  370. [fWindow setContentView:fView];
  371. [fWindow makeFirstResponder:fView];
  372. [fWindow makeKeyAndOrderFront:fWindow];
  373. [NSApp activateIgnoringOtherApps:YES];
  374. [fWindow center];
  375. if (parentId != 0)
  376. setTransientWinId(parentId);
  377. }
  378. ~CocoaPluginUI() override
  379. {
  380. if (fView == nullptr)
  381. return;
  382. [fWindow close];
  383. [fView release];
  384. [fWindow release];
  385. }
  386. void show() override
  387. {
  388. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,);
  389. [fView setHidden:NO];
  390. [fWindow setIsVisible:YES];
  391. if (fParentId != 0)
  392. setTransientWinId(fParentId);
  393. }
  394. void hide() override
  395. {
  396. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,);
  397. [fWindow setIsVisible:NO];
  398. [fView setHidden:YES];
  399. }
  400. void idle() override
  401. {
  402. }
  403. void focus() override
  404. {
  405. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  406. [fWindow makeKeyWindow];
  407. }
  408. void setSize(const uint width, const uint height, const bool forceUpdate) override
  409. {
  410. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  411. CARLA_SAFE_ASSERT_RETURN(fView != nullptr,);
  412. [fView setFrame:NSMakeRect(0, 0, width, height)];
  413. const NSSize size = NSMakeSize(width, height);
  414. [fWindow setContentSize:size];
  415. if (fIsResizable)
  416. {
  417. [fWindow setContentMinSize:NSMakeSize(1, 1)];
  418. [fWindow setContentMaxSize:NSMakeSize(99999, 99999)];
  419. [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:NO];
  420. }
  421. else
  422. {
  423. [fWindow setContentMinSize:size];
  424. [fWindow setContentMaxSize:size];
  425. [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
  426. }
  427. }
  428. void setTitle(const char* const title) override
  429. {
  430. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  431. NSString* titleString = [[NSString alloc]
  432. initWithBytes:title
  433. length:strlen(title)
  434. encoding:NSUTF8StringEncoding];
  435. [fWindow setTitle:titleString];
  436. }
  437. void setTransientWinId(const uintptr_t winId) override
  438. {
  439. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  440. NSWindow* window = [NSApp windowWithWindowNumber:winId];
  441. CARLA_SAFE_ASSERT_RETURN(window != nullptr,);
  442. [window addChildWindow:fWindow
  443. ordered:NSWindowAbove];
  444. [fWindow makeKeyWindow];
  445. }
  446. void setChildWindow(void* const winId) override
  447. {
  448. CARLA_SAFE_ASSERT_RETURN(winId != nullptr,);
  449. }
  450. void* getPtr() const noexcept override
  451. {
  452. return (void*)fView;
  453. }
  454. void* getDisplay() const noexcept
  455. {
  456. return (void*)fWindow;
  457. }
  458. private:
  459. NSView* fView;
  460. id fWindow;
  461. uintptr_t fParentId;
  462. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CocoaPluginUI)
  463. };
  464. #endif // CARLA_OS_MAC
  465. // ---------------------------------------------------------------------------------------------------------------------
  466. // Windows
  467. #ifdef CARLA_OS_WIN
  468. #define PUGL_LOCAL_CLOSE_MSG (WM_USER + 50)
  469. static LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  470. class WindowsPluginUI : public CarlaPluginUI
  471. {
  472. public:
  473. WindowsPluginUI(Callback* const cb, const uintptr_t parentId, const bool isResizable) noexcept
  474. : CarlaPluginUI(cb, isResizable),
  475. fWindow(0),
  476. fIsVisible(false),
  477. fFirstShow(true)
  478. {
  479. // FIXME
  480. static int wc_count = 0;
  481. char classNameBuf[256];
  482. std::srand((std::time(NULL)));
  483. _snprintf(classNameBuf, sizeof(classNameBuf), "CaWin_%d-%d", std::rand(), ++wc_count);
  484. classNameBuf[sizeof(classNameBuf)-1] = '\0';
  485. const HINSTANCE hInstance = GetModuleHandleA(nullptr);
  486. carla_zeroStruct(fWindowClass);
  487. fWindowClass.style = CS_OWNDC;
  488. fWindowClass.lpfnWndProc = wndProc;
  489. fWindowClass.hInstance = hInstance;
  490. fWindowClass.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
  491. fWindowClass.hCursor = LoadCursor(hInstance, IDC_ARROW);
  492. fWindowClass.lpszClassName = strdup(classNameBuf);
  493. if (!RegisterClass(&fWindowClass)) {
  494. free((void*)fWindowClass.lpszClassName);
  495. return;
  496. }
  497. int winFlags = WS_POPUPWINDOW | WS_CAPTION;
  498. if (isResizable)
  499. winFlags |= WS_SIZEBOX;
  500. fWindow = CreateWindowEx(WS_EX_TOPMOST,
  501. classNameBuf, "Carla Plugin UI", winFlags,
  502. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  503. NULL, NULL, hInstance, NULL);
  504. if (! fWindow) {
  505. UnregisterClass(fWindowClass.lpszClassName, NULL);
  506. free((void*)fWindowClass.lpszClassName);
  507. return;
  508. }
  509. SetWindowLongPtr(fWindow, GWLP_USERDATA, (LONG_PTR)this);
  510. if (parentId != 0)
  511. setTransientWinId(parentId);
  512. }
  513. ~WindowsPluginUI() override
  514. {
  515. CARLA_SAFE_ASSERT(! fIsVisible);
  516. if (fWindow != 0)
  517. {
  518. if (fIsVisible)
  519. ShowWindow(fWindow, SW_HIDE);
  520. DestroyWindow(fWindow);
  521. fWindow = 0;
  522. }
  523. // FIXME
  524. UnregisterClass(fWindowClass.lpszClassName, NULL);
  525. free((void*)fWindowClass.lpszClassName);
  526. }
  527. void show() override
  528. {
  529. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  530. ShowWindow(fWindow, fFirstShow ? SW_SHOWNORMAL : SW_RESTORE);
  531. fIsVisible = true;
  532. fFirstShow = false;
  533. UpdateWindow(fWindow);
  534. }
  535. void hide() override
  536. {
  537. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  538. ShowWindow(fWindow, SW_HIDE);
  539. fIsVisible = false;
  540. UpdateWindow(fWindow);
  541. }
  542. void idle() override
  543. {
  544. if (fIsIdling || fWindow == 0)
  545. return;
  546. MSG msg;
  547. fIsIdling = true;
  548. while (::PeekMessage(&msg, fWindow, 0, 0, PM_REMOVE))
  549. {
  550. switch (msg.message)
  551. {
  552. case WM_QUIT:
  553. case PUGL_LOCAL_CLOSE_MSG:
  554. fIsVisible = false;
  555. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  556. fCallback->handlePluginUIClosed();
  557. break;
  558. }
  559. DispatchMessageA(&msg);
  560. }
  561. fIsIdling = false;
  562. }
  563. LRESULT checkAndHandleMessage(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  564. {
  565. if (fWindow == hwnd)
  566. {
  567. switch (message)
  568. {
  569. case WM_QUIT:
  570. case PUGL_LOCAL_CLOSE_MSG:
  571. fIsVisible = false;
  572. CARLA_SAFE_ASSERT_BREAK(fCallback != nullptr);
  573. fCallback->handlePluginUIClosed();
  574. break;
  575. }
  576. }
  577. return DefWindowProcA(hwnd, message, wParam, lParam);
  578. }
  579. void focus() override
  580. {
  581. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  582. SetForegroundWindow(fWindow);
  583. SetActiveWindow(fWindow);
  584. SetFocus(fWindow);
  585. }
  586. void setSize(const uint width, const uint height, const bool forceUpdate) override
  587. {
  588. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  589. const int winFlags = WS_POPUPWINDOW | WS_CAPTION | (fIsResizable ? WS_SIZEBOX : 0x0);
  590. RECT wr = { 0, 0, static_cast<long>(width), static_cast<long>(height) };
  591. AdjustWindowRectEx(&wr, winFlags, FALSE, WS_EX_TOPMOST);
  592. SetWindowPos(fWindow, 0, 0, 0, wr.right-wr.left, wr.bottom-wr.top,
  593. SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
  594. if (forceUpdate)
  595. UpdateWindow(fWindow);
  596. }
  597. void setTitle(const char* const title) override
  598. {
  599. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  600. SetWindowTextA(fWindow, title);
  601. }
  602. void setTransientWinId(const uintptr_t winId) override
  603. {
  604. CARLA_SAFE_ASSERT_RETURN(fWindow != 0,);
  605. // TODO
  606. }
  607. void setChildWindow(void* const winId) override
  608. {
  609. CARLA_SAFE_ASSERT_RETURN(winId != nullptr,);
  610. }
  611. void* getPtr() const noexcept override
  612. {
  613. return (void*)fWindow;
  614. }
  615. void* getDisplay() const noexcept
  616. {
  617. return nullptr;
  618. }
  619. private:
  620. HWND fWindow;
  621. WNDCLASS fWindowClass;
  622. bool fIsVisible;
  623. bool fFirstShow;
  624. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(WindowsPluginUI)
  625. };
  626. LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  627. {
  628. switch (message)
  629. {
  630. case WM_CLOSE:
  631. PostMessage(hwnd, PUGL_LOCAL_CLOSE_MSG, wParam, lParam);
  632. return 0;
  633. #if 0
  634. case WM_CREATE:
  635. PostMessage(hwnd, WM_SHOWWINDOW, TRUE, 0);
  636. return 0;
  637. case WM_DESTROY:
  638. return 0;
  639. #endif
  640. default:
  641. if (WindowsPluginUI* const ui = (WindowsPluginUI*)GetWindowLongPtr(hwnd, GWLP_USERDATA))
  642. return ui->checkAndHandleMessage(hwnd, message, wParam, lParam);
  643. return DefWindowProcA(hwnd, message, wParam, lParam);
  644. }
  645. }
  646. #endif // CARLA_OS_WIN
  647. // -----------------------------------------------------
  648. bool CarlaPluginUI::tryTransientWinIdMatch(const uintptr_t pid, const char* const uiTitle, const uintptr_t winId, const bool centerUI)
  649. {
  650. CARLA_SAFE_ASSERT_RETURN(uiTitle != nullptr && uiTitle[0] != '\0', true);
  651. CARLA_SAFE_ASSERT_RETURN(winId != 0, true);
  652. #if defined(HAVE_X11)
  653. struct ScopedDisplay {
  654. Display* display;
  655. ScopedDisplay() : display(XOpenDisplay(nullptr)) {}
  656. ~ScopedDisplay() { if (display!=nullptr) XCloseDisplay(display); }
  657. // c++ compat stuff
  658. CARLA_PREVENT_HEAP_ALLOCATION
  659. CARLA_DECLARE_NON_COPY_CLASS(ScopedDisplay)
  660. };
  661. struct ScopedFreeData {
  662. union {
  663. char* data;
  664. uchar* udata;
  665. };
  666. ScopedFreeData(char* d) : data(d) {}
  667. ScopedFreeData(uchar* d) : udata(d) {}
  668. ~ScopedFreeData() { XFree(data); }
  669. // c++ compat stuff
  670. CARLA_PREVENT_HEAP_ALLOCATION
  671. CARLA_DECLARE_NON_COPY_CLASS(ScopedFreeData)
  672. };
  673. const ScopedDisplay sd;
  674. CARLA_SAFE_ASSERT_RETURN(sd.display != nullptr, true);
  675. const Window rootWindow(DefaultRootWindow(sd.display));
  676. const Atom _ncl = XInternAtom(sd.display, "_NET_CLIENT_LIST" , False);
  677. const Atom _nwn = XInternAtom(sd.display, "_NET_WM_NAME", False);
  678. const Atom _nwp = XInternAtom(sd.display, "_NET_WM_PID", False);
  679. const Atom utf8 = XInternAtom(sd.display, "UTF8_STRING", True);
  680. Atom actualType;
  681. int actualFormat;
  682. ulong numWindows, bytesAfter;
  683. uchar* data = nullptr;
  684. int status = XGetWindowProperty(sd.display, rootWindow, _ncl, 0L, (~0L), False, AnyPropertyType, &actualType, &actualFormat, &numWindows, &bytesAfter, &data);
  685. CARLA_SAFE_ASSERT_RETURN(data != nullptr, true);
  686. const ScopedFreeData sfd(data);
  687. CARLA_SAFE_ASSERT_RETURN(status == Success, true);
  688. CARLA_SAFE_ASSERT_RETURN(actualFormat == 32, true);
  689. CARLA_SAFE_ASSERT_RETURN(numWindows != 0, true);
  690. Window* windows = (Window*)data;
  691. Window lastGoodWindowPID = 0, lastGoodWindowNameSimple = 0, lastGoodWindowNameUTF8 = 0;
  692. for (ulong i = 0; i < numWindows; i++)
  693. {
  694. const Window window(windows[i]);
  695. CARLA_SAFE_ASSERT_CONTINUE(window != 0);
  696. // ------------------------------------------------
  697. // try using pid
  698. if (pid != 0)
  699. {
  700. ulong pidSize;
  701. uchar* pidData = nullptr;
  702. status = XGetWindowProperty(sd.display, window, _nwp, 0L, (~0L), False, XA_CARDINAL, &actualType, &actualFormat, &pidSize, &bytesAfter, &pidData);
  703. if (pidData != nullptr)
  704. {
  705. const ScopedFreeData sfd2(pidData);
  706. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  707. CARLA_SAFE_ASSERT_CONTINUE(pidSize != 0);
  708. if (*(ulong*)pidData == static_cast<ulong>(pid))
  709. lastGoodWindowPID = window;
  710. }
  711. }
  712. // ------------------------------------------------
  713. // try using name (UTF-8)
  714. ulong nameSize;
  715. uchar* nameData = nullptr;
  716. status = XGetWindowProperty(sd.display, window, _nwn, 0L, (~0L), False, utf8, &actualType, &actualFormat, &nameSize, &bytesAfter, &nameData);
  717. if (nameData != nullptr)
  718. {
  719. const ScopedFreeData sfd2(nameData);
  720. CARLA_SAFE_ASSERT_CONTINUE(status == Success);
  721. if (nameSize != 0 && std::strstr((const char*)nameData, uiTitle) != nullptr)
  722. lastGoodWindowNameUTF8 = window;
  723. }
  724. // ------------------------------------------------
  725. // try using name (simple)
  726. char* wmName = nullptr;
  727. status = XFetchName(sd.display, window, &wmName);
  728. if (wmName != nullptr)
  729. {
  730. const ScopedFreeData sfd2(wmName);
  731. CARLA_SAFE_ASSERT_CONTINUE(status != 0);
  732. if (std::strstr(wmName, uiTitle) != nullptr)
  733. lastGoodWindowNameSimple = window;
  734. }
  735. }
  736. if (lastGoodWindowPID == 0 && lastGoodWindowNameSimple == 0 && lastGoodWindowNameUTF8 == 0)
  737. return false;
  738. Window windowToMap;
  739. if (lastGoodWindowPID != 0)
  740. {
  741. if (lastGoodWindowPID == lastGoodWindowNameSimple && lastGoodWindowPID == lastGoodWindowNameUTF8)
  742. {
  743. carla_stdout("Match found using pid, simple and UTF-8 name all at once, nice!");
  744. windowToMap = lastGoodWindowPID;
  745. }
  746. else if (lastGoodWindowPID == lastGoodWindowNameUTF8)
  747. {
  748. carla_stdout("Match found using pid and UTF-8 name");
  749. windowToMap = lastGoodWindowPID;
  750. }
  751. else if (lastGoodWindowPID == lastGoodWindowNameSimple)
  752. {
  753. carla_stdout("Match found using pid and simple name");
  754. windowToMap = lastGoodWindowPID;
  755. }
  756. else
  757. {
  758. carla_stdout("Match found using pid");
  759. windowToMap = lastGoodWindowPID;
  760. }
  761. }
  762. else if (lastGoodWindowNameUTF8 != 0)
  763. {
  764. if (lastGoodWindowNameUTF8 == lastGoodWindowNameSimple)
  765. {
  766. carla_stdout("Match found using simple and UTF-8 name");
  767. windowToMap = lastGoodWindowNameUTF8;
  768. }
  769. else
  770. {
  771. carla_stdout("Match found using simple and UTF-8 name");
  772. windowToMap = lastGoodWindowNameUTF8;
  773. }
  774. }
  775. else
  776. {
  777. carla_stdout("Match found using simple name");
  778. windowToMap = lastGoodWindowNameSimple;
  779. }
  780. const Atom _nwt = XInternAtom(sd.display ,"_NET_WM_STATE", False);
  781. const Atom _nws[2] = {
  782. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_TASKBAR", False),
  783. XInternAtom(sd.display, "_NET_WM_STATE_SKIP_PAGER", False)
  784. };
  785. XChangeProperty(sd.display, windowToMap, _nwt, XA_ATOM, 32, PropModeAppend, (const uchar*)_nws, 2);
  786. const Atom _nwi = XInternAtom(sd.display, "_NET_WM_ICON", False);
  787. XChangeProperty(sd.display, windowToMap, _nwi, XA_CARDINAL, 32, PropModeReplace, (const uchar*)sCarlaX11Icon, sCarlaX11IconSize);
  788. const Window hostWinId((Window)winId);
  789. XSetTransientForHint(sd.display, windowToMap, hostWinId);
  790. if (centerUI && false /* moving the window after being shown isn't pretty... */)
  791. {
  792. int hostX, hostY, pluginX, pluginY;
  793. uint hostWidth, hostHeight, pluginWidth, pluginHeight, border, depth;
  794. Window retWindow;
  795. if (XGetGeometry(sd.display, hostWinId, &retWindow, &hostX, &hostY, &hostWidth, &hostHeight, &border, &depth) != 0 &&
  796. XGetGeometry(sd.display, windowToMap, &retWindow, &pluginX, &pluginY, &pluginWidth, &pluginHeight, &border, &depth) != 0)
  797. {
  798. if (XTranslateCoordinates(sd.display, hostWinId, rootWindow, hostX, hostY, &hostX, &hostY, &retWindow) == True &&
  799. XTranslateCoordinates(sd.display, windowToMap, rootWindow, pluginX, pluginY, &pluginX, &pluginY, &retWindow) == True)
  800. {
  801. const int newX = hostX + int(hostWidth/2 - pluginWidth/2);
  802. const int newY = hostY + int(hostHeight/2 - pluginHeight/2);
  803. XMoveWindow(sd.display, windowToMap, newX, newY);
  804. }
  805. }
  806. }
  807. // focusing the host UI and then the plugin UI forces the WM to repaint the plugin window icon
  808. XRaiseWindow(sd.display, hostWinId);
  809. XSetInputFocus(sd.display, hostWinId, RevertToPointerRoot, CurrentTime);
  810. XRaiseWindow(sd.display, windowToMap);
  811. XSetInputFocus(sd.display, windowToMap, RevertToPointerRoot, CurrentTime);
  812. XFlush(sd.display);
  813. return true;
  814. #else
  815. return true;
  816. (void)pid; (void)centerUI;
  817. #endif
  818. }
  819. // -----------------------------------------------------
  820. #ifdef HAVE_X11
  821. CarlaPluginUI* CarlaPluginUI::newX11(Callback* cb, uintptr_t parentId, bool isResizable)
  822. {
  823. return new X11PluginUI(cb, parentId, isResizable);
  824. }
  825. #endif
  826. #ifdef CARLA_OS_MAC
  827. CarlaPluginUI* CarlaPluginUI::newCocoa(Callback* cb, uintptr_t parentId, bool isResizable)
  828. {
  829. return new CocoaPluginUI(cb, parentId, isResizable);
  830. }
  831. #endif
  832. #ifdef CARLA_OS_WIN
  833. CarlaPluginUI* CarlaPluginUI::newWindows(Callback* cb, uintptr_t parentId, bool isResizable)
  834. {
  835. return new WindowsPluginUI(cb, parentId, isResizable);
  836. }
  837. #endif
  838. // -----------------------------------------------------