DISTRHO Plugin Framework
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.

293 lines
6.7KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DGL_NTK_APP_HPP_INCLUDED
  17. #define DGL_NTK_APP_HPP_INCLUDED
  18. #include "../Base.hpp"
  19. #include "../../distrho/DistrhoUI.hpp"
  20. #include "../../distrho/extra/d_thread.hpp"
  21. #ifdef override
  22. # define override_defined
  23. # undef override
  24. #endif
  25. #include <list>
  26. #include <FL/Fl.H>
  27. #include <FL/Fl_Double_Window.H>
  28. #include <FL/Fl_Shared_Image.H>
  29. #include <FL/x.H>
  30. #ifdef override_defined
  31. # define override
  32. # undef override_defined
  33. #endif
  34. // -----------------------------------------------------------------------
  35. namespace DISTRHO_NAMESPACE {
  36. class UI;
  37. }
  38. struct FlScopedLock {
  39. FlScopedLock() { Fl::lock(); }
  40. ~FlScopedLock() { Fl::unlock(); }
  41. };
  42. // -----------------------------------------------------------------------
  43. START_NAMESPACE_DGL
  44. class NtkWindow;
  45. typedef DISTRHO_NAMESPACE::Mutex d_Mutex;
  46. typedef DISTRHO_NAMESPACE::MutexLocker d_MutexLocker;
  47. typedef DISTRHO_NAMESPACE::Thread d_Thread;
  48. typedef DISTRHO_NAMESPACE::UI d_UI;
  49. // -----------------------------------------------------------------------
  50. /**
  51. DGL compatible App class that uses NTK instead of OpenGL.
  52. @see App
  53. */
  54. class NtkApp : d_Thread
  55. {
  56. public:
  57. /**
  58. Constructor.
  59. */
  60. NtkApp()
  61. : d_Thread("NtkApp"),
  62. fWindows(),
  63. fWindowMutex(),
  64. fNextUI(),
  65. fDoNextUI(false),
  66. fThreadInitialized(false)
  67. {
  68. #ifdef DISTRHO_OS_LINUX
  69. XInitThreads();
  70. #endif
  71. startThread();
  72. for (; ! fThreadInitialized;)
  73. d_msleep(10);
  74. }
  75. /**
  76. Destructor.
  77. */
  78. ~NtkApp()
  79. {
  80. stopThread(-1);
  81. fWindows.clear();
  82. }
  83. /**
  84. Idle function.
  85. This calls does nothing.
  86. */
  87. void idle() {}
  88. /**
  89. Run the application event-loop until all Windows are closed.
  90. @note: This function is meant for standalones only, *never* call this from plugins.
  91. */
  92. void exec()
  93. {
  94. while (isThreadRunning() && ! shouldThreadExit())
  95. d_sleep(1);
  96. }
  97. /**
  98. Quit the application.
  99. This stops the event-loop and closes all Windows.
  100. */
  101. void quit()
  102. {
  103. signalThreadShouldExit();
  104. }
  105. /**
  106. Check if the application is about to quit.
  107. Returning true means there's no event-loop running at the moment.
  108. */
  109. bool isQuiting() const noexcept
  110. {
  111. if (isThreadRunning() && ! shouldThreadExit())
  112. return false;
  113. return true;
  114. }
  115. // -------------------------------------------------------------------
  116. /**
  117. Create UI on our separate thread.
  118. Blocks until the UI is created and returns it.
  119. */
  120. d_UI* createUI(void* const func)
  121. {
  122. DISTRHO_SAFE_ASSERT_RETURN(isThreadRunning(), nullptr);
  123. DISTRHO_SAFE_ASSERT_RETURN(! fDoNextUI, nullptr);
  124. fNextUI.create = true;
  125. fNextUI.func = (NextUI::UiFunc)func;
  126. fDoNextUI = true;
  127. if (isThreadRunning() && ! shouldThreadExit())
  128. {
  129. for (; fDoNextUI;)
  130. d_msleep(10);
  131. }
  132. else
  133. {
  134. fNextUI.run();
  135. fDoNextUI = false;
  136. }
  137. return fNextUI.ui;
  138. }
  139. /**
  140. Delete UI on our separate thread.
  141. Blocks until the UI is deleted.
  142. */
  143. void deleteUI(d_UI* const ui)
  144. {
  145. DISTRHO_SAFE_ASSERT_RETURN(! fDoNextUI,);
  146. fNextUI.create = false;
  147. fNextUI.ui = ui;
  148. fDoNextUI = true;
  149. if (isThreadRunning() && ! shouldThreadExit())
  150. {
  151. for (; fDoNextUI;)
  152. d_msleep(10);
  153. }
  154. else
  155. {
  156. fNextUI.run();
  157. fDoNextUI = false;
  158. }
  159. }
  160. // -------------------------------------------------------------------
  161. private:
  162. struct NextUI {
  163. typedef d_UI* (*UiFunc)();
  164. bool create;
  165. union {
  166. UiFunc func;
  167. d_UI* ui;
  168. };
  169. NextUI()
  170. : create(false),
  171. func(nullptr) {}
  172. void run();
  173. };
  174. std::list<Fl_Double_Window*> fWindows;
  175. d_Mutex fWindowMutex;
  176. NextUI fNextUI;
  177. volatile bool fDoNextUI;
  178. volatile bool fThreadInitialized;
  179. /** @internal used by NtkWindow. */
  180. void addWindow(Fl_Double_Window* const window)
  181. {
  182. DISTRHO_SAFE_ASSERT_RETURN(window != nullptr,);
  183. if (fWindows.size() == 0 && ! isThreadRunning())
  184. startThread();
  185. const d_MutexLocker cml(fWindowMutex);
  186. fWindows.push_back(window);
  187. }
  188. /** @internal used by NtkWindow. */
  189. void removeWindow(Fl_Double_Window* const window)
  190. {
  191. DISTRHO_SAFE_ASSERT_RETURN(window != nullptr,);
  192. const d_MutexLocker cml(fWindowMutex);
  193. fWindows.remove(window);
  194. if (fWindows.size() == 0)
  195. signalThreadShouldExit();
  196. }
  197. /** @internal */
  198. void run() override
  199. {
  200. static bool initialized = false;
  201. if (! initialized)
  202. {
  203. initialized = true;
  204. fl_register_images();
  205. #ifdef DISTRHO_OS_LINUX
  206. fl_open_display();
  207. #endif
  208. }
  209. fThreadInitialized = true;
  210. for (; ! shouldThreadExit();)
  211. {
  212. {
  213. const FlScopedLock csl;
  214. if (fDoNextUI)
  215. {
  216. fNextUI.run();
  217. fDoNextUI = false;
  218. }
  219. Fl::check();
  220. Fl::flush();
  221. }
  222. d_msleep(20);
  223. }
  224. const FlScopedLock csl;
  225. const d_MutexLocker cml(fWindowMutex);
  226. for (std::list<Fl_Double_Window*>::reverse_iterator rit = fWindows.rbegin(), rite = fWindows.rend(); rit != rite; ++rit)
  227. {
  228. Fl_Double_Window* const window(*rit);
  229. window->hide();
  230. }
  231. }
  232. friend class NtkWindow;
  233. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NtkApp)
  234. };
  235. // -----------------------------------------------------------------------
  236. END_NAMESPACE_DGL
  237. #endif // DGL_NTK_APP_HPP_INCLUDED