@ng-native/desktop 0.26.1 → 0.27.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.
- package/README.md +6 -3
- package/bin/app-config.mjs +1 -0
- package/bin/archive.mjs +166 -0
- package/bin/autolink.mjs +61 -30
- package/bin/cli.mjs +127 -27
- package/bin/host.mjs +255 -58
- package/bin/package-linux.mjs +1394 -0
- package/bin/package.mjs +171 -35
- package/bin/plist.mjs +6 -2
- package/bin/png-icon.mjs +315 -0
- package/bin/prebuilt.mjs +147 -26
- package/bin/toolchain.mjs +258 -19
- package/native/CMakeLists.txt +64 -6
- package/native/appkit/CMakeLists.txt +2 -1
- package/native/appkit/appkit-backend.h +1 -0
- package/native/appkit/appkit-backend.mm +13 -0
- package/native/appkit/header-toolbar.mm +28 -25
- package/native/appkit/host-view.h +10 -0
- package/native/appkit/host-view.mm +146 -40
- package/native/appkit/input-replay.h +6 -0
- package/native/appkit/input-replay.mm +21 -12
- package/native/appkit/main.mm +11 -7
- package/native/appkit/native-controls.mm +13 -1
- package/native/appkit/split-view.mm +7 -13
- package/native/appkit/text-input-field.h +4 -0
- package/native/appkit/text-input-field.mm +20 -1
- package/native/cmake/curl.cmake +79 -0
- package/native/cmake/libraries/react-native-reanimated.cmake +5 -0
- package/native/cmake/libraries/react-native-worklets.cmake +5 -0
- package/native/cmake/linked-modules.cmake +20 -4
- package/native/cmake/react-native.cmake +18 -1
- package/native/core/include/ngn/accent-colors.h +38 -0
- package/native/core/include/ngn/accessibility-relations.h +65 -0
- package/native/core/include/ngn/accessibility.h +26 -0
- package/native/core/include/ngn/animated-frame-loop.h +56 -0
- package/native/core/include/ngn/app-info.h +24 -8
- package/native/core/include/ngn/app-menus.h +14 -1
- package/native/core/include/ngn/app-windows.h +15 -2
- package/native/core/include/ngn/aria-role.h +96 -0
- package/native/core/include/ngn/audio.h +2 -0
- package/native/core/include/ngn/camera.h +9 -0
- package/native/core/include/ngn/control-sizes.h +7 -2
- package/native/core/include/ngn/crash-report.h +31 -0
- package/native/core/include/ngn/curl-network.h +93 -0
- package/native/core/include/ngn/dbus-menu.h +39 -0
- package/native/core/include/ngn/default-menu.h +36 -0
- package/native/core/include/ngn/desktop-expo.h +7 -1
- package/native/core/include/ngn/desktop-modules.h +22 -0
- package/native/core/include/ngn/desktop-props.h +21 -1
- package/native/core/include/ngn/desktop-view-props.h +6 -0
- package/native/core/include/ngn/focus-order.h +10 -0
- package/native/core/include/ngn/header-bar.h +22 -0
- package/native/core/include/ngn/host-os.h +6 -0
- package/native/core/include/ngn/host-reload.h +21 -0
- package/native/core/include/ngn/host-state.h +23 -0
- package/native/core/include/ngn/image-source.h +26 -1
- package/native/core/include/ngn/input-script.h +3 -3
- package/native/core/include/ngn/input.h +4 -1
- package/native/core/include/ngn/launch-frame.h +21 -0
- package/native/core/include/ngn/launch-settings.h +23 -5
- package/native/core/include/ngn/mounting-backend.h +5 -0
- package/native/core/include/ngn/mounting-manager.h +1 -0
- package/native/core/include/ngn/native-controls.h +26 -5
- package/native/core/include/ngn/notification-schedule.h +74 -0
- package/native/core/include/ngn/notifications.h +2 -1
- package/native/core/include/ngn/openssl-update-crypto.h +15 -0
- package/native/core/include/ngn/photo-library.h +60 -20
- package/native/core/include/ngn/posix-locale.h +43 -0
- package/native/core/include/ngn/runtime.h +5 -0
- package/native/core/include/ngn/safe-areas.h +14 -2
- package/native/core/include/ngn/split-view.h +78 -0
- package/native/core/include/ngn/surface-metrics.h +7 -0
- package/native/core/include/ngn/system.h +14 -0
- package/native/core/include/ngn/text-input.h +13 -0
- package/native/core/include/ngn/text-units.h +38 -0
- package/native/core/include/ngn/updates.h +14 -2
- package/native/core/include/ngn/view-box.h +40 -0
- package/native/core/include/ngn/view-transform.h +15 -0
- package/native/core/include/ngn/web-browser.h +46 -0
- package/native/core/src/accent-colors.cpp +88 -0
- package/native/core/src/accessibility-relations.cpp +138 -0
- package/native/core/src/accessibility.cpp +50 -0
- package/native/core/src/animated-frame-loop.cpp +166 -0
- package/native/core/src/app-menus.cpp +26 -9
- package/native/core/src/app-windows.cpp +27 -6
- package/native/core/src/aria-role.cpp +127 -0
- package/native/core/src/control-sizes.cpp +9 -5
- package/native/core/src/crash-report.cpp +196 -0
- package/native/core/src/curl-network.cpp +1562 -0
- package/native/core/src/dbus-menu.cpp +71 -0
- package/native/core/src/default-menu.cpp +134 -0
- package/native/core/src/desktop-view-props.cpp +14 -0
- package/native/core/src/focus-order.cpp +19 -0
- package/native/core/src/header-bar.cpp +20 -0
- package/native/core/src/host-os.cpp +19 -0
- package/native/core/src/host-reload.cpp +45 -0
- package/native/core/src/host-state.cpp +8 -0
- package/native/core/src/image-source.cpp +90 -0
- package/native/core/src/input-dispatcher.cpp +27 -5
- package/native/core/src/input-script.cpp +3 -2
- package/native/core/src/launch-settings.cpp +76 -0
- package/native/core/src/libraries/react-native-reanimated.cpp +6 -1
- package/native/core/src/modules/app-info.cpp +55 -6
- package/native/core/src/modules/app-modules.cpp +1 -1
- package/native/core/src/modules/audio.cpp +4 -2
- package/native/core/src/modules/camera.cpp +8 -11
- package/native/core/src/modules/desktop-expo.cpp +10 -5
- package/native/core/src/modules/desktop-modules.cpp +106 -41
- package/native/core/src/modules/file-photo-picker.cpp +408 -0
- package/native/core/src/modules/notifications.cpp +13 -17
- package/native/core/src/modules/photo-library.cpp +42 -13
- package/native/core/src/modules/web-browser.cpp +89 -0
- package/native/core/src/mounting-manager.cpp +12 -0
- package/native/core/src/native-controls.cpp +42 -1
- package/native/core/src/notification-schedule.cpp +429 -0
- package/native/core/src/openssl-update-crypto.cpp +83 -0
- package/native/core/src/posix-locale.cpp +192 -0
- package/native/core/src/runtime.cpp +30 -1
- package/native/core/src/safe-areas.cpp +19 -4
- package/native/core/src/spawn-rules.cpp +4 -1
- package/native/core/src/split-view.cpp +93 -0
- package/native/core/src/system.cpp +19 -0
- package/native/core/src/text-input.cpp +18 -1
- package/native/core/src/text-units.cpp +99 -0
- package/native/core/src/updates.cpp +76 -17
- package/native/core/src/view-box.cpp +75 -40
- package/native/core/src/view-transform.cpp +28 -0
- package/native/docker/Dockerfile +38 -0
- package/native/docker/Dockerfile.fedora +36 -0
- package/native/docker/glibc-floor.sh +49 -0
- package/native/gtk/CMakeLists.txt +137 -0
- package/native/gtk/accent-colour.cpp +57 -0
- package/native/gtk/accent-colour.h +41 -0
- package/native/gtk/accessibility-settings.cpp +159 -0
- package/native/gtk/accessibility-settings.h +62 -0
- package/native/gtk/accessible-text.cpp +151 -0
- package/native/gtk/accessible-text.h +49 -0
- package/native/gtk/accessible.cpp +306 -0
- package/native/gtk/accessible.h +75 -0
- package/native/gtk/alerts.cpp +267 -0
- package/native/gtk/alerts.h +30 -0
- package/native/gtk/animation-frames.cpp +159 -0
- package/native/gtk/animation-frames.h +20 -0
- package/native/gtk/app-info.cpp +91 -0
- package/native/gtk/app-info.h +25 -0
- package/native/gtk/app-windows.cpp +439 -0
- package/native/gtk/app-windows.h +150 -0
- package/native/gtk/audio.cpp +370 -0
- package/native/gtk/audio.h +54 -0
- package/native/gtk/auth-session.cpp +73 -0
- package/native/gtk/auth-session.h +16 -0
- package/native/gtk/battery.cpp +110 -0
- package/native/gtk/battery.h +19 -0
- package/native/gtk/box-paint.cpp +262 -0
- package/native/gtk/box-paint.h +32 -0
- package/native/gtk/cairo-canvas.cpp +755 -0
- package/native/gtk/cairo-canvas.h +49 -0
- package/native/gtk/camera.cpp +909 -0
- package/native/gtk/camera.h +69 -0
- package/native/gtk/capture.cpp +46 -0
- package/native/gtk/capture.h +19 -0
- package/native/gtk/clock-format.cpp +151 -0
- package/native/gtk/clock-format.h +36 -0
- package/native/gtk/crypto.cpp +75 -0
- package/native/gtk/crypto.h +14 -0
- package/native/gtk/dbus-properties.cpp +131 -0
- package/native/gtk/dbus-properties.h +61 -0
- package/native/gtk/drag-and-drop.cpp +339 -0
- package/native/gtk/drag-and-drop.h +30 -0
- package/native/gtk/file-dialogs.cpp +239 -0
- package/native/gtk/file-dialogs.h +21 -0
- package/native/gtk/file-system.cpp +212 -0
- package/native/gtk/file-system.h +25 -0
- package/native/gtk/focus.cpp +282 -0
- package/native/gtk/focus.h +37 -0
- package/native/gtk/fonts.cpp +89 -0
- package/native/gtk/fonts.h +22 -0
- package/native/gtk/gst-frames.cpp +150 -0
- package/native/gtk/gst-frames.h +47 -0
- package/native/gtk/gst-playback.cpp +496 -0
- package/native/gtk/gst-playback.h +81 -0
- package/native/gtk/gtk-backend.cpp +690 -0
- package/native/gtk/gtk-backend.h +134 -0
- package/native/gtk/host-sources.cpp +354 -0
- package/native/gtk/host-sources.h +46 -0
- package/native/gtk/image-manipulator.cpp +254 -0
- package/native/gtk/image-manipulator.h +30 -0
- package/native/gtk/image-view.cpp +256 -0
- package/native/gtk/image-view.h +54 -0
- package/native/gtk/input-replay.cpp +299 -0
- package/native/gtk/input-replay.h +36 -0
- package/native/gtk/launcher-badge.cpp +29 -0
- package/native/gtk/launcher-badge.h +21 -0
- package/native/gtk/locales.cpp +131 -0
- package/native/gtk/locales.h +23 -0
- package/native/gtk/location.cpp +274 -0
- package/native/gtk/location.h +19 -0
- package/native/gtk/main.cpp +1092 -0
- package/native/gtk/menus.cpp +447 -0
- package/native/gtk/menus.h +100 -0
- package/native/gtk/native-controls.cpp +831 -0
- package/native/gtk/native-controls.h +58 -0
- package/native/gtk/network-state.cpp +72 -0
- package/native/gtk/network-state.h +24 -0
- package/native/gtk/notifications.cpp +235 -0
- package/native/gtk/notifications.h +30 -0
- package/native/gtk/pango-text.cpp +274 -0
- package/native/gtk/pango-text.h +73 -0
- package/native/gtk/portal.cpp +451 -0
- package/native/gtk/portal.h +71 -0
- package/native/gtk/printing.cpp +338 -0
- package/native/gtk/printing.h +34 -0
- package/native/gtk/proxy-resolver.cpp +74 -0
- package/native/gtk/proxy-resolver.h +20 -0
- package/native/gtk/screens.cpp +1461 -0
- package/native/gtk/screens.h +274 -0
- package/native/gtk/scroll-view.cpp +414 -0
- package/native/gtk/scroll-view.h +82 -0
- package/native/gtk/secret-store.cpp +120 -0
- package/native/gtk/secret-store.h +27 -0
- package/native/gtk/settings-portal.cpp +129 -0
- package/native/gtk/settings-portal.h +47 -0
- package/native/gtk/split-view.cpp +1105 -0
- package/native/gtk/split-view.h +195 -0
- package/native/gtk/status-notifier.cpp +487 -0
- package/native/gtk/status-notifier.h +43 -0
- package/native/gtk/surface-input.cpp +251 -0
- package/native/gtk/surface-input.h +57 -0
- package/native/gtk/system-access.cpp +295 -0
- package/native/gtk/system-access.h +37 -0
- package/native/gtk/text-field.cpp +827 -0
- package/native/gtk/text-field.h +78 -0
- package/native/gtk/video.cpp +527 -0
- package/native/gtk/video.h +47 -0
- package/native/gtk/view-widget.cpp +431 -0
- package/native/gtk/view-widget.h +105 -0
- package/native/gtk/window-scale.cpp +95 -0
- package/native/gtk/window-scale.h +35 -0
- package/native/scripts/atspi-check.py +310 -0
- package/native/scripts/atspi-check.test.py +48 -0
- package/native/scripts/build.sh +20 -3
- package/native/scripts/compilers.sh +17 -0
- package/native/scripts/deb-depends-check.sh +22 -0
- package/native/scripts/fetch.sh +23 -2
- package/native/scripts/launch-cell.sh +157 -0
- package/native/scripts/launch-probe.sh +14 -3
- package/native/scripts/make-prebuilt.mjs +133 -44
- package/native/scripts/prebuilt-host-cache.mjs +71 -40
- package/native/scripts/reset-for-compilers.sh +44 -0
- package/native/scripts/start-xvfb.sh +35 -0
- package/native/shim/paragraph-platform/react/renderer/components/text/HostPlatformParagraphProps.h +35 -0
- package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +5 -2
- package/native/shim/worklets/linux-reanimated-props.h +14 -0
- package/native/shim/worklets/linux-worklets.h +23 -0
- package/package.json +6 -3
- package/prebuilt.json +70 -6
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
#include "portal.h"
|
|
2
|
+
|
|
3
|
+
#include <gtk/gtk.h>
|
|
4
|
+
#ifdef GDK_WINDOWING_X11
|
|
5
|
+
#include <gdk/x11/gdkx.h>
|
|
6
|
+
#endif
|
|
7
|
+
#ifdef GDK_WINDOWING_WAYLAND
|
|
8
|
+
#include <gdk/wayland/gdkwayland.h>
|
|
9
|
+
#endif
|
|
10
|
+
|
|
11
|
+
#include <atomic>
|
|
12
|
+
#include <cstdio>
|
|
13
|
+
#include <map>
|
|
14
|
+
#include <memory>
|
|
15
|
+
|
|
16
|
+
namespace ngn::gtk::portal {
|
|
17
|
+
|
|
18
|
+
namespace {
|
|
19
|
+
|
|
20
|
+
constexpr const char *kBusName = "org.freedesktop.portal.Desktop";
|
|
21
|
+
constexpr const char *kObjectPath = "/org/freedesktop/portal/desktop";
|
|
22
|
+
|
|
23
|
+
GDBusConnection *gInjected = nullptr;
|
|
24
|
+
|
|
25
|
+
// A new reference to the connection requests go over, or null when there is no session bus.
|
|
26
|
+
GDBusConnection *connection() {
|
|
27
|
+
if (gInjected) {
|
|
28
|
+
return G_DBUS_CONNECTION(g_object_ref(gInjected));
|
|
29
|
+
}
|
|
30
|
+
GError *error = nullptr;
|
|
31
|
+
GDBusConnection *bus = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, &error);
|
|
32
|
+
if (error) {
|
|
33
|
+
g_error_free(error);
|
|
34
|
+
}
|
|
35
|
+
return bus;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// One request in flight: its Response's subscription, the object path it arrives on, and the
|
|
39
|
+
// answer, given once.
|
|
40
|
+
struct Pending {
|
|
41
|
+
GDBusConnection *connection{nullptr};
|
|
42
|
+
guint subscription{0};
|
|
43
|
+
std::string path;
|
|
44
|
+
Answer answer;
|
|
45
|
+
bool returnsRequest{true};
|
|
46
|
+
std::function<void(const std::string &)> started;
|
|
47
|
+
bool answered{false};
|
|
48
|
+
// Watches for the portal that took the request going away: its bus name vanishing, or a
|
|
49
|
+
// peer-to-peer connection closing. Either way no Response is coming.
|
|
50
|
+
guint ownerWatch{0};
|
|
51
|
+
gulong closedHandler{0};
|
|
52
|
+
|
|
53
|
+
~Pending() {
|
|
54
|
+
unwatchOwner();
|
|
55
|
+
unsubscribe();
|
|
56
|
+
g_clear_object(&connection);
|
|
57
|
+
}
|
|
58
|
+
void unwatchOwner() {
|
|
59
|
+
if (ownerWatch != 0) {
|
|
60
|
+
g_bus_unwatch_name(ownerWatch);
|
|
61
|
+
ownerWatch = 0;
|
|
62
|
+
}
|
|
63
|
+
if (closedHandler != 0) {
|
|
64
|
+
g_signal_handler_disconnect(connection, closedHandler);
|
|
65
|
+
closedHandler = 0;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
void unsubscribe() {
|
|
69
|
+
if (subscription != 0) {
|
|
70
|
+
g_dbus_connection_signal_unsubscribe(connection, subscription);
|
|
71
|
+
subscription = 0;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
void finish(Outcome outcome, GVariant *results) {
|
|
75
|
+
if (answered) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
answered = true;
|
|
79
|
+
unwatchOwner();
|
|
80
|
+
unsubscribe();
|
|
81
|
+
answer(outcome, results);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// The pending requests, by an id the signal and reply callbacks carry, so a request whose answer
|
|
86
|
+
// came is gone for both (a Response after the reply's error, or the reverse).
|
|
87
|
+
std::map<guint, std::unique_ptr<Pending>> &pending() {
|
|
88
|
+
static std::map<guint, std::unique_ptr<Pending>> requests;
|
|
89
|
+
return requests;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
void finish(guint id, Outcome outcome, GVariant *results) {
|
|
93
|
+
auto it = pending().find(id);
|
|
94
|
+
if (it == pending().end()) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
auto request = std::move(it->second);
|
|
98
|
+
pending().erase(it);
|
|
99
|
+
request->finish(outcome, results);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
void responded(GDBusConnection *, const gchar *, const gchar *path, const gchar *, const gchar *, GVariant *parameters,
|
|
103
|
+
gpointer data) {
|
|
104
|
+
auto id = GPOINTER_TO_UINT(data);
|
|
105
|
+
auto it = pending().find(id);
|
|
106
|
+
if (it == pending().end() || it->second->path != path) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
guint32 response = 2;
|
|
110
|
+
GVariant *results = nullptr;
|
|
111
|
+
g_variant_get(parameters, "(u@a{sv})", &response, &results);
|
|
112
|
+
finish(id, response == 0 ? Outcome::Done : response == 1 ? Outcome::Cancelled : Outcome::Failed, results);
|
|
113
|
+
g_variant_unref(results);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
void subscribe(Pending &request, guint id) {
|
|
117
|
+
request.unsubscribe();
|
|
118
|
+
request.subscription = g_dbus_connection_signal_subscribe(
|
|
119
|
+
request.connection, gInjected ? nullptr : kBusName, "org.freedesktop.portal.Request", "Response",
|
|
120
|
+
request.path.empty() ? nullptr : request.path.c_str(), nullptr, G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE, responded, GUINT_TO_POINTER(id), nullptr);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// The portal that took the request is gone before its Response: the request fails, as a portal's
|
|
124
|
+
// Response of 2 does. A request that has not been answered by then is not timed out.
|
|
125
|
+
void ownerGone(guint id) {
|
|
126
|
+
finish(id, Outcome::Failed, nullptr);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Called once the portal has taken the request (its reply is in), so the bus name has an owner
|
|
130
|
+
// to lose.
|
|
131
|
+
void watchOwner(Pending &request, guint id) {
|
|
132
|
+
if (request.ownerWatch != 0 || request.closedHandler != 0) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (gInjected) {
|
|
136
|
+
request.closedHandler = g_signal_connect_data(
|
|
137
|
+
request.connection, "closed",
|
|
138
|
+
G_CALLBACK(+[](GDBusConnection *, gboolean, GError *, gpointer data) { ownerGone(GPOINTER_TO_UINT(data)); }),
|
|
139
|
+
GUINT_TO_POINTER(id), nullptr, GConnectFlags(0));
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
request.ownerWatch = g_bus_watch_name_on_connection(
|
|
143
|
+
request.connection, kBusName, G_BUS_NAME_WATCHER_FLAGS_NONE, nullptr,
|
|
144
|
+
[](GDBusConnection *, const gchar *, gpointer data) { ownerGone(GPOINTER_TO_UINT(data)); }, GUINT_TO_POINTER(id), nullptr);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// The path a Request for `token` has, from the connection's unique name (":1.42" makes
|
|
148
|
+
// "1_42"), as xdg-desktop-portal names it; empty on a peer-to-peer connection, which has none.
|
|
149
|
+
std::string requestPath(GDBusConnection *connection, const std::string &token) {
|
|
150
|
+
const gchar *unique = g_dbus_connection_get_unique_name(connection);
|
|
151
|
+
if (!unique) {
|
|
152
|
+
return "";
|
|
153
|
+
}
|
|
154
|
+
std::string sender = unique[0] == ':' ? unique + 1 : unique;
|
|
155
|
+
for (auto &c : sender) {
|
|
156
|
+
if (c == '.') {
|
|
157
|
+
c = '_';
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return std::string(kObjectPath) + "/request/" + sender + "/" + token;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Whether a call's error says no portal (or not this one) is there to answer, rather than that
|
|
164
|
+
// the portal refused it: the bus's own errors, from a name nobody owns to a method not offered.
|
|
165
|
+
bool absent(const GError *error) {
|
|
166
|
+
return error->domain == G_DBUS_ERROR || error->domain == G_IO_ERROR;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
void replied(GObject *source, GAsyncResult *result, gpointer data) {
|
|
170
|
+
auto id = GPOINTER_TO_UINT(data);
|
|
171
|
+
GError *error = nullptr;
|
|
172
|
+
GVariant *reply = g_dbus_connection_call_with_unix_fd_list_finish(G_DBUS_CONNECTION(source), nullptr, result, &error);
|
|
173
|
+
if (error) {
|
|
174
|
+
bool missing = absent(error);
|
|
175
|
+
if (!missing) {
|
|
176
|
+
fprintf(stderr, "[ngn] portal: %s\n", error->message);
|
|
177
|
+
}
|
|
178
|
+
g_error_free(error);
|
|
179
|
+
finish(id, missing ? Outcome::Absent : Outcome::Failed, nullptr);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
auto it = pending().find(id);
|
|
183
|
+
if (it == pending().end()) {
|
|
184
|
+
g_variant_unref(reply);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
auto &request = *it->second;
|
|
188
|
+
if (!request.returnsRequest) {
|
|
189
|
+
if (request.started && g_variant_is_of_type(reply, G_VARIANT_TYPE("(o)"))) {
|
|
190
|
+
const gchar *path = nullptr;
|
|
191
|
+
g_variant_get(reply, "(&o)", &path);
|
|
192
|
+
auto started = std::move(request.started);
|
|
193
|
+
started(path);
|
|
194
|
+
}
|
|
195
|
+
g_variant_unref(reply);
|
|
196
|
+
finish(id, Outcome::Done, nullptr);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const gchar *handle = nullptr;
|
|
200
|
+
if (g_variant_is_of_type(reply, G_VARIANT_TYPE("(o)"))) {
|
|
201
|
+
g_variant_get(reply, "(&o)", &handle);
|
|
202
|
+
}
|
|
203
|
+
// A portal older than handle_token names its Request itself: the Response is on that path. A
|
|
204
|
+
// peer-to-peer connection's subscription matches every path (its unique name, and so the path,
|
|
205
|
+
// was unknown), and its replies and signals arrive in order, so it only needs the path to match.
|
|
206
|
+
if (handle && request.path != handle) {
|
|
207
|
+
bool everyPath = request.path.empty();
|
|
208
|
+
request.path = handle;
|
|
209
|
+
if (!everyPath) {
|
|
210
|
+
subscribe(request, id);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
g_variant_unref(reply);
|
|
214
|
+
watchOwner(request, id);
|
|
215
|
+
if (request.started) {
|
|
216
|
+
auto started = std::move(request.started);
|
|
217
|
+
started(request.path);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
struct Call {
|
|
222
|
+
std::string interface;
|
|
223
|
+
std::string method;
|
|
224
|
+
GVariant *leading;
|
|
225
|
+
GVariantBuilder *options;
|
|
226
|
+
GUnixFDList *fds;
|
|
227
|
+
Answer answer;
|
|
228
|
+
bool returnsRequest;
|
|
229
|
+
std::function<void(const std::string &)> started;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
void start(std::unique_ptr<Call> call) {
|
|
233
|
+
static guint next = 0;
|
|
234
|
+
static std::atomic<int> tokens{0};
|
|
235
|
+
GDBusConnection *bus = connection();
|
|
236
|
+
if (!bus) {
|
|
237
|
+
g_variant_unref(call->leading);
|
|
238
|
+
if (call->options) {
|
|
239
|
+
g_variant_builder_unref(call->options);
|
|
240
|
+
}
|
|
241
|
+
g_clear_object(&call->fds);
|
|
242
|
+
call->answer(Outcome::Absent, nullptr);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
// A method with no Request takes no handle_token: a portal that checks its options (the
|
|
246
|
+
// Notification portal's) refuses one it does not know.
|
|
247
|
+
auto token = "ngn" + std::to_string(++tokens);
|
|
248
|
+
if (call->returnsRequest && call->options) {
|
|
249
|
+
g_variant_builder_add(call->options, "{sv}", "handle_token", g_variant_new_string(token.c_str()));
|
|
250
|
+
}
|
|
251
|
+
GVariantBuilder arguments;
|
|
252
|
+
g_variant_builder_init(&arguments, G_VARIANT_TYPE_TUPLE);
|
|
253
|
+
for (gsize i = 0, n = g_variant_n_children(call->leading); i < n; ++i) {
|
|
254
|
+
g_variant_builder_add_value(&arguments, g_variant_get_child_value(call->leading, i));
|
|
255
|
+
}
|
|
256
|
+
if (call->options) {
|
|
257
|
+
g_variant_builder_add_value(&arguments, g_variant_builder_end(call->options));
|
|
258
|
+
g_variant_builder_unref(call->options);
|
|
259
|
+
}
|
|
260
|
+
g_variant_unref(call->leading);
|
|
261
|
+
|
|
262
|
+
auto id = ++next;
|
|
263
|
+
auto request = std::make_unique<Pending>();
|
|
264
|
+
request->connection = bus;
|
|
265
|
+
request->path = requestPath(bus, token);
|
|
266
|
+
request->answer = std::move(call->answer);
|
|
267
|
+
request->returnsRequest = call->returnsRequest;
|
|
268
|
+
request->started = std::move(call->started);
|
|
269
|
+
// Subscribed before the call, so a Response sent before the reply is heard.
|
|
270
|
+
if (call->returnsRequest) {
|
|
271
|
+
subscribe(*request, id);
|
|
272
|
+
}
|
|
273
|
+
pending()[id] = std::move(request);
|
|
274
|
+
g_dbus_connection_call_with_unix_fd_list(bus, gInjected ? nullptr : kBusName, kObjectPath, call->interface.c_str(),
|
|
275
|
+
call->method.c_str(), g_variant_builder_end(&arguments), nullptr,
|
|
276
|
+
G_DBUS_CALL_FLAGS_NONE, G_MAXINT, call->fds, nullptr, replied, GUINT_TO_POINTER(id));
|
|
277
|
+
g_clear_object(&call->fds);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
} // namespace
|
|
281
|
+
|
|
282
|
+
void request(const char *interface, const char *method, GVariant *leading, GVariantBuilder *options, GUnixFDList *fds,
|
|
283
|
+
Answer answer, bool returnsRequest, std::function<void(const std::string &)> started) {
|
|
284
|
+
auto call = std::make_unique<Call>(Call{
|
|
285
|
+
.interface = interface,
|
|
286
|
+
.method = method,
|
|
287
|
+
.leading = g_variant_ref_sink(leading),
|
|
288
|
+
.options = options,
|
|
289
|
+
.fds = fds ? G_UNIX_FD_LIST(g_object_ref(fds)) : nullptr,
|
|
290
|
+
.answer = std::move(answer),
|
|
291
|
+
.returnsRequest = returnsRequest,
|
|
292
|
+
.started = std::move(started),
|
|
293
|
+
});
|
|
294
|
+
// On the main context, where the replies and signals then arrive, whichever thread asked.
|
|
295
|
+
g_main_context_invoke(nullptr, +[](gpointer data) -> gboolean {
|
|
296
|
+
start(std::unique_ptr<Call>(static_cast<Call *>(data)));
|
|
297
|
+
return G_SOURCE_REMOVE;
|
|
298
|
+
}, call.release());
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
void parentHandle(GtkWindow *window, std::function<void(const std::string &, std::function<void()>)> ready) {
|
|
302
|
+
[[maybe_unused]] GdkSurface *surface = window ? gtk_native_get_surface(GTK_NATIVE(window)) : nullptr;
|
|
303
|
+
#ifdef GDK_WINDOWING_X11
|
|
304
|
+
if (surface && GDK_IS_X11_SURFACE(surface)) {
|
|
305
|
+
char handle[32];
|
|
306
|
+
snprintf(handle, sizeof handle, "x11:%lx", static_cast<unsigned long>(gdk_x11_surface_get_xid(surface)));
|
|
307
|
+
ready(handle, [] {});
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
#endif
|
|
311
|
+
#ifdef GDK_WINDOWING_WAYLAND
|
|
312
|
+
if (surface && GDK_IS_WAYLAND_TOPLEVEL(surface)) {
|
|
313
|
+
using Ready = std::shared_ptr<std::function<void(const std::string &, std::function<void()>)>>;
|
|
314
|
+
auto shared = std::make_shared<std::function<void(const std::string &, std::function<void()>)>>(std::move(ready));
|
|
315
|
+
auto exported = gdk_wayland_toplevel_export_handle(GDK_TOPLEVEL(surface), +[](GdkToplevel *toplevel, const char *handle, gpointer data) {
|
|
316
|
+
auto &ready = **static_cast<Ready *>(data);
|
|
317
|
+
g_object_ref(toplevel);
|
|
318
|
+
ready(std::string("wayland:") + handle, [toplevel] {
|
|
319
|
+
gdk_wayland_toplevel_unexport_handle(toplevel);
|
|
320
|
+
g_object_unref(toplevel);
|
|
321
|
+
});
|
|
322
|
+
}, new Ready(shared), +[](gpointer data) { delete static_cast<Ready *>(data); });
|
|
323
|
+
if (!exported) {
|
|
324
|
+
// The compositor has no xdg-foreign: the dialog comes up unparented.
|
|
325
|
+
(*shared)("", [] {});
|
|
326
|
+
}
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
#endif
|
|
330
|
+
ready("", [] {});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
GtkWindow *frontWindow() {
|
|
334
|
+
GtkWindow *front = nullptr;
|
|
335
|
+
GListModel *windows = gtk_window_get_toplevels();
|
|
336
|
+
for (guint i = 0, n = g_list_model_get_n_items(windows); i < n; ++i) {
|
|
337
|
+
auto *window = GTK_WINDOW(g_list_model_get_item(windows, i));
|
|
338
|
+
g_object_unref(window);
|
|
339
|
+
if (gtk_widget_get_visible(GTK_WIDGET(window)) && (!front || gtk_window_is_active(window))) {
|
|
340
|
+
front = window;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return front;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
std::function<void()> subscribe(const char *interface, const char *signal, std::function<void(GVariant *)> heard) {
|
|
347
|
+
GDBusConnection *bus = connection();
|
|
348
|
+
if (!bus) {
|
|
349
|
+
return [] {};
|
|
350
|
+
}
|
|
351
|
+
using Heard = std::function<void(GVariant *)>;
|
|
352
|
+
guint id = g_dbus_connection_signal_subscribe(
|
|
353
|
+
bus, gInjected ? nullptr : kBusName, interface, signal, kObjectPath, nullptr, G_DBUS_SIGNAL_FLAGS_NONE,
|
|
354
|
+
+[](GDBusConnection *, const gchar *, const gchar *, const gchar *, const gchar *, GVariant *parameters, gpointer data) {
|
|
355
|
+
(*static_cast<Heard *>(data))(parameters);
|
|
356
|
+
},
|
|
357
|
+
new Heard(std::move(heard)), +[](gpointer data) { delete static_cast<Heard *>(data); });
|
|
358
|
+
return [bus, id] {
|
|
359
|
+
g_dbus_connection_signal_unsubscribe(bus, id);
|
|
360
|
+
g_object_unref(bus);
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
void close(const std::string &path) {
|
|
365
|
+
auto *heap = new std::string(path);
|
|
366
|
+
g_main_context_invoke(nullptr, +[](gpointer data) -> gboolean {
|
|
367
|
+
std::unique_ptr<std::string> path(static_cast<std::string *>(data));
|
|
368
|
+
// A closed Request sends no Response: it is dropped here, unanswered, with its subscription.
|
|
369
|
+
for (auto it = pending().begin(); it != pending().end(); ++it) {
|
|
370
|
+
if (it->second->path == *path) {
|
|
371
|
+
pending().erase(it);
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (GDBusConnection *bus = connection()) {
|
|
376
|
+
g_dbus_connection_call(bus, gInjected ? nullptr : kBusName, path->c_str(), "org.freedesktop.portal.Request", "Close",
|
|
377
|
+
nullptr, nullptr, G_DBUS_CALL_FLAGS_NONE, -1, nullptr, nullptr, nullptr);
|
|
378
|
+
g_object_unref(bus);
|
|
379
|
+
}
|
|
380
|
+
return G_SOURCE_REMOVE;
|
|
381
|
+
}, heap);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
void closeSession(const std::string &path) {
|
|
385
|
+
auto *heap = new std::string(path);
|
|
386
|
+
g_main_context_invoke(nullptr, +[](gpointer data) -> gboolean {
|
|
387
|
+
std::unique_ptr<std::string> path(static_cast<std::string *>(data));
|
|
388
|
+
if (GDBusConnection *bus = connection()) {
|
|
389
|
+
g_dbus_connection_call(bus, gInjected ? nullptr : kBusName, path->c_str(), "org.freedesktop.portal.Session", "Close",
|
|
390
|
+
nullptr, nullptr, G_DBUS_CALL_FLAGS_NONE, -1, nullptr, nullptr, nullptr);
|
|
391
|
+
g_object_unref(bus);
|
|
392
|
+
}
|
|
393
|
+
return G_SOURCE_REMOVE;
|
|
394
|
+
}, heap);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
size_t requestsInFlight() {
|
|
398
|
+
return pending().size();
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
GVariant *property(const char *interface, const char *name) {
|
|
402
|
+
GDBusConnection *bus = connection();
|
|
403
|
+
if (!bus) {
|
|
404
|
+
return nullptr;
|
|
405
|
+
}
|
|
406
|
+
GVariant *reply = g_dbus_connection_call_sync(bus, kBusName, kObjectPath, "org.freedesktop.DBus.Properties", "Get",
|
|
407
|
+
g_variant_new("(ss)", interface, name), G_VARIANT_TYPE("(v)"), G_DBUS_CALL_FLAGS_NONE,
|
|
408
|
+
2000, nullptr, nullptr);
|
|
409
|
+
g_object_unref(bus);
|
|
410
|
+
if (!reply) {
|
|
411
|
+
return nullptr;
|
|
412
|
+
}
|
|
413
|
+
GVariant *value = nullptr;
|
|
414
|
+
g_variant_get(reply, "(v)", &value);
|
|
415
|
+
g_variant_unref(reply);
|
|
416
|
+
return value;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
int openPipeWireRemote(const char *interface) {
|
|
420
|
+
GDBusConnection *bus = connection();
|
|
421
|
+
if (!bus) {
|
|
422
|
+
return -1;
|
|
423
|
+
}
|
|
424
|
+
GUnixFDList *fds = nullptr;
|
|
425
|
+
GVariant *reply = g_dbus_connection_call_with_unix_fd_list_sync(
|
|
426
|
+
bus, kBusName, kObjectPath, interface, "OpenPipeWireRemote", g_variant_new("(a{sv})", nullptr), G_VARIANT_TYPE("(h)"),
|
|
427
|
+
G_DBUS_CALL_FLAGS_NONE, 5000, nullptr, &fds, nullptr, nullptr);
|
|
428
|
+
g_object_unref(bus);
|
|
429
|
+
int fd = -1;
|
|
430
|
+
if (reply && fds) {
|
|
431
|
+
gint32 index = -1;
|
|
432
|
+
g_variant_get(reply, "(h)", &index);
|
|
433
|
+
fd = g_unix_fd_list_get(fds, index, nullptr);
|
|
434
|
+
}
|
|
435
|
+
if (reply) {
|
|
436
|
+
g_variant_unref(reply);
|
|
437
|
+
}
|
|
438
|
+
if (fds) {
|
|
439
|
+
g_object_unref(fds);
|
|
440
|
+
}
|
|
441
|
+
return fd;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
void useConnection(GDBusConnection *connection) {
|
|
445
|
+
g_clear_object(&gInjected);
|
|
446
|
+
if (connection) {
|
|
447
|
+
gInjected = G_DBUS_CONNECTION(g_object_ref(connection));
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
} // namespace ngn::gtk::portal
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// The XDG desktop portals (org.freedesktop.portal.Desktop on the session bus), called over GDBus
|
|
2
|
+
// with one Request and Response routine, inside a sandbox and outside one, so Flatpak is a packaging
|
|
3
|
+
// step and not a second code path. Where no portal answers (no session bus, as in the dev loop on a
|
|
4
|
+
// Mac, or no xdg-desktop-portal running), a caller falls back to GTK's own path.
|
|
5
|
+
#pragma once
|
|
6
|
+
|
|
7
|
+
#include <gio/gio.h>
|
|
8
|
+
#include <gio/gunixfdlist.h>
|
|
9
|
+
#include <gtk/gtk.h>
|
|
10
|
+
|
|
11
|
+
#include <functional>
|
|
12
|
+
#include <string>
|
|
13
|
+
|
|
14
|
+
namespace ngn::gtk::portal {
|
|
15
|
+
|
|
16
|
+
// How a portal request ended: the user's choice (Response's 0, 1 and 2), or no portal to ask.
|
|
17
|
+
enum class Outcome { Done, Cancelled, Failed, Absent };
|
|
18
|
+
|
|
19
|
+
// Answers on the main context, with the Response's results (an a{sv}, borrowed for the call), or
|
|
20
|
+
// null when the request never reached a portal.
|
|
21
|
+
using Answer = std::function<void(Outcome outcome, GVariant *results)>;
|
|
22
|
+
|
|
23
|
+
// Calls `method` of `interface` (org.freedesktop.portal.OpenURI, say) with `leading` (a tuple of
|
|
24
|
+
// the arguments before its options, floating or owned, consumed) and `options` (an a{sv} builder's
|
|
25
|
+
// end, consumed; null for a method with no options; a handle_token is added when the method returns
|
|
26
|
+
// a Request), and with `fds` (may be
|
|
27
|
+
// null) for its `h` arguments. Answers once the portal's Request has its Response. A portal with no
|
|
28
|
+
// Request (a method that returns at once, `returnsRequest` false) answers Done with null results.
|
|
29
|
+
// `started`, when given, hears the Request's object path once the portal returns it: a request
|
|
30
|
+
// that lasts until closed (Inhibit's) is ended with close(path). For a method with no Request it
|
|
31
|
+
// hears the object path the method returns, if any (CreateSession's session), before the answer.
|
|
32
|
+
void request(const char *interface, const char *method, GVariant *leading, GVariantBuilder *options, GUnixFDList *fds,
|
|
33
|
+
Answer answer, bool returnsRequest = true, std::function<void(const std::string &path)> started = {});
|
|
34
|
+
|
|
35
|
+
// Hears each `signal` of `interface` the portal sends (Notification's ActionInvoked, say), with its
|
|
36
|
+
// parameters borrowed for the call, on the main context, until the answer is called. Nothing is
|
|
37
|
+
// heard where there is no session bus.
|
|
38
|
+
std::function<void()> subscribe(const char *interface, const char *signal, std::function<void(GVariant *parameters)> heard);
|
|
39
|
+
|
|
40
|
+
// Ends the Request at `path` (org.freedesktop.portal.Request.Close), which then never answers.
|
|
41
|
+
void close(const std::string &path);
|
|
42
|
+
|
|
43
|
+
// Ends the Session at `path` (org.freedesktop.portal.Session.Close): a Location session's updates
|
|
44
|
+
// stop.
|
|
45
|
+
void closeSession(const std::string &path);
|
|
46
|
+
|
|
47
|
+
// The requests still waiting for a Response: none once each has answered or been closed.
|
|
48
|
+
size_t requestsInFlight();
|
|
49
|
+
|
|
50
|
+
// The window identifier the portals place a dialog against ("x11:<xid>" or "wayland:<handle>"),
|
|
51
|
+
// handed to `ready` on the main context; empty for no window or a windowing system with none (a
|
|
52
|
+
// Mac's). A Wayland handle is exported for the call and dropped by the returned cleanup.
|
|
53
|
+
void parentHandle(GtkWindow *window, std::function<void(const std::string &handle, std::function<void()> drop)> ready);
|
|
54
|
+
|
|
55
|
+
// The window the app is in front with: a dialog's parent, and where a launched app takes its
|
|
56
|
+
// activation token from.
|
|
57
|
+
GtkWindow *frontWindow();
|
|
58
|
+
|
|
59
|
+
// A property of the portal object's `interface` (Camera's IsCameraPresent, say), owned by the
|
|
60
|
+
// caller, or null where no portal answers. Blocks for the call, two seconds at most.
|
|
61
|
+
GVariant *property(const char *interface, const char *name);
|
|
62
|
+
|
|
63
|
+
// A file descriptor for a PipeWire connection `interface` (the Camera portal's) opens for the app,
|
|
64
|
+
// owned by the caller, or -1. Blocks for the call.
|
|
65
|
+
int openPipeWireRemote(const char *interface);
|
|
66
|
+
|
|
67
|
+
// The connection requests go over: the session bus, or, in the GTK tests, a peer-to-peer
|
|
68
|
+
// connection to a stand-in portal (null restores the session bus).
|
|
69
|
+
void useConnection(GDBusConnection *connection);
|
|
70
|
+
|
|
71
|
+
} // namespace ngn::gtk::portal
|