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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* Copyright (C) 2009 Bastien Nocera
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include <glib.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <canberra.h>
#include <libmatemixer/matemixer.h>
#ifdef HAVE_PULSEAUDIO
#include <pulse/pulseaudio.h>
#endif
#include "gvc-speaker-test.h"
#define GVC_SPEAKER_TEST_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GVC_TYPE_SPEAKER_TEST, GvcSpeakerTestPrivate))
struct _GvcSpeakerTestPrivate
{
// XXX pulse constant
GtkWidget *channel_controls[PA_CHANNEL_POSITION_MAX];
ca_context *canberra;
MateMixerControl *control;
MateMixerDevice *device;
};
enum {
PROP_0,
PROP_CONTROL,
PROP_DEVICE,
N_PROPERTIES
};
static GParamSpec *properties[N_PROPERTIES] = { NULL, };
static void gvc_speaker_test_class_init (GvcSpeakerTestClass *klass);
static void gvc_speaker_test_init (GvcSpeakerTest *speaker_test);
static void gvc_speaker_test_dispose (GObject *object);
static void gvc_speaker_test_finalize (GObject *object);
static void update_channel_map (GvcSpeakerTest *speaker_test);
G_DEFINE_TYPE (GvcSpeakerTest, gvc_speaker_test, GTK_TYPE_TABLE)
static const int position_table[] = {
/* Position, X, Y */
MATE_MIXER_CHANNEL_FRONT_LEFT, 0, 0,
MATE_MIXER_CHANNEL_FRONT_LEFT_CENTER, 1, 0,
MATE_MIXER_CHANNEL_FRONT_CENTER, 2, 0,
MATE_MIXER_CHANNEL_MONO, 2, 0,
MATE_MIXER_CHANNEL_FRONT_RIGHT_CENTER, 3, 0,
MATE_MIXER_CHANNEL_FRONT_RIGHT, 4, 0,
MATE_MIXER_CHANNEL_SIDE_LEFT, 0, 1,
MATE_MIXER_CHANNEL_SIDE_RIGHT, 4, 1,
MATE_MIXER_CHANNEL_BACK_LEFT, 0, 2,
MATE_MIXER_CHANNEL_BACK_CENTER, 2, 2,
MATE_MIXER_CHANNEL_BACK_RIGHT, 4, 2,
MATE_MIXER_CHANNEL_LFE, 3, 2
};
static void
gvc_speaker_test_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GvcSpeakerTest *self = GVC_SPEAKER_TEST (object);
switch (prop_id) {
case PROP_CONTROL:
/* Construct-only object */
self->priv->control = g_value_dup_object (value);
if (self->priv->control != NULL)
update_channel_map (self);
break;
case PROP_DEVICE:
if (self->priv->device)
g_object_unref (self->priv->device);
self->priv->device = g_value_dup_object (value);
if (self->priv->device != NULL)
update_channel_map (self);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gvc_speaker_test_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GvcSpeakerTest *self = GVC_SPEAKER_TEST (object);
switch (prop_id) {
case PROP_CONTROL:
g_value_set_object (value, self->priv->control);
break;
case PROP_DEVICE:
g_value_set_object (value, self->priv->device);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gvc_speaker_test_class_init (GvcSpeakerTestClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = gvc_speaker_test_dispose;
object_class->finalize = gvc_speaker_test_finalize;
object_class->set_property = gvc_speaker_test_set_property;
object_class->get_property = gvc_speaker_test_get_property;
properties[PROP_CONTROL] =
g_param_spec_object ("control",
"Control",
"The mixer controller",
MATE_MIXER_TYPE_CONTROL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
properties[PROP_DEVICE] =
g_param_spec_object ("device",
"Device",
"The mixer device",
MATE_MIXER_TYPE_DEVICE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
g_object_class_install_properties (object_class, N_PROPERTIES, properties);
g_type_class_add_private (klass, sizeof (GvcSpeakerTestPrivate));
}
static const gchar *
sound_name (MateMixerChannelPosition position)
{
switch (position) {
case MATE_MIXER_CHANNEL_FRONT_LEFT:
return "audio-channel-front-left";
case MATE_MIXER_CHANNEL_FRONT_RIGHT:
return "audio-channel-front-right";
case MATE_MIXER_CHANNEL_FRONT_CENTER:
return "audio-channel-front-center";
case MATE_MIXER_CHANNEL_BACK_LEFT:
return "audio-channel-rear-left";
case MATE_MIXER_CHANNEL_BACK_RIGHT:
return "audio-channel-rear-right";
case MATE_MIXER_CHANNEL_BACK_CENTER:
return "audio-channel-rear-center";
case MATE_MIXER_CHANNEL_LFE:
return "audio-channel-lfe";
case MATE_MIXER_CHANNEL_SIDE_LEFT:
return "audio-channel-side-left";
case MATE_MIXER_CHANNEL_SIDE_RIGHT:
return "audio-channel-side-right";
default:
return NULL;
}
}
static const gchar *
icon_name (MateMixerChannelPosition position, gboolean playing)
{
switch (position) {
case MATE_MIXER_CHANNEL_FRONT_LEFT:
return playing ? "audio-speaker-left-testing" : "audio-speaker-left";
case MATE_MIXER_CHANNEL_FRONT_RIGHT:
return playing ? "audio-speaker-right-testing" : "audio-speaker-right";
case MATE_MIXER_CHANNEL_FRONT_CENTER:
return playing ? "audio-speaker-center-testing" : "audio-speaker-center";
case MATE_MIXER_CHANNEL_BACK_LEFT:
return playing ? "audio-speaker-left-back-testing" : "audio-speaker-left-back";
case MATE_MIXER_CHANNEL_BACK_RIGHT:
return playing ? "audio-speaker-right-back-testing" : "audio-speaker-right-back";
case MATE_MIXER_CHANNEL_BACK_CENTER:
return playing ? "audio-speaker-center-back-testing" : "audio-speaker-center-back";
case MATE_MIXER_CHANNEL_LFE:
return playing ? "audio-subwoofer-testing" : "audio-subwoofer";
case MATE_MIXER_CHANNEL_SIDE_LEFT:
return playing ? "audio-speaker-left-side-testing" : "audio-speaker-left-side";
case MATE_MIXER_CHANNEL_SIDE_RIGHT:
return playing ? "audio-speaker-right-side-testing" : "audio-speaker-right-side";
default:
return NULL;
}
}
static void
update_button (GtkWidget *control)
{
GtkWidget *button;
GtkWidget *image;
pa_channel_position_t position;
gboolean playing;
button = g_object_get_data (G_OBJECT (control), "button");
image = g_object_get_data (G_OBJECT (control), "image");
position = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (control), "position"));
playing = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (control), "playing"));
gtk_button_set_label (GTK_BUTTON (button), playing ? _("Stop") : _("Test"));
gtk_image_set_from_icon_name (GTK_IMAGE (image), icon_name (position, playing), GTK_ICON_SIZE_DIALOG);
}
static const char *
channel_position_string (MateMixerChannelPosition position, gboolean pretty)
{
#ifdef HAVE_PULSEAUDIO
pa_channel_position_t pa_position;
switch (position) {
case MATE_MIXER_CHANNEL_MONO:
pa_position = PA_CHANNEL_POSITION_MONO;
break;
case MATE_MIXER_CHANNEL_FRONT_LEFT:
pa_position = PA_CHANNEL_POSITION_FRONT_LEFT;
break;
case MATE_MIXER_CHANNEL_FRONT_RIGHT:
pa_position = PA_CHANNEL_POSITION_FRONT_RIGHT;
break;
case MATE_MIXER_CHANNEL_FRONT_CENTER:
pa_position = PA_CHANNEL_POSITION_FRONT_CENTER;
break;
case MATE_MIXER_CHANNEL_LFE:
pa_position = PA_CHANNEL_POSITION_LFE;
break;
case MATE_MIXER_CHANNEL_BACK_LEFT:
pa_position = PA_CHANNEL_POSITION_REAR_LEFT;
break;
case MATE_MIXER_CHANNEL_BACK_RIGHT:
pa_position = PA_CHANNEL_POSITION_REAR_RIGHT;
break;
case MATE_MIXER_CHANNEL_BACK_CENTER:
pa_position = PA_CHANNEL_POSITION_REAR_CENTER;
break;
case MATE_MIXER_CHANNEL_FRONT_LEFT_CENTER:
pa_position = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
break;
case MATE_MIXER_CHANNEL_FRONT_RIGHT_CENTER:
pa_position = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
break;
case MATE_MIXER_CHANNEL_SIDE_LEFT:
pa_position = PA_CHANNEL_POSITION_SIDE_LEFT;
break;
case MATE_MIXER_CHANNEL_SIDE_RIGHT:
pa_position = PA_CHANNEL_POSITION_SIDE_RIGHT;
break;
case MATE_MIXER_CHANNEL_TOP_FRONT_LEFT:
pa_position = PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
break;
case MATE_MIXER_CHANNEL_TOP_FRONT_RIGHT:
pa_position = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
break;
case MATE_MIXER_CHANNEL_TOP_FRONT_CENTER:
pa_position = PA_CHANNEL_POSITION_TOP_FRONT_CENTER;
break;
case MATE_MIXER_CHANNEL_TOP_CENTER:
pa_position = PA_CHANNEL_POSITION_TOP_CENTER;
break;
case MATE_MIXER_CHANNEL_TOP_BACK_LEFT:
pa_position = PA_CHANNEL_POSITION_TOP_REAR_LEFT;
break;
case MATE_MIXER_CHANNEL_TOP_BACK_RIGHT:
pa_position = PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
break;
case MATE_MIXER_CHANNEL_TOP_BACK_CENTER:
pa_position = PA_CHANNEL_POSITION_TOP_REAR_CENTER;
break;
default:
pa_position = PA_CHANNEL_POSITION_INVALID;
break;
}
if (pretty)
return pa_channel_position_to_pretty_string (pa_position);
else
return pa_channel_position_to_string (pa_position);
#endif
return NULL;
}
static gboolean
idle_cb (GtkWidget *control)
{
if (control != NULL) {
/* This is called in the background thread, hence forward to main thread
* via idle callback */
g_object_set_data (G_OBJECT (control), "playing", GINT_TO_POINTER (FALSE));
update_button (control);
}
return FALSE;
}
static void
finish_cb (ca_context *c, uint32_t id, int error_code, void *userdata)
{
GtkWidget *control = (GtkWidget *) userdata;
if (error_code == CA_ERROR_DESTROYED || control == NULL)
return;
g_idle_add ((GSourceFunc) idle_cb, control);
}
static void
on_test_button_clicked (GtkButton *button, GtkWidget *control)
{
gboolean playing;
ca_context *canberra;
canberra = g_object_get_data (G_OBJECT (control), "canberra");
ca_context_cancel (canberra, 1);
playing = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (control), "playing"));
if (playing) {
g_object_set_data (G_OBJECT (control), "playing", GINT_TO_POINTER (FALSE));
} else {
MateMixerChannelPosition position;
const char *name;
ca_proplist *proplist;
position = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (control), "position"));
ca_proplist_create (&proplist);
ca_proplist_sets (proplist, CA_PROP_MEDIA_ROLE, "test");
ca_proplist_sets (proplist,
CA_PROP_MEDIA_NAME,
channel_position_string (position, TRUE));
ca_proplist_sets (proplist,
CA_PROP_CANBERRA_FORCE_CHANNEL,
channel_position_string (position, FALSE));
ca_proplist_sets (proplist, CA_PROP_CANBERRA_ENABLE, "1");
name = sound_name (position);
if (name != NULL) {
ca_proplist_sets (proplist, CA_PROP_EVENT_ID, name);
playing = ca_context_play_full (canberra, 1, proplist, finish_cb, control) >= 0;
}
if (!playing) {
ca_proplist_sets (proplist, CA_PROP_EVENT_ID, "audio-test-signal");
playing = ca_context_play_full (canberra, 1, proplist, finish_cb, control) >= 0;
}
if (!playing) {
ca_proplist_sets(proplist, CA_PROP_EVENT_ID, "bell-window-system");
playing = ca_context_play_full (canberra, 1, proplist, finish_cb, control) >= 0;
}
g_object_set_data (G_OBJECT (control), "playing", GINT_TO_POINTER(playing));
}
update_button (control);
}
static GtkWidget *
channel_control_new (ca_context *canberra, MateMixerChannelPosition position)
{
GtkWidget *control;
GtkWidget *box;
GtkWidget *label;
GtkWidget *image;
GtkWidget *test_button;
const char *name;
control = gtk_vbox_new (FALSE, 6);
g_object_set_data (G_OBJECT (control), "playing", GINT_TO_POINTER (FALSE));
g_object_set_data (G_OBJECT (control), "position", GINT_TO_POINTER (position));
g_object_set_data (G_OBJECT (control), "canberra", canberra);
name = icon_name (position, FALSE);
if (name == NULL)
name = "audio-volume-medium";
image = gtk_image_new_from_icon_name (name, GTK_ICON_SIZE_DIALOG);
g_object_set_data (G_OBJECT (control), "image", image);
gtk_box_pack_start (GTK_BOX (control), image, FALSE, FALSE, 0);
label = gtk_label_new (channel_position_string (position, TRUE));
gtk_box_pack_start (GTK_BOX (control), label, FALSE, FALSE, 0);
test_button = gtk_button_new_with_label (_("Test"));
g_signal_connect (G_OBJECT (test_button), "clicked",
G_CALLBACK (on_test_button_clicked), control);
g_object_set_data (G_OBJECT (control), "button", test_button);
box = gtk_hbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX (box), test_button, TRUE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (control), box, FALSE, FALSE, 0);
gtk_widget_show_all (control);
return control;
}
static void
create_channel_controls (GvcSpeakerTest *test)
{
guint i;
for (i = 0; i < G_N_ELEMENTS (position_table); i += 3) {
test->priv->channel_controls[position_table[i]] =
channel_control_new (test->priv->canberra,
(MateMixerChannelPosition) position_table[i]);
gtk_table_attach (GTK_TABLE (test),
test->priv->channel_controls[position_table[i]],
position_table[i+1],
position_table[i+1]+1,
position_table[i+2],
position_table[i+2]+1,
GTK_EXPAND, GTK_EXPAND, 0, 0);
}
}
static MateMixerStream *
get_device_output_stream (MateMixerControl *control, MateMixerDevice *device)
{
MateMixerStream *stream;
const GList *streams;
streams = mate_mixer_control_list_streams (control);
while (streams) {
stream = MATE_MIXER_STREAM (streams->data);
if (mate_mixer_stream_get_device (stream) == device) {
if ((mate_mixer_stream_get_flags (stream) &
(MATE_MIXER_STREAM_OUTPUT |
MATE_MIXER_STREAM_CLIENT)) == MATE_MIXER_STREAM_OUTPUT)
break;
}
stream = NULL;
streams = streams->next;
}
return stream;
}
static void
update_channel_map (GvcSpeakerTest *test)
{
MateMixerStream *stream;
guint i;
if (test->priv->control == NULL ||
test->priv->device == NULL)
return;
stream = get_device_output_stream (test->priv->control, test->priv->device);
if (G_UNLIKELY (stream == NULL)) {
g_debug ("Failed to find an output stream for sound device %s",
mate_mixer_device_get_name (test->priv->device));
return;
}
ca_context_change_device (test->priv->canberra,
mate_mixer_stream_get_name (stream));
for (i = 0; i < G_N_ELEMENTS (position_table); i += 3)
gtk_widget_set_visible (test->priv->channel_controls[position_table[i]],
mate_mixer_stream_has_position (stream,
position_table[i]));
}
static void
gvc_speaker_test_init (GvcSpeakerTest *test)
{
GtkWidget *icon;
test->priv = GVC_SPEAKER_TEST_GET_PRIVATE (test);
ca_context_create (&test->priv->canberra);
/* The test sounds are played for a single channel, set up using the
* FORCE_CHANNEL property of libcanberra; this property is only supported
* in the PulseAudio backend, so avoid other backends completely */
ca_context_set_driver (test->priv->canberra, "pulse");
ca_context_change_props (test->priv->canberra,
CA_PROP_APPLICATION_NAME, _("MATE Volume Control Applet"),
CA_PROP_APPLICATION_ID, "org.mate.VolumeControl",
CA_PROP_APPLICATION_VERSION, VERSION,
CA_PROP_APPLICATION_ICON_NAME, "multimedia-volume-control",
NULL);
gtk_table_resize (GTK_TABLE (test), 3, 5);
gtk_container_set_border_width (GTK_CONTAINER (test), 12);
gtk_table_set_homogeneous (GTK_TABLE (test), TRUE);
gtk_table_set_row_spacings (GTK_TABLE (test), 12);
gtk_table_set_col_spacings (GTK_TABLE (test), 12);
create_channel_controls (test);
icon = gtk_image_new_from_icon_name ("computer", GTK_ICON_SIZE_DIALOG);
gtk_table_attach (GTK_TABLE (test), icon,
2, 3, 1, 2,
GTK_EXPAND,
GTK_EXPAND,
0, 0);
gtk_widget_show (icon);
}
static void
gvc_speaker_test_dispose (GObject *object)
{
GvcSpeakerTest *test;
test = GVC_SPEAKER_TEST (object);
g_clear_object (&test->priv->control);
g_clear_object (&test->priv->device);
G_OBJECT_CLASS (gvc_speaker_test_parent_class)->dispose (object);
}
static void
gvc_speaker_test_finalize (GObject *object)
{
GvcSpeakerTest *test;
test = GVC_SPEAKER_TEST (object);
ca_context_destroy (test->priv->canberra);
G_OBJECT_CLASS (gvc_speaker_test_parent_class)->finalize (object);
}
GtkWidget *
gvc_speaker_test_new (MateMixerControl *control, MateMixerDevice *device)
{
GObject *test;
g_return_val_if_fail (MATE_MIXER_IS_CONTROL (control), NULL);
g_return_val_if_fail (MATE_MIXER_IS_DEVICE (device), NULL);
test = g_object_new (GVC_TYPE_SPEAKER_TEST,
"control", control,
"device", device,
NULL);
return GTK_WIDGET (test);
}
|