@ng-native/desktop 0.26.0 → 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.
Files changed (256) hide show
  1. package/README.md +6 -3
  2. package/bin/app-config.mjs +1 -0
  3. package/bin/archive.mjs +166 -0
  4. package/bin/autolink.mjs +61 -30
  5. package/bin/cli.mjs +127 -27
  6. package/bin/host.mjs +255 -58
  7. package/bin/package-linux.mjs +1394 -0
  8. package/bin/package.mjs +171 -35
  9. package/bin/plist.mjs +6 -2
  10. package/bin/png-icon.mjs +315 -0
  11. package/bin/prebuilt.mjs +147 -26
  12. package/bin/toolchain.mjs +258 -19
  13. package/native/CMakeLists.txt +64 -6
  14. package/native/appkit/CMakeLists.txt +2 -1
  15. package/native/appkit/appkit-backend.h +1 -0
  16. package/native/appkit/appkit-backend.mm +13 -0
  17. package/native/appkit/header-toolbar.mm +28 -25
  18. package/native/appkit/host-view.h +10 -0
  19. package/native/appkit/host-view.mm +146 -40
  20. package/native/appkit/input-replay.h +6 -0
  21. package/native/appkit/input-replay.mm +21 -12
  22. package/native/appkit/main.mm +11 -7
  23. package/native/appkit/native-controls.mm +13 -1
  24. package/native/appkit/split-view.mm +7 -13
  25. package/native/appkit/text-input-field.h +4 -0
  26. package/native/appkit/text-input-field.mm +20 -1
  27. package/native/cmake/curl.cmake +79 -0
  28. package/native/cmake/libraries/react-native-reanimated.cmake +5 -0
  29. package/native/cmake/libraries/react-native-worklets.cmake +5 -0
  30. package/native/cmake/linked-modules.cmake +20 -4
  31. package/native/cmake/react-native.cmake +18 -1
  32. package/native/core/include/ngn/accent-colors.h +38 -0
  33. package/native/core/include/ngn/accessibility-relations.h +65 -0
  34. package/native/core/include/ngn/accessibility.h +26 -0
  35. package/native/core/include/ngn/animated-frame-loop.h +56 -0
  36. package/native/core/include/ngn/app-info.h +24 -8
  37. package/native/core/include/ngn/app-menus.h +14 -1
  38. package/native/core/include/ngn/app-windows.h +15 -2
  39. package/native/core/include/ngn/aria-role.h +96 -0
  40. package/native/core/include/ngn/audio.h +2 -0
  41. package/native/core/include/ngn/camera.h +9 -0
  42. package/native/core/include/ngn/control-sizes.h +7 -2
  43. package/native/core/include/ngn/crash-report.h +31 -0
  44. package/native/core/include/ngn/curl-network.h +93 -0
  45. package/native/core/include/ngn/dbus-menu.h +39 -0
  46. package/native/core/include/ngn/default-menu.h +36 -0
  47. package/native/core/include/ngn/desktop-expo.h +7 -1
  48. package/native/core/include/ngn/desktop-modules.h +22 -0
  49. package/native/core/include/ngn/desktop-props.h +21 -1
  50. package/native/core/include/ngn/desktop-view-props.h +6 -0
  51. package/native/core/include/ngn/focus-order.h +10 -0
  52. package/native/core/include/ngn/header-bar.h +22 -0
  53. package/native/core/include/ngn/host-os.h +6 -0
  54. package/native/core/include/ngn/host-reload.h +21 -0
  55. package/native/core/include/ngn/host-state.h +23 -0
  56. package/native/core/include/ngn/image-source.h +26 -1
  57. package/native/core/include/ngn/input-script.h +3 -3
  58. package/native/core/include/ngn/input.h +4 -1
  59. package/native/core/include/ngn/launch-frame.h +21 -0
  60. package/native/core/include/ngn/launch-settings.h +23 -5
  61. package/native/core/include/ngn/mounting-backend.h +5 -0
  62. package/native/core/include/ngn/mounting-manager.h +1 -0
  63. package/native/core/include/ngn/native-controls.h +26 -5
  64. package/native/core/include/ngn/notification-schedule.h +74 -0
  65. package/native/core/include/ngn/notifications.h +2 -1
  66. package/native/core/include/ngn/openssl-update-crypto.h +15 -0
  67. package/native/core/include/ngn/photo-library.h +60 -20
  68. package/native/core/include/ngn/posix-locale.h +43 -0
  69. package/native/core/include/ngn/runtime.h +5 -0
  70. package/native/core/include/ngn/safe-areas.h +14 -2
  71. package/native/core/include/ngn/split-view.h +78 -0
  72. package/native/core/include/ngn/surface-metrics.h +7 -0
  73. package/native/core/include/ngn/system.h +14 -0
  74. package/native/core/include/ngn/text-input.h +13 -0
  75. package/native/core/include/ngn/text-units.h +38 -0
  76. package/native/core/include/ngn/updates.h +14 -2
  77. package/native/core/include/ngn/view-box.h +40 -0
  78. package/native/core/include/ngn/view-transform.h +15 -0
  79. package/native/core/include/ngn/web-browser.h +46 -0
  80. package/native/core/src/accent-colors.cpp +88 -0
  81. package/native/core/src/accessibility-relations.cpp +138 -0
  82. package/native/core/src/accessibility.cpp +50 -0
  83. package/native/core/src/animated-frame-loop.cpp +166 -0
  84. package/native/core/src/app-menus.cpp +26 -9
  85. package/native/core/src/app-windows.cpp +27 -6
  86. package/native/core/src/aria-role.cpp +127 -0
  87. package/native/core/src/control-sizes.cpp +9 -5
  88. package/native/core/src/crash-report.cpp +196 -0
  89. package/native/core/src/curl-network.cpp +1562 -0
  90. package/native/core/src/dbus-menu.cpp +71 -0
  91. package/native/core/src/default-menu.cpp +134 -0
  92. package/native/core/src/desktop-view-props.cpp +14 -0
  93. package/native/core/src/focus-order.cpp +19 -0
  94. package/native/core/src/header-bar.cpp +20 -0
  95. package/native/core/src/host-os.cpp +19 -0
  96. package/native/core/src/host-reload.cpp +45 -0
  97. package/native/core/src/host-state.cpp +8 -0
  98. package/native/core/src/image-source.cpp +90 -0
  99. package/native/core/src/input-dispatcher.cpp +27 -5
  100. package/native/core/src/input-script.cpp +3 -2
  101. package/native/core/src/launch-settings.cpp +76 -0
  102. package/native/core/src/libraries/react-native-reanimated.cpp +6 -1
  103. package/native/core/src/modules/app-info.cpp +55 -6
  104. package/native/core/src/modules/app-modules.cpp +1 -1
  105. package/native/core/src/modules/audio.cpp +4 -2
  106. package/native/core/src/modules/camera.cpp +8 -11
  107. package/native/core/src/modules/desktop-expo.cpp +10 -5
  108. package/native/core/src/modules/desktop-modules.cpp +106 -41
  109. package/native/core/src/modules/file-photo-picker.cpp +408 -0
  110. package/native/core/src/modules/notifications.cpp +13 -17
  111. package/native/core/src/modules/photo-library.cpp +42 -13
  112. package/native/core/src/modules/web-browser.cpp +89 -0
  113. package/native/core/src/mounting-manager.cpp +12 -0
  114. package/native/core/src/native-controls.cpp +42 -1
  115. package/native/core/src/notification-schedule.cpp +429 -0
  116. package/native/core/src/openssl-update-crypto.cpp +83 -0
  117. package/native/core/src/posix-locale.cpp +192 -0
  118. package/native/core/src/runtime.cpp +30 -1
  119. package/native/core/src/safe-areas.cpp +19 -4
  120. package/native/core/src/spawn-rules.cpp +4 -1
  121. package/native/core/src/split-view.cpp +93 -0
  122. package/native/core/src/system.cpp +19 -0
  123. package/native/core/src/text-input.cpp +18 -1
  124. package/native/core/src/text-units.cpp +99 -0
  125. package/native/core/src/updates.cpp +76 -17
  126. package/native/core/src/view-box.cpp +75 -40
  127. package/native/core/src/view-transform.cpp +28 -0
  128. package/native/docker/Dockerfile +38 -0
  129. package/native/docker/Dockerfile.fedora +36 -0
  130. package/native/docker/glibc-floor.sh +49 -0
  131. package/native/gtk/CMakeLists.txt +137 -0
  132. package/native/gtk/accent-colour.cpp +57 -0
  133. package/native/gtk/accent-colour.h +41 -0
  134. package/native/gtk/accessibility-settings.cpp +159 -0
  135. package/native/gtk/accessibility-settings.h +62 -0
  136. package/native/gtk/accessible-text.cpp +151 -0
  137. package/native/gtk/accessible-text.h +49 -0
  138. package/native/gtk/accessible.cpp +306 -0
  139. package/native/gtk/accessible.h +75 -0
  140. package/native/gtk/alerts.cpp +267 -0
  141. package/native/gtk/alerts.h +30 -0
  142. package/native/gtk/animation-frames.cpp +159 -0
  143. package/native/gtk/animation-frames.h +20 -0
  144. package/native/gtk/app-info.cpp +91 -0
  145. package/native/gtk/app-info.h +25 -0
  146. package/native/gtk/app-windows.cpp +439 -0
  147. package/native/gtk/app-windows.h +150 -0
  148. package/native/gtk/audio.cpp +370 -0
  149. package/native/gtk/audio.h +54 -0
  150. package/native/gtk/auth-session.cpp +73 -0
  151. package/native/gtk/auth-session.h +16 -0
  152. package/native/gtk/battery.cpp +110 -0
  153. package/native/gtk/battery.h +19 -0
  154. package/native/gtk/box-paint.cpp +262 -0
  155. package/native/gtk/box-paint.h +32 -0
  156. package/native/gtk/cairo-canvas.cpp +755 -0
  157. package/native/gtk/cairo-canvas.h +49 -0
  158. package/native/gtk/camera.cpp +909 -0
  159. package/native/gtk/camera.h +69 -0
  160. package/native/gtk/capture.cpp +46 -0
  161. package/native/gtk/capture.h +19 -0
  162. package/native/gtk/clock-format.cpp +151 -0
  163. package/native/gtk/clock-format.h +36 -0
  164. package/native/gtk/crypto.cpp +75 -0
  165. package/native/gtk/crypto.h +14 -0
  166. package/native/gtk/dbus-properties.cpp +131 -0
  167. package/native/gtk/dbus-properties.h +61 -0
  168. package/native/gtk/drag-and-drop.cpp +339 -0
  169. package/native/gtk/drag-and-drop.h +30 -0
  170. package/native/gtk/file-dialogs.cpp +239 -0
  171. package/native/gtk/file-dialogs.h +21 -0
  172. package/native/gtk/file-system.cpp +212 -0
  173. package/native/gtk/file-system.h +25 -0
  174. package/native/gtk/focus.cpp +282 -0
  175. package/native/gtk/focus.h +37 -0
  176. package/native/gtk/fonts.cpp +89 -0
  177. package/native/gtk/fonts.h +22 -0
  178. package/native/gtk/gst-frames.cpp +150 -0
  179. package/native/gtk/gst-frames.h +47 -0
  180. package/native/gtk/gst-playback.cpp +496 -0
  181. package/native/gtk/gst-playback.h +81 -0
  182. package/native/gtk/gtk-backend.cpp +690 -0
  183. package/native/gtk/gtk-backend.h +134 -0
  184. package/native/gtk/host-sources.cpp +354 -0
  185. package/native/gtk/host-sources.h +46 -0
  186. package/native/gtk/image-manipulator.cpp +254 -0
  187. package/native/gtk/image-manipulator.h +30 -0
  188. package/native/gtk/image-view.cpp +256 -0
  189. package/native/gtk/image-view.h +54 -0
  190. package/native/gtk/input-replay.cpp +299 -0
  191. package/native/gtk/input-replay.h +36 -0
  192. package/native/gtk/launcher-badge.cpp +29 -0
  193. package/native/gtk/launcher-badge.h +21 -0
  194. package/native/gtk/locales.cpp +131 -0
  195. package/native/gtk/locales.h +23 -0
  196. package/native/gtk/location.cpp +274 -0
  197. package/native/gtk/location.h +19 -0
  198. package/native/gtk/main.cpp +1092 -0
  199. package/native/gtk/menus.cpp +447 -0
  200. package/native/gtk/menus.h +100 -0
  201. package/native/gtk/native-controls.cpp +831 -0
  202. package/native/gtk/native-controls.h +58 -0
  203. package/native/gtk/network-state.cpp +72 -0
  204. package/native/gtk/network-state.h +24 -0
  205. package/native/gtk/notifications.cpp +235 -0
  206. package/native/gtk/notifications.h +30 -0
  207. package/native/gtk/pango-text.cpp +274 -0
  208. package/native/gtk/pango-text.h +73 -0
  209. package/native/gtk/portal.cpp +451 -0
  210. package/native/gtk/portal.h +71 -0
  211. package/native/gtk/printing.cpp +338 -0
  212. package/native/gtk/printing.h +34 -0
  213. package/native/gtk/proxy-resolver.cpp +74 -0
  214. package/native/gtk/proxy-resolver.h +20 -0
  215. package/native/gtk/screens.cpp +1461 -0
  216. package/native/gtk/screens.h +274 -0
  217. package/native/gtk/scroll-view.cpp +414 -0
  218. package/native/gtk/scroll-view.h +82 -0
  219. package/native/gtk/secret-store.cpp +120 -0
  220. package/native/gtk/secret-store.h +27 -0
  221. package/native/gtk/settings-portal.cpp +129 -0
  222. package/native/gtk/settings-portal.h +47 -0
  223. package/native/gtk/split-view.cpp +1105 -0
  224. package/native/gtk/split-view.h +195 -0
  225. package/native/gtk/status-notifier.cpp +487 -0
  226. package/native/gtk/status-notifier.h +43 -0
  227. package/native/gtk/surface-input.cpp +251 -0
  228. package/native/gtk/surface-input.h +57 -0
  229. package/native/gtk/system-access.cpp +295 -0
  230. package/native/gtk/system-access.h +37 -0
  231. package/native/gtk/text-field.cpp +827 -0
  232. package/native/gtk/text-field.h +78 -0
  233. package/native/gtk/video.cpp +527 -0
  234. package/native/gtk/video.h +47 -0
  235. package/native/gtk/view-widget.cpp +431 -0
  236. package/native/gtk/view-widget.h +105 -0
  237. package/native/gtk/window-scale.cpp +95 -0
  238. package/native/gtk/window-scale.h +35 -0
  239. package/native/scripts/atspi-check.py +310 -0
  240. package/native/scripts/atspi-check.test.py +48 -0
  241. package/native/scripts/build.sh +20 -3
  242. package/native/scripts/compilers.sh +17 -0
  243. package/native/scripts/deb-depends-check.sh +22 -0
  244. package/native/scripts/fetch.sh +23 -2
  245. package/native/scripts/launch-cell.sh +157 -0
  246. package/native/scripts/launch-probe.sh +14 -3
  247. package/native/scripts/make-prebuilt.mjs +133 -44
  248. package/native/scripts/prebuilt-host-cache.mjs +71 -40
  249. package/native/scripts/reset-for-compilers.sh +44 -0
  250. package/native/scripts/start-xvfb.sh +35 -0
  251. package/native/shim/paragraph-platform/react/renderer/components/text/HostPlatformParagraphProps.h +35 -0
  252. package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +5 -2
  253. package/native/shim/worklets/linux-reanimated-props.h +14 -0
  254. package/native/shim/worklets/linux-worklets.h +23 -0
  255. package/package.json +6 -3
  256. package/prebuilt.json +70 -6
@@ -0,0 +1,69 @@
1
+ // The OS half of `<expo-camera>`, NgNativeCamera and ImagePicker's camera on Linux (the core's
2
+ // camera.h), over the Camera portal and GStreamer.
3
+ //
4
+ // Access is the portal's AccessCamera, which asks once and remembers: until the app has asked in
5
+ // this run, access is not determined, and asking again answers at once with what was decided.
6
+ // Whether there is a camera is the portal's IsCameraPresent; with no Camera portal there is none.
7
+ // A running camera is `pipewiresrc` on the PipeWire connection the portal opens for the app, its
8
+ // frames shown as textures (gst-frames.h). A picture is the newest frame, written as a JPEG by
9
+ // gdk-pixbuf or a PNG by GDK. A recording is a second stream from the camera into a QuickTime
10
+ // movie, H.264 where an encoder (x264 or OpenH264) and h264parse are installed and Motion JPEG
11
+ // otherwise, with AAC sound from the default microphone when it is allowed and present, an AAC
12
+ // encoder is installed and the view is not muted. A camera starts off the main loop.
13
+ //
14
+ // `facing` and `mirror` do nothing: the portal offers the cameras as one.
15
+ #pragma once
16
+
17
+ #include "view-widget.h"
18
+
19
+ #include "ngn/audio.h"
20
+ #include "ngn/camera.h"
21
+
22
+ #include <gtk/gtk.h>
23
+
24
+ #include <functional>
25
+ #include <memory>
26
+ #include <string>
27
+
28
+ namespace ngn::gtk {
29
+
30
+ struct CameraSetup {
31
+ // The camera as a gst-launch description, granted and present; empty for the portal's.
32
+ std::string source;
33
+ // The microphone a recording's sound comes from, its access and presence.
34
+ std::shared_ptr<AudioInput> microphone;
35
+ // The microphone as a gst-launch description; empty for `pulsesrc`.
36
+ std::string microphoneSource;
37
+ };
38
+
39
+ // Null when GStreamer has no `pipewiresrc` (or, with a source given, no `videoconvert`).
40
+ std::shared_ptr<CameraHardware> makeCameraHardware(CameraSetup setup);
41
+
42
+ // What the views and the sheet start cameras with, and whether they may ask: set once, as the
43
+ // host starts.
44
+ void useCameraHardware(std::shared_ptr<CameraHardware> hardware, std::shared_ptr<CameraViews> views, bool noPrompts);
45
+
46
+ // `<expo-camera>`'s picture in a view: starts and stops the camera as the view's props and the
47
+ // permission say, sending cameraReady with its first frame and mountError once when it cannot
48
+ // start. `send` gets each event and its payload.
49
+ struct CameraSurface;
50
+ std::shared_ptr<CameraSurface> makeCameraSurface(GtkWidget *view, int tag, std::function<void(const std::string &, folly::dynamic)> send);
51
+ void applyCameraSurface(CameraSurface &surface, const CameraProps &props);
52
+ GtkWidget *cameraWidget(CameraSurface &surface);
53
+ GtkPicture *cameraPicture(CameraSurface &surface);
54
+
55
+ // The video encoder a recording uses for `codec`, as a gst-launch description, by what `installed`
56
+ // says GStreamer has: H.264 (x264, then OpenH264) behind h264parse, which qtmux needs, and Motion
57
+ // JPEG for "jpeg" or where either is missing.
58
+ std::string cameraVideoEncoder(const std::string &codec, const std::function<bool(const char *)> &installed);
59
+
60
+ // The host's camera screen for ImagePicker: a dialog over the front window with the preview,
61
+ // Cancel (Escape) and the shutter, and a Photo and Video switch for a request that takes either.
62
+ std::shared_ptr<CameraSheet> makeCameraSheet();
63
+ // The dialog the sheet shows now, or null; its preview, shutter and cancel buttons, for the tests.
64
+ GtkWindow *cameraSheetWindow();
65
+ GtkPicture *cameraSheetPreview();
66
+ GtkWidget *cameraSheetShutter();
67
+ GtkWidget *cameraSheetCancel();
68
+
69
+ } // namespace ngn::gtk
@@ -0,0 +1,46 @@
1
+ #include "capture.h"
2
+
3
+ #include "window-scale.h"
4
+
5
+ namespace ngn::gtk {
6
+
7
+ GdkTexture *renderWidget(GtkWidget *widget) {
8
+ int width = gtk_widget_get_width(widget);
9
+ int height = gtk_widget_get_height(widget);
10
+ // GSK's cairo renderer, on no surface: the GL renderer of GTK 4.12 on Mesa's software GL hands
11
+ // back the texture of its first render before it has finished drawing it.
12
+ static GskRenderer *renderer = [] {
13
+ GskRenderer *cairo = gsk_cairo_renderer_new();
14
+ return gsk_renderer_realize(cairo, nullptr, nullptr) ? cairo : nullptr;
15
+ }();
16
+ if (width <= 0 || height <= 0 || !renderer) {
17
+ return nullptr;
18
+ }
19
+ float scale = static_cast<float>(windowScale(widget));
20
+ GdkPaintable *paintable = gtk_widget_paintable_new(widget);
21
+ GtkSnapshot *snapshot = gtk_snapshot_new();
22
+ gtk_snapshot_scale(snapshot, scale, scale);
23
+ gdk_paintable_snapshot(paintable, snapshot, width, height);
24
+ g_object_unref(paintable);
25
+ GskRenderNode *node = gtk_snapshot_free_to_node(snapshot);
26
+ if (!node) {
27
+ return nullptr;
28
+ }
29
+ graphene_rect_t bounds;
30
+ graphene_rect_init(&bounds, 0, 0, static_cast<float>(width) * scale, static_cast<float>(height) * scale);
31
+ GdkTexture *texture = gsk_renderer_render_texture(renderer, node, &bounds);
32
+ gsk_render_node_unref(node);
33
+ return texture;
34
+ }
35
+
36
+ bool captureWidget(GtkWidget *widget, const std::string &path) {
37
+ GdkTexture *texture = renderWidget(widget);
38
+ if (!texture) {
39
+ return false;
40
+ }
41
+ bool saved = gdk_texture_save_to_png(texture, path.c_str());
42
+ g_object_unref(texture);
43
+ return saved;
44
+ }
45
+
46
+ } // namespace ngn::gtk
@@ -0,0 +1,19 @@
1
+ // A widget drawn into a texture in-process, through GSK's cairo renderer: what the host's
2
+ // NGN_CAPTURE screenshot and the GTK view tests read. A compositor screenshot is no use where
3
+ // there is none to ask (mutter alone, Xvfb) and cannot see an offscreen window.
4
+ #pragma once
5
+
6
+ #include <gtk/gtk.h>
7
+
8
+ #include <string>
9
+
10
+ namespace ngn::gtk {
11
+
12
+ // The widget as it draws now, at its window's scale. Null before it has a size. The caller unrefs
13
+ // it.
14
+ GdkTexture *renderWidget(GtkWidget *widget);
15
+
16
+ // renderWidget, saved as a PNG at `path`.
17
+ bool captureWidget(GtkWidget *widget, const std::string &path);
18
+
19
+ } // namespace ngn::gtk
@@ -0,0 +1,151 @@
1
+ #include "clock-format.h"
2
+
3
+ #include "ngn/native-controls.h"
4
+
5
+ #include <cstring>
6
+ #include <string>
7
+
8
+ namespace ngn::gtk {
9
+
10
+ namespace {
11
+
12
+ constexpr const char *kNamespace = "org.gnome.desktop.interface";
13
+ constexpr const char *kKey = "clock-format";
14
+
15
+ // The string inside a portal value, which ReadOne gives as is and the older Read wraps in another
16
+ // variant.
17
+ std::string portalString(GVariant *value) {
18
+ GVariant *inner = value ? g_variant_ref(value) : nullptr;
19
+ while (inner && g_variant_is_of_type(inner, G_VARIANT_TYPE_VARIANT)) {
20
+ GVariant *next = g_variant_get_variant(inner);
21
+ g_variant_unref(inner);
22
+ inner = next;
23
+ }
24
+ std::string out = inner && g_variant_is_of_type(inner, G_VARIANT_TYPE_STRING) ? g_variant_get_string(inner, nullptr) : "";
25
+ if (inner) {
26
+ g_variant_unref(inner);
27
+ }
28
+ return out;
29
+ }
30
+
31
+ } // namespace
32
+
33
+ ClockFormatWatcher::ClockFormatWatcher(std::function<void(std::optional<bool>)> changed, GDBusConnection *connection)
34
+ : changed_(std::move(changed)), cancellable_(g_cancellable_new()) {
35
+ if (connection) {
36
+ start(connection);
37
+ return;
38
+ }
39
+ g_bus_get(
40
+ G_BUS_TYPE_SESSION, cancellable_,
41
+ +[](GObject *, GAsyncResult *result, gpointer data) {
42
+ GError *error = nullptr;
43
+ GDBusConnection *bus = g_bus_get_finish(result, &error);
44
+ if (!bus) {
45
+ bool cancelled = g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
46
+ g_error_free(error);
47
+ // Cancelled: the watcher is gone. With no session bus, GSettings may still have the setting.
48
+ if (!cancelled) {
49
+ static_cast<ClockFormatWatcher *>(data)->readGSettings();
50
+ }
51
+ return;
52
+ }
53
+ static_cast<ClockFormatWatcher *>(data)->start(bus);
54
+ g_object_unref(bus);
55
+ },
56
+ this);
57
+ }
58
+
59
+ ClockFormatWatcher::~ClockFormatWatcher() {
60
+ // Replies still on their way find nothing to call.
61
+ g_cancellable_cancel(cancellable_);
62
+ g_clear_object(&cancellable_);
63
+ if (subscription_) {
64
+ g_dbus_connection_signal_unsubscribe(connection_, subscription_);
65
+ }
66
+ g_clear_object(&connection_);
67
+ if (settings_) {
68
+ g_signal_handlers_disconnect_by_data(settings_, this);
69
+ g_clear_object(&settings_);
70
+ }
71
+ }
72
+
73
+ void ClockFormatWatcher::start(GDBusConnection *connection) {
74
+ connection_ = G_DBUS_CONNECTION(g_object_ref(connection));
75
+ subscription_ = g_dbus_connection_signal_subscribe(
76
+ connection_, nullptr, "org.freedesktop.portal.Settings", "SettingChanged", "/org/freedesktop/portal/desktop", kNamespace,
77
+ G_DBUS_SIGNAL_FLAGS_NONE,
78
+ +[](GDBusConnection *, const gchar *, const gchar *, const gchar *, const gchar *, GVariant *params, gpointer data) {
79
+ const char *ns = nullptr;
80
+ const char *key = nullptr;
81
+ GVariant *value = nullptr;
82
+ if (!g_variant_is_of_type(params, G_VARIANT_TYPE("(ssv)"))) {
83
+ return;
84
+ }
85
+ g_variant_get(params, "(&s&sv)", &ns, &key, &value);
86
+ if (std::strcmp(key, kKey) == 0) {
87
+ static_cast<ClockFormatWatcher *>(data)->readValue(value);
88
+ }
89
+ g_variant_unref(value);
90
+ },
91
+ this, nullptr);
92
+ read("ReadOne");
93
+ }
94
+
95
+ void ClockFormatWatcher::read(const char *method) {
96
+ // ReadOne where the portal has it (version 2), Read on an older one, GSettings with no portal.
97
+ g_dbus_connection_call(
98
+ connection_, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop", "org.freedesktop.portal.Settings", method,
99
+ g_variant_new("(ss)", kNamespace, kKey), nullptr, G_DBUS_CALL_FLAGS_NONE, -1, cancellable_,
100
+ +[](GObject *connection, GAsyncResult *result, gpointer data) {
101
+ GError *error = nullptr;
102
+ GVariant *reply = g_dbus_connection_call_finish(G_DBUS_CONNECTION(connection), result, &error);
103
+ if (!reply) {
104
+ bool cancelled = g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
105
+ bool readOne = g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD) && std::strstr(error->message, "ReadOne");
106
+ g_error_free(error);
107
+ if (cancelled) {
108
+ return;
109
+ }
110
+ auto *self = static_cast<ClockFormatWatcher *>(data);
111
+ if (readOne) {
112
+ self->read("Read");
113
+ } else {
114
+ self->readGSettings();
115
+ }
116
+ return;
117
+ }
118
+ GVariant *value = g_variant_get_child_value(reply, 0);
119
+ static_cast<ClockFormatWatcher *>(data)->readValue(value);
120
+ g_variant_unref(value);
121
+ g_variant_unref(reply);
122
+ },
123
+ this);
124
+ }
125
+
126
+ void ClockFormatWatcher::readValue(GVariant *value) {
127
+ changed_(clockFormatTwelveHour(portalString(value)));
128
+ }
129
+
130
+ void ClockFormatWatcher::readGSettings() {
131
+ GSettingsSchemaSource *source = g_settings_schema_source_get_default();
132
+ GSettingsSchema *schema = source ? g_settings_schema_source_lookup(source, kNamespace, TRUE) : nullptr;
133
+ bool has = schema && g_settings_schema_has_key(schema, kKey);
134
+ if (schema) {
135
+ g_settings_schema_unref(schema);
136
+ }
137
+ if (!has) {
138
+ changed_(std::nullopt);
139
+ return;
140
+ }
141
+ settings_ = g_settings_new(kNamespace);
142
+ auto read = +[](GSettings *settings, const gchar *, gpointer data) {
143
+ gchar *format = g_settings_get_string(settings, kKey);
144
+ static_cast<ClockFormatWatcher *>(data)->changed_(clockFormatTwelveHour(format));
145
+ g_free(format);
146
+ };
147
+ g_signal_connect(settings_, "changed::clock-format", G_CALLBACK(read), this);
148
+ read(settings_, kKey, this);
149
+ }
150
+
151
+ } // namespace ngn::gtk
@@ -0,0 +1,36 @@
1
+ // GNOME's 12 or 24-hour clock (org.gnome.desktop.interface's clock-format), which the date
2
+ // picker's time follows: read through the settings portal and followed by its SettingChanged, or
3
+ // from GSettings where no portal answers and the schema is installed. With neither, or a value
4
+ // other than 12h and 24h, the answer is nothing and the locale's time format decides.
5
+ #pragma once
6
+
7
+ #include <gio/gio.h>
8
+
9
+ #include <functional>
10
+ #include <optional>
11
+
12
+ namespace ngn::gtk {
13
+
14
+ class ClockFormatWatcher {
15
+ public:
16
+ // `changed` hears the clock format once read and on each change, on the main context.
17
+ // `connection` is the bus the portal is on: the session bus when null.
18
+ explicit ClockFormatWatcher(std::function<void(std::optional<bool> twelveHour)> changed, GDBusConnection *connection = nullptr);
19
+ ~ClockFormatWatcher();
20
+ ClockFormatWatcher(const ClockFormatWatcher &) = delete;
21
+ ClockFormatWatcher &operator=(const ClockFormatWatcher &) = delete;
22
+
23
+ private:
24
+ void start(GDBusConnection *connection);
25
+ void read(const char *method);
26
+ void readValue(GVariant *value);
27
+ void readGSettings();
28
+
29
+ std::function<void(std::optional<bool>)> changed_;
30
+ GCancellable *cancellable_{nullptr};
31
+ GDBusConnection *connection_{nullptr};
32
+ guint subscription_{0};
33
+ GSettings *settings_{nullptr};
34
+ };
35
+
36
+ } // namespace ngn::gtk
@@ -0,0 +1,75 @@
1
+ #include "crypto.h"
2
+
3
+ #include <glib.h>
4
+
5
+ #include <sys/random.h>
6
+ #include <unistd.h>
7
+
8
+ #include <algorithm>
9
+ #include <cerrno>
10
+ #include <map>
11
+ #include <stdexcept>
12
+
13
+ namespace ngn::gtk {
14
+
15
+ namespace {
16
+
17
+ // Up to `count` secure random bytes into `out`: how many, or -1 with errno set.
18
+ ssize_t secureRandom(char *out, size_t count) {
19
+ #ifdef __linux__
20
+ return getrandom(out, count, 0);
21
+ #else
22
+ // The dev loop on a Mac: getentropy, 256 bytes at most a call.
23
+ size_t n = std::min<size_t>(256, count);
24
+ return getentropy(out, n) == 0 ? static_cast<ssize_t>(n) : -1;
25
+ #endif
26
+ }
27
+
28
+ class GlibCrypto final : public CryptoSource {
29
+ public:
30
+ std::optional<std::string> digest(const std::string &algorithm, const std::string &bytes) override {
31
+ static const std::map<std::string, GChecksumType> types{
32
+ {"SHA-1", G_CHECKSUM_SHA1}, {"SHA-256", G_CHECKSUM_SHA256}, {"SHA-384", G_CHECKSUM_SHA384},
33
+ {"SHA-512", G_CHECKSUM_SHA512}, {"MD5", G_CHECKSUM_MD5}};
34
+ auto type = types.find(algorithm);
35
+ if (type == types.end()) {
36
+ return std::nullopt;
37
+ }
38
+ GChecksum *checksum = g_checksum_new(type->second);
39
+ // Fed in chunks a gssize holds on every architecture.
40
+ constexpr size_t chunk = size_t{1} << 30;
41
+ for (size_t at = 0; at < bytes.size(); at += chunk) {
42
+ g_checksum_update(checksum, reinterpret_cast<const guchar *>(bytes.data() + at),
43
+ static_cast<gssize>(std::min(chunk, bytes.size() - at)));
44
+ }
45
+ std::string out(static_cast<size_t>(g_checksum_type_get_length(type->second)), '\0');
46
+ gsize length = out.size();
47
+ g_checksum_get_digest(checksum, reinterpret_cast<guint8 *>(out.data()), &length);
48
+ g_checksum_free(checksum);
49
+ out.resize(length);
50
+ return out;
51
+ }
52
+
53
+ std::string randomBytes(size_t count) override {
54
+ std::string out(count, '\0');
55
+ for (size_t filled = 0; filled < count;) {
56
+ ssize_t n = secureRandom(out.data() + filled, count - filled);
57
+ if (n < 0 && errno == EINTR) {
58
+ continue;
59
+ }
60
+ if (n <= 0) {
61
+ throw std::runtime_error("Crypto: the system's secure random source failed");
62
+ }
63
+ filled += static_cast<size_t>(n);
64
+ }
65
+ return out;
66
+ }
67
+ };
68
+
69
+ } // namespace
70
+
71
+ std::shared_ptr<CryptoSource> makeCryptoSource() {
72
+ return std::make_shared<GlibCrypto>();
73
+ }
74
+
75
+ } // namespace ngn::gtk
@@ -0,0 +1,14 @@
1
+ // The OS half of NgNativeCrypto on Linux (the core's crypto.h): GLib's GChecksum for SHA-1, SHA-256,
2
+ // SHA-384, SHA-512 and MD5, and the kernel's getrandom(2). GLib has no MD2 or MD4, for which digest
3
+ // answers nothing.
4
+ #pragma once
5
+
6
+ #include "ngn/crypto.h"
7
+
8
+ #include <memory>
9
+
10
+ namespace ngn::gtk {
11
+
12
+ std::shared_ptr<CryptoSource> makeCryptoSource();
13
+
14
+ } // namespace ngn::gtk
@@ -0,0 +1,131 @@
1
+ #include "dbus-properties.h"
2
+
3
+ #include <vector>
4
+
5
+ namespace ngn::gtk {
6
+
7
+ namespace {
8
+
9
+ bool cancelled(GError *error) {
10
+ bool was = g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
11
+ g_clear_error(&error);
12
+ return was;
13
+ }
14
+
15
+ } // namespace
16
+
17
+ DBusProperties::DBusProperties(GBusType bus, GDBusConnection *connection, std::string name, std::string path, std::string interface)
18
+ : name_(std::move(name)), path_(std::move(path)), interface_(std::move(interface)), cancellable_(g_cancellable_new()) {
19
+ // Made on the main context, where the proxy's signals then arrive.
20
+ if (connection) {
21
+ start(connection);
22
+ } else {
23
+ g_bus_get(bus, cancellable_, connected, this);
24
+ }
25
+ }
26
+
27
+ DBusProperties::~DBusProperties() {
28
+ g_cancellable_cancel(cancellable_);
29
+ g_object_unref(cancellable_);
30
+ if (proxy_) {
31
+ g_signal_handlers_disconnect_by_data(proxy_, this);
32
+ g_object_unref(proxy_);
33
+ }
34
+ }
35
+
36
+ void DBusProperties::connected(GObject *, GAsyncResult *result, gpointer data) {
37
+ GError *error = nullptr;
38
+ GDBusConnection *connection = g_bus_get_finish(result, &error);
39
+ if (!connection) {
40
+ if (cancelled(error)) {
41
+ return;
42
+ }
43
+ // No such bus: nothing is known, and that is final.
44
+ auto *self = static_cast<DBusProperties *>(data);
45
+ {
46
+ std::lock_guard lock(self->mutex_);
47
+ self->ready_ = true;
48
+ }
49
+ self->notify();
50
+ return;
51
+ }
52
+ static_cast<DBusProperties *>(data)->start(connection);
53
+ g_object_unref(connection);
54
+ }
55
+
56
+ void DBusProperties::start(GDBusConnection *connection) {
57
+ // A peer-to-peer connection has no bus, and so no names.
58
+ const char *name = g_dbus_connection_get_unique_name(connection) ? name_.c_str() : nullptr;
59
+ g_dbus_proxy_new(connection, G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES, nullptr, name, path_.c_str(), interface_.c_str(),
60
+ cancellable_, made, this);
61
+ }
62
+
63
+ void DBusProperties::made(GObject *, GAsyncResult *result, gpointer data) {
64
+ GError *error = nullptr;
65
+ GDBusProxy *proxy = g_dbus_proxy_new_finish(result, &error);
66
+ if (!proxy && cancelled(error)) {
67
+ return;
68
+ }
69
+ auto *self = static_cast<DBusProperties *>(data);
70
+ if (proxy) {
71
+ g_signal_connect(proxy, "g-properties-changed", G_CALLBACK(+[](GDBusProxy *, GVariant *, GStrv, gpointer self) {
72
+ static_cast<DBusProperties *>(self)->notify();
73
+ }), self);
74
+ // The service started, or went: its properties come, or go, all at once.
75
+ g_signal_connect(proxy, "notify::g-name-owner", G_CALLBACK(+[](GObject *, GParamSpec *, gpointer self) {
76
+ static_cast<DBusProperties *>(self)->notify();
77
+ }), self);
78
+ }
79
+ {
80
+ std::lock_guard lock(self->mutex_);
81
+ self->proxy_ = proxy;
82
+ self->ready_ = true;
83
+ }
84
+ self->notify();
85
+ }
86
+
87
+ void DBusProperties::notify() {
88
+ std::vector<std::shared_ptr<Listener>> listeners;
89
+ {
90
+ std::lock_guard lock(mutex_);
91
+ for (const auto &[id, listener] : listeners_) {
92
+ listeners.push_back(listener);
93
+ }
94
+ }
95
+ for (const auto &listener : listeners) {
96
+ // A listener stopped by an earlier one in this round, or from another thread, is not called.
97
+ std::lock_guard calling(listener->calling);
98
+ if (listener->active) {
99
+ listener->changed();
100
+ }
101
+ }
102
+ }
103
+
104
+ GVariant *DBusProperties::get(const char *property) const {
105
+ std::lock_guard lock(mutex_);
106
+ return proxy_ ? g_dbus_proxy_get_cached_property(proxy_, property) : nullptr;
107
+ }
108
+
109
+ bool DBusProperties::ready() const {
110
+ std::lock_guard lock(mutex_);
111
+ return ready_;
112
+ }
113
+
114
+ std::function<void()> DBusProperties::watch(std::function<void()> changed) {
115
+ std::lock_guard lock(mutex_);
116
+ int id = ++next_;
117
+ auto listener = std::make_shared<Listener>();
118
+ listener->changed = std::move(changed);
119
+ listeners_[id] = listener;
120
+ return [this, id, listener] {
121
+ {
122
+ std::lock_guard lock(mutex_);
123
+ listeners_.erase(id);
124
+ }
125
+ // Waits for a call in flight on another thread; the listener stopping itself is that call.
126
+ std::lock_guard calling(listener->calling);
127
+ listener->active = false;
128
+ };
129
+ }
130
+
131
+ } // namespace ngn::gtk
@@ -0,0 +1,61 @@
1
+ // A D-Bus service's object read through its properties, for the modules whose source is a system
2
+ // service with no portal (UPower, NetworkManager, power-profiles-daemon) or a portal with no
3
+ // Request (PowerProfileMonitor): a GDBusProxy made on the main context, without blocking, whose
4
+ // cached properties any thread reads. A service that is not running, or a bus that is not there,
5
+ // leaves every property null, which the caller reads as "not known".
6
+ #pragma once
7
+
8
+ #include <gio/gio.h>
9
+
10
+ #include <functional>
11
+ #include <map>
12
+ #include <memory>
13
+ #include <mutex>
14
+ #include <string>
15
+
16
+ namespace ngn::gtk {
17
+
18
+ class DBusProperties {
19
+ public:
20
+ // The object at `path` on the service `name`, on `connection` when given (a peer-to-peer one in
21
+ // the tests, where the name is not used), else on the `bus`.
22
+ DBusProperties(GBusType bus, GDBusConnection *connection, std::string name, std::string path, std::string interface);
23
+ ~DBusProperties();
24
+ DBusProperties(const DBusProperties &) = delete;
25
+ DBusProperties &operator=(const DBusProperties &) = delete;
26
+
27
+ // The property's value now, or null. The caller owns the reference.
28
+ GVariant *get(const char *property) const;
29
+
30
+ // Calls `changed` on the main context once the object has been read and each time a property
31
+ // changes, until the answer is called.
32
+ std::function<void()> watch(std::function<void()> changed);
33
+
34
+ // Whether the object has been read (or found missing): until then every property is null.
35
+ bool ready() const;
36
+
37
+ private:
38
+ static void connected(GObject *, GAsyncResult *result, gpointer data);
39
+ static void made(GObject *, GAsyncResult *result, gpointer data);
40
+ void start(GDBusConnection *connection);
41
+ void notify();
42
+
43
+ std::string name_, path_, interface_;
44
+ GCancellable *cancellable_;
45
+ mutable std::mutex mutex_;
46
+ GDBusProxy *proxy_{nullptr};
47
+ bool ready_{false};
48
+ int next_{0};
49
+
50
+ // One watch. `calling` is held for the length of each call, so a stop waits for the call in
51
+ // flight (or, from inside it, is the same thread and goes on); `active` is read under it, so
52
+ // once a stop has returned the callback never starts again.
53
+ struct Listener {
54
+ std::function<void()> changed;
55
+ std::recursive_mutex calling;
56
+ bool active{true};
57
+ };
58
+ std::map<int, std::shared_ptr<Listener>> listeners_;
59
+ };
60
+
61
+ } // namespace ngn::gtk