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.

177 lines
4.9KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 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. #include "ApplicationPrivateData.hpp"
  17. #include "../Window.hpp"
  18. #include "pugl.hpp"
  19. #include <ctime>
  20. START_NAMESPACE_DGL
  21. typedef std::list<DGL_NAMESPACE::Window*>::reverse_iterator WindowListReverseIterator;
  22. static d_ThreadHandle getCurrentThreadHandle() noexcept
  23. {
  24. #ifdef DISTRHO_OS_WINDOWS
  25. return GetCurrentThread();
  26. #else
  27. return pthread_self();
  28. #endif
  29. }
  30. static bool isThisTheMainThread(const d_ThreadHandle mainThreadHandle) noexcept
  31. {
  32. #ifdef DISTRHO_OS_WINDOWS
  33. return GetCurrentThread() == mainThreadHandle; // IsGUIThread ?
  34. #else
  35. return pthread_equal(getCurrentThreadHandle(), mainThreadHandle) != 0;
  36. #endif
  37. }
  38. // --------------------------------------------------------------------------------------------------------------------
  39. const char* Application::getClassName() const noexcept
  40. {
  41. return puglGetClassName(pData->world);
  42. }
  43. // --------------------------------------------------------------------------------------------------------------------
  44. Application::PrivateData::PrivateData(const bool standalone)
  45. : world(puglNewWorld(standalone ? PUGL_PROGRAM : PUGL_MODULE,
  46. standalone ? PUGL_WORLD_THREADS : 0x0)),
  47. isStandalone(standalone),
  48. isQuitting(false),
  49. isQuittingInNextCycle(false),
  50. isStarting(true),
  51. visibleWindows(0),
  52. mainThreadHandle(getCurrentThreadHandle()),
  53. windows(),
  54. idleCallbacks()
  55. {
  56. DISTRHO_SAFE_ASSERT_RETURN(world != nullptr,);
  57. puglSetWorldHandle(world, this);
  58. #ifndef __EMSCRIPTEN__
  59. puglSetClassName(world, DISTRHO_MACRO_AS_STRING(DGL_NAMESPACE));
  60. #endif
  61. }
  62. Application::PrivateData::~PrivateData()
  63. {
  64. DISTRHO_SAFE_ASSERT(isStarting || isQuitting);
  65. DISTRHO_SAFE_ASSERT(visibleWindows == 0);
  66. windows.clear();
  67. idleCallbacks.clear();
  68. if (world != nullptr)
  69. puglFreeWorld(world);
  70. }
  71. // --------------------------------------------------------------------------------------------------------------------
  72. void Application::PrivateData::oneWindowShown() noexcept
  73. {
  74. if (++visibleWindows == 1)
  75. {
  76. isQuitting = false;
  77. isStarting = false;
  78. }
  79. }
  80. void Application::PrivateData::oneWindowClosed() noexcept
  81. {
  82. DISTRHO_SAFE_ASSERT_RETURN(visibleWindows != 0,);
  83. if (--visibleWindows == 0)
  84. isQuitting = true;
  85. }
  86. // --------------------------------------------------------------------------------------------------------------------
  87. void Application::PrivateData::idle(const uint timeoutInMs)
  88. {
  89. if (isQuittingInNextCycle)
  90. {
  91. quit();
  92. isQuittingInNextCycle = false;
  93. }
  94. if (world != nullptr)
  95. {
  96. const double timeoutInSeconds = timeoutInMs != 0
  97. ? static_cast<double>(timeoutInMs) / 1000.0
  98. : 0.0;
  99. puglUpdate(world, timeoutInSeconds);
  100. }
  101. triggerIdleCallbacks();
  102. }
  103. void Application::PrivateData::triggerIdleCallbacks()
  104. {
  105. for (std::list<IdleCallback*>::iterator it = idleCallbacks.begin(), ite = idleCallbacks.end(); it != ite; ++it)
  106. {
  107. IdleCallback* const idleCallback(*it);
  108. idleCallback->idleCallback();
  109. }
  110. }
  111. void Application::PrivateData::quit()
  112. {
  113. if (! isThisTheMainThread(mainThreadHandle))
  114. {
  115. if (! isQuittingInNextCycle)
  116. {
  117. isQuittingInNextCycle = true;
  118. return;
  119. }
  120. }
  121. isQuitting = true;
  122. #ifndef DPF_TEST_APPLICATION_CPP
  123. for (WindowListReverseIterator rit = windows.rbegin(), rite = windows.rend(); rit != rite; ++rit)
  124. {
  125. DGL_NAMESPACE::Window* const window(*rit);
  126. window->close();
  127. }
  128. #endif
  129. }
  130. double Application::PrivateData::getTime() const
  131. {
  132. DISTRHO_SAFE_ASSERT_RETURN(world != nullptr, 0.0);
  133. return puglGetTime(world);
  134. }
  135. void Application::PrivateData::setClassName(const char* const name)
  136. {
  137. DISTRHO_SAFE_ASSERT_RETURN(world != nullptr,);
  138. DISTRHO_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  139. puglSetClassName(world, name);
  140. }
  141. // --------------------------------------------------------------------------------------------------------------------
  142. END_NAMESPACE_DGL