Audio plugin host https://kx.studio/carla
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.

juce_linux_X11.cpp 11KB

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