juce_linux_X11.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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::registerFdCallback (fd,
  148. [this](int)
  149. {
  150. do
  151. {
  152. XEvent evt;
  153. {
  154. ScopedXLock xlock (display);
  155. if (! XPending (display))
  156. return;
  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. });
  171. }
  172. void XWindowSystem::destroyXDisplay() noexcept
  173. {
  174. ScopedXLock xlock (display);
  175. XDestroyWindow (display, juce_messageWindowHandle);
  176. juce_messageWindowHandle = 0;
  177. XSync (display, True);
  178. LinuxEventLoop::unregisterFdCallback (XConnectionNumber (display));
  179. }
  180. JUCE_IMPLEMENT_SINGLETON (XWindowSystem)
  181. //==============================================================================
  182. ScopedXDisplay::ScopedXDisplay() : display (XWindowSystem::getInstance()->displayRef())
  183. {
  184. }
  185. ScopedXDisplay::~ScopedXDisplay()
  186. {
  187. XWindowSystem::getInstance()->displayUnref();
  188. }
  189. //==============================================================================
  190. ScopedXLock::ScopedXLock (::Display* d) : display (d)
  191. {
  192. if (display != nullptr)
  193. XLockDisplay (display);
  194. }
  195. ScopedXLock::~ScopedXLock()
  196. {
  197. if (display != nullptr)
  198. XUnlockDisplay (display);
  199. }
  200. //==============================================================================
  201. Atoms::Atoms (::Display* display)
  202. {
  203. protocols = getIfExists (display, "WM_PROTOCOLS");
  204. protocolList [TAKE_FOCUS] = getIfExists (display, "WM_TAKE_FOCUS");
  205. protocolList [DELETE_WINDOW] = getIfExists (display, "WM_DELETE_WINDOW");
  206. protocolList [PING] = getIfExists (display, "_NET_WM_PING");
  207. changeState = getIfExists (display, "WM_CHANGE_STATE");
  208. state = getIfExists (display, "WM_STATE");
  209. userTime = getCreating (display, "_NET_WM_USER_TIME");
  210. activeWin = getCreating (display, "_NET_ACTIVE_WINDOW");
  211. pid = getCreating (display, "_NET_WM_PID");
  212. windowType = getIfExists (display, "_NET_WM_WINDOW_TYPE");
  213. windowState = getIfExists (display, "_NET_WM_STATE");
  214. XdndAware = getCreating (display, "XdndAware");
  215. XdndEnter = getCreating (display, "XdndEnter");
  216. XdndLeave = getCreating (display, "XdndLeave");
  217. XdndPosition = getCreating (display, "XdndPosition");
  218. XdndStatus = getCreating (display, "XdndStatus");
  219. XdndDrop = getCreating (display, "XdndDrop");
  220. XdndFinished = getCreating (display, "XdndFinished");
  221. XdndSelection = getCreating (display, "XdndSelection");
  222. XdndTypeList = getCreating (display, "XdndTypeList");
  223. XdndActionList = getCreating (display, "XdndActionList");
  224. XdndActionCopy = getCreating (display, "XdndActionCopy");
  225. XdndActionPrivate = getCreating (display, "XdndActionPrivate");
  226. XdndActionDescription = getCreating (display, "XdndActionDescription");
  227. XembedMsgType = getCreating (display, "_XEMBED");
  228. XembedInfo = getCreating (display, "_XEMBED_INFO");
  229. allowedMimeTypes[0] = getCreating (display, "UTF8_STRING");
  230. allowedMimeTypes[1] = getCreating (display, "text/plain;charset=utf-8");
  231. allowedMimeTypes[2] = getCreating (display, "text/plain");
  232. allowedMimeTypes[3] = getCreating (display, "text/uri-list");
  233. allowedActions[0] = getCreating (display, "XdndActionMove");
  234. allowedActions[1] = XdndActionCopy;
  235. allowedActions[2] = getCreating (display, "XdndActionLink");
  236. allowedActions[3] = getCreating (display, "XdndActionAsk");
  237. allowedActions[4] = XdndActionPrivate;
  238. }
  239. Atom Atoms::getIfExists (::Display* display, const char* name) { return XInternAtom (display, name, True); }
  240. Atom Atoms::getCreating (::Display* display, const char* name) { return XInternAtom (display, name, False); }
  241. String Atoms::getName (::Display* display, const Atom atom)
  242. {
  243. if (atom == None)
  244. return "None";
  245. return String (XGetAtomName (display, atom));
  246. }
  247. bool Atoms::isMimeTypeFile (::Display* display, const Atom atom)
  248. {
  249. return getName (display, atom).equalsIgnoreCase ("text/uri-list");
  250. }
  251. const unsigned long Atoms::DndVersion = 3;
  252. //==============================================================================
  253. GetXProperty::GetXProperty (::Display* display, Window window, Atom atom,
  254. long offset, long length, bool shouldDelete,
  255. Atom requestedType)
  256. {
  257. success = (XGetWindowProperty (display, window, atom, offset, length,
  258. (Bool) shouldDelete, requestedType, &actualType,
  259. &actualFormat, &numItems, &bytesLeft, &data) == Success)
  260. && data != nullptr;
  261. }
  262. GetXProperty::~GetXProperty()
  263. {
  264. if (data != nullptr)
  265. XFree (data);
  266. }
  267. } // namespace juce