janela 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +95 -0
  3. package/bin/janela.mjs +271 -0
  4. package/package.json +43 -0
  5. package/runtime/janela.ts +193 -0
  6. package/shim/wvshim.cc +201 -0
  7. package/templates/index.html +48 -0
  8. package/templates/janela.conf.json +10 -0
  9. package/templates/main.ts +37 -0
  10. package/vendor-webview/LICENSE +22 -0
  11. package/vendor-webview/core/include/webview/api.h +251 -0
  12. package/vendor-webview/core/include/webview/backends.hh +40 -0
  13. package/vendor-webview/core/include/webview/c_api_impl.hh +258 -0
  14. package/vendor-webview/core/include/webview/detail/backends/cocoa_webkit.hh +626 -0
  15. package/vendor-webview/core/include/webview/detail/backends/gtk_webkitgtk.hh +355 -0
  16. package/vendor-webview/core/include/webview/detail/backends/win32_edge.hh +909 -0
  17. package/vendor-webview/core/include/webview/detail/basic_result.hh +119 -0
  18. package/vendor-webview/core/include/webview/detail/engine_base.hh +382 -0
  19. package/vendor-webview/core/include/webview/detail/exceptions.hh +42 -0
  20. package/vendor-webview/core/include/webview/detail/json.hh +337 -0
  21. package/vendor-webview/core/include/webview/detail/native_library.hh +170 -0
  22. package/vendor-webview/core/include/webview/detail/optional.hh +118 -0
  23. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSApplication.hh +107 -0
  24. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSBundle.hh +56 -0
  25. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSEvent.hh +71 -0
  26. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSInvocation.hh +66 -0
  27. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSMethodSignature.hh +52 -0
  28. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSNotification.hh +51 -0
  29. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSNumber.hh +53 -0
  30. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSObject.hh +51 -0
  31. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSOpenPanel.hh +71 -0
  32. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSPoint.hh +53 -0
  33. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSRect.hh +53 -0
  34. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSSavePanel.hh +52 -0
  35. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSSize.hh +53 -0
  36. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSString.hh +87 -0
  37. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSURL.hh +63 -0
  38. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSURLRequest.hh +52 -0
  39. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSValue.hh +56 -0
  40. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSView.hh +91 -0
  41. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSWindow.hh +137 -0
  42. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/cocoa.hh +59 -0
  43. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/types.hh +49 -0
  44. package/vendor-webview/core/include/webview/detail/platform/darwin/objc/Class.hh +55 -0
  45. package/vendor-webview/core/include/webview/detail/platform/darwin/objc/autoreleasepool.hh +70 -0
  46. package/vendor-webview/core/include/webview/detail/platform/darwin/objc/invoke.hh +75 -0
  47. package/vendor-webview/core/include/webview/detail/platform/darwin/objc/memory.hh +57 -0
  48. package/vendor-webview/core/include/webview/detail/platform/darwin/objc/objc.hh +47 -0
  49. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKOpenPanelParameters.hh +57 -0
  50. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKScriptMessage.hh +51 -0
  51. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKUserContentController.hh +61 -0
  52. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKUserScript.hh +71 -0
  53. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKWebView.hh +108 -0
  54. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKWebViewConfiguration.hh +60 -0
  55. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/webkit.hh +46 -0
  56. package/vendor-webview/core/include/webview/detail/platform/linux/gtk/compat.hh +137 -0
  57. package/vendor-webview/core/include/webview/detail/platform/linux/webkitgtk/compat.hh +142 -0
  58. package/vendor-webview/core/include/webview/detail/platform/linux/webkitgtk/dmabuf.hh +167 -0
  59. package/vendor-webview/core/include/webview/detail/platform/windows/com_init_wrapper.hh +126 -0
  60. package/vendor-webview/core/include/webview/detail/platform/windows/dpi.hh +161 -0
  61. package/vendor-webview/core/include/webview/detail/platform/windows/dwmapi.hh +67 -0
  62. package/vendor-webview/core/include/webview/detail/platform/windows/iid.hh +78 -0
  63. package/vendor-webview/core/include/webview/detail/platform/windows/ntdll.hh +58 -0
  64. package/vendor-webview/core/include/webview/detail/platform/windows/reg_key.hh +135 -0
  65. package/vendor-webview/core/include/webview/detail/platform/windows/shcore.hh +59 -0
  66. package/vendor-webview/core/include/webview/detail/platform/windows/theme.hh +76 -0
  67. package/vendor-webview/core/include/webview/detail/platform/windows/user32.hh +90 -0
  68. package/vendor-webview/core/include/webview/detail/platform/windows/version.hh +151 -0
  69. package/vendor-webview/core/include/webview/detail/platform/windows/webview2/loader.hh +391 -0
  70. package/vendor-webview/core/include/webview/detail/user_script.hh +76 -0
  71. package/vendor-webview/core/include/webview/detail/utility/string.hh +99 -0
  72. package/vendor-webview/core/include/webview/errors.h +67 -0
  73. package/vendor-webview/core/include/webview/errors.hh +85 -0
  74. package/vendor-webview/core/include/webview/json_deprecated.hh +61 -0
  75. package/vendor-webview/core/include/webview/macros.h +123 -0
  76. package/vendor-webview/core/include/webview/types.h +81 -0
  77. package/vendor-webview/core/include/webview/types.hh +48 -0
  78. package/vendor-webview/core/include/webview/version.h +67 -0
  79. package/vendor-webview/core/include/webview/webview.h +32 -0
  80. package/vendor-webview/core/include/webview.h +15 -0
@@ -0,0 +1,108 @@
1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2017 Serge Zaitsev
5
+ * Copyright (c) 2022 Steffen André Langnes
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ #ifndef WEBVIEW_PLATFORM_DARWIN_WEBKIT_WKWEBVIEW_HH
27
+ #define WEBVIEW_PLATFORM_DARWIN_WEBKIT_WKWEBVIEW_HH
28
+
29
+ #if defined(__cplusplus) && !defined(WEBVIEW_HEADER)
30
+
31
+ #include "../../../../macros.h"
32
+
33
+ #if defined(WEBVIEW_PLATFORM_DARWIN) && defined(WEBVIEW_COCOA)
34
+
35
+ #include "../objc/objc.hh"
36
+
37
+ #include <CoreGraphics/CoreGraphics.h>
38
+
39
+ namespace webview {
40
+ namespace detail {
41
+ namespace webkit {
42
+
43
+ inline id WKWebView_alloc() {
44
+ return objc::msg_send<id>(objc::get_class("WKWebView"),
45
+ objc::selector("alloc"));
46
+ }
47
+
48
+ inline id WKWebView_initWithFrame(id self, CGRect frame, id configuration) {
49
+ return objc::msg_send<id>(self,
50
+ objc::selector("initWithFrame:configuration:"),
51
+ frame, configuration);
52
+ }
53
+
54
+ inline id WKWebView_withFrame(CGRect frame, id configuration) {
55
+ return objc::autorelease(
56
+ WKWebView_initWithFrame(WKWebView_alloc(), frame, configuration));
57
+ }
58
+
59
+ inline id WKWebView_get_UIDelegate(id self) {
60
+ return objc::msg_send<id>(self, objc::selector("UIDelegate"));
61
+ }
62
+
63
+ inline void WKWebView_set_UIDelegate(id self, id ui_delegate) {
64
+ objc::msg_send<void>(self, objc::selector("setUIDelegate:"), ui_delegate);
65
+ }
66
+
67
+ inline id WKWebView_loadHTMLString(id self, id string, id base_url) {
68
+ return objc::msg_send<id>(self, objc::selector("loadHTMLString:baseURL:"),
69
+ string, base_url);
70
+ }
71
+
72
+ inline id WKWebView_get_URL(id self) {
73
+ return objc::msg_send<id>(self, objc::selector("URL"));
74
+ }
75
+
76
+ inline id WKWebView_loadRequest(id self, id request) {
77
+ return objc::msg_send<id>(self, objc::selector("loadRequest:"), request);
78
+ }
79
+
80
+ inline void WKWebView_evaluateJavaScript(id self, id js_string,
81
+ const void *completion_handler) {
82
+ return objc::msg_send<void>(
83
+ self, objc::selector("evaluateJavaScript:completionHandler:"), js_string,
84
+ completion_handler);
85
+ }
86
+
87
+ inline void WKWebView_set_inspectable(id self, bool inspectable) {
88
+ #if defined(__has_builtin)
89
+ #if __has_builtin(__builtin_available)
90
+ if (__builtin_available(macOS 13.3, iOS 16.4, tvOS 16.4, *)) {
91
+ objc::msg_send<void>(self, objc::selector("setInspectable:"),
92
+ static_cast<BOOL>(inspectable));
93
+ }
94
+ #else
95
+ #error __builtin_available not supported by compiler
96
+ #endif
97
+ #else
98
+ #error __has_builtin not supported by compiler
99
+ #endif
100
+ }
101
+
102
+ } // namespace webkit
103
+ } // namespace detail
104
+ } // namespace webview
105
+
106
+ #endif // defined(WEBVIEW_PLATFORM_DARWIN) && defined(WEBVIEW_COCOA)
107
+ #endif // defined(__cplusplus) && !defined(WEBVIEW_HEADER)
108
+ #endif // WEBVIEW_PLATFORM_DARWIN_WEBKIT_WKWEBVIEW_HH
@@ -0,0 +1,60 @@
1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2017 Serge Zaitsev
5
+ * Copyright (c) 2022 Steffen André Langnes
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ #ifndef WEBVIEW_PLATFORM_DARWIN_WEBKIT_WKWEBVIEWCONFIGURATION_HH
27
+ #define WEBVIEW_PLATFORM_DARWIN_WEBKIT_WKWEBVIEWCONFIGURATION_HH
28
+
29
+ #if defined(__cplusplus) && !defined(WEBVIEW_HEADER)
30
+
31
+ #include "../../../../macros.h"
32
+
33
+ #if defined(WEBVIEW_PLATFORM_DARWIN) && defined(WEBVIEW_COCOA)
34
+
35
+ #include "../objc/objc.hh"
36
+
37
+ namespace webview {
38
+ namespace detail {
39
+ namespace webkit {
40
+
41
+ inline id WKWebViewConfiguration_new() {
42
+ return objc::msg_send<id>(objc::get_class("WKWebViewConfiguration"),
43
+ objc::selector("new"));
44
+ }
45
+
46
+ inline id WKWebViewConfiguration_get_userContentController(id self) {
47
+ return objc::msg_send<id>(self, objc::selector("userContentController"));
48
+ }
49
+
50
+ inline id WKWebViewConfiguration_get_preferences(id self) {
51
+ return objc::msg_send<id>(self, objc::selector("preferences"));
52
+ }
53
+
54
+ } // namespace webkit
55
+ } // namespace detail
56
+ } // namespace webview
57
+
58
+ #endif // defined(WEBVIEW_PLATFORM_DARWIN) && defined(WEBVIEW_COCOA)
59
+ #endif // defined(__cplusplus) && !defined(WEBVIEW_HEADER)
60
+ #endif // WEBVIEW_PLATFORM_DARWIN_WEBKIT_WKWEBVIEWCONFIGURATION_HH
@@ -0,0 +1,46 @@
1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2017 Serge Zaitsev
5
+ * Copyright (c) 2022 Steffen André Langnes
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ #ifndef WEBVIEW_PLATFORM_DARWIN_WEBKIT_HH
27
+ #define WEBVIEW_PLATFORM_DARWIN_WEBKIT_HH
28
+
29
+ #if defined(__cplusplus) && !defined(WEBVIEW_HEADER)
30
+
31
+ #include "../../../../macros.h"
32
+
33
+ #if defined(WEBVIEW_PLATFORM_DARWIN) && defined(WEBVIEW_COCOA)
34
+
35
+ // IWYU pragma: begin_exports
36
+ #include "WKOpenPanelParameters.hh"
37
+ #include "WKScriptMessage.hh"
38
+ #include "WKUserContentController.hh"
39
+ #include "WKUserScript.hh"
40
+ #include "WKWebView.hh"
41
+ #include "WKWebViewConfiguration.hh"
42
+ // IWYU pragma: end_exports
43
+
44
+ #endif // defined(WEBVIEW_PLATFORM_DARWIN) && defined(WEBVIEW_COCOA)
45
+ #endif // defined(__cplusplus) && !defined(WEBVIEW_HEADER)
46
+ #endif // WEBVIEW_PLATFORM_DARWIN_WEBKIT_HH
@@ -0,0 +1,137 @@
1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2017 Serge Zaitsev
5
+ * Copyright (c) 2022 Steffen André Langnes
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ #ifndef WEBVIEW_PLATFORM_LINUX_GTK_COMPAT_HH
27
+ #define WEBVIEW_PLATFORM_LINUX_GTK_COMPAT_HH
28
+
29
+ #if defined(__cplusplus) && !defined(WEBVIEW_HEADER)
30
+
31
+ #include "../../../../macros.h"
32
+
33
+ #if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
34
+
35
+ #include <gtk/gtk.h>
36
+
37
+ #if GTK_MAJOR_VERSION >= 4
38
+
39
+ #ifdef GDK_WINDOWING_X11
40
+ #include <gdk/x11/gdkx.h>
41
+ #endif
42
+
43
+ #elif GTK_MAJOR_VERSION >= 3
44
+
45
+ #ifdef GDK_WINDOWING_X11
46
+ #include <gdk/gdkx.h>
47
+ #endif
48
+
49
+ #endif
50
+
51
+ namespace webview {
52
+ namespace detail {
53
+
54
+ /**
55
+ * GTK compatibility helper class.
56
+ */
57
+ class gtk_compat {
58
+ public:
59
+ static gboolean init_check() {
60
+ #if GTK_MAJOR_VERSION >= 4
61
+ return gtk_init_check();
62
+ #else
63
+ return gtk_init_check(nullptr, nullptr);
64
+ #endif
65
+ }
66
+
67
+ static GtkWidget *window_new() {
68
+ #if GTK_MAJOR_VERSION >= 4
69
+ return gtk_window_new();
70
+ #else
71
+ return gtk_window_new(GTK_WINDOW_TOPLEVEL);
72
+ #endif
73
+ }
74
+
75
+ static void window_set_child(GtkWindow *window, GtkWidget *widget) {
76
+ #if GTK_MAJOR_VERSION >= 4
77
+ gtk_window_set_child(window, widget);
78
+ #else
79
+ gtk_container_add(GTK_CONTAINER(window), widget);
80
+ #endif
81
+ }
82
+
83
+ static void window_remove_child(GtkWindow *window, GtkWidget *widget) {
84
+ #if GTK_MAJOR_VERSION >= 4
85
+ if (gtk_window_get_child(window) == widget) {
86
+ gtk_window_set_child(window, nullptr);
87
+ }
88
+ #else
89
+ gtk_container_remove(GTK_CONTAINER(window), widget);
90
+ #endif
91
+ }
92
+
93
+ static void widget_set_visible(GtkWidget *widget, bool visible) {
94
+ #if GTK_MAJOR_VERSION >= 4
95
+ gtk_widget_set_visible(widget, visible ? TRUE : FALSE);
96
+ #else
97
+ if (visible) {
98
+ gtk_widget_show(widget);
99
+ } else {
100
+ gtk_widget_hide(widget);
101
+ }
102
+ #endif
103
+ }
104
+
105
+ static void window_set_size(GtkWindow *window, int width, int height) {
106
+ // GTK 4 can set a default window size, but unlike GTK 3 it can't resize
107
+ // the window after it has been set up.
108
+ #if GTK_MAJOR_VERSION >= 4
109
+ gtk_window_set_default_size(window, width, height);
110
+ #else
111
+ gtk_window_resize(window, width, height);
112
+ #endif
113
+ }
114
+
115
+ static void window_set_max_size(GtkWindow *window, int width, int height) {
116
+ // X11-specific features are available in GTK 3 but not GTK 4
117
+ #if GTK_MAJOR_VERSION < 4
118
+ GdkGeometry g{};
119
+ g.max_width = width;
120
+ g.max_height = height;
121
+ GdkWindowHints h = GDK_HINT_MAX_SIZE;
122
+ gtk_window_set_geometry_hints(GTK_WINDOW(window), nullptr, &g, h);
123
+ #else
124
+ // Avoid "unused parameter" warnings
125
+ (void)window;
126
+ (void)width;
127
+ (void)height;
128
+ #endif
129
+ }
130
+ };
131
+
132
+ } // namespace detail
133
+ } // namespace webview
134
+
135
+ #endif // defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
136
+ #endif // defined(__cplusplus) && !defined(WEBVIEW_HEADER)
137
+ #endif // WEBVIEW_PLATFORM_LINUX_GTK_COMPAT_HH
@@ -0,0 +1,142 @@
1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2017 Serge Zaitsev
5
+ * Copyright (c) 2022 Steffen André Langnes
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ #ifndef WEBVIEW_PLATFORM_LINUX_WEBKITGTK_COMPAT_HH
27
+ #define WEBVIEW_PLATFORM_LINUX_WEBKITGTK_COMPAT_HH
28
+
29
+ #if defined(__cplusplus) && !defined(WEBVIEW_HEADER)
30
+
31
+ #include "../../../../macros.h"
32
+
33
+ #if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
34
+
35
+ #include <functional>
36
+ #include <string>
37
+
38
+ #include <gtk/gtk.h>
39
+
40
+ #if GTK_MAJOR_VERSION >= 4
41
+
42
+ #include <jsc/jsc.h>
43
+ #include <webkit/webkit.h>
44
+
45
+ #ifdef GDK_WINDOWING_X11
46
+ #include <gdk/x11/gdkx.h>
47
+ #endif
48
+
49
+ #elif GTK_MAJOR_VERSION >= 3
50
+
51
+ #include <JavaScriptCore/JavaScript.h>
52
+ #include <webkit2/webkit2.h>
53
+
54
+ #ifdef GDK_WINDOWING_X11
55
+ #include <gdk/gdkx.h>
56
+ #endif
57
+
58
+ #endif
59
+
60
+ namespace webview {
61
+ namespace detail {
62
+
63
+ /**
64
+ * WebKitGTK compatibility helper class.
65
+ */
66
+ class webkitgtk_compat {
67
+ public:
68
+ #if GTK_MAJOR_VERSION >= 4
69
+ using wk_handler_js_value_t = JSCValue;
70
+ #else
71
+ using wk_handler_js_value_t = WebKitJavascriptResult;
72
+ #endif
73
+
74
+ using on_script_message_received_t =
75
+ std::function<void(WebKitUserContentManager *, const std::string &)>;
76
+ static void
77
+ connect_script_message_received(WebKitUserContentManager *manager,
78
+ const std::string &handler_name,
79
+ on_script_message_received_t handler) {
80
+ std::string signal_name = "script-message-received::";
81
+ signal_name += handler_name;
82
+
83
+ auto callback = +[](WebKitUserContentManager *manager,
84
+ wk_handler_js_value_t *r, gpointer arg) {
85
+ auto *handler = static_cast<on_script_message_received_t *>(arg);
86
+ (*handler)(manager, get_string_from_js_result(r));
87
+ };
88
+
89
+ auto deleter = +[](gpointer data, GClosure *) {
90
+ delete static_cast<on_script_message_received_t *>(data);
91
+ };
92
+
93
+ g_signal_connect_data(manager, signal_name.c_str(), G_CALLBACK(callback),
94
+ new on_script_message_received_t{handler}, deleter,
95
+ static_cast<GConnectFlags>(0) /*G_CONNECT_DEFAULT*/);
96
+ }
97
+
98
+ static std::string get_string_from_js_result(JSCValue *r) {
99
+ char *cs = jsc_value_to_string(r);
100
+ std::string s{cs};
101
+ g_free(cs);
102
+ return s;
103
+ }
104
+
105
+ #if GTK_MAJOR_VERSION < 4
106
+ static std::string get_string_from_js_result(WebKitJavascriptResult *r) {
107
+ #if (WEBKIT_MAJOR_VERSION == 2 && WEBKIT_MINOR_VERSION >= 22) || \
108
+ WEBKIT_MAJOR_VERSION > 2
109
+ JSCValue *value = webkit_javascript_result_get_js_value(r);
110
+ return get_string_from_js_result(value);
111
+ #else
112
+ JSGlobalContextRef ctx = webkit_javascript_result_get_global_context(r);
113
+ JSValueRef value = webkit_javascript_result_get_value(r);
114
+ JSStringRef js = JSValueToStringCopy(ctx, value, nullptr);
115
+ size_t n = JSStringGetMaximumUTF8CStringSize(js);
116
+ char *cs = g_new(char, n);
117
+ JSStringGetUTF8CString(js, cs, n);
118
+ JSStringRelease(js);
119
+ std::string s{cs};
120
+ g_free(cs);
121
+ return s;
122
+ #endif
123
+ }
124
+ #endif
125
+
126
+ static void user_content_manager_register_script_message_handler(
127
+ WebKitUserContentManager *manager, const gchar *name) {
128
+ #if GTK_MAJOR_VERSION >= 4
129
+ webkit_user_content_manager_register_script_message_handler(manager, name,
130
+ nullptr);
131
+ #else
132
+ webkit_user_content_manager_register_script_message_handler(manager, name);
133
+ #endif
134
+ }
135
+ };
136
+
137
+ } // namespace detail
138
+ } // namespace webview
139
+
140
+ #endif // defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
141
+ #endif // defined(__cplusplus) && !defined(WEBVIEW_HEADER)
142
+ #endif // WEBVIEW_PLATFORM_LINUX_WEBKITGTK_COMPAT_HH
@@ -0,0 +1,167 @@
1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2017 Serge Zaitsev
5
+ * Copyright (c) 2022 Steffen André Langnes
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ #ifndef WEBVIEW_BACKENDS_GTK_WEBKITGTK_DMABUF_HH
27
+ #define WEBVIEW_BACKENDS_GTK_WEBKITGTK_DMABUF_HH
28
+
29
+ #if defined(__cplusplus) && !defined(WEBVIEW_HEADER)
30
+
31
+ #include "../../../../macros.h"
32
+
33
+ #if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
34
+
35
+ #include <cstdlib>
36
+ #include <string>
37
+
38
+ #include <gtk/gtk.h>
39
+
40
+ #if GTK_MAJOR_VERSION >= 4
41
+
42
+ #include <jsc/jsc.h>
43
+ #include <webkit/webkit.h>
44
+
45
+ #ifdef GDK_WINDOWING_X11
46
+ #include <gdk/x11/gdkx.h>
47
+ #endif
48
+
49
+ #elif GTK_MAJOR_VERSION >= 3
50
+
51
+ #include <JavaScriptCore/JavaScript.h>
52
+ #include <webkit2/webkit2.h>
53
+
54
+ #ifdef GDK_WINDOWING_X11
55
+ #include <gdk/gdkx.h>
56
+ #endif
57
+
58
+ #endif
59
+
60
+ #include <fcntl.h>
61
+ #include <sys/stat.h>
62
+
63
+ namespace webview {
64
+ namespace detail {
65
+
66
+ // Namespace containing workaround for WebKit 2.42 when using NVIDIA GPU
67
+ // driver.
68
+ // See WebKit bug: https://bugs.webkit.org/show_bug.cgi?id=261874
69
+ // Please remove all of the code in this namespace when it's no longer needed.
70
+ namespace webkit_dmabuf {
71
+
72
+ // Get environment variable. Not thread-safe.
73
+ static inline std::string get_env(const std::string &name) {
74
+ auto *value = std::getenv(name.c_str());
75
+ if (value) {
76
+ return {value};
77
+ }
78
+ return {};
79
+ }
80
+
81
+ // Set environment variable. Not thread-safe.
82
+ static inline void set_env(const std::string &name, const std::string &value) {
83
+ ::setenv(name.c_str(), value.c_str(), 1);
84
+ }
85
+
86
+ // Checks whether the NVIDIA GPU driver is used based on whether the kernel
87
+ // module is loaded.
88
+ static inline bool is_using_nvidia_driver() {
89
+ struct ::stat buffer {};
90
+ if (::stat("/sys/module/nvidia", &buffer) != 0) {
91
+ return false;
92
+ }
93
+ return S_ISDIR(buffer.st_mode);
94
+ }
95
+
96
+ // Checks whether the windowing system is Wayland.
97
+ static inline bool is_wayland_display() {
98
+ if (!get_env("WAYLAND_DISPLAY").empty()) {
99
+ return true;
100
+ }
101
+ if (get_env("XDG_SESSION_TYPE") == "wayland") {
102
+ return true;
103
+ }
104
+ if (get_env("DESKTOP_SESSION").find("wayland") != std::string::npos) {
105
+ return true;
106
+ }
107
+ return false;
108
+ }
109
+
110
+ // Checks whether the GDK X11 backend is used.
111
+ // See: https://docs.gtk.org/gdk3/class.DisplayManager.html
112
+ static inline bool is_gdk_x11_backend() {
113
+ #ifdef GDK_WINDOWING_X11
114
+ auto *gdk_display = gdk_display_get_default();
115
+ return GDK_IS_X11_DISPLAY(gdk_display); // NOLINT(misc-const-correctness)
116
+ #else
117
+ return false;
118
+ #endif
119
+ }
120
+
121
+ // Checks whether WebKit is affected by bug when using DMA-BUF renderer.
122
+ // Returns true if all of the following conditions are met:
123
+ // - WebKit version is >= 2.42 (please narrow this down when there's a fix).
124
+ // - Environment variables are empty or not set:
125
+ // - WEBKIT_DISABLE_DMABUF_RENDERER
126
+ // - Windowing system is not Wayland.
127
+ // - GDK backend is X11.
128
+ // - NVIDIA GPU driver is used.
129
+ static inline bool is_webkit_dmabuf_bugged() {
130
+ auto wk_major = webkit_get_major_version();
131
+ auto wk_minor = webkit_get_minor_version();
132
+ // TODO: Narrow down affected WebKit version when there's a fixed version
133
+ auto is_affected_wk_version = wk_major == 2 && wk_minor >= 42;
134
+ if (!is_affected_wk_version) {
135
+ return false;
136
+ }
137
+ if (!get_env("WEBKIT_DISABLE_DMABUF_RENDERER").empty()) {
138
+ return false;
139
+ }
140
+ if (is_wayland_display()) {
141
+ return false;
142
+ }
143
+ if (!is_gdk_x11_backend()) {
144
+ return false;
145
+ }
146
+ if (!is_using_nvidia_driver()) {
147
+ return false;
148
+ }
149
+ return true;
150
+ }
151
+
152
+ // Applies workaround for WebKit DMA-BUF bug if needed.
153
+ // See WebKit bug: https://bugs.webkit.org/show_bug.cgi?id=261874
154
+ static inline void apply_webkit_dmabuf_workaround() {
155
+ if (!is_webkit_dmabuf_bugged()) {
156
+ return;
157
+ }
158
+ set_env("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
159
+ }
160
+
161
+ } // namespace webkit_dmabuf
162
+ } // namespace detail
163
+ } // namespace webview
164
+
165
+ #endif // defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
166
+ #endif // defined(__cplusplus) && !defined(WEBVIEW_HEADER)
167
+ #endif // WEBVIEW_BACKENDS_GTK_WEBKITGTK_DMABUF_HH