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.

292 lines
6.5KB

  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. struct ScopedDisplayLock {
  35. ScopedDisplayLock()
  36. {
  37. #ifdef DISTRHO_OS_LINUX
  38. XLockDisplay(fl_display);
  39. #endif
  40. }
  41. ~ScopedDisplayLock()
  42. {
  43. #ifdef DISTRHO_OS_LINUX
  44. XUnlockDisplay(fl_display);
  45. #endif
  46. }
  47. };
  48. // -----------------------------------------------------------------------
  49. namespace DISTRHO_NAMESPACE {
  50. class UI;
  51. }
  52. START_NAMESPACE_DGL
  53. class NtkWindow;
  54. typedef DISTRHO_NAMESPACE::Mutex d_Mutex;
  55. typedef DISTRHO_NAMESPACE::MutexLocker d_MutexLocker;
  56. typedef DISTRHO_NAMESPACE::Thread d_Thread;
  57. typedef DISTRHO_NAMESPACE::UI d_UI;
  58. // -----------------------------------------------------------------------
  59. /**
  60. DGL compatible App class that uses NTK instead of OpenGL.
  61. @see App
  62. */
  63. class NtkApp : d_Thread
  64. {
  65. public:
  66. /**
  67. Constructor.
  68. */
  69. NtkApp()
  70. : d_Thread("NtkApp"),
  71. fWindows(),
  72. fWindowMutex(),
  73. fNextUI(),
  74. fDoNextUI(false),
  75. fInitialized(false)
  76. {
  77. #ifdef DISTRHO_OS_LINUX
  78. //XInitThreads();
  79. #endif
  80. startThread();
  81. for (; ! fInitialized;)
  82. d_msleep(10);
  83. }
  84. /**
  85. Destructor.
  86. */
  87. ~NtkApp()
  88. {
  89. stopThread(-1);
  90. fWindows.clear();
  91. }
  92. /**
  93. Idle function.
  94. This calls does nothing.
  95. */
  96. void idle() {}
  97. /**
  98. Run the application event-loop until all Windows are closed.
  99. @note: This function is meant for standalones only, *never* call this from plugins.
  100. */
  101. void exec()
  102. {
  103. while (isThreadRunning() && ! shouldThreadExit())
  104. d_sleep(1);
  105. }
  106. /**
  107. Quit the application.
  108. This stops the event-loop and closes all Windows.
  109. */
  110. void quit()
  111. {
  112. signalThreadShouldExit();
  113. }
  114. /**
  115. Check if the application is about to quit.
  116. Returning true means there's no event-loop running at the moment.
  117. */
  118. bool isQuiting() const noexcept
  119. {
  120. if (isThreadRunning() && ! shouldThreadExit())
  121. return false;
  122. return true;
  123. }
  124. // -------------------------------------------------------------------
  125. /**
  126. Create UI on our separate thread.
  127. Blocks until the UI is created and returns it.
  128. */
  129. d_UI* createUI(void* const func)
  130. {
  131. DISTRHO_SAFE_ASSERT_RETURN(isThreadRunning(), nullptr);
  132. DISTRHO_SAFE_ASSERT_RETURN(! fDoNextUI, nullptr);
  133. fNextUI.create = true;
  134. fNextUI.func = (NextUI::UiFunc)func;
  135. fDoNextUI = true;
  136. for (; fDoNextUI;)
  137. d_msleep(10);
  138. return fNextUI.ui;
  139. }
  140. /**
  141. Delete UI on our separate thread.
  142. Blocks until the UI is deleted.
  143. */
  144. void deleteUI(d_UI* const ui)
  145. {
  146. DISTRHO_SAFE_ASSERT_RETURN(! fDoNextUI,);
  147. fNextUI.create = false;
  148. fNextUI.ui = ui;
  149. fDoNextUI = true;
  150. if (isThreadRunning())
  151. {
  152. for (; fDoNextUI;)
  153. d_msleep(10);
  154. }
  155. else
  156. {
  157. fNextUI.run();
  158. fDoNextUI = false;
  159. }
  160. }
  161. // -------------------------------------------------------------------
  162. private:
  163. struct NextUI {
  164. typedef d_UI* (*UiFunc)();
  165. bool create;
  166. union {
  167. UiFunc func;
  168. d_UI* ui;
  169. };
  170. NextUI()
  171. : create(false),
  172. func(nullptr) {}
  173. void run();
  174. };
  175. std::list<Fl_Double_Window*> fWindows;
  176. d_Mutex fWindowMutex;
  177. NextUI fNextUI;
  178. volatile bool fDoNextUI;
  179. volatile bool fInitialized;
  180. /** @internal used by NtkWindow. */
  181. void addWindow(Fl_Double_Window* const window)
  182. {
  183. DISTRHO_SAFE_ASSERT_RETURN(window != nullptr,);
  184. if (fWindows.size() == 0 && ! isThreadRunning())
  185. startThread();
  186. const d_MutexLocker sl(fWindowMutex);
  187. fWindows.push_back(window);
  188. }
  189. /** @internal used by NtkWindow. */
  190. void removeWindow(Fl_Double_Window* const window)
  191. {
  192. DISTRHO_SAFE_ASSERT_RETURN(window != nullptr,);
  193. const d_MutexLocker sl(fWindowMutex);
  194. fWindows.remove(window);
  195. if (fWindows.size() == 0)
  196. signalThreadShouldExit();
  197. }
  198. /** @internal */
  199. void run() override
  200. {
  201. static bool initialized = false;
  202. if (! initialized)
  203. {
  204. initialized = true;
  205. fl_register_images();
  206. #ifdef DISTRHO_OS_LINUX
  207. fl_open_display();
  208. #endif
  209. }
  210. fInitialized = true;
  211. for (; ! shouldThreadExit();)
  212. {
  213. if (fDoNextUI)
  214. {
  215. const ScopedDisplayLock csdl;
  216. fNextUI.run();
  217. fDoNextUI = false;
  218. }
  219. const ScopedDisplayLock csdl;
  220. Fl::check();
  221. Fl::flush();
  222. d_msleep(20);
  223. }
  224. const d_MutexLocker sl(fWindowMutex);
  225. const ScopedDisplayLock csdl;
  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