@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.
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,78 @@
1
+ // A `<text-input>` on GTK: a GtkText for a single line, a GtkTextView in a GtkScrolledWindow for
2
+ // `multiline`, inside the view, which paints the field's border and background itself, under it.
3
+ // React Native's iOS TextInput props and TextInputEventEmitter, unchanged: GTK's character offsets
4
+ // become React Native's UTF-16 units on the way out and back. Each has its own input method
5
+ // context; while it composes, a controlled value waits, as on macOS (text-input.h).
6
+ #pragma once
7
+
8
+ #include "view-widget.h"
9
+
10
+ #include <react/renderer/core/Props.h>
11
+
12
+ #include <folly/dynamic.h>
13
+
14
+ #include <gtk/gtk.h>
15
+
16
+ #include <optional>
17
+ #include <string>
18
+ #include <utility>
19
+
20
+ namespace ngn::gtk {
21
+
22
+ struct TextField {
23
+ // The GtkText, or the GtkTextView of a multiline field.
24
+ GtkWidget *text{nullptr};
25
+ // A multiline field's scrolled window, which holds its GtkTextView.
26
+ GtkWidget *scroll{nullptr};
27
+ ViewData *view{nullptr};
28
+ // How many changes the user made, which a controlled value from JavaScript must have seen.
29
+ int eventCount{0};
30
+ // A change the backend made, which JavaScript already knows of.
31
+ bool settingText{false};
32
+ // Undo, redo or Backspace putting back what the field had: the user's change, but not fitted
33
+ // again.
34
+ bool replaying{false};
35
+ // Whether an input method is composing: its preedit text is not yet the field's.
36
+ bool composing{false};
37
+ // A controlled value that arrived while an input method composed, with the event count it had
38
+ // seen: applied when composition ends, unless the user changed the text since or a newer value
39
+ // replaced it.
40
+ struct Pending {
41
+ std::string text;
42
+ int seen;
43
+ // The UTF-16 range a setTextAndSelection asked for, selected once the text is set.
44
+ std::optional<std::pair<int, int>> selection;
45
+ };
46
+ std::optional<Pending> pendingText;
47
+ // The idle source that applies pendingText after composition ends, so that a commit following
48
+ // the empty preedit lands first and makes the value stale. Zero when none.
49
+ unsigned applyPendingSource{0};
50
+ // The props last applied, against which a `selection` prop is new. The same props again (a
51
+ // layout update) never put their controlled value back over a setTextAndSelection.
52
+ facebook::react::Props::Shared applied;
53
+ };
54
+
55
+ // The UTF-16 length of the first `characters` characters of `utf8`.
56
+ int utf16Units(const std::string &utf8, int characters);
57
+ // The characters in the first `units` UTF-16 units of `utf8`.
58
+ int charactersIn(const std::string &utf8, int units);
59
+
60
+ // Makes `view`'s field, child of `widget`, from its props: multiline or not, which a view keeps.
61
+ void makeTextField(GtkWidget *widget, ViewData &view);
62
+ void destroyTextField(ViewData &view);
63
+ // The field whose GtkText or GtkTextView `widget` is, or null.
64
+ TextField *textFieldOf(GtkWidget *widget);
65
+ // The field's child of its view: its scrolled window when multiline, else its GtkText.
66
+ GtkWidget *fieldWidget(const TextField &field);
67
+ // The field's text, as JavaScript reads it.
68
+ std::string fieldText(const TextField &field);
69
+ // Applies `view.props` to its field.
70
+ void applyTextFieldProps(ViewData &view);
71
+ // Places the field in the view's content box: a single line centred down it, many filling it.
72
+ void allocateTextField(ViewData &view, int width, int height);
73
+ // A multiline field's placeholder, while it is empty: GtkTextView has none of its own.
74
+ void snapshotPlaceholder(const ViewData &view, GtkSnapshot *snapshot);
75
+ // focus, blur and setTextAndSelection.
76
+ void textFieldCommand(ViewData &view, const std::string &name, const folly::dynamic &args);
77
+
78
+ } // namespace ngn::gtk
@@ -0,0 +1,527 @@
1
+ #include "video.h"
2
+
3
+ #include "gst-frames.h"
4
+ #include "gst-playback.h"
5
+
6
+ #include <map>
7
+ #include <mutex>
8
+ #include <optional>
9
+ #include <vector>
10
+
11
+ namespace ngn::gtk {
12
+
13
+ namespace {
14
+
15
+ class GstVideoPlayer;
16
+
17
+ // A live player by its id, which its views find it by: its frames, and the player while it lives.
18
+ struct PlayerLink {
19
+ std::mutex mutex;
20
+ GstVideoPlayer *player{nullptr};
21
+ std::shared_ptr<Frames> frames = std::make_shared<Frames>();
22
+ };
23
+
24
+ std::mutex gLinksMutex;
25
+ std::map<int, std::weak_ptr<PlayerLink>> gLinks;
26
+
27
+ std::shared_ptr<PlayerLink> linkFor(int id) {
28
+ std::lock_guard lock(gLinksMutex);
29
+ auto found = gLinks.find(id);
30
+ return found != gLinks.end() ? found->second.lock() : nullptr;
31
+ }
32
+
33
+ using Change = VideoPlayback::Change;
34
+
35
+ class GstVideoPlayer final : public VideoPlayback {
36
+ public:
37
+ GstVideoPlayer(int id, std::string audioSink) : id_(id), audioSink_(std::move(audioSink)) {
38
+ link_->player = this;
39
+ std::lock_guard lock(gLinksMutex);
40
+ gLinks[id] = link_;
41
+ }
42
+
43
+ ~GstVideoPlayer() override {
44
+ {
45
+ std::lock_guard lock(link_->mutex);
46
+ link_->player = nullptr;
47
+ }
48
+ {
49
+ std::lock_guard lock(gLinksMutex);
50
+ if (auto found = gLinks.find(id_); found != gLinks.end() && found->second.lock() == link_) {
51
+ gLinks.erase(found);
52
+ }
53
+ }
54
+ std::lock_guard lock(mutex_);
55
+ playback_.reset();
56
+ Frames::post(link_->frames, nullptr);
57
+ }
58
+
59
+ void load(const std::optional<std::string> &uri, std::function<void(Change)> changed) override {
60
+ std::lock_guard lock(mutex_);
61
+ playback_.reset();
62
+ Frames::post(link_->frames, nullptr);
63
+ if (!uri) {
64
+ return;
65
+ }
66
+ uri_ = *uri;
67
+ changed_ = std::move(changed);
68
+ open();
69
+ }
70
+
71
+ VideoState state() override {
72
+ std::lock_guard lock(mutex_);
73
+ VideoState out;
74
+ if (!playback_) {
75
+ return out;
76
+ }
77
+ auto now = playback_->state();
78
+ out.load = now.load == PlaybackState::Load::Ready ? VideoState::Load::Ready
79
+ : now.load == PlaybackState::Load::Failed ? VideoState::Load::Failed
80
+ : VideoState::Load::Loading;
81
+ out.error = now.error;
82
+ out.playing = now.playing;
83
+ out.buffering = now.buffering;
84
+ out.currentTime = now.currentTime;
85
+ out.duration = now.duration;
86
+ out.bufferedPosition = now.bufferedPosition;
87
+ return out;
88
+ }
89
+
90
+ void play(double rate) override {
91
+ std::lock_guard lock(mutex_);
92
+ rate_ = rate;
93
+ if (playback_) {
94
+ playback_->play(rate);
95
+ }
96
+ }
97
+
98
+ // The view's controls: at the rate last asked for.
99
+ void resume() {
100
+ std::lock_guard lock(mutex_);
101
+ if (playback_) {
102
+ if (playback_->state().ended) {
103
+ playback_->seek(0, nullptr);
104
+ }
105
+ playback_->play(rate_);
106
+ }
107
+ }
108
+
109
+ void pause() override {
110
+ std::lock_guard lock(mutex_);
111
+ if (playback_) {
112
+ playback_->pause();
113
+ }
114
+ }
115
+
116
+ void seek(double seconds, std::function<void()> done) override {
117
+ std::lock_guard lock(mutex_);
118
+ if (playback_) {
119
+ playback_->seek(seconds, std::move(done));
120
+ } else if (done) {
121
+ done();
122
+ }
123
+ }
124
+
125
+ void setRate(double rate) override {
126
+ std::lock_guard lock(mutex_);
127
+ rate_ = rate;
128
+ if (playback_) {
129
+ playback_->setRate(rate);
130
+ }
131
+ }
132
+
133
+ void setVolume(double volume) override {
134
+ std::lock_guard lock(mutex_);
135
+ volume_ = volume;
136
+ if (playback_) {
137
+ playback_->setVolume(volume);
138
+ }
139
+ }
140
+
141
+ void setMuted(bool muted) override {
142
+ std::lock_guard lock(mutex_);
143
+ muted_ = muted;
144
+ if (playback_) {
145
+ playback_->setMuted(muted);
146
+ }
147
+ }
148
+
149
+ // playbin3 takes its audio filter only as it is made: a source playing is opened again with the
150
+ // new one, at its place and playing as it was.
151
+ void setPreservesPitch(bool preserves) override {
152
+ std::lock_guard lock(mutex_);
153
+ if (pitch_ == preserves) {
154
+ return;
155
+ }
156
+ pitch_ = preserves;
157
+ if (!playback_) {
158
+ return;
159
+ }
160
+ auto was = playback_->state();
161
+ open();
162
+ playback_->seek(was.currentTime, nullptr);
163
+ if (was.playing) {
164
+ playback_->play(rate_);
165
+ }
166
+ }
167
+
168
+ void setTimeUpdates(double interval) override {
169
+ std::lock_guard lock(mutex_);
170
+ interval_ = interval;
171
+ if (playback_) {
172
+ playback_->setInterval(interval);
173
+ }
174
+ }
175
+
176
+ std::shared_ptr<PlayerLink> link() const { return link_; }
177
+
178
+ private:
179
+ // A new pipeline for the source, with the settings as they are. Under `mutex_`.
180
+ void open() {
181
+ playback_.reset();
182
+ auto changed = changed_;
183
+ playback_ = std::make_unique<Playback>(
184
+ uri_,
185
+ Playback::Options{.audioSink = audioSink_, .videoSink = makeFrameSink(link_->frames, true), .video = true, .preservePitch = pitch_,
186
+ .interval = interval_},
187
+ [changed](Playback::Change change) {
188
+ changed(change == Playback::Change::Ended ? Change::Ended : change == Playback::Change::Time ? Change::Time : Change::State);
189
+ });
190
+ playback_->setVolume(volume_);
191
+ playback_->setMuted(muted_);
192
+ }
193
+
194
+ std::string uri_;
195
+ std::function<void(Change)> changed_;
196
+ int id_;
197
+ std::string audioSink_;
198
+ std::shared_ptr<PlayerLink> link_ = std::make_shared<PlayerLink>();
199
+ std::mutex mutex_;
200
+ std::unique_ptr<Playback> playback_;
201
+ double rate_{1};
202
+ double volume_{1};
203
+ bool muted_{false};
204
+ bool pitch_{true};
205
+ double interval_{0};
206
+ };
207
+
208
+ class GstVideoOutput final : public VideoOutput {
209
+ public:
210
+ explicit GstVideoOutput(std::string audioSink) : audioSink_(std::move(audioSink)) {}
211
+ std::unique_ptr<VideoPlayback> make(int id) override { return std::make_unique<GstVideoPlayer>(id, audioSink_); }
212
+
213
+ private:
214
+ std::string audioSink_;
215
+ };
216
+
217
+ // Runs `fn` with the live player `link` holds, if it still lives.
218
+ template <typename Fn>
219
+ void withPlayer(const std::shared_ptr<PlayerLink> &link, Fn fn) {
220
+ if (!link) {
221
+ return;
222
+ }
223
+ std::lock_guard lock(link->mutex);
224
+ if (link->player) {
225
+ fn(*link->player);
226
+ }
227
+ }
228
+
229
+ GtkContentFit fitOf(VideoViewModel::Fit fit) {
230
+ switch (fit) {
231
+ case VideoViewModel::Fit::Cover: return GTK_CONTENT_FIT_COVER;
232
+ case VideoViewModel::Fit::Fill: return GTK_CONTENT_FIT_FILL;
233
+ default: return GTK_CONTENT_FIT_CONTAIN;
234
+ }
235
+ }
236
+
237
+ } // namespace
238
+
239
+ struct VideoSurface {
240
+ // Fills the view, and moves with its children to a widget that replaces it.
241
+ GtkWidget *overlay{nullptr};
242
+ GtkPicture *picture{nullptr};
243
+ GtkWidget *controls{nullptr};
244
+ GtkWidget *playButton{nullptr};
245
+ GtkWidget *scale{nullptr};
246
+ GtkWidget *fullButton{nullptr};
247
+ std::function<void(const std::string &)> events;
248
+ // The view is going: it sends nothing more.
249
+ bool detached{false};
250
+ VideoViewModel model;
251
+ std::shared_ptr<PlayerLink> link;
252
+ int watcher{-1};
253
+ bool firstShown{false};
254
+ GtkWindow *fullscreen{nullptr};
255
+ GtkPicture *fullPicture{nullptr};
256
+ int fullWatcher{-1};
257
+ guint timer{0};
258
+ // The position the controls last set on the scale, so its own change is not taken for a drag.
259
+ bool updating{false};
260
+
261
+ ~VideoSurface();
262
+ void send(const char *name) {
263
+ if (!detached && events) {
264
+ events(name);
265
+ }
266
+ }
267
+ };
268
+
269
+ namespace {
270
+
271
+ void unwatch(const std::shared_ptr<PlayerLink> &link, int &watcher) {
272
+ if (link && watcher >= 0) {
273
+ link->frames->unwatch(watcher);
274
+ }
275
+ watcher = -1;
276
+ }
277
+
278
+ int watch(const std::shared_ptr<PlayerLink> &link, std::function<void(GdkTexture *)> shown) {
279
+ return link->frames->watch(std::move(shown));
280
+ }
281
+
282
+ void updateControls(VideoSurface &s) {
283
+ if (!s.controls) {
284
+ return;
285
+ }
286
+ std::optional<VideoState> state;
287
+ withPlayer(s.link, [&](GstVideoPlayer &player) { state = player.state(); });
288
+ bool playing = state && state->playing;
289
+ gtk_button_set_icon_name(GTK_BUTTON(s.playButton), playing ? "media-playback-pause-symbolic" : "media-playback-start-symbolic");
290
+ gtk_widget_set_tooltip_text(s.playButton, playing ? "Pause" : "Play");
291
+ s.updating = true;
292
+ GtkAdjustment *adjustment = gtk_range_get_adjustment(GTK_RANGE(s.scale));
293
+ gtk_adjustment_set_upper(adjustment, state && state->duration > 0 ? state->duration : 1);
294
+ gtk_adjustment_set_value(adjustment, state ? state->currentTime : 0);
295
+ s.updating = false;
296
+ gtk_widget_set_sensitive(s.scale, state && state->duration > 0);
297
+ }
298
+
299
+ void closeFullscreen(VideoSurface &s) {
300
+ if (!s.fullscreen) {
301
+ return;
302
+ }
303
+ unwatch(s.link, s.fullWatcher);
304
+ GtkWindow *window = std::exchange(s.fullscreen, nullptr);
305
+ s.fullPicture = nullptr;
306
+ g_signal_handlers_disconnect_by_data(window, &s);
307
+ gtk_window_destroy(window);
308
+ // Sent even once detached: expo-video's view is gone, but the event is the last word on it.
309
+ if (s.events) {
310
+ s.events("fullscreenExit");
311
+ }
312
+ }
313
+
314
+ void attachPlayer(VideoSurface &s) {
315
+ std::shared_ptr<PlayerLink> link = s.model.player ? linkFor(*s.model.player) : nullptr;
316
+ if (link == s.link) {
317
+ return;
318
+ }
319
+ unwatch(s.link, s.watcher);
320
+ unwatch(s.link, s.fullWatcher);
321
+ s.link = link;
322
+ s.firstShown = false;
323
+ gtk_picture_set_paintable(s.picture, nullptr);
324
+ if (s.fullPicture) {
325
+ gtk_picture_set_paintable(s.fullPicture, nullptr);
326
+ }
327
+ if (!link) {
328
+ return;
329
+ }
330
+ auto show = [&s](GdkTexture *texture) {
331
+ gtk_picture_set_paintable(s.picture, texture ? GDK_PAINTABLE(texture) : nullptr);
332
+ if (texture && !s.firstShown) {
333
+ s.firstShown = true;
334
+ s.send("firstFrameRender");
335
+ } else if (!texture) {
336
+ // A new source: its first frame is a first frame again.
337
+ s.firstShown = false;
338
+ }
339
+ };
340
+ s.watcher = watch(link, show);
341
+ if (link->frames->shown()) {
342
+ show(link->frames->shown());
343
+ }
344
+ if (s.fullPicture) {
345
+ s.fullWatcher = watch(link, [&s](GdkTexture *texture) { gtk_picture_set_paintable(s.fullPicture, texture ? GDK_PAINTABLE(texture) : nullptr); });
346
+ gtk_picture_set_paintable(s.fullPicture, link->frames->shown() ? GDK_PAINTABLE(link->frames->shown()) : nullptr);
347
+ }
348
+ }
349
+
350
+ void openFullscreen(VideoSurface &s) {
351
+ if (s.fullscreen || s.detached) {
352
+ return;
353
+ }
354
+ GtkWidget *window = gtk_window_new();
355
+ gtk_window_set_title(GTK_WINDOW(window), "Video");
356
+ if (GtkRoot *root = gtk_widget_get_root(s.overlay); root && GTK_IS_WINDOW(root)) {
357
+ gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(root));
358
+ if (GtkApplication *app = gtk_window_get_application(GTK_WINDOW(root))) {
359
+ gtk_window_set_application(GTK_WINDOW(window), app);
360
+ }
361
+ }
362
+ GtkWidget *picture = gtk_picture_new();
363
+ gtk_picture_set_content_fit(GTK_PICTURE(picture), GTK_CONTENT_FIT_CONTAIN);
364
+ gtk_widget_add_css_class(window, "ngn-video-fullscreen");
365
+ gtk_window_set_child(GTK_WINDOW(window), picture);
366
+ s.fullscreen = GTK_WINDOW(window);
367
+ s.fullPicture = GTK_PICTURE(picture);
368
+ if (s.link) {
369
+ s.fullWatcher = watch(s.link, [&s](GdkTexture *texture) { gtk_picture_set_paintable(s.fullPicture, texture ? GDK_PAINTABLE(texture) : nullptr); });
370
+ gtk_picture_set_paintable(s.fullPicture, s.link->frames->shown() ? GDK_PAINTABLE(s.link->frames->shown()) : nullptr);
371
+ }
372
+ GtkEventController *keys = gtk_event_controller_key_new();
373
+ g_signal_connect(keys, "key-pressed", G_CALLBACK(+[](GtkEventControllerKey *, guint key, guint, GdkModifierType, gpointer data) -> gboolean {
374
+ if (key == GDK_KEY_Escape) {
375
+ closeFullscreen(*static_cast<VideoSurface *>(data));
376
+ return TRUE;
377
+ }
378
+ return FALSE;
379
+ }), &s);
380
+ gtk_widget_add_controller(window, keys);
381
+ g_signal_connect(window, "close-request", G_CALLBACK(+[](GtkWindow *, gpointer data) -> gboolean {
382
+ closeFullscreen(*static_cast<VideoSurface *>(data));
383
+ return TRUE;
384
+ }), &s);
385
+ // Asked, not awaited: a window manager may not make it full screen, and Xvfb has none.
386
+ gtk_window_fullscreen(GTK_WINDOW(window));
387
+ gtk_window_present(GTK_WINDOW(window));
388
+ s.send("fullscreenEnter");
389
+ }
390
+
391
+ void buildControls(VideoSurface &s) {
392
+ s.controls = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
393
+ gtk_widget_add_css_class(s.controls, "osd");
394
+ gtk_widget_add_css_class(s.controls, "toolbar");
395
+ gtk_widget_set_valign(s.controls, GTK_ALIGN_END);
396
+ gtk_widget_set_margin_start(s.controls, 8);
397
+ gtk_widget_set_margin_end(s.controls, 8);
398
+ gtk_widget_set_margin_bottom(s.controls, 8);
399
+ s.playButton = gtk_button_new_from_icon_name("media-playback-start-symbolic");
400
+ gtk_widget_add_css_class(s.playButton, "flat");
401
+ g_signal_connect(s.playButton, "clicked", G_CALLBACK(+[](GtkButton *, gpointer data) {
402
+ auto &s = *static_cast<VideoSurface *>(data);
403
+ withPlayer(s.link, [](GstVideoPlayer &player) {
404
+ if (player.state().playing) {
405
+ player.pause();
406
+ } else {
407
+ player.resume();
408
+ }
409
+ });
410
+ updateControls(s);
411
+ }), &s);
412
+ s.scale = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, 0, 1, 0.1);
413
+ gtk_scale_set_draw_value(GTK_SCALE(s.scale), FALSE);
414
+ gtk_widget_set_hexpand(s.scale, TRUE);
415
+ gtk_accessible_update_property(GTK_ACCESSIBLE(s.scale), GTK_ACCESSIBLE_PROPERTY_LABEL, "Position", -1);
416
+ g_signal_connect(s.scale, "value-changed", G_CALLBACK(+[](GtkRange *range, gpointer data) {
417
+ auto &s = *static_cast<VideoSurface *>(data);
418
+ if (s.updating) {
419
+ return;
420
+ }
421
+ double to = gtk_range_get_value(range);
422
+ withPlayer(s.link, [to](GstVideoPlayer &player) { player.seek(to, nullptr); });
423
+ }), &s);
424
+ s.fullButton = gtk_button_new_from_icon_name("view-fullscreen-symbolic");
425
+ gtk_widget_add_css_class(s.fullButton, "flat");
426
+ gtk_widget_set_tooltip_text(s.fullButton, "Full Screen");
427
+ g_signal_connect(s.fullButton, "clicked", G_CALLBACK(+[](GtkButton *, gpointer data) { openFullscreen(*static_cast<VideoSurface *>(data)); }), &s);
428
+ gtk_box_append(GTK_BOX(s.controls), s.playButton);
429
+ gtk_box_append(GTK_BOX(s.controls), s.scale);
430
+ gtk_box_append(GTK_BOX(s.controls), s.fullButton);
431
+ gtk_overlay_add_overlay(GTK_OVERLAY(s.overlay), s.controls);
432
+ s.timer = g_timeout_add(250, [](gpointer data) -> gboolean {
433
+ updateControls(*static_cast<VideoSurface *>(data));
434
+ return G_SOURCE_CONTINUE;
435
+ }, &s);
436
+ }
437
+
438
+ void dropControls(VideoSurface &s) {
439
+ if (!s.controls) {
440
+ return;
441
+ }
442
+ if (s.timer) {
443
+ g_source_remove(s.timer);
444
+ s.timer = 0;
445
+ }
446
+ gtk_overlay_remove_overlay(GTK_OVERLAY(s.overlay), s.controls);
447
+ s.controls = s.playButton = s.scale = s.fullButton = nullptr;
448
+ }
449
+
450
+ } // namespace
451
+
452
+ VideoSurface::~VideoSurface() {
453
+ dropControls(*this);
454
+ // The view is going: its emitter with it.
455
+ detached = true;
456
+ events = nullptr;
457
+ closeFullscreen(*this);
458
+ unwatch(link, watcher);
459
+ if (overlay) {
460
+ if (gtk_widget_get_parent(overlay)) {
461
+ gtk_widget_unparent(overlay);
462
+ }
463
+ g_object_unref(overlay);
464
+ }
465
+ }
466
+
467
+ std::shared_ptr<VideoOutput> makeVideoOutput(std::string audioSink) {
468
+ if (!gstreamerHas({"playbin3", "appsink", "videoconvert"})) {
469
+ return nullptr;
470
+ }
471
+ return std::make_shared<GstVideoOutput>(std::move(audioSink));
472
+ }
473
+
474
+ std::shared_ptr<VideoSurface> makeVideoSurface(GtkWidget *view, std::function<void(const std::string &)> events) {
475
+ auto s = std::make_shared<VideoSurface>();
476
+ s->events = std::move(events);
477
+ s->overlay = GTK_WIDGET(g_object_ref_sink(gtk_overlay_new()));
478
+ GtkWidget *picture = gtk_picture_new();
479
+ gtk_picture_set_can_shrink(GTK_PICTURE(picture), TRUE);
480
+ s->picture = GTK_PICTURE(picture);
481
+ gtk_overlay_set_child(GTK_OVERLAY(s->overlay), picture);
482
+ gtk_widget_set_parent(s->overlay, view);
483
+ applyVideoSurface(*s, VideoViewModel{});
484
+ return s;
485
+ }
486
+
487
+ void applyVideoSurface(VideoSurface &s, const VideoViewModel &model) {
488
+ bool controls = model.nativeControls;
489
+ s.model = model;
490
+ attachPlayer(s);
491
+ gtk_picture_set_content_fit(s.picture, fitOf(model.contentFit));
492
+ if (controls && !s.controls) {
493
+ buildControls(s);
494
+ } else if (!controls) {
495
+ dropControls(s);
496
+ }
497
+ if (s.fullButton) {
498
+ gtk_widget_set_visible(s.fullButton, model.fullscreen);
499
+ }
500
+ updateControls(s);
501
+ }
502
+
503
+ void setVideoFullscreen(VideoSurface &s, bool on) {
504
+ if (on) {
505
+ openFullscreen(s);
506
+ } else {
507
+ closeFullscreen(s);
508
+ }
509
+ }
510
+
511
+ GtkWindow *videoFullscreenWindow(VideoSurface &s) {
512
+ return s.fullscreen;
513
+ }
514
+
515
+ GtkPicture *videoPicture(VideoSurface &s) {
516
+ return s.picture;
517
+ }
518
+
519
+ GtkWidget *videoPlayButton(VideoSurface &s) {
520
+ return s.playButton;
521
+ }
522
+
523
+ GtkWidget *videoWidget(VideoSurface &s) {
524
+ return s.overlay;
525
+ }
526
+
527
+ } // namespace ngn::gtk
@@ -0,0 +1,47 @@
1
+ // The OS half of NgNativeVideo on Linux (the core's video.h), and `<expo-video>`'s view, over
2
+ // GStreamer and GTK.
3
+ //
4
+ // Each player is a `playbin3` per source (gst-playback.h) whose video goes to the views showing it
5
+ // as textures (gst-frames.h).
6
+ //
7
+ // The view is a GtkPicture with its content fit, under controls of our own (play and pause, the
8
+ // position, and full screen) when `nativeControls` is on, since GTK has no player view. Full
9
+ // screen opens a window of its own over the same frames, asked to go full screen: under a window
10
+ // manager that will not (or with none, as under Xvfb) it stays a window, and it is still left with
11
+ // Escape or by closing it. A view unmounted while its video is full screen closes that window.
12
+ // Picture in picture is absent.
13
+ #pragma once
14
+
15
+ #include "view-widget.h"
16
+
17
+ #include "ngn/video.h"
18
+
19
+ #include <gtk/gtk.h>
20
+
21
+ #include <functional>
22
+ #include <memory>
23
+ #include <string>
24
+
25
+ namespace ngn::gtk {
26
+
27
+ // Null when GStreamer, playbin3 or appsink is not installed. `audioSink` is the audio sink's
28
+ // factory name, empty for GStreamer's own choice.
29
+ std::shared_ptr<VideoOutput> makeVideoOutput(std::string audioSink = "");
30
+
31
+ // `<expo-video>`'s widget in a view: fills `view` and shows what its props say. `events` gets
32
+ // expo-video's view events by name (firstFrameRender, fullscreenEnter, fullscreenExit).
33
+ struct VideoSurface;
34
+ std::shared_ptr<VideoSurface> makeVideoSurface(GtkWidget *view, std::function<void(const std::string &)> events);
35
+ void applyVideoSurface(VideoSurface &surface, const VideoViewModel &model);
36
+ // Enters or leaves full screen, as the button does.
37
+ void setVideoFullscreen(VideoSurface &surface, bool on);
38
+ // The window full screen opened, or null.
39
+ GtkWindow *videoFullscreenWindow(VideoSurface &surface);
40
+ // The picture the view shows its player's frames in.
41
+ GtkPicture *videoPicture(VideoSurface &surface);
42
+ // The view's play and pause button, or null without controls.
43
+ GtkWidget *videoPlayButton(VideoSurface &surface);
44
+ // The widget that fills the view.
45
+ GtkWidget *videoWidget(VideoSurface &surface);
46
+
47
+ } // namespace ngn::gtk