summaryrefslogtreecommitdiff
path: root/libmatemixer/matemixer.c
blob: 6c25cc54e7841363d32868791ec788c416cae03e (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
/*
 * 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"

static gboolean  mixer_load_backends    (void);
static gint      mixer_compare_modules  (gconstpointer a, gconstpointer b);

static GList    *mixer_modules = NULL;
static gboolean  mixer_initialized = FALSE;

gboolean
mate_mixer_init (void)
{
    if (!mixer_initialized)
        mixer_initialized = mixer_load_backends ();

    return mixer_initialized;
}

void
mate_mixer_deinit (void)
{
    GList *list;

    if (!mixer_initialized)
        return;

    list = mixer_modules;
    while (list) {
        MateMixerBackendModule *module;

        module = MATE_MIXER_BACKEND_MODULE (list->data);

        g_type_module_unuse (G_TYPE_MODULE (module));

        // XXX it is not possible to unref the module, figure out how
        // to handle repeated initialization
        // g_object_unref (module);

        list = list->next;
    }
    g_list_free (mixer_modules);

    mixer_initialized = FALSE;
}

/* Internal function: return a *shared* list of loaded backend modules */
GList *
mate_mixer_get_modules (void)
{
    return mixer_modules;
}

/* Internal function: return TRUE if the library has been initialized */
gboolean
mate_mixer_is_initialized (void)
{
    gboolean initialized;

    G_LOCK_DEFINE_STATIC (mixer_initialized_lock);

    G_LOCK (mixer_initialized_lock);
    initialized = mixer_initialized;
    G_UNLOCK (mixer_initialized_lock);

    return initialized;
}

static gboolean
mixer_load_backends (void)
{
    GDir   *dir;
    GError *error = NULL;

    if (!g_module_supported ()) {
        g_critical ("Unable to load backend modules: GModule not supported");
        return FALSE;
    }

    /* Read the directory which contains module libraries and load them */
    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;
            MateMixerBackendModule *module;

            if (!g_str_has_suffix (name, "." G_MODULE_SUFFIX))
                continue;

            file = g_build_filename (LIBMATEMIXER_BACKEND_DIR, name, NULL);
            module = mate_mixer_backend_module_new (file);

            /* Load the backend module and make sure it includes all the
             * required symbols */
            if (g_type_module_use (G_TYPE_MODULE (module)))
                mixer_modules = g_list_prepend (mixer_modules, module);
            else
                g_object_unref (module);

            g_free (file);
        }

        if (mixer_modules == NULL)
            g_critical ("No usable backend modules have been found");

        g_dir_close (dir);
    } else {
        g_critical ("%s", error->message);
        g_error_free (error);
    }

    if (mixer_modules) {
        mixer_modules = g_list_sort (mixer_modules, mixer_compare_modules);
        return TRUE;
    }

    /* As we include a "null" backend module to provide no functionality,
     * we fail the initialization in case no module could be loaded */
    return FALSE;
}

/* GCompareFunc function to sort backend modules by the priority, lower
 * number means higher priority */
static gint
mixer_compare_modules (gconstpointer a, gconstpointer b)
{
    const MateMixerBackendModuleInfo *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 info1->priority - info2->priority;
}