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.

342 lines
11KB

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