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.

855 lines
30KB

  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. //==============================================================================
  400. template <typename This>
  401. static Point<float> localToGlobal (This& t, Point<float> relativePosition)
  402. {
  403. return relativePosition + t.getScreenPosition (false).toFloat();
  404. }
  405. template <typename This>
  406. static Point<float> globalToLocal (This& t, Point<float> screenPosition)
  407. {
  408. return screenPosition - t.getScreenPosition (false).toFloat();
  409. }
  410. //==============================================================================
  411. void settingChanged (const XWindowSystemUtilities::XSetting& settingThatHasChanged) override
  412. {
  413. static StringArray possibleSettings { XWindowSystem::getWindowScalingFactorSettingName(),
  414. "Gdk/UnscaledDPI",
  415. "Xft/DPI" };
  416. if (possibleSettings.contains (settingThatHasChanged.name))
  417. forceDisplayUpdate();
  418. }
  419. void updateScaleFactorFromNewBounds (const Rectangle<int>& newBounds, bool isPhysical)
  420. {
  421. Point<int> translation = (parentWindow != 0 ? getScreenPosition (isPhysical) : Point<int>());
  422. const auto& desktop = Desktop::getInstance();
  423. if (auto* display = desktop.getDisplays().getDisplayForRect (newBounds.translated (translation.x, translation.y),
  424. isPhysical))
  425. {
  426. auto newScaleFactor = display->scale / desktop.getGlobalScaleFactor();
  427. if (! approximatelyEqual (newScaleFactor, currentScaleFactor))
  428. {
  429. currentScaleFactor = newScaleFactor;
  430. scaleFactorListeners.call ([&] (ScaleFactorListener& l) { l.nativeScaleFactorChanged (currentScaleFactor); });
  431. }
  432. }
  433. }
  434. void onVBlank()
  435. {
  436. vBlankListeners.call ([] (auto& l) { l.onVBlank(); });
  437. if (repainter != nullptr)
  438. repainter->dispatchDeferredRepaints();
  439. }
  440. void updateVBlankTimer()
  441. {
  442. if (auto* display = Desktop::getInstance().getDisplays().getDisplayForRect (bounds))
  443. {
  444. // Some systems fail to set an explicit refresh rate, or ask for a refresh rate of 0
  445. // (observed on Raspbian Bullseye over VNC). In these situations, use a fallback value.
  446. const auto newIntFrequencyHz = roundToInt (display->verticalFrequencyHz.value_or (0.0));
  447. const auto frequencyToUse = newIntFrequencyHz != 0 ? newIntFrequencyHz : 100;
  448. if (vBlankManager.getTimerInterval() != frequencyToUse)
  449. vBlankManager.startTimerHz (frequencyToUse);
  450. }
  451. }
  452. //==============================================================================
  453. std::unique_ptr<LinuxRepaintManager> repainter;
  454. TimedCallback vBlankManager { [this]() { onVBlank(); } };
  455. ::Window windowH = {}, parentWindow = {};
  456. Rectangle<int> bounds;
  457. ComponentPeer::OptionalBorderSize windowBorder;
  458. bool fullScreen = false, isAlwaysOnTop = false;
  459. double currentScaleFactor = 1.0;
  460. Array<Component*> glRepaintListeners;
  461. ScopedWindowAssociation association;
  462. //==============================================================================
  463. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LinuxComponentPeer)
  464. };
  465. bool LinuxComponentPeer::isActiveApplication = false;
  466. //==============================================================================
  467. ComponentPeer* Component::createNewPeer (int styleFlags, void* nativeWindowToAttachTo)
  468. {
  469. return new LinuxComponentPeer (*this, styleFlags, (::Window) nativeWindowToAttachTo);
  470. }
  471. //==============================================================================
  472. JUCE_API bool JUCE_CALLTYPE Process::isForegroundProcess() { return LinuxComponentPeer::isActiveApplication; }
  473. JUCE_API void JUCE_CALLTYPE Process::makeForegroundProcess() {}
  474. JUCE_API void JUCE_CALLTYPE Process::hide() {}
  475. //==============================================================================
  476. void Desktop::setKioskComponent (Component* comp, bool enableOrDisable, bool)
  477. {
  478. if (enableOrDisable)
  479. comp->setBounds (getDisplays().getDisplayForRect (comp->getScreenBounds())->totalArea);
  480. }
  481. void Displays::findDisplays (float masterScale)
  482. {
  483. if (XWindowSystem::getInstance()->getDisplay() != nullptr)
  484. {
  485. displays = XWindowSystem::getInstance()->findDisplays (masterScale);
  486. if (! displays.isEmpty())
  487. updateToLogical();
  488. }
  489. }
  490. bool Desktop::canUseSemiTransparentWindows() noexcept
  491. {
  492. return XWindowSystem::getInstance()->canUseSemiTransparentWindows();
  493. }
  494. class Desktop::NativeDarkModeChangeDetectorImpl : private XWindowSystemUtilities::XSettings::Listener
  495. {
  496. public:
  497. NativeDarkModeChangeDetectorImpl()
  498. {
  499. const auto* windowSystem = XWindowSystem::getInstance();
  500. if (auto* xSettings = windowSystem->getXSettings())
  501. xSettings->addListener (this);
  502. darkModeEnabled = windowSystem->isDarkModeActive();
  503. }
  504. ~NativeDarkModeChangeDetectorImpl() override
  505. {
  506. if (auto* windowSystem = XWindowSystem::getInstanceWithoutCreating())
  507. if (auto* xSettings = windowSystem->getXSettings())
  508. xSettings->removeListener (this);
  509. }
  510. bool isDarkModeEnabled() const noexcept { return darkModeEnabled; }
  511. private:
  512. void settingChanged (const XWindowSystemUtilities::XSetting& settingThatHasChanged) override
  513. {
  514. if (settingThatHasChanged.name == XWindowSystem::getThemeNameSettingName())
  515. {
  516. const auto wasDarkModeEnabled = std::exchange (darkModeEnabled, XWindowSystem::getInstance()->isDarkModeActive());
  517. if (darkModeEnabled != wasDarkModeEnabled)
  518. Desktop::getInstance().darkModeChanged();
  519. }
  520. }
  521. bool darkModeEnabled = false;
  522. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeDarkModeChangeDetectorImpl)
  523. };
  524. std::unique_ptr<Desktop::NativeDarkModeChangeDetectorImpl> Desktop::createNativeDarkModeChangeDetectorImpl()
  525. {
  526. return std::make_unique<NativeDarkModeChangeDetectorImpl>();
  527. }
  528. bool Desktop::isDarkModeActive() const
  529. {
  530. return nativeDarkModeChangeDetectorImpl->isDarkModeEnabled();
  531. }
  532. static bool screenSaverAllowed = true;
  533. void Desktop::setScreenSaverEnabled (bool isEnabled)
  534. {
  535. if (screenSaverAllowed != isEnabled)
  536. {
  537. screenSaverAllowed = isEnabled;
  538. XWindowSystem::getInstance()->setScreenSaverEnabled (screenSaverAllowed);
  539. }
  540. }
  541. bool Desktop::isScreenSaverEnabled()
  542. {
  543. return screenSaverAllowed;
  544. }
  545. double Desktop::getDefaultMasterScale() { return 1.0; }
  546. Desktop::DisplayOrientation Desktop::getCurrentOrientation() const { return upright; }
  547. void Desktop::allowedOrientationsChanged() {}
  548. //==============================================================================
  549. bool detail::MouseInputSourceList::addSource()
  550. {
  551. if (sources.isEmpty())
  552. {
  553. addSource (0, MouseInputSource::InputSourceType::mouse);
  554. return true;
  555. }
  556. return false;
  557. }
  558. bool detail::MouseInputSourceList::canUseTouch() const
  559. {
  560. return false;
  561. }
  562. Point<float> MouseInputSource::getCurrentRawMousePosition()
  563. {
  564. return Desktop::getInstance().getDisplays().physicalToLogical (XWindowSystem::getInstance()->getCurrentMousePosition());
  565. }
  566. void MouseInputSource::setRawMousePosition (Point<float> newPosition)
  567. {
  568. XWindowSystem::getInstance()->setMousePosition (Desktop::getInstance().getDisplays().logicalToPhysical (newPosition));
  569. }
  570. //==============================================================================
  571. class MouseCursor::PlatformSpecificHandle
  572. {
  573. public:
  574. explicit PlatformSpecificHandle (const MouseCursor::StandardCursorType type)
  575. : cursorHandle (makeHandle (type)) {}
  576. explicit PlatformSpecificHandle (const detail::CustomMouseCursorInfo& info)
  577. : cursorHandle (makeHandle (info)) {}
  578. ~PlatformSpecificHandle()
  579. {
  580. if (cursorHandle != Cursor{})
  581. XWindowSystem::getInstance()->deleteMouseCursor (cursorHandle);
  582. }
  583. static void showInWindow (PlatformSpecificHandle* handle, ComponentPeer* peer)
  584. {
  585. const auto cursor = handle != nullptr ? handle->cursorHandle : Cursor{};
  586. if (peer != nullptr)
  587. XWindowSystem::getInstance()->showCursor ((::Window) peer->getNativeHandle(), cursor);
  588. }
  589. private:
  590. static Cursor makeHandle (const detail::CustomMouseCursorInfo& info)
  591. {
  592. const auto image = info.image.getImage();
  593. return XWindowSystem::getInstance()->createCustomMouseCursorInfo (image.rescaled ((int) (image.getWidth() / info.image.getScale()),
  594. (int) (image.getHeight() / info.image.getScale())), info.hotspot);
  595. }
  596. static Cursor makeHandle (MouseCursor::StandardCursorType type)
  597. {
  598. return XWindowSystem::getInstance()->createStandardMouseCursor (type);
  599. }
  600. Cursor cursorHandle;
  601. //==============================================================================
  602. JUCE_DECLARE_NON_COPYABLE (PlatformSpecificHandle)
  603. JUCE_DECLARE_NON_MOVEABLE (PlatformSpecificHandle)
  604. };
  605. //==============================================================================
  606. static LinuxComponentPeer* getPeerForDragEvent (Component* sourceComp)
  607. {
  608. if (sourceComp == nullptr)
  609. if (auto* draggingSource = Desktop::getInstance().getDraggingMouseSource (0))
  610. sourceComp = draggingSource->getComponentUnderMouse();
  611. if (sourceComp != nullptr)
  612. if (auto* lp = dynamic_cast<LinuxComponentPeer*> (sourceComp->getPeer()))
  613. return lp;
  614. jassertfalse; // This method must be called in response to a component's mouseDown or mouseDrag event!
  615. return nullptr;
  616. }
  617. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, bool canMoveFiles,
  618. Component* sourceComp, std::function<void()> callback)
  619. {
  620. if (files.isEmpty())
  621. return false;
  622. if (auto* peer = getPeerForDragEvent (sourceComp))
  623. return XWindowSystem::getInstance()->externalDragFileInit (peer, files, canMoveFiles, std::move (callback));
  624. // This method must be called in response to a component's mouseDown or mouseDrag event!
  625. jassertfalse;
  626. return false;
  627. }
  628. bool DragAndDropContainer::performExternalDragDropOfText (const String& text, Component* sourceComp,
  629. std::function<void()> callback)
  630. {
  631. if (text.isEmpty())
  632. return false;
  633. if (auto* peer = getPeerForDragEvent (sourceComp))
  634. return XWindowSystem::getInstance()->externalDragTextInit (peer, text, std::move (callback));
  635. // This method must be called in response to a component's mouseDown or mouseDrag event!
  636. jassertfalse;
  637. return false;
  638. }
  639. //==============================================================================
  640. void SystemClipboard::copyTextToClipboard (const String& clipText)
  641. {
  642. XWindowSystem::getInstance()->copyTextToClipboard (clipText);
  643. }
  644. String SystemClipboard::getTextFromClipboard()
  645. {
  646. return XWindowSystem::getInstance()->getTextFromClipboard();
  647. }
  648. //==============================================================================
  649. bool KeyPress::isKeyCurrentlyDown (int keyCode)
  650. {
  651. return XWindowSystem::getInstance()->isKeyCurrentlyDown (keyCode);
  652. }
  653. void LookAndFeel::playAlertSound()
  654. {
  655. std::cout << "\a" << std::flush;
  656. }
  657. //==============================================================================
  658. Image detail::WindowingHelpers::createIconForFile (const File&)
  659. {
  660. return {};
  661. }
  662. void juce_LinuxAddRepaintListener (ComponentPeer* peer, Component* dummy);
  663. void juce_LinuxAddRepaintListener (ComponentPeer* peer, Component* dummy)
  664. {
  665. if (auto* linuxPeer = dynamic_cast<LinuxComponentPeer*> (peer))
  666. linuxPeer->addOpenGLRepaintListener (dummy);
  667. }
  668. void juce_LinuxRemoveRepaintListener (ComponentPeer* peer, Component* dummy);
  669. void juce_LinuxRemoveRepaintListener (ComponentPeer* peer, Component* dummy)
  670. {
  671. if (auto* linuxPeer = dynamic_cast<LinuxComponentPeer*> (peer))
  672. linuxPeer->removeOpenGLRepaintListener (dummy);
  673. }
  674. } // namespace juce