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.

173 lines
4.7KB

  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. Application::PrivateData::PrivateData(const bool standalone)
  40. : world(puglNewWorld(standalone ? PUGL_PROGRAM : PUGL_MODULE,
  41. standalone ? PUGL_WORLD_THREADS : 0x0)),
  42. isStandalone(standalone),
  43. isQuitting(false),
  44. isQuittingInNextCycle(false),
  45. isStarting(true),
  46. visibleWindows(0),
  47. mainThreadHandle(getCurrentThreadHandle()),
  48. windows(),
  49. idleCallbacks()
  50. {
  51. DISTRHO_SAFE_ASSERT_RETURN(world != nullptr,);
  52. puglSetWorldHandle(world, this);
  53. puglSetClassName(world, DISTRHO_MACRO_AS_STRING(DGL_NAMESPACE));
  54. #ifdef DISTRHO_OS_MAC
  55. if (standalone)
  56. puglMacOSActivateApp();
  57. #endif
  58. }
  59. Application::PrivateData::~PrivateData()
  60. {
  61. DISTRHO_SAFE_ASSERT(isStarting || isQuitting);
  62. DISTRHO_SAFE_ASSERT(visibleWindows == 0);
  63. windows.clear();
  64. idleCallbacks.clear();
  65. if (world != nullptr)
  66. puglFreeWorld(world);
  67. }
  68. // --------------------------------------------------------------------------------------------------------------------
  69. void Application::PrivateData::oneWindowShown() noexcept
  70. {
  71. if (++visibleWindows == 1)
  72. {
  73. isQuitting = false;
  74. isStarting = false;
  75. }
  76. }
  77. void Application::PrivateData::oneWindowClosed() noexcept
  78. {
  79. DISTRHO_SAFE_ASSERT_RETURN(visibleWindows != 0,);
  80. if (--visibleWindows == 0)
  81. isQuitting = true;
  82. }
  83. // --------------------------------------------------------------------------------------------------------------------
  84. void Application::PrivateData::idle(const uint timeoutInMs)
  85. {
  86. if (isQuittingInNextCycle)
  87. {
  88. quit();
  89. isQuittingInNextCycle = false;
  90. }
  91. if (world != nullptr)
  92. {
  93. const double timeoutInSeconds = timeoutInMs != 0
  94. ? static_cast<double>(timeoutInMs) / 1000.0
  95. : 0.0;
  96. puglUpdate(world, timeoutInSeconds);
  97. }
  98. triggerIdleCallbacks();
  99. }
  100. void Application::PrivateData::triggerIdleCallbacks()
  101. {
  102. for (std::list<IdleCallback*>::iterator it = idleCallbacks.begin(), ite = idleCallbacks.end(); it != ite; ++it)
  103. {
  104. IdleCallback* const idleCallback(*it);
  105. idleCallback->idleCallback();
  106. }
  107. }
  108. void Application::PrivateData::quit()
  109. {
  110. if (! isThisTheMainThread(mainThreadHandle))
  111. {
  112. if (! isQuittingInNextCycle)
  113. {
  114. isQuittingInNextCycle = true;
  115. return;
  116. }
  117. }
  118. isQuitting = true;
  119. #ifndef DPF_TEST_APPLICATION_CPP
  120. for (WindowListReverseIterator rit = windows.rbegin(), rite = windows.rend(); rit != rite; ++rit)
  121. {
  122. DGL_NAMESPACE::Window* const window(*rit);
  123. window->close();
  124. }
  125. #endif
  126. }
  127. double Application::PrivateData::getTime() const
  128. {
  129. DISTRHO_SAFE_ASSERT_RETURN(world != nullptr, 0.0);
  130. return puglGetTime(world);
  131. }
  132. void Application::PrivateData::setClassName(const char* const name)
  133. {
  134. DISTRHO_SAFE_ASSERT_RETURN(world != nullptr,);
  135. DISTRHO_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  136. puglSetClassName(world, name);
  137. }
  138. // --------------------------------------------------------------------------------------------------------------------
  139. END_NAMESPACE_DGL