@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,370 @@
|
|
|
1
|
+
#include "audio.h"
|
|
2
|
+
|
|
3
|
+
#include "gst-playback.h"
|
|
4
|
+
|
|
5
|
+
#include <glib/gstdio.h>
|
|
6
|
+
|
|
7
|
+
#include <atomic>
|
|
8
|
+
#include <cmath>
|
|
9
|
+
#include <mutex>
|
|
10
|
+
|
|
11
|
+
namespace ngn::gtk {
|
|
12
|
+
|
|
13
|
+
namespace {
|
|
14
|
+
|
|
15
|
+
using Change = AudioOutput::Change;
|
|
16
|
+
|
|
17
|
+
class GstTrack final : public AudioTrack {
|
|
18
|
+
public:
|
|
19
|
+
GstTrack(const std::string &uri, const std::string &sink, double interval, std::function<void(Change)> changed)
|
|
20
|
+
: playback_(uri, {.audioSink = sink, .interval = interval > 0 ? interval : 0.5}, [changed = std::move(changed)](Playback::Change change) {
|
|
21
|
+
changed(change == Playback::Change::Ended ? Change::Ended : Change::State);
|
|
22
|
+
}) {}
|
|
23
|
+
|
|
24
|
+
TrackState state() override {
|
|
25
|
+
auto now = playback_.state();
|
|
26
|
+
TrackState out;
|
|
27
|
+
out.load = now.load == PlaybackState::Load::Ready ? TrackState::Load::Ready
|
|
28
|
+
: now.load == PlaybackState::Load::Failed ? TrackState::Load::Failed
|
|
29
|
+
: TrackState::Load::Loading;
|
|
30
|
+
out.error = now.error;
|
|
31
|
+
out.playing = now.playing;
|
|
32
|
+
out.buffering = now.buffering;
|
|
33
|
+
out.currentTime = now.currentTime;
|
|
34
|
+
out.duration = now.duration;
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
void play(double rate) override { playback_.play(rate); }
|
|
38
|
+
void pause() override { playback_.pause(); }
|
|
39
|
+
void seek(double seconds, std::function<void()> done) override { playback_.seek(seconds, std::move(done)); }
|
|
40
|
+
void setRate(double rate) override { playback_.setRate(rate); }
|
|
41
|
+
void setVolume(double volume) override { playback_.setVolume(volume); }
|
|
42
|
+
|
|
43
|
+
private:
|
|
44
|
+
Playback playback_;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
class GstOutput final : public AudioOutput {
|
|
48
|
+
public:
|
|
49
|
+
explicit GstOutput(std::string sink) : sink_(std::move(sink)) {}
|
|
50
|
+
std::unique_ptr<AudioTrack> open(const std::string &uri, double interval, std::function<void(Change)> changed) override {
|
|
51
|
+
return std::make_unique<GstTrack>(uri, sink_, interval, std::move(changed));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private:
|
|
55
|
+
std::string sink_;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// What the bus says while recording, kept by its sync handler on a streaming thread: the level's
|
|
59
|
+
// messages are read and dropped there, so a long recording queues none.
|
|
60
|
+
struct RecordingBus {
|
|
61
|
+
std::atomic<double> rms{-160};
|
|
62
|
+
std::mutex mutex;
|
|
63
|
+
std::string error;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
GstBusSyncReply onRecordingMessage(GstBus *, GstMessage *message, gpointer data) {
|
|
67
|
+
auto *bus = static_cast<RecordingBus *>(data);
|
|
68
|
+
if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ELEMENT) {
|
|
69
|
+
const GstStructure *s = gst_message_get_structure(message);
|
|
70
|
+
if (s && gst_structure_has_name(s, "level")) {
|
|
71
|
+
// A GValueArray of one RMS per channel, in dBFS.
|
|
72
|
+
const GValue *list = gst_structure_get_value(s, "rms");
|
|
73
|
+
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
|
74
|
+
auto *channels = list && G_VALUE_HOLDS(list, G_TYPE_VALUE_ARRAY) ? static_cast<GValueArray *>(g_value_get_boxed(list)) : nullptr;
|
|
75
|
+
if (channels && channels->n_values > 0) {
|
|
76
|
+
double rms = g_value_get_double(g_value_array_get_nth(channels, 0));
|
|
77
|
+
bus->rms = std::isfinite(rms) ? std::max(rms, -160.0) : -160.0;
|
|
78
|
+
}
|
|
79
|
+
G_GNUC_END_IGNORE_DEPRECATIONS
|
|
80
|
+
gst_message_unref(message);
|
|
81
|
+
return GST_BUS_DROP;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
|
|
85
|
+
GError *error = nullptr;
|
|
86
|
+
gst_message_parse_error(message, &error, nullptr);
|
|
87
|
+
std::lock_guard lock(bus->mutex);
|
|
88
|
+
bus->error = error && error->message ? error->message : "the recording failed";
|
|
89
|
+
if (error) {
|
|
90
|
+
g_error_free(error);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return GST_BUS_PASS;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
class GstRecording final : public AudioRecording {
|
|
97
|
+
public:
|
|
98
|
+
GstRecording(GstElement *pipeline, bool metering) : pipeline_(pipeline), metering_(metering) {
|
|
99
|
+
GstBus *bus = gst_element_get_bus(pipeline_);
|
|
100
|
+
gst_bus_set_sync_handler(bus, onRecordingMessage, &bus_, nullptr);
|
|
101
|
+
gst_object_unref(bus);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
~GstRecording() override {
|
|
105
|
+
stop();
|
|
106
|
+
GstBus *bus = gst_element_get_bus(pipeline_);
|
|
107
|
+
gst_bus_set_sync_handler(bus, nullptr, nullptr, nullptr);
|
|
108
|
+
gst_object_unref(bus);
|
|
109
|
+
gst_object_unref(pipeline_);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
bool record() override {
|
|
113
|
+
if (stopped_) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
started_ = true;
|
|
117
|
+
return gst_element_set_state(pipeline_, GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
void pause() override {
|
|
121
|
+
if (!stopped_) {
|
|
122
|
+
time_ = position();
|
|
123
|
+
gst_element_set_state(pipeline_, GST_STATE_PAUSED);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
void stop() override {
|
|
128
|
+
if (stopped_) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
stopped_ = true;
|
|
132
|
+
if (started_) {
|
|
133
|
+
time_ = position();
|
|
134
|
+
// The muxer writes its index at the end of the stream, so the stream ends before the pipeline
|
|
135
|
+
// stops: a paused live source sends nothing, so it runs to carry the end through.
|
|
136
|
+
gst_element_set_state(pipeline_, GST_STATE_PLAYING);
|
|
137
|
+
gst_element_send_event(pipeline_, gst_event_new_eos());
|
|
138
|
+
GstBus *bus = gst_element_get_bus(pipeline_);
|
|
139
|
+
GstMessage *done = gst_bus_timed_pop_filtered(bus, 5 * GST_SECOND, static_cast<GstMessageType>(GST_MESSAGE_EOS | GST_MESSAGE_ERROR));
|
|
140
|
+
gst_object_unref(bus);
|
|
141
|
+
// Only the end of the stream says the muxer wrote the file whole.
|
|
142
|
+
if (!done) {
|
|
143
|
+
failed_ = "the recording did not finish writing its file";
|
|
144
|
+
} else if (GST_MESSAGE_TYPE(done) == GST_MESSAGE_ERROR) {
|
|
145
|
+
GError *error = nullptr;
|
|
146
|
+
gst_message_parse_error(done, &error, nullptr);
|
|
147
|
+
failed_ = error && error->message ? error->message : "the recording failed";
|
|
148
|
+
g_clear_error(&error);
|
|
149
|
+
}
|
|
150
|
+
if (done) {
|
|
151
|
+
gst_message_unref(done);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
gst_element_set_state(pipeline_, GST_STATE_NULL);
|
|
155
|
+
if (failed_.empty()) {
|
|
156
|
+
// An error posted while it recorded, which the sync handler kept.
|
|
157
|
+
std::lock_guard lock(bus_.mutex);
|
|
158
|
+
failed_ = bus_.error;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
std::string error() override {
|
|
163
|
+
return failed_;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
bool recording() override {
|
|
167
|
+
if (stopped_) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
GstState state = GST_STATE_NULL;
|
|
171
|
+
gst_element_get_state(pipeline_, &state, nullptr, 0);
|
|
172
|
+
return state == GST_STATE_PLAYING;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
double currentTime() override {
|
|
176
|
+
return stopped_ || !recording() ? time_ : position();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
std::optional<double> metering() override {
|
|
180
|
+
if (!metering_) {
|
|
181
|
+
return std::nullopt;
|
|
182
|
+
}
|
|
183
|
+
return bus_.rms.load();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private:
|
|
187
|
+
double position() {
|
|
188
|
+
gint64 at = 0;
|
|
189
|
+
return gst_element_query_position(pipeline_, GST_FORMAT_TIME, &at) && at > 0 ? static_cast<double>(at) / GST_SECOND : time_;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
GstElement *pipeline_;
|
|
193
|
+
bool metering_;
|
|
194
|
+
RecordingBus bus_;
|
|
195
|
+
bool started_{false};
|
|
196
|
+
bool stopped_{false};
|
|
197
|
+
double time_{0};
|
|
198
|
+
std::string failed_;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
} // namespace
|
|
202
|
+
|
|
203
|
+
bool isMicrophone(const GstStructure *properties) {
|
|
204
|
+
const gchar *kind = properties ? gst_structure_get_string(properties, "device.class") : nullptr;
|
|
205
|
+
return g_strcmp0(kind, "monitor") != 0;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
namespace {
|
|
209
|
+
|
|
210
|
+
bool deviceMonitorHasMicrophone() {
|
|
211
|
+
GstDeviceMonitor *monitor = gst_device_monitor_new();
|
|
212
|
+
gst_device_monitor_add_filter(monitor, "Audio/Source", nullptr);
|
|
213
|
+
bool found = false;
|
|
214
|
+
if (gst_device_monitor_start(monitor)) {
|
|
215
|
+
GList *devices = gst_device_monitor_get_devices(monitor);
|
|
216
|
+
for (GList *each = devices; each && !found; each = each->next) {
|
|
217
|
+
GstStructure *properties = gst_device_get_properties(GST_DEVICE(each->data));
|
|
218
|
+
found = isMicrophone(properties);
|
|
219
|
+
if (properties) {
|
|
220
|
+
gst_structure_free(properties);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
g_list_free_full(devices, gst_object_unref);
|
|
224
|
+
gst_device_monitor_stop(monitor);
|
|
225
|
+
}
|
|
226
|
+
gst_object_unref(monitor);
|
|
227
|
+
return found;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
class GstInput final : public AudioInput {
|
|
231
|
+
public:
|
|
232
|
+
explicit GstInput(AudioInputSetup setup) : setup_(std::move(setup)) {
|
|
233
|
+
if (setup_.source.empty()) {
|
|
234
|
+
setup_.source = "pulsesrc";
|
|
235
|
+
}
|
|
236
|
+
if (!setup_.microphone) {
|
|
237
|
+
setup_.microphone = deviceMonitorHasMicrophone;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
Access access() override { return Access::Granted; }
|
|
242
|
+
void requestAccess(std::function<void(Access)> done) override { done(Access::Granted); }
|
|
243
|
+
bool hasMicrophone() override { return setup_.microphone(); }
|
|
244
|
+
|
|
245
|
+
std::unique_ptr<AudioRecording> prepare(const std::string &path, const RecordingSettings &settings, std::string &error) override {
|
|
246
|
+
auto elements = recordingElementsOf(settings);
|
|
247
|
+
if (!elements.error.empty()) {
|
|
248
|
+
error = elements.error;
|
|
249
|
+
return nullptr;
|
|
250
|
+
}
|
|
251
|
+
gchar *folder = g_path_get_dirname(path.c_str());
|
|
252
|
+
g_mkdir_with_parents(folder, 0700);
|
|
253
|
+
g_free(folder);
|
|
254
|
+
bool aiff = elements.muxer == "aiffmux";
|
|
255
|
+
std::string format = settings.format == "pcm" ? std::string(",format=") + pcmFormatOf(settings.bitDepth, aiff) : "";
|
|
256
|
+
std::string line = setup_.source + " ! audioconvert ! audioresample ! audio/x-raw,rate=" +
|
|
257
|
+
std::to_string(recordingRateOf(settings, elements)) + ",channels=" + std::to_string(settings.channels) +
|
|
258
|
+
format + (settings.metering ? " ! level interval=100000000" : "") +
|
|
259
|
+
(elements.encoder.empty() ? "" : " ! " + elements.encoder) + " ! " + elements.muxer +
|
|
260
|
+
" ! filesink name=file";
|
|
261
|
+
GError *failure = nullptr;
|
|
262
|
+
GstElement *pipeline = gst_parse_launch(line.c_str(), &failure);
|
|
263
|
+
if (!pipeline || failure) {
|
|
264
|
+
error = failure && failure->message ? failure->message : "the recording pipeline could not be made";
|
|
265
|
+
if (failure) {
|
|
266
|
+
g_error_free(failure);
|
|
267
|
+
}
|
|
268
|
+
if (pipeline) {
|
|
269
|
+
gst_object_unref(pipeline);
|
|
270
|
+
}
|
|
271
|
+
return nullptr;
|
|
272
|
+
}
|
|
273
|
+
GstElement *file = gst_bin_get_by_name(GST_BIN(pipeline), "file");
|
|
274
|
+
g_object_set(file, "location", path.c_str(), nullptr);
|
|
275
|
+
gst_object_unref(file);
|
|
276
|
+
auto recording = std::make_unique<GstRecording>(pipeline, settings.metering);
|
|
277
|
+
// Opens the file and the source; a live source starts no data until it records.
|
|
278
|
+
if (gst_element_set_state(pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
|
|
279
|
+
error = "the microphone or the file " + path + " could not be opened";
|
|
280
|
+
return nullptr;
|
|
281
|
+
}
|
|
282
|
+
return recording;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private:
|
|
286
|
+
// WAV's samples are little-endian, and unsigned at 8 bits; AIFF's big-endian and signed.
|
|
287
|
+
static const char *pcmFormatOf(int bits, bool aiff) {
|
|
288
|
+
switch (bits) {
|
|
289
|
+
case 8: return aiff ? "S8" : "U8";
|
|
290
|
+
case 24: return aiff ? "S24BE" : "S24LE";
|
|
291
|
+
case 32: return aiff ? "S32BE" : "S32LE";
|
|
292
|
+
default: return aiff ? "S16BE" : "S16LE";
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
AudioInputSetup setup_;
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
} // namespace
|
|
300
|
+
|
|
301
|
+
int recordingRateOf(const RecordingSettings &settings, const RecordingElements &elements) {
|
|
302
|
+
int rate = static_cast<int>(settings.sampleRate);
|
|
303
|
+
if (elements.encoder.rfind("opusenc", 0) != 0) {
|
|
304
|
+
return rate;
|
|
305
|
+
}
|
|
306
|
+
// Opus takes 8, 12, 16, 24 or 48 kHz: the lowest of them that keeps the rate asked for.
|
|
307
|
+
for (int each : {8000, 12000, 16000, 24000}) {
|
|
308
|
+
if (rate <= each) {
|
|
309
|
+
return each;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return 48000;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
RecordingElements recordingElementsOf(const RecordingSettings &settings) {
|
|
316
|
+
const std::string &ext = settings.extension;
|
|
317
|
+
auto first = [](std::initializer_list<const char *> names) -> std::string {
|
|
318
|
+
for (const char *name : names) {
|
|
319
|
+
if (gstreamerHas({name})) {
|
|
320
|
+
return name;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return {};
|
|
324
|
+
};
|
|
325
|
+
if (settings.format == "pcm") {
|
|
326
|
+
if (ext == ".wav") {
|
|
327
|
+
return {.encoder = "", .muxer = "wavenc"};
|
|
328
|
+
}
|
|
329
|
+
if ((ext == ".aif" || ext == ".aiff") && gstreamerHas({"aiffmux"})) {
|
|
330
|
+
return {.encoder = "", .muxer = "aiffmux"};
|
|
331
|
+
}
|
|
332
|
+
return {.error = "Linux cannot record linear PCM into a " + ext + " file; use .wav"};
|
|
333
|
+
}
|
|
334
|
+
if (ext == ".ogg" || ext == ".opus" || ext == ".webm") {
|
|
335
|
+
if (!gstreamerHas({"opusenc"})) {
|
|
336
|
+
return {.error = "GStreamer's Opus encoder (opusenc) is not installed"};
|
|
337
|
+
}
|
|
338
|
+
return {.encoder = "opusenc bitrate=" + std::to_string(settings.bitRate), .muxer = ext == ".webm" ? "webmmux" : "oggmux"};
|
|
339
|
+
}
|
|
340
|
+
if (ext != ".m4a" && ext != ".mp4") {
|
|
341
|
+
return {.error = "Linux cannot record into a " + ext + " file; use .m4a, .wav or .ogg"};
|
|
342
|
+
}
|
|
343
|
+
if (settings.format == "alac") {
|
|
344
|
+
if (!gstreamerHas({"avenc_alac"})) {
|
|
345
|
+
return {.error = "no ALAC encoder is installed (GStreamer's avenc_alac, from gst-libav)"};
|
|
346
|
+
}
|
|
347
|
+
return {.encoder = "avenc_alac", .muxer = "mp4mux"};
|
|
348
|
+
}
|
|
349
|
+
std::string aac = first({"fdkaacenc", "avenc_aac", "voaacenc"});
|
|
350
|
+
if (aac.empty()) {
|
|
351
|
+
return {.error = "no AAC encoder is installed (GStreamer's fdkaacenc, avenc_aac or voaacenc)"};
|
|
352
|
+
}
|
|
353
|
+
return {.encoder = aac + " bitrate=" + std::to_string(settings.bitRate), .muxer = "mp4mux"};
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
std::shared_ptr<AudioOutput> makeAudioOutput(std::string sink) {
|
|
357
|
+
if (!gstreamerHas({"playbin3"})) {
|
|
358
|
+
return nullptr;
|
|
359
|
+
}
|
|
360
|
+
return std::make_shared<GstOutput>(std::move(sink));
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
std::shared_ptr<AudioInput> makeAudioInput(AudioInputSetup setup) {
|
|
364
|
+
if (!gstreamerHas({"audioconvert", "audioresample"}) || (setup.source.empty() && !gstreamerHas({"pulsesrc"}))) {
|
|
365
|
+
return nullptr;
|
|
366
|
+
}
|
|
367
|
+
return std::make_shared<GstInput>(std::move(setup));
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
} // namespace ngn::gtk
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// The OS half of NgNativeAudio on Linux (the core's audio.h), over GStreamer.
|
|
2
|
+
//
|
|
3
|
+
// Playback is a `playbin3` per track (gst-playback.h), with no video, its pitch kept at any rate,
|
|
4
|
+
// into PulseAudio's sink, which PipeWire answers as well. Recording is the default source
|
|
5
|
+
// (`pulsesrc`, a PipeWire source through pipewire-pulse too) into an encoder and a muxer by the
|
|
6
|
+
// recording's extension: AAC in MPEG-4 (.m4a, .mp4), linear PCM in WAV (.wav), and Opus in Ogg
|
|
7
|
+
// (.ogg, .opus). Another extension, or a format with no encoder installed, fails to prepare with
|
|
8
|
+
// why. Metering is the `level` element's RMS of the first channel.
|
|
9
|
+
//
|
|
10
|
+
// There is no microphone permission on Linux: whoever reaches the PipeWire or PulseAudio socket may
|
|
11
|
+
// record, so access is always granted and nothing is asked. Whether there is a microphone is
|
|
12
|
+
// whether GstDeviceMonitor lists an audio source that is not a monitor of an output.
|
|
13
|
+
#pragma once
|
|
14
|
+
|
|
15
|
+
#include "ngn/audio.h"
|
|
16
|
+
|
|
17
|
+
#include <gst/gst.h>
|
|
18
|
+
|
|
19
|
+
#include <functional>
|
|
20
|
+
#include <memory>
|
|
21
|
+
#include <string>
|
|
22
|
+
|
|
23
|
+
namespace ngn::gtk {
|
|
24
|
+
|
|
25
|
+
// Null when GStreamer or playbin3 is not installed. `sink` is the audio sink's factory name, empty
|
|
26
|
+
// for GStreamer's own choice.
|
|
27
|
+
std::shared_ptr<AudioOutput> makeAudioOutput(std::string sink = "");
|
|
28
|
+
|
|
29
|
+
struct AudioInputSetup {
|
|
30
|
+
// The source, as a gst-launch description; empty for `pulsesrc`.
|
|
31
|
+
std::string source;
|
|
32
|
+
// Whether there is a microphone; empty to ask GstDeviceMonitor.
|
|
33
|
+
std::function<bool()> microphone;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// Null when GStreamer has no source to record from.
|
|
37
|
+
std::shared_ptr<AudioInput> makeAudioInput(AudioInputSetup setup = {});
|
|
38
|
+
|
|
39
|
+
// Whether an audio source GstDeviceMonitor lists, by its properties, is a microphone: PulseAudio and
|
|
40
|
+
// PipeWire list each output's monitor as a source too, with `device.class` "monitor".
|
|
41
|
+
bool isMicrophone(const GstStructure *properties);
|
|
42
|
+
|
|
43
|
+
// The encoder and muxer (gst-launch descriptions) a recording is made with, or why there are none.
|
|
44
|
+
struct RecordingElements {
|
|
45
|
+
std::string encoder;
|
|
46
|
+
std::string muxer;
|
|
47
|
+
std::string error;
|
|
48
|
+
};
|
|
49
|
+
RecordingElements recordingElementsOf(const RecordingSettings &settings);
|
|
50
|
+
// The sample rate a recording is made at: the one asked for, or for Opus, which takes only 8, 12,
|
|
51
|
+
// 16, 24 and 48 kHz, the lowest of those that keeps it.
|
|
52
|
+
int recordingRateOf(const RecordingSettings &settings, const RecordingElements &elements);
|
|
53
|
+
|
|
54
|
+
} // namespace ngn::gtk
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#include "auth-session.h"
|
|
2
|
+
|
|
3
|
+
#include "app-windows.h"
|
|
4
|
+
#include "portal.h"
|
|
5
|
+
#include "system-access.h"
|
|
6
|
+
|
|
7
|
+
#include <adwaita.h>
|
|
8
|
+
|
|
9
|
+
#include <utility>
|
|
10
|
+
|
|
11
|
+
namespace ngn::gtk {
|
|
12
|
+
|
|
13
|
+
namespace {
|
|
14
|
+
|
|
15
|
+
// The dialog while it is up (a weak pointer: null once it is gone, however it went), and whether
|
|
16
|
+
// the session took it away or the user answered it.
|
|
17
|
+
struct Prompt {
|
|
18
|
+
GtkWidget *dialog{nullptr};
|
|
19
|
+
bool done{false};
|
|
20
|
+
std::function<void()> cancelled;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// AdwMessageDialog: libadwaita 1.4's, deprecated from 1.6, where AdwAlertDialog replaces it.
|
|
24
|
+
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
|
25
|
+
void showPrompt(const std::shared_ptr<Prompt> &prompt) {
|
|
26
|
+
if (prompt->done) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
GtkWidget *dialog = adw_message_dialog_new(portal::frontWindow(), "Continue in Your Browser",
|
|
30
|
+
"Finish signing in in your browser. It brings you back here when you are done.");
|
|
31
|
+
adw_message_dialog_add_response(ADW_MESSAGE_DIALOG(dialog), "cancel", "Cancel");
|
|
32
|
+
adw_message_dialog_set_close_response(ADW_MESSAGE_DIALOG(dialog), "cancel");
|
|
33
|
+
g_signal_connect_data(
|
|
34
|
+
dialog, "response", G_CALLBACK(+[](AdwMessageDialog *, const char *, gpointer data) {
|
|
35
|
+
auto prompt = *static_cast<std::shared_ptr<Prompt> *>(data);
|
|
36
|
+
// Ending the session takes the dialog away, if its button has not closed it already.
|
|
37
|
+
if (!prompt->done) {
|
|
38
|
+
prompt->done = true;
|
|
39
|
+
prompt->cancelled();
|
|
40
|
+
}
|
|
41
|
+
}),
|
|
42
|
+
new std::shared_ptr<Prompt>(prompt), +[](gpointer data, GClosure *) { delete static_cast<std::shared_ptr<Prompt> *>(data); },
|
|
43
|
+
GConnectFlags(0));
|
|
44
|
+
prompt->dialog = dialog;
|
|
45
|
+
// The closure holds the prompt as long as the dialog lives, so the pointer's place outlives it.
|
|
46
|
+
g_object_add_weak_pointer(G_OBJECT(dialog), reinterpret_cast<gpointer *>(&prompt->dialog));
|
|
47
|
+
gtk_window_present(GTK_WINDOW(dialog));
|
|
48
|
+
}
|
|
49
|
+
G_GNUC_END_IGNORE_DEPRECATIONS
|
|
50
|
+
|
|
51
|
+
} // namespace
|
|
52
|
+
|
|
53
|
+
std::shared_ptr<LinkAuthSession> makeAuthSession() {
|
|
54
|
+
return std::make_shared<LinkAuthSession>(LinkAuthSession::Hooks{
|
|
55
|
+
.openUrl = [](const std::string &url, std::function<void(bool)> opened) { openUri(url, std::move(opened)); },
|
|
56
|
+
.prompt =
|
|
57
|
+
[](std::function<void()> cancelled) {
|
|
58
|
+
auto prompt = std::make_shared<Prompt>();
|
|
59
|
+
prompt->cancelled = std::move(cancelled);
|
|
60
|
+
onMainContext([prompt] { showPrompt(prompt); });
|
|
61
|
+
return std::function<void()>([prompt] {
|
|
62
|
+
onMainContext([prompt] {
|
|
63
|
+
prompt->done = true;
|
|
64
|
+
if (GtkWidget *dialog = std::exchange(prompt->dialog, nullptr)) {
|
|
65
|
+
g_object_remove_weak_pointer(G_OBJECT(dialog), reinterpret_cast<gpointer *>(&prompt->dialog));
|
|
66
|
+
gtk_window_destroy(GTK_WINDOW(dialog));
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
}});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
} // namespace ngn::gtk
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// WebBrowser.openAuthSessionAsync on GTK (the core's LinkAuthSession, web-browser.h): the page
|
|
2
|
+
// opens in the user's browser through the OpenURI portal, or GTK's launcher where no portal
|
|
3
|
+
// answers, and the session ends on the link back on its scheme, which the desktop hands the
|
|
4
|
+
// running app (GApplication's open: a second launch or D-Bus activation). Meanwhile a dialog over
|
|
5
|
+
// the front window says the sign-in goes on in the browser, and its Cancel cancels the session.
|
|
6
|
+
#pragma once
|
|
7
|
+
|
|
8
|
+
#include "ngn/web-browser.h"
|
|
9
|
+
|
|
10
|
+
#include <memory>
|
|
11
|
+
|
|
12
|
+
namespace ngn::gtk {
|
|
13
|
+
|
|
14
|
+
std::shared_ptr<LinkAuthSession> makeAuthSession();
|
|
15
|
+
|
|
16
|
+
} // namespace ngn::gtk
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
#include "battery.h"
|
|
2
|
+
|
|
3
|
+
#include "dbus-properties.h"
|
|
4
|
+
|
|
5
|
+
#include <algorithm>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
9
|
+
namespace ngn::gtk {
|
|
10
|
+
|
|
11
|
+
namespace {
|
|
12
|
+
|
|
13
|
+
// UPower's device types and states (org.freedesktop.UPower.Device).
|
|
14
|
+
constexpr guint32 kBatteryType = 2;
|
|
15
|
+
constexpr guint32 kCharging = 1;
|
|
16
|
+
constexpr guint32 kFullyCharged = 4;
|
|
17
|
+
constexpr guint32 kPendingCharge = 5;
|
|
18
|
+
|
|
19
|
+
template <typename T>
|
|
20
|
+
std::optional<T> property(const DBusProperties &object, const char *name, const GVariantType *type, T (*read)(GVariant *)) {
|
|
21
|
+
GVariant *value = object.get(name);
|
|
22
|
+
if (!value) {
|
|
23
|
+
return std::nullopt;
|
|
24
|
+
}
|
|
25
|
+
std::optional<T> out;
|
|
26
|
+
if (g_variant_is_of_type(value, type)) {
|
|
27
|
+
out = read(value);
|
|
28
|
+
}
|
|
29
|
+
g_variant_unref(value);
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
std::optional<bool> boolean(const DBusProperties &object, const char *name) {
|
|
34
|
+
return property<bool>(object, name, G_VARIANT_TYPE_BOOLEAN, [](GVariant *v) -> bool { return g_variant_get_boolean(v); });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
std::optional<guint32> number(const DBusProperties &object, const char *name) {
|
|
38
|
+
return property<guint32>(object, name, G_VARIANT_TYPE_UINT32, g_variant_get_uint32);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
class UPowerSource final : public PowerSource {
|
|
42
|
+
public:
|
|
43
|
+
explicit UPowerSource(GDBusConnection *connection)
|
|
44
|
+
: display_(G_BUS_TYPE_SYSTEM, connection, "org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice",
|
|
45
|
+
"org.freedesktop.UPower.Device"),
|
|
46
|
+
upower_(G_BUS_TYPE_SYSTEM, connection, "org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower"),
|
|
47
|
+
portal_(G_BUS_TYPE_SESSION, connection, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop",
|
|
48
|
+
"org.freedesktop.portal.PowerProfileMonitor"),
|
|
49
|
+
profiles_(G_BUS_TYPE_SYSTEM, connection, "net.hadess.PowerProfiles", "/net/hadess/PowerProfiles", "net.hadess.PowerProfiles") {}
|
|
50
|
+
|
|
51
|
+
PowerFacts facts() override {
|
|
52
|
+
PowerFacts facts;
|
|
53
|
+
auto state = number(display_, "State").value_or(0);
|
|
54
|
+
facts.hasBattery = boolean(display_, "IsPresent").value_or(false) && number(display_, "Type") == kBatteryType;
|
|
55
|
+
if (facts.hasBattery) {
|
|
56
|
+
GVariant *percentage = display_.get("Percentage");
|
|
57
|
+
if (percentage && g_variant_is_of_type(percentage, G_VARIANT_TYPE_DOUBLE)) {
|
|
58
|
+
facts.level = std::clamp(g_variant_get_double(percentage) / 100, 0.0, 1.0);
|
|
59
|
+
}
|
|
60
|
+
if (percentage) {
|
|
61
|
+
g_variant_unref(percentage);
|
|
62
|
+
}
|
|
63
|
+
facts.charging = state == kCharging;
|
|
64
|
+
facts.charged = state == kFullyCharged;
|
|
65
|
+
}
|
|
66
|
+
// On the adapter: UPower's own word, else what the battery's state says.
|
|
67
|
+
auto onBattery = boolean(upower_, "OnBattery");
|
|
68
|
+
facts.externalPower = onBattery ? !*onBattery : state == kCharging || state == kFullyCharged || state == kPendingCharge;
|
|
69
|
+
facts.lowPowerMode = lowPower();
|
|
70
|
+
return facts;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
std::function<void()> watch(std::function<void()> changed) override {
|
|
74
|
+
auto shared = std::make_shared<std::function<void()>>(std::move(changed));
|
|
75
|
+
auto call = [shared] { (*shared)(); };
|
|
76
|
+
std::vector<std::function<void()>> stops{display_.watch(call), upower_.watch(call), portal_.watch(call), profiles_.watch(call)};
|
|
77
|
+
return [stops] {
|
|
78
|
+
for (const auto &stop : stops) {
|
|
79
|
+
stop();
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private:
|
|
85
|
+
// The portal's answer where it has one (a sandbox reaches no system service), else the daemon's.
|
|
86
|
+
bool lowPower() {
|
|
87
|
+
if (auto saver = boolean(portal_, "power-saver-enabled")) {
|
|
88
|
+
return *saver;
|
|
89
|
+
}
|
|
90
|
+
GVariant *profile = profiles_.get("ActiveProfile");
|
|
91
|
+
bool saver = profile && g_variant_is_of_type(profile, G_VARIANT_TYPE_STRING) && std::string(g_variant_get_string(profile, nullptr)) == "power-saver";
|
|
92
|
+
if (profile) {
|
|
93
|
+
g_variant_unref(profile);
|
|
94
|
+
}
|
|
95
|
+
return saver;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
DBusProperties display_;
|
|
99
|
+
DBusProperties upower_;
|
|
100
|
+
DBusProperties portal_;
|
|
101
|
+
DBusProperties profiles_;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
} // namespace
|
|
105
|
+
|
|
106
|
+
std::shared_ptr<PowerSource> makePowerSource(GDBusConnection *connection) {
|
|
107
|
+
return std::make_shared<UPowerSource>(connection);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
} // namespace ngn::gtk
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// The OS half of NgNativeBattery on Linux (the core's battery.h): UPower's DisplayDevice, the
|
|
2
|
+
// battery the desktop shows, and whether the machine runs on battery; low power from the
|
|
3
|
+
// PowerProfileMonitor portal, else power-profiles-daemon's active profile. With no UPower the
|
|
4
|
+
// machine has no battery as far as the app can tell, and isAvailableAsync answers false.
|
|
5
|
+
#pragma once
|
|
6
|
+
|
|
7
|
+
#include "ngn/battery.h"
|
|
8
|
+
|
|
9
|
+
#include <gio/gio.h>
|
|
10
|
+
|
|
11
|
+
#include <memory>
|
|
12
|
+
|
|
13
|
+
namespace ngn::gtk {
|
|
14
|
+
|
|
15
|
+
// Made on the main context. `connection`, in the tests, is a peer-to-peer connection to stand-in
|
|
16
|
+
// services, used for every object in place of the system and session buses.
|
|
17
|
+
std::shared_ptr<PowerSource> makePowerSource(GDBusConnection *connection = nullptr);
|
|
18
|
+
|
|
19
|
+
} // namespace ngn::gtk
|