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.

182 lines
4.5KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2015 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_ZITA_COMMON_HPP_INCLUDED
  18. #define CARLA_ZITA_COMMON_HPP_INCLUDED
  19. #include "CarlaMutex.hpp"
  20. #include "CarlaThread.hpp"
  21. #include "LinkedList.hpp"
  22. #include <png.h>
  23. #include <clxclient.h>
  24. #define EV_X11 16
  25. #define EV_EXIT 31
  26. // -----------------------------------------------------------------------
  27. struct X_handler_Param {
  28. uint32_t index;
  29. float value;
  30. };
  31. typedef LinkedList<X_handler_Param> ParamList;
  32. template<class MainwinType>
  33. class X_handler_thread : public CarlaThread
  34. {
  35. public:
  36. struct SetValueCallback {
  37. virtual ~SetValueCallback() {}
  38. virtual void setParameterValueFromHandlerThread(const uint32_t index, const float value) = 0;
  39. };
  40. X_handler_thread(SetValueCallback* const cb)
  41. : CarlaThread("X_handler"),
  42. fCallback(cb),
  43. fMutex(),
  44. fHandler(nullptr),
  45. fRootwin(nullptr),
  46. fMainwin(nullptr),
  47. fClosed(false),
  48. fParamMutex(),
  49. fParamChanges() {}
  50. void setupAndRun(X_handler* const h, X_rootwin* const r, MainwinType* const m) noexcept
  51. {
  52. const CarlaMutexLocker cml(fMutex);
  53. fHandler = h;
  54. fRootwin = r;
  55. fMainwin = m;
  56. startThread();
  57. }
  58. void stopThread() noexcept
  59. {
  60. signalThreadShouldExit();
  61. {
  62. const CarlaMutexLocker cml(fMutex);
  63. fHandler = nullptr;
  64. fRootwin = nullptr;
  65. fMainwin = nullptr;
  66. }
  67. CarlaThread::stopThread(1000);
  68. }
  69. CarlaMutex& getLock() noexcept
  70. {
  71. return fMutex;
  72. }
  73. void setParameterValueLater(const uint32_t index, const float value) noexcept
  74. {
  75. const CarlaMutexLocker cml(fParamMutex);
  76. for (ParamList::Itenerator it = fParamChanges.begin(); it.valid(); it.next())
  77. {
  78. X_handler_Param& param(it.getValue());
  79. if (param.index != index)
  80. continue;
  81. param.value = value;
  82. return;
  83. }
  84. const X_handler_Param param = { index, value };
  85. fParamChanges.append(param);
  86. }
  87. bool wasClosed() noexcept
  88. {
  89. if (fClosed)
  90. {
  91. fClosed = false;
  92. return true;
  93. }
  94. return false;
  95. }
  96. private:
  97. SetValueCallback* const fCallback;
  98. CarlaMutex fMutex;
  99. X_handler* fHandler;
  100. X_rootwin* fRootwin;
  101. MainwinType* fMainwin;
  102. volatile bool fClosed;
  103. CarlaMutex fParamMutex;
  104. ParamList fParamChanges;
  105. void run() override
  106. {
  107. for (; ! shouldThreadExit();)
  108. {
  109. int ev;
  110. {
  111. const CarlaMutexLocker cml(fMutex);
  112. CARLA_SAFE_ASSERT_RETURN(fMainwin != nullptr,);
  113. {
  114. const CarlaMutexLocker cml(fParamMutex);
  115. for (ParamList::Itenerator it = fParamChanges.begin(); it.valid(); it.next())
  116. {
  117. const X_handler_Param& param(it.getValue());
  118. fCallback->setParameterValueFromHandlerThread(param.index, param.value);
  119. }
  120. fParamChanges.clear();
  121. }
  122. for (; (ev = fMainwin->process()) == EV_X11;)
  123. {
  124. fRootwin->handle_event();
  125. fHandler->next_event();
  126. }
  127. if (ev == Esync::EV_TIME)
  128. {
  129. fRootwin->handle_event();
  130. }
  131. else if (ev == EV_EXIT)
  132. {
  133. fClosed = true;
  134. fHandler = nullptr;
  135. fMainwin = nullptr;
  136. fRootwin = nullptr;
  137. return;
  138. }
  139. }
  140. carla_msleep(10);
  141. }
  142. }
  143. };
  144. // -----------------------------------------------------------------------
  145. #endif // CARLA_ZITA_COMMON_HPP_INCLUDED