-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.c
496 lines (433 loc) · 10.5 KB
/
output.c
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
/*
* Copyright 2008-2013 Various Authors
* Copyright 2004-2006 Timo Hirvonen
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "output.h"
#include "op.h"
#include "mixer.h"
#include "sf.h"
#include "utils.h"
#include "xmalloc.h"
#include "list.h"
#include "debug.h"
#include "ui_curses.h"
#include "options.h"
#include "xstrjoin.h"
#include "misc.h"
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <dirent.h>
#include <dlfcn.h>
struct output_plugin {
struct list_head node;
char *name;
void *handle;
const unsigned *abi_version_ptr;
const struct output_plugin_ops *pcm_ops;
const struct mixer_plugin_ops *mixer_ops;
const struct output_plugin_opt *pcm_options;
const struct mixer_plugin_opt *mixer_options;
int priority;
unsigned int pcm_initialized : 1;
unsigned int mixer_initialized : 1;
unsigned int mixer_open : 1;
};
static const char *plugin_dir;
static LIST_HEAD(op_head);
static struct output_plugin *op = NULL;
/* volume is between 0 and volume_max */
int volume_max = 0;
int volume_l = -1;
int volume_r = -1;
static void add_plugin(struct output_plugin *plugin)
{
struct list_head *item = op_head.next;
while (item != &op_head) {
struct output_plugin *o = container_of(item, struct output_plugin, node);
if (plugin->priority < o->priority)
break;
item = item->next;
}
/* add before item */
list_add_tail(&plugin->node, item);
}
void op_load_plugins(void)
{
DIR *dir;
struct dirent *d;
plugin_dir = xstrjoin(cmus_lib_dir, "/op");
dir = opendir(plugin_dir);
if (dir == NULL) {
error_msg("couldn't open directory `%s': %s", plugin_dir, strerror(errno));
return;
}
while ((d = (struct dirent *) readdir(dir)) != NULL) {
char filename[512];
struct output_plugin *plug;
void *so, *symptr;
char *ext;
bool err = false;
if (d->d_name[0] == '.')
continue;
ext = strrchr(d->d_name, '.');
if (ext == NULL)
continue;
if (strcmp(ext, ".so"))
continue;
snprintf(filename, sizeof(filename), "%s/%s", plugin_dir, d->d_name);
so = dlopen(filename, RTLD_NOW);
if (so == NULL) {
d_print("%s: %s\n", filename, dlerror());
continue;
}
plug = xnew(struct output_plugin, 1);
plug->pcm_ops = dlsym(so, "op_pcm_ops");
plug->pcm_options = dlsym(so, "op_pcm_options");
symptr = dlsym(so, "op_priority");
plug->abi_version_ptr = dlsym(so, "op_abi_version");
if (!plug->pcm_ops || !plug->pcm_options || !symptr) {
error_msg("%s: missing symbol", filename);
err = true;
}
STATIC_ASSERT(OP_ABI_VERSION == 2);
if (!plug->abi_version_ptr || (*plug->abi_version_ptr != 1 && *plug->abi_version_ptr != 2)) {
error_msg("%s: incompatible plugin version", filename);
err = true;
}
if (err) {
free(plug);
dlclose(so);
continue;
}
plug->priority = *(int *)symptr;
plug->mixer_ops = dlsym(so, "op_mixer_ops");
plug->mixer_options = dlsym(so, "op_mixer_options");
if (plug->mixer_ops == NULL || plug->mixer_options == NULL) {
plug->mixer_ops = NULL;
plug->mixer_options = NULL;
}
plug->name = xstrndup(d->d_name, ext - d->d_name);
plug->handle = so;
plug->pcm_initialized = 0;
plug->mixer_initialized = 0;
plug->mixer_open = 0;
add_plugin(plug);
}
closedir(dir);
}
static void init_plugin(struct output_plugin *o)
{
if (!o->mixer_initialized && o->mixer_ops) {
if (o->mixer_ops->init() == 0) {
d_print("initialized mixer for %s\n", o->name);
o->mixer_initialized = 1;
} else {
d_print("could not initialize mixer `%s'\n", o->name);
}
}
if (!o->pcm_initialized) {
if (o->pcm_ops->init() == 0) {
d_print("initialized pcm for %s\n", o->name);
o->pcm_initialized = 1;
} else {
d_print("could not initialize pcm `%s'\n", o->name);
}
}
}
void op_exit_plugins(void)
{
struct output_plugin *o;
list_for_each_entry(o, &op_head, node) {
if (o->mixer_initialized && o->mixer_ops)
o->mixer_ops->exit();
if (o->pcm_initialized)
o->pcm_ops->exit();
}
}
void mixer_close(void)
{
volume_max = 0;
if (op && op->mixer_open) {
BUG_ON(op->mixer_ops == NULL);
op->mixer_ops->close();
op->mixer_open = 0;
}
}
void mixer_open(void)
{
if (op == NULL)
return;
BUG_ON(op->mixer_open);
if (op->mixer_ops && op->mixer_initialized) {
int rc;
rc = op->mixer_ops->open(&volume_max);
if (rc == 0) {
op->mixer_open = 1;
mixer_read_volume();
} else {
volume_max = 0;
}
}
}
static int select_plugin(struct output_plugin *o)
{
/* try to initialize if not initialized yet */
init_plugin(o);
if (!o->pcm_initialized)
return -OP_ERROR_NOT_INITIALIZED;
op = o;
return 0;
}
int op_select(const char *name)
{
struct output_plugin *o;
list_for_each_entry(o, &op_head, node) {
if (strcasecmp(name, o->name) == 0)
return select_plugin(o);
}
return -OP_ERROR_NO_PLUGIN;
}
int op_select_any(void)
{
struct output_plugin *o;
int rc = -OP_ERROR_NO_PLUGIN;
sample_format_t sf = sf_channels(2) | sf_rate(44100) | sf_bits(16) | sf_signed(1);
list_for_each_entry(o, &op_head, node) {
rc = select_plugin(o);
if (rc != 0)
continue;
rc = o->pcm_ops->open(sf, NULL);
if (rc == 0) {
o->pcm_ops->close();
break;
}
}
return rc;
}
int op_open(sample_format_t sf, const channel_position_t *channel_map)
{
if (op == NULL)
return -OP_ERROR_NOT_INITIALIZED;
return op->pcm_ops->open(sf, channel_map);
}
int op_drop(void)
{
if (op->pcm_ops->drop == NULL)
return -OP_ERROR_NOT_SUPPORTED;
return op->pcm_ops->drop();
}
int op_close(void)
{
return op->pcm_ops->close();
}
int op_write(const char *buffer, int count)
{
return op->pcm_ops->write(buffer, count);
}
int op_pause(void)
{
if (op->pcm_ops->pause == NULL)
return 0;
return op->pcm_ops->pause();
}
int op_unpause(void)
{
if (op->pcm_ops->unpause == NULL)
return 0;
return op->pcm_ops->unpause();
}
int op_buffer_space(void)
{
return op->pcm_ops->buffer_space();
}
int mixer_set_volume(int left, int right)
{
if (op == NULL)
return -OP_ERROR_NOT_INITIALIZED;
if (!op->mixer_open)
return -OP_ERROR_NOT_OPEN;
return op->mixer_ops->set_volume(left, right);
}
int mixer_read_volume(void)
{
if (op == NULL)
return -OP_ERROR_NOT_INITIALIZED;
if (!op->mixer_open)
return -OP_ERROR_NOT_OPEN;
return op->mixer_ops->get_volume(&volume_l, &volume_r);
}
int mixer_get_fds(int what, int *fds)
{
if (op == NULL)
return -OP_ERROR_NOT_INITIALIZED;
if (!op->mixer_open)
return -OP_ERROR_NOT_OPEN;
switch (*op->abi_version_ptr) {
case 1:
if (!op->mixer_ops->get_fds.abi_1)
return -OP_ERROR_NOT_SUPPORTED;
if (what != MIXER_FDS_VOLUME)
return 0;
return op->mixer_ops->get_fds.abi_1(fds);
default:
if (!op->mixer_ops->get_fds.abi_2)
return -OP_ERROR_NOT_SUPPORTED;
return op->mixer_ops->get_fds.abi_2(what, fds);
}
}
extern int soft_vol;
static void option_error(int rc)
{
char *msg = op_get_error_msg(rc, "setting option");
error_msg("%s", msg);
free(msg);
}
static void set_dsp_option(void *data, const char *val)
{
const struct output_plugin_opt *o = data;
int rc;
rc = o->set(val);
if (rc)
option_error(rc);
}
static bool option_of_current_mixer(const struct mixer_plugin_opt *opt)
{
const struct mixer_plugin_opt *mpo;
if (!op)
return false;
for (mpo = op->mixer_options; mpo && mpo->name; mpo++) {
if (mpo == opt)
return true;
}
return false;
}
static void set_mixer_option(void *data, const char *val)
{
const struct mixer_plugin_opt *o = data;
int rc;
rc = o->set(val);
if (rc) {
option_error(rc);
} else if (option_of_current_mixer(o)) {
/* option of the current op was set
* try to reopen the mixer */
mixer_close();
if (!soft_vol)
mixer_open();
}
}
static void get_dsp_option(void *data, char *buf, size_t size)
{
const struct output_plugin_opt *o = data;
char *val = NULL;
o->get(&val);
if (val) {
strscpy(buf, val, size);
free(val);
}
}
static void get_mixer_option(void *data, char *buf, size_t size)
{
const struct mixer_plugin_opt *o = data;
char *val = NULL;
o->get(&val);
if (val) {
strscpy(buf, val, size);
free(val);
}
}
void op_add_options(void)
{
struct output_plugin *o;
const struct output_plugin_opt *opo;
const struct mixer_plugin_opt *mpo;
char key[64];
list_for_each_entry(o, &op_head, node) {
for (opo = o->pcm_options; opo->name; opo++) {
snprintf(key, sizeof(key), "dsp.%s.%s", o->name,
opo->name);
option_add(xstrdup(key), opo, get_dsp_option,
set_dsp_option, NULL, 0);
}
for (mpo = o->mixer_options; mpo && mpo->name; mpo++) {
snprintf(key, sizeof(key), "mixer.%s.%s", o->name,
mpo->name);
option_add(xstrdup(key), mpo, get_mixer_option,
set_mixer_option, NULL, 0);
}
}
}
char *op_get_error_msg(int rc, const char *arg)
{
char buffer[1024];
switch (-rc) {
case OP_ERROR_ERRNO:
snprintf(buffer, sizeof(buffer), "%s: %s", arg, strerror(errno));
break;
case OP_ERROR_NO_PLUGIN:
snprintf(buffer, sizeof(buffer),
"%s: no such plugin", arg);
break;
case OP_ERROR_NOT_INITIALIZED:
snprintf(buffer, sizeof(buffer),
"%s: couldn't initialize required output plugin", arg);
break;
case OP_ERROR_NOT_SUPPORTED:
snprintf(buffer, sizeof(buffer),
"%s: function not supported", arg);
break;
case OP_ERROR_NOT_OPEN:
snprintf(buffer, sizeof(buffer),
"%s: mixer is not open", arg);
break;
case OP_ERROR_SAMPLE_FORMAT:
snprintf(buffer, sizeof(buffer),
"%s: sample format not supported", arg);
break;
case OP_ERROR_NOT_OPTION:
snprintf(buffer, sizeof(buffer),
"%s: no such option", arg);
break;
case OP_ERROR_INTERNAL:
snprintf(buffer, sizeof(buffer), "%s: internal error", arg);
break;
case OP_ERROR_SUCCESS:
default:
snprintf(buffer, sizeof(buffer),
"%s: this is not an error (%d), this is a bug",
arg, rc);
break;
}
return xstrdup(buffer);
}
void op_dump_plugins(void)
{
struct output_plugin *o;
printf("\nOutput Plugins: %s\n", plugin_dir);
list_for_each_entry(o, &op_head, node) {
printf(" %s\n", o->name);
}
}
const char *op_get_current(void)
{
if (op)
return op->name;
return NULL;
}