Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

439 lines
16KB

  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. Displays::Displays (Desktop& desktop)
  21. {
  22. init (desktop);
  23. }
  24. void Displays::init (Desktop& desktop)
  25. {
  26. findDisplays (desktop.getGlobalScaleFactor());
  27. }
  28. const Displays::Display* Displays::getDisplayForRect (Rectangle<int> rect, bool isPhysical) const noexcept
  29. {
  30. int maxArea = -1;
  31. const Display* foundDisplay = nullptr;
  32. for (auto& display : displays)
  33. {
  34. auto displayArea = display.totalArea;
  35. if (isPhysical)
  36. displayArea = (displayArea.withZeroOrigin() * display.scale) + display.topLeftPhysical;
  37. displayArea = displayArea.getIntersection (rect);
  38. auto area = displayArea.getWidth() * displayArea.getHeight();
  39. if (area >= maxArea)
  40. {
  41. maxArea = area;
  42. foundDisplay = &display;
  43. }
  44. }
  45. return foundDisplay;
  46. }
  47. const Displays::Display* Displays::getDisplayForPoint (Point<int> point, bool isPhysical) const noexcept
  48. {
  49. auto minDistance = std::numeric_limits<int>::max();
  50. const Display* foundDisplay = nullptr;
  51. for (auto& display : displays)
  52. {
  53. auto displayArea = display.totalArea;
  54. if (isPhysical)
  55. displayArea = (displayArea.withZeroOrigin() * display.scale) + display.topLeftPhysical;
  56. if (displayArea.contains (point))
  57. return &display;
  58. auto distance = displayArea.getCentre().getDistanceFrom (point);
  59. if (distance <= minDistance)
  60. {
  61. minDistance = distance;
  62. foundDisplay = &display;
  63. }
  64. }
  65. return foundDisplay;
  66. }
  67. Rectangle<int> Displays::physicalToLogical (Rectangle<int> rect, const Display* useScaleFactorOfDisplay) const noexcept
  68. {
  69. return physicalToLogical (rect.toFloat(), useScaleFactorOfDisplay).toNearestInt();
  70. }
  71. Rectangle<float> Displays::physicalToLogical (Rectangle<float> rect, const Display* useScaleFactorOfDisplay) const noexcept
  72. {
  73. const auto* display = useScaleFactorOfDisplay != nullptr ? useScaleFactorOfDisplay
  74. : getDisplayForRect (rect.toNearestInt(), true);
  75. if (display == nullptr)
  76. return rect;
  77. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  78. return ((rect - display->topLeftPhysical.toFloat()) / (display->scale / globalScale))
  79. + (display->totalArea.getTopLeft().toFloat() * globalScale);
  80. }
  81. Rectangle<int> Displays::logicalToPhysical (Rectangle<int> rect, const Display* useScaleFactorOfDisplay) const noexcept
  82. {
  83. return logicalToPhysical (rect.toFloat(), useScaleFactorOfDisplay).toNearestInt();
  84. }
  85. Rectangle<float> Displays::logicalToPhysical (Rectangle<float> rect, const Display* useScaleFactorOfDisplay) const noexcept
  86. {
  87. const auto* display = useScaleFactorOfDisplay != nullptr ? useScaleFactorOfDisplay
  88. : getDisplayForRect (rect.toNearestInt(), false);
  89. if (display == nullptr)
  90. return rect;
  91. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  92. return ((rect.toFloat() - (display->totalArea.getTopLeft().toFloat() * globalScale)) * (display->scale / globalScale))
  93. + display->topLeftPhysical.toFloat();
  94. }
  95. template <typename ValueType>
  96. Point<ValueType> Displays::physicalToLogical (Point<ValueType> point, const Display* useScaleFactorOfDisplay) const noexcept
  97. {
  98. const auto* display = useScaleFactorOfDisplay != nullptr ? useScaleFactorOfDisplay
  99. : getDisplayForPoint (point.roundToInt(), true);
  100. if (display == nullptr)
  101. return point;
  102. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  103. Point<ValueType> logicalTopLeft (static_cast<ValueType> (display->totalArea.getX()), static_cast<ValueType> (display->totalArea.getY()));
  104. Point<ValueType> physicalTopLeft (static_cast<ValueType> (display->topLeftPhysical.getX()), static_cast<ValueType> (display->topLeftPhysical.getY()));
  105. return ((point - physicalTopLeft) / (display->scale / globalScale)) + (logicalTopLeft * globalScale);
  106. }
  107. template <typename ValueType>
  108. Point<ValueType> Displays::logicalToPhysical (Point<ValueType> point, const Display* useScaleFactorOfDisplay) const noexcept
  109. {
  110. const auto* display = useScaleFactorOfDisplay != nullptr ? useScaleFactorOfDisplay
  111. : getDisplayForPoint (point.roundToInt(), false);
  112. if (display == nullptr)
  113. return point;
  114. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  115. Point<ValueType> logicalTopLeft (static_cast<ValueType> (display->totalArea.getX()), static_cast<ValueType> (display->totalArea.getY()));
  116. Point<ValueType> physicalTopLeft (static_cast<ValueType> (display->topLeftPhysical.getX()), static_cast<ValueType> (display->topLeftPhysical.getY()));
  117. return ((point - (logicalTopLeft * globalScale)) * (display->scale / globalScale)) + physicalTopLeft;
  118. }
  119. const Displays::Display* Displays::getPrimaryDisplay() const noexcept
  120. {
  121. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  122. for (auto& d : displays)
  123. if (d.isMain)
  124. return &d;
  125. return nullptr;
  126. }
  127. RectangleList<int> Displays::getRectangleList (bool userAreasOnly) const
  128. {
  129. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  130. RectangleList<int> rl;
  131. for (auto& d : displays)
  132. rl.addWithoutMerging (userAreasOnly ? d.userArea : d.totalArea);
  133. return rl;
  134. }
  135. Rectangle<int> Displays::getTotalBounds (bool userAreasOnly) const
  136. {
  137. return getRectangleList (userAreasOnly).getBounds();
  138. }
  139. void Displays::refresh()
  140. {
  141. Array<Display> oldDisplays;
  142. oldDisplays.swapWith (displays);
  143. init (Desktop::getInstance());
  144. if (oldDisplays != displays)
  145. {
  146. for (auto i = ComponentPeer::getNumPeers(); --i >= 0;)
  147. if (auto* peer = ComponentPeer::getPeer (i))
  148. peer->handleScreenSizeChange();
  149. }
  150. }
  151. bool operator== (const Displays::Display& d1, const Displays::Display& d2) noexcept;
  152. bool operator== (const Displays::Display& d1, const Displays::Display& d2) noexcept
  153. {
  154. return d1.isMain == d2.isMain
  155. && d1.totalArea == d2.totalArea
  156. && d1.userArea == d2.userArea
  157. && d1.topLeftPhysical == d2.topLeftPhysical
  158. && d1.scale == d2.scale
  159. && d1.dpi == d2.dpi;
  160. }
  161. bool operator!= (const Displays::Display& d1, const Displays::Display& d2) noexcept;
  162. bool operator!= (const Displays::Display& d1, const Displays::Display& d2) noexcept { return ! (d1 == d2); }
  163. //==============================================================================
  164. // These methods are used for converting the totalArea and userArea Rectangles in Display from physical to logical
  165. // pixels. We do this by constructing a graph of connected displays where the root node has position (0, 0); this can be
  166. // safely converted to logical pixels using its scale factor and we can then traverse the graph and work out the logical pixels
  167. // for all the other connected displays. We need to do this as the logical bounds of a display depend not only on its scale
  168. // factor but also the scale factor of the displays connected to it.
  169. /**
  170. Represents a node in our graph of displays.
  171. */
  172. struct DisplayNode
  173. {
  174. /** The Display object that this represents. */
  175. Displays::Display* display;
  176. /** True if this represents the 'root' display with position (0, 0). */
  177. bool isRoot = false;
  178. /** The parent node of this node in our display graph. This will have a correct logicalArea. */
  179. DisplayNode* parent = nullptr;
  180. /** The logical area to be calculated. This will be valid after processDisplay() has
  181. been called on this node.
  182. */
  183. Rectangle<double> logicalArea;
  184. };
  185. /** Recursive - will calculate and set the logicalArea member of current. */
  186. static void processDisplay (DisplayNode* currentNode, Array<DisplayNode>& allNodes)
  187. {
  188. const auto physicalArea = currentNode->display->totalArea.toDouble();
  189. const auto scale = currentNode->display->scale;
  190. if (! currentNode->isRoot)
  191. {
  192. const auto logicalWidth = physicalArea.getWidth() / scale;
  193. const auto logicalHeight = physicalArea.getHeight() / scale;
  194. const auto physicalParentArea = currentNode->parent->display->totalArea.toDouble();
  195. const auto logicalParentArea = currentNode->parent->logicalArea; // logical area of parent has already been calculated
  196. const auto parentScale = currentNode->parent->display->scale;
  197. Rectangle<double> logicalArea (0.0, 0.0, logicalWidth, logicalHeight);
  198. if (physicalArea.getRight() == physicalParentArea.getX()) logicalArea.setPosition ({ logicalParentArea.getX() - logicalWidth, physicalArea.getY() / parentScale }); // on left
  199. else if (physicalArea.getX() == physicalParentArea.getRight()) logicalArea.setPosition ({ logicalParentArea.getRight(), physicalArea.getY() / parentScale }); // on right
  200. else if (physicalArea.getBottom() == physicalParentArea.getY()) logicalArea.setPosition ({ physicalArea.getX() / parentScale, logicalParentArea.getY() - logicalHeight }); // on top
  201. else if (physicalArea.getY() == physicalParentArea.getBottom()) logicalArea.setPosition ({ physicalArea.getX() / parentScale, logicalParentArea.getBottom() }); // on bottom
  202. else jassertfalse;
  203. currentNode->logicalArea = logicalArea;
  204. }
  205. else
  206. {
  207. // If currentNode is the root (position (0, 0)) then we can just scale the physical area
  208. currentNode->logicalArea = physicalArea / scale;
  209. currentNode->parent = currentNode;
  210. }
  211. // Find child nodes
  212. Array<DisplayNode*> children;
  213. for (auto& node : allNodes)
  214. {
  215. // Already calculated
  216. if (node.parent != nullptr)
  217. continue;
  218. const auto otherPhysicalArea = node.display->totalArea.toDouble();
  219. // If the displays are touching on any side
  220. if (otherPhysicalArea.getX() == physicalArea.getRight() || otherPhysicalArea.getRight() == physicalArea.getX()
  221. || otherPhysicalArea.getY() == physicalArea.getBottom() || otherPhysicalArea.getBottom() == physicalArea.getY())
  222. {
  223. node.parent = currentNode;
  224. children.add (&node);
  225. }
  226. }
  227. // Recursively process all child nodes
  228. for (auto child : children)
  229. processDisplay (child, allNodes);
  230. }
  231. /** This is called when the displays Array has been filled out with the info for all connected displays and the
  232. totalArea and userArea Rectangles need to be converted from physical to logical coordinates.
  233. */
  234. void Displays::updateToLogical()
  235. {
  236. if (displays.size() == 1)
  237. {
  238. auto& display = displays.getReference (0);
  239. display.totalArea = (display.totalArea.toDouble() / display.scale).toNearestInt();
  240. display.userArea = (display.userArea.toDouble() / display.scale).toNearestInt();
  241. return;
  242. }
  243. Array<DisplayNode> displayNodes;
  244. for (auto& d : displays)
  245. {
  246. DisplayNode node;
  247. node.display = &d;
  248. if (d.totalArea.getTopLeft() == Point<int>())
  249. node.isRoot = true;
  250. displayNodes.add (node);
  251. }
  252. auto* root = [&displayNodes]() -> DisplayNode*
  253. {
  254. for (auto& node : displayNodes)
  255. if (node.isRoot)
  256. return &node;
  257. auto minDistance = std::numeric_limits<int>::max();
  258. DisplayNode* retVal = nullptr;
  259. for (auto& node : displayNodes)
  260. {
  261. auto distance = node.display->totalArea.getTopLeft().getDistanceFrom ({});
  262. if (distance < minDistance)
  263. {
  264. minDistance = distance;
  265. retVal = &node;
  266. }
  267. }
  268. if (retVal != nullptr)
  269. retVal->isRoot = true;
  270. return retVal;
  271. }();
  272. // Must have a root node!
  273. jassert (root != nullptr);
  274. // Recursively traverse the display graph from the root and work out logical bounds
  275. processDisplay (root, displayNodes);
  276. for (auto& node : displayNodes)
  277. {
  278. // All of the nodes should have a parent
  279. jassert (node.parent != nullptr);
  280. auto relativeUserArea = (node.display->userArea.toDouble() - node.display->totalArea.toDouble().getTopLeft()) / node.display->scale;
  281. // Now set Display::totalArea and ::userArea using the logical area that we have calculated
  282. node.display->topLeftPhysical = node.display->totalArea.getTopLeft();
  283. node.display->totalArea = node.logicalArea.toNearestInt();
  284. node.display->userArea = (relativeUserArea + node.logicalArea.getTopLeft()).toNearestInt();
  285. }
  286. }
  287. #ifndef DOXYGEN
  288. // explicit template instantiations
  289. template Point<int> Displays::physicalToLogical (Point<int>, const Display*) const noexcept;
  290. template Point<float> Displays::physicalToLogical (Point<float>, const Display*) const noexcept;
  291. template Point<int> Displays::logicalToPhysical (Point<int>, const Display*) const noexcept;
  292. template Point<float> Displays::logicalToPhysical (Point<float>, const Display*) const noexcept;
  293. #endif
  294. //==============================================================================
  295. // Deprecated methods
  296. const Displays::Display& Displays::getDisplayContaining (Point<int> position) const noexcept
  297. {
  298. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  299. const auto* best = &displays.getReference (0);
  300. auto bestDistance = std::numeric_limits<int>::max();
  301. for (auto& d : displays)
  302. {
  303. if (d.totalArea.contains (position))
  304. {
  305. best = &d;
  306. break;
  307. }
  308. auto distance = d.totalArea.getCentre().getDistanceFrom (position);
  309. if (distance < bestDistance)
  310. {
  311. bestDistance = distance;
  312. best = &d;
  313. }
  314. }
  315. return *best;
  316. }
  317. const Displays::Display& Displays::findDisplayForRect (Rectangle<int> rect, bool isPhysical) const noexcept
  318. {
  319. if (auto* display = getDisplayForRect (rect, isPhysical))
  320. return *display;
  321. return emptyDisplay;
  322. }
  323. const Displays::Display& Displays::findDisplayForPoint (Point<int> point, bool isPhysical) const noexcept
  324. {
  325. if (auto* display = getDisplayForPoint (point, isPhysical))
  326. return *display;
  327. return emptyDisplay;
  328. }
  329. const Displays::Display& Displays::getMainDisplay() const noexcept
  330. {
  331. if (auto* display = getPrimaryDisplay())
  332. return *display;
  333. return emptyDisplay;
  334. }
  335. } // namespace juce