| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include <sys/poll.h> |
| 24 |
|
| 25 |
#include <alsa/asoundlib.h> |
| 26 |
#include <alsa/control_external.h> |
| 27 |
|
| 28 |
#include "pulse.h" |
| 29 |
|
| 30 |
typedef struct snd_ctl_pulse { |
| 31 |
snd_ctl_ext_t ext; |
| 32 |
|
| 33 |
snd_pulse_t *p; |
| 34 |
|
| 35 |
char *source; |
| 36 |
char *sink; |
| 37 |
|
| 38 |
pa_cvolume sink_volume; |
| 39 |
pa_cvolume source_volume; |
| 40 |
|
| 41 |
int sink_muted; |
| 42 |
int source_muted; |
| 43 |
|
| 44 |
int subscribed; |
| 45 |
int updated; |
| 46 |
} snd_ctl_pulse_t; |
| 47 |
|
| 48 |
#define SOURCE_VOL_NAME "Capture Volume" |
| 49 |
#define SOURCE_MUTE_NAME "Capture Switch" |
| 50 |
#define SINK_VOL_NAME "Master Playback Volume" |
| 51 |
#define SINK_MUTE_NAME "Master Playback Switch" |
| 52 |
|
| 53 |
#define UPDATE_SINK_VOL 0x01 |
| 54 |
#define UPDATE_SINK_MUTE 0x02 |
| 55 |
#define UPDATE_SOURCE_VOL 0x04 |
| 56 |
#define UPDATE_SOURCE_MUTE 0x08 |
| 57 |
|
| 58 |
static void sink_info_cb(pa_context * c, const pa_sink_info * i, |
| 59 |
int is_last, void *userdata) |
| 60 |
{ |
| 61 |
snd_ctl_pulse_t *ctl = (snd_ctl_pulse_t *) userdata; |
| 62 |
int changed = 0; |
| 63 |
|
| 64 |
assert(ctl); |
| 65 |
|
| 66 |
if (is_last) { |
| 67 |
pa_threaded_mainloop_signal(ctl->p->mainloop, 0); |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
assert(i); |
| 72 |
|
| 73 |
if (!!ctl->sink_muted != !!i->mute) { |
| 74 |
ctl->sink_muted = i->mute; |
| 75 |
ctl->updated |= UPDATE_SINK_MUTE; |
| 76 |
changed = 1; |
| 77 |
} |
| 78 |
|
| 79 |
if (!pa_cvolume_equal(&ctl->sink_volume, &i->volume)) { |
| 80 |
ctl->sink_volume = i->volume; |
| 81 |
ctl->updated |= UPDATE_SINK_VOL; |
| 82 |
changed = 1; |
| 83 |
} |
| 84 |
|
| 85 |
if (changed) |
| 86 |
pulse_poll_activate(ctl->p); |
| 87 |
} |
| 88 |
|
| 89 |
static void source_info_cb(pa_context * c, const pa_source_info * i, |
| 90 |
int is_last, void *userdata) |
| 91 |
{ |
| 92 |
snd_ctl_pulse_t *ctl = (snd_ctl_pulse_t *) userdata; |
| 93 |
int changed = 0; |
| 94 |
|
| 95 |
assert(ctl); |
| 96 |
|
| 97 |
if (is_last) { |
| 98 |
pa_threaded_mainloop_signal(ctl->p->mainloop, 0); |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
assert(i); |
| 103 |
|
| 104 |
if (!!ctl->source_muted != !!i->mute) { |
| 105 |
ctl->source_muted = i->mute; |
| 106 |
ctl->updated |= UPDATE_SOURCE_MUTE; |
| 107 |
changed = 1; |
| 108 |
} |
| 109 |
|
| 110 |
if (!pa_cvolume_equal(&ctl->source_volume, &i->volume)) { |
| 111 |
ctl->source_volume = i->volume; |
| 112 |
ctl->updated |= UPDATE_SOURCE_VOL; |
| 113 |
changed = 1; |
| 114 |
} |
| 115 |
|
| 116 |
if (changed) |
| 117 |
pulse_poll_activate(ctl->p); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
static void event_cb(pa_context * c, pa_subscription_event_type_t t, |
| 122 |
uint32_t index, void *userdata) |
| 123 |
{ |
| 124 |
snd_ctl_pulse_t *ctl = (snd_ctl_pulse_t *) userdata; |
| 125 |
pa_operation *o; |
| 126 |
|
| 127 |
assert(ctl); |
| 128 |
|
| 129 |
if (!ctl->p || !ctl->p->mainloop || !ctl->p->context) |
| 130 |
return; |
| 131 |
|
| 132 |
o = pa_context_get_sink_info_by_name(ctl->p->context, ctl->sink, |
| 133 |
sink_info_cb, ctl); |
| 134 |
|
| 135 |
if (o) |
| 136 |
pa_operation_unref(o); |
| 137 |
|
| 138 |
o = pa_context_get_source_info_by_name(ctl->p->context, |
| 139 |
ctl->source, source_info_cb, |
| 140 |
ctl); |
| 141 |
|
| 142 |
if (o) |
| 143 |
pa_operation_unref(o); |
| 144 |
} |
| 145 |
|
| 146 |
static int pulse_update_volume(snd_ctl_pulse_t * ctl) |
| 147 |
{ |
| 148 |
int err; |
| 149 |
pa_operation *o; |
| 150 |
|
| 151 |
assert(ctl); |
| 152 |
|
| 153 |
if (!ctl->p) |
| 154 |
return -EBADFD; |
| 155 |
|
| 156 |
err = pulse_check_connection(ctl->p); |
| 157 |
if (err < 0) |
| 158 |
return err; |
| 159 |
|
| 160 |
o = pa_context_get_sink_info_by_name(ctl->p->context, ctl->sink, |
| 161 |
sink_info_cb, ctl); |
| 162 |
if (o) { |
| 163 |
err = pulse_wait_operation(ctl->p, o); |
| 164 |
pa_operation_unref(o); |
| 165 |
} else |
| 166 |
err = -EIO; |
| 167 |
|
| 168 |
if (err < 0) |
| 169 |
return err; |
| 170 |
|
| 171 |
o = pa_context_get_source_info_by_name(ctl->p->context, |
| 172 |
ctl->source, source_info_cb, |
| 173 |
ctl); |
| 174 |
if (o) { |
| 175 |
err = pulse_wait_operation(ctl->p, o); |
| 176 |
pa_operation_unref(o); |
| 177 |
} else |
| 178 |
err = -EIO; |
| 179 |
|
| 180 |
if (err < 0) |
| 181 |
return err; |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
static int pulse_elem_count(snd_ctl_ext_t * ext) |
| 187 |
{ |
| 188 |
snd_ctl_pulse_t *ctl = ext->private_data; |
| 189 |
int count = 0, err; |
| 190 |
|
| 191 |
assert(ctl); |
| 192 |
|
| 193 |
if (!ctl->p || !ctl->p->mainloop) |
| 194 |
return -EBADFD; |
| 195 |
|
| 196 |
pa_threaded_mainloop_lock(ctl->p->mainloop); |
| 197 |
|
| 198 |
err = pulse_check_connection(ctl->p); |
| 199 |
if (err < 0) { |
| 200 |
count = err; |
| 201 |
goto finish; |
| 202 |
} |
| 203 |
|
| 204 |
if (ctl->source) |
| 205 |
count += 2; |
| 206 |
if (ctl->sink) |
| 207 |
count += 2; |
| 208 |
|
| 209 |
finish: |
| 210 |
pa_threaded_mainloop_unlock(ctl->p->mainloop); |
| 211 |
|
| 212 |
return count; |
| 213 |
} |
| 214 |
|
| 215 |
static int pulse_elem_list(snd_ctl_ext_t * ext, unsigned int offset, |
| 216 |
snd_ctl_elem_id_t * id) |
| 217 |
{ |
| 218 |
snd_ctl_pulse_t *ctl = ext->private_data; |
| 219 |
int err; |
| 220 |
|
| 221 |
assert(ctl); |
| 222 |
|
| 223 |
if (!ctl->p || !ctl->p->mainloop) |
| 224 |
return -EBADFD; |
| 225 |
|
| 226 |
snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER); |
| 227 |
|
| 228 |
pa_threaded_mainloop_lock(ctl->p->mainloop); |
| 229 |
|
| 230 |
err = pulse_check_connection(ctl->p); |
| 231 |
if (err < 0) |
| 232 |
goto finish; |
| 233 |
|
| 234 |
if (ctl->source) { |
| 235 |
if (offset == 0) |
| 236 |
snd_ctl_elem_id_set_name(id, SOURCE_VOL_NAME); |
| 237 |
else if (offset == 1) |
| 238 |
snd_ctl_elem_id_set_name(id, SOURCE_MUTE_NAME); |
| 239 |
} else |
| 240 |
offset += 2; |
| 241 |
|
| 242 |
err = 0; |
| 243 |
|
| 244 |
finish: |
| 245 |
pa_threaded_mainloop_unlock(ctl->p->mainloop); |
| 246 |
|
| 247 |
if (err >= 0) { |
| 248 |
if (offset == 2) |
| 249 |
snd_ctl_elem_id_set_name(id, SINK_VOL_NAME); |
| 250 |
else if (offset == 3) |
| 251 |
snd_ctl_elem_id_set_name(id, SINK_MUTE_NAME); |
| 252 |
} |
| 253 |
|
| 254 |
return err; |
| 255 |
} |
| 256 |
|
| 257 |
static snd_ctl_ext_key_t pulse_find_elem(snd_ctl_ext_t * ext, |
| 258 |
const snd_ctl_elem_id_t * id) |
| 259 |
{ |
| 260 |
const char *name; |
| 261 |
unsigned int numid; |
| 262 |
|
| 263 |
numid = snd_ctl_elem_id_get_numid(id); |
| 264 |
if (numid > 0 && numid <= 4) |
| 265 |
return numid - 1; |
| 266 |
|
| 267 |
name = snd_ctl_elem_id_get_name(id); |
| 268 |
|
| 269 |
if (strcmp(name, SOURCE_VOL_NAME) == 0) |
| 270 |
return 0; |
| 271 |
if (strcmp(name, SOURCE_MUTE_NAME) == 0) |
| 272 |
return 1; |
| 273 |
if (strcmp(name, SINK_VOL_NAME) == 0) |
| 274 |
return 2; |
| 275 |
if (strcmp(name, SINK_MUTE_NAME) == 0) |
| 276 |
return 3; |
| 277 |
|
| 278 |
return SND_CTL_EXT_KEY_NOT_FOUND; |
| 279 |
} |
| 280 |
|
| 281 |
static int pulse_get_attribute(snd_ctl_ext_t * ext, snd_ctl_ext_key_t key, |
| 282 |
int *type, unsigned int *acc, |
| 283 |
unsigned int *count) |
| 284 |
{ |
| 285 |
snd_ctl_pulse_t *ctl = ext->private_data; |
| 286 |
int err = 0; |
| 287 |
|
| 288 |
if (key > 3) |
| 289 |
return -EINVAL; |
| 290 |
|
| 291 |
assert(ctl); |
| 292 |
|
| 293 |
if (!ctl->p || !ctl->p->mainloop) |
| 294 |
return -EBADFD; |
| 295 |
|
| 296 |
pa_threaded_mainloop_lock(ctl->p->mainloop); |
| 297 |
|
| 298 |
err = pulse_check_connection(ctl->p); |
| 299 |
if (err < 0) |
| 300 |
goto finish; |
| 301 |
|
| 302 |
err = pulse_update_volume(ctl); |
| 303 |
if (err < 0) |
| 304 |
goto finish; |
| 305 |
|
| 306 |
if (key & 1) |
| 307 |
*type = SND_CTL_ELEM_TYPE_BOOLEAN; |
| 308 |
else |
| 309 |
*type = SND_CTL_ELEM_TYPE_INTEGER; |
| 310 |
|
| 311 |
*acc = SND_CTL_EXT_ACCESS_READWRITE; |
| 312 |
|
| 313 |
if (key == 0) |
| 314 |
*count = ctl->source_volume.channels; |
| 315 |
else if (key == 2) |
| 316 |
*count = ctl->sink_volume.channels; |
| 317 |
else |
| 318 |
*count = 1; |
| 319 |
|
| 320 |
finish: |
| 321 |
pa_threaded_mainloop_unlock(ctl->p->mainloop); |
| 322 |
|
| 323 |
return err; |
| 324 |
} |
| 325 |
|
| 326 |
static int pulse_get_integer_info(snd_ctl_ext_t * ext, |
| 327 |
snd_ctl_ext_key_t key, long *imin, |
| 328 |
long *imax, long *istep) |
| 329 |
{ |
| 330 |
*istep = 1; |
| 331 |
*imin = 0; |
| 332 |
*imax = PA_VOLUME_NORM; |
| 333 |
|
| 334 |
return 0; |
| 335 |
} |
| 336 |
|
| 337 |
static int pulse_read_integer(snd_ctl_ext_t * ext, snd_ctl_ext_key_t key, |
| 338 |
long *value) |
| 339 |
{ |
| 340 |
snd_ctl_pulse_t *ctl = ext->private_data; |
| 341 |
int err = 0, i; |
| 342 |
pa_cvolume *vol = NULL; |
| 343 |
|
| 344 |
assert(ctl); |
| 345 |
|
| 346 |
if (!ctl->p || !ctl->p->mainloop) |
| 347 |
return -EBADFD; |
| 348 |
|
| 349 |
pa_threaded_mainloop_lock(ctl->p->mainloop); |
| 350 |
|
| 351 |
err = pulse_check_connection(ctl->p); |
| 352 |
if (err < 0) |
| 353 |
goto finish; |
| 354 |
|
| 355 |
err = pulse_update_volume(ctl); |
| 356 |
if (err < 0) |
| 357 |
goto finish; |
| 358 |
|
| 359 |
switch (key) { |
| 360 |
case 0: |
| 361 |
vol = &ctl->source_volume; |
| 362 |
break; |
| 363 |
case 1: |
| 364 |
*value = !ctl->source_muted; |
| 365 |
break; |
| 366 |
case 2: |
| 367 |
vol = &ctl->sink_volume; |
| 368 |
break; |
| 369 |
case 3: |
| 370 |
*value = !ctl->sink_muted; |
| 371 |
break; |
| 372 |
default: |
| 373 |
err = -EINVAL; |
| 374 |
goto finish; |
| 375 |
} |
| 376 |
|
| 377 |
if (vol) { |
| 378 |
for (i = 0; i < vol->channels; i++) |
| 379 |
value[i] = vol->values[i]; |
| 380 |
} |
| 381 |
|
| 382 |
finish: |
| 383 |
pa_threaded_mainloop_unlock(ctl->p->mainloop); |
| 384 |
|
| 385 |
return err; |
| 386 |
} |
| 387 |
|
| 388 |
static int pulse_write_integer(snd_ctl_ext_t * ext, snd_ctl_ext_key_t key, |
| 389 |
long *value) |
| 390 |
{ |
| 391 |
snd_ctl_pulse_t *ctl = ext->private_data; |
| 392 |
int err = 0, i; |
| 393 |
pa_operation *o; |
| 394 |
pa_cvolume *vol = NULL; |
| 395 |
|
| 396 |
assert(ctl); |
| 397 |
|
| 398 |
if (!ctl->p || !ctl->p->mainloop) |
| 399 |
return -EBADFD; |
| 400 |
|
| 401 |
pa_threaded_mainloop_lock(ctl->p->mainloop); |
| 402 |
|
| 403 |
err = pulse_check_connection(ctl->p); |
| 404 |
if (err < 0) |
| 405 |
goto finish; |
| 406 |
|
| 407 |
err = pulse_update_volume(ctl); |
| 408 |
if (err < 0) |
| 409 |
goto finish; |
| 410 |
|
| 411 |
switch (key) { |
| 412 |
case 0: |
| 413 |
vol = &ctl->source_volume; |
| 414 |
break; |
| 415 |
case 1: |
| 416 |
if (!!ctl->source_muted == !*value) |
| 417 |
goto finish; |
| 418 |
ctl->source_muted = !*value; |
| 419 |
break; |
| 420 |
case 2: |
| 421 |
vol = &ctl->sink_volume; |
| 422 |
break; |
| 423 |
case 3: |
| 424 |
if (!!ctl->sink_muted == !*value) |
| 425 |
goto finish; |
| 426 |
ctl->sink_muted = !*value; |
| 427 |
break; |
| 428 |
default: |
| 429 |
err = -EINVAL; |
| 430 |
goto finish; |
| 431 |
} |
| 432 |
|
| 433 |
if (vol) { |
| 434 |
for (i = 0; i < vol->channels; i++) |
| 435 |
if (value[i] != vol->values[i]) |
| 436 |
break; |
| 437 |
|
| 438 |
if (i == vol->channels) |
| 439 |
goto finish; |
| 440 |
|
| 441 |
for (i = 0; i < vol->channels; i++) |
| 442 |
vol->values[i] = value[i]; |
| 443 |
|
| 444 |
if (key == 0) |
| 445 |
o = pa_context_set_source_volume_by_name(ctl->p-> |
| 446 |
context, |
| 447 |
ctl-> |
| 448 |
source, |
| 449 |
vol, |
| 450 |
pulse_context_success_cb, |
| 451 |
ctl->p); |
| 452 |
else |
| 453 |
o = pa_context_set_sink_volume_by_name(ctl->p-> |
| 454 |
context, |
| 455 |
ctl->sink, |
| 456 |
vol, |
| 457 |
pulse_context_success_cb, |
| 458 |
ctl->p); |
| 459 |
} else { |
| 460 |
if (key == 1) |
| 461 |
o = pa_context_set_source_mute_by_name(ctl->p-> |
| 462 |
context, |
| 463 |
ctl->source, |
| 464 |
ctl-> |
| 465 |
source_muted, |
| 466 |
pulse_context_success_cb, |
| 467 |
ctl->p); |
| 468 |
else |
| 469 |
o = pa_context_set_sink_mute_by_name(ctl->p-> |
| 470 |
context, |
| 471 |
ctl->sink, |
| 472 |
ctl-> |
| 473 |
sink_muted, |
| 474 |
pulse_context_success_cb, |
| 475 |
ctl->p); |
| 476 |
} |
| 477 |
|
| 478 |
if (!o) { |
| 479 |
err = -EIO; |
| 480 |
goto finish; |
| 481 |
} |
| 482 |
|
| 483 |
err = pulse_wait_operation(ctl->p, o); |
| 484 |
pa_operation_unref(o); |
| 485 |
|
| 486 |
if (err < 0) |
| 487 |
goto finish; |
| 488 |
|
| 489 |
err = 1; |
| 490 |
|
| 491 |
finish: |
| 492 |
pa_threaded_mainloop_unlock(ctl->p->mainloop); |
| 493 |
|
| 494 |
return err; |
| 495 |
} |
| 496 |
|
| 497 |
static void pulse_subscribe_events(snd_ctl_ext_t * ext, int subscribe) |
| 498 |
{ |
| 499 |
snd_ctl_pulse_t *ctl = ext->private_data; |
| 500 |
|
| 501 |
assert(ctl); |
| 502 |
|
| 503 |
if (!ctl->p || !ctl->p->mainloop) |
| 504 |
return; |
| 505 |
|
| 506 |
pa_threaded_mainloop_lock(ctl->p->mainloop); |
| 507 |
|
| 508 |
ctl->subscribed = !!(subscribe & SND_CTL_EVENT_MASK_VALUE); |
| 509 |
|
| 510 |
pa_threaded_mainloop_unlock(ctl->p->mainloop); |
| 511 |
} |
| 512 |
|
| 513 |
static int pulse_read_event(snd_ctl_ext_t * ext, snd_ctl_elem_id_t * id, |
| 514 |
unsigned int *event_mask) |
| 515 |
{ |
| 516 |
snd_ctl_pulse_t *ctl = ext->private_data; |
| 517 |
int offset; |
| 518 |
int err; |
| 519 |
|
| 520 |
assert(ctl); |
| 521 |
|
| 522 |
if (!ctl->p || !ctl->p->mainloop) |
| 523 |
return -EBADFD; |
| 524 |
|
| 525 |
pa_threaded_mainloop_lock(ctl->p->mainloop); |
| 526 |
|
| 527 |
err = pulse_check_connection(ctl->p); |
| 528 |
if (err < 0) |
| 529 |
goto finish; |
| 530 |
|
| 531 |
if (!ctl->updated || !ctl->subscribed) { |
| 532 |
err = -EAGAIN; |
| 533 |
goto finish; |
| 534 |
} |
| 535 |
|
| 536 |
if (ctl->source) |
| 537 |
offset = 2; |
| 538 |
else |
| 539 |
offset = 0; |
| 540 |
|
| 541 |
if (ctl->updated & UPDATE_SOURCE_VOL) { |
| 542 |
pulse_elem_list(ext, 0, id); |
| 543 |
ctl->updated &= ~UPDATE_SOURCE_VOL; |
| 544 |
} else if (ctl->updated & UPDATE_SOURCE_MUTE) { |
| 545 |
pulse_elem_list(ext, 1, id); |
| 546 |
ctl->updated &= ~UPDATE_SOURCE_MUTE; |
| 547 |
} else if (ctl->updated & UPDATE_SINK_VOL) { |
| 548 |
pulse_elem_list(ext, offset + 0, id); |
| 549 |
ctl->updated &= ~UPDATE_SINK_VOL; |
| 550 |
} else if (ctl->updated & UPDATE_SINK_MUTE) { |
| 551 |
pulse_elem_list(ext, offset + 1, id); |
| 552 |
ctl->updated &= ~UPDATE_SINK_MUTE; |
| 553 |
} |
| 554 |
|
| 555 |
*event_mask = SND_CTL_EVENT_MASK_VALUE; |
| 556 |
|
| 557 |
if (!ctl->updated) |
| 558 |
pulse_poll_deactivate(ctl->p); |
| 559 |
|
| 560 |
err = 1; |
| 561 |
|
| 562 |
finish: |
| 563 |
pa_threaded_mainloop_unlock(ctl->p->mainloop); |
| 564 |
|
| 565 |
return err; |
| 566 |
} |
| 567 |
|
| 568 |
static int pulse_ctl_poll_revents(snd_ctl_ext_t * ext, struct pollfd *pfd, |
| 569 |
unsigned int nfds, |
| 570 |
unsigned short *revents) |
| 571 |
{ |
| 572 |
snd_ctl_pulse_t *ctl = ext->private_data; |
| 573 |
int err; |
| 574 |
|
| 575 |
assert(ctl); |
| 576 |
|
| 577 |
if (!ctl->p || !ctl->p->mainloop) |
| 578 |
return -EBADFD; |
| 579 |
|
| 580 |
pa_threaded_mainloop_lock(ctl->p->mainloop); |
| 581 |
|
| 582 |
err = pulse_check_connection(ctl->p); |
| 583 |
if (err < 0) |
| 584 |
goto finish; |
| 585 |
|
| 586 |
if (ctl->updated) |
| 587 |
*revents = POLLIN; |
| 588 |
else |
| 589 |
*revents = 0; |
| 590 |
|
| 591 |
err = 0; |
| 592 |
|
| 593 |
finish: |
| 594 |
|
| 595 |
pa_threaded_mainloop_unlock(ctl->p->mainloop); |
| 596 |
|
| 597 |
return err; |
| 598 |
} |
| 599 |
|
| 600 |
static void pulse_close(snd_ctl_ext_t * ext) |
| 601 |
{ |
| 602 |
snd_ctl_pulse_t *ctl = ext->private_data; |
| 603 |
|
| 604 |
assert(ctl); |
| 605 |
|
| 606 |
if (ctl->p) |
| 607 |
pulse_free(ctl->p); |
| 608 |
|
| 609 |
free(ctl->source); |
| 610 |
free(ctl->sink); |
| 611 |
free(ctl); |
| 612 |
} |
| 613 |
|
| 614 |
static const snd_ctl_ext_callback_t pulse_ext_callback = { |
| 615 |
.elem_count = pulse_elem_count, |
| 616 |
.elem_list = pulse_elem_list, |
| 617 |
.find_elem = pulse_find_elem, |
| 618 |
.get_attribute = pulse_get_attribute, |
| 619 |
.get_integer_info = pulse_get_integer_info, |
| 620 |
.read_integer = pulse_read_integer, |
| 621 |
.write_integer = pulse_write_integer, |
| 622 |
.subscribe_events = pulse_subscribe_events, |
| 623 |
.read_event = pulse_read_event, |
| 624 |
.poll_revents = pulse_ctl_poll_revents, |
| 625 |
.close = pulse_close, |
| 626 |
}; |
| 627 |
|
| 628 |
static void server_info_cb(pa_context * c, const pa_server_info * i, |
| 629 |
void *userdata) |
| 630 |
{ |
| 631 |
snd_ctl_pulse_t *ctl = (snd_ctl_pulse_t *) userdata; |
| 632 |
|
| 633 |
assert(ctl && i); |
| 634 |
|
| 635 |
if (i->default_source_name && !ctl->source) |
| 636 |
ctl->source = strdup(i->default_source_name); |
| 637 |
if (i->default_sink_name && !ctl->sink) |
| 638 |
ctl->sink = strdup(i->default_sink_name); |
| 639 |
|
| 640 |
pa_threaded_mainloop_signal(ctl->p->mainloop, 0); |
| 641 |
} |
| 642 |
|
| 643 |
SND_CTL_PLUGIN_DEFINE_FUNC(pulse) |
| 644 |
{ |
| 645 |
snd_config_iterator_t i, next; |
| 646 |
const char *server = NULL; |
| 647 |
const char *device = NULL; |
| 648 |
const char *source = NULL; |
| 649 |
const char *sink = NULL; |
| 650 |
const char *fallback_name = NULL; |
| 651 |
int err; |
| 652 |
snd_ctl_pulse_t *ctl; |
| 653 |
pa_operation *o; |
| 654 |
|
| 655 |
snd_config_for_each(i, next, conf) { |
| 656 |
snd_config_t *n = snd_config_iterator_entry(i); |
| 657 |
const char *id; |
| 658 |
if (snd_config_get_id(n, &id) < 0) |
| 659 |
continue; |
| 660 |
if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 |
| 661 |
|| strcmp(id, "hint") == 0) |
| 662 |
continue; |
| 663 |
if (strcmp(id, "server") == 0) { |
| 664 |
if (snd_config_get_string(n, &server) < 0) { |
| 665 |
SNDERR("Invalid type for %s", id); |
| 666 |
return -EINVAL; |
| 667 |
} else if (!*server) { |
| 668 |
server = NULL; |
| 669 |
} |
| 670 |
continue; |
| 671 |
} |
| 672 |
if (strcmp(id, "device") == 0) { |
| 673 |
if (snd_config_get_string(n, &device) < 0) { |
| 674 |
SNDERR("Invalid type for %s", id); |
| 675 |
return -EINVAL; |
| 676 |
} else if (!*device) { |
| 677 |
device = NULL; |
| 678 |
} |
| 679 |
continue; |
| 680 |
} |
| 681 |
if (strcmp(id, "source") == 0) { |
| 682 |
if (snd_config_get_string(n, &source) < 0) { |
| 683 |
SNDERR("Invalid type for %s", id); |
| 684 |
return -EINVAL; |
| 685 |
} else if (!*source) { |
| 686 |
source = NULL; |
| 687 |
} |
| 688 |
continue; |
| 689 |
} |
| 690 |
if (strcmp(id, "sink") == 0) { |
| 691 |
if (snd_config_get_string(n, &sink) < 0) { |
| 692 |
SNDERR("Invalid type for %s", id); |
| 693 |
return -EINVAL; |
| 694 |
} else if (!*sink) { |
| 695 |
sink = NULL; |
| 696 |
} |
| 697 |
continue; |
| 698 |
} |
| 699 |
if (strcmp(id, "fallback") == 0) { |
| 700 |
if (snd_config_get_string(n, &fallback_name) < 0) { |
| 701 |
SNDERR("Invalid value for %s", id); |
| 702 |
return -EINVAL; |
| 703 |
} |
| 704 |
continue; |
| 705 |
} |
| 706 |
SNDERR("Unknown field %s", id); |
| 707 |
return -EINVAL; |
| 708 |
} |
| 709 |
|
| 710 |
if (fallback_name && name && !strcmp(name, fallback_name)) |
| 711 |
fallback_name = NULL; |
| 712 |
|
| 713 |
ctl = calloc(1, sizeof(*ctl)); |
| 714 |
if (!ctl) |
| 715 |
return -ENOMEM; |
| 716 |
|
| 717 |
ctl->p = pulse_new(); |
| 718 |
if (!ctl->p) { |
| 719 |
err = -EIO; |
| 720 |
goto error; |
| 721 |
} |
| 722 |
|
| 723 |
err = pulse_connect(ctl->p, server, fallback_name != NULL); |
| 724 |
if (err < 0) |
| 725 |
goto error; |
| 726 |
|
| 727 |
if (source) |
| 728 |
ctl->source = strdup(source); |
| 729 |
else if (device) |
| 730 |
ctl->source = strdup(device); |
| 731 |
|
| 732 |
if ((source || device) && !ctl->source) { |
| 733 |
err = -ENOMEM; |
| 734 |
goto error; |
| 735 |
} |
| 736 |
|
| 737 |
if (sink) |
| 738 |
ctl->sink = strdup(sink); |
| 739 |
else if (device) |
| 740 |
ctl->sink = strdup(device); |
| 741 |
|
| 742 |
if ((sink || device) && !ctl->sink) { |
| 743 |
err = -ENOMEM; |
| 744 |
goto error; |
| 745 |
} |
| 746 |
|
| 747 |
if (!ctl->source || !ctl->sink) { |
| 748 |
pa_threaded_mainloop_lock(ctl->p->mainloop); |
| 749 |
|
| 750 |
o = pa_context_get_server_info(ctl->p->context, |
| 751 |
server_info_cb, ctl); |
| 752 |
|
| 753 |
if (o) { |
| 754 |
err = pulse_wait_operation(ctl->p, o); |
| 755 |
pa_operation_unref(o); |
| 756 |
} else |
| 757 |
err = -EIO; |
| 758 |
|
| 759 |
pa_threaded_mainloop_unlock(ctl->p->mainloop); |
| 760 |
|
| 761 |
if (err < 0) |
| 762 |
goto error; |
| 763 |
} |
| 764 |
|
| 765 |
pa_threaded_mainloop_lock(ctl->p->mainloop); |
| 766 |
|
| 767 |
pa_context_set_subscribe_callback(ctl->p->context, event_cb, ctl); |
| 768 |
|
| 769 |
o = pa_context_subscribe(ctl->p->context, |
| 770 |
PA_SUBSCRIPTION_MASK_SINK | |
| 771 |
PA_SUBSCRIPTION_MASK_SOURCE, |
| 772 |
pulse_context_success_cb, ctl->p); |
| 773 |
|
| 774 |
if (o) { |
| 775 |
err = pulse_wait_operation(ctl->p, o); |
| 776 |
pa_operation_unref(o); |
| 777 |
} else |
| 778 |
err = -EIO; |
| 779 |
|
| 780 |
pa_threaded_mainloop_unlock(ctl->p->mainloop); |
| 781 |
|
| 782 |
if (err < 0) |
| 783 |
goto error; |
| 784 |
|
| 785 |
ctl->ext.version = SND_CTL_EXT_VERSION; |
| 786 |
ctl->ext.card_idx = 0; |
| 787 |
strncpy(ctl->ext.id, "pulse", sizeof(ctl->ext.id) - 1); |
| 788 |
strncpy(ctl->ext.driver, "PulseAudio plugin", |
| 789 |
sizeof(ctl->ext.driver) - 1); |
| 790 |
strncpy(ctl->ext.name, "PulseAudio", sizeof(ctl->ext.name) - 1); |
| 791 |
strncpy(ctl->ext.longname, "PulseAudio", |
| 792 |
sizeof(ctl->ext.longname) - 1); |
| 793 |
strncpy(ctl->ext.mixername, "PulseAudio", |
| 794 |
sizeof(ctl->ext.mixername) - 1); |
| 795 |
ctl->ext.poll_fd = ctl->p->main_fd; |
| 796 |
|
| 797 |
ctl->ext.callback = &pulse_ext_callback; |
| 798 |
ctl->ext.private_data = ctl; |
| 799 |
|
| 800 |
err = snd_ctl_ext_create(&ctl->ext, name, mode); |
| 801 |
if (err < 0) |
| 802 |
goto error; |
| 803 |
|
| 804 |
*handlep = ctl->ext.handle; |
| 805 |
|
| 806 |
return 0; |
| 807 |
|
| 808 |
error: |
| 809 |
if (ctl->p) |
| 810 |
pulse_free(ctl->p); |
| 811 |
|
| 812 |
free(ctl->source); |
| 813 |
free(ctl->sink); |
| 814 |
free(ctl); |
| 815 |
|
| 816 |
if (fallback_name) |
| 817 |
return snd_ctl_open_fallback(handlep, root, |
| 818 |
fallback_name, name, mode); |
| 819 |
|
| 820 |
return err; |
| 821 |
} |
| 822 |
|
| 823 |
SND_CTL_PLUGIN_SYMBOL(pulse); |