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.

102 lines
3.1KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2024 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. #if !defined(DISTRHO_WEB_VIEW_HPP_INCLUDED) && !defined(DGL_WEB_VIEW_HPP_INCLUDED)
  17. # define DISTRHO_WEB_VIEW_INCLUDE_IMPLEMENTATION
  18. # include "WebView.hpp"
  19. #endif
  20. // --------------------------------------------------------------------------------------------------------------------
  21. // Web View stuff
  22. START_NAMESPACE_DISTRHO
  23. class WebView;
  24. WebView* webview_choc_create(const WebViewOptions& opts);
  25. void webview_choc_destroy(WebView*);
  26. void* webview_choc_handle(WebView*);
  27. void webview_choc_eval(WebView*, const char* js);
  28. void webview_choc_navigate(WebView*, const char* url);
  29. END_NAMESPACE_DISTRHO
  30. // --------------------------------------------------------------------------------------------------------------------
  31. #ifdef DISTRHO_WEB_VIEW_INCLUDE_IMPLEMENTATION
  32. # define WC_ERR_INVALID_CHARS 0
  33. # include "choc/choc_WebView.h"
  34. START_NAMESPACE_DISTRHO
  35. WebView* webview_choc_create(const WebViewOptions& opts)
  36. {
  37. WebView::Options wopts;
  38. wopts.acceptsFirstMouseClick = true;
  39. wopts.enableDebugMode = true;
  40. std::unique_ptr<WebView> webview = std::make_unique<WebView>(wopts);
  41. DISTRHO_SAFE_ASSERT_RETURN(webview->loadedOK(), nullptr);
  42. if (const WebViewMessageCallback callback = opts.callback)
  43. {
  44. webview->addInitScript("function postMessage(m){window.chrome.webview.postMessage(m);}");
  45. void* const callbackPtr = opts.callbackPtr;
  46. webview->bind([callback, callbackPtr](const std::string& value) {
  47. char* const data = strdup(value.data());
  48. callback(callbackPtr, data);
  49. std::free(data);
  50. });
  51. }
  52. else
  53. {
  54. webview->addInitScript("function postMessage(m){}");
  55. }
  56. if (opts.initialJS != nullptr)
  57. webview->addInitScript(opts.initialJS);
  58. return webview.release();
  59. }
  60. void webview_choc_destroy(WebView* const webview)
  61. {
  62. delete webview;
  63. }
  64. void* webview_choc_handle(WebView* const webview)
  65. {
  66. return webview->getViewHandle();
  67. }
  68. void webview_choc_eval(WebView* const webview, const char* const js)
  69. {
  70. webview->evaluateJavascript(js);
  71. }
  72. void webview_choc_navigate(WebView* const webview, const char* const url)
  73. {
  74. webview->navigate(url);
  75. }
  76. END_NAMESPACE_DISTRHO
  77. #endif // DISTRHO_WEB_VIEW_INCLUDE_IMPLEMENTATION
  78. // --------------------------------------------------------------------------------------------------------------------