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.

226 lines
9.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. namespace XWindowSystemUtilities
  17. {
  18. //==============================================================================
  19. /** A handy struct that uses XLockDisplay and XUnlockDisplay to lock the X server
  20. using RAII.
  21. @tags{GUI}
  22. */
  23. struct ScopedXLock
  24. {
  25. ScopedXLock();
  26. ~ScopedXLock();
  27. };
  28. //==============================================================================
  29. /** Gets a specified window property and stores its associated data, freeing it
  30. on deletion.
  31. @tags{GUI}
  32. */
  33. struct GetXProperty
  34. {
  35. GetXProperty (::Window windowH, Atom property, long offset,
  36. long length, bool shouldDelete, Atom requestedType);
  37. ~GetXProperty();
  38. bool success = false;
  39. unsigned char* data = nullptr;
  40. unsigned long numItems = 0, bytesLeft = 0;
  41. Atom actualType;
  42. int actualFormat = -1;
  43. };
  44. //==============================================================================
  45. /** Initialises and stores some atoms for the display.
  46. @tags{GUI}
  47. */
  48. struct Atoms
  49. {
  50. enum ProtocolItems
  51. {
  52. TAKE_FOCUS = 0,
  53. DELETE_WINDOW = 1,
  54. PING = 2
  55. };
  56. Atoms (::Display*);
  57. static Atom getIfExists (::Display*, const char* name);
  58. static Atom getCreating (::Display*, const char* name);
  59. static String getName (::Display*, Atom);
  60. static bool isMimeTypeFile (::Display*, Atom);
  61. static constexpr unsigned long DndVersion = 3;
  62. Atom protocols, protocolList[3], changeState, state, userTime, activeWin, pid, windowType, windowState,
  63. XdndAware, XdndEnter, XdndLeave, XdndPosition, XdndStatus, XdndDrop, XdndFinished, XdndSelection,
  64. XdndTypeList, XdndActionList, XdndActionDescription, XdndActionCopy, XdndActionPrivate,
  65. XembedMsgType, XembedInfo, allowedActions[5], allowedMimeTypes[4], utf8String, clipboard, targets;
  66. };
  67. }
  68. //==============================================================================
  69. template<typename WindowHandle>
  70. class LinuxComponentPeer;
  71. class XWindowSystem : public DeletedAtShutdown
  72. {
  73. public:
  74. //==============================================================================
  75. ::Window createWindow (::Window parentWindow, LinuxComponentPeer<::Window>* peer) const;
  76. void destroyWindow (::Window windowH);
  77. void setTitle (::Window windowH, const String& title) const;
  78. void setIcon (::Window windowH, const Image& newIcon) const;
  79. void setVisible (::Window windowH, bool shouldBeVisible) const;
  80. void setBounds (::Window windowH, Rectangle<int> newBounds, bool fullScreen) const;
  81. BorderSize<int> getBorderSize (::Window windowH) const;
  82. Rectangle<int> getWindowBounds (::Window windowH, ::Window parentWindow);
  83. Point<int> getParentScreenPosition() const;
  84. bool contains (::Window windowH, Point<int> localPos) const;
  85. void setMinimised (::Window windowH, bool shouldBeMinimised) const;
  86. bool isMinimised (::Window windowH) const;
  87. void toFront (::Window windowH, bool makeActive) const;
  88. void toBehind (::Window windowH, ::Window otherWindow) const;
  89. bool isFocused (::Window windowH) const;
  90. bool grabFocus (::Window windowH) const;
  91. bool canUseSemiTransparentWindows() const;
  92. bool canUseARGBImages() const;
  93. int getNumPaintsPending (::Window windowH) const;
  94. Image createImage (int width, int height, bool argb) const;
  95. void blitToWindow (::Window windowH, Image image, Rectangle<int> destinationRect, Rectangle<int> totalRect) const;
  96. void setScreenSaverEnabled (bool enabled) const;
  97. Point<float> getCurrentMousePosition() const;
  98. void setMousePosition (Point<float> pos) const;
  99. void* createCustomMouseCursorInfo (const Image& image, Point<int> hotspot) const;
  100. void deleteMouseCursor (void* cursorHandle) const;
  101. void* createStandardMouseCursor (MouseCursor::StandardCursorType type) const;
  102. void showCursor (::Window windowH, void* cursorHandle) const;
  103. bool isKeyCurrentlyDown (int keyCode) const;
  104. ModifierKeys getNativeRealtimeModifiers() const;
  105. Array<Displays::Display> findDisplays (float masterScale) const;
  106. ::Window createKeyProxy (::Window windowH) const;
  107. void deleteKeyProxy (::Window keyProxy) const;
  108. bool externalDragFileInit (LinuxComponentPeer<::Window>* peer, const StringArray& files, bool canMove, std::function<void()>&& callback) const;
  109. bool externalDragTextInit (LinuxComponentPeer<::Window>* peer, const String& text, std::function<void()>&& callback) const;
  110. void copyTextToClipboard (const String& clipText);
  111. String getTextFromClipboard() const;
  112. String getLocalClipboardContent() const { return localClipboardContent; }
  113. ::Display* getDisplay() { return display; }
  114. XWindowSystemUtilities::Atoms& getAtoms() { jassert (atoms.get() != nullptr); return *atoms; }
  115. //==============================================================================
  116. void handleWindowMessage (LinuxComponentPeer<::Window>* peer, XEvent& event) const;
  117. //==============================================================================
  118. JUCE_DECLARE_SINGLETON (XWindowSystem, false)
  119. private:
  120. XWindowSystem();
  121. ~XWindowSystem();
  122. //==============================================================================
  123. void initialiseXDisplay();
  124. void destroyXDisplay();
  125. //==============================================================================
  126. ::Window getFocusWindow (::Window windowH) const;
  127. bool isParentWindowOf (::Window windowH, ::Window possibleChild) const;
  128. bool isFrontWindow (::Window windowH) const;
  129. //==============================================================================
  130. void xchangeProperty (::Window windowH, Atom property, Atom type, int format, const void* data, int numElements) const;
  131. void removeWindowDecorations (::Window windowH) const;
  132. void addWindowButtons (::Window windowH, int styleFlags) const;
  133. void setWindowType (::Window windowH, int styleFlags) const;
  134. void initialisePointerMap();
  135. void deleteIconPixmaps (::Window windowH) const;
  136. void updateModifierMappings() const;
  137. long getUserTime (::Window windowH) const;
  138. //==============================================================================
  139. void handleKeyPressEvent (LinuxComponentPeer<::Window>*, XKeyEvent&) const;
  140. void handleKeyReleaseEvent (LinuxComponentPeer<::Window>*, const XKeyEvent&) const;
  141. void handleWheelEvent (LinuxComponentPeer<::Window>*, const XButtonPressedEvent&, float) const;
  142. void handleButtonPressEvent (LinuxComponentPeer<::Window>*, const XButtonPressedEvent&, int) const;
  143. void handleButtonPressEvent (LinuxComponentPeer<::Window>*, const XButtonPressedEvent&) const;
  144. void handleButtonReleaseEvent (LinuxComponentPeer<::Window>*, const XButtonReleasedEvent&) const;
  145. void handleMotionNotifyEvent (LinuxComponentPeer<::Window>*, const XPointerMovedEvent&) const;
  146. void handleEnterNotifyEvent (LinuxComponentPeer<::Window>*, const XEnterWindowEvent&) const;
  147. void handleLeaveNotifyEvent (LinuxComponentPeer<::Window>*, const XLeaveWindowEvent&) const;
  148. void handleFocusInEvent (LinuxComponentPeer<::Window>*) const;
  149. void handleFocusOutEvent (LinuxComponentPeer<::Window>*) const;
  150. void handleExposeEvent (LinuxComponentPeer<::Window>*, XExposeEvent&) const;
  151. void handleConfigureNotifyEvent (LinuxComponentPeer<::Window>*, XConfigureEvent&) const;
  152. void handleGravityNotify (LinuxComponentPeer<::Window>*) const;
  153. void handleMappingNotify (XMappingEvent&) const;
  154. void handleClientMessageEvent (LinuxComponentPeer<::Window>*, XClientMessageEvent&, XEvent&) const;
  155. void handleXEmbedMessage (LinuxComponentPeer<::Window>*, XClientMessageEvent&) const;
  156. //==============================================================================
  157. bool xIsAvailable = false;
  158. std::unique_ptr<XWindowSystemUtilities::Atoms> atoms;
  159. ::Display* display = nullptr;
  160. Colormap colormap = {};
  161. Visual* visual = nullptr;
  162. int depth = 0, shmCompletionEvent = 0;
  163. int pointerMap[5] = {};
  164. String localClipboardContent;
  165. Point<int> parentScreenPosition;
  166. //==============================================================================
  167. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (XWindowSystem)
  168. };
  169. } // namespace juce