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.

243 lines
6.8KB

  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, const Type type)
  87. : pData(new PrivateData(isStandalone, type))
  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, kTypeAuto))
  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. #else
  119. // unused
  120. (void)argc;
  121. (void)argv;
  122. #endif
  123. // build config sentinels
  124. #ifdef DPF_DEBUG
  125. fail_to_link_is_mismatch_dpf_debug_on.ok = true;
  126. #else
  127. fail_to_link_is_mismatch_dpf_debug_off.ok = true;
  128. #endif
  129. #ifdef DGL_USE_FILE_BROWSER
  130. fail_to_link_is_mismatch_dgl_use_file_browser_on.ok = true;
  131. #else
  132. fail_to_link_is_mismatch_dgl_use_file_browser_off.ok = true;
  133. #endif
  134. #ifdef DGL_USE_WEB_VIEW
  135. fail_to_link_is_mismatch_dgl_use_web_view_on.ok = true;
  136. #else
  137. fail_to_link_is_mismatch_dgl_use_web_view_off.ok = true;
  138. #endif
  139. #ifdef DGL_NO_SHARED_RESOURCES
  140. fail_to_link_is_mismatch_dgl_no_shared_resources_on.ok = true;
  141. #else
  142. fail_to_link_is_mismatch_dgl_no_shared_resources_off.ok = true;
  143. #endif
  144. DISTRHO_SAFE_ASSERT(dpf_check_build_status());
  145. }
  146. Application::~Application()
  147. {
  148. delete pData;
  149. }
  150. void Application::idle()
  151. {
  152. pData->idle(0);
  153. }
  154. void Application::exec(const uint idleTimeInMs)
  155. {
  156. DISTRHO_SAFE_ASSERT_RETURN(pData->isStandalone,);
  157. #if defined(__EMSCRIPTEN__)
  158. emscripten_set_main_loop_arg(app_idle, this, 0, true);
  159. #elif defined(DISTRHO_OS_MAC)
  160. const CFTimeInterval idleTimeInSecs = static_cast<CFTimeInterval>(idleTimeInMs) / 1000;
  161. while (! pData->isQuitting)
  162. {
  163. pData->idle(0);
  164. if (CFRunLoopRunInMode(kCFRunLoopDefaultMode, idleTimeInSecs, true) == kCFRunLoopRunFinished)
  165. break;
  166. }
  167. #else
  168. while (! pData->isQuitting)
  169. pData->idle(idleTimeInMs);
  170. #endif
  171. }
  172. void Application::quit()
  173. {
  174. pData->quit();
  175. }
  176. bool Application::isQuitting() const noexcept
  177. {
  178. return pData->isQuitting || pData->isQuittingInNextCycle;
  179. }
  180. bool Application::isStandalone() const noexcept
  181. {
  182. return pData->isStandalone;
  183. }
  184. double Application::getTime() const
  185. {
  186. return pData->getTime();
  187. }
  188. Application::Type Application::getType() const noexcept
  189. {
  190. return pData->isModern ? kTypeModern : kTypeClassic;
  191. }
  192. void Application::addIdleCallback(IdleCallback* const callback)
  193. {
  194. DISTRHO_SAFE_ASSERT_RETURN(callback != nullptr,)
  195. pData->idleCallbacks.push_back(callback);
  196. }
  197. void Application::removeIdleCallback(IdleCallback* const callback)
  198. {
  199. DISTRHO_SAFE_ASSERT_RETURN(callback != nullptr,)
  200. pData->idleCallbacks.remove(callback);
  201. }
  202. void Application::setClassName(const char* const name)
  203. {
  204. pData->setClassName(name);
  205. }
  206. // --------------------------------------------------------------------------------------------------------------------
  207. END_NAMESPACE_DGL