~ubuntu-branches/ubuntu/utopic/mpd/utopic-proposed

« back to all changes in this revision

Viewing changes to src/mixer_control.c

  • Committer: Package Import Robot
  • Author(s): Steve Kowalik
  • Date: 2013-11-12 18:17:40 UTC
  • mfrom: (2.2.36 sid)
  • Revision ID: package-import@ubuntu.com-20131112181740-72aa4zihehoobedp
Tags: 0.18.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Add libmp3lame-dev to Build-Depends, and enable LAME.
  - Read the user for the daemon from the config file in the init script.
  - Move avahi-daemon from Suggests to Recommends.
  - Added apport hook to include user configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 
 * http://www.musicpd.org
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License along
16
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
#include "mixer_control.h"
22
 
#include "mixer_api.h"
23
 
 
24
 
#include <assert.h>
25
 
#include <stddef.h>
26
 
 
27
 
#undef G_LOG_DOMAIN
28
 
#define G_LOG_DOMAIN "mixer"
29
 
 
30
 
struct mixer *
31
 
mixer_new(const struct mixer_plugin *plugin, void *ao,
32
 
          const struct config_param *param,
33
 
          GError **error_r)
34
 
{
35
 
        struct mixer *mixer;
36
 
 
37
 
        assert(plugin != NULL);
38
 
 
39
 
        mixer = plugin->init(ao, param, error_r);
40
 
 
41
 
        assert(mixer == NULL || mixer->plugin == plugin);
42
 
 
43
 
        return mixer;
44
 
}
45
 
 
46
 
void
47
 
mixer_free(struct mixer *mixer)
48
 
{
49
 
        assert(mixer != NULL);
50
 
        assert(mixer->plugin != NULL);
51
 
        assert(mixer->mutex != NULL);
52
 
 
53
 
        /* mixers with the "global" flag set might still be open at
54
 
           this point (see mixer_auto_close()) */
55
 
        mixer_close(mixer);
56
 
 
57
 
        g_mutex_free(mixer->mutex);
58
 
 
59
 
        mixer->plugin->finish(mixer);
60
 
}
61
 
 
62
 
bool
63
 
mixer_open(struct mixer *mixer, GError **error_r)
64
 
{
65
 
        bool success;
66
 
 
67
 
        assert(mixer != NULL);
68
 
        assert(mixer->plugin != NULL);
69
 
 
70
 
        g_mutex_lock(mixer->mutex);
71
 
 
72
 
        if (mixer->open)
73
 
                success = true;
74
 
        else if (mixer->plugin->open == NULL)
75
 
                success = mixer->open = true;
76
 
        else
77
 
                success = mixer->open = mixer->plugin->open(mixer, error_r);
78
 
 
79
 
        mixer->failed = !success;
80
 
 
81
 
        g_mutex_unlock(mixer->mutex);
82
 
 
83
 
        return success;
84
 
}
85
 
 
86
 
static void
87
 
mixer_close_internal(struct mixer *mixer)
88
 
{
89
 
        assert(mixer != NULL);
90
 
        assert(mixer->plugin != NULL);
91
 
        assert(mixer->open);
92
 
 
93
 
        if (mixer->plugin->close != NULL)
94
 
                mixer->plugin->close(mixer);
95
 
 
96
 
        mixer->open = false;
97
 
}
98
 
 
99
 
void
100
 
mixer_close(struct mixer *mixer)
101
 
{
102
 
        assert(mixer != NULL);
103
 
        assert(mixer->plugin != NULL);
104
 
 
105
 
        g_mutex_lock(mixer->mutex);
106
 
 
107
 
        if (mixer->open)
108
 
                mixer_close_internal(mixer);
109
 
 
110
 
        g_mutex_unlock(mixer->mutex);
111
 
}
112
 
 
113
 
void
114
 
mixer_auto_close(struct mixer *mixer)
115
 
{
116
 
        if (!mixer->plugin->global)
117
 
                mixer_close(mixer);
118
 
}
119
 
 
120
 
/*
121
 
 * Close the mixer due to failure.  The mutex must be locked before
122
 
 * calling this function.
123
 
 */
124
 
static void
125
 
mixer_failed(struct mixer *mixer)
126
 
{
127
 
        assert(mixer->open);
128
 
 
129
 
        mixer_close_internal(mixer);
130
 
 
131
 
        mixer->failed = true;
132
 
}
133
 
 
134
 
int
135
 
mixer_get_volume(struct mixer *mixer, GError **error_r)
136
 
{
137
 
        int volume;
138
 
 
139
 
        assert(mixer != NULL);
140
 
 
141
 
        if (mixer->plugin->global && !mixer->failed &&
142
 
            !mixer_open(mixer, error_r))
143
 
                return -1;
144
 
 
145
 
        g_mutex_lock(mixer->mutex);
146
 
 
147
 
        if (mixer->open) {
148
 
                GError *error = NULL;
149
 
 
150
 
                volume = mixer->plugin->get_volume(mixer, &error);
151
 
                if (volume < 0 && error != NULL) {
152
 
                        g_propagate_error(error_r, error);
153
 
                        mixer_failed(mixer);
154
 
                }
155
 
        } else
156
 
                volume = -1;
157
 
 
158
 
        g_mutex_unlock(mixer->mutex);
159
 
 
160
 
        return volume;
161
 
}
162
 
 
163
 
bool
164
 
mixer_set_volume(struct mixer *mixer, unsigned volume, GError **error_r)
165
 
{
166
 
        bool success;
167
 
 
168
 
        assert(mixer != NULL);
169
 
        assert(volume <= 100);
170
 
 
171
 
        if (mixer->plugin->global && !mixer->failed &&
172
 
            !mixer_open(mixer, error_r))
173
 
                return false;
174
 
 
175
 
        g_mutex_lock(mixer->mutex);
176
 
 
177
 
        if (mixer->open) {
178
 
                success = mixer->plugin->set_volume(mixer, volume, error_r);
179
 
        } else
180
 
                success = false;
181
 
 
182
 
        g_mutex_unlock(mixer->mutex);
183
 
 
184
 
        return success;
185
 
}