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.

130 lines
3.0KB

  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 <png.h>
  22. #include <clxclient.h>
  23. #define EV_X11 16
  24. #define EV_EXIT 31
  25. // -----------------------------------------------------------------------
  26. template<class MainwinType>
  27. class X_handler_thread : public CarlaThread
  28. {
  29. public:
  30. X_handler_thread()
  31. : CarlaThread("X_handler"),
  32. fMutex(),
  33. fHandler(nullptr),
  34. fRootwin(nullptr),
  35. fMainwin(nullptr),
  36. fClosed(false) {}
  37. void setupAndRun(X_handler* const h, X_rootwin* const r, MainwinType* const m) noexcept
  38. {
  39. const CarlaMutexLocker cml(fMutex);
  40. fHandler = h;
  41. fRootwin = r;
  42. fMainwin = m;
  43. startThread();
  44. }
  45. void stopThread() noexcept
  46. {
  47. signalThreadShouldExit();
  48. {
  49. const CarlaMutexLocker cml(fMutex);
  50. fHandler = nullptr;
  51. fRootwin = nullptr;
  52. fMainwin = nullptr;
  53. }
  54. CarlaThread::stopThread(1000);
  55. }
  56. CarlaMutex& getLock() noexcept
  57. {
  58. return fMutex;
  59. }
  60. bool wasClosed() noexcept
  61. {
  62. if (fClosed)
  63. {
  64. fClosed = false;
  65. return true;
  66. }
  67. return false;
  68. }
  69. private:
  70. CarlaMutex fMutex;
  71. X_handler* fHandler;
  72. X_rootwin* fRootwin;
  73. MainwinType* fMainwin;
  74. volatile bool fClosed;
  75. void run() override
  76. {
  77. for (; ! shouldThreadExit();)
  78. {
  79. int ev;
  80. {
  81. const CarlaMutexLocker cml(fMutex);
  82. CARLA_SAFE_ASSERT_RETURN(fMainwin != nullptr,);
  83. for (; (ev = fMainwin->process()) == EV_X11;)
  84. {
  85. fRootwin->handle_event();
  86. fHandler->next_event();
  87. }
  88. if (ev == Esync::EV_TIME)
  89. {
  90. fRootwin->handle_event();
  91. }
  92. else if (ev == EV_EXIT)
  93. {
  94. fClosed = true;
  95. fHandler = nullptr;
  96. fMainwin = nullptr;
  97. fRootwin = nullptr;
  98. return;
  99. }
  100. }
  101. carla_msleep(10);
  102. }
  103. }
  104. };
  105. // -----------------------------------------------------------------------
  106. #endif // CARLA_ZITA_COMMON_HPP_INCLUDED