11#include "IPlugWebView.h"
15#include <webkit2/webkit2.h>
27extern "C" void IPlugLinuxWebViewPump()
29 for (
int i = 0; i < 32 && g_main_context_pending(
nullptr); ++i)
30 g_main_context_iteration(
nullptr, FALSE);
34static void IPlugLinuxWebViewModuleAnchor() {}
48 void* OpenWebView(
void* pParent,
float x,
float y,
float w,
float h,
float scale)
58 gdk_set_allowed_backends(
"x11");
62 gtk_disable_setlocale();
64 if (!gtk_init_check(
nullptr,
nullptr))
69 GdkDisplay* pDisplay = gdk_display_get_default();
71 if (!GDK_IS_X11_DISPLAY(pDisplay))
74 Window parentXID =
static_cast<Window
>(
reinterpret_cast<uintptr_t
>(pParent));
75 GdkWindow* pOwnWindow = gdk_x11_window_lookup_for_display(pDisplay, parentXID);
78 if (pOwnWindow && gdk_window_get_window_type(pOwnWindow) != GDK_WINDOW_FOREIGN)
83 const char* menuOffset = getenv(
"IPLUG2_MENU_OFFSET");
84 mHostScale = std::max(1, gdk_window_get_scale_factor(pOwnWindow));
85 Display* pXDisplay = gdk_x11_display_get_xdisplay(pDisplay);
86 mHostChild = XCreateSimpleWindow(pXDisplay, parentXID, 0, menuOffset ? std::clamp(atoi(menuOffset), 0, 100) : 0, std::max(1,
static_cast<int>(std::ceil(w * scale))) * mHostScale,
87 std::max(1,
static_cast<int>(std::ceil(h * scale))) * mHostScale, 0, 0, 0);
88 XMapWindow(pXDisplay, mHostChild);
89 XSync(pXDisplay, False);
90 parentXID = mHostChild;
93 GdkWindow* pHostWindow = gdk_x11_window_foreign_new_for_display(pDisplay, parentXID);
101 g_object_unref(pHostWindow);
102 mPlug = gtk_plug_new(parentXID);
111 g_signal_connect(mPlug,
"destroy", G_CALLBACK(OnPlugDestroyed),
this);
113 mView = webkit_web_view_new();
114 auto* pManager = webkit_web_view_get_user_content_manager(WEBKIT_WEB_VIEW(mView));
115 webkit_user_content_manager_register_script_message_handler(pManager,
"iplug");
116 g_signal_connect(pManager,
"script-message-received::iplug", G_CALLBACK(OnScriptMessage),
this);
118 auto* pBridge = webkit_user_script_new(
"window.IPlugSendMsg = function(msg) { window.webkit.messageHandlers.iplug.postMessage(JSON.stringify(msg)); };", WEBKIT_USER_CONTENT_INJECT_TOP_FRAME,
119 WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START,
nullptr,
nullptr);
120 webkit_user_content_manager_add_script(pManager, pBridge);
121 webkit_user_script_unref(pBridge);
123 g_signal_connect(mView,
"decide-policy", G_CALLBACK(OnDecidePolicy),
this);
124 g_signal_connect(mView,
"load-changed", G_CALLBACK(OnLoadChanged),
this);
125 g_signal_connect(mView,
"load-failed", G_CALLBACK(OnLoadFailed),
this);
131 const char* str = getenv(
"IPLUG2_SCREEN_SCALE");
132 char* pEnd =
nullptr;
133 const double screenScale = str ? strtod(str, &pEnd) : 0.;
135 if (str && pEnd != str && *pEnd ==
'\0' && screenScale >= 1. && screenScale <= 8.)
136 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(mView), screenScale / mHostScale);
139 gtk_container_add(GTK_CONTAINER(mPlug), mView);
140 SetWebViewBounds(x, y, w, h, scale);
141 gtk_widget_show_all(mPlug);
143 if (!gtk_plug_get_embedded(GTK_PLUG(mPlug)))
150 SetWebViewBounds(x, y, w, h, scale);
153 return reinterpret_cast<void*
>(
static_cast<uintptr_t
>(gtk_plug_get_id(GTK_PLUG(mPlug))));
160 g_signal_handlers_disconnect_by_data(mPlug,
this);
164 g_signal_handlers_disconnect_by_data(mView,
this);
165 auto* pManager = webkit_web_view_get_user_content_manager(WEBKIT_WEB_VIEW(mView));
166 g_signal_handlers_disconnect_by_data(pManager,
this);
169 gtk_widget_destroy(mPlug);
176 XDestroyWindow(gdk_x11_get_default_xdisplay(), mHostChild);
181 void HideWebView(
bool hide)
186 gtk_widget_set_visible(mPlug, !hide);
192 void LoadHTML(
const char* html)
195 webkit_web_view_load_html(WEBKIT_WEB_VIEW(mView), html,
nullptr);
198 void LoadURL(
const char* url)
201 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(mView), url);
204 void LoadFile(
const char* fileName,
const char*)
206 if (!mView || !fileName)
212 namespace fs = std::filesystem;
213 fs::path path(fileName);
215 if (!path.is_absolute())
219 if (!dladdr(
reinterpret_cast<void*
>(&IPlugLinuxWebViewModuleAnchor), &info))
222 fs::path directory = fs::path(info.dli_fname).parent_path();
226 if (directory.parent_path().filename() ==
"Contents")
227 directory = directory.parent_path().parent_path();
229 if (fs::path(info.dli_fname).extension() ==
".clap")
230 path = fs::path(info.dli_fname).string() +
".resources/web/" + path.string();
232 path = directory /
"Resources" /
"web" / path;
239 if (fs::path absolute = fs::absolute(path, ec); !ec)
242 mWebRoot.Set(path.parent_path().string().c_str());
243 GError* pError =
nullptr;
244 char* uri = g_filename_to_uri(path.string().c_str(),
nullptr, &pError);
246 if (uri && fs::exists(path, ec))
249 g_warning(
"iPlug2 WebView: missing file %s", path.string().c_str());
254 g_error_free(pError);
257 void ReloadPageContent()
260 webkit_web_view_reload(WEBKIT_WEB_VIEW(mView));
263 void EvaluateJavaScript(
const char* script, IWebView::completionHandlerFunc func)
268 auto* pCallback =
new IWebView::completionHandlerFunc(std::move(func));
269 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(mView), script,
nullptr, OnJavaScriptFinished, pCallback);
272 void EnableScroll(
bool) {}
274 void EnableInteraction(
bool enable)
277 gtk_widget_set_sensitive(mView, enable);
280 void SetWebViewBounds(
float,
float,
float w,
float h,
float scale)
287 const int unit = mHostChild ? mHostScale : 1;
288 const int width = std::max(1,
static_cast<int>(std::ceil(w * scale))) * unit;
289 const int height = std::max(1,
static_cast<int>(std::ceil(h * scale))) * unit;
292 const int gdkScale = std::max(1, gtk_widget_get_scale_factor(mPlug));
293 const int gtkWidth = std::max(1, (width + gdkScale - 1) / gdkScale);
294 const int gtkHeight = std::max(1, (height + gdkScale - 1) / gdkScale);
297 XResizeWindow(gdk_x11_get_default_xdisplay(), mHostChild, width, height);
299 gtk_widget_set_size_request(mPlug, gtkWidth, gtkHeight);
301 if (gtk_widget_get_realized(mPlug))
303 gtk_window_resize(GTK_WINDOW(mPlug), gtkWidth, gtkHeight);
304 XResizeWindow(gdk_x11_get_default_xdisplay(), gtk_plug_get_id(GTK_PLUG(mPlug)), width, height);
308 void GetWebRoot(WDL_String& path)
const { path.Set(mWebRoot.Get()); }
310 void GetLocalDownloadPathForFile(
const char* fileName, WDL_String& path)
314 path.Append(fileName);
318 static void OnPlugDestroyed(GtkWidget*, gpointer pData)
321 pSelf->mPlug =
nullptr;
322 pSelf->mView =
nullptr;
325 static void OnScriptMessage(WebKitUserContentManager*, WebKitJavascriptResult* pResult, gpointer pData)
328 char* text = jsc_value_to_string(webkit_javascript_result_get_js_value(pResult));
332 pSelf->mOwner->OnMessageFromWebView(text);
337 static gboolean OnDecidePolicy(WebKitWebView*, WebKitPolicyDecision* pDecision, WebKitPolicyDecisionType type, gpointer pData)
339 if (type != WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION)
342 const char* uri = webkit_uri_request_get_uri(webkit_navigation_policy_decision_get_request(WEBKIT_NAVIGATION_POLICY_DECISION(pDecision)));
347 webkit_policy_decision_ignore(pDecision);
351 static void OnLoadChanged(WebKitWebView*, WebKitLoadEvent event, gpointer pData)
355 if (event == WEBKIT_LOAD_STARTED)
356 pSelf->mLoadFailed =
false;
358 if (event == WEBKIT_LOAD_FINISHED && !pSelf->mLoadFailed)
359 pSelf->mOwner->OnWebContentLoaded();
362 static gboolean OnLoadFailed(WebKitWebView*, WebKitLoadEvent,
const char*, GError*, gpointer pData)
368 static void OnJavaScriptFinished(GObject* pSource, GAsyncResult* pResult, gpointer pData)
370 std::unique_ptr<IWebView::completionHandlerFunc> callback(
static_cast<IWebView::completionHandlerFunc*
>(pData));
371 WebKitJavascriptResult* pValue = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(pSource), pResult,
nullptr);
377 char* text = jsc_value_to_string(webkit_javascript_result_get_js_value(pValue));
382 webkit_javascript_result_unref(pValue);
390 if (GdkWindow* pWindow = gtk_widget_get_window(mPlug))
391 gdk_window_show(pWindow);
395 GtkWidget* mPlug =
nullptr;
396 GtkWidget* mView =
nullptr;
397 Window mHostChild = 0;
399 bool mLoadFailed =
false;
405#include "IPlugWebView.cpp"
Common paths useful for plug-ins.
static BEGIN_IPLUG_NAMESPACE bool PathHasDotDotComponent(const char *str)
void DesktopPath(WDL_String &path)
IWebView is a base interface for hosting a platform web view inside an IPlug plug-in's UI.
virtual bool OnCanNavigateToURL(const char *url)
Override to filter URLs.
virtual void OnWebViewReady()
Called when the web view is ready to receive navigation instructions.