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.

373 lines
9.9KB

  1. /*
  2. * Carla Pipe utils
  3. * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_PIPE_UTILS_HPP_INCLUDED
  18. #define CARLA_PIPE_UTILS_HPP_INCLUDED
  19. #include "CarlaJuceUtils.hpp"
  20. #include "CarlaMutex.hpp"
  21. #ifdef BUILDING_CARLA
  22. # include "lv2/atom.h"
  23. #else
  24. # include "lv2/lv2plug.in/ns/ext/atom/atom.h"
  25. #endif
  26. // -----------------------------------------------------------------------
  27. // CarlaPipeCommon class
  28. class CarlaPipeCommon
  29. {
  30. protected:
  31. /*!
  32. * Constructor.
  33. */
  34. CarlaPipeCommon() noexcept;
  35. /*!
  36. * Destructor.
  37. */
  38. virtual ~CarlaPipeCommon() /*noexcept*/;
  39. /*!
  40. * A message has been received (in the context of idlePipe()).
  41. * If extra data is required, use any of the readNextLineAs* functions.
  42. * Returning true means the message has been handled and should not propagate to subclasses.
  43. */
  44. virtual bool msgReceived(const char* const msg) noexcept = 0;
  45. /*!
  46. * An error has occurred during the current requested operation.
  47. * Reimplementing this method allows to catch these errors as strings.
  48. * By default the error is simply printed to stderr.
  49. */
  50. virtual void fail(const char* const error) noexcept
  51. {
  52. carla_stderr2(error);
  53. }
  54. public:
  55. /*!
  56. * Check if the pipe is running.
  57. */
  58. bool isPipeRunning() const noexcept;
  59. /*!
  60. * Check the pipe for new messages and send them to msgReceived().
  61. */
  62. void idlePipe(const bool onlyOnce = false) noexcept;
  63. // -------------------------------------------------------------------
  64. // write lock
  65. /*!
  66. * Lock the pipe write mutex.
  67. */
  68. void lockPipe() const noexcept;
  69. /*!
  70. * Try locking the pipe write mutex.
  71. * Returns true if successful.
  72. */
  73. bool tryLockPipe() const noexcept;
  74. /*!
  75. * Unlock the pipe write mutex.
  76. */
  77. void unlockPipe() const noexcept;
  78. /*!
  79. * Get the pipe write lock.
  80. */
  81. CarlaMutex& getPipeLock() const noexcept;
  82. // -------------------------------------------------------------------
  83. // read lines, must only be called in the context of msgReceived()
  84. /*!
  85. * Read the next line as a boolean.
  86. */
  87. bool readNextLineAsBool(bool& value) const noexcept;
  88. /*!
  89. * Read the next line as a byte.
  90. */
  91. bool readNextLineAsByte(uint8_t& value) const noexcept;
  92. /*!
  93. * Read the next line as an integer.
  94. */
  95. bool readNextLineAsInt(int32_t& value) const noexcept;
  96. /*!
  97. * Read the next line as an unsigned integer.
  98. */
  99. bool readNextLineAsUInt(uint32_t& value) const noexcept;
  100. /*!
  101. * Read the next line as a long integer.
  102. */
  103. bool readNextLineAsLong(int64_t& value) const noexcept;
  104. /*!
  105. * Read the next line as a long unsigned integer.
  106. */
  107. bool readNextLineAsULong(uint64_t& value) const noexcept;
  108. /*!
  109. * Read the next line as a floating point number (single precision).
  110. */
  111. bool readNextLineAsFloat(float& value) const noexcept;
  112. /*!
  113. * Read the next line as a floating point number (double precision).
  114. */
  115. bool readNextLineAsDouble(double& value) const noexcept;
  116. /*!
  117. * Read the next line as a string.
  118. * @note: @a value must be deleted if valid.
  119. */
  120. bool readNextLineAsString(const char*& value) const noexcept;
  121. // -------------------------------------------------------------------
  122. // write messages, must be locked before calling
  123. /*!
  124. * Write a valid message with unknown size.
  125. * A valid message has only one '\n' character and it's at the end.
  126. */
  127. bool writeMessage(const char* const msg) const noexcept;
  128. /*!
  129. * Write a valid message with known size.
  130. * A valid message has only one '\n' character and it's at the end.
  131. */
  132. bool writeMessage(const char* const msg, std::size_t size) const noexcept;
  133. /*!
  134. * Write and fix a message.
  135. */
  136. bool writeAndFixMessage(const char* const msg) const noexcept;
  137. /*!
  138. * Flush all messages currently in cache.
  139. */
  140. bool flushMessages() const noexcept;
  141. // -------------------------------------------------------------------
  142. // write prepared messages, no lock or flush needed (done internally)
  143. /*!
  144. * Write an "error" message.
  145. */
  146. void writeErrorMessage(const char* const error) const noexcept;
  147. /*!
  148. * Write a "control" message used for parameter changes.
  149. */
  150. void writeControlMessage(const uint32_t index, const float value) const noexcept;
  151. /*!
  152. * Write a "configure" message used for state changes.
  153. */
  154. void writeConfigureMessage(const char* const key, const char* const value) const noexcept;
  155. /*!
  156. * Write a "program" message (using index).
  157. */
  158. void writeProgramMessage(const uint32_t index) const noexcept;
  159. /*!
  160. * Write a "midiprogram" message (using bank and program).
  161. */
  162. void writeMidiProgramMessage(const uint32_t bank, const uint32_t program) const noexcept;
  163. /*!
  164. * Write a "reloadprograms" message.
  165. */
  166. void writeReloadProgramsMessage(const int32_t index) const noexcept;
  167. /*!
  168. * Write a MIDI "note" message.
  169. */
  170. void writeMidiNoteMessage(const bool onOff, const uint8_t channel, const uint8_t note, const uint8_t velocity) const noexcept;
  171. /*!
  172. * Write an lv2 "atom" message.
  173. */
  174. void writeLv2AtomMessage(const uint32_t index, const LV2_Atom* const atom) const noexcept;
  175. /*!
  176. * Write an lv2 "urid" message.
  177. */
  178. void writeLv2UridMessage(const uint32_t urid, const char* const uri) const noexcept;
  179. // -------------------------------------------------------------------
  180. protected:
  181. struct PrivateData;
  182. PrivateData* const pData;
  183. // -------------------------------------------------------------------
  184. /*! @internal */
  185. const char* _readline() const noexcept;
  186. /*! @internal */
  187. const char* _readlineblock(const uint32_t timeOutMilliseconds = 50) const noexcept;
  188. /*! @internal */
  189. bool _writeMsgBuffer(const char* const msg, const std::size_t size) const noexcept;
  190. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPipeCommon)
  191. };
  192. // -----------------------------------------------------------------------
  193. // CarlaPipeServer class
  194. class CarlaPipeServer : public CarlaPipeCommon
  195. {
  196. public:
  197. /*!
  198. * Constructor.
  199. */
  200. CarlaPipeServer() noexcept;
  201. /*!
  202. * Destructor.
  203. */
  204. ~CarlaPipeServer() /*noexcept*/ override;
  205. /*!
  206. * Get the process ID of this pipe's matching client.
  207. * Will return 0 if client is not running.
  208. * @note: Unsupported on Windows
  209. */
  210. uintptr_t getPID() const noexcept;
  211. /*!
  212. * Start the pipe server using @a filename with 2 arguments.
  213. * @see fail()
  214. */
  215. bool startPipeServer(const char* const filename, const char* const arg1, const char* const arg2, const int size = -1) noexcept;
  216. /*!
  217. * Stop the pipe server.
  218. * This will send a quit message to the client, wait for it to close for @a timeOutMilliseconds, and close the pipes.
  219. */
  220. void stopPipeServer(const uint32_t timeOutMilliseconds) noexcept;
  221. /*!
  222. * Close the pipes without waiting for the child process to terminate.
  223. */
  224. void closePipeServer() noexcept;
  225. // -------------------------------------------------------------------
  226. // write prepared messages, no lock or flush needed (done internally)
  227. /*!
  228. * Write a single "show" message.
  229. */
  230. void writeShowMessage() const noexcept;
  231. /*!
  232. * Write a single "focus" message.
  233. */
  234. void writeFocusMessage() const noexcept;
  235. /*!
  236. * Write a single "hide" message.
  237. */
  238. void writeHideMessage() const noexcept;
  239. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPipeServer)
  240. };
  241. // -----------------------------------------------------------------------
  242. // CarlaPipeClient class
  243. class CarlaPipeClient : public CarlaPipeCommon
  244. {
  245. public:
  246. /*!
  247. * Constructor.
  248. */
  249. CarlaPipeClient() noexcept;
  250. /*!
  251. * Destructor.
  252. */
  253. ~CarlaPipeClient() /*noexcept*/ override;
  254. /*!
  255. * Initialize the pipes used by a server.
  256. * @a argv must match the arguments set the by server.
  257. */
  258. bool initPipeClient(const char* argv[]) noexcept;
  259. /*!
  260. * Close the pipes.
  261. */
  262. void closePipeClient() noexcept;
  263. // -------------------------------------------------------------------
  264. // write prepared messages, no lock or flush needed (done internally)
  265. /*!
  266. * Write a single "exiting" message and wait for server to respond.
  267. */
  268. void writeExitingMessageAndWait() noexcept;
  269. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPipeClient)
  270. };
  271. // -----------------------------------------------------------------------
  272. // ScopedEnvVar class
  273. class ScopedEnvVar {
  274. public:
  275. ScopedEnvVar(const char* const key, const char* const value) noexcept;
  276. ~ScopedEnvVar() noexcept;
  277. private:
  278. const char* fKey;
  279. const char* fOrigValue;
  280. CARLA_DECLARE_NON_COPY_CLASS(ScopedEnvVar)
  281. CARLA_PREVENT_HEAP_ALLOCATION
  282. };
  283. // -----------------------------------------------------------------------
  284. // ScopedLocale class
  285. class ScopedLocale {
  286. public:
  287. ScopedLocale() noexcept;
  288. ~ScopedLocale() noexcept;
  289. private:
  290. const char* const fLocale;
  291. CARLA_DECLARE_NON_COPY_CLASS(ScopedLocale)
  292. CARLA_PREVENT_HEAP_ALLOCATION
  293. };
  294. // -----------------------------------------------------------------------
  295. #endif // CARLA_PIPE_UTILS_HPP_INCLUDED