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_Messaging.cpp 12KB

10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #if JUCE_DEBUG && ! defined (JUCE_DEBUG_XERRORS)
  18. #define JUCE_DEBUG_XERRORS 1
  19. #endif
  20. Display* display = nullptr;
  21. Window juce_messageWindowHandle = None;
  22. XContext windowHandleXContext; // This is referenced from Windowing.cpp
  23. typedef bool (*WindowMessageReceiveCallback) (XEvent&);
  24. WindowMessageReceiveCallback dispatchWindowMessage = nullptr;
  25. typedef void (*SelectionRequestCallback) (XSelectionRequestEvent&);
  26. SelectionRequestCallback handleSelectionRequest = nullptr;
  27. //==============================================================================
  28. ScopedXLock::ScopedXLock() { XLockDisplay (display); }
  29. ScopedXLock::~ScopedXLock() { XUnlockDisplay (display); }
  30. //==============================================================================
  31. class InternalMessageQueue
  32. {
  33. public:
  34. InternalMessageQueue()
  35. : bytesInSocket (0),
  36. totalEventCount (0)
  37. {
  38. int ret = ::socketpair (AF_LOCAL, SOCK_STREAM, 0, fd);
  39. (void) ret; jassert (ret == 0);
  40. }
  41. ~InternalMessageQueue()
  42. {
  43. close (fd[0]);
  44. close (fd[1]);
  45. clearSingletonInstance();
  46. }
  47. //==============================================================================
  48. void postMessage (MessageManager::MessageBase* const msg)
  49. {
  50. const int maxBytesInSocketQueue = 128;
  51. ScopedLock sl (lock);
  52. queue.add (msg);
  53. if (bytesInSocket < maxBytesInSocketQueue)
  54. {
  55. ++bytesInSocket;
  56. ScopedUnlock ul (lock);
  57. const unsigned char x = 0xff;
  58. size_t bytesWritten = write (fd[0], &x, 1);
  59. (void) bytesWritten;
  60. }
  61. }
  62. bool isEmpty() const
  63. {
  64. ScopedLock sl (lock);
  65. return queue.size() == 0;
  66. }
  67. bool dispatchNextEvent()
  68. {
  69. // This alternates between giving priority to XEvents or internal messages,
  70. // to keep everything running smoothly..
  71. if ((++totalEventCount & 1) != 0)
  72. return dispatchNextXEvent() || dispatchNextInternalMessage();
  73. return dispatchNextInternalMessage() || dispatchNextXEvent();
  74. }
  75. // Wait for an event (either XEvent, or an internal Message)
  76. bool sleepUntilEvent (const int timeoutMs)
  77. {
  78. if (! isEmpty())
  79. return true;
  80. if (display != 0)
  81. {
  82. ScopedXLock xlock;
  83. if (XPending (display))
  84. return true;
  85. }
  86. struct timeval tv;
  87. tv.tv_sec = 0;
  88. tv.tv_usec = timeoutMs * 1000;
  89. int fd0 = getWaitHandle();
  90. int fdmax = fd0;
  91. fd_set readset;
  92. FD_ZERO (&readset);
  93. FD_SET (fd0, &readset);
  94. if (display != 0)
  95. {
  96. ScopedXLock xlock;
  97. int fd1 = XConnectionNumber (display);
  98. FD_SET (fd1, &readset);
  99. fdmax = jmax (fd0, fd1);
  100. }
  101. const int ret = select (fdmax + 1, &readset, 0, 0, &tv);
  102. return (ret > 0); // ret <= 0 if error or timeout
  103. }
  104. //==============================================================================
  105. juce_DeclareSingleton_SingleThreaded_Minimal (InternalMessageQueue);
  106. private:
  107. CriticalSection lock;
  108. ReferenceCountedArray <MessageManager::MessageBase> queue;
  109. int fd[2];
  110. int bytesInSocket;
  111. int totalEventCount;
  112. int getWaitHandle() const noexcept { return fd[1]; }
  113. static bool setNonBlocking (int handle)
  114. {
  115. int socketFlags = fcntl (handle, F_GETFL, 0);
  116. if (socketFlags == -1)
  117. return false;
  118. socketFlags |= O_NONBLOCK;
  119. return fcntl (handle, F_SETFL, socketFlags) == 0;
  120. }
  121. static bool dispatchNextXEvent()
  122. {
  123. if (display == 0)
  124. return false;
  125. XEvent evt;
  126. {
  127. ScopedXLock xlock;
  128. if (! XPending (display))
  129. return false;
  130. XNextEvent (display, &evt);
  131. }
  132. if (evt.type == SelectionRequest && evt.xany.window == juce_messageWindowHandle
  133. && handleSelectionRequest != nullptr)
  134. handleSelectionRequest (evt.xselectionrequest);
  135. else if (evt.xany.window != juce_messageWindowHandle && dispatchWindowMessage != nullptr)
  136. dispatchWindowMessage (evt);
  137. return true;
  138. }
  139. MessageManager::MessageBase::Ptr popNextMessage()
  140. {
  141. const ScopedLock sl (lock);
  142. if (bytesInSocket > 0)
  143. {
  144. --bytesInSocket;
  145. const ScopedUnlock ul (lock);
  146. unsigned char x;
  147. size_t numBytes = read (fd[1], &x, 1);
  148. (void) numBytes;
  149. }
  150. return queue.removeAndReturn (0);
  151. }
  152. bool dispatchNextInternalMessage()
  153. {
  154. if (const MessageManager::MessageBase::Ptr msg = popNextMessage())
  155. {
  156. JUCE_TRY
  157. {
  158. msg->messageCallback();
  159. return true;
  160. }
  161. JUCE_CATCH_EXCEPTION
  162. }
  163. return false;
  164. }
  165. };
  166. juce_ImplementSingleton_SingleThreaded (InternalMessageQueue);
  167. //==============================================================================
  168. namespace LinuxErrorHandling
  169. {
  170. static bool errorOccurred = false;
  171. static bool keyboardBreakOccurred = false;
  172. static XErrorHandler oldErrorHandler = (XErrorHandler) 0;
  173. static XIOErrorHandler oldIOErrorHandler = (XIOErrorHandler) 0;
  174. //==============================================================================
  175. // Usually happens when client-server connection is broken
  176. int ioErrorHandler (Display*)
  177. {
  178. DBG ("ERROR: connection to X server broken.. terminating.");
  179. if (JUCEApplicationBase::isStandaloneApp())
  180. MessageManager::getInstance()->stopDispatchLoop();
  181. errorOccurred = true;
  182. return 0;
  183. }
  184. int errorHandler (Display* display, XErrorEvent* event)
  185. {
  186. #if JUCE_DEBUG_XERRORS
  187. char errorStr[64] = { 0 };
  188. char requestStr[64] = { 0 };
  189. XGetErrorText (display, event->error_code, errorStr, 64);
  190. XGetErrorDatabaseText (display, "XRequest", String (event->request_code).toUTF8(), "Unknown", requestStr, 64);
  191. DBG ("ERROR: X returned " << errorStr << " for operation " << requestStr);
  192. #endif
  193. return 0;
  194. }
  195. void installXErrorHandlers()
  196. {
  197. oldIOErrorHandler = XSetIOErrorHandler (ioErrorHandler);
  198. oldErrorHandler = XSetErrorHandler (errorHandler);
  199. }
  200. void removeXErrorHandlers()
  201. {
  202. if (JUCEApplicationBase::isStandaloneApp())
  203. {
  204. XSetIOErrorHandler (oldIOErrorHandler);
  205. oldIOErrorHandler = 0;
  206. XSetErrorHandler (oldErrorHandler);
  207. oldErrorHandler = 0;
  208. }
  209. }
  210. //==============================================================================
  211. void keyboardBreakSignalHandler (int sig)
  212. {
  213. if (sig == SIGINT)
  214. keyboardBreakOccurred = true;
  215. }
  216. void installKeyboardBreakHandler()
  217. {
  218. struct sigaction saction;
  219. sigset_t maskSet;
  220. sigemptyset (&maskSet);
  221. saction.sa_handler = keyboardBreakSignalHandler;
  222. saction.sa_mask = maskSet;
  223. saction.sa_flags = 0;
  224. sigaction (SIGINT, &saction, 0);
  225. }
  226. }
  227. //==============================================================================
  228. void MessageManager::doPlatformSpecificInitialisation()
  229. {
  230. if (JUCEApplicationBase::isStandaloneApp())
  231. {
  232. // Initialise xlib for multiple thread support
  233. static bool initThreadCalled = false;
  234. if (! initThreadCalled)
  235. {
  236. if (! XInitThreads())
  237. {
  238. // This is fatal! Print error and closedown
  239. Logger::outputDebugString ("Failed to initialise xlib thread support.");
  240. Process::terminate();
  241. return;
  242. }
  243. initThreadCalled = true;
  244. }
  245. LinuxErrorHandling::installXErrorHandlers();
  246. LinuxErrorHandling::installKeyboardBreakHandler();
  247. }
  248. // Create the internal message queue
  249. InternalMessageQueue::getInstance();
  250. // Try to connect to a display
  251. String displayName (getenv ("DISPLAY"));
  252. if (displayName.isEmpty())
  253. displayName = ":0.0";
  254. display = XOpenDisplay (displayName.toUTF8());
  255. if (display != 0) // This is not fatal! we can run headless.
  256. {
  257. // Create a context to store user data associated with Windows we create
  258. windowHandleXContext = XUniqueContext();
  259. // We're only interested in client messages for this window, which are always sent
  260. XSetWindowAttributes swa;
  261. swa.event_mask = NoEventMask;
  262. // Create our message window (this will never be mapped)
  263. const int screen = DefaultScreen (display);
  264. juce_messageWindowHandle = XCreateWindow (display, RootWindow (display, screen),
  265. 0, 0, 1, 1, 0, 0, InputOnly,
  266. DefaultVisual (display, screen),
  267. CWEventMask, &swa);
  268. }
  269. }
  270. void MessageManager::doPlatformSpecificShutdown()
  271. {
  272. InternalMessageQueue::deleteInstance();
  273. if (display != 0 && ! LinuxErrorHandling::errorOccurred)
  274. {
  275. XDestroyWindow (display, juce_messageWindowHandle);
  276. XCloseDisplay (display);
  277. juce_messageWindowHandle = 0;
  278. display = nullptr;
  279. LinuxErrorHandling::removeXErrorHandlers();
  280. }
  281. }
  282. bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
  283. {
  284. if (LinuxErrorHandling::errorOccurred)
  285. return false;
  286. if (InternalMessageQueue* const queue = InternalMessageQueue::getInstanceWithoutCreating())
  287. {
  288. queue->postMessage (message);
  289. return true;
  290. }
  291. return false;
  292. }
  293. void MessageManager::broadcastMessage (const String& /* value */)
  294. {
  295. /* TODO */
  296. }
  297. // this function expects that it will NEVER be called simultaneously for two concurrent threads
  298. bool MessageManager::dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages)
  299. {
  300. while (! LinuxErrorHandling::errorOccurred)
  301. {
  302. if (LinuxErrorHandling::keyboardBreakOccurred)
  303. {
  304. LinuxErrorHandling::errorOccurred = true;
  305. if (JUCEApplicationBase::isStandaloneApp())
  306. Process::terminate();
  307. break;
  308. }
  309. InternalMessageQueue* const queue = InternalMessageQueue::getInstanceWithoutCreating();
  310. jassert (queue != nullptr);
  311. if (queue->dispatchNextEvent())
  312. return true;
  313. if (returnIfNoPendingMessages)
  314. break;
  315. queue->sleepUntilEvent (2000);
  316. }
  317. return false;
  318. }