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.

160 lines
4.4KB

  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 ThreadHandle getCurrentThreadHandle() noexcept
  23. {
  24. #ifdef DISTRHO_OS_WINDOWS
  25. return GetCurrentThread();
  26. #else
  27. return pthread_self();
  28. #endif
  29. }
  30. static bool isThisMainThread(const 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 HAVE_X11
  55. sofdFileDialogSetup(world);
  56. #endif
  57. }
  58. Application::PrivateData::~PrivateData()
  59. {
  60. DISTRHO_SAFE_ASSERT(isStarting || isQuitting);
  61. DISTRHO_SAFE_ASSERT(visibleWindows == 0);
  62. windows.clear();
  63. idleCallbacks.clear();
  64. if (world != nullptr)
  65. puglFreeWorld(world);
  66. }
  67. // --------------------------------------------------------------------------------------------------------------------
  68. void Application::PrivateData::oneWindowShown() noexcept
  69. {
  70. if (++visibleWindows == 1)
  71. {
  72. isQuitting = false;
  73. isStarting = false;
  74. }
  75. }
  76. void Application::PrivateData::oneWindowClosed() noexcept
  77. {
  78. DISTRHO_SAFE_ASSERT_RETURN(visibleWindows != 0,);
  79. if (--visibleWindows == 0)
  80. isQuitting = true;
  81. }
  82. // --------------------------------------------------------------------------------------------------------------------
  83. void Application::PrivateData::idle(const uint timeoutInMs)
  84. {
  85. if (isQuittingInNextCycle)
  86. {
  87. quit();
  88. isQuittingInNextCycle = false;
  89. }
  90. if (world != nullptr)
  91. {
  92. const double timeoutInSeconds = timeoutInMs != 0
  93. ? static_cast<double>(timeoutInMs) / 1000.0
  94. : 0.0;
  95. puglUpdate(world, timeoutInSeconds);
  96. }
  97. for (std::list<IdleCallback*>::iterator it = idleCallbacks.begin(), ite = idleCallbacks.end(); it != ite; ++it)
  98. {
  99. IdleCallback* const idleCallback(*it);
  100. idleCallback->idleCallback();
  101. }
  102. }
  103. void Application::PrivateData::quit()
  104. {
  105. DISTRHO_SAFE_ASSERT_RETURN(isStandalone,);
  106. if (! isThisMainThread(mainThreadHandle))
  107. {
  108. if (! isQuittingInNextCycle)
  109. {
  110. isQuittingInNextCycle = true;
  111. return;
  112. }
  113. }
  114. isQuitting = true;
  115. #ifndef DPF_TEST_APPLICATION_CPP
  116. for (WindowListReverseIterator rit = windows.rbegin(), rite = windows.rend(); rit != rite; ++rit)
  117. {
  118. DGL_NAMESPACE::Window* const window(*rit);
  119. window->close();
  120. }
  121. #endif
  122. }
  123. void Application::PrivateData::setClassName(const char* const name)
  124. {
  125. DISTRHO_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  126. puglSetClassName(world, name);
  127. }
  128. // --------------------------------------------------------------------------------------------------------------------
  129. END_NAMESPACE_DGL