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.

267 lines
7.7KB

  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. INTERNAL_QUEUE_FD,
  20. WINDOW_SYSTEM_FD,
  21. FD_COUNT,
  22. };
  23. //==============================================================================
  24. class InternalMessageQueue
  25. {
  26. public:
  27. InternalMessageQueue()
  28. : fdCount (1),
  29. loopCount (0),
  30. bytesInSocket (0)
  31. {
  32. int 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. {
  97. if ((*readCallback[i]) (pfds[i].fd))
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. bool sleepUntilEvent (const int timeoutMs)
  104. {
  105. const int pnum = poll (pfds, static_cast<nfds_t> (fdCount), timeoutMs);
  106. return (pnum > 0);
  107. }
  108. //==============================================================================
  109. juce_DeclareSingleton_SingleThreaded_Minimal (InternalMessageQueue)
  110. private:
  111. CriticalSection lock;
  112. ReferenceCountedArray <MessageManager::MessageBase> queue;
  113. int fd[2];
  114. pollfd pfds[FD_COUNT];
  115. ScopedPointer<LinuxEventLoop::CallbackFunctionBase> readCallback[FD_COUNT];
  116. int fdCount;
  117. int loopCount;
  118. int bytesInSocket;
  119. int getWriteHandle() const noexcept { return fd[0]; }
  120. int getReadHandle() const noexcept { return fd[1]; }
  121. MessageManager::MessageBase::Ptr popNextMessage (int _fd) noexcept
  122. {
  123. const ScopedLock sl (lock);
  124. if (bytesInSocket > 0)
  125. {
  126. --bytesInSocket;
  127. const ScopedUnlock ul (lock);
  128. unsigned char x;
  129. ssize_t numBytes = read (_fd, &x, 1);
  130. ignoreUnused (numBytes);
  131. }
  132. return queue.removeAndReturn (0);
  133. }
  134. };
  135. juce_ImplementSingleton_SingleThreaded (InternalMessageQueue)
  136. //==============================================================================
  137. namespace LinuxErrorHandling
  138. {
  139. static bool keyboardBreakOccurred = false;
  140. //==============================================================================
  141. void keyboardBreakSignalHandler (int sig)
  142. {
  143. if (sig == SIGINT)
  144. keyboardBreakOccurred = true;
  145. }
  146. void installKeyboardBreakHandler()
  147. {
  148. struct sigaction saction;
  149. sigset_t maskSet;
  150. sigemptyset (&maskSet);
  151. saction.sa_handler = keyboardBreakSignalHandler;
  152. saction.sa_mask = maskSet;
  153. saction.sa_flags = 0;
  154. sigaction (SIGINT, &saction, 0);
  155. }
  156. }
  157. //==============================================================================
  158. void MessageManager::doPlatformSpecificInitialisation()
  159. {
  160. if (JUCEApplicationBase::isStandaloneApp())
  161. {
  162. LinuxErrorHandling::installKeyboardBreakHandler();
  163. }
  164. // Create the internal message queue
  165. InternalMessageQueue* queue = InternalMessageQueue::getInstance();
  166. ignoreUnused (queue);
  167. }
  168. void MessageManager::doPlatformSpecificShutdown()
  169. {
  170. InternalMessageQueue::deleteInstance();
  171. }
  172. bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
  173. {
  174. if (InternalMessageQueue* queue = InternalMessageQueue::getInstanceWithoutCreating())
  175. {
  176. queue->postMessage (message);
  177. return true;
  178. }
  179. return false;
  180. }
  181. void MessageManager::broadcastMessage (const String& /* value */)
  182. {
  183. /* TODO */
  184. }
  185. // this function expects that it will NEVER be called simultaneously for two concurrent threads
  186. bool MessageManager::dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages)
  187. {
  188. for (;;)
  189. {
  190. if (LinuxErrorHandling::keyboardBreakOccurred)
  191. JUCEApplicationBase::getInstance()->quit();
  192. if (InternalMessageQueue* queue = InternalMessageQueue::getInstanceWithoutCreating())
  193. {
  194. if (queue->dispatchNextEvent())
  195. break;
  196. else if (returnIfNoPendingMessages)
  197. return false;
  198. // wait for 2000ms for next events if necessary
  199. queue->sleepUntilEvent (2000);
  200. }
  201. }
  202. return true;
  203. }
  204. //==============================================================================
  205. void LinuxEventLoop::setWindowSystemFdInternal (int fd, LinuxEventLoop::CallbackFunctionBase* readCallback) noexcept
  206. {
  207. if (InternalMessageQueue* queue = InternalMessageQueue::getInstanceWithoutCreating())
  208. queue->setWindowSystemFd (fd, readCallback);
  209. }
  210. void LinuxEventLoop::removeWindowSystemFd() noexcept
  211. {
  212. if (InternalMessageQueue* queue = InternalMessageQueue::getInstanceWithoutCreating())
  213. queue->removeWindowSystemFd();
  214. }