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.

332 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. typedef void (*WindowMessageReceiveCallback) (XEvent&);
  16. WindowMessageReceiveCallback dispatchWindowMessage = nullptr;
  17. typedef void (*SelectionRequestCallback) (XSelectionRequestEvent&);
  18. SelectionRequestCallback handleSelectionRequest = nullptr;
  19. ::Window juce_messageWindowHandle;
  20. XContext windowHandleXContext;
  21. //==============================================================================
  22. namespace X11ErrorHandling
  23. {
  24. static XErrorHandler oldErrorHandler = {};
  25. static XIOErrorHandler oldIOErrorHandler = {};
  26. //==============================================================================
  27. // Usually happens when client-server connection is broken
  28. int ioErrorHandler (::Display*)
  29. {
  30. DBG ("ERROR: connection to X server broken.. terminating.");
  31. if (JUCEApplicationBase::isStandaloneApp())
  32. MessageManager::getInstance()->stopDispatchLoop();
  33. return 0;
  34. }
  35. int errorHandler (::Display* display, XErrorEvent* event)
  36. {
  37. ignoreUnused (display, event);
  38. #if JUCE_DEBUG_XERRORS
  39. char errorStr[64] = { 0 };
  40. char requestStr[64] = { 0 };
  41. XGetErrorText (display, event->error_code, errorStr, 64);
  42. XGetErrorDatabaseText (display, "XRequest", String (event->request_code).toUTF8(), "Unknown", requestStr, 64);
  43. DBG ("ERROR: X returned " << errorStr << " for operation " << requestStr);
  44. #endif
  45. return 0;
  46. }
  47. void installXErrorHandlers()
  48. {
  49. oldIOErrorHandler = XSetIOErrorHandler (ioErrorHandler);
  50. oldErrorHandler = XSetErrorHandler (errorHandler);
  51. }
  52. void removeXErrorHandlers()
  53. {
  54. XSetIOErrorHandler (oldIOErrorHandler);
  55. oldIOErrorHandler = {};
  56. XSetErrorHandler (oldErrorHandler);
  57. oldErrorHandler = {};
  58. }
  59. }
  60. //==============================================================================
  61. XWindowSystem::XWindowSystem() noexcept
  62. {
  63. if (JUCEApplicationBase::isStandaloneApp())
  64. {
  65. // Initialise xlib for multiple thread support
  66. static bool initThreadCalled = false;
  67. if (! initThreadCalled)
  68. {
  69. if (! XInitThreads())
  70. {
  71. // This is fatal! Print error and closedown
  72. Logger::outputDebugString ("Failed to initialise xlib thread support.");
  73. Process::terminate();
  74. return;
  75. }
  76. initThreadCalled = true;
  77. }
  78. X11ErrorHandling::installXErrorHandlers();
  79. }
  80. }
  81. XWindowSystem::~XWindowSystem() noexcept
  82. {
  83. if (JUCEApplicationBase::isStandaloneApp())
  84. X11ErrorHandling::removeXErrorHandlers();
  85. clearSingletonInstance();
  86. }
  87. ::Display* XWindowSystem::displayRef() noexcept
  88. {
  89. if (++displayCount == 1)
  90. {
  91. jassert (display == nullptr);
  92. String displayName (getenv ("DISPLAY"));
  93. if (displayName.isEmpty())
  94. displayName = ":0.0";
  95. // it seems that on some systems XOpenDisplay will occasionally
  96. // fail the first time, but succeed on a second attempt..
  97. for (int retries = 2; --retries >= 0;)
  98. {
  99. display = XOpenDisplay (displayName.toUTF8());
  100. if (display != nullptr)
  101. break;
  102. }
  103. initialiseXDisplay();
  104. }
  105. return display;
  106. }
  107. ::Display* XWindowSystem::displayUnref() noexcept
  108. {
  109. jassert (display != nullptr);
  110. jassert (displayCount.get() > 0);
  111. if (--displayCount == 0)
  112. {
  113. destroyXDisplay();
  114. XCloseDisplay (display);
  115. display = nullptr;
  116. }
  117. return display;
  118. }
  119. void XWindowSystem::initialiseXDisplay() noexcept
  120. {
  121. // This is fatal! Print error and closedown
  122. if (display == nullptr)
  123. {
  124. Logger::outputDebugString ("Failed to connect to the X Server.");
  125. Process::terminate();
  126. }
  127. // Create a context to store user data associated with Windows we create
  128. windowHandleXContext = XUniqueContext();
  129. // We're only interested in client messages for this window, which are always sent
  130. XSetWindowAttributes swa;
  131. swa.event_mask = NoEventMask;
  132. // Create our message window (this will never be mapped)
  133. const int screen = DefaultScreen (display);
  134. juce_messageWindowHandle = XCreateWindow (display, RootWindow (display, screen),
  135. 0, 0, 1, 1, 0, 0, InputOnly,
  136. DefaultVisual (display, screen),
  137. CWEventMask, &swa);
  138. XSync (display, False);
  139. // Setup input event handler
  140. int fd = XConnectionNumber (display);
  141. LinuxEventLoop::registerFdCallback (fd,
  142. [this](int)
  143. {
  144. do
  145. {
  146. XEvent evt;
  147. {
  148. ScopedXLock xlock (display);
  149. if (! XPending (display))
  150. return;
  151. XNextEvent (display, &evt);
  152. }
  153. if (evt.type == SelectionRequest && evt.xany.window == juce_messageWindowHandle
  154. && handleSelectionRequest != nullptr)
  155. {
  156. handleSelectionRequest (evt.xselectionrequest);
  157. }
  158. else if (evt.xany.window != juce_messageWindowHandle
  159. && dispatchWindowMessage != nullptr)
  160. {
  161. dispatchWindowMessage (evt);
  162. }
  163. } while (display != nullptr);
  164. });
  165. }
  166. void XWindowSystem::destroyXDisplay() noexcept
  167. {
  168. ScopedXLock xlock (display);
  169. XDestroyWindow (display, juce_messageWindowHandle);
  170. juce_messageWindowHandle = 0;
  171. XSync (display, True);
  172. LinuxEventLoop::unregisterFdCallback (XConnectionNumber (display));
  173. }
  174. JUCE_IMPLEMENT_SINGLETON (XWindowSystem)
  175. //==============================================================================
  176. ScopedXDisplay::ScopedXDisplay() : display (XWindowSystem::getInstance()->displayRef())
  177. {
  178. }
  179. ScopedXDisplay::~ScopedXDisplay()
  180. {
  181. XWindowSystem::getInstance()->displayUnref();
  182. }
  183. //==============================================================================
  184. ScopedXLock::ScopedXLock (::Display* d) : display (d)
  185. {
  186. if (display != nullptr)
  187. XLockDisplay (display);
  188. }
  189. ScopedXLock::~ScopedXLock()
  190. {
  191. if (display != nullptr)
  192. XUnlockDisplay (display);
  193. }
  194. //==============================================================================
  195. Atoms::Atoms (::Display* display)
  196. {
  197. protocols = getIfExists (display, "WM_PROTOCOLS");
  198. protocolList [TAKE_FOCUS] = getIfExists (display, "WM_TAKE_FOCUS");
  199. protocolList [DELETE_WINDOW] = getIfExists (display, "WM_DELETE_WINDOW");
  200. protocolList [PING] = getIfExists (display, "_NET_WM_PING");
  201. changeState = getIfExists (display, "WM_CHANGE_STATE");
  202. state = getIfExists (display, "WM_STATE");
  203. userTime = getCreating (display, "_NET_WM_USER_TIME");
  204. activeWin = getCreating (display, "_NET_ACTIVE_WINDOW");
  205. pid = getCreating (display, "_NET_WM_PID");
  206. windowType = getIfExists (display, "_NET_WM_WINDOW_TYPE");
  207. windowState = getIfExists (display, "_NET_WM_STATE");
  208. XdndAware = getCreating (display, "XdndAware");
  209. XdndEnter = getCreating (display, "XdndEnter");
  210. XdndLeave = getCreating (display, "XdndLeave");
  211. XdndPosition = getCreating (display, "XdndPosition");
  212. XdndStatus = getCreating (display, "XdndStatus");
  213. XdndDrop = getCreating (display, "XdndDrop");
  214. XdndFinished = getCreating (display, "XdndFinished");
  215. XdndSelection = getCreating (display, "XdndSelection");
  216. XdndTypeList = getCreating (display, "XdndTypeList");
  217. XdndActionList = getCreating (display, "XdndActionList");
  218. XdndActionCopy = getCreating (display, "XdndActionCopy");
  219. XdndActionPrivate = getCreating (display, "XdndActionPrivate");
  220. XdndActionDescription = getCreating (display, "XdndActionDescription");
  221. XembedMsgType = getCreating (display, "_XEMBED");
  222. XembedInfo = getCreating (display, "_XEMBED_INFO");
  223. allowedMimeTypes[0] = getCreating (display, "UTF8_STRING");
  224. allowedMimeTypes[1] = getCreating (display, "text/plain;charset=utf-8");
  225. allowedMimeTypes[2] = getCreating (display, "text/plain");
  226. allowedMimeTypes[3] = getCreating (display, "text/uri-list");
  227. allowedActions[0] = getCreating (display, "XdndActionMove");
  228. allowedActions[1] = XdndActionCopy;
  229. allowedActions[2] = getCreating (display, "XdndActionLink");
  230. allowedActions[3] = getCreating (display, "XdndActionAsk");
  231. allowedActions[4] = XdndActionPrivate;
  232. }
  233. Atom Atoms::getIfExists (::Display* display, const char* name) { return XInternAtom (display, name, True); }
  234. Atom Atoms::getCreating (::Display* display, const char* name) { return XInternAtom (display, name, False); }
  235. String Atoms::getName (::Display* display, const Atom atom)
  236. {
  237. if (atom == None)
  238. return "None";
  239. return String (XGetAtomName (display, atom));
  240. }
  241. bool Atoms::isMimeTypeFile (::Display* display, const Atom atom)
  242. {
  243. return getName (display, atom).equalsIgnoreCase ("text/uri-list");
  244. }
  245. const unsigned long Atoms::DndVersion = 3;
  246. //==============================================================================
  247. GetXProperty::GetXProperty (::Display* display, Window window, Atom atom,
  248. long offset, long length, bool shouldDelete,
  249. Atom requestedType)
  250. {
  251. success = (XGetWindowProperty (display, window, atom, offset, length,
  252. (Bool) shouldDelete, requestedType, &actualType,
  253. &actualFormat, &numItems, &bytesLeft, &data) == Success)
  254. && data != nullptr;
  255. }
  256. GetXProperty::~GetXProperty()
  257. {
  258. if (data != nullptr)
  259. XFree (data);
  260. }
  261. } // namespace juce