summaryrefslogtreecommitdiff
path: root/overlays/quickshell/pipewire.patch
blob: 33025d8bc5b7d33962cc7cf815061b4fbcefbcbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
diff --git i/src/services/pipewire/device.cpp w/src/services/pipewire/device.cpp
index 616e7d0..0c55008 100644
--- i/src/services/pipewire/device.cpp
+++ w/src/services/pipewire/device.cpp
@@ -3,6 +3,7 @@
 #include <cstdint>
 #include <functional>
 #include <utility>
+#include <algorithm>
 
 #include <pipewire/device.h>
 #include <qcontainerfwd.h>
@@ -19,6 +20,8 @@
 #include <spa/pod/pod.h>
 #include <spa/pod/vararg.h>
 #include <spa/utils/type.h>
+#include <spa/monitor/device.h>
+#include <spa/utils/keys.h>
 
 #include "../../core/logcat.hpp"
 #include "core.hpp"
@@ -46,6 +49,25 @@ void PwDevice::unbindHooks() {
 	this->mWaitingForDevice = false;
 }
 
+void PwDevice::initProps(const spa_dict* props) {
+	if (const auto* deviceName = spa_dict_lookup(props, SPA_KEY_DEVICE_NAME)) {
+		this->name = deviceName;
+	}
+
+	if (const auto* deviceDesc = spa_dict_lookup(props, SPA_KEY_DEVICE_DESCRIPTION)) {
+		this->description = deviceDesc;
+	}
+
+	if (const auto* deviceNick = spa_dict_lookup(props, SPA_KEY_DEVICE_NICK)) {
+		this->nick = deviceNick;
+	}
+
+	if (const auto* mediaClass = spa_dict_lookup(props, SPA_KEY_MEDIA_CLASS)) {
+		this->type = mediaClass;
+	}
+}
+
+
 const pw_device_events PwDevice::EVENTS = {
     .version = PW_VERSION_DEVICE_EVENTS,
     .info = &PwDevice::onInfo,
@@ -71,6 +93,11 @@ void PwDevice::onInfo(void* data, const pw_device_info* info) {
 				}
 
 				break;
+			} else if (param.id == SPA_PARAM_EnumProfile && param.flags & SPA_PARAM_INFO_READ) {
+				self->validProfiles.clear();
+				pw_device_enum_params(self->proxy(), 0, param.id, 0, UINT32_MAX, nullptr);
+			} else if (param.id == SPA_PARAM_Profile && param.flags & SPA_PARAM_INFO_READ) {
+				pw_device_enum_params(self->proxy(), 0, param.id, 0, UINT32_MAX, nullptr);
 			}
 		}
 	}
@@ -97,6 +124,15 @@ void PwDevice::onParam(
 		}
 
 		self->addDeviceIndexPairs(param);
+	} else if (id == SPA_PARAM_EnumProfile) {
+		PwProfile profile = PwProfile::parseSpaPod(param);
+		self->profilesUpdated = true;
+		self->profiles.insertOrAssign(profile.index, profile);
+		self->validProfiles.insert(profile.index);
+	} else if (id == SPA_PARAM_Profile) {
+		PwProfile profile = PwProfile::parseSpaPod(param);
+		self->currentProfileUpdated = true;
+		self->currentProfile = profile;
 	}
 }
 
@@ -145,6 +181,21 @@ void PwDevice::polled() {
 			return false;
 		});
 	}
+	if (this->profilesUpdated) {
+		this->profiles.removeIf([&](const std::pair<qint32, PwProfile>& entry) {
+			return !this->validProfiles.contains(entry.first);
+		});
+		this->profilesUpdated = false;
+		QList<PwProfile> profiles = this->profiles.values();
+		std::sort(profiles.begin(), profiles.end(), [](const PwProfile& a, const PwProfile& b) { return a.index < b.index; });
+		emit this->profilesChanged(profiles);
+	}
+	if (this->currentProfileUpdated) {
+		this->currentProfileUpdated = false;
+		if (this->currentProfile) {
+			emit this->currentProfileChanged(*this->currentProfile);
+		}
+	}
 }
 
 bool PwDevice::setVolumes(qint32 routeDevice, const QVector<float>& volumes) {
@@ -182,6 +233,15 @@ bool PwDevice::setMuted(qint32 routeDevice, bool muted) {
 	});
 }
 
+void PwDevice::setProfile(qint32 profileIndex) {
+	auto buffer = std::array<uint8_t, 1024>();
+	auto builder = SPA_POD_BUILDER_INIT(buffer.data(), buffer.size());
+	auto* pod = spa_pod_builder_add_object(&builder,
+		SPA_TYPE_OBJECT_ParamProfile, SPA_PARAM_Profile,
+		SPA_PARAM_PROFILE_index, SPA_POD_Int(profileIndex));
+	pw_device_set_param(this->proxy(), SPA_PARAM_Profile, 0, static_cast<spa_pod*>(pod));
+}
+
 void PwDevice::waitForDevice() { this->mWaitingForDevice = true; }
 bool PwDevice::waitingForDevice() const { return this->mWaitingForDevice; }
 
@@ -222,4 +282,24 @@ bool PwDevice::setRouteProps(
 	return true;
 }
 
+PwProfile PwProfile::parseSpaPod(const spa_pod* param) {
+	PwProfile profile;
+
+	const auto* indexProp = spa_pod_find_prop(param, nullptr, SPA_PARAM_PROFILE_index);
+	const auto* descProp = spa_pod_find_prop(param, nullptr, SPA_PARAM_PROFILE_description);
+	const auto* nameProp = spa_pod_find_prop(param, nullptr, SPA_PARAM_PROFILE_name);
+
+	spa_pod_get_int(&indexProp->value, &profile.index);
+
+	const char* desc_cstr = nullptr;
+	spa_pod_get_string(&descProp->value, &desc_cstr);
+	profile.description = QString(desc_cstr);
+
+	const char* name_cstr = nullptr;
+	spa_pod_get_string(&nameProp->value, &name_cstr);
+	profile.name = QString(name_cstr);
+
+	return profile;
+}
+
 } // namespace qs::service::pipewire
diff --git i/src/services/pipewire/device.hpp w/src/services/pipewire/device.hpp
index 1a1f705..ee64858 100644
--- i/src/services/pipewire/device.hpp
+++ w/src/services/pipewire/device.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <functional>
+#include <optional>
 
 #include <pipewire/core.h>
 #include <pipewire/device.h>
@@ -17,6 +18,20 @@
 
 namespace qs::service::pipewire {
 
+struct PwProfile {
+	Q_GADGET;
+	Q_PROPERTY(qint32 index MEMBER index)
+	Q_PROPERTY(QString description MEMBER description)
+	Q_PROPERTY(QString name MEMBER name)
+
+public:
+	qint32 index;
+	QString description;
+	QString name;
+
+	static PwProfile parseSpaPod(const spa_pod* param);
+};
+
 class PwDevice;
 
 class PwDevice: public PwBindable<pw_device, PW_TYPE_INTERFACE_Device, PW_VERSION_DEVICE> {
@@ -25,6 +40,12 @@ class PwDevice: public PwBindable<pw_device, PW_TYPE_INTERFACE_Device, PW_VERSIO
 public:
 	void bindHooks() override;
 	void unbindHooks() override;
+	void initProps(const spa_dict* props) override;
+
+	QString name;
+	QString description;
+	QString nick;
+	QString type;
 
 	bool setVolumes(qint32 routeDevice, const QVector<float>& volumes);
 	bool setMuted(qint32 routeDevice, bool muted);
@@ -32,9 +53,16 @@ public:
 	void waitForDevice();
 	[[nodiscard]] bool waitingForDevice() const;
 
+	void setProfile(qint32 profileIndex);
+
+	QHash<qint32, PwProfile> profiles;
+	std::optional<PwProfile> currentProfile;
+
 signals:
 	void deviceReady();
 	void routeVolumesChanged(qint32 routeDevice, const PwVolumeProps& volumeProps);
+	void profilesChanged(QList<PwProfile> profiles);
+        void currentProfileChanged(PwProfile profile);
 
 private slots:
 	void polled();
@@ -49,6 +77,11 @@ private:
 	QList<qint32> stagingIndexes;
 	void addDeviceIndexPairs(const spa_pod* param);
 
+	bool profilesUpdated = false;
+	QSet<qint32> validProfiles;
+
+	bool currentProfileUpdated = false;
+
 	bool
 	setRouteProps(qint32 routeDevice, const std::function<void*(spa_pod_builder*)>& propsCallback);
 
diff --git i/src/services/pipewire/qml.cpp w/src/services/pipewire/qml.cpp
index 9efb17e..921d12a 100644
--- i/src/services/pipewire/qml.cpp
+++ w/src/services/pipewire/qml.cpp
@@ -9,6 +9,9 @@
 #include <qtypes.h>
 #include <qvariant.h>
 
+#include <cstdint>
+#include <algorithm>
+
 #include "../../core/model.hpp"
 #include "connection.hpp"
 #include "defaults.hpp"
@@ -54,6 +57,12 @@ Pipewire::Pipewire(QObject* parent): QObject(parent) {
 
 	QObject::connect(&connection->registry, &PwRegistry::nodeAdded, this, &Pipewire::onNodeAdded);
 
+	for (auto* device: connection->registry.devices.values()) {
+		this->onDeviceAdded(device);
+	}
+
+	QObject::connect(&connection->registry, &PwRegistry::deviceAdded, this, &Pipewire::onDeviceAdded);
+
 	for (auto* link: connection->registry.links.values()) {
 		this->onLinkAdded(link);
 	}
@@ -123,6 +132,19 @@ void Pipewire::onNodeRemoved(QObject* object) {
 	this->mNodes.removeObject(iface);
 }
 
+ObjectModel<PwDeviceIface>* Pipewire::devices() { return &this->mDevices; }
+
+void Pipewire::onDeviceAdded(PwDevice* device) {
+	auto* iface = PwDeviceIface::instance(device);
+	QObject::connect(iface, &QObject::destroyed, this, &Pipewire::onDeviceRemoved);
+	this->mDevices.insertObject(iface);
+}
+
+void Pipewire::onDeviceRemoved(QObject* object) {
+	auto* iface = static_cast<PwDeviceIface*>(object); // NOLINT
+	this->mDevices.removeObject(iface);
+}
+
 ObjectModel<PwLinkIface>* Pipewire::links() { return &this->mLinks; }
 
 void Pipewire::onLinkAdded(PwLink* link) {
@@ -357,6 +379,8 @@ QVariantMap PwNodeIface::properties() const {
 
 PwNodeAudioIface* PwNodeIface::audio() const { return this->audioIface; }
 
+PwDeviceIface* PwNodeIface::device() const { return PwDeviceIface::instance(this->mNode->device); }
+
 PwNodeIface* PwNodeIface::instance(PwNode* node) {
 	if (node == nullptr) return nullptr;
 
@@ -481,4 +505,42 @@ void PwObjectTracker::objectDestroyed(QObject* object) {
 	emit this->objectsChanged();
 }
 
+PwDeviceIface::PwDeviceIface(PwDevice* device): PwObjectIface(device), mDevice(device) {
+	QObject::connect(device, &PwDevice::profilesChanged, this, &PwDeviceIface::deviceProfilesChanged);
+	QObject::connect(device, &PwDevice::currentProfileChanged, this, &PwDeviceIface::deviceCurrentProfileChanged);
+}
+
+void PwDeviceIface::deviceProfilesChanged(QList<PwProfile>) { emit this->profilesChanged(); }
+void PwDeviceIface::deviceCurrentProfileChanged(PwProfile) { emit this->currentProfileChanged(); }
+
+quint32 PwDeviceIface::id() const { return this->mDevice->id; }
+QString PwDeviceIface::name() const { return this->mDevice->name; }
+QString PwDeviceIface::description() const { return this->mDevice->description; }
+QString PwDeviceIface::nickname() const { return this->mDevice->nick; }
+QString PwDeviceIface::type() const { return this->mDevice->type; }
+QList<PwProfile> PwDeviceIface::profiles() const {
+	QList<PwProfile> profiles = this->mDevice->profiles.values();
+	std::sort(profiles.begin(), profiles.end(), [](const PwProfile& a, const PwProfile& b) { return a.index < b.index; });
+	return profiles;
+}
+qint32 PwDeviceIface::currentProfile() const { return this->mDevice->currentProfile->index; }
+
+PwDeviceIface* PwDeviceIface::instance(PwDevice* device) {
+	if (device == nullptr) return nullptr;
+
+	auto v = device->property("iface");
+	if (v.canConvert<PwDeviceIface*>()) {
+		return v.value<PwDeviceIface*>();
+	}
+
+	auto* instance = new PwDeviceIface(device);
+	device->setProperty("iface", QVariant::fromValue(instance));
+
+	return instance;
+}
+
+void PwDeviceIface::setProfile(qint32 profileIndex) {
+	this->mDevice->setProfile(profileIndex);
+}
+
 } // namespace qs::service::pipewire
diff --git i/src/services/pipewire/qml.hpp w/src/services/pipewire/qml.hpp
index e3489a1..e5e1891 100644
--- i/src/services/pipewire/qml.hpp
+++ w/src/services/pipewire/qml.hpp
@@ -12,11 +12,13 @@
 #include "../../core/model.hpp"
 #include "link.hpp"
 #include "node.hpp"
+#include "device.hpp"
 #include "registry.hpp"
 
 namespace qs::service::pipewire {
 
 class PwNodeIface;
+class PwDeviceIface;
 class PwLinkIface;
 class PwLinkGroupIface;
 
@@ -65,6 +67,8 @@ class Pipewire: public QObject {
 	/// - @@PwNode.audio - if non null the node is an audio node.
 	QSDOC_TYPE_OVERRIDE(ObjectModel<qs::service::pipewire::PwNodeIface>*);
 	Q_PROPERTY(UntypedObjectModel* nodes READ nodes CONSTANT);
+	QSDOC_TYPE_OVERRIDE(ObjectModel<qs::service::pipewire::PwDeviceIface>*);
+	Q_PROPERTY(UntypedObjectModel* devices READ devices CONSTANT);
 	/// All links present in pipewire.
 	///
 	/// Links connect pipewire nodes to each other, and can be used to determine
@@ -134,6 +138,7 @@ public:
 	explicit Pipewire(QObject* parent = nullptr);
 
 	[[nodiscard]] ObjectModel<PwNodeIface>* nodes();
+	[[nodiscard]] ObjectModel<PwDeviceIface>* devices();
 	[[nodiscard]] ObjectModel<PwLinkIface>* links();
 	[[nodiscard]] ObjectModel<PwLinkGroupIface>* linkGroups();
 
@@ -159,7 +164,9 @@ signals:
 
 private slots:
 	void onNodeAdded(PwNode* node);
+	void onDeviceAdded(PwDevice* node);
 	void onNodeRemoved(QObject* object);
+	void onDeviceRemoved(QObject* object);
 	void onLinkAdded(PwLink* link);
 	void onLinkRemoved(QObject* object);
 	void onLinkGroupAdded(PwLinkGroup* group);
@@ -167,6 +174,7 @@ private slots:
 
 private:
 	ObjectModel<PwNodeIface> mNodes {this};
+	ObjectModel<PwDeviceIface> mDevices {this};
 	ObjectModel<PwLinkIface> mLinks {this};
 	ObjectModel<PwLinkGroupIface> mLinkGroups {this};
 };
@@ -315,6 +323,7 @@ class PwNodeIface: public PwObjectIface {
 	/// > [!NOTE] The node may be used before it is fully bound, but some data
 	/// > may be missing or incorrect.
 	Q_PROPERTY(bool ready READ isReady NOTIFY readyChanged);
+	Q_PROPERTY(qs::service::pipewire::PwDeviceIface* device READ device CONSTANT);
 	QML_NAMED_ELEMENT(PwNode);
 	QML_UNCREATABLE("PwNodes cannot be created directly");
 
@@ -332,6 +341,7 @@ public:
 	[[nodiscard]] PwNodeType::Flags type() const;
 	[[nodiscard]] QVariantMap properties() const;
 	[[nodiscard]] PwNodeAudioIface* audio() const;
+	[[nodiscard]] PwDeviceIface* device() const;
 
 	static PwNodeIface* instance(PwNode* node);
 
@@ -344,6 +354,44 @@ private:
 	PwNodeAudioIface* audioIface = nullptr;
 };
 
+class PwDeviceIface: public PwObjectIface {
+	Q_OBJECT;
+	Q_PROPERTY(quint32 id READ id CONSTANT);
+	Q_PROPERTY(QString name READ name CONSTANT);
+	Q_PROPERTY(QString description READ description CONSTANT);
+	Q_PROPERTY(QString nickname READ nickname CONSTANT);
+	Q_PROPERTY(QString type READ type CONSTANT);
+	Q_PROPERTY(QList<PwProfile> profiles READ profiles NOTIFY profilesChanged);
+	Q_PROPERTY(qint32 currentProfile READ currentProfile NOTIFY currentProfileChanged);
+
+	QML_NAMED_ELEMENT(PwDevice);
+	QML_UNCREATABLE("PwDevices cannot be created directly");
+
+signals:
+	void profilesChanged();
+	void currentProfileChanged();
+
+public:
+	explicit PwDeviceIface(PwDevice* node);
+
+	[[nodiscard]] quint32 id() const;
+	[[nodiscard]] QString name() const;
+	[[nodiscard]] QString description() const;
+	[[nodiscard]] QString nickname() const;
+	[[nodiscard]] QString type() const;
+	QList<PwProfile> profiles() const;
+	qint32 currentProfile() const;
+
+	Q_INVOKABLE void setProfile(qint32 profileIndex);
+
+	static PwDeviceIface* instance(PwDevice* node);
+private:
+	PwDevice* mDevice;
+
+	void deviceProfilesChanged(QList<PwProfile> profiles);
+	void deviceCurrentProfileChanged(PwProfile profile);
+};
+
 ///! A connection between pipewire nodes.
 /// Note that there is one link per *channel* of a connection between nodes.
 /// You usually want @@PwLinkGroup.
diff --git i/src/services/pipewire/registry.cpp w/src/services/pipewire/registry.cpp
index c08fc1d..50c6d7a 100644
--- i/src/services/pipewire/registry.cpp
+++ w/src/services/pipewire/registry.cpp
@@ -196,6 +196,7 @@ void PwRegistry::onGlobal(
 		device->initProps(props);
 
 		self->devices.emplace(id, device);
+		emit self->deviceAdded(device);
 	}
 }
 
@@ -211,6 +212,9 @@ void PwRegistry::onGlobalRemoved(void* data, quint32 id) {
 	} else if (auto* node = self->nodes.value(id)) {
 		self->nodes.remove(id);
 		node->safeDestroy();
+	} else if (auto* device = self->devices.value(id)) {
+		self->devices.remove(id);
+		device->safeDestroy();
 	}
 }
 
diff --git i/src/services/pipewire/registry.hpp w/src/services/pipewire/registry.hpp
index 8473f04..87e0766 100644
--- i/src/services/pipewire/registry.hpp
+++ w/src/services/pipewire/registry.hpp
@@ -132,6 +132,7 @@ public:
 
 signals:
 	void nodeAdded(PwNode* node);
+	void deviceAdded(PwDevice* node);
 	void linkAdded(PwLink* link);
 	void linkGroupAdded(PwLinkGroup* group);
 	void metadataAdded(PwMetadata* metadata);