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.

349 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. namespace XWindowSystemUtilities
  22. {
  23. //==============================================================================
  24. /** A handy struct that uses XLockDisplay and XUnlockDisplay to lock the X server
  25. using RAII.
  26. @tags{GUI}
  27. */
  28. struct ScopedXLock
  29. {
  30. ScopedXLock();
  31. ~ScopedXLock();
  32. };
  33. //==============================================================================
  34. /** Gets a specified window property and stores its associated data, freeing it
  35. on deletion.
  36. @tags{GUI}
  37. */
  38. struct GetXProperty
  39. {
  40. GetXProperty (::Display* display, ::Window windowH, Atom property,
  41. long offset, long length, bool shouldDelete, Atom requestedType);
  42. ~GetXProperty();
  43. bool success = false;
  44. unsigned char* data = nullptr;
  45. unsigned long numItems = 0, bytesLeft = 0;
  46. Atom actualType;
  47. int actualFormat = -1;
  48. };
  49. //==============================================================================
  50. /** Initialises and stores some atoms for the display.
  51. @tags{GUI}
  52. */
  53. struct Atoms
  54. {
  55. enum ProtocolItems
  56. {
  57. TAKE_FOCUS = 0,
  58. DELETE_WINDOW = 1,
  59. PING = 2
  60. };
  61. Atoms() = default;
  62. explicit Atoms (::Display*);
  63. static Atom getIfExists (::Display*, const char* name);
  64. static Atom getCreating (::Display*, const char* name);
  65. static String getName (::Display*, Atom);
  66. static bool isMimeTypeFile (::Display*, Atom);
  67. static constexpr unsigned long DndVersion = 3;
  68. Atom protocols, protocolList[3], changeState, state, userTime, activeWin, pid, windowType, windowState, windowStateHidden,
  69. XdndAware, XdndEnter, XdndLeave, XdndPosition, XdndStatus, XdndDrop, XdndFinished, XdndSelection,
  70. XdndTypeList, XdndActionList, XdndActionDescription, XdndActionCopy, XdndActionPrivate,
  71. XembedMsgType, XembedInfo, allowedActions[5], allowedMimeTypes[4], utf8String, clipboard, targets;
  72. };
  73. //==============================================================================
  74. /** Represents a setting according to the XSETTINGS specification.
  75. @tags{GUI}
  76. */
  77. struct XSetting
  78. {
  79. enum class Type
  80. {
  81. integer,
  82. string,
  83. colour,
  84. invalid
  85. };
  86. XSetting() = default;
  87. XSetting (const String& n, int v) : name (n), type (Type::integer), integerValue (v) {}
  88. XSetting (const String& n, const String& v) : name (n), type (Type::string), stringValue (v) {}
  89. XSetting (const String& n, const Colour& v) : name (n), type (Type::colour), colourValue (v) {}
  90. bool isValid() const noexcept { return type != Type::invalid; }
  91. String name;
  92. Type type = Type::invalid;
  93. int integerValue = -1;
  94. String stringValue;
  95. Colour colourValue;
  96. };
  97. /** Parses and stores the X11 settings for a display according to the XSETTINGS
  98. specification.
  99. @tags{GUI}
  100. */
  101. class XSettings
  102. {
  103. public:
  104. explicit XSettings (::Display*);
  105. //==============================================================================
  106. void update();
  107. ::Window getSettingsWindow() const noexcept { return settingsWindow; }
  108. XSetting getSetting (const String& settingName) const;
  109. //==============================================================================
  110. struct Listener
  111. {
  112. virtual ~Listener() = default;
  113. virtual void settingChanged (const XSetting& settingThatHasChanged) = 0;
  114. };
  115. void addListener (Listener* listenerToAdd) { listeners.add (listenerToAdd); }
  116. void removeListener (Listener* listenerToRemove) { listeners.remove (listenerToRemove); }
  117. private:
  118. ::Display* display = nullptr;
  119. ::Window settingsWindow = None;
  120. Atom settingsAtom;
  121. int lastUpdateSerial = -1;
  122. std::unordered_map<String, XSetting> settings;
  123. ListenerList<Listener> listeners;
  124. //==============================================================================
  125. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (XSettings)
  126. };
  127. }
  128. //==============================================================================
  129. class LinuxComponentPeer;
  130. class XWindowSystem : public DeletedAtShutdown
  131. {
  132. public:
  133. //==============================================================================
  134. ::Window createWindow (::Window parentWindow, LinuxComponentPeer*) const;
  135. void destroyWindow (::Window);
  136. void setTitle (::Window, const String&) const;
  137. void setIcon (::Window , const Image&) const;
  138. void setVisible (::Window, bool shouldBeVisible) const;
  139. void setBounds (::Window, Rectangle<int>, bool fullScreen) const;
  140. void updateConstraints (::Window) const;
  141. BorderSize<int> getBorderSize (::Window) const;
  142. Rectangle<int> getWindowBounds (::Window, ::Window parentWindow);
  143. Point<int> getPhysicalParentScreenPosition() const;
  144. bool contains (::Window, Point<int> localPos) const;
  145. void setMinimised (::Window, bool shouldBeMinimised) const;
  146. bool isMinimised (::Window) const;
  147. void setMaximised (::Window, bool shouldBeMinimised) const;
  148. void toFront (::Window, bool makeActive) const;
  149. void toBehind (::Window, ::Window otherWindow) const;
  150. bool isFocused (::Window) const;
  151. bool grabFocus (::Window) const;
  152. bool canUseSemiTransparentWindows() const;
  153. bool canUseARGBImages() const;
  154. bool isDarkModeActive() const;
  155. int getNumPaintsPendingForWindow (::Window);
  156. void processPendingPaintsForWindow (::Window);
  157. void addPendingPaintForWindow (::Window);
  158. void removePendingPaintForWindow (::Window);
  159. Image createImage (bool isSemiTransparentWindow, int width, int height, bool argb) const;
  160. void blitToWindow (::Window, Image, Rectangle<int> destinationRect, Rectangle<int> totalRect) const;
  161. void setScreenSaverEnabled (bool enabled) const;
  162. Point<float> getCurrentMousePosition() const;
  163. void setMousePosition (Point<float> pos) const;
  164. Cursor createCustomMouseCursorInfo (const Image&, Point<int> hotspot) const;
  165. void deleteMouseCursor (Cursor cursorHandle) const;
  166. Cursor createStandardMouseCursor (MouseCursor::StandardCursorType) const;
  167. void showCursor (::Window, Cursor cursorHandle) const;
  168. bool isKeyCurrentlyDown (int keyCode) const;
  169. ModifierKeys getNativeRealtimeModifiers() const;
  170. Array<Displays::Display> findDisplays (float masterScale) const;
  171. ::Window createKeyProxy (::Window) const;
  172. void deleteKeyProxy (::Window) const;
  173. bool externalDragFileInit (LinuxComponentPeer*, const StringArray& files, bool canMove, std::function<void()>&& callback) const;
  174. bool externalDragTextInit (LinuxComponentPeer*, const String& text, std::function<void()>&& callback) const;
  175. void copyTextToClipboard (const String&);
  176. String getTextFromClipboard() const;
  177. String getLocalClipboardContent() const noexcept { return localClipboardContent; }
  178. ::Display* getDisplay() const noexcept { return display; }
  179. const XWindowSystemUtilities::Atoms& getAtoms() const noexcept { return atoms; }
  180. XWindowSystemUtilities::XSettings* getXSettings() const noexcept { return xSettings.get(); }
  181. bool isX11Available() const noexcept { return xIsAvailable; }
  182. static String getWindowScalingFactorSettingName() { return "Gdk/WindowScalingFactor"; }
  183. static String getThemeNameSettingName() { return "Net/ThemeName"; }
  184. //==============================================================================
  185. void handleWindowMessage (LinuxComponentPeer*, XEvent&) const;
  186. bool isParentWindowOf (::Window, ::Window possibleChild) const;
  187. //==============================================================================
  188. JUCE_DECLARE_SINGLETON (XWindowSystem, false)
  189. private:
  190. XWindowSystem();
  191. ~XWindowSystem();
  192. //==============================================================================
  193. struct VisualAndDepth
  194. {
  195. Visual* visual;
  196. int depth;
  197. };
  198. struct DisplayVisuals
  199. {
  200. explicit DisplayVisuals (::Display*);
  201. VisualAndDepth getBestVisualForWindow (bool) const;
  202. bool isValid() const noexcept;
  203. Visual* visual16Bit = nullptr;
  204. Visual* visual24Bit = nullptr;
  205. Visual* visual32Bit = nullptr;
  206. };
  207. bool initialiseXDisplay();
  208. void destroyXDisplay();
  209. //==============================================================================
  210. ::Window getFocusWindow (::Window) const;
  211. bool isFrontWindow (::Window) const;
  212. //==============================================================================
  213. void xchangeProperty (::Window, Atom, Atom, int, const void*, int) const;
  214. void removeWindowDecorations (::Window) const;
  215. void addWindowButtons (::Window, int) const;
  216. void setWindowType (::Window, int) const;
  217. void initialisePointerMap();
  218. void deleteIconPixmaps (::Window) const;
  219. void updateModifierMappings() const;
  220. long getUserTime (::Window) const;
  221. void initialiseXSettings();
  222. //==============================================================================
  223. void handleKeyPressEvent (LinuxComponentPeer*, XKeyEvent&) const;
  224. void handleKeyReleaseEvent (LinuxComponentPeer*, const XKeyEvent&) const;
  225. void handleWheelEvent (LinuxComponentPeer*, const XButtonPressedEvent&, float) const;
  226. void handleButtonPressEvent (LinuxComponentPeer*, const XButtonPressedEvent&, int) const;
  227. void handleButtonPressEvent (LinuxComponentPeer*, const XButtonPressedEvent&) const;
  228. void handleButtonReleaseEvent (LinuxComponentPeer*, const XButtonReleasedEvent&) const;
  229. void handleMotionNotifyEvent (LinuxComponentPeer*, const XPointerMovedEvent&) const;
  230. void handleEnterNotifyEvent (LinuxComponentPeer*, const XEnterWindowEvent&) const;
  231. void handleLeaveNotifyEvent (LinuxComponentPeer*, const XLeaveWindowEvent&) const;
  232. void handleFocusInEvent (LinuxComponentPeer*) const;
  233. void handleFocusOutEvent (LinuxComponentPeer*) const;
  234. void handleExposeEvent (LinuxComponentPeer*, XExposeEvent&) const;
  235. void handleConfigureNotifyEvent (LinuxComponentPeer*, XConfigureEvent&) const;
  236. void handleGravityNotify (LinuxComponentPeer*) const;
  237. void propertyNotifyEvent (LinuxComponentPeer*, const XPropertyEvent&) const;
  238. void handleMappingNotify (XMappingEvent&) const;
  239. void handleClientMessageEvent (LinuxComponentPeer*, XClientMessageEvent&, XEvent&) const;
  240. void handleXEmbedMessage (LinuxComponentPeer*, XClientMessageEvent&) const;
  241. void dismissBlockingModals (LinuxComponentPeer*) const;
  242. void dismissBlockingModals (LinuxComponentPeer*, const XConfigureEvent&) const;
  243. void updateConstraints (::Window, ComponentPeer&) const;
  244. ::Window findTopLevelWindowOf (::Window) const;
  245. static void windowMessageReceive (XEvent&);
  246. //==============================================================================
  247. bool xIsAvailable = false;
  248. XWindowSystemUtilities::Atoms atoms;
  249. ::Display* display = nullptr;
  250. std::unique_ptr<DisplayVisuals> displayVisuals;
  251. std::unique_ptr<XWindowSystemUtilities::XSettings> xSettings;
  252. #if JUCE_USE_XSHM
  253. std::map<::Window, int> shmPaintsPendingMap;
  254. #endif
  255. int shmCompletionEvent = 0;
  256. int pointerMap[5] = {};
  257. String localClipboardContent;
  258. Point<int> parentScreenPosition;
  259. //==============================================================================
  260. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (XWindowSystem)
  261. };
  262. } // namespace juce