summaryrefslogtreecommitdiff
path: root/lib/wireplumber/src/device.c
blob: af0760cb3e59d31a0b3e1421140ad1932c794855 (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
#include <wp/wp.h>

#include "device-private.h"
#include "profile.h"

struct _AstalWpDevice {
    GObject parent_instance;

    guint id;
    gchar *description;
    gchar *icon;
    gint active_profile;
    AstalWpDeviceType type;
};

typedef struct {
    WpDevice *device;
    GHashTable *profiles;
} AstalWpDevicePrivate;

G_DEFINE_FINAL_TYPE_WITH_PRIVATE(AstalWpDevice, astal_wp_device, G_TYPE_OBJECT);

G_DEFINE_ENUM_TYPE(AstalWpDeviceType, astal_wp_device_type,
                   G_DEFINE_ENUM_VALUE(ASTAL_WP_DEVICE_TYPE_AUDIO, "Audio/Device"),
                   G_DEFINE_ENUM_VALUE(ASTAL_WP_DEVICE_TYPE_VIDEO, "Video/Device"));

typedef enum {
    ASTAL_WP_DEVICE_PROP_ID = 1,
    ASTAL_WP_DEVICE_PROP_DESCRIPTION,
    ASTAL_WP_DEVICE_PROP_ICON,
    ASTAL_WP_DEVICE_PROP_PROFILES,
    ASTAL_WP_DEVICE_PROP_ACTIVE_PROFILE,
    ASTAL_WP_DEVICE_PROP_DEVICE_TYPE,
    ASTAL_WP_DEVICE_N_PROPERTIES,
} AstalWpDeviceProperties;

static GParamSpec *astal_wp_device_properties[ASTAL_WP_DEVICE_N_PROPERTIES] = {
    NULL,
};

/**
 * astal_wp_device_get_id
 * @self: the AstalWpDevice object
 *
 * gets the id of this device
 *
 */
guint astal_wp_device_get_id(AstalWpDevice *self) { return self->id; }

/**
 * astal_wp_device_get_description
 * @self: the AstalWpDevice object
 *
 * gets the description of this device
 *
 */
const gchar *astal_wp_device_get_description(AstalWpDevice *self) { return self->description; }

/**
 * astal_wp_device_get_icon
 * @self: the AstalWpDevice object
 *
 * gets the icon of this device
 *
 */
const gchar *astal_wp_device_get_icon(AstalWpDevice *self) {
    g_return_val_if_fail(self != NULL, "audio-card-symbolic");
    return self->icon;
}

/**
 * astal_wp_device_get_device_type
 * @self: the AstalWpDevice object
 *
 * gets the type of this device
 *
 */
AstalWpDeviceType astal_wp_device_get_device_type(AstalWpDevice *self) { return self->type; }

/**
 * astal_wp_device_get_active_profile
 * @self: the AstalWpDevice object
 *
 * gets the currently active profile of this device
 *
 */
gint astal_wp_device_get_active_profile(AstalWpDevice *self) { return self->active_profile; }

/**
 * astal_wp_device_set_active_profile
 * @self: the AstalWpDevice object
 * @profile_id: the id of the profile
 *
 * sets the profile for this device
 *
 */
void astal_wp_device_set_active_profile(AstalWpDevice *self, int profile_id) {
    AstalWpDevicePrivate *priv = astal_wp_device_get_instance_private(self);

    WpSpaPodBuilder *builder =
        wp_spa_pod_builder_new_object("Spa:Pod:Object:Param:Profile", "Profile");
    wp_spa_pod_builder_add_property(builder, "index");
    wp_spa_pod_builder_add_int(builder, profile_id);
    WpSpaPod *pod = wp_spa_pod_builder_end(builder);
    wp_pipewire_object_set_param(WP_PIPEWIRE_OBJECT(priv->device), "Profile", 0, pod);

    wp_spa_pod_builder_unref(builder);
}

/**
 * astal_wp_device_get_profile:
 * @self: the AstalWpDevice object
 * @id: the id of the profile
 *
 * gets the profile with the given id
 *
 * Returns: (transfer none) (nullable)
 */
AstalWpProfile *astal_wp_device_get_profile(AstalWpDevice *self, gint id) {
    AstalWpDevicePrivate *priv = astal_wp_device_get_instance_private(self);

    return g_hash_table_lookup(priv->profiles, GINT_TO_POINTER(id));
}

/**
 * astal_wp_device_get_profiles:
 * @self: the AstalWpDevice object
 *
 * gets a GList containing the profiles
 *
 * Returns: (transfer container) (nullable) (type GList(AstalWpProfile))
 */
GList *astal_wp_device_get_profiles(AstalWpDevice *self) {
    AstalWpDevicePrivate *priv = astal_wp_device_get_instance_private(self);
    return g_hash_table_get_values(priv->profiles);
}

static void astal_wp_device_get_property(GObject *object, guint property_id, GValue *value,
                                         GParamSpec *pspec) {
    AstalWpDevice *self = ASTAL_WP_DEVICE(object);

    switch (property_id) {
        case ASTAL_WP_DEVICE_PROP_ID:
            g_value_set_uint(value, self->id);
            break;
        case ASTAL_WP_DEVICE_PROP_DESCRIPTION:
            g_value_set_string(value, self->description);
            break;
        case ASTAL_WP_DEVICE_PROP_ICON:
            g_value_set_string(value, self->icon);
            break;
        case ASTAL_WP_DEVICE_PROP_PROFILES:
            g_value_set_pointer(value, astal_wp_device_get_profiles(self));
            break;
        case ASTAL_WP_DEVICE_PROP_DEVICE_TYPE:
            g_value_set_enum(value, astal_wp_device_get_device_type(self));
            break;
        case ASTAL_WP_DEVICE_PROP_ACTIVE_PROFILE:
            g_value_set_int(value, self->active_profile);
            break;
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
            break;
    }
}

static void astal_wp_device_set_property(GObject *object, guint property_id, const GValue *value,
                                         GParamSpec *pspec) {
    AstalWpDevice *self = ASTAL_WP_DEVICE(object);

    switch (property_id) {
        case ASTAL_WP_DEVICE_PROP_ACTIVE_PROFILE:
            astal_wp_device_set_active_profile(self, g_value_get_int(value));
            break;
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
            break;
    }
}

static void astal_wp_device_update_profiles(AstalWpDevice *self) {
    AstalWpDevicePrivate *priv = astal_wp_device_get_instance_private(self);
    g_hash_table_remove_all(priv->profiles);

    WpIterator *iter =
        wp_pipewire_object_enum_params_sync(WP_PIPEWIRE_OBJECT(priv->device), "EnumProfile", NULL);
    if (iter == NULL) return;
    GValue profile = G_VALUE_INIT;
    while (wp_iterator_next(iter, &profile)) {
        WpSpaPod *pod = g_value_get_boxed(&profile);

        gint index;
        gchar *description;
        wp_spa_pod_get_object(pod, NULL, "index", "i", &index, "description", "s", &description,
                              NULL);

        g_hash_table_insert(
            priv->profiles, GINT_TO_POINTER(index),
            g_object_new(ASTAL_WP_TYPE_PROFILE, "index", index, "description", description, NULL));
        g_value_unset(&profile);
    }
    wp_iterator_unref(iter);

    g_object_notify(G_OBJECT(self), "profiles");
}

static void astal_wp_device_update_active_profile(AstalWpDevice *self) {
    AstalWpDevicePrivate *priv = astal_wp_device_get_instance_private(self);

    WpIterator *iter =
        wp_pipewire_object_enum_params_sync(WP_PIPEWIRE_OBJECT(priv->device), "Profile", NULL);
    if (iter == NULL) return;
    GValue profile = G_VALUE_INIT;
    while (wp_iterator_next(iter, &profile)) {
        WpSpaPod *pod = g_value_get_boxed(&profile);

        gint index;
        gchar *description;
        wp_spa_pod_get_object(pod, NULL, "index", "i", &index, "description", "s", &description,
                              NULL);

        g_hash_table_insert(
            priv->profiles, GINT_TO_POINTER(index),
            g_object_new(ASTAL_WP_TYPE_PROFILE, "index", index, "description", description, NULL));

        self->active_profile = index;
        g_value_unset(&profile);
    }
    wp_iterator_unref(iter);

    g_object_notify(G_OBJECT(self), "active-profile-id");
}

static void astal_wp_device_params_changed(AstalWpDevice *self, const gchar *prop) {
    if (g_strcmp0(prop, "EnumProfile") == 0) {
        astal_wp_device_update_profiles(self);
    } else if (g_strcmp0(prop, "Profile") == 0) {
        astal_wp_device_update_active_profile(self);
    }
}

static void astal_wp_device_update_properties(AstalWpDevice *self) {
    AstalWpDevicePrivate *priv = astal_wp_device_get_instance_private(self);
    if (priv->device == NULL) return;
    self->id = wp_proxy_get_bound_id(WP_PROXY(priv->device));
    const gchar *description =
        wp_pipewire_object_get_property(WP_PIPEWIRE_OBJECT(priv->device), "device.description");
    if (description == NULL) {
        description =
            wp_pipewire_object_get_property(WP_PIPEWIRE_OBJECT(priv->device), "device.name");
    }
    if (description == NULL) {
        description = "unknown";
    }
    g_free(self->description);
    self->description = g_strdup(description);

    const gchar *icon =
        wp_pipewire_object_get_property(WP_PIPEWIRE_OBJECT(priv->device), "device.icon-name");
    if (icon == NULL) {
        icon = "audio-card-symbolic";
    }
    g_free(self->icon);
    self->icon = g_strdup(icon);

    const gchar *type =
        wp_pipewire_object_get_property(WP_PIPEWIRE_OBJECT(priv->device), "media.class");
    GEnumClass *enum_class = g_type_class_ref(ASTAL_WP_TYPE_DEVICE_TYPE);
    if (g_enum_get_value_by_nick(enum_class, type) != NULL)
        self->type = g_enum_get_value_by_nick(enum_class, type)->value;
    g_type_class_unref(enum_class);

    astal_wp_device_update_profiles(self);
    astal_wp_device_update_active_profile(self);

    g_object_notify(G_OBJECT(self), "id");
    g_object_notify(G_OBJECT(self), "device-type");
    g_object_notify(G_OBJECT(self), "icon");
    g_object_notify(G_OBJECT(self), "description");
}

AstalWpDevice *astal_wp_device_create(WpDevice *device) {
    AstalWpDevice *self = g_object_new(ASTAL_WP_TYPE_DEVICE, NULL);
    AstalWpDevicePrivate *priv = astal_wp_device_get_instance_private(self);

    priv->device = g_object_ref(device);

    g_signal_connect_swapped(priv->device, "params-changed",
                             G_CALLBACK(astal_wp_device_params_changed), self);

    astal_wp_device_update_properties(self);
    return self;
}

static void astal_wp_device_init(AstalWpDevice *self) {
    AstalWpDevicePrivate *priv = astal_wp_device_get_instance_private(self);
    priv->device = NULL;

    priv->profiles = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_object_unref);

    self->description = NULL;
    self->icon = NULL;
}

static void astal_wp_device_dispose(GObject *object) {
    AstalWpDevice *self = ASTAL_WP_DEVICE(object);
    AstalWpDevicePrivate *priv = astal_wp_device_get_instance_private(self);

    g_clear_object(&priv->device);
}

static void astal_wp_device_finalize(GObject *object) {
    AstalWpDevice *self = ASTAL_WP_DEVICE(object);
    g_free(self->description);
    g_free(self->icon);
}

static void astal_wp_device_class_init(AstalWpDeviceClass *class) {
    GObjectClass *object_class = G_OBJECT_CLASS(class);
    object_class->dispose = astal_wp_device_dispose;
    object_class->finalize = astal_wp_device_finalize;
    object_class->get_property = astal_wp_device_get_property;
    object_class->set_property = astal_wp_device_set_property;
    /**
     * AstalWpDevice:id
     *
     * The id of this device.
     */
    astal_wp_device_properties[ASTAL_WP_DEVICE_PROP_ID] =
        g_param_spec_uint("id", "id", "id", 0, UINT_MAX, 0, G_PARAM_READABLE);
    /**
     * AstalWpDevice:description
     *
     * The description of this device.
     */
    astal_wp_device_properties[ASTAL_WP_DEVICE_PROP_DESCRIPTION] =
        g_param_spec_string("description", "description", "description", NULL, G_PARAM_READABLE);
    /**
     * AstalWpDevice:icon
     *
     * The icon name for this device.
     */
    astal_wp_device_properties[ASTAL_WP_DEVICE_PROP_ICON] =
        g_param_spec_string("icon", "icon", "icon", NULL, G_PARAM_READABLE);
    /**
     * AstalWpDevice:device-type: (type AstalWpDeviceType)
     *
     * The type of this device
     */
    astal_wp_device_properties[ASTAL_WP_DEVICE_PROP_DEVICE_TYPE] =
        g_param_spec_enum("device-type", "device-type", "device-type", ASTAL_WP_TYPE_DEVICE_TYPE, 1,
                          G_PARAM_READABLE);
    /**
     * AstalWpDevice:profiles: (type GList(AstalWpProfile)) (transfer container)
     *
     * A list of available profiles
     */
    astal_wp_device_properties[ASTAL_WP_DEVICE_PROP_PROFILES] =
        g_param_spec_pointer("profiles", "profiles", "profiles", G_PARAM_READABLE);
    /**
     * AstalWpDevice:active-profile-id
     *
     * The id of the currently active profile.
     */
    astal_wp_device_properties[ASTAL_WP_DEVICE_PROP_ACTIVE_PROFILE] =
        g_param_spec_int("active-profile-id", "active-profile-id", "active-profile-id", G_MININT,
                         G_MAXINT, 0, G_PARAM_READWRITE);

    g_object_class_install_properties(object_class, ASTAL_WP_DEVICE_N_PROPERTIES,
                                      astal_wp_device_properties);
}