juce_linux_Messaging.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. class InternalMessageQueue
  21. {
  22. public:
  23. InternalMessageQueue()
  24. {
  25. auto err = ::socketpair (AF_LOCAL, SOCK_STREAM, 0, msgpipe);
  26. jassert (err == 0);
  27. ignoreUnused (err);
  28. LinuxEventLoop::registerFdCallback (getReadHandle(),
  29. [this] (int fd)
  30. {
  31. while (auto msg = popNextMessage (fd))
  32. {
  33. JUCE_TRY
  34. {
  35. msg->messageCallback();
  36. }
  37. JUCE_CATCH_EXCEPTION
  38. }
  39. });
  40. }
  41. ~InternalMessageQueue()
  42. {
  43. LinuxEventLoop::unregisterFdCallback (getReadHandle());
  44. close (getReadHandle());
  45. close (getWriteHandle());
  46. clearSingletonInstance();
  47. }
  48. //==============================================================================
  49. void postMessage (MessageManager::MessageBase* const msg) noexcept
  50. {
  51. ScopedLock sl (lock);
  52. queue.add (msg);
  53. if (bytesInSocket < maxBytesInSocketQueue)
  54. {
  55. bytesInSocket++;
  56. ScopedUnlock ul (lock);
  57. unsigned char x = 0xff;
  58. auto numBytes = write (getWriteHandle(), &x, 1);
  59. ignoreUnused (numBytes);
  60. }
  61. }
  62. //==============================================================================
  63. JUCE_DECLARE_SINGLETON (InternalMessageQueue, false)
  64. private:
  65. CriticalSection lock;
  66. ReferenceCountedArray <MessageManager::MessageBase> queue;
  67. int msgpipe[2];
  68. int bytesInSocket = 0;
  69. static constexpr int maxBytesInSocketQueue = 128;
  70. int getWriteHandle() const noexcept { return msgpipe[0]; }
  71. int getReadHandle() const noexcept { return msgpipe[1]; }
  72. MessageManager::MessageBase::Ptr popNextMessage (int fd) noexcept
  73. {
  74. const ScopedLock sl (lock);
  75. if (bytesInSocket > 0)
  76. {
  77. --bytesInSocket;
  78. ScopedUnlock ul (lock);
  79. unsigned char x;
  80. auto numBytes = read (fd, &x, 1);
  81. ignoreUnused (numBytes);
  82. }
  83. return queue.removeAndReturn (0);
  84. }
  85. };
  86. JUCE_IMPLEMENT_SINGLETON (InternalMessageQueue)
  87. //==============================================================================
  88. struct InternalRunLoop
  89. {
  90. public:
  91. InternalRunLoop()
  92. {
  93. fdReadCallbacks.reserve (8);
  94. }
  95. void registerFdCallback (int fd, std::function<void(int)>&& cb, short eventMask)
  96. {
  97. const ScopedLock sl (lock);
  98. fdReadCallbacks.push_back ({ fd, std::move (cb) });
  99. pfds.push_back ({ fd, eventMask, 0 });
  100. }
  101. void unregisterFdCallback (int fd)
  102. {
  103. const ScopedLock sl (lock);
  104. {
  105. auto removePredicate = [=] (const std::pair<int, std::function<void(int)>>& cb) { return cb.first == fd; };
  106. fdReadCallbacks.erase (std::remove_if (std::begin (fdReadCallbacks), std::end (fdReadCallbacks), removePredicate),
  107. std::end (fdReadCallbacks));
  108. }
  109. {
  110. auto removePredicate = [=] (const pollfd& pfd) { return pfd.fd == fd; };
  111. pfds.erase (std::remove_if (std::begin (pfds), std::end (pfds), removePredicate),
  112. std::end (pfds));
  113. }
  114. }
  115. bool dispatchPendingEvents()
  116. {
  117. const ScopedLock sl (lock);
  118. if (poll (&pfds.front(), static_cast<nfds_t> (pfds.size()), 0) == 0)
  119. return false;
  120. bool eventWasSent = false;
  121. for (auto& pfd : pfds)
  122. {
  123. if (pfd.revents == 0)
  124. continue;
  125. pfd.revents = 0;
  126. auto fd = pfd.fd;
  127. for (auto& fdAndCallback : fdReadCallbacks)
  128. {
  129. if (fdAndCallback.first == fd)
  130. {
  131. fdAndCallback.second (fd);
  132. eventWasSent = true;
  133. }
  134. }
  135. }
  136. return eventWasSent;
  137. }
  138. void sleepUntilNextEvent (int timeoutMs)
  139. {
  140. poll (&pfds.front(), static_cast<nfds_t> (pfds.size()), timeoutMs);
  141. }
  142. //==============================================================================
  143. JUCE_DECLARE_SINGLETON (InternalRunLoop, false)
  144. private:
  145. CriticalSection lock;
  146. std::vector<std::pair<int, std::function<void(int)>>> fdReadCallbacks;
  147. std::vector<pollfd> pfds;
  148. };
  149. JUCE_IMPLEMENT_SINGLETON (InternalRunLoop)
  150. //==============================================================================
  151. namespace LinuxErrorHandling
  152. {
  153. static bool keyboardBreakOccurred = false;
  154. void keyboardBreakSignalHandler (int sig)
  155. {
  156. if (sig == SIGINT)
  157. keyboardBreakOccurred = true;
  158. }
  159. void installKeyboardBreakHandler()
  160. {
  161. struct sigaction saction;
  162. sigset_t maskSet;
  163. sigemptyset (&maskSet);
  164. saction.sa_handler = keyboardBreakSignalHandler;
  165. saction.sa_mask = maskSet;
  166. saction.sa_flags = 0;
  167. sigaction (SIGINT, &saction, nullptr);
  168. }
  169. }
  170. //==============================================================================
  171. void MessageManager::doPlatformSpecificInitialisation()
  172. {
  173. if (JUCEApplicationBase::isStandaloneApp())
  174. LinuxErrorHandling::installKeyboardBreakHandler();
  175. InternalRunLoop::getInstance();
  176. InternalMessageQueue::getInstance();
  177. }
  178. void MessageManager::doPlatformSpecificShutdown()
  179. {
  180. InternalMessageQueue::deleteInstance();
  181. InternalRunLoop::deleteInstance();
  182. }
  183. bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
  184. {
  185. if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
  186. {
  187. queue->postMessage (message);
  188. return true;
  189. }
  190. return false;
  191. }
  192. void MessageManager::broadcastMessage (const String&)
  193. {
  194. // TODO
  195. }
  196. // this function expects that it will NEVER be called simultaneously for two concurrent threads
  197. bool MessageManager::dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages)
  198. {
  199. for (;;)
  200. {
  201. if (LinuxErrorHandling::keyboardBreakOccurred)
  202. JUCEApplicationBase::quit();
  203. if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
  204. {
  205. if (runLoop->dispatchPendingEvents())
  206. break;
  207. if (returnIfNoPendingMessages)
  208. return false;
  209. runLoop->sleepUntilNextEvent (2000);
  210. }
  211. }
  212. return true;
  213. }
  214. //==============================================================================
  215. void LinuxEventLoop::registerFdCallback (int fd, std::function<void(int)> readCallback, short eventMask)
  216. {
  217. if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
  218. runLoop->registerFdCallback (fd, std::move (readCallback), eventMask);
  219. }
  220. void LinuxEventLoop::unregisterFdCallback (int fd)
  221. {
  222. if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
  223. runLoop->unregisterFdCallback (fd);
  224. }
  225. } // namespace juce