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.

871 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 : 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. //==============================================================================
  311. static bool isActiveApplication;
  312. bool focused = false;
  313. private:
  314. //==============================================================================
  315. class LinuxRepaintManager
  316. {
  317. public:
  318. LinuxRepaintManager (LinuxComponentPeer& p)
  319. : peer (p),
  320. isSemiTransparentWindow ((peer.getStyleFlags() & ComponentPeer::windowIsSemiTransparent) != 0)
  321. {
  322. }
  323. void dispatchDeferredRepaints()
  324. {
  325. XWindowSystem::getInstance()->processPendingPaintsForWindow (peer.windowH);
  326. if (XWindowSystem::getInstance()->getNumPaintsPendingForWindow (peer.windowH) > 0)
  327. return;
  328. if (! regionsNeedingRepaint.isEmpty())
  329. performAnyPendingRepaintsNow();
  330. else if (Time::getApproximateMillisecondCounter() > lastTimeImageUsed + 3000)
  331. image = Image();
  332. }
  333. void repaint (Rectangle<int> area)
  334. {
  335. regionsNeedingRepaint.add (area * peer.currentScaleFactor);
  336. }
  337. void performAnyPendingRepaintsNow()
  338. {
  339. if (XWindowSystem::getInstance()->getNumPaintsPendingForWindow (peer.windowH) > 0)
  340. return;
  341. auto originalRepaintRegion = regionsNeedingRepaint;
  342. regionsNeedingRepaint.clear();
  343. auto totalArea = originalRepaintRegion.getBounds();
  344. if (! totalArea.isEmpty())
  345. {
  346. const auto wasImageNull = image.isNull();
  347. if (wasImageNull || image.getWidth() < totalArea.getWidth()
  348. || image.getHeight() < totalArea.getHeight())
  349. {
  350. image = XWindowSystem::getInstance()->createImage (isSemiTransparentWindow,
  351. totalArea.getWidth(), totalArea.getHeight(),
  352. useARGBImagesForRendering);
  353. if (wasImageNull)
  354. {
  355. // After calling createImage() XWindowSystem::getWindowBounds() will return
  356. // changed coordinates that look like the result of some position
  357. // defaulting mechanism. If we handle a configureNotifyEvent after
  358. // createImage() and before we would issue new, valid coordinates, we will
  359. // apply these default, unwanted coordinates to our window. To avoid that
  360. // we immediately send another positioning message to guarantee that the
  361. // next configureNotifyEvent will read valid values.
  362. //
  363. // This issue only occurs right after peer creation, when the image is
  364. // null. Updating when only the width or height is changed would lead to
  365. // incorrect behaviour.
  366. peer.forceSetBounds (detail::ScalingHelpers::scaledScreenPosToUnscaled (peer.component,
  367. peer.component.getBoundsInParent()),
  368. peer.isFullScreen());
  369. }
  370. }
  371. RectangleList<int> adjustedList (originalRepaintRegion);
  372. adjustedList.offsetAll (-totalArea.getX(), -totalArea.getY());
  373. if (XWindowSystem::getInstance()->canUseARGBImages())
  374. for (auto& i : originalRepaintRegion)
  375. image.clear (i - totalArea.getPosition());
  376. {
  377. auto context = peer.getComponent().getLookAndFeel()
  378. .createGraphicsContext (image, -totalArea.getPosition(), adjustedList);
  379. context->addTransform (AffineTransform::scale ((float) peer.currentScaleFactor));
  380. peer.handlePaint (*context);
  381. }
  382. for (auto& i : originalRepaintRegion)
  383. XWindowSystem::getInstance()->blitToWindow (peer.windowH, image, i, totalArea);
  384. }
  385. lastTimeImageUsed = Time::getApproximateMillisecondCounter();
  386. }
  387. private:
  388. LinuxComponentPeer& peer;
  389. const bool isSemiTransparentWindow;
  390. Image image;
  391. uint32 lastTimeImageUsed = 0;
  392. RectangleList<int> regionsNeedingRepaint;
  393. bool useARGBImagesForRendering = XWindowSystem::getInstance()->canUseARGBImages();
  394. JUCE_DECLARE_NON_COPYABLE (LinuxRepaintManager)
  395. };
  396. class LinuxVBlankManager : public Timer
  397. {
  398. public:
  399. explicit LinuxVBlankManager (std::function<void()> cb) : callback (std::move (cb))
  400. {
  401. jassert (callback);
  402. }
  403. ~LinuxVBlankManager() override { stopTimer(); }
  404. //==============================================================================
  405. void timerCallback() override { callback(); }
  406. private:
  407. std::function<void()> callback;
  408. JUCE_DECLARE_NON_COPYABLE (LinuxVBlankManager)
  409. JUCE_DECLARE_NON_MOVEABLE (LinuxVBlankManager)
  410. };
  411. //==============================================================================
  412. template <typename This>
  413. static Point<float> localToGlobal (This& t, Point<float> relativePosition)
  414. {
  415. return relativePosition + t.getScreenPosition (false).toFloat();
  416. }
  417. template <typename This>
  418. static Point<float> globalToLocal (This& t, Point<float> screenPosition)
  419. {
  420. return screenPosition - t.getScreenPosition (false).toFloat();
  421. }
  422. //==============================================================================
  423. void settingChanged (const XWindowSystemUtilities::XSetting& settingThatHasChanged) override
  424. {
  425. static StringArray possibleSettings { XWindowSystem::getWindowScalingFactorSettingName(),
  426. "Gdk/UnscaledDPI",
  427. "Xft/DPI" };
  428. if (possibleSettings.contains (settingThatHasChanged.name))
  429. forceDisplayUpdate();
  430. }
  431. void updateScaleFactorFromNewBounds (const Rectangle<int>& newBounds, bool isPhysical)
  432. {
  433. Point<int> translation = (parentWindow != 0 ? getScreenPosition (isPhysical) : Point<int>());
  434. const auto& desktop = Desktop::getInstance();
  435. if (auto* display = desktop.getDisplays().getDisplayForRect (newBounds.translated (translation.x, translation.y),
  436. isPhysical))
  437. {
  438. auto newScaleFactor = display->scale / desktop.getGlobalScaleFactor();
  439. if (! approximatelyEqual (newScaleFactor, currentScaleFactor))
  440. {
  441. currentScaleFactor = newScaleFactor;
  442. scaleFactorListeners.call ([&] (ScaleFactorListener& l) { l.nativeScaleFactorChanged (currentScaleFactor); });
  443. }
  444. }
  445. }
  446. void onVBlank()
  447. {
  448. vBlankListeners.call ([] (auto& l) { l.onVBlank(); });
  449. if (repainter != nullptr)
  450. repainter->dispatchDeferredRepaints();
  451. }
  452. void updateVBlankTimer()
  453. {
  454. if (auto* display = Desktop::getInstance().getDisplays().getDisplayForRect (bounds))
  455. {
  456. // Some systems fail to set an explicit refresh rate, or ask for a refresh rate of 0
  457. // (observed on Raspbian Bullseye over VNC). In these situations, use a fallback value.
  458. const auto newIntFrequencyHz = roundToInt (display->verticalFrequencyHz.value_or (0.0));
  459. const auto frequencyToUse = newIntFrequencyHz != 0 ? newIntFrequencyHz : 100;
  460. if (vBlankManager.getTimerInterval() != frequencyToUse)
  461. vBlankManager.startTimerHz (frequencyToUse);
  462. }
  463. }
  464. //==============================================================================
  465. std::unique_ptr<LinuxRepaintManager> repainter;
  466. LinuxVBlankManager vBlankManager { [this]() { onVBlank(); } };
  467. ::Window windowH = {}, parentWindow = {};
  468. Rectangle<int> bounds;
  469. ComponentPeer::OptionalBorderSize windowBorder;
  470. bool fullScreen = false, isAlwaysOnTop = false;
  471. double currentScaleFactor = 1.0;
  472. Array<Component*> glRepaintListeners;
  473. ScopedWindowAssociation association;
  474. //==============================================================================
  475. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LinuxComponentPeer)
  476. };
  477. bool LinuxComponentPeer::isActiveApplication = false;
  478. //==============================================================================
  479. ComponentPeer* Component::createNewPeer (int styleFlags, void* nativeWindowToAttachTo)
  480. {
  481. return new LinuxComponentPeer (*this, styleFlags, (::Window) nativeWindowToAttachTo);
  482. }
  483. //==============================================================================
  484. JUCE_API bool JUCE_CALLTYPE Process::isForegroundProcess() { return LinuxComponentPeer::isActiveApplication; }
  485. JUCE_API void JUCE_CALLTYPE Process::makeForegroundProcess() {}
  486. JUCE_API void JUCE_CALLTYPE Process::hide() {}
  487. //==============================================================================
  488. void Desktop::setKioskComponent (Component* comp, bool enableOrDisable, bool)
  489. {
  490. if (enableOrDisable)
  491. comp->setBounds (getDisplays().getDisplayForRect (comp->getScreenBounds())->totalArea);
  492. }
  493. void Displays::findDisplays (float masterScale)
  494. {
  495. if (XWindowSystem::getInstance()->getDisplay() != nullptr)
  496. {
  497. displays = XWindowSystem::getInstance()->findDisplays (masterScale);
  498. if (! displays.isEmpty())
  499. updateToLogical();
  500. }
  501. }
  502. bool Desktop::canUseSemiTransparentWindows() noexcept
  503. {
  504. return XWindowSystem::getInstance()->canUseSemiTransparentWindows();
  505. }
  506. class Desktop::NativeDarkModeChangeDetectorImpl : private XWindowSystemUtilities::XSettings::Listener
  507. {
  508. public:
  509. NativeDarkModeChangeDetectorImpl()
  510. {
  511. const auto* windowSystem = XWindowSystem::getInstance();
  512. if (auto* xSettings = windowSystem->getXSettings())
  513. xSettings->addListener (this);
  514. darkModeEnabled = windowSystem->isDarkModeActive();
  515. }
  516. ~NativeDarkModeChangeDetectorImpl() override
  517. {
  518. if (auto* windowSystem = XWindowSystem::getInstanceWithoutCreating())
  519. if (auto* xSettings = windowSystem->getXSettings())
  520. xSettings->removeListener (this);
  521. }
  522. bool isDarkModeEnabled() const noexcept { return darkModeEnabled; }
  523. private:
  524. void settingChanged (const XWindowSystemUtilities::XSetting& settingThatHasChanged) override
  525. {
  526. if (settingThatHasChanged.name == XWindowSystem::getThemeNameSettingName())
  527. {
  528. const auto wasDarkModeEnabled = std::exchange (darkModeEnabled, XWindowSystem::getInstance()->isDarkModeActive());
  529. if (darkModeEnabled != wasDarkModeEnabled)
  530. Desktop::getInstance().darkModeChanged();
  531. }
  532. }
  533. bool darkModeEnabled = false;
  534. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeDarkModeChangeDetectorImpl)
  535. };
  536. std::unique_ptr<Desktop::NativeDarkModeChangeDetectorImpl> Desktop::createNativeDarkModeChangeDetectorImpl()
  537. {
  538. return std::make_unique<NativeDarkModeChangeDetectorImpl>();
  539. }
  540. bool Desktop::isDarkModeActive() const
  541. {
  542. return nativeDarkModeChangeDetectorImpl->isDarkModeEnabled();
  543. }
  544. static bool screenSaverAllowed = true;
  545. void Desktop::setScreenSaverEnabled (bool isEnabled)
  546. {
  547. if (screenSaverAllowed != isEnabled)
  548. {
  549. screenSaverAllowed = isEnabled;
  550. XWindowSystem::getInstance()->setScreenSaverEnabled (screenSaverAllowed);
  551. }
  552. }
  553. bool Desktop::isScreenSaverEnabled()
  554. {
  555. return screenSaverAllowed;
  556. }
  557. double Desktop::getDefaultMasterScale() { return 1.0; }
  558. Desktop::DisplayOrientation Desktop::getCurrentOrientation() const { return upright; }
  559. void Desktop::allowedOrientationsChanged() {}
  560. //==============================================================================
  561. bool detail::MouseInputSourceList::addSource()
  562. {
  563. if (sources.isEmpty())
  564. {
  565. addSource (0, MouseInputSource::InputSourceType::mouse);
  566. return true;
  567. }
  568. return false;
  569. }
  570. bool detail::MouseInputSourceList::canUseTouch() const
  571. {
  572. return false;
  573. }
  574. Point<float> MouseInputSource::getCurrentRawMousePosition()
  575. {
  576. return Desktop::getInstance().getDisplays().physicalToLogical (XWindowSystem::getInstance()->getCurrentMousePosition());
  577. }
  578. void MouseInputSource::setRawMousePosition (Point<float> newPosition)
  579. {
  580. XWindowSystem::getInstance()->setMousePosition (Desktop::getInstance().getDisplays().logicalToPhysical (newPosition));
  581. }
  582. //==============================================================================
  583. class MouseCursor::PlatformSpecificHandle
  584. {
  585. public:
  586. explicit PlatformSpecificHandle (const MouseCursor::StandardCursorType type)
  587. : cursorHandle (makeHandle (type)) {}
  588. explicit PlatformSpecificHandle (const detail::CustomMouseCursorInfo& info)
  589. : cursorHandle (makeHandle (info)) {}
  590. ~PlatformSpecificHandle()
  591. {
  592. if (cursorHandle != Cursor{})
  593. XWindowSystem::getInstance()->deleteMouseCursor (cursorHandle);
  594. }
  595. static void showInWindow (PlatformSpecificHandle* handle, ComponentPeer* peer)
  596. {
  597. const auto cursor = handle != nullptr ? handle->cursorHandle : Cursor{};
  598. if (peer != nullptr)
  599. XWindowSystem::getInstance()->showCursor ((::Window) peer->getNativeHandle(), cursor);
  600. }
  601. private:
  602. static Cursor makeHandle (const detail::CustomMouseCursorInfo& info)
  603. {
  604. const auto image = info.image.getImage();
  605. return XWindowSystem::getInstance()->createCustomMouseCursorInfo (image.rescaled ((int) (image.getWidth() / info.image.getScale()),
  606. (int) (image.getHeight() / info.image.getScale())), info.hotspot);
  607. }
  608. static Cursor makeHandle (MouseCursor::StandardCursorType type)
  609. {
  610. return XWindowSystem::getInstance()->createStandardMouseCursor (type);
  611. }
  612. Cursor cursorHandle;
  613. //==============================================================================
  614. JUCE_DECLARE_NON_COPYABLE (PlatformSpecificHandle)
  615. JUCE_DECLARE_NON_MOVEABLE (PlatformSpecificHandle)
  616. };
  617. //==============================================================================
  618. static LinuxComponentPeer* getPeerForDragEvent (Component* sourceComp)
  619. {
  620. if (sourceComp == nullptr)
  621. if (auto* draggingSource = Desktop::getInstance().getDraggingMouseSource (0))
  622. sourceComp = draggingSource->getComponentUnderMouse();
  623. if (sourceComp != nullptr)
  624. if (auto* lp = dynamic_cast<LinuxComponentPeer*> (sourceComp->getPeer()))
  625. return lp;
  626. jassertfalse; // This method must be called in response to a component's mouseDown or mouseDrag event!
  627. return nullptr;
  628. }
  629. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, bool canMoveFiles,
  630. Component* sourceComp, std::function<void()> callback)
  631. {
  632. if (files.isEmpty())
  633. return false;
  634. if (auto* peer = getPeerForDragEvent (sourceComp))
  635. return XWindowSystem::getInstance()->externalDragFileInit (peer, files, canMoveFiles, std::move (callback));
  636. // This method must be called in response to a component's mouseDown or mouseDrag event!
  637. jassertfalse;
  638. return false;
  639. }
  640. bool DragAndDropContainer::performExternalDragDropOfText (const String& text, Component* sourceComp,
  641. std::function<void()> callback)
  642. {
  643. if (text.isEmpty())
  644. return false;
  645. if (auto* peer = getPeerForDragEvent (sourceComp))
  646. return XWindowSystem::getInstance()->externalDragTextInit (peer, text, std::move (callback));
  647. // This method must be called in response to a component's mouseDown or mouseDrag event!
  648. jassertfalse;
  649. return false;
  650. }
  651. //==============================================================================
  652. void SystemClipboard::copyTextToClipboard (const String& clipText)
  653. {
  654. XWindowSystem::getInstance()->copyTextToClipboard (clipText);
  655. }
  656. String SystemClipboard::getTextFromClipboard()
  657. {
  658. return XWindowSystem::getInstance()->getTextFromClipboard();
  659. }
  660. //==============================================================================
  661. bool KeyPress::isKeyCurrentlyDown (int keyCode)
  662. {
  663. return XWindowSystem::getInstance()->isKeyCurrentlyDown (keyCode);
  664. }
  665. void LookAndFeel::playAlertSound()
  666. {
  667. std::cout << "\a" << std::flush;
  668. }
  669. //==============================================================================
  670. Image detail::WindowingHelpers::createIconForFile (const File&)
  671. {
  672. return {};
  673. }
  674. void juce_LinuxAddRepaintListener (ComponentPeer* peer, Component* dummy);
  675. void juce_LinuxAddRepaintListener (ComponentPeer* peer, Component* dummy)
  676. {
  677. if (auto* linuxPeer = dynamic_cast<LinuxComponentPeer*> (peer))
  678. linuxPeer->addOpenGLRepaintListener (dummy);
  679. }
  680. void juce_LinuxRemoveRepaintListener (ComponentPeer* peer, Component* dummy);
  681. void juce_LinuxRemoveRepaintListener (ComponentPeer* peer, Component* dummy)
  682. {
  683. if (auto* linuxPeer = dynamic_cast<LinuxComponentPeer*> (peer))
  684. linuxPeer->removeOpenGLRepaintListener (dummy);
  685. }
  686. } // namespace juce