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 7.5KB

7 years ago
7 years ago
7 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
7 years ago
10 years ago
10 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #include <poll.h>
  18. enum FdType
  19. {
  20. INTERNAL_QUEUE_FD,
  21. WINDOW_SYSTEM_FD,
  22. FD_COUNT,
  23. };
  24. namespace juce
  25. {
  26. //==============================================================================
  27. class InternalMessageQueue
  28. {
  29. public:
  30. InternalMessageQueue()
  31. {
  32. auto ret = ::socketpair (AF_LOCAL, SOCK_STREAM, 0, fd);
  33. ignoreUnused (ret); jassert (ret == 0);
  34. auto internalQueueCb = [this] (int _fd)
  35. {
  36. if (const MessageManager::MessageBase::Ptr msg = this->popNextMessage (_fd))
  37. {
  38. JUCE_TRY
  39. {
  40. msg->messageCallback();
  41. return true;
  42. }
  43. JUCE_CATCH_EXCEPTION
  44. }
  45. return false;
  46. };
  47. pfds[INTERNAL_QUEUE_FD].fd = getReadHandle();
  48. pfds[INTERNAL_QUEUE_FD].events = POLLIN;
  49. readCallback[INTERNAL_QUEUE_FD] = new LinuxEventLoop::CallbackFunction<decltype(internalQueueCb)> (internalQueueCb);
  50. }
  51. ~InternalMessageQueue()
  52. {
  53. close (getReadHandle());
  54. close (getWriteHandle());
  55. clearSingletonInstance();
  56. }
  57. //==============================================================================
  58. void postMessage (MessageManager::MessageBase* const msg) noexcept
  59. {
  60. ScopedLock sl (lock);
  61. queue.add (msg);
  62. const int maxBytesInSocketQueue = 128;
  63. if (bytesInSocket < maxBytesInSocketQueue)
  64. {
  65. bytesInSocket++;
  66. ScopedUnlock ul (lock);
  67. const unsigned char x = 0xff;
  68. ssize_t bytesWritten = write (getWriteHandle(), &x, 1);
  69. ignoreUnused (bytesWritten);
  70. }
  71. }
  72. void setWindowSystemFd (int _fd, LinuxEventLoop::CallbackFunctionBase* _readCallback)
  73. {
  74. jassert (fdCount == 1);
  75. ScopedLock sl (lock);
  76. fdCount = 2;
  77. pfds[WINDOW_SYSTEM_FD].fd = _fd;
  78. pfds[WINDOW_SYSTEM_FD].events = POLLIN;
  79. readCallback[WINDOW_SYSTEM_FD] = _readCallback;
  80. readCallback[WINDOW_SYSTEM_FD]->active = true;
  81. }
  82. void removeWindowSystemFd()
  83. {
  84. jassert (fdCount == FD_COUNT);
  85. ScopedLock sl (lock);
  86. fdCount = 1;
  87. readCallback[WINDOW_SYSTEM_FD]->active = false;
  88. }
  89. bool dispatchNextEvent() noexcept
  90. {
  91. for (int counter = 0; counter < fdCount; counter++)
  92. {
  93. const int i = loopCount++;
  94. loopCount %= fdCount;
  95. if (readCallback[i] != nullptr && readCallback[i]->active)
  96. if ((*readCallback[i]) (pfds[i].fd))
  97. return true;
  98. }
  99. return false;
  100. }
  101. bool sleepUntilEvent (const int timeoutMs)
  102. {
  103. const int pnum = poll (pfds, static_cast<nfds_t> (fdCount), timeoutMs);
  104. return (pnum > 0);
  105. }
  106. //==============================================================================
  107. juce_DeclareSingleton_SingleThreaded_Minimal (InternalMessageQueue)
  108. private:
  109. CriticalSection lock;
  110. ReferenceCountedArray <MessageManager::MessageBase> queue;
  111. int fd[2];
  112. pollfd pfds[FD_COUNT];
  113. ScopedPointer<LinuxEventLoop::CallbackFunctionBase> readCallback[FD_COUNT];
  114. int fdCount = 1;
  115. int loopCount = 0;
  116. int bytesInSocket = 0;
  117. int getWriteHandle() const noexcept { return fd[0]; }
  118. int getReadHandle() const noexcept { return fd[1]; }
  119. MessageManager::MessageBase::Ptr popNextMessage (int _fd) noexcept
  120. {
  121. const ScopedLock sl (lock);
  122. if (bytesInSocket > 0)
  123. {
  124. --bytesInSocket;
  125. const ScopedUnlock ul (lock);
  126. unsigned char x;
  127. ssize_t numBytes = read (_fd, &x, 1);
  128. ignoreUnused (numBytes);
  129. }
  130. return queue.removeAndReturn (0);
  131. }
  132. };
  133. juce_ImplementSingleton_SingleThreaded (InternalMessageQueue)
  134. //==============================================================================
  135. namespace LinuxErrorHandling
  136. {
  137. static bool keyboardBreakOccurred = false;
  138. //==============================================================================
  139. void keyboardBreakSignalHandler (int sig)
  140. {
  141. if (sig == SIGINT)
  142. keyboardBreakOccurred = true;
  143. }
  144. void installKeyboardBreakHandler()
  145. {
  146. struct sigaction saction;
  147. sigset_t maskSet;
  148. sigemptyset (&maskSet);
  149. saction.sa_handler = keyboardBreakSignalHandler;
  150. saction.sa_mask = maskSet;
  151. saction.sa_flags = 0;
  152. sigaction (SIGINT, &saction, 0);
  153. }
  154. }
  155. //==============================================================================
  156. void MessageManager::doPlatformSpecificInitialisation()
  157. {
  158. if (JUCEApplicationBase::isStandaloneApp())
  159. LinuxErrorHandling::installKeyboardBreakHandler();
  160. // Create the internal message queue
  161. auto* queue = InternalMessageQueue::getInstance();
  162. ignoreUnused (queue);
  163. }
  164. void MessageManager::doPlatformSpecificShutdown()
  165. {
  166. InternalMessageQueue::deleteInstance();
  167. }
  168. bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
  169. {
  170. if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
  171. {
  172. queue->postMessage (message);
  173. return true;
  174. }
  175. return false;
  176. }
  177. void MessageManager::broadcastMessage (const String&)
  178. {
  179. // TODO
  180. }
  181. // this function expects that it will NEVER be called simultaneously for two concurrent threads
  182. bool MessageManager::dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages)
  183. {
  184. for (;;)
  185. {
  186. if (LinuxErrorHandling::keyboardBreakOccurred)
  187. JUCEApplicationBase::getInstance()->quit();
  188. if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
  189. {
  190. if (queue->dispatchNextEvent())
  191. break;
  192. if (returnIfNoPendingMessages)
  193. return false;
  194. // wait for 2000ms for next events if necessary
  195. queue->sleepUntilEvent (2000);
  196. }
  197. }
  198. return true;
  199. }
  200. //==============================================================================
  201. void LinuxEventLoop::setWindowSystemFdInternal (int fd, LinuxEventLoop::CallbackFunctionBase* readCallback) noexcept
  202. {
  203. if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
  204. queue->setWindowSystemFd (fd, readCallback);
  205. }
  206. void LinuxEventLoop::removeWindowSystemFd() noexcept
  207. {
  208. if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
  209. queue->removeWindowSystemFd();
  210. }
  211. } // namespace juce