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.

128 lines
3.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. // --------------------------------------------------------------------------------------------------------------------
  23. Application::PrivateData::PrivateData(const bool standalone)
  24. : world(puglNewWorld(standalone ? PUGL_PROGRAM : PUGL_MODULE,
  25. standalone ? PUGL_WORLD_THREADS : 0x0)),
  26. isStandalone(standalone),
  27. isQuitting(false),
  28. isStarting(true),
  29. visibleWindows(0),
  30. windows(),
  31. idleCallbacks()
  32. {
  33. DISTRHO_SAFE_ASSERT_RETURN(world != nullptr,);
  34. puglSetWorldHandle(world, this);
  35. // FIXME
  36. static int wc_count = 0;
  37. char classNameBuf[256];
  38. std::srand((std::time(NULL)));
  39. std::snprintf(classNameBuf, sizeof(classNameBuf), "%s_%d-%d-%p",
  40. DISTRHO_MACRO_AS_STRING(DGL_NAMESPACE), std::rand(), ++wc_count, this);
  41. classNameBuf[sizeof(classNameBuf)-1] = '\0';
  42. d_stderr("--------------------------------------------------------------- className is %s", classNameBuf);
  43. puglSetClassName(world, classNameBuf);
  44. #ifdef HAVE_X11
  45. sofdFileDialogSetup(world);
  46. #endif
  47. }
  48. Application::PrivateData::~PrivateData()
  49. {
  50. DISTRHO_SAFE_ASSERT(isStarting || isQuitting);
  51. DISTRHO_SAFE_ASSERT(visibleWindows == 0);
  52. windows.clear();
  53. idleCallbacks.clear();
  54. if (world != nullptr)
  55. puglFreeWorld(world);
  56. }
  57. // --------------------------------------------------------------------------------------------------------------------
  58. void Application::PrivateData::oneWindowShown() noexcept
  59. {
  60. if (++visibleWindows == 1)
  61. {
  62. isQuitting = false;
  63. isStarting = false;
  64. }
  65. }
  66. void Application::PrivateData::oneWindowClosed() noexcept
  67. {
  68. DISTRHO_SAFE_ASSERT_RETURN(visibleWindows != 0,);
  69. if (--visibleWindows == 0)
  70. isQuitting = true;
  71. }
  72. // --------------------------------------------------------------------------------------------------------------------
  73. void Application::PrivateData::idle(const uint timeoutInMs)
  74. {
  75. if (world != nullptr)
  76. {
  77. const double timeoutInSeconds = timeoutInMs != 0
  78. ? static_cast<double>(timeoutInMs) / 1000.0
  79. : 0.0;
  80. puglUpdate(world, timeoutInSeconds);
  81. }
  82. for (std::list<IdleCallback*>::iterator it = idleCallbacks.begin(), ite = idleCallbacks.end(); it != ite; ++it)
  83. {
  84. IdleCallback* const idleCallback(*it);
  85. idleCallback->idleCallback();
  86. }
  87. }
  88. void Application::PrivateData::quit()
  89. {
  90. DISTRHO_SAFE_ASSERT_RETURN(isStandalone,);
  91. isQuitting = true;
  92. #ifndef DPF_TEST_APPLICATION_CPP
  93. for (WindowListReverseIterator rit = windows.rbegin(), rite = windows.rend(); rit != rite; ++rit)
  94. {
  95. DGL_NAMESPACE::Window* const window(*rit);
  96. window->close();
  97. }
  98. #endif
  99. }
  100. // --------------------------------------------------------------------------------------------------------------------
  101. END_NAMESPACE_DGL