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
/*
 * Copyright (C) 2014 Michal Ratajsky <michal.ratajsky@gmail.com>
 *
 * This program 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 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>
#include <locale.h>
#include <glib.h>
#include <glib-object.h>

#ifdef G_OS_UNIX
#include <glib-unix.h>
#endif

#include <libcafemixer/cafemixer.h>

static CafeMixerContext *context;
static GMainLoop        *mainloop;

/* Convert CafeMixerStreamControlRole constant to a string */
static const gchar *
get_role_string (CafeMixerStreamControlRole role)
{
    switch (role) {
    case CAFE_MIXER_STREAM_CONTROL_ROLE_MASTER:
        return "Master";
    case CAFE_MIXER_STREAM_CONTROL_ROLE_APPLICATION:
        return "Application";
    case CAFE_MIXER_STREAM_CONTROL_ROLE_PCM:
        return "PCM";
    case CAFE_MIXER_STREAM_CONTROL_ROLE_SPEAKER:
        return "Speaker";
    case CAFE_MIXER_STREAM_CONTROL_ROLE_MICROPHONE:
        return "Microphone";
    case CAFE_MIXER_STREAM_CONTROL_ROLE_PORT:
        return "Port";
    case CAFE_MIXER_STREAM_CONTROL_ROLE_BOOST:
        return "Boost";
    case CAFE_MIXER_STREAM_CONTROL_ROLE_BASS:
        return "Bass";
    case CAFE_MIXER_STREAM_CONTROL_ROLE_TREBLE:
        return "Treble";
    case CAFE_MIXER_STREAM_CONTROL_ROLE_CD:
        return "CD";
    case CAFE_MIXER_STREAM_CONTROL_ROLE_VIDEO:
        return "Video";
    case CAFE_MIXER_STREAM_CONTROL_ROLE_MUSIC:
        return "Music";
    default:
        return "Unknown";
    }
}

/* Convert CafeMixerStreamControlMediaRole constant to a string */
static const gchar *
get_media_role_string (CafeMixerStreamControlMediaRole role)
{
    switch (role) {
    case CAFE_MIXER_STREAM_CONTROL_MEDIA_ROLE_VIDEO:
        return "Video";
    case CAFE_MIXER_STREAM_CONTROL_MEDIA_ROLE_MUSIC:
        return "Music";
    case CAFE_MIXER_STREAM_CONTROL_MEDIA_ROLE_GAME:
        return "Game";
    case CAFE_MIXER_STREAM_CONTROL_MEDIA_ROLE_EVENT:
        return "Event";
    case CAFE_MIXER_STREAM_CONTROL_MEDIA_ROLE_PHONE:
        return "Phone";
    case CAFE_MIXER_STREAM_CONTROL_MEDIA_ROLE_ANIMATION:
        return "Animation";
    case CAFE_MIXER_STREAM_CONTROL_MEDIA_ROLE_PRODUCTION:
        return "Production";
    case CAFE_MIXER_STREAM_CONTROL_MEDIA_ROLE_A11Y:
        return "A11y";
    case CAFE_MIXER_STREAM_CONTROL_MEDIA_ROLE_TEST:
        return "Test";
    case CAFE_MIXER_STREAM_CONTROL_MEDIA_ROLE_ABSTRACT:
        return "Abstract";
    case CAFE_MIXER_STREAM_CONTROL_MEDIA_ROLE_FILTER:
        return "Filter";
    default:
        return "Unknown";
    }
}

/* Convert CafeMixerDeviceSwitchRole constant to a string */
static const gchar *
get_device_switch_role_string (CafeMixerDeviceSwitchRole role)
{
    switch (role) {
    case CAFE_MIXER_DEVICE_SWITCH_ROLE_PROFILE:
        return "Device Profile";
    default:
        return "Unknown";
    }
}

/* Convert CafeMixerStreamSwitchRole constant to a string */
static const gchar *
get_stream_switch_role_string (CafeMixerStreamSwitchRole role)
{
    switch (role) {
    case CAFE_MIXER_STREAM_SWITCH_ROLE_PORT:
        return "Port";
    case CAFE_MIXER_STREAM_SWITCH_ROLE_BOOST:
        return "Boost";
    default:
        return "Unknown";
    }
}

/* Convert CafeMixerDirection constant to a string */
static const gchar *
get_direction_string (CafeMixerDirection direction)
{
    switch (direction) {
    case CAFE_MIXER_DIRECTION_INPUT:
        return "Record";
    case CAFE_MIXER_DIRECTION_OUTPUT:
        return "Playback";
    default:
        return "Unknown";
    }
}

static gdouble
get_volume_percentage (CafeMixerStreamControl *control)
{
    guint volume;
    guint volume_min;
    guint volume_max;

    volume     = cafe_mixer_stream_control_get_volume (control);
    volume_min = cafe_mixer_stream_control_get_min_volume (control);
    volume_max = cafe_mixer_stream_control_get_normal_volume (control);

    return (gdouble) (volume - volume_min) / (volume_max - volume_min) * 100;
}

/* Print a list of sound devices */
static void
print_devices (void)
{
    const GList *devices;

    /* Read a list of devices from the context */
    devices = cafe_mixer_context_list_devices (context);
    while (devices != NULL) {
        const GList     *switches;
        CafeMixerDevice *device = CAFE_MIXER_DEVICE (devices->data);

        g_print ("Device %s:\n"
                 "\tLabel     : %s\n"
                 "\tIcon Name : %s\n\n",
                 cafe_mixer_device_get_name (device),
                 cafe_mixer_device_get_label (device),
                 cafe_mixer_device_get_icon (device));

        /* Read a list of switches that belong to the device */
        switches = cafe_mixer_device_list_switches (device);
        while (switches != NULL) {
            const GList           *options;
            CafeMixerSwitch       *swtch  = CAFE_MIXER_SWITCH (switches->data);
            CafeMixerSwitchOption *active = cafe_mixer_switch_get_active_option (swtch);

            g_print ("\tSwitch %s:\n"
                     "\t\tLabel : %s\n"
                     "\t\tRole  : %s\n",
                     cafe_mixer_switch_get_name (swtch),
                     cafe_mixer_switch_get_label (swtch),
                     get_device_switch_role_string (cafe_mixer_device_switch_get_role (CAFE_MIXER_DEVICE_SWITCH (swtch))));

            g_print ("\tOptions:\n");

            /* Read a list of switch options that belong to the switch */
            options = cafe_mixer_switch_list_options (swtch);
            while (options != NULL) {
                CafeMixerSwitchOption *option = CAFE_MIXER_SWITCH_OPTION (options->data);

                g_print ("\t\t|%c| %s\n",
                         (active != NULL && option == active)
                            ? '*'
                            : '-',
                         cafe_mixer_switch_option_get_label (option));

                options = options->next;
            }

            g_print ("\n");
            switches = switches->next;
        }

        devices = devices->next;
    }
}

/* Print a list of streams */
static void
print_streams (void)
{
    const GList *streams;

    /* Read a list of streams from the context */
    streams = cafe_mixer_context_list_streams (context);
    while (streams != NULL) {
        CafeMixerStream *stream = CAFE_MIXER_STREAM (streams->data);
        const GList     *controls;
        const GList     *switches;

        g_print ("Stream %s:\n"
                 "\tLabel     : %s\n"
                 "\tDirection : %s\n\n",
                 cafe_mixer_stream_get_name (stream),
                 cafe_mixer_stream_get_label (stream),
                 get_direction_string (cafe_mixer_stream_get_direction (stream)));

        /* Read a list of controls in the stream */
        controls = cafe_mixer_stream_list_controls (stream);
        while (controls != NULL) {
            CafeMixerStreamControl *control = CAFE_MIXER_STREAM_CONTROL (controls->data);

            g_print ("\tControl %s:\n"
                     "\t\tLabel      : %s\n"
                     "\t\tVolume     : %.0f %%\n"
                     "\t\tMuted      : %s\n"
                     "\t\tChannels   : %d\n"
                     "\t\tBalance    : %.1f\n"
                     "\t\tFade       : %.1f\n"
                     "\t\tRole       : %s\n"
                     "\t\tMedia role : %s\n",
                     cafe_mixer_stream_control_get_name (control),
                     cafe_mixer_stream_control_get_label (control),
                     get_volume_percentage (control),
                     cafe_mixer_stream_control_get_mute (control) ? "Yes" : "No",
                     cafe_mixer_stream_control_get_num_channels (control),
                     cafe_mixer_stream_control_get_balance (control),
                     cafe_mixer_stream_control_get_fade (control),
                     get_role_string (cafe_mixer_stream_control_get_role (control)),
                     get_media_role_string (cafe_mixer_stream_control_get_media_role (control)));

            g_print ("\n");
            controls = controls->next;
        }

        /* Read a list of switches in the stream */
        switches = cafe_mixer_stream_list_switches (stream);
        while (switches != NULL) {
            const GList           *options;
            CafeMixerSwitch       *swtch  = CAFE_MIXER_SWITCH (switches->data);
            CafeMixerSwitchOption *active = cafe_mixer_switch_get_active_option (swtch);

            g_print ("\tSwitch %s:\n"
                     "\t\tLabel      : %s\n"
                     "\t\tRole       : %s\n",
                     cafe_mixer_switch_get_name (swtch),
                     cafe_mixer_switch_get_label (swtch),
                     get_stream_switch_role_string (cafe_mixer_stream_switch_get_role (CAFE_MIXER_STREAM_SWITCH (swtch))));

            g_print ("\tOptions:\n");

            /* Read a list of switch options that belong to the switch */
            options = cafe_mixer_switch_list_options (swtch);
            while (options != NULL) {
                CafeMixerSwitchOption *option = CAFE_MIXER_SWITCH_OPTION (options->data);

                g_print ("\t\t|%c| %s\n",
                         (active != NULL && option == active)
                            ? '*'
                            : '-',
                         cafe_mixer_switch_option_get_label (option));

                options = options->next;
            }

            g_print ("\n");
            switches = switches->next;
        }

        streams = streams->next;
    }
}

static void
connected (void)
{
    g_print ("Connected using the %s backend.\n\n",
             cafe_mixer_context_get_backend_name (context));

    print_devices ();
    print_streams ();

    g_print ("Waiting for events. Hit CTRL+C to quit.\n");
}

static void
on_context_state_notify (void)
{
    CafeMixerState state;

    state = cafe_mixer_context_get_state (context);

    switch (state) {
    case CAFE_MIXER_STATE_READY:
        /* This state can be reached repeatedly if the context is connected
         * to a sound server, the connection is dropped and then reestablished */
        connected ();
        break;
    case CAFE_MIXER_STATE_FAILED:
        g_printerr ("Connection failed.\n");
        g_main_loop_quit (mainloop);
        break;
    default:
        break;
    }
}

static void
on_context_device_added (CafeMixerContext *context G_GNUC_UNUSED,
			 const gchar      *name)
{
    g_print ("Device added: %s\n", name);
}

static void
on_context_device_removed (CafeMixerContext *context G_GNUC_UNUSED,
			   const gchar      *name)
{
    g_print ("Device removed: %s\n", name);
}

static void
on_context_stream_added (CafeMixerContext *context G_GNUC_UNUSED,
			 const gchar      *name)
{
    g_print ("Stream added: %s\n", name);
}

static void
on_context_stream_removed (CafeMixerContext *context G_GNUC_UNUSED,
			   const gchar      *name)
{
    g_print ("Stream removed: %s\n", name);
}

#ifdef G_OS_UNIX
static gboolean
on_signal (gpointer mainloop)
{
    g_idle_add ((GSourceFunc) g_main_loop_quit, mainloop);

    return G_SOURCE_REMOVE;
}
#endif

int main (int argc, char *argv[])
{
    CafeMixerState  state;
    GOptionContext *ctx;
    gboolean        debug   = FALSE;
    gchar          *backend = NULL;
    gchar          *server  = NULL;
    GError         *error   = NULL;
    GOptionEntry    entries[] = {
        { "backend", 'b', 0, G_OPTION_ARG_STRING, &backend, "Sound system to use (pulseaudio, alsa, oss, null)", NULL },
        { "debug",   'd', 0, G_OPTION_ARG_NONE,   &debug,   "Enable debug", NULL },
        { "server",  's', 0, G_OPTION_ARG_STRING, &server,  "Sound server address", NULL },
        { NULL }
    };

    ctx = g_option_context_new ("- libcafemixer monitor");

    g_option_context_add_main_entries (ctx, entries, NULL);

    if (g_option_context_parse (ctx, &argc, &argv, &error) == FALSE) {
        g_printerr ("%s\n", error->message);
        g_error_free (error);
        g_option_context_free (ctx);
        return 1;
    }

    g_option_context_free (ctx);

    if (debug == TRUE)
        g_setenv ("G_MESSAGES_DEBUG", "all", FALSE);

    /* Initialize the library.
     * If the function returns FALSE, the library is not usable. */
    if (cafe_mixer_init () == FALSE)
        return 1;

    setlocale (LC_ALL, "");

    /* Create a libcafemixer context to access the library */
    context = cafe_mixer_context_new ();

    /* Fill in some details about our application, only used with the PulseAudio backend */
    cafe_mixer_context_set_app_name (context, "CafeMixer Monitor");
    cafe_mixer_context_set_app_id (context, "org.cafe-desktop.libcafemixer-monitor");
    cafe_mixer_context_set_app_version (context, "1.0");
    cafe_mixer_context_set_app_icon (context, "multimedia-volume-control");

    if (backend != NULL) {
        if (strcmp (backend, "pulseaudio") == 0)
            cafe_mixer_context_set_backend_type (context, CAFE_MIXER_BACKEND_PULSEAUDIO);
        else if (strcmp (backend, "alsa") == 0)
            cafe_mixer_context_set_backend_type (context, CAFE_MIXER_BACKEND_ALSA);
        else if (strcmp (backend, "oss") == 0)
            cafe_mixer_context_set_backend_type (context, CAFE_MIXER_BACKEND_OSS);
        else if (strcmp (backend, "null") == 0)
            cafe_mixer_context_set_backend_type (context, CAFE_MIXER_BACKEND_NULL);
        else
            g_printerr ("Sound system backend '%s' is unknown, the backend will be auto-detected.\n",
                        backend);

        g_free (backend);
    }

    /* Set PulseAudio server address if requested */
    if (server != NULL) {
        cafe_mixer_context_set_server_address (context, server);
        g_free (server);
    }

    /* Initiate connection to a sound system */
    if (cafe_mixer_context_open (context) == FALSE) {
        g_printerr ("Could not connect to a sound system, quitting.\n");
        g_object_unref (context);
        return 1;
    }

    /* Connect to some basic signals of the context */
    g_signal_connect (G_OBJECT (context),
                      "device-added",
                      G_CALLBACK (on_context_device_added),
                      NULL);
    g_signal_connect (G_OBJECT (context),
                      "device-removed",
                      G_CALLBACK (on_context_device_removed),
                      NULL);
    g_signal_connect (G_OBJECT (context),
                      "stream-added",
                      G_CALLBACK (on_context_stream_added),
                      NULL);
    g_signal_connect (G_OBJECT (context),
                      "stream-removed",
                      G_CALLBACK (on_context_stream_removed),
                      NULL);

    /* When cafe_mixer_context_open() returns TRUE, the state must be either
     * CAFE_MIXER_STATE_READY or CAFE_MIXER_STATE_CONNECTING. */
    state = cafe_mixer_context_get_state (context);

    switch (state) {
    case CAFE_MIXER_STATE_READY:
        connected ();
        break;
    case CAFE_MIXER_STATE_CONNECTING:
        g_print ("Waiting for connection...\n");

        /* The state will change asynchronously to either CAFE_MIXER_STATE_READY
         * or CAFE_MIXER_STATE_FAILED, wait for the change in a main loop */
        g_signal_connect (G_OBJECT (context),
                          "notify::state",
                          G_CALLBACK (on_context_state_notify),
                          NULL);
        break;
    default:
        g_assert_not_reached ();
        break;
    }

    mainloop = g_main_loop_new (NULL, FALSE);

#ifdef G_OS_UNIX
    g_unix_signal_add (SIGTERM, on_signal, mainloop);
    g_unix_signal_add (SIGINT,  on_signal, mainloop);
#endif

    g_main_loop_run (mainloop);

    g_object_unref (context);
    return 0;
}