summaryrefslogtreecommitdiff
path: root/libmatemixer/matemixer.c
blob: 1c09c7b50f55cf14331bc52fa92f1d3c71601bd2 (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
/*
 * 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 "config.h"

#include <glib.h>
#include <glib-object.h>
#include <gmodule.h>

#include "matemixer.h"
#include "matemixer-private.h"
#include "matemixer-backend-module.h"

/**
 * SECTION:matemixer
 * @short_description: Library initialization and support functions
 * @include: libmatemixer/matemixer.h
 * @see_also: #MateMixerContext
 *
 * The libmatemixer library must be initialized before it is used by an
 * application. The initialization function loads dynamic modules which provide
 * access to sound systems (also called backends) and it only succeeds if there
 * is at least one usable module present on the target system.
 *
 * To connect to a sound system and access the mixer functionality after the
 * library is initialized, create a #MateMixerContext using the
 * mate_mixer_context_new() function.
 */

static void       load_modules     (void);
static gint       compare_modules  (gconstpointer a,
                                    gconstpointer b);

static GList     *modules = NULL;
static gboolean   initialized = FALSE;

/**
 * mate_mixer_init:
 *
 * Initializes the library. You must call this function before using any other
 * function from the library.
 *
 * Returns: %TRUE on success or %FALSE if the library installation does not
 * provide support for any sound system backends.
 */
gboolean
mate_mixer_init (void)
{
    if (initialized == TRUE)
        return TRUE;

    load_modules ();

    if (modules != NULL) {
        GList *list = modules;

        while (list != NULL) {
            GTypeModule *module = G_TYPE_MODULE (list->data);
            GList       *next = list->next;

            /* Load the plugin and remove it from the list if it fails */
            if (g_type_module_use (module) == FALSE) {
                g_object_unref (module);
                modules = g_list_delete_link (modules, list);
            }
            list = next;
        }

        if (modules != NULL) {
            /* Sort the usable modules by priority */
            modules = g_list_sort (modules, compare_modules);
            initialized = TRUE;
        } else
            g_critical ("No usable backend modules have been found");
    } else
        g_critical ("No backend modules have been found");

    return initialized;
}

/**
 * mate_mixer_is_initialized:
 *
 * Returns %TRUE if the library has been initialized.
 *
 * Returns: %TRUE or %FALSE.
 */
gboolean
mate_mixer_is_initialized (void)
{
    return initialized;
}

/**
 * _mate_mixer_list_modules:
 *
 * Gets a list of loaded backend modules.
 *
 * Returns: a #GList.
 */
const GList *
_mate_mixer_list_modules (void)
{
    return (const GList *) modules;
}

/**
 * _mate_mixer_create_channel_mask:
 * @positions: an array of channel positions
 * @n: number of channel positions in the array
 *
 * Creates a channel mask using the given list of channel positions.
 *
 * Returns: a channel mask.
 */
guint32
_mate_mixer_create_channel_mask (MateMixerChannelPosition *positions, guint n)
{
    guint32 mask = 0;
    guint   i = 0;

    for (i = 0; i < n; i++) {
        if (positions[i] > MATE_MIXER_CHANNEL_UNKNOWN &&
            positions[i] < MATE_MIXER_CHANNEL_MAX)
            mask |= 1 << positions[i];
    }
    return mask;
}

static void
load_modules (void)
{
    static gboolean loaded = FALSE;

    if (loaded == TRUE)
        return;

    if G_LIKELY (g_module_supported () == TRUE) {
        GDir   *dir;
        GError *error = NULL;

        /* Read the directory which contains module libraries and create a list
         * of those that are likely to be usable backend modules */
        dir = g_dir_open (LIBMATEMIXER_BACKEND_DIR, 0, &error);
        if (dir != NULL) {
            const gchar *name;

            while ((name = g_dir_read_name (dir)) != NULL) {
                gchar *file;

                if (g_str_has_suffix (name, "." G_MODULE_SUFFIX) == FALSE)
                    continue;

                file = g_build_filename (LIBMATEMIXER_BACKEND_DIR, name, NULL);
                modules = g_list_prepend (modules,
                                          mate_mixer_backend_module_new (file));
                g_free (file);
            }

            g_dir_close (dir);
        } else {
            g_critical ("%s", error->message);
            g_error_free (error);
        }
    } else {
        g_critical ("Unable to load backend modules: Not supported");
    }

    loaded = TRUE;
}

/* Backend modules sorting function, higher priority number means higher priority
 * of the backend module */
static gint
compare_modules (gconstpointer a, gconstpointer b)
{
    const MateMixerBackendInfo *info1, *info2;

    info1 = mate_mixer_backend_module_get_info (MATE_MIXER_BACKEND_MODULE (a));
    info2 = mate_mixer_backend_module_get_info (MATE_MIXER_BACKEND_MODULE (b));

    return info2->priority - info1->priority;
}