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.

171 lines
4.2KB

  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. const X_handler_Param param = { index, value };
  77. fParamChanges.append(param);
  78. }
  79. bool wasClosed() noexcept
  80. {
  81. if (fClosed)
  82. {
  83. fClosed = false;
  84. return true;
  85. }
  86. return false;
  87. }
  88. private:
  89. SetValueCallback* const fCallback;
  90. CarlaMutex fMutex;
  91. X_handler* fHandler;
  92. X_rootwin* fRootwin;
  93. MainwinType* fMainwin;
  94. volatile bool fClosed;
  95. CarlaMutex fParamMutex;
  96. ParamList fParamChanges;
  97. void run() override
  98. {
  99. for (; ! shouldThreadExit();)
  100. {
  101. int ev;
  102. {
  103. const CarlaMutexLocker cml(fMutex);
  104. CARLA_SAFE_ASSERT_RETURN(fMainwin != nullptr,);
  105. {
  106. const CarlaMutexLocker cml(fParamMutex);
  107. for (ParamList::Itenerator it = fParamChanges.begin(); it.valid(); it.next())
  108. {
  109. const X_handler_Param& param(it.getValue());
  110. fCallback->setParameterValueFromHandlerThread(param.index, param.value);
  111. }
  112. fParamChanges.clear();
  113. }
  114. for (; (ev = fMainwin->process()) == EV_X11;)
  115. {
  116. fRootwin->handle_event();
  117. fHandler->next_event();
  118. }
  119. if (ev == Esync::EV_TIME)
  120. {
  121. fRootwin->handle_event();
  122. }
  123. else if (ev == EV_EXIT)
  124. {
  125. fClosed = true;
  126. fHandler = nullptr;
  127. fMainwin = nullptr;
  128. fRootwin = nullptr;
  129. return;
  130. }
  131. }
  132. carla_msleep(10);
  133. }
  134. }
  135. };
  136. // -----------------------------------------------------------------------
  137. #endif // CARLA_ZITA_COMMON_HPP_INCLUDED