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.

875 lines
31KB

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