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
|
/*
* Copyright (C) 2014 Michal Ratajsky <michal.ratajsky@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the licence, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>
#include <libmatemixer/matemixer.h>
#include <libmatemixer/matemixer-private.h>
#include <pulse/pulseaudio.h>
#include "pulse-connection.h"
#include "pulse-device.h"
#include "pulse-device-profile.h"
#include "pulse-device-switch.h"
#include "pulse-port.h"
#include "pulse-stream.h"
struct _PulseDevicePrivate
{
guint32 index;
GHashTable *ports;
GHashTable *streams;
GList *streams_list;
PulseConnection *connection;
PulseDeviceSwitch *pswitch;
GList *pswitch_list;
};
enum {
PROP_0,
PROP_INDEX,
PROP_CONNECTION,
N_PROPERTIES
};
static GParamSpec *properties[N_PROPERTIES] = { NULL, };
static void pulse_device_class_init (PulseDeviceClass *klass);
static void pulse_device_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec);
static void pulse_device_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
static void pulse_device_init (PulseDevice *device);
static void pulse_device_dispose (GObject *object);
static void pulse_device_finalize (GObject *object);
G_DEFINE_TYPE (PulseDevice, pulse_device, MATE_MIXER_TYPE_DEVICE)
static MateMixerStream *pulse_device_get_stream (MateMixerDevice *mmd,
const gchar *name);
static const GList * pulse_device_list_streams (MateMixerDevice *mmd);
static const GList * pulse_device_list_switches (MateMixerDevice *mmd);
static void pulse_device_load (PulseDevice *device,
const pa_card_info *info);
static void free_list_streams (PulseDevice *device);
static void
pulse_device_class_init (PulseDeviceClass *klass)
{
GObjectClass *object_class;
MateMixerDeviceClass *device_class;
object_class = G_OBJECT_CLASS (klass);
object_class->dispose = pulse_device_dispose;
object_class->finalize = pulse_device_finalize;
object_class->get_property = pulse_device_get_property;
object_class->set_property = pulse_device_set_property;
device_class = MATE_MIXER_DEVICE_CLASS (klass);
device_class->get_stream = pulse_device_get_stream;
device_class->list_streams = pulse_device_list_streams;
device_class->list_switches = pulse_device_list_switches;
properties[PROP_INDEX] =
g_param_spec_uint ("index",
"Index",
"Index of the device",
0,
G_MAXUINT,
0,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
properties[PROP_CONNECTION] =
g_param_spec_object ("connection",
"Connection",
"PulseAudio connection",
PULSE_TYPE_CONNECTION,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, N_PROPERTIES, properties);
g_type_class_add_private (object_class, sizeof (PulseDevicePrivate));
}
static void
pulse_device_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
{
PulseDevice *device;
device = PULSE_DEVICE (object);
switch (param_id) {
case PROP_INDEX:
g_value_set_uint (value, device->priv->index);
break;
case PROP_CONNECTION:
g_value_set_object (value, device->priv->connection);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
static void
pulse_device_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
{
PulseDevice *device;
device = PULSE_DEVICE (object);
switch (param_id) {
case PROP_INDEX:
device->priv->index = g_value_get_uint (value);
break;
case PROP_CONNECTION:
device->priv->connection = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
static void
pulse_device_init (PulseDevice *device)
{
device->priv = G_TYPE_INSTANCE_GET_PRIVATE (device,
PULSE_TYPE_DEVICE,
PulseDevicePrivate);
device->priv->ports = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
g_object_unref);
device->priv->streams = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
g_object_unref);
}
static void
pulse_device_dispose (GObject *object)
{
PulseDevice *device;
device = PULSE_DEVICE (object);
g_hash_table_remove_all (device->priv->ports);
g_hash_table_remove_all (device->priv->streams);
g_clear_object (&device->priv->connection);
g_clear_object (&device->priv->pswitch);
free_list_streams (device);
if (device->priv->pswitch_list != NULL) {
g_list_free (device->priv->pswitch_list);
device->priv->pswitch_list = NULL;
}
G_OBJECT_CLASS (pulse_device_parent_class)->dispose (object);
}
static void
pulse_device_finalize (GObject *object)
{
PulseDevice *device;
device = PULSE_DEVICE (object);
g_hash_table_unref (device->priv->ports);
g_hash_table_unref (device->priv->streams);
G_OBJECT_CLASS (pulse_device_parent_class)->finalize (object);
}
PulseDevice *
pulse_device_new (PulseConnection *connection, const pa_card_info *info)
{
PulseDevice *device;
const gchar *label;
const gchar *icon;
g_return_val_if_fail (PULSE_IS_CONNECTION (connection), NULL);
g_return_val_if_fail (info != NULL, NULL);
label = pa_proplist_gets (info->proplist, PA_PROP_DEVICE_DESCRIPTION);
if G_UNLIKELY (label == NULL)
label = info->name;
icon = pa_proplist_gets (info->proplist, PA_PROP_DEVICE_ICON_NAME);
if G_UNLIKELY (icon == NULL)
icon = "audio-card";
/* Consider the device index as unchanging parameter */
device = g_object_new (PULSE_TYPE_DEVICE,
"index", info->index,
"connection", connection,
"name", info->name,
"label", label,
"icon", icon,
NULL);
pulse_device_load (device, info);
pulse_device_update (device, info);
return device;
}
void
pulse_device_update (PulseDevice *device, const pa_card_info *info)
{
g_return_if_fail (PULSE_IS_DEVICE (device));
g_return_if_fail (info != NULL);
#if PA_CHECK_VERSION (5, 0, 0)
if G_LIKELY (info->active_profile2 != NULL)
pulse_device_switch_set_active_profile_by_name (device->priv->pswitch,
info->active_profile2->name);
#else
if G_LIKELY (info->active_profile != NULL)
pulse_device_switch_set_active_profile_by_name (device->priv->pswitch,
info->active_profile->name);
#endif
}
void
pulse_device_add_stream (PulseDevice *device, PulseStream *stream)
{
const gchar *name;
g_return_if_fail (PULSE_IS_DEVICE (device));
g_return_if_fail (PULSE_IS_STREAM (stream));
name = mate_mixer_stream_get_name (MATE_MIXER_STREAM (stream));
g_hash_table_insert (device->priv->streams,
g_strdup (name),
g_object_ref (stream));
free_list_streams (device);
g_signal_emit_by_name (G_OBJECT (device),
"stream-added",
MATE_MIXER_STREAM (stream));
}
void
pulse_device_remove_stream (PulseDevice *device, PulseStream *stream)
{
const gchar *name;
g_return_if_fail (PULSE_IS_DEVICE (device));
g_return_if_fail (PULSE_IS_STREAM (stream));
name = mate_mixer_stream_get_name (MATE_MIXER_STREAM (stream));
g_object_ref (stream);
g_hash_table_remove (device->priv->streams, name);
free_list_streams (device);
g_signal_emit_by_name (G_OBJECT (device),
"stream-removed",
MATE_MIXER_STREAM (stream));
g_object_unref (stream);
}
guint32
pulse_device_get_index (PulseDevice *device)
{
g_return_val_if_fail (PULSE_IS_DEVICE (device), 0);
return device->priv->index;
}
PulseConnection *
pulse_device_get_connection (PulseDevice *device)
{
g_return_val_if_fail (PULSE_IS_DEVICE (device), NULL);
return device->priv->connection;
}
PulsePort *
pulse_device_get_port (PulseDevice *device, const gchar *name)
{
g_return_val_if_fail (PULSE_IS_DEVICE (device), NULL);
g_return_val_if_fail (name != NULL, NULL);
return g_hash_table_lookup (device->priv->ports, name);
}
static MateMixerStream *
pulse_device_get_stream (MateMixerDevice *mmd, const gchar *name)
{
g_return_val_if_fail (PULSE_IS_DEVICE (mmd), NULL);
g_return_val_if_fail (name != NULL, NULL);
return g_hash_table_lookup (PULSE_DEVICE (mmd)->priv->streams, name);
}
static const GList *
pulse_device_list_streams (MateMixerDevice *mmd)
{
PulseDevice *device;
g_return_val_if_fail (PULSE_IS_DEVICE (mmd), NULL);
device = PULSE_DEVICE (mmd);
if (device->priv->streams_list == NULL) {
device->priv->streams_list = g_hash_table_get_values (device->priv->streams);
if (device->priv->streams_list != NULL)
g_list_foreach (device->priv->streams_list, (GFunc) g_object_ref, NULL);
}
return device->priv->streams_list;
}
static const GList *
pulse_device_list_switches (MateMixerDevice *mmd)
{
g_return_val_if_fail (PULSE_IS_DEVICE (mmd), NULL);
return PULSE_DEVICE (mmd)->priv->pswitch_list;
}
static void
pulse_device_load (PulseDevice *device, const pa_card_info *info)
{
guint i;
for (i = 0; i < info->n_ports; i++) {
PulsePort *port;
const gchar *name;
const gchar *icon;
name = info->ports[i]->name;
icon = pa_proplist_gets (info->ports[i]->proplist, "device.icon_name");
port = pulse_port_new (name,
info->ports[i]->description,
icon,
info->ports[i]->priority);
g_hash_table_insert (device->priv->ports,
g_strdup (name),
port);
}
/* Create the device profile switch */
if (info->n_profiles > 0) {
device->priv->pswitch = pulse_device_switch_new ("profile",
_("Profile"),
device);
device->priv->pswitch_list = g_list_prepend (NULL, device->priv->pswitch);
}
for (i = 0; i < info->n_profiles; i++) {
PulseDeviceProfile *profile;
#if PA_CHECK_VERSION (5, 0, 0)
pa_card_profile_info2 *p_info = info->profiles2[i];
/* PulseAudio 5.0 includes a new pa_card_profile_info2 which only
* differs in the new available flag, we use it not to include profiles
* which are unavailable */
if (p_info->available == 0)
continue;
#else
/* The old profile list is an array of structs, not pointers */
pa_card_profile_info *p_info = &info->profiles[i];
#endif
profile = pulse_device_profile_new (p_info->name,
p_info->description,
p_info->priority);
pulse_device_switch_add_profile (device->priv->pswitch, profile);
g_object_unref (profile);
}
}
static void
free_list_streams (PulseDevice *device)
{
if (device->priv->streams_list == NULL)
return;
g_list_free_full (device->priv->streams_list, g_object_unref);
device->priv->streams_list = NULL;
}
|