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.

234 lines
6.6KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2025 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. #if defined(__EMSCRIPTEN__)
  18. # include <emscripten/emscripten.h>
  19. #elif defined(DISTRHO_OS_MAC)
  20. # include <CoreFoundation/CoreFoundation.h>
  21. #endif
  22. START_NAMESPACE_DGL
  23. /* define webview start */
  24. #if defined(HAVE_X11) && defined(DISTRHO_OS_LINUX) && defined(DGL_USE_WEB_VIEW)
  25. int dpf_webview_start(int argc, char* argv[]);
  26. #endif
  27. // --------------------------------------------------------------------------------------------------------------------
  28. // build config sentinels
  29. #define BUILD_CONFIG_SENTINEL(NAME) \
  30. DISTRHO_JOIN_MACRO(_, NAME)::DISTRHO_JOIN_MACRO(_, NAME)() noexcept : ok(false) {}
  31. #ifdef DPF_DEBUG
  32. BUILD_CONFIG_SENTINEL(fail_to_link_is_mismatch_dpf_debug_on)
  33. #else
  34. BUILD_CONFIG_SENTINEL(fail_to_link_is_mismatch_dpf_debug_off)
  35. #endif
  36. #ifdef DGL_USE_FILE_BROWSER
  37. BUILD_CONFIG_SENTINEL(fail_to_link_is_mismatch_dgl_use_file_browser_on)
  38. #else
  39. BUILD_CONFIG_SENTINEL(fail_to_link_is_mismatch_dgl_use_file_browser_off)
  40. #endif
  41. #ifdef DGL_USE_WEB_VIEW
  42. BUILD_CONFIG_SENTINEL(fail_to_link_is_mismatch_dgl_use_web_view_on)
  43. #else
  44. BUILD_CONFIG_SENTINEL(fail_to_link_is_mismatch_dgl_use_web_view_off)
  45. #endif
  46. #ifdef DGL_NO_SHARED_RESOURCES
  47. BUILD_CONFIG_SENTINEL(fail_to_link_is_mismatch_dgl_no_shared_resources_on)
  48. #else
  49. BUILD_CONFIG_SENTINEL(fail_to_link_is_mismatch_dgl_no_shared_resources_off)
  50. #endif
  51. #undef BUILD_CONFIG_SENTINEL
  52. static inline
  53. bool dpf_check_build_status() noexcept
  54. {
  55. return (
  56. #ifdef DPF_DEBUG
  57. fail_to_link_is_mismatch_dpf_debug_on.ok &&
  58. #else
  59. fail_to_link_is_mismatch_dpf_debug_off.ok &&
  60. #endif
  61. #ifdef DGL_USE_FILE_BROWSER
  62. fail_to_link_is_mismatch_dgl_use_file_browser_on.ok &&
  63. #else
  64. fail_to_link_is_mismatch_dgl_use_file_browser_off.ok &&
  65. #endif
  66. #ifdef DGL_USE_WEB_VIEW
  67. fail_to_link_is_mismatch_dgl_use_web_view_on.ok &&
  68. #else
  69. fail_to_link_is_mismatch_dgl_use_web_view_off.ok &&
  70. #endif
  71. #ifdef DGL_NO_SHARED_RESOURCES
  72. fail_to_link_is_mismatch_dgl_no_shared_resources_on.ok &&
  73. #else
  74. fail_to_link_is_mismatch_dgl_no_shared_resources_off.ok &&
  75. #endif
  76. true
  77. );
  78. }
  79. // --------------------------------------------------------------------------------------------------------------------
  80. #ifdef __EMSCRIPTEN__
  81. static void app_idle(void* const app)
  82. {
  83. static_cast<Application*>(app)->idle();
  84. }
  85. #endif
  86. Application::Application(const bool isStandalone)
  87. : pData(new PrivateData(isStandalone))
  88. {
  89. // build config sentinels
  90. #ifdef DPF_DEBUG
  91. fail_to_link_is_mismatch_dpf_debug_on.ok = true;
  92. #else
  93. fail_to_link_is_mismatch_dpf_debug_off.ok = true;
  94. #endif
  95. #ifdef DGL_USE_FILE_BROWSER
  96. fail_to_link_is_mismatch_dgl_use_file_browser_on.ok = true;
  97. #else
  98. fail_to_link_is_mismatch_dgl_use_file_browser_off.ok = true;
  99. #endif
  100. #ifdef DGL_USE_WEB_VIEW
  101. fail_to_link_is_mismatch_dgl_use_web_view_on.ok = true;
  102. #else
  103. fail_to_link_is_mismatch_dgl_use_web_view_off.ok = true;
  104. #endif
  105. #ifdef DGL_NO_SHARED_RESOURCES
  106. fail_to_link_is_mismatch_dgl_no_shared_resources_on.ok = true;
  107. #else
  108. fail_to_link_is_mismatch_dgl_no_shared_resources_off.ok = true;
  109. #endif
  110. DISTRHO_SAFE_ASSERT(dpf_check_build_status());
  111. }
  112. Application::Application(int argc, char* argv[])
  113. : pData(new PrivateData(true))
  114. {
  115. #if defined(HAVE_X11) && defined(DISTRHO_OS_LINUX) && defined(DGL_USE_WEB_VIEW)
  116. if (argc >= 2 && std::strcmp(argv[1], "dpf-ld-linux-webview") == 0)
  117. std::exit(dpf_webview_start(argc, argv));
  118. #endif
  119. // build config sentinels
  120. #ifdef DPF_DEBUG
  121. fail_to_link_is_mismatch_dpf_debug_on.ok = true;
  122. #else
  123. fail_to_link_is_mismatch_dpf_debug_off.ok = true;
  124. #endif
  125. #ifdef DGL_USE_FILE_BROWSER
  126. fail_to_link_is_mismatch_dgl_use_file_browser_on.ok = true;
  127. #else
  128. fail_to_link_is_mismatch_dgl_use_file_browser_off.ok = true;
  129. #endif
  130. #ifdef DGL_USE_WEB_VIEW
  131. fail_to_link_is_mismatch_dgl_use_web_view_on.ok = true;
  132. #else
  133. fail_to_link_is_mismatch_dgl_use_web_view_off.ok = true;
  134. #endif
  135. #ifdef DGL_NO_SHARED_RESOURCES
  136. fail_to_link_is_mismatch_dgl_no_shared_resources_on.ok = true;
  137. #else
  138. fail_to_link_is_mismatch_dgl_no_shared_resources_off.ok = true;
  139. #endif
  140. DISTRHO_SAFE_ASSERT(dpf_check_build_status());
  141. }
  142. Application::~Application()
  143. {
  144. delete pData;
  145. }
  146. void Application::idle()
  147. {
  148. pData->idle(0);
  149. }
  150. void Application::exec(const uint idleTimeInMs)
  151. {
  152. DISTRHO_SAFE_ASSERT_RETURN(pData->isStandalone,);
  153. #if defined(__EMSCRIPTEN__)
  154. emscripten_set_main_loop_arg(app_idle, this, 0, true);
  155. #elif defined(DISTRHO_OS_MAC)
  156. const CFTimeInterval idleTimeInSecs = static_cast<CFTimeInterval>(idleTimeInMs) / 1000;
  157. while (! pData->isQuitting)
  158. {
  159. pData->idle(0);
  160. if (CFRunLoopRunInMode(kCFRunLoopDefaultMode, idleTimeInSecs, true) == kCFRunLoopRunFinished)
  161. break;
  162. }
  163. #else
  164. while (! pData->isQuitting)
  165. pData->idle(idleTimeInMs);
  166. #endif
  167. }
  168. void Application::quit()
  169. {
  170. pData->quit();
  171. }
  172. bool Application::isQuitting() const noexcept
  173. {
  174. return pData->isQuitting || pData->isQuittingInNextCycle;
  175. }
  176. bool Application::isStandalone() const noexcept
  177. {
  178. return pData->isStandalone;
  179. }
  180. double Application::getTime() const
  181. {
  182. return pData->getTime();
  183. }
  184. void Application::addIdleCallback(IdleCallback* const callback)
  185. {
  186. DISTRHO_SAFE_ASSERT_RETURN(callback != nullptr,)
  187. pData->idleCallbacks.push_back(callback);
  188. }
  189. void Application::removeIdleCallback(IdleCallback* const callback)
  190. {
  191. DISTRHO_SAFE_ASSERT_RETURN(callback != nullptr,)
  192. pData->idleCallbacks.remove(callback);
  193. }
  194. void Application::setClassName(const char* const name)
  195. {
  196. pData->setClassName(name);
  197. }
  198. // --------------------------------------------------------------------------------------------------------------------
  199. END_NAMESPACE_DGL