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.

1269 lines
31KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. // we need this for now
  17. //#define PUGL_GRAB_FOCUS 1
  18. #include "../Base.hpp"
  19. #undef PUGL_HAVE_CAIRO
  20. #undef PUGL_HAVE_GL
  21. #define PUGL_HAVE_GL 1
  22. #include "pugl/pugl.h"
  23. #if defined(__GNUC__) && (__GNUC__ >= 7)
  24. # pragma GCC diagnostic push
  25. # pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
  26. #endif
  27. #if defined(DISTRHO_OS_WINDOWS)
  28. # include "pugl/pugl_win.cpp"
  29. #elif defined(DISTRHO_OS_MAC)
  30. # include "pugl/pugl_osx.m"
  31. #elif defined(DISTRHO_OS_LINUX)
  32. # include <sys/types.h>
  33. # include <unistd.h>
  34. extern "C" {
  35. # include "pugl/pugl_x11.c"
  36. }
  37. #else
  38. # error Unsupported platform
  39. #endif
  40. #if defined(__GNUC__) && (__GNUC__ >= 7)
  41. # pragma GCC diagnostic pop
  42. #endif
  43. #include "ApplicationPrivateData.hpp"
  44. #include "WidgetPrivateData.hpp"
  45. #include "../StandaloneWindow.hpp"
  46. #include "../../distrho/extra/String.hpp"
  47. #define FOR_EACH_WIDGET(it) \
  48. for (std::list<Widget*>::iterator it = fWidgets.begin(); it != fWidgets.end(); ++it)
  49. #define FOR_EACH_WIDGET_INV(rit) \
  50. for (std::list<Widget*>::reverse_iterator rit = fWidgets.rbegin(); rit != fWidgets.rend(); ++rit)
  51. #ifdef DEBUG
  52. # define DBG(msg) std::fprintf(stderr, "%s", msg);
  53. # define DBGp(...) std::fprintf(stderr, __VA_ARGS__);
  54. # define DBGF std::fflush(stderr);
  55. #else
  56. # define DBG(msg)
  57. # define DBGp(...)
  58. # define DBGF
  59. #endif
  60. START_NAMESPACE_DGL
  61. // -----------------------------------------------------------------------
  62. // Window Private
  63. struct Window::PrivateData {
  64. PrivateData(Application& app, Window* const self)
  65. : fApp(app),
  66. fSelf(self),
  67. fView(puglInit()),
  68. fFirstInit(true),
  69. fVisible(false),
  70. fResizable(true),
  71. fUsingEmbed(false),
  72. fWidth(1),
  73. fHeight(1),
  74. fTitle(nullptr),
  75. fWidgets(),
  76. fModal(),
  77. #if defined(DISTRHO_OS_WINDOWS)
  78. hwnd(0)
  79. #elif defined(DISTRHO_OS_LINUX)
  80. xDisplay(nullptr),
  81. xWindow(0)
  82. #elif defined(DISTRHO_OS_MAC)
  83. fNeedsIdle(true),
  84. mView(nullptr),
  85. mWindow(nullptr)
  86. #endif
  87. {
  88. DBG("Creating window without parent..."); DBGF;
  89. init();
  90. }
  91. PrivateData(Application& app, Window* const self, Window& parent)
  92. : fApp(app),
  93. fSelf(self),
  94. fView(puglInit()),
  95. fFirstInit(true),
  96. fVisible(false),
  97. fResizable(true),
  98. fUsingEmbed(false),
  99. fWidth(1),
  100. fHeight(1),
  101. fTitle(nullptr),
  102. fWidgets(),
  103. fModal(parent.pData),
  104. #if defined(DISTRHO_OS_WINDOWS)
  105. hwnd(0)
  106. #elif defined(DISTRHO_OS_LINUX)
  107. xDisplay(nullptr),
  108. xWindow(0)
  109. #elif defined(DISTRHO_OS_MAC)
  110. fNeedsIdle(false),
  111. mView(nullptr),
  112. mWindow(nullptr)
  113. #endif
  114. {
  115. DBG("Creating window with parent..."); DBGF;
  116. init();
  117. const PuglInternals* const parentImpl(parent.pData->fView->impl);
  118. #if defined(DISTRHO_OS_LINUX)
  119. XSetTransientForHint(xDisplay, xWindow, parentImpl->win);
  120. //#elif defined(DISTRHO_OS_MAC)
  121. // [parentImpl->window orderWindow:NSWindowBelow relativeTo:[[mView window] windowNumber]];
  122. #else
  123. // unused
  124. return; (void)parentImpl;
  125. #endif
  126. }
  127. PrivateData(Application& app, Window* const self, const intptr_t parentId)
  128. : fApp(app),
  129. fSelf(self),
  130. fView(puglInit()),
  131. fFirstInit(true),
  132. fVisible(parentId != 0),
  133. fResizable(parentId == 0),
  134. fUsingEmbed(parentId != 0),
  135. fWidth(1),
  136. fHeight(1),
  137. fTitle(nullptr),
  138. fWidgets(),
  139. fModal(),
  140. #if defined(DISTRHO_OS_WINDOWS)
  141. hwnd(0)
  142. #elif defined(DISTRHO_OS_LINUX)
  143. xDisplay(nullptr),
  144. xWindow(0)
  145. #elif defined(DISTRHO_OS_MAC)
  146. fNeedsIdle(parentId == 0),
  147. mView(nullptr),
  148. mWindow(nullptr)
  149. #endif
  150. {
  151. if (fUsingEmbed)
  152. {
  153. DBG("Creating embedded window..."); DBGF;
  154. puglInitWindowParent(fView, parentId);
  155. }
  156. else
  157. {
  158. DBG("Creating window without parent..."); DBGF;
  159. }
  160. init();
  161. if (fUsingEmbed)
  162. {
  163. DBG("NOTE: Embed window is always visible and non-resizable\n");
  164. puglShowWindow(fView);
  165. fApp.pData->oneShown();
  166. fFirstInit = false;
  167. }
  168. }
  169. void init()
  170. {
  171. if (fSelf == nullptr || fView == nullptr)
  172. {
  173. DBG("Failed!\n");
  174. return;
  175. }
  176. puglInitContextType(fView, PUGL_GL);
  177. puglInitUserResizable(fView, fResizable);
  178. puglInitWindowSize(fView, static_cast<int>(fWidth), static_cast<int>(fHeight));
  179. puglSetHandle(fView, this);
  180. puglSetDisplayFunc(fView, onDisplayCallback);
  181. puglSetKeyboardFunc(fView, onKeyboardCallback);
  182. puglSetMotionFunc(fView, onMotionCallback);
  183. puglSetMouseFunc(fView, onMouseCallback);
  184. puglSetScrollFunc(fView, onScrollCallback);
  185. puglSetSpecialFunc(fView, onSpecialCallback);
  186. puglSetReshapeFunc(fView, onReshapeCallback);
  187. puglSetCloseFunc(fView, onCloseCallback);
  188. #ifndef DGL_FILE_BROWSER_DISABLED
  189. puglSetFileSelectedFunc(fView, fileBrowserSelectedCallback);
  190. #endif
  191. puglCreateWindow(fView, nullptr);
  192. PuglInternals* impl = fView->impl;
  193. #if defined(DISTRHO_OS_WINDOWS)
  194. hwnd = impl->hwnd;
  195. DISTRHO_SAFE_ASSERT(hwnd != 0);
  196. #elif defined(DISTRHO_OS_MAC)
  197. mView = impl->glview;
  198. mWindow = impl->window;
  199. DISTRHO_SAFE_ASSERT(mView != nullptr);
  200. if (fUsingEmbed) {
  201. DISTRHO_SAFE_ASSERT(mWindow == nullptr);
  202. } else {
  203. DISTRHO_SAFE_ASSERT(mWindow != nullptr);
  204. }
  205. #elif defined(DISTRHO_OS_LINUX)
  206. xDisplay = impl->display;
  207. xWindow = impl->win;
  208. DISTRHO_SAFE_ASSERT(xWindow != 0);
  209. if (! fUsingEmbed)
  210. {
  211. pid_t pid = getpid();
  212. Atom _nwp = XInternAtom(xDisplay, "_NET_WM_PID", True);
  213. XChangeProperty(xDisplay, xWindow, _nwp, XA_CARDINAL, 32, PropModeReplace, (const uchar*)&pid, 1);
  214. }
  215. #endif
  216. puglEnterContext(fView);
  217. fApp.pData->windows.push_back(fSelf);
  218. DBG("Success!\n");
  219. }
  220. ~PrivateData()
  221. {
  222. DBG("Destroying window..."); DBGF;
  223. if (fModal.enabled)
  224. {
  225. exec_fini();
  226. close();
  227. }
  228. fWidgets.clear();
  229. if (fUsingEmbed)
  230. {
  231. puglHideWindow(fView);
  232. fApp.pData->oneHidden();
  233. }
  234. if (fSelf != nullptr)
  235. {
  236. fApp.pData->windows.remove(fSelf);
  237. fSelf = nullptr;
  238. }
  239. if (fView != nullptr)
  240. {
  241. puglDestroy(fView);
  242. fView = nullptr;
  243. }
  244. if (fTitle != nullptr)
  245. {
  246. std::free(fTitle);
  247. fTitle = nullptr;
  248. }
  249. #if defined(DISTRHO_OS_WINDOWS)
  250. hwnd = 0;
  251. #elif defined(DISTRHO_OS_MAC)
  252. mView = nullptr;
  253. mWindow = nullptr;
  254. #elif defined(DISTRHO_OS_LINUX)
  255. xDisplay = nullptr;
  256. xWindow = 0;
  257. #endif
  258. DBG("Success!\n");
  259. }
  260. // -------------------------------------------------------------------
  261. void close()
  262. {
  263. DBG("Window close\n");
  264. if (fUsingEmbed)
  265. return;
  266. setVisible(false);
  267. if (! fFirstInit)
  268. {
  269. fApp.pData->oneHidden();
  270. fFirstInit = true;
  271. }
  272. }
  273. void exec(const bool lockWait)
  274. {
  275. DBG("Window exec\n");
  276. exec_init();
  277. if (lockWait)
  278. {
  279. for (; fVisible && fModal.enabled;)
  280. {
  281. idle();
  282. d_msleep(10);
  283. }
  284. exec_fini();
  285. }
  286. else
  287. {
  288. idle();
  289. }
  290. }
  291. // -------------------------------------------------------------------
  292. void exec_init()
  293. {
  294. DBG("Window modal loop starting..."); DBGF;
  295. DISTRHO_SAFE_ASSERT_RETURN(fModal.parent != nullptr, setVisible(true));
  296. fModal.enabled = true;
  297. fModal.parent->fModal.childFocus = this;
  298. #ifdef DISTRHO_OS_WINDOWS
  299. // Center this window
  300. PuglInternals* const parentImpl = fModal.parent->fView->impl;
  301. RECT curRect;
  302. RECT parentRect;
  303. GetWindowRect(hwnd, &curRect);
  304. GetWindowRect(parentImpl->hwnd, &parentRect);
  305. int x = parentRect.left+(parentRect.right-curRect.right)/2;
  306. int y = parentRect.top +(parentRect.bottom-curRect.bottom)/2;
  307. SetWindowPos(hwnd, 0, x, y, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
  308. UpdateWindow(hwnd);
  309. #endif
  310. fModal.parent->setVisible(true);
  311. setVisible(true);
  312. DBG("Ok\n");
  313. }
  314. void exec_fini()
  315. {
  316. DBG("Window modal loop stopping..."); DBGF;
  317. fModal.enabled = false;
  318. if (fModal.parent != nullptr)
  319. {
  320. fModal.parent->fModal.childFocus = nullptr;
  321. // the mouse position probably changed since the modal appeared,
  322. // so send a mouse motion event to the modal's parent window
  323. #if defined(DISTRHO_OS_WINDOWS)
  324. // TODO
  325. #elif defined(DISTRHO_OS_MAC)
  326. // TODO
  327. #elif defined(DISTRHO_OS_LINUX)
  328. int i, wx, wy;
  329. uint u;
  330. ::Window w;
  331. if (XQueryPointer(fModal.parent->xDisplay, fModal.parent->xWindow, &w, &w, &i, &i, &wx, &wy, &u) == True)
  332. fModal.parent->onPuglMotion(wx, wy);
  333. #endif
  334. }
  335. DBG("Ok\n");
  336. }
  337. // -------------------------------------------------------------------
  338. void focus()
  339. {
  340. DBG("Window focus\n");
  341. #if defined(DISTRHO_OS_WINDOWS)
  342. SetForegroundWindow(hwnd);
  343. SetActiveWindow(hwnd);
  344. SetFocus(hwnd);
  345. #elif defined(DISTRHO_OS_MAC)
  346. if (mWindow != nullptr)
  347. [mWindow makeKeyWindow];
  348. #elif defined(DISTRHO_OS_LINUX)
  349. XRaiseWindow(xDisplay, xWindow);
  350. XSetInputFocus(xDisplay, xWindow, RevertToPointerRoot, CurrentTime);
  351. XFlush(xDisplay);
  352. #endif
  353. }
  354. // -------------------------------------------------------------------
  355. void setVisible(const bool yesNo)
  356. {
  357. if (fVisible == yesNo)
  358. {
  359. DBG("Window setVisible matches current state, ignoring request\n");
  360. return;
  361. }
  362. if (fUsingEmbed)
  363. {
  364. DBG("Window setVisible cannot be called when embedded\n");
  365. return;
  366. }
  367. DBG("Window setVisible called\n");
  368. fVisible = yesNo;
  369. if (yesNo && fFirstInit)
  370. setSize(fWidth, fHeight, true);
  371. #if defined(DISTRHO_OS_WINDOWS)
  372. if (yesNo)
  373. ShowWindow(hwnd, fFirstInit ? SW_SHOWNORMAL : SW_RESTORE);
  374. else
  375. ShowWindow(hwnd, SW_HIDE);
  376. UpdateWindow(hwnd);
  377. #elif defined(DISTRHO_OS_MAC)
  378. if (yesNo)
  379. {
  380. if (mWindow != nullptr)
  381. [mWindow setIsVisible:YES];
  382. else
  383. [mView setHidden:NO];
  384. }
  385. else
  386. {
  387. if (mWindow != nullptr)
  388. [mWindow setIsVisible:NO];
  389. else
  390. [mView setHidden:YES];
  391. }
  392. #elif defined(DISTRHO_OS_LINUX)
  393. if (yesNo)
  394. XMapRaised(xDisplay, xWindow);
  395. else
  396. XUnmapWindow(xDisplay, xWindow);
  397. XFlush(xDisplay);
  398. #endif
  399. if (yesNo)
  400. {
  401. if (fFirstInit)
  402. {
  403. fApp.pData->oneShown();
  404. fFirstInit = false;
  405. }
  406. }
  407. else if (fModal.enabled)
  408. exec_fini();
  409. }
  410. // -------------------------------------------------------------------
  411. void setResizable(const bool yesNo)
  412. {
  413. if (fResizable == yesNo)
  414. {
  415. DBG("Window setResizable matches current state, ignoring request\n");
  416. return;
  417. }
  418. if (fUsingEmbed)
  419. {
  420. DBG("Window setResizable cannot be called when embedded\n");
  421. return;
  422. }
  423. DBG("Window setResizable called\n");
  424. fResizable = yesNo;
  425. #if defined(DISTRHO_OS_WINDOWS)
  426. const int winFlags = fResizable ? GetWindowLong(hwnd, GWL_STYLE) | WS_SIZEBOX
  427. : GetWindowLong(hwnd, GWL_STYLE) & ~WS_SIZEBOX;
  428. SetWindowLong(hwnd, GWL_STYLE, winFlags);
  429. #elif defined(DISTRHO_OS_MAC)
  430. const uint flags(yesNo ? (NSViewWidthSizable|NSViewHeightSizable) : 0x0);
  431. [mView setAutoresizingMask:flags];
  432. #endif
  433. setSize(fWidth, fHeight, true);
  434. }
  435. // -------------------------------------------------------------------
  436. void setSize(uint width, uint height, const bool forced = false)
  437. {
  438. if (width <= 1 || height <= 1)
  439. {
  440. DBGp("Window setSize called with invalid value(s) %i %i, ignoring request\n", width, height);
  441. return;
  442. }
  443. if (fWidth == width && fHeight == height && ! forced)
  444. {
  445. DBGp("Window setSize matches current size, ignoring request (%i %i)\n", width, height);
  446. return;
  447. }
  448. fWidth = width;
  449. fHeight = height;
  450. DBGp("Window setSize called %s, size %i %i, resizable %s\n", forced ? "(forced)" : "(not forced)", width, height, fResizable?"true":"false");
  451. #if defined(DISTRHO_OS_WINDOWS)
  452. const int winFlags = WS_POPUPWINDOW | WS_CAPTION | (fResizable ? WS_SIZEBOX : 0x0);
  453. RECT wr = { 0, 0, static_cast<long>(width), static_cast<long>(height) };
  454. AdjustWindowRectEx(&wr, fUsingEmbed ? WS_CHILD : winFlags, FALSE, WS_EX_TOPMOST);
  455. SetWindowPos(hwnd, 0, 0, 0, wr.right-wr.left, wr.bottom-wr.top,
  456. SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
  457. if (! forced)
  458. UpdateWindow(hwnd);
  459. #elif defined(DISTRHO_OS_MAC)
  460. [mView setFrame:NSMakeRect(0, 0, width, height)];
  461. if (mWindow != nullptr)
  462. {
  463. const NSSize size = NSMakeSize(width, height);
  464. [mWindow setContentSize:size];
  465. if (fResizable)
  466. {
  467. [mWindow setContentMinSize:NSMakeSize(1, 1)];
  468. [mWindow setContentMaxSize:NSMakeSize(99999, 99999)];
  469. [[mWindow standardWindowButton:NSWindowZoomButton] setHidden:NO];
  470. }
  471. else
  472. {
  473. [mWindow setContentMinSize:size];
  474. [mWindow setContentMaxSize:size];
  475. [[mWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
  476. }
  477. }
  478. #elif defined(DISTRHO_OS_LINUX)
  479. XResizeWindow(xDisplay, xWindow, width, height);
  480. if (! fResizable)
  481. {
  482. XSizeHints sizeHints;
  483. memset(&sizeHints, 0, sizeof(sizeHints));
  484. sizeHints.flags = PSize|PMinSize|PMaxSize;
  485. sizeHints.width = static_cast<int>(width);
  486. sizeHints.height = static_cast<int>(height);
  487. sizeHints.min_width = static_cast<int>(width);
  488. sizeHints.min_height = static_cast<int>(height);
  489. sizeHints.max_width = static_cast<int>(width);
  490. sizeHints.max_height = static_cast<int>(height);
  491. XSetNormalHints(xDisplay, xWindow, &sizeHints);
  492. }
  493. if (! forced)
  494. XFlush(xDisplay);
  495. #endif
  496. puglPostRedisplay(fView);
  497. }
  498. // -------------------------------------------------------------------
  499. const char* getTitle() const noexcept
  500. {
  501. static const char* const kFallback = "";
  502. return fTitle != nullptr ? fTitle : kFallback;
  503. }
  504. void setTitle(const char* const title)
  505. {
  506. DBGp("Window setTitle \"%s\"\n", title);
  507. if (fTitle != nullptr)
  508. std::free(fTitle);
  509. fTitle = strdup(title);
  510. #if defined(DISTRHO_OS_WINDOWS)
  511. SetWindowTextA(hwnd, title);
  512. #elif defined(DISTRHO_OS_MAC)
  513. if (mWindow != nullptr)
  514. {
  515. NSString* titleString = [[NSString alloc]
  516. initWithBytes:title
  517. length:strlen(title)
  518. encoding:NSUTF8StringEncoding];
  519. [mWindow setTitle:titleString];
  520. }
  521. #elif defined(DISTRHO_OS_LINUX)
  522. XStoreName(xDisplay, xWindow, title);
  523. #endif
  524. }
  525. void setTransientWinId(const uintptr_t winId)
  526. {
  527. DISTRHO_SAFE_ASSERT_RETURN(winId != 0,);
  528. #if defined(DISTRHO_OS_MAC)
  529. NSWindow* window = [NSApp windowWithWindowNumber:winId];
  530. DISTRHO_SAFE_ASSERT_RETURN(window != nullptr,);
  531. [window addChildWindow:mWindow
  532. ordered:NSWindowAbove];
  533. [mWindow makeKeyWindow];
  534. #elif defined(DISTRHO_OS_LINUX)
  535. XSetTransientForHint(xDisplay, xWindow, static_cast< ::Window>(winId));
  536. #endif
  537. }
  538. // -------------------------------------------------------------------
  539. void addWidget(Widget* const widget)
  540. {
  541. fWidgets.push_back(widget);
  542. }
  543. void removeWidget(Widget* const widget)
  544. {
  545. fWidgets.remove(widget);
  546. }
  547. void idle()
  548. {
  549. puglProcessEvents(fView);
  550. #ifdef DISTRHO_OS_MAC
  551. if (fNeedsIdle)
  552. {
  553. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  554. NSEvent* event;
  555. for (;;)
  556. {
  557. event = [NSApp
  558. nextEventMatchingMask:NSAnyEventMask
  559. untilDate:[NSDate distantPast]
  560. inMode:NSDefaultRunLoopMode
  561. dequeue:YES];
  562. if (event == nil)
  563. break;
  564. [NSApp sendEvent: event];
  565. }
  566. [pool release];
  567. }
  568. #endif
  569. if (fModal.enabled && fModal.parent != nullptr)
  570. fModal.parent->idle();
  571. }
  572. // -------------------------------------------------------------------
  573. void onPuglDisplay()
  574. {
  575. fSelf->onDisplayBefore();
  576. FOR_EACH_WIDGET(it)
  577. {
  578. Widget* const widget(*it);
  579. widget->pData->display(fWidth, fHeight);
  580. }
  581. fSelf->onDisplayAfter();
  582. }
  583. int onPuglKeyboard(const bool press, const uint key)
  584. {
  585. DBGp("PUGL: onKeyboard : %i %i\n", press, key);
  586. if (fModal.childFocus != nullptr)
  587. {
  588. fModal.childFocus->focus();
  589. return 0;
  590. }
  591. Widget::KeyboardEvent ev;
  592. ev.press = press;
  593. ev.key = key;
  594. ev.mod = static_cast<Modifier>(puglGetModifiers(fView));
  595. ev.time = puglGetEventTimestamp(fView);
  596. FOR_EACH_WIDGET_INV(rit)
  597. {
  598. Widget* const widget(*rit);
  599. if (widget->isVisible() && widget->onKeyboard(ev))
  600. return 0;
  601. }
  602. return 1;
  603. }
  604. int onPuglSpecial(const bool press, const Key key)
  605. {
  606. DBGp("PUGL: onSpecial : %i %i\n", press, key);
  607. if (fModal.childFocus != nullptr)
  608. {
  609. fModal.childFocus->focus();
  610. return 0;
  611. }
  612. Widget::SpecialEvent ev;
  613. ev.press = press;
  614. ev.key = key;
  615. ev.mod = static_cast<Modifier>(puglGetModifiers(fView));
  616. ev.time = puglGetEventTimestamp(fView);
  617. FOR_EACH_WIDGET_INV(rit)
  618. {
  619. Widget* const widget(*rit);
  620. if (widget->isVisible() && widget->onSpecial(ev))
  621. return 0;
  622. }
  623. return 1;
  624. }
  625. void onPuglMouse(const int button, const bool press, const int x, const int y)
  626. {
  627. DBGp("PUGL: onMouse : %i %i %i %i\n", button, press, x, y);
  628. // FIXME - pugl sends 2 of these for each window on init, don't ask me why. we'll ignore it
  629. if (press && button == 0 && x == 0 && y == 0) return;
  630. if (fModal.childFocus != nullptr)
  631. return fModal.childFocus->focus();
  632. Widget::MouseEvent ev;
  633. ev.button = button;
  634. ev.press = press;
  635. ev.mod = static_cast<Modifier>(puglGetModifiers(fView));
  636. ev.time = puglGetEventTimestamp(fView);
  637. FOR_EACH_WIDGET_INV(rit)
  638. {
  639. Widget* const widget(*rit);
  640. ev.pos = Point<int>(x-widget->getAbsoluteX(), y-widget->getAbsoluteY());
  641. if (widget->isVisible() && widget->onMouse(ev))
  642. break;
  643. }
  644. }
  645. void onPuglMotion(const int x, const int y)
  646. {
  647. DBGp("PUGL: onMotion : %i %i\n", x, y);
  648. if (fModal.childFocus != nullptr)
  649. return;
  650. Widget::MotionEvent ev;
  651. ev.mod = static_cast<Modifier>(puglGetModifiers(fView));
  652. ev.time = puglGetEventTimestamp(fView);
  653. FOR_EACH_WIDGET_INV(rit)
  654. {
  655. Widget* const widget(*rit);
  656. ev.pos = Point<int>(x-widget->getAbsoluteX(), y-widget->getAbsoluteY());
  657. if (widget->isVisible() && widget->onMotion(ev))
  658. break;
  659. }
  660. }
  661. void onPuglScroll(const int x, const int y, const float dx, const float dy)
  662. {
  663. DBGp("PUGL: onScroll : %i %i %f %f\n", x, y, dx, dy);
  664. if (fModal.childFocus != nullptr)
  665. return;
  666. Widget::ScrollEvent ev;
  667. ev.delta = Point<float>(dx, dy);
  668. ev.mod = static_cast<Modifier>(puglGetModifiers(fView));
  669. ev.time = puglGetEventTimestamp(fView);
  670. FOR_EACH_WIDGET_INV(rit)
  671. {
  672. Widget* const widget(*rit);
  673. ev.pos = Point<int>(x-widget->getAbsoluteX(), y-widget->getAbsoluteY());
  674. if (widget->isVisible() && widget->onScroll(ev))
  675. break;
  676. }
  677. }
  678. void onPuglReshape(const int width, const int height)
  679. {
  680. DBGp("PUGL: onReshape : %i %i\n", width, height);
  681. if (width <= 1 && height <= 1)
  682. return;
  683. fWidth = static_cast<uint>(width);
  684. fHeight = static_cast<uint>(height);
  685. fSelf->onReshape(fWidth, fHeight);
  686. FOR_EACH_WIDGET(it)
  687. {
  688. Widget* const widget(*it);
  689. if (widget->pData->needsFullViewport)
  690. widget->setSize(fWidth, fHeight);
  691. }
  692. }
  693. void onPuglClose()
  694. {
  695. DBG("PUGL: onClose\n");
  696. if (fModal.enabled)
  697. exec_fini();
  698. fSelf->onClose();
  699. if (fModal.childFocus != nullptr)
  700. fModal.childFocus->fSelf->onClose();
  701. close();
  702. }
  703. // -------------------------------------------------------------------
  704. Application& fApp;
  705. Window* fSelf;
  706. PuglView* fView;
  707. bool fFirstInit;
  708. bool fVisible;
  709. bool fResizable;
  710. bool fUsingEmbed;
  711. uint fWidth;
  712. uint fHeight;
  713. char* fTitle;
  714. std::list<Widget*> fWidgets;
  715. struct Modal {
  716. bool enabled;
  717. PrivateData* parent;
  718. PrivateData* childFocus;
  719. Modal()
  720. : enabled(false),
  721. parent(nullptr),
  722. childFocus(nullptr) {}
  723. Modal(PrivateData* const p)
  724. : enabled(false),
  725. parent(p),
  726. childFocus(nullptr) {}
  727. ~Modal()
  728. {
  729. DISTRHO_SAFE_ASSERT(! enabled);
  730. DISTRHO_SAFE_ASSERT(childFocus == nullptr);
  731. }
  732. DISTRHO_DECLARE_NON_COPY_STRUCT(Modal)
  733. } fModal;
  734. #if defined(DISTRHO_OS_WINDOWS)
  735. HWND hwnd;
  736. #elif defined(DISTRHO_OS_LINUX)
  737. Display* xDisplay;
  738. ::Window xWindow;
  739. #elif defined(DISTRHO_OS_MAC)
  740. bool fNeedsIdle;
  741. PuglOpenGLView* mView;
  742. id mWindow;
  743. #endif
  744. // -------------------------------------------------------------------
  745. // Callbacks
  746. #define handlePtr ((PrivateData*)puglGetHandle(view))
  747. static void onDisplayCallback(PuglView* view)
  748. {
  749. handlePtr->onPuglDisplay();
  750. }
  751. static int onKeyboardCallback(PuglView* view, bool press, uint32_t key)
  752. {
  753. return handlePtr->onPuglKeyboard(press, key);
  754. }
  755. static int onSpecialCallback(PuglView* view, bool press, PuglKey key)
  756. {
  757. return handlePtr->onPuglSpecial(press, static_cast<Key>(key));
  758. }
  759. static void onMouseCallback(PuglView* view, int button, bool press, int x, int y)
  760. {
  761. handlePtr->onPuglMouse(button, press, x, y);
  762. }
  763. static void onMotionCallback(PuglView* view, int x, int y)
  764. {
  765. handlePtr->onPuglMotion(x, y);
  766. }
  767. static void onScrollCallback(PuglView* view, int x, int y, float dx, float dy)
  768. {
  769. handlePtr->onPuglScroll(x, y, dx, dy);
  770. }
  771. static void onReshapeCallback(PuglView* view, int width, int height)
  772. {
  773. handlePtr->onPuglReshape(width, height);
  774. }
  775. static void onCloseCallback(PuglView* view)
  776. {
  777. handlePtr->onPuglClose();
  778. }
  779. #ifndef DGL_FILE_BROWSER_DISABLED
  780. static void fileBrowserSelectedCallback(PuglView* view, const char* filename)
  781. {
  782. handlePtr->fSelf->fileBrowserSelected(filename);
  783. }
  784. #endif
  785. #undef handlePtr
  786. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PrivateData)
  787. };
  788. // -----------------------------------------------------------------------
  789. // Window
  790. Window::Window(Application& app)
  791. : pData(new PrivateData(app, this)) {}
  792. Window::Window(Application& app, Window& parent)
  793. : pData(new PrivateData(app, this, parent)) {}
  794. Window::Window(Application& app, intptr_t parentId)
  795. : pData(new PrivateData(app, this, parentId)) {}
  796. Window::~Window()
  797. {
  798. delete pData;
  799. }
  800. void Window::show()
  801. {
  802. pData->setVisible(true);
  803. }
  804. void Window::hide()
  805. {
  806. pData->setVisible(false);
  807. }
  808. void Window::close()
  809. {
  810. pData->close();
  811. }
  812. void Window::exec(bool lockWait)
  813. {
  814. pData->exec(lockWait);
  815. }
  816. void Window::focus()
  817. {
  818. pData->focus();
  819. }
  820. void Window::repaint() noexcept
  821. {
  822. puglPostRedisplay(pData->fView);
  823. }
  824. // static int fib_filter_filename_filter(const char* const name)
  825. // {
  826. // return 1;
  827. // (void)name;
  828. // }
  829. #ifndef DGL_FILE_BROWSER_DISABLED
  830. bool Window::openFileBrowser(const FileBrowserOptions& options)
  831. {
  832. #ifdef SOFD_HAVE_X11
  833. using DISTRHO_NAMESPACE::String;
  834. // --------------------------------------------------------------------------
  835. // configure start dir
  836. // TODO: get abspath if needed
  837. // TODO: cross-platform
  838. String startDir(options.startDir);
  839. if (startDir.isEmpty())
  840. {
  841. if (char* const dir_name = get_current_dir_name())
  842. {
  843. startDir = dir_name;
  844. std::free(dir_name);
  845. }
  846. }
  847. DISTRHO_SAFE_ASSERT_RETURN(startDir.isNotEmpty(), false);
  848. if (! startDir.endsWith('/'))
  849. startDir += "/";
  850. DISTRHO_SAFE_ASSERT_RETURN(x_fib_configure(0, startDir) == 0, false);
  851. // --------------------------------------------------------------------------
  852. // configure title
  853. String title(options.title);
  854. if (title.isEmpty())
  855. {
  856. title = pData->getTitle();
  857. if (title.isEmpty())
  858. title = "FileBrowser";
  859. }
  860. DISTRHO_SAFE_ASSERT_RETURN(x_fib_configure(1, title) == 0, false);
  861. // --------------------------------------------------------------------------
  862. // configure filters
  863. x_fib_cfg_filter_callback(nullptr); //fib_filter_filename_filter);
  864. // --------------------------------------------------------------------------
  865. // configure buttons
  866. x_fib_cfg_buttons(3, options.buttons.listAllFiles-1);
  867. x_fib_cfg_buttons(1, options.buttons.showHidden-1);
  868. x_fib_cfg_buttons(2, options.buttons.showPlaces-1);
  869. // --------------------------------------------------------------------------
  870. // show
  871. return (x_fib_show(pData->xDisplay, pData->xWindow, /*options.width*/0, /*options.height*/0) == 0);
  872. #else
  873. // not implemented
  874. return false;
  875. #endif
  876. }
  877. #endif
  878. bool Window::isVisible() const noexcept
  879. {
  880. return pData->fVisible;
  881. }
  882. void Window::setVisible(bool yesNo)
  883. {
  884. pData->setVisible(yesNo);
  885. }
  886. bool Window::isResizable() const noexcept
  887. {
  888. return pData->fResizable;
  889. }
  890. void Window::setResizable(bool yesNo)
  891. {
  892. pData->setResizable(yesNo);
  893. }
  894. uint Window::getWidth() const noexcept
  895. {
  896. return pData->fWidth;
  897. }
  898. uint Window::getHeight() const noexcept
  899. {
  900. return pData->fHeight;
  901. }
  902. Size<uint> Window::getSize() const noexcept
  903. {
  904. return Size<uint>(pData->fWidth, pData->fHeight);
  905. }
  906. void Window::setSize(uint width, uint height)
  907. {
  908. pData->setSize(width, height);
  909. }
  910. void Window::setSize(Size<uint> size)
  911. {
  912. pData->setSize(size.getWidth(), size.getHeight());
  913. }
  914. const char* Window::getTitle() const noexcept
  915. {
  916. return pData->getTitle();
  917. }
  918. void Window::setTitle(const char* title)
  919. {
  920. pData->setTitle(title);
  921. }
  922. void Window::setTransientWinId(uintptr_t winId)
  923. {
  924. pData->setTransientWinId(winId);
  925. }
  926. Application& Window::getApp() const noexcept
  927. {
  928. return pData->fApp;
  929. }
  930. intptr_t Window::getWindowId() const noexcept
  931. {
  932. return puglGetNativeWindow(pData->fView);
  933. }
  934. void Window::_addWidget(Widget* const widget)
  935. {
  936. pData->addWidget(widget);
  937. }
  938. void Window::_removeWidget(Widget* const widget)
  939. {
  940. pData->removeWidget(widget);
  941. }
  942. void Window::_idle()
  943. {
  944. pData->idle();
  945. }
  946. // -----------------------------------------------------------------------
  947. void Window::addIdleCallback(IdleCallback* const callback)
  948. {
  949. DISTRHO_SAFE_ASSERT_RETURN(callback != nullptr,)
  950. pData->fApp.pData->idleCallbacks.push_back(callback);
  951. }
  952. void Window::removeIdleCallback(IdleCallback* const callback)
  953. {
  954. DISTRHO_SAFE_ASSERT_RETURN(callback != nullptr,)
  955. pData->fApp.pData->idleCallbacks.remove(callback);
  956. }
  957. // -----------------------------------------------------------------------
  958. void Window::onDisplayBefore()
  959. {
  960. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  961. glLoadIdentity();
  962. }
  963. void Window::onDisplayAfter()
  964. {
  965. }
  966. void Window::onReshape(uint width, uint height)
  967. {
  968. glEnable(GL_BLEND);
  969. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  970. glMatrixMode(GL_PROJECTION);
  971. glLoadIdentity();
  972. glOrtho(0.0, static_cast<GLdouble>(width), static_cast<GLdouble>(height), 0.0, 0.0, 1.0);
  973. glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
  974. glMatrixMode(GL_MODELVIEW);
  975. glLoadIdentity();
  976. }
  977. void Window::onClose()
  978. {
  979. }
  980. #ifndef DGL_FILE_BROWSER_DISABLED
  981. void Window::fileBrowserSelected(const char*)
  982. {
  983. }
  984. #endif
  985. // -----------------------------------------------------------------------
  986. StandaloneWindow::StandaloneWindow()
  987. : Application(),
  988. Window((Application&)*this),
  989. fWidget(nullptr) {}
  990. void StandaloneWindow::exec()
  991. {
  992. Window::show();
  993. Application::exec();
  994. }
  995. void StandaloneWindow::onReshape(uint width, uint height)
  996. {
  997. if (fWidget != nullptr)
  998. fWidget->setSize(width, height);
  999. Window::onReshape(width, height);
  1000. }
  1001. void StandaloneWindow::_addWidget(Widget* widget)
  1002. {
  1003. if (fWidget == nullptr)
  1004. {
  1005. fWidget = widget;
  1006. fWidget->pData->needsFullViewport = true;
  1007. }
  1008. Window::_addWidget(widget);
  1009. }
  1010. void StandaloneWindow::_removeWidget(Widget* widget)
  1011. {
  1012. if (fWidget == widget)
  1013. {
  1014. fWidget->pData->needsFullViewport = false;
  1015. fWidget = nullptr;
  1016. }
  1017. Window::_removeWidget(widget);
  1018. }
  1019. // -----------------------------------------------------------------------
  1020. END_NAMESPACE_DGL
  1021. #undef DBG
  1022. #undef DBGF