The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

989 lines
36KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. static int numAlwaysOnTopPeers = 0;
  22. bool juce_areThereAnyAlwaysOnTopWindows() { return numAlwaysOnTopPeers > 0; }
  23. //==============================================================================
  24. class LinuxComponentPeer : public ComponentPeer,
  25. private XWindowSystemUtilities::XSettings::Listener
  26. {
  27. public:
  28. LinuxComponentPeer (Component& comp, int windowStyleFlags, ::Window parentToAddTo)
  29. : ComponentPeer (comp, windowStyleFlags),
  30. isAlwaysOnTop (comp.isAlwaysOnTop())
  31. {
  32. // it's dangerous to create a window on a thread other than the message thread.
  33. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  34. const auto* instance = XWindowSystem::getInstance();
  35. if (! instance->isX11Available())
  36. return;
  37. if (isAlwaysOnTop)
  38. ++numAlwaysOnTopPeers;
  39. repainter = std::make_unique<LinuxRepaintManager> (*this);
  40. windowH = instance->createWindow (parentToAddTo, this);
  41. parentWindow = parentToAddTo;
  42. setTitle (component.getName());
  43. if (auto* xSettings = instance->getXSettings())
  44. xSettings->addListener (this);
  45. getNativeRealtimeModifiers = []() -> ModifierKeys { return XWindowSystem::getInstance()->getNativeRealtimeModifiers(); };
  46. }
  47. ~LinuxComponentPeer() override
  48. {
  49. // it's dangerous to delete a window on a thread other than the message thread.
  50. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  51. auto* instance = XWindowSystem::getInstance();
  52. repainter = nullptr;
  53. instance->destroyWindow (windowH);
  54. if (auto* xSettings = instance->getXSettings())
  55. xSettings->removeListener (this);
  56. if (isAlwaysOnTop)
  57. --numAlwaysOnTopPeers;
  58. }
  59. ::Window getWindowHandle() const noexcept
  60. {
  61. return windowH;
  62. }
  63. //==============================================================================
  64. void* getNativeHandle() const override
  65. {
  66. return reinterpret_cast<void*> (getWindowHandle());
  67. }
  68. //==============================================================================
  69. void forceSetBounds (const Rectangle<int>& correctedNewBounds, bool isNowFullScreen)
  70. {
  71. bounds = correctedNewBounds;
  72. updateScaleFactorFromNewBounds (bounds, false);
  73. auto physicalBounds = parentWindow == 0 ? Desktop::getInstance().getDisplays().logicalToPhysical (bounds)
  74. : bounds * currentScaleFactor;
  75. WeakReference<Component> deletionChecker (&component);
  76. XWindowSystem::getInstance()->setBounds (windowH, physicalBounds, isNowFullScreen);
  77. fullScreen = isNowFullScreen;
  78. if (deletionChecker != nullptr)
  79. {
  80. updateBorderSize();
  81. handleMovedOrResized();
  82. }
  83. }
  84. void setBounds (const Rectangle<int>& newBounds, bool isNowFullScreen) override
  85. {
  86. const auto correctedNewBounds = newBounds.withSize (jmax (1, newBounds.getWidth()),
  87. jmax (1, newBounds.getHeight()));
  88. if (bounds != correctedNewBounds || fullScreen != isNowFullScreen)
  89. forceSetBounds (correctedNewBounds, isNowFullScreen);
  90. }
  91. Point<int> getScreenPosition (bool physical) const
  92. {
  93. auto physicalParentPosition = XWindowSystem::getInstance()->getPhysicalParentScreenPosition();
  94. auto parentPosition = parentWindow == 0 ? Desktop::getInstance().getDisplays().physicalToLogical (physicalParentPosition)
  95. : physicalParentPosition / currentScaleFactor;
  96. auto screenBounds = parentWindow == 0 ? bounds
  97. : bounds.translated (parentPosition.x, parentPosition.y);
  98. if (physical)
  99. return parentWindow == 0 ? Desktop::getInstance().getDisplays().logicalToPhysical (screenBounds.getTopLeft())
  100. : screenBounds.getTopLeft() * currentScaleFactor;
  101. return screenBounds.getTopLeft();
  102. }
  103. Rectangle<int> getBounds() const override
  104. {
  105. return bounds;
  106. }
  107. OptionalBorderSize getFrameSizeIfPresent() const override
  108. {
  109. return windowBorder;
  110. }
  111. BorderSize<int> getFrameSize() const override
  112. {
  113. const auto optionalBorderSize = getFrameSizeIfPresent();
  114. return optionalBorderSize ? (*optionalBorderSize) : BorderSize<int>();
  115. }
  116. Point<float> localToGlobal (Point<float> relativePosition) override
  117. {
  118. return localToGlobal (*this, relativePosition);
  119. }
  120. Point<float> globalToLocal (Point<float> screenPosition) override
  121. {
  122. return globalToLocal (*this, screenPosition);
  123. }
  124. using ComponentPeer::localToGlobal;
  125. using ComponentPeer::globalToLocal;
  126. //==============================================================================
  127. StringArray getAvailableRenderingEngines() override
  128. {
  129. return { "Software Renderer" };
  130. }
  131. void setVisible (bool shouldBeVisible) override
  132. {
  133. XWindowSystem::getInstance()->setVisible (windowH, shouldBeVisible);
  134. }
  135. void setTitle (const String& title) override
  136. {
  137. XWindowSystem::getInstance()->setTitle (windowH, title);
  138. }
  139. void setMinimised (bool shouldBeMinimised) override
  140. {
  141. if (shouldBeMinimised)
  142. XWindowSystem::getInstance()->setMinimised (windowH, shouldBeMinimised);
  143. else
  144. setVisible (true);
  145. }
  146. bool isMinimised() const override
  147. {
  148. return XWindowSystem::getInstance()->isMinimised (windowH);
  149. }
  150. void setFullScreen (bool shouldBeFullScreen) override
  151. {
  152. auto r = lastNonFullscreenBounds; // (get a copy of this before de-minimising)
  153. setMinimised (false);
  154. if (fullScreen != shouldBeFullScreen)
  155. {
  156. const auto usingNativeTitleBar = ((styleFlags & windowHasTitleBar) != 0);
  157. if (usingNativeTitleBar)
  158. XWindowSystem::getInstance()->setMaximised (windowH, shouldBeFullScreen);
  159. if (shouldBeFullScreen)
  160. r = usingNativeTitleBar ? XWindowSystem::getInstance()->getWindowBounds (windowH, parentWindow)
  161. : Desktop::getInstance().getDisplays().getDisplayForRect (bounds)->userArea;
  162. if (! r.isEmpty())
  163. setBounds (ScalingHelpers::scaledScreenPosToUnscaled (component, r), shouldBeFullScreen);
  164. component.repaint();
  165. }
  166. }
  167. bool isFullScreen() const override
  168. {
  169. return fullScreen;
  170. }
  171. bool contains (Point<int> localPos, bool trueIfInAChildWindow) const override
  172. {
  173. if (! bounds.withZeroOrigin().contains (localPos))
  174. return false;
  175. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  176. {
  177. auto* c = Desktop::getInstance().getComponent (i);
  178. if (c == &component)
  179. break;
  180. if (! c->isVisible())
  181. continue;
  182. auto* otherPeer = c->getPeer();
  183. jassert (otherPeer == nullptr || dynamic_cast<LinuxComponentPeer*> (c->getPeer()) != nullptr);
  184. if (auto* peer = static_cast<LinuxComponentPeer*> (otherPeer))
  185. if (peer->contains (globalToLocal (*peer, localToGlobal (*this, localPos.toFloat())).roundToInt(), true))
  186. return false;
  187. }
  188. if (trueIfInAChildWindow)
  189. return true;
  190. return XWindowSystem::getInstance()->contains (windowH, localPos * currentScaleFactor);
  191. }
  192. void toFront (bool makeActive) override
  193. {
  194. if (makeActive)
  195. {
  196. setVisible (true);
  197. grabFocus();
  198. }
  199. XWindowSystem::getInstance()->toFront (windowH, makeActive);
  200. handleBroughtToFront();
  201. }
  202. void toBehind (ComponentPeer* other) override
  203. {
  204. if (auto* otherPeer = dynamic_cast<LinuxComponentPeer*> (other))
  205. {
  206. if (otherPeer->styleFlags & windowIsTemporary)
  207. return;
  208. setMinimised (false);
  209. XWindowSystem::getInstance()->toBehind (windowH, otherPeer->windowH);
  210. }
  211. else
  212. {
  213. jassertfalse; // wrong type of window?
  214. }
  215. }
  216. bool isFocused() const override
  217. {
  218. return XWindowSystem::getInstance()->isFocused (windowH);
  219. }
  220. void grabFocus() override
  221. {
  222. if (XWindowSystem::getInstance()->grabFocus (windowH))
  223. isActiveApplication = true;
  224. }
  225. //==============================================================================
  226. void repaint (const Rectangle<int>& area) override
  227. {
  228. if (repainter != nullptr)
  229. repainter->repaint (area.getIntersection (bounds.withZeroOrigin()));
  230. }
  231. void performAnyPendingRepaintsNow() override
  232. {
  233. if (repainter != nullptr)
  234. repainter->performAnyPendingRepaintsNow();
  235. }
  236. void setIcon (const Image& newIcon) override
  237. {
  238. XWindowSystem::getInstance()->setIcon (windowH, newIcon);
  239. }
  240. double getPlatformScaleFactor() const noexcept override
  241. {
  242. return currentScaleFactor;
  243. }
  244. void setAlpha (float) override {}
  245. bool setAlwaysOnTop (bool) override { return false; }
  246. void textInputRequired (Point<int>, TextInputTarget&) override {}
  247. //==============================================================================
  248. void addOpenGLRepaintListener (Component* dummy)
  249. {
  250. if (dummy != nullptr)
  251. glRepaintListeners.addIfNotAlreadyThere (dummy);
  252. }
  253. void removeOpenGLRepaintListener (Component* dummy)
  254. {
  255. if (dummy != nullptr)
  256. glRepaintListeners.removeAllInstancesOf (dummy);
  257. }
  258. void repaintOpenGLContexts()
  259. {
  260. for (auto* c : glRepaintListeners)
  261. c->handleCommandMessage (0);
  262. }
  263. //==============================================================================
  264. ::Window getParentWindow() { return parentWindow; }
  265. void setParentWindow (::Window newParent) { parentWindow = newParent; }
  266. //==============================================================================
  267. bool isConstrainedNativeWindow() const
  268. {
  269. return constrainer != nullptr
  270. && (styleFlags & (windowHasTitleBar | windowIsResizable)) == (windowHasTitleBar | windowIsResizable)
  271. && ! isKioskMode();
  272. }
  273. void updateWindowBounds()
  274. {
  275. if (windowH == 0)
  276. {
  277. jassertfalse;
  278. return;
  279. }
  280. if (isConstrainedNativeWindow())
  281. XWindowSystem::getInstance()->updateConstraints (windowH);
  282. auto physicalBounds = XWindowSystem::getInstance()->getWindowBounds (windowH, parentWindow);
  283. updateScaleFactorFromNewBounds (physicalBounds, true);
  284. bounds = parentWindow == 0 ? Desktop::getInstance().getDisplays().physicalToLogical (physicalBounds)
  285. : physicalBounds / currentScaleFactor;
  286. updateVBlankTimer();
  287. }
  288. void updateBorderSize()
  289. {
  290. if ((styleFlags & windowHasTitleBar) == 0)
  291. {
  292. windowBorder = ComponentPeer::OptionalBorderSize { BorderSize<int>() };
  293. }
  294. else if (! windowBorder
  295. || ((*windowBorder).getTopAndBottom() == 0 && (*windowBorder).getLeftAndRight() == 0))
  296. {
  297. windowBorder = [&]()
  298. {
  299. if (auto unscaledBorderSize = XWindowSystem::getInstance()->getBorderSize (windowH))
  300. return OptionalBorderSize { (*unscaledBorderSize).multipliedBy (1.0 / currentScaleFactor) };
  301. return OptionalBorderSize {};
  302. }();
  303. }
  304. }
  305. //==============================================================================
  306. static bool isActiveApplication;
  307. bool focused = false;
  308. private:
  309. //==============================================================================
  310. class LinuxRepaintManager
  311. {
  312. public:
  313. LinuxRepaintManager (LinuxComponentPeer& p)
  314. : peer (p),
  315. isSemiTransparentWindow ((peer.getStyleFlags() & ComponentPeer::windowIsSemiTransparent) != 0)
  316. {
  317. }
  318. void dispatchDeferredRepaints()
  319. {
  320. XWindowSystem::getInstance()->processPendingPaintsForWindow (peer.windowH);
  321. if (XWindowSystem::getInstance()->getNumPaintsPendingForWindow (peer.windowH) > 0)
  322. return;
  323. if (! regionsNeedingRepaint.isEmpty())
  324. performAnyPendingRepaintsNow();
  325. else if (Time::getApproximateMillisecondCounter() > lastTimeImageUsed + 3000)
  326. image = Image();
  327. }
  328. void repaint (Rectangle<int> area)
  329. {
  330. regionsNeedingRepaint.add (area * peer.currentScaleFactor);
  331. }
  332. void performAnyPendingRepaintsNow()
  333. {
  334. if (XWindowSystem::getInstance()->getNumPaintsPendingForWindow (peer.windowH) > 0)
  335. return;
  336. auto originalRepaintRegion = regionsNeedingRepaint;
  337. regionsNeedingRepaint.clear();
  338. auto totalArea = originalRepaintRegion.getBounds();
  339. if (! totalArea.isEmpty())
  340. {
  341. const auto wasImageNull = image.isNull();
  342. if (wasImageNull || image.getWidth() < totalArea.getWidth()
  343. || image.getHeight() < totalArea.getHeight())
  344. {
  345. image = XWindowSystem::getInstance()->createImage (isSemiTransparentWindow,
  346. totalArea.getWidth(), totalArea.getHeight(),
  347. useARGBImagesForRendering);
  348. if (wasImageNull)
  349. {
  350. // After calling createImage() XWindowSystem::getWindowBounds() will return
  351. // changed coordinates that look like the result of some position
  352. // defaulting mechanism. If we handle a configureNotifyEvent after
  353. // createImage() and before we would issue new, valid coordinates, we will
  354. // apply these default, unwanted coordinates to our window. To avoid that
  355. // we immediately send another positioning message to guarantee that the
  356. // next configureNotifyEvent will read valid values.
  357. //
  358. // This issue only occurs right after peer creation, when the image is
  359. // null. Updating when only the width or height is changed would lead to
  360. // incorrect behaviour.
  361. peer.forceSetBounds (ScalingHelpers::scaledScreenPosToUnscaled (peer.component,
  362. peer.component.getBoundsInParent()),
  363. peer.isFullScreen());
  364. }
  365. }
  366. RectangleList<int> adjustedList (originalRepaintRegion);
  367. adjustedList.offsetAll (-totalArea.getX(), -totalArea.getY());
  368. if (XWindowSystem::getInstance()->canUseARGBImages())
  369. for (auto& i : originalRepaintRegion)
  370. image.clear (i - totalArea.getPosition());
  371. {
  372. auto context = peer.getComponent().getLookAndFeel()
  373. .createGraphicsContext (image, -totalArea.getPosition(), adjustedList);
  374. context->addTransform (AffineTransform::scale ((float) peer.currentScaleFactor));
  375. peer.handlePaint (*context);
  376. }
  377. for (auto& i : originalRepaintRegion)
  378. XWindowSystem::getInstance()->blitToWindow (peer.windowH, image, i, totalArea);
  379. }
  380. lastTimeImageUsed = Time::getApproximateMillisecondCounter();
  381. }
  382. private:
  383. LinuxComponentPeer& peer;
  384. const bool isSemiTransparentWindow;
  385. Image image;
  386. uint32 lastTimeImageUsed = 0;
  387. RectangleList<int> regionsNeedingRepaint;
  388. bool useARGBImagesForRendering = XWindowSystem::getInstance()->canUseARGBImages();
  389. JUCE_DECLARE_NON_COPYABLE (LinuxRepaintManager)
  390. };
  391. class LinuxVBlankManager : public Timer
  392. {
  393. public:
  394. explicit LinuxVBlankManager (std::function<void()> cb) : callback (std::move (cb))
  395. {
  396. jassert (callback);
  397. }
  398. ~LinuxVBlankManager() override { stopTimer(); }
  399. //==============================================================================
  400. void timerCallback() override { callback(); }
  401. private:
  402. std::function<void()> callback;
  403. JUCE_DECLARE_NON_COPYABLE (LinuxVBlankManager)
  404. JUCE_DECLARE_NON_MOVEABLE (LinuxVBlankManager)
  405. };
  406. //==============================================================================
  407. template <typename This>
  408. static Point<float> localToGlobal (This& t, Point<float> relativePosition)
  409. {
  410. return relativePosition + t.getScreenPosition (false).toFloat();
  411. }
  412. template <typename This>
  413. static Point<float> globalToLocal (This& t, Point<float> screenPosition)
  414. {
  415. return screenPosition - t.getScreenPosition (false).toFloat();
  416. }
  417. //==============================================================================
  418. void settingChanged (const XWindowSystemUtilities::XSetting& settingThatHasChanged) override
  419. {
  420. static StringArray possibleSettings { XWindowSystem::getWindowScalingFactorSettingName(),
  421. "Gdk/UnscaledDPI",
  422. "Xft/DPI" };
  423. if (possibleSettings.contains (settingThatHasChanged.name))
  424. forceDisplayUpdate();
  425. }
  426. void updateScaleFactorFromNewBounds (const Rectangle<int>& newBounds, bool isPhysical)
  427. {
  428. Point<int> translation = (parentWindow != 0 ? getScreenPosition (isPhysical) : Point<int>());
  429. const auto& desktop = Desktop::getInstance();
  430. if (auto* display = desktop.getDisplays().getDisplayForRect (newBounds.translated (translation.x, translation.y),
  431. isPhysical))
  432. {
  433. auto newScaleFactor = display->scale / desktop.getGlobalScaleFactor();
  434. if (! approximatelyEqual (newScaleFactor, currentScaleFactor))
  435. {
  436. currentScaleFactor = newScaleFactor;
  437. scaleFactorListeners.call ([&] (ScaleFactorListener& l) { l.nativeScaleFactorChanged (currentScaleFactor); });
  438. }
  439. }
  440. }
  441. void onVBlank()
  442. {
  443. vBlankListeners.call ([] (auto& l) { l.onVBlank(); });
  444. if (repainter != nullptr)
  445. repainter->dispatchDeferredRepaints();
  446. }
  447. void updateVBlankTimer()
  448. {
  449. if (auto* display = Desktop::getInstance().getDisplays().getDisplayForRect (bounds))
  450. {
  451. if (display->verticalFrequencyHz)
  452. {
  453. const auto newIntFrequencyHz = roundToInt (*display->verticalFrequencyHz);
  454. if (vBlankManager.getTimerInterval() != newIntFrequencyHz)
  455. vBlankManager.startTimerHz (newIntFrequencyHz);
  456. }
  457. }
  458. }
  459. //==============================================================================
  460. std::unique_ptr<LinuxRepaintManager> repainter;
  461. LinuxVBlankManager vBlankManager { [this]() { onVBlank(); } };
  462. ::Window windowH = {}, parentWindow = {};
  463. Rectangle<int> bounds;
  464. ComponentPeer::OptionalBorderSize windowBorder;
  465. bool fullScreen = false, isAlwaysOnTop = false;
  466. double currentScaleFactor = 1.0;
  467. Array<Component*> glRepaintListeners;
  468. //==============================================================================
  469. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LinuxComponentPeer)
  470. };
  471. bool LinuxComponentPeer::isActiveApplication = false;
  472. //==============================================================================
  473. ComponentPeer* Component::createNewPeer (int styleFlags, void* nativeWindowToAttachTo)
  474. {
  475. return new LinuxComponentPeer (*this, styleFlags, (::Window) nativeWindowToAttachTo);
  476. }
  477. //==============================================================================
  478. JUCE_API bool JUCE_CALLTYPE Process::isForegroundProcess() { return LinuxComponentPeer::isActiveApplication; }
  479. JUCE_API void JUCE_CALLTYPE Process::makeForegroundProcess() {}
  480. JUCE_API void JUCE_CALLTYPE Process::hide() {}
  481. //==============================================================================
  482. void Desktop::setKioskComponent (Component* comp, bool enableOrDisable, bool)
  483. {
  484. if (enableOrDisable)
  485. comp->setBounds (getDisplays().getDisplayForRect (comp->getScreenBounds())->totalArea);
  486. }
  487. void Displays::findDisplays (float masterScale)
  488. {
  489. if (XWindowSystem::getInstance()->getDisplay() != nullptr)
  490. {
  491. displays = XWindowSystem::getInstance()->findDisplays (masterScale);
  492. if (! displays.isEmpty())
  493. updateToLogical();
  494. }
  495. }
  496. bool Desktop::canUseSemiTransparentWindows() noexcept
  497. {
  498. return XWindowSystem::getInstance()->canUseSemiTransparentWindows();
  499. }
  500. class Desktop::NativeDarkModeChangeDetectorImpl : private XWindowSystemUtilities::XSettings::Listener
  501. {
  502. public:
  503. NativeDarkModeChangeDetectorImpl()
  504. {
  505. const auto* windowSystem = XWindowSystem::getInstance();
  506. if (auto* xSettings = windowSystem->getXSettings())
  507. xSettings->addListener (this);
  508. darkModeEnabled = windowSystem->isDarkModeActive();
  509. }
  510. ~NativeDarkModeChangeDetectorImpl() override
  511. {
  512. if (auto* windowSystem = XWindowSystem::getInstanceWithoutCreating())
  513. if (auto* xSettings = windowSystem->getXSettings())
  514. xSettings->removeListener (this);
  515. }
  516. bool isDarkModeEnabled() const noexcept { return darkModeEnabled; }
  517. private:
  518. void settingChanged (const XWindowSystemUtilities::XSetting& settingThatHasChanged) override
  519. {
  520. if (settingThatHasChanged.name == XWindowSystem::getThemeNameSettingName())
  521. {
  522. const auto wasDarkModeEnabled = std::exchange (darkModeEnabled, XWindowSystem::getInstance()->isDarkModeActive());
  523. if (darkModeEnabled != wasDarkModeEnabled)
  524. Desktop::getInstance().darkModeChanged();
  525. }
  526. }
  527. bool darkModeEnabled = false;
  528. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeDarkModeChangeDetectorImpl)
  529. };
  530. std::unique_ptr<Desktop::NativeDarkModeChangeDetectorImpl> Desktop::createNativeDarkModeChangeDetectorImpl()
  531. {
  532. return std::make_unique<NativeDarkModeChangeDetectorImpl>();
  533. }
  534. bool Desktop::isDarkModeActive() const
  535. {
  536. return nativeDarkModeChangeDetectorImpl->isDarkModeEnabled();
  537. }
  538. static bool screenSaverAllowed = true;
  539. void Desktop::setScreenSaverEnabled (bool isEnabled)
  540. {
  541. if (screenSaverAllowed != isEnabled)
  542. {
  543. screenSaverAllowed = isEnabled;
  544. XWindowSystem::getInstance()->setScreenSaverEnabled (screenSaverAllowed);
  545. }
  546. }
  547. bool Desktop::isScreenSaverEnabled()
  548. {
  549. return screenSaverAllowed;
  550. }
  551. double Desktop::getDefaultMasterScale() { return 1.0; }
  552. Desktop::DisplayOrientation Desktop::getCurrentOrientation() const { return upright; }
  553. void Desktop::allowedOrientationsChanged() {}
  554. //==============================================================================
  555. bool MouseInputSource::SourceList::addSource()
  556. {
  557. if (sources.isEmpty())
  558. {
  559. addSource (0, MouseInputSource::InputSourceType::mouse);
  560. return true;
  561. }
  562. return false;
  563. }
  564. bool MouseInputSource::SourceList::canUseTouch()
  565. {
  566. return false;
  567. }
  568. Point<float> MouseInputSource::getCurrentRawMousePosition()
  569. {
  570. return Desktop::getInstance().getDisplays().physicalToLogical (XWindowSystem::getInstance()->getCurrentMousePosition());
  571. }
  572. void MouseInputSource::setRawMousePosition (Point<float> newPosition)
  573. {
  574. XWindowSystem::getInstance()->setMousePosition (Desktop::getInstance().getDisplays().logicalToPhysical (newPosition));
  575. }
  576. //==============================================================================
  577. class MouseCursor::PlatformSpecificHandle
  578. {
  579. public:
  580. explicit PlatformSpecificHandle (const MouseCursor::StandardCursorType type)
  581. : cursorHandle (makeHandle (type)) {}
  582. explicit PlatformSpecificHandle (const CustomMouseCursorInfo& info)
  583. : cursorHandle (makeHandle (info)) {}
  584. ~PlatformSpecificHandle()
  585. {
  586. if (cursorHandle != Cursor{})
  587. XWindowSystem::getInstance()->deleteMouseCursor (cursorHandle);
  588. }
  589. static void showInWindow (PlatformSpecificHandle* handle, ComponentPeer* peer)
  590. {
  591. const auto cursor = handle != nullptr ? handle->cursorHandle : Cursor{};
  592. if (peer != nullptr)
  593. XWindowSystem::getInstance()->showCursor ((::Window) peer->getNativeHandle(), cursor);
  594. }
  595. private:
  596. static Cursor makeHandle (const CustomMouseCursorInfo& info)
  597. {
  598. const auto image = info.image.getImage();
  599. return XWindowSystem::getInstance()->createCustomMouseCursorInfo (image.rescaled ((int) (image.getWidth() / info.image.getScale()),
  600. (int) (image.getHeight() / info.image.getScale())), info.hotspot);
  601. }
  602. static Cursor makeHandle (MouseCursor::StandardCursorType type)
  603. {
  604. return XWindowSystem::getInstance()->createStandardMouseCursor (type);
  605. }
  606. Cursor cursorHandle;
  607. //==============================================================================
  608. JUCE_DECLARE_NON_COPYABLE (PlatformSpecificHandle)
  609. JUCE_DECLARE_NON_MOVEABLE (PlatformSpecificHandle)
  610. };
  611. //==============================================================================
  612. static LinuxComponentPeer* getPeerForDragEvent (Component* sourceComp)
  613. {
  614. if (sourceComp == nullptr)
  615. if (auto* draggingSource = Desktop::getInstance().getDraggingMouseSource (0))
  616. sourceComp = draggingSource->getComponentUnderMouse();
  617. if (sourceComp != nullptr)
  618. if (auto* lp = dynamic_cast<LinuxComponentPeer*> (sourceComp->getPeer()))
  619. return lp;
  620. jassertfalse; // This method must be called in response to a component's mouseDown or mouseDrag event!
  621. return nullptr;
  622. }
  623. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, bool canMoveFiles,
  624. Component* sourceComp, std::function<void()> callback)
  625. {
  626. if (files.isEmpty())
  627. return false;
  628. if (auto* peer = getPeerForDragEvent (sourceComp))
  629. return XWindowSystem::getInstance()->externalDragFileInit (peer, files, canMoveFiles, std::move (callback));
  630. // This method must be called in response to a component's mouseDown or mouseDrag event!
  631. jassertfalse;
  632. return false;
  633. }
  634. bool DragAndDropContainer::performExternalDragDropOfText (const String& text, Component* sourceComp,
  635. std::function<void()> callback)
  636. {
  637. if (text.isEmpty())
  638. return false;
  639. if (auto* peer = getPeerForDragEvent (sourceComp))
  640. return XWindowSystem::getInstance()->externalDragTextInit (peer, text, std::move (callback));
  641. // This method must be called in response to a component's mouseDown or mouseDrag event!
  642. jassertfalse;
  643. return false;
  644. }
  645. //==============================================================================
  646. void SystemClipboard::copyTextToClipboard (const String& clipText)
  647. {
  648. XWindowSystem::getInstance()->copyTextToClipboard (clipText);
  649. }
  650. String SystemClipboard::getTextFromClipboard()
  651. {
  652. return XWindowSystem::getInstance()->getTextFromClipboard();
  653. }
  654. //==============================================================================
  655. bool KeyPress::isKeyCurrentlyDown (int keyCode)
  656. {
  657. return XWindowSystem::getInstance()->isKeyCurrentlyDown (keyCode);
  658. }
  659. void LookAndFeel::playAlertSound()
  660. {
  661. std::cout << "\a" << std::flush;
  662. }
  663. //==============================================================================
  664. static int showDialog (const MessageBoxOptions& options,
  665. ModalComponentManager::Callback* callback,
  666. Async async)
  667. {
  668. const auto dummyCallback = [] (int) {};
  669. switch (options.getNumButtons())
  670. {
  671. case 2:
  672. {
  673. if (async == Async::yes && callback == nullptr)
  674. callback = ModalCallbackFunction::create (dummyCallback);
  675. return AlertWindow::showOkCancelBox (options.getIconType(),
  676. options.getTitle(),
  677. options.getMessage(),
  678. options.getButtonText (0),
  679. options.getButtonText (1),
  680. options.getAssociatedComponent(),
  681. callback) ? 1 : 0;
  682. }
  683. case 3:
  684. {
  685. if (async == Async::yes && callback == nullptr)
  686. callback = ModalCallbackFunction::create (dummyCallback);
  687. return AlertWindow::showYesNoCancelBox (options.getIconType(),
  688. options.getTitle(),
  689. options.getMessage(),
  690. options.getButtonText (0),
  691. options.getButtonText (1),
  692. options.getButtonText (2),
  693. options.getAssociatedComponent(),
  694. callback);
  695. }
  696. case 1:
  697. default:
  698. break;
  699. }
  700. #if JUCE_MODAL_LOOPS_PERMITTED
  701. if (async == Async::no)
  702. {
  703. AlertWindow::showMessageBox (options.getIconType(),
  704. options.getTitle(),
  705. options.getMessage(),
  706. options.getButtonText (0),
  707. options.getAssociatedComponent());
  708. }
  709. else
  710. #endif
  711. {
  712. AlertWindow::showMessageBoxAsync (options.getIconType(),
  713. options.getTitle(),
  714. options.getMessage(),
  715. options.getButtonText (0),
  716. options.getAssociatedComponent(),
  717. callback);
  718. }
  719. return 0;
  720. }
  721. #if JUCE_MODAL_LOOPS_PERMITTED
  722. void JUCE_CALLTYPE NativeMessageBox::showMessageBox (MessageBoxIconType iconType,
  723. const String& title, const String& message,
  724. Component* /*associatedComponent*/)
  725. {
  726. AlertWindow::showMessageBox (iconType, title, message);
  727. }
  728. int JUCE_CALLTYPE NativeMessageBox::show (const MessageBoxOptions& options)
  729. {
  730. return showDialog (options, nullptr, Async::no);
  731. }
  732. #endif
  733. void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (MessageBoxIconType iconType,
  734. const String& title, const String& message,
  735. Component* associatedComponent,
  736. ModalComponentManager::Callback* callback)
  737. {
  738. AlertWindow::showMessageBoxAsync (iconType, title, message, {}, associatedComponent, callback);
  739. }
  740. bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (MessageBoxIconType iconType,
  741. const String& title, const String& message,
  742. Component* associatedComponent,
  743. ModalComponentManager::Callback* callback)
  744. {
  745. return AlertWindow::showOkCancelBox (iconType, title, message, {}, {}, associatedComponent, callback);
  746. }
  747. int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (MessageBoxIconType iconType,
  748. const String& title, const String& message,
  749. Component* associatedComponent,
  750. ModalComponentManager::Callback* callback)
  751. {
  752. return AlertWindow::showYesNoCancelBox (iconType, title, message, {}, {}, {},
  753. associatedComponent, callback);
  754. }
  755. int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (MessageBoxIconType iconType,
  756. const String& title, const String& message,
  757. Component* associatedComponent,
  758. ModalComponentManager::Callback* callback)
  759. {
  760. return AlertWindow::showOkCancelBox (iconType, title, message, TRANS("Yes"), TRANS("No"),
  761. associatedComponent, callback);
  762. }
  763. void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,
  764. ModalComponentManager::Callback* callback)
  765. {
  766. showDialog (options, callback, Async::yes);
  767. }
  768. void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,
  769. std::function<void (int)> callback)
  770. {
  771. showAsync (options, ModalCallbackFunction::create (callback));
  772. }
  773. //==============================================================================
  774. Image juce_createIconForFile (const File&)
  775. {
  776. return {};
  777. }
  778. void juce_LinuxAddRepaintListener (ComponentPeer* peer, Component* dummy);
  779. void juce_LinuxAddRepaintListener (ComponentPeer* peer, Component* dummy)
  780. {
  781. if (auto* linuxPeer = dynamic_cast<LinuxComponentPeer*> (peer))
  782. linuxPeer->addOpenGLRepaintListener (dummy);
  783. }
  784. void juce_LinuxRemoveRepaintListener (ComponentPeer* peer, Component* dummy);
  785. void juce_LinuxRemoveRepaintListener (ComponentPeer* peer, Component* dummy)
  786. {
  787. if (auto* linuxPeer = dynamic_cast<LinuxComponentPeer*> (peer))
  788. linuxPeer->removeOpenGLRepaintListener (dummy);
  789. }
  790. } // namespace juce