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.

258 lines
10KB

  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 (::Window windowH, Atom property, long offset,
  41. 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,
  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. //==============================================================================
  75. template<typename WindowHandle>
  76. class LinuxComponentPeer;
  77. class XWindowSystem : public DeletedAtShutdown
  78. {
  79. public:
  80. //==============================================================================
  81. ::Window createWindow (::Window parentWindow, LinuxComponentPeer<::Window>* peer) const;
  82. void destroyWindow (::Window windowH);
  83. void setTitle (::Window windowH, const String& title) const;
  84. void setIcon (::Window windowH, const Image& newIcon) const;
  85. void setVisible (::Window windowH, bool shouldBeVisible) const;
  86. void setBounds (::Window windowH, Rectangle<int> newBounds, bool fullScreen) const;
  87. BorderSize<int> getBorderSize (::Window windowH) const;
  88. Rectangle<int> getWindowBounds (::Window windowH, ::Window parentWindow);
  89. Point<int> getParentScreenPosition() const;
  90. bool contains (::Window windowH, Point<int> localPos) const;
  91. void setMinimised (::Window windowH, bool shouldBeMinimised) const;
  92. bool isMinimised (::Window windowH) const;
  93. void toFront (::Window windowH, bool makeActive) const;
  94. void toBehind (::Window windowH, ::Window otherWindow) const;
  95. bool isFocused (::Window windowH) const;
  96. bool grabFocus (::Window windowH) const;
  97. bool canUseSemiTransparentWindows() const;
  98. bool canUseARGBImages() const;
  99. int getNumPaintsPendingForWindow (::Window windowH);
  100. void processPendingPaintsForWindow (::Window windowH);
  101. void addPendingPaintForWindow (::Window windowH);
  102. void removePendingPaintForWindow (::Window windowH);
  103. Image createImage (bool isSemiTransparentWindow, int width, int height, bool argb) const;
  104. void blitToWindow (::Window windowH, Image image, Rectangle<int> destinationRect, Rectangle<int> totalRect) const;
  105. void setScreenSaverEnabled (bool enabled) const;
  106. Point<float> getCurrentMousePosition() const;
  107. void setMousePosition (Point<float> pos) const;
  108. void* createCustomMouseCursorInfo (const Image& image, Point<int> hotspot) const;
  109. void deleteMouseCursor (void* cursorHandle) const;
  110. void* createStandardMouseCursor (MouseCursor::StandardCursorType type) const;
  111. void showCursor (::Window windowH, void* cursorHandle) const;
  112. bool isKeyCurrentlyDown (int keyCode) const;
  113. ModifierKeys getNativeRealtimeModifiers() const;
  114. Array<Displays::Display> findDisplays (float masterScale) const;
  115. ::Window createKeyProxy (::Window windowH) const;
  116. void deleteKeyProxy (::Window keyProxy) const;
  117. bool externalDragFileInit (LinuxComponentPeer<::Window>* peer, const StringArray& files, bool canMove, std::function<void()>&& callback) const;
  118. bool externalDragTextInit (LinuxComponentPeer<::Window>* peer, const String& text, std::function<void()>&& callback) const;
  119. void copyTextToClipboard (const String& clipText);
  120. String getTextFromClipboard() const;
  121. String getLocalClipboardContent() const { return localClipboardContent; }
  122. ::Display* getDisplay() { return display; }
  123. XWindowSystemUtilities::Atoms& getAtoms() { return atoms; }
  124. //==============================================================================
  125. void handleWindowMessage (LinuxComponentPeer<::Window>* peer, XEvent& event) const;
  126. //==============================================================================
  127. JUCE_DECLARE_SINGLETON (XWindowSystem, false)
  128. private:
  129. XWindowSystem();
  130. ~XWindowSystem();
  131. //==============================================================================
  132. struct VisualAndDepth
  133. {
  134. Visual* visual;
  135. int depth;
  136. };
  137. struct DisplayVisuals
  138. {
  139. explicit DisplayVisuals (::Display* d);
  140. VisualAndDepth getBestVisualForWindow (bool isSemiTransparent) const;
  141. bool isValid() const noexcept;
  142. Visual* visual16Bit = nullptr;
  143. Visual* visual24Bit = nullptr;
  144. Visual* visual32Bit = nullptr;
  145. };
  146. bool initialiseXDisplay();
  147. void destroyXDisplay();
  148. //==============================================================================
  149. ::Window getFocusWindow (::Window windowH) const;
  150. bool isParentWindowOf (::Window windowH, ::Window possibleChild) const;
  151. bool isFrontWindow (::Window windowH) const;
  152. //==============================================================================
  153. void xchangeProperty (::Window windowH, Atom property, Atom type, int format, const void* data, int numElements) const;
  154. void removeWindowDecorations (::Window windowH) const;
  155. void addWindowButtons (::Window windowH, int styleFlags) const;
  156. void setWindowType (::Window windowH, int styleFlags) const;
  157. void initialisePointerMap();
  158. void deleteIconPixmaps (::Window windowH) const;
  159. void updateModifierMappings() const;
  160. long getUserTime (::Window windowH) const;
  161. //==============================================================================
  162. void handleKeyPressEvent (LinuxComponentPeer<::Window>*, XKeyEvent&) const;
  163. void handleKeyReleaseEvent (LinuxComponentPeer<::Window>*, const XKeyEvent&) const;
  164. void handleWheelEvent (LinuxComponentPeer<::Window>*, const XButtonPressedEvent&, float) const;
  165. void handleButtonPressEvent (LinuxComponentPeer<::Window>*, const XButtonPressedEvent&, int) const;
  166. void handleButtonPressEvent (LinuxComponentPeer<::Window>*, const XButtonPressedEvent&) const;
  167. void handleButtonReleaseEvent (LinuxComponentPeer<::Window>*, const XButtonReleasedEvent&) const;
  168. void handleMotionNotifyEvent (LinuxComponentPeer<::Window>*, const XPointerMovedEvent&) const;
  169. void handleEnterNotifyEvent (LinuxComponentPeer<::Window>*, const XEnterWindowEvent&) const;
  170. void handleLeaveNotifyEvent (LinuxComponentPeer<::Window>*, const XLeaveWindowEvent&) const;
  171. void handleFocusInEvent (LinuxComponentPeer<::Window>*) const;
  172. void handleFocusOutEvent (LinuxComponentPeer<::Window>*) const;
  173. void handleExposeEvent (LinuxComponentPeer<::Window>*, XExposeEvent&) const;
  174. void handleConfigureNotifyEvent (LinuxComponentPeer<::Window>*, XConfigureEvent&) const;
  175. void handleGravityNotify (LinuxComponentPeer<::Window>*) const;
  176. void handleMappingNotify (XMappingEvent&) const;
  177. void handleClientMessageEvent (LinuxComponentPeer<::Window>*, XClientMessageEvent&, XEvent&) const;
  178. void handleXEmbedMessage (LinuxComponentPeer<::Window>*, XClientMessageEvent&) const;
  179. //==============================================================================
  180. bool xIsAvailable = false;
  181. XWindowSystemUtilities::Atoms atoms;
  182. ::Display* display = nullptr;
  183. std::unique_ptr<DisplayVisuals> displayVisuals;
  184. #if JUCE_USE_XSHM
  185. std::map<::Window, int> shmPaintsPendingMap;
  186. #endif
  187. int shmCompletionEvent = 0;
  188. int pointerMap[5] = {};
  189. String localClipboardContent;
  190. Point<int> parentScreenPosition;
  191. //==============================================================================
  192. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (XWindowSystem)
  193. };
  194. } // namespace juce