The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

275 lines
8.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #include <poll.h>
  24. enum FdType {
  25. INTERNAL_QUEUE_FD,
  26. WINDOW_SYSTEM_FD,
  27. FD_COUNT,
  28. };
  29. //==============================================================================
  30. class InternalMessageQueue
  31. {
  32. public:
  33. InternalMessageQueue()
  34. : fdCount (1),
  35. loopCount (0),
  36. bytesInSocket (0)
  37. {
  38. int ret = ::socketpair (AF_LOCAL, SOCK_STREAM, 0, fd);
  39. ignoreUnused (ret); jassert (ret == 0);
  40. auto internalQueueCb = [this] (int _fd)
  41. {
  42. if (const MessageManager::MessageBase::Ptr msg = this->popNextMessage (_fd))
  43. {
  44. JUCE_TRY
  45. {
  46. msg->messageCallback();
  47. return true;
  48. }
  49. JUCE_CATCH_EXCEPTION
  50. }
  51. return false;
  52. };
  53. pfds[INTERNAL_QUEUE_FD].fd = getReadHandle();
  54. pfds[INTERNAL_QUEUE_FD].events = POLLIN;
  55. readCallback[INTERNAL_QUEUE_FD] = new LinuxEventLoop::CallbackFunction<decltype(internalQueueCb)> (internalQueueCb);
  56. }
  57. ~InternalMessageQueue()
  58. {
  59. close (getReadHandle());
  60. close (getWriteHandle());
  61. clearSingletonInstance();
  62. }
  63. //==============================================================================
  64. void postMessage (MessageManager::MessageBase* const msg) noexcept
  65. {
  66. ScopedLock sl (lock);
  67. queue.add (msg);
  68. const int maxBytesInSocketQueue = 128;
  69. if (bytesInSocket < maxBytesInSocketQueue)
  70. {
  71. bytesInSocket++;
  72. ScopedUnlock ul (lock);
  73. const unsigned char x = 0xff;
  74. ssize_t bytesWritten = write (getWriteHandle(), &x, 1);
  75. ignoreUnused (bytesWritten);
  76. }
  77. }
  78. void setWindowSystemFd (int _fd, LinuxEventLoop::CallbackFunctionBase* _readCallback)
  79. {
  80. jassert (fdCount == 1);
  81. ScopedLock sl (lock);
  82. fdCount = 2;
  83. pfds[WINDOW_SYSTEM_FD].fd = _fd;
  84. pfds[WINDOW_SYSTEM_FD].events = POLLIN;
  85. readCallback[WINDOW_SYSTEM_FD] = _readCallback;
  86. readCallback[WINDOW_SYSTEM_FD]->active = true;
  87. }
  88. void removeWindowSystemFd ()
  89. {
  90. jassert (fdCount == FD_COUNT);
  91. ScopedLock sl (lock);
  92. fdCount = 1;
  93. readCallback[WINDOW_SYSTEM_FD]->active = false;
  94. }
  95. bool dispatchNextEvent() noexcept
  96. {
  97. for (int counter = 0; counter < fdCount; counter++)
  98. {
  99. const int i = loopCount++;
  100. loopCount %= fdCount;
  101. if (readCallback[i] != nullptr && readCallback[i]->active)
  102. {
  103. if ((*readCallback[i]) (pfds[i].fd))
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. bool sleepUntilEvent (const int timeoutMs)
  110. {
  111. const int pnum = poll (pfds, static_cast<nfds_t> (fdCount), timeoutMs);
  112. return (pnum > 0);
  113. }
  114. //==============================================================================
  115. juce_DeclareSingleton_SingleThreaded_Minimal (InternalMessageQueue)
  116. private:
  117. CriticalSection lock;
  118. ReferenceCountedArray <MessageManager::MessageBase> queue;
  119. int fd[2];
  120. pollfd pfds[FD_COUNT];
  121. ScopedPointer<LinuxEventLoop::CallbackFunctionBase> readCallback[FD_COUNT];
  122. int fdCount;
  123. int loopCount;
  124. int bytesInSocket;
  125. int getWriteHandle() const noexcept { return fd[0]; }
  126. int getReadHandle() const noexcept { return fd[1]; }
  127. MessageManager::MessageBase::Ptr popNextMessage (int _fd) noexcept
  128. {
  129. const ScopedLock sl (lock);
  130. if (bytesInSocket > 0)
  131. {
  132. --bytesInSocket;
  133. const ScopedUnlock ul (lock);
  134. unsigned char x;
  135. ssize_t numBytes = read (_fd, &x, 1);
  136. ignoreUnused (numBytes);
  137. }
  138. return queue.removeAndReturn (0);
  139. }
  140. };
  141. juce_ImplementSingleton_SingleThreaded (InternalMessageQueue)
  142. //==============================================================================
  143. namespace LinuxErrorHandling
  144. {
  145. static bool keyboardBreakOccurred = false;
  146. //==============================================================================
  147. void keyboardBreakSignalHandler (int sig)
  148. {
  149. if (sig == SIGINT)
  150. keyboardBreakOccurred = true;
  151. }
  152. void installKeyboardBreakHandler()
  153. {
  154. struct sigaction saction;
  155. sigset_t maskSet;
  156. sigemptyset (&maskSet);
  157. saction.sa_handler = keyboardBreakSignalHandler;
  158. saction.sa_mask = maskSet;
  159. saction.sa_flags = 0;
  160. sigaction (SIGINT, &saction, 0);
  161. }
  162. }
  163. //==============================================================================
  164. void MessageManager::doPlatformSpecificInitialisation()
  165. {
  166. if (JUCEApplicationBase::isStandaloneApp())
  167. {
  168. LinuxErrorHandling::installKeyboardBreakHandler();
  169. }
  170. // Create the internal message queue
  171. InternalMessageQueue* queue = InternalMessageQueue::getInstance();
  172. ignoreUnused (queue);
  173. }
  174. void MessageManager::doPlatformSpecificShutdown()
  175. {
  176. InternalMessageQueue::deleteInstance();
  177. }
  178. bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
  179. {
  180. if (InternalMessageQueue* queue = InternalMessageQueue::getInstanceWithoutCreating())
  181. {
  182. queue->postMessage (message);
  183. return true;
  184. }
  185. return false;
  186. }
  187. void MessageManager::broadcastMessage (const String& /* value */)
  188. {
  189. /* TODO */
  190. }
  191. // this function expects that it will NEVER be called simultaneously for two concurrent threads
  192. bool MessageManager::dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages)
  193. {
  194. for (;;)
  195. {
  196. if (LinuxErrorHandling::keyboardBreakOccurred)
  197. JUCEApplicationBase::getInstance()->quit();
  198. if (InternalMessageQueue* queue = InternalMessageQueue::getInstanceWithoutCreating())
  199. {
  200. if (queue->dispatchNextEvent())
  201. break;
  202. else if (returnIfNoPendingMessages)
  203. return false;
  204. // wait for 2000ms for next events if necessary
  205. queue->sleepUntilEvent (2000);
  206. }
  207. }
  208. return true;
  209. }
  210. //==============================================================================
  211. void LinuxEventLoop::setWindowSystemFdInternal (int fd, LinuxEventLoop::CallbackFunctionBase* readCallback) noexcept
  212. {
  213. if (InternalMessageQueue* queue = InternalMessageQueue::getInstanceWithoutCreating())
  214. queue->setWindowSystemFd (fd, readCallback);
  215. }
  216. void LinuxEventLoop::removeWindowSystemFd() noexcept
  217. {
  218. if (InternalMessageQueue* queue = InternalMessageQueue::getInstanceWithoutCreating())
  219. queue->removeWindowSystemFd();
  220. }