-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathbluealsa-ctl.c
1207 lines (976 loc) · 34.1 KB
/
bluealsa-ctl.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* bluealsa-ctl.c
* Copyright (c) 2016-2021 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
* This project is licensed under the terms of the MIT license.
*
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <unistd.h>
#include <alsa/asoundlib.h>
#include <alsa/control_external.h>
#include <bluetooth/bluetooth.h>
#include <dbus/dbus.h>
#include "shared/dbus-client.h"
#include "shared/defs.h"
enum ctl_elem_type {
CTL_ELEM_TYPE_BATTERY,
CTL_ELEM_TYPE_SWITCH,
CTL_ELEM_TYPE_VOLUME,
};
struct ctl_elem {
enum ctl_elem_type type;
struct bt_dev *dev;
struct ba_pcm *pcm;
char name[44 /* internal ALSA constraint */ + 1];
/* if true, element is a playback control */
bool playback;
};
struct ctl_elem_update {
char name[sizeof(((struct ctl_elem *)0)->name)];
int event_mask;
};
struct bt_dev {
char device_path[sizeof(((struct ba_pcm *)0)->device_path)];
char rfcomm_path[sizeof(((struct ba_pcm *)0)->device_path)];
char name[sizeof(((struct ctl_elem *)0)->name)];
int battery_level;
int mask;
};
struct bluealsa_ctl {
snd_ctl_ext_t ext;
/* D-Bus connection context */
struct ba_dbus_ctx dbus_ctx;
/* list of BT devices */
struct bt_dev **dev_list;
size_t dev_list_size;
/* list of all BlueALSA PCMs */
struct ba_pcm *pcm_list;
size_t pcm_list_size;
/* list of ALSA control elements */
struct ctl_elem *elem_list;
size_t elem_list_size;
/* list of control element update events */
struct ctl_elem_update *elem_update_list;
size_t elem_update_list_size;
size_t elem_update_event_i;
/* Disconnection event pipe.
* This allows us to generate a POLLERR event by closing the read end
* then polling the write end. No actual reads or writes are performed
* on this pipe, so no risk of SIGPIPE.
* Many applications (including alsamixer) interpret POLLERR as
* indicating the mixer device has been disconnected. */
int pipefd[2];
/* if true, show battery meter */
bool show_battery;
/* if true, this mixer is for a single Bluetooth device */
bool single_device;
};
static int str2bdaddr(const char *str, bdaddr_t *ba) {
unsigned int x[6];
if (sscanf(str, "%x:%x:%x:%x:%x:%x",
&x[5], &x[4], &x[3], &x[2], &x[1], &x[0]) != 6)
return -1;
size_t i;
for (i = 0; i < 6; i++)
ba->b[i] = x[i];
return 0;
}
static int bluealsa_bt_dev_cmp(const void *p1, const void *p2) {
const struct bt_dev *d1 = *(const struct bt_dev **)p1;
const struct bt_dev *d2 = *(const struct bt_dev **)p2;
return strcmp(d1->device_path, d2->device_path);
}
static int bluealsa_elem_cmp(const void *p1, const void *p2) {
const struct ctl_elem *e1 = (const struct ctl_elem *)p1;
const struct ctl_elem *e2 = (const struct ctl_elem *)p2;
int rv;
if ((rv = strcmp(e1->name, e2->name)) == 0)
rv = bacmp(&e1->pcm->addr, &e2->pcm->addr);
return rv;
}
static DBusMessage *bluealsa_dbus_get_property(DBusConnection *conn,
const char *service, const char *path, const char *interface,
const char *property, DBusError *error) {
DBusMessage *msg;
if ((msg = dbus_message_new_method_call(service, path,
DBUS_INTERFACE_PROPERTIES, "Get")) == NULL)
return NULL;
DBusMessage *rep = NULL;
if (!dbus_message_append_args(msg,
DBUS_TYPE_STRING, &interface,
DBUS_TYPE_STRING, &property,
DBUS_TYPE_INVALID))
goto fail;
rep = dbus_connection_send_with_reply_and_block(conn, msg,
DBUS_TIMEOUT_USE_DEFAULT, error);
fail:
dbus_message_unref(msg);
return rep;
}
/**
* Get BT device ID number.
*
* @param ctl The BlueALSA controller context.
* @param pcm BlueALSA PCM structure.
* @return The device ID number, or -1 upon error. */
static int bluealsa_dev_get_id(struct bluealsa_ctl *ctl, const struct ba_pcm *pcm) {
size_t i;
for (i = 0; i < ctl->dev_list_size; i++)
if (strcmp(ctl->dev_list[i]->device_path, pcm->device_path) == 0)
return i + 1;
return -1;
}
static int bluealsa_dev_fetch_name(struct bluealsa_ctl *ctl, struct bt_dev *dev) {
DBusMessage *rep;
DBusError err = DBUS_ERROR_INIT;
if ((rep = bluealsa_dbus_get_property(ctl->dbus_ctx.conn, "org.bluez",
dev->device_path, "org.bluez.Device1", "Alias", &err)) == NULL) {
SNDERR("Couldn't get device name: %s", err.message);
dbus_error_free(&err);
return -1;
}
DBusMessageIter iter;
DBusMessageIter iter_val;
dbus_message_iter_init(rep, &iter);
dbus_message_iter_recurse(&iter, &iter_val);
const char *name;
dbus_message_iter_get_basic(&iter_val, &name);
*stpncpy(dev->name, name, sizeof(dev->name) - 1) = '\0';
dbus_message_unref(rep);
return 0;
}
static int bluealsa_dev_fetch_battery(struct bluealsa_ctl *ctl, struct bt_dev *dev) {
DBusMessage *rep;
if ((rep = bluealsa_dbus_get_property(ctl->dbus_ctx.conn, ctl->dbus_ctx.ba_service,
dev->rfcomm_path, BLUEALSA_INTERFACE_RFCOMM, "Battery", NULL)) == NULL)
return -1;
DBusMessageIter iter;
DBusMessageIter iter_val;
dbus_message_iter_init(rep, &iter);
dbus_message_iter_recurse(&iter, &iter_val);
char battery;
dbus_message_iter_get_basic(&iter_val, &battery);
dev->battery_level = battery;
dbus_message_unref(rep);
return battery;
}
/**
* Get BT device structure.
*
* @param ctl The BlueALSA controller context.
* @param pcm BlueALSA PCM structure.
* @return The BT device, or NULL upon error. */
static struct bt_dev *bluealsa_dev_get(struct bluealsa_ctl *ctl, const struct ba_pcm *pcm) {
size_t i;
for (i = 0; i < ctl->dev_list_size; i++)
if (strcmp(ctl->dev_list[i]->device_path, pcm->device_path) == 0)
return ctl->dev_list[i];
/* If device is not cached yet, fetch data from
* the BlueZ via the B-Bus interface. */
struct bt_dev **dev_list = ctl->dev_list;
size_t size = ctl->dev_list_size;
if ((dev_list = realloc(dev_list, (size + 1) * sizeof(*dev_list))) == NULL)
return NULL;
ctl->dev_list = dev_list;
struct bt_dev *dev;
if ((dev_list[size] = dev = malloc(sizeof(*dev))) == NULL)
return NULL;
ctl->dev_list_size++;
strcpy(dev->device_path, pcm->device_path);
sprintf(dev->rfcomm_path, "/org/bluealsa%.64s/rfcomm", &dev->device_path[10]);
sprintf(dev->name, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
pcm->addr.b[5], pcm->addr.b[4], pcm->addr.b[3],
pcm->addr.b[2], pcm->addr.b[1], pcm->addr.b[0]);
dev->battery_level = -1;
/* Sort device list by an object path, so the bluealsa_dev_get_id() will
* return consistent IDs ordering in case of name duplications. */
qsort(dev_list, ctl->dev_list_size, sizeof(*dev_list), bluealsa_bt_dev_cmp);
bluealsa_dev_fetch_name(ctl, dev);
return dev;
}
static int bluealsa_pcm_add(struct bluealsa_ctl *ctl, const struct ba_pcm *pcm) {
struct ba_pcm *tmp = ctl->pcm_list;
if ((tmp = realloc(tmp, (ctl->pcm_list_size + 1) * sizeof(*tmp))) == NULL)
return -1;
memcpy(&tmp[ctl->pcm_list_size++], pcm, sizeof(*tmp));
ctl->pcm_list = tmp;
return 0;
}
static int bluealsa_pcm_remove(struct bluealsa_ctl *ctl, const char *path) {
size_t i;
for (i = 0; i < ctl->pcm_list_size; i++)
if (strcmp(ctl->pcm_list[i].pcm_path, path) == 0)
memcpy(&ctl->pcm_list[i], &ctl->pcm_list[--ctl->pcm_list_size], sizeof(*ctl->pcm_list));
return 0;
}
/**
* Update element name based on given string and PCM type.
*
* @param elem An address to the element structure.
* @param name A string which should be used as a base for the element name. May
* be NULL if no base prefix is required.
* @param id An unique ID number. If the ID is other than -1, it will be
* attached to the element name in order to prevent duplications. */
static void bluealsa_elem_set_name(struct ctl_elem *elem, const char *name, int id) {
if (name != NULL) {
/* multi-device mixer - include device alias in control names */
const int name_len = strlen(name);
int len = sizeof(elem->name) - 16 - 1;
char no[16] = "";
if (id != -1) {
sprintf(no, " #%u", id);
len -= strlen(no);
}
if (elem->type == CTL_ELEM_TYPE_BATTERY) {
len = MIN(len - 10, name_len);
while (isspace(name[len - 1]))
len--;
sprintf(elem->name, "%.*s%s | Battery", len, name, no);
}
else {
/* avoid name duplication by adding profile suffixes */
switch (elem->pcm->transport) {
case BA_PCM_TRANSPORT_A2DP_SOURCE:
case BA_PCM_TRANSPORT_A2DP_SINK:
len = MIN(len - 7, name_len);
while (isspace(name[len - 1]))
len--;
sprintf(elem->name, "%.*s%s - A2DP", len, name, no);
break;
case BA_PCM_TRANSPORT_HFP_AG:
case BA_PCM_TRANSPORT_HFP_HF:
case BA_PCM_TRANSPORT_HSP_AG:
case BA_PCM_TRANSPORT_HSP_HS:
len = MIN(len - 6, name_len);
while (isspace(name[len - 1]))
len--;
sprintf(elem->name, "%.*s%s - SCO", len, name, no);
break;
}
}
}
else {
/* single-device mixer - constant control names */
if (elem->type == CTL_ELEM_TYPE_BATTERY)
strcpy(elem->name, "Battery");
else
switch (elem->pcm->transport) {
case BA_PCM_TRANSPORT_A2DP_SOURCE:
case BA_PCM_TRANSPORT_A2DP_SINK:
strcpy(elem->name, "A2DP");
break;
case BA_PCM_TRANSPORT_HFP_AG:
case BA_PCM_TRANSPORT_HFP_HF:
case BA_PCM_TRANSPORT_HSP_AG:
case BA_PCM_TRANSPORT_HSP_HS:
strcpy(elem->name, "SCO");
break;
}
}
/* ALSA library determines the element type by checking it's
* name suffix. This feature is not well documented, though. */
strcat(elem->name, elem->playback ? " Playback" : " Capture");
switch (elem->type) {
case CTL_ELEM_TYPE_SWITCH:
strcat(elem->name, " Switch");
break;
case CTL_ELEM_TYPE_BATTERY:
case CTL_ELEM_TYPE_VOLUME:
strcat(elem->name, " Volume");
break;
}
}
static int bluealsa_create_elem_list(struct bluealsa_ctl *ctl) {
size_t count = 0;
size_t i;
struct ctl_elem *elem_list = ctl->elem_list;
if (ctl->pcm_list_size > 0) {
for (i = 0; i < ctl->pcm_list_size; i++) {
/* Every stream has two controls associated to
* itself - volume adjustment and mute switch. */
count += 2;
/* It is possible, that BT device battery level will be exposed via
* RFCOMM interface, so in order to account for a special "battery"
* element we have to increment our element counter by one. */
count += 1;
}
if ((elem_list = realloc(elem_list, count * sizeof(*elem_list))) == NULL)
return -1;
}
/* Clear device mask, so we can distinguish currently used and unused (old)
* device entries - we are not invalidating device list after PCM remove. */
for (i = 0; i < ctl->dev_list_size; i++)
ctl->dev_list[i]->mask = 0;
count = 0;
/* Construct control elements based on available PCMs. */
for (i = 0; i < ctl->pcm_list_size; i++) {
struct ba_pcm *pcm = &ctl->pcm_list[i];
struct bt_dev *dev = bluealsa_dev_get(ctl, pcm);
const char *name = ctl->single_device ? NULL : dev->name;
elem_list[count].type = CTL_ELEM_TYPE_VOLUME;
elem_list[count].dev = dev;
elem_list[count].pcm = pcm;
elem_list[count].playback = pcm->mode == BA_PCM_MODE_SINK;
bluealsa_elem_set_name(&elem_list[count], name, -1);
count++;
elem_list[count].type = CTL_ELEM_TYPE_SWITCH;
elem_list[count].dev = dev;
elem_list[count].pcm = pcm;
elem_list[count].playback = pcm->mode == BA_PCM_MODE_SINK;
bluealsa_elem_set_name(&elem_list[count], name, -1);
count++;
/* Try to add special "battery" element. */
if (ctl->show_battery && dev->battery_level == -1 &&
bluealsa_dev_fetch_battery(ctl, dev) != -1) {
elem_list[count].type = CTL_ELEM_TYPE_BATTERY;
elem_list[count].dev = dev;
elem_list[count].pcm = pcm;
elem_list[count].playback = true;
bluealsa_elem_set_name(&elem_list[count], name, -1);
count++;
}
}
/* Sort control elements alphabetically. */
qsort(elem_list, count, sizeof(*elem_list), bluealsa_elem_cmp);
/* Detect element name duplicates and annotate them with the
* consecutive device ID number - make ALSA library happy. */
if (!ctl->single_device)
for (i = 0; i < count; i++) {
char tmp[sizeof(elem_list[0].name)];
bool duplicated = false;
size_t ii;
for (ii = i + 1; ii < count; ii++)
if (strcmp(elem_list[i].name, elem_list[ii].name) == 0) {
bluealsa_elem_set_name(&elem_list[ii], strcpy(tmp, elem_list[ii].dev->name),
bluealsa_dev_get_id(ctl, elem_list[ii].pcm));
duplicated = true;
}
if (duplicated)
bluealsa_elem_set_name(&elem_list[i], strcpy(tmp, elem_list[i].dev->name),
bluealsa_dev_get_id(ctl, elem_list[i].pcm));
}
ctl->elem_list = elem_list;
ctl->elem_list_size = count;
return count;
}
static void bluealsa_close(snd_ctl_ext_t *ext) {
struct bluealsa_ctl *ctl = (struct bluealsa_ctl *)ext->private_data;
size_t i;
bluealsa_dbus_connection_ctx_free(&ctl->dbus_ctx);
if (ctl->pipefd[0] != -1)
close(ctl->pipefd[0]);
if (ctl->pipefd[1] != -1)
close(ctl->pipefd[1]);
for (i = 0; i < ctl->dev_list_size; i++)
free(ctl->dev_list[i]);
free(ctl->dev_list);
free(ctl->pcm_list);
free(ctl->elem_list);
free(ctl->elem_update_list);
free(ctl);
}
static int bluealsa_elem_count(snd_ctl_ext_t *ext) {
struct bluealsa_ctl *ctl = (struct bluealsa_ctl *)ext->private_data;
return ctl->elem_list_size;
}
static int bluealsa_elem_list(snd_ctl_ext_t *ext, unsigned int offset, snd_ctl_elem_id_t *id) {
struct bluealsa_ctl *ctl = (struct bluealsa_ctl *)ext->private_data;
if (offset > ctl->elem_list_size)
return -EINVAL;
snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
snd_ctl_elem_id_set_name(id, ctl->elem_list[offset].name);
return 0;
}
static snd_ctl_ext_key_t bluealsa_find_elem(snd_ctl_ext_t *ext, const snd_ctl_elem_id_t *id) {
struct bluealsa_ctl *ctl = (struct bluealsa_ctl *)ext->private_data;
unsigned int numid = snd_ctl_elem_id_get_numid(id);
if (numid > 0 && numid <= ctl->elem_list_size)
return numid - 1;
const char *name = snd_ctl_elem_id_get_name(id);
size_t i;
for (i = 0; i < ctl->elem_list_size; i++)
if (strcmp(ctl->elem_list[i].name, name) == 0)
return i;
return SND_CTL_EXT_KEY_NOT_FOUND;
}
static int bluealsa_get_attribute(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key,
int *type, unsigned int *acc, unsigned int *count) {
struct bluealsa_ctl *ctl = (struct bluealsa_ctl *)ext->private_data;
if (key > ctl->elem_list_size)
return -EINVAL;
const struct ctl_elem *elem = &ctl->elem_list[key];
const struct ba_pcm *pcm = elem->pcm;
switch (elem->type) {
case CTL_ELEM_TYPE_BATTERY:
*acc = SND_CTL_EXT_ACCESS_READ;
*type = SND_CTL_ELEM_TYPE_INTEGER;
*count = 1;
break;
case CTL_ELEM_TYPE_SWITCH:
*acc = SND_CTL_EXT_ACCESS_READWRITE;
*type = SND_CTL_ELEM_TYPE_BOOLEAN;
*count = pcm->channels;
break;
case CTL_ELEM_TYPE_VOLUME:
*acc = SND_CTL_EXT_ACCESS_READWRITE |
SND_CTL_EXT_ACCESS_TLV_CALLBACK |
SND_CTL_EXT_ACCESS_TLV_READ;
*type = SND_CTL_ELEM_TYPE_INTEGER;
*count = pcm->channels;
break;
}
return 0;
}
static int bluealsa_get_integer_info(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key,
long *imin, long *imax, long *istep) {
struct bluealsa_ctl *ctl = (struct bluealsa_ctl *)ext->private_data;
if (key > ctl->elem_list_size)
return -EINVAL;
const struct ctl_elem *elem = &ctl->elem_list[key];
switch (elem->type) {
case CTL_ELEM_TYPE_BATTERY:
*imin = 0;
*imax = 100;
*istep = 1;
break;
case CTL_ELEM_TYPE_SWITCH:
return -EINVAL;
case CTL_ELEM_TYPE_VOLUME:
switch (elem->pcm->transport) {
case BA_PCM_TRANSPORT_A2DP_SOURCE:
case BA_PCM_TRANSPORT_A2DP_SINK:
*imax = 127;
break;
case BA_PCM_TRANSPORT_HFP_AG:
case BA_PCM_TRANSPORT_HFP_HF:
case BA_PCM_TRANSPORT_HSP_AG:
case BA_PCM_TRANSPORT_HSP_HS:
*imax = 15;
break;
default:
return -EINVAL;
}
*imin = 0;
*istep = 1;
break;
}
return 0;
}
static int bluealsa_read_integer(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key, long *value) {
struct bluealsa_ctl *ctl = (struct bluealsa_ctl *)ext->private_data;
if (key > ctl->elem_list_size)
return -EINVAL;
const struct ctl_elem *elem = &ctl->elem_list[key];
const struct ba_pcm *pcm = elem->pcm;
switch (elem->type) {
case CTL_ELEM_TYPE_BATTERY:
value[0] = elem->dev->battery_level;
break;
case CTL_ELEM_TYPE_SWITCH:
value[0] = !pcm->volume.ch1_muted;
if (pcm->channels == 2)
value[1] = !pcm->volume.ch2_muted;
break;
case CTL_ELEM_TYPE_VOLUME:
value[0] = pcm->volume.ch1_volume;
if (pcm->channels == 2)
value[1] = pcm->volume.ch2_volume;
break;
}
return 0;
}
static int bluealsa_write_integer(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key, long *value) {
struct bluealsa_ctl *ctl = (struct bluealsa_ctl *)ext->private_data;
if (key > ctl->elem_list_size)
return -EINVAL;
struct ctl_elem *elem = &ctl->elem_list[key];
struct ba_pcm *pcm = elem->pcm;
uint16_t old = pcm->volume.raw;
switch (elem->type) {
case CTL_ELEM_TYPE_BATTERY:
/* this element should be read-only */
return -EINVAL;
case CTL_ELEM_TYPE_SWITCH:
pcm->volume.ch1_muted = !value[0];
if (pcm->channels == 2)
pcm->volume.ch2_muted = !value[1];
break;
case CTL_ELEM_TYPE_VOLUME:
pcm->volume.ch1_volume = value[0];
if (pcm->channels == 2)
pcm->volume.ch2_volume = value[1];
break;
}
/* check whether update is required */
if (pcm->volume.raw == old)
return 0;
if (!bluealsa_dbus_pcm_update(&ctl->dbus_ctx, pcm, BLUEALSA_PCM_VOLUME, NULL))
return -ENOMEM;
return 1;
}
static void bluealsa_subscribe_events(snd_ctl_ext_t *ext, int subscribe) {
struct bluealsa_ctl *ctl = (struct bluealsa_ctl *)ext->private_data;
if (subscribe) {
if (!ctl->single_device)
bluealsa_dbus_connection_signal_match_add(&ctl->dbus_ctx, ctl->dbus_ctx.ba_service, NULL,
DBUS_INTERFACE_OBJECT_MANAGER, "InterfacesAdded", "path_namespace='/org/bluealsa'");
bluealsa_dbus_connection_signal_match_add(&ctl->dbus_ctx, ctl->dbus_ctx.ba_service, NULL,
DBUS_INTERFACE_OBJECT_MANAGER, "InterfacesRemoved", "path_namespace='/org/bluealsa'");
char dbus_args[50];
snprintf(dbus_args, sizeof(dbus_args), "arg0='%s',arg2=''", ctl->dbus_ctx.ba_service);
bluealsa_dbus_connection_signal_match_add(&ctl->dbus_ctx, DBUS_SERVICE_DBUS, NULL,
DBUS_INTERFACE_DBUS, "NameOwnerChanged", dbus_args);
bluealsa_dbus_connection_signal_match_add(&ctl->dbus_ctx, ctl->dbus_ctx.ba_service, NULL,
DBUS_INTERFACE_PROPERTIES, "PropertiesChanged", "arg0='"BLUEALSA_INTERFACE_PCM"'");
bluealsa_dbus_connection_signal_match_add(&ctl->dbus_ctx, ctl->dbus_ctx.ba_service, NULL,
DBUS_INTERFACE_PROPERTIES, "PropertiesChanged", "arg0='"BLUEALSA_INTERFACE_RFCOMM"'");
bluealsa_dbus_connection_signal_match_add(&ctl->dbus_ctx, "org.bluez", NULL,
DBUS_INTERFACE_PROPERTIES, "PropertiesChanged", "arg0='org.bluez.Device1'");
}
else
bluealsa_dbus_connection_signal_match_clean(&ctl->dbus_ctx);
dbus_connection_flush(ctl->dbus_ctx.conn);
}
static int bluealsa_elem_update_list_add(struct bluealsa_ctl *ctl,
const char *elem_name, unsigned int mask) {
struct ctl_elem_update *tmp = ctl->elem_update_list;
if ((tmp = realloc(tmp, (ctl->elem_update_list_size + 1) * sizeof(*tmp))) == NULL)
return -1;
tmp[ctl->elem_update_list_size].event_mask = mask;
*stpncpy(tmp[ctl->elem_update_list_size].name, elem_name,
sizeof(tmp[ctl->elem_update_list_size].name) - 1) = '\0';
ctl->elem_update_list = tmp;
ctl->elem_update_list_size++;
return 0;
}
#define bluealsa_event_elem_added(ctl, elem) \
bluealsa_elem_update_list_add(ctl, elem, SND_CTL_EVENT_MASK_ADD)
#define bluealsa_event_elem_removed(ctl, elem) \
bluealsa_elem_update_list_add(ctl, elem, SND_CTL_EVENT_MASK_REMOVE)
#define bluealsa_event_elem_updated(ctl, elem) \
bluealsa_elem_update_list_add(ctl, elem, SND_CTL_EVENT_MASK_VALUE)
static dbus_bool_t bluealsa_dbus_msg_update_dev(const char *key,
DBusMessageIter *variant, void *userdata, DBusError *error) {
(void)error;
struct bt_dev *dev = (struct bt_dev *)userdata;
dev->mask = 0;
if (strcmp(key, "Alias") == 0) {
const char *alias;
dbus_message_iter_get_basic(variant, &alias);
*stpncpy(dev->name, alias, sizeof(dev->name) - 1) = '\0';
dev->mask = SND_CTL_EVENT_MASK_ADD;
}
if (strcmp(key, "Battery") == 0) {
char battery_level;
dbus_message_iter_get_basic(variant, &battery_level);
dev->mask = dev->battery_level == -1 ? SND_CTL_EVENT_MASK_ADD : SND_CTL_EVENT_MASK_VALUE;
dev->battery_level = battery_level;
}
return TRUE;
}
static DBusHandlerResult bluealsa_dbus_msg_filter(DBusConnection *conn,
DBusMessage *message, void *data) {
struct bluealsa_ctl *ctl = (struct bluealsa_ctl *)data;
(void)conn;
if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
DBusMessageIter iter;
if (!dbus_message_iter_init(message, &iter))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
const char *path = dbus_message_get_path(message);
const char *interface = dbus_message_get_interface(message);
const char *signal = dbus_message_get_member(message);
size_t i;
if (strcmp(interface, DBUS_INTERFACE_PROPERTIES) == 0 &&
strcmp(signal, "PropertiesChanged") == 0) {
const char *updated_interface;
dbus_message_iter_get_basic(&iter, &updated_interface);
dbus_message_iter_next(&iter);
/* handle BlueZ device properties update */
if (strcmp(updated_interface, "org.bluez.Device1") == 0)
for (i = 0; i < ctl->elem_list_size; i++) {
struct bt_dev *dev = ctl->elem_list[i].dev;
if (strcmp(dev->device_path, path) == 0)
bluealsa_dbus_message_iter_dict(&iter, NULL,
bluealsa_dbus_msg_update_dev, dev);
if (dev->mask & SND_CTL_EVENT_MASK_ADD)
goto remove_add;
}
/* handle BlueALSA RFCOMM properties update */
if (strcmp(updated_interface, BLUEALSA_INTERFACE_RFCOMM) == 0)
for (i = 0; i < ctl->elem_list_size; i++) {
struct bt_dev *dev = ctl->elem_list[i].dev;
if (strcmp(dev->rfcomm_path, path) == 0) {
bluealsa_dbus_message_iter_dict(&iter, NULL,
bluealsa_dbus_msg_update_dev, dev);
if (dev->mask & SND_CTL_EVENT_MASK_ADD)
goto remove_add;
if (dev->mask & SND_CTL_EVENT_MASK_VALUE)
bluealsa_event_elem_updated(ctl, ctl->elem_list[i].name);
}
}
/* handle BlueALSA PCM properties update */
if (strcmp(updated_interface, BLUEALSA_INTERFACE_PCM) == 0)
for (i = 0; i < ctl->elem_list_size; i++) {
struct ctl_elem *elem = &ctl->elem_list[i];
if (strcmp(elem->pcm->pcm_path, path) == 0) {
if (elem->type == CTL_ELEM_TYPE_BATTERY) {
bluealsa_dbus_message_iter_dict(&iter, NULL,
bluealsa_dbus_msg_update_dev, elem->dev);
if (elem->dev->mask & SND_CTL_EVENT_MASK_ADD)
goto remove_add;
if (elem->dev->mask & SND_CTL_EVENT_MASK_VALUE)
bluealsa_event_elem_updated(ctl, ctl->elem_list[i].name);
}
else {
bluealsa_dbus_message_iter_get_pcm_props(&iter, NULL, elem->pcm);
bluealsa_event_elem_updated(ctl, elem->name);
}
}
}
}
else if (strcmp(interface, DBUS_INTERFACE_OBJECT_MANAGER) == 0) {
if (!ctl->single_device &&
strcmp(signal, "InterfacesAdded") == 0) {
struct ba_pcm pcm;
if (bluealsa_dbus_message_iter_get_pcm(&iter, NULL, &pcm) &&
pcm.transport != BA_PCM_TRANSPORT_NONE) {
bluealsa_pcm_add(ctl, &pcm);
goto remove_add;
}
}
if (strcmp(signal, "InterfacesRemoved") == 0) {
const char *pcm_path;
dbus_message_iter_get_basic(&iter, &pcm_path);
bluealsa_pcm_remove(ctl, pcm_path);
goto remove_add;
}
}
else if (strcmp(interface, DBUS_INTERFACE_DBUS) == 0 &&
strcmp(signal, "NameOwnerChanged") == 0) {
const char *service, *arg2;
dbus_message_iter_get_basic(&iter, &service);
if (strcmp(service, ctl->dbus_ctx.ba_service) == 0) {
if (dbus_message_iter_next(&iter) &&
dbus_message_iter_next(&iter) &&
dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) {
dbus_message_iter_get_basic(&iter, &arg2);
if (strlen(arg2) == 0) {
/* BlueALSA daemon has terminated,
* so all PCMs have been removed. */
ctl->pcm_list_size = 0;
goto remove_add;
}
}
}
}
return DBUS_HANDLER_RESULT_HANDLED;
remove_add: {
/* During a PCM name change, new PCM insertion and/or deletion, the name
* of all control elements might have change, because of optional unique
* device ID suffix - for more information see the bluealsa_elem_set_name()
* function. So, in such a case we will simply remove all old controllers
* and add new ones in order to update potential name changes. */
for (i = 0; i < ctl->elem_list_size; i++)
bluealsa_event_elem_removed(ctl, ctl->elem_list[i].name);
bluealsa_create_elem_list(ctl);
for (i = 0; i < ctl->elem_list_size; i++)
bluealsa_event_elem_added(ctl, ctl->elem_list[i].name);
if (ctl->single_device && ctl->pcm_list_size == 0) {
/* Trigger POLLERR by closing the read end of our pipe. This
* simulates a CTL device being unplugged. */
close(ctl->pipefd[0]);
ctl->pipefd[0] = -1;
}
return DBUS_HANDLER_RESULT_HANDLED;
}}
static int bluealsa_read_event(snd_ctl_ext_t *ext, snd_ctl_elem_id_t *id, unsigned int *event_mask) {
struct bluealsa_ctl *ctl = ext->private_data;
/* Some applications (e.g. MPD) ignore POLLERR and rely on snd_ctl_read()
* to return an appropriate error code. So we check the state of our
* device disconnection pipe and return -ENODEV if the device is
* disconnected. */
if (ctl->single_device && ctl->pipefd[0] == -1)
return -ENODEV;
if (ctl->elem_update_list_size) {
snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
snd_ctl_elem_id_set_name(id, ctl->elem_update_list[ctl->elem_update_event_i].name);
*event_mask = ctl->elem_update_list[ctl->elem_update_event_i].event_mask;
if (++ctl->elem_update_event_i == ctl->elem_update_list_size) {
ctl->elem_update_list_size = 0;
ctl->elem_update_event_i = 0;
}
return 1;
}
/* The ALSA snd_mixer API does not propagate the
* snd_mixer_poll_descriptors_revents() call down to the underlying hctl
* API, so our .poll_revents callback is never invoked by applications
* using the snd_mixer API (i.e. just about every mixer application!).
* But we need to feed poll() events back to our dispatching function.
* Since ALSA is not cooperating, we will call poll() once more by
* ourself and receive required event flags. If someday ALSA will be so
* kind to actually call .poll_revents(), this code should remain as a
* backward compatibility. */
bluealsa_dbus_connection_dispatch(&ctl->dbus_ctx);
if (ctl->elem_update_list_size)
return bluealsa_read_event(ext, id, event_mask);
return -EAGAIN;
}
static int bluealsa_poll_descriptors_count(snd_ctl_ext_t *ext) {
struct bluealsa_ctl *ctl = ext->private_data;
nfds_t dbus_nfds = 0;
bluealsa_dbus_connection_poll_fds(&ctl->dbus_ctx, NULL, &dbus_nfds);
return 1 + dbus_nfds;
}
static int bluealsa_poll_descriptors(snd_ctl_ext_t *ext, struct pollfd *pfd,
unsigned int nfds) {
struct bluealsa_ctl *ctl = ext->private_data;
nfds_t dbus_nfds = nfds - 1;
pfd[0].fd = ctl->pipefd[1];
/* For our internal PIPE we are not interested
* in any I/O events, only in error condition. */
pfd[0].events = 0;
if (!bluealsa_dbus_connection_poll_fds(&ctl->dbus_ctx, &pfd[1], &dbus_nfds))
return -EINVAL;
return 1 + dbus_nfds;
}
static int bluealsa_poll_revents(snd_ctl_ext_t *ext, struct pollfd *pfd,
unsigned int nfds, unsigned short *revents) {
struct bluealsa_ctl *ctl = ext->private_data;
*revents = pfd[0].revents;
if (bluealsa_dbus_connection_poll_dispatch(&ctl->dbus_ctx, &pfd[1], nfds - 1))
*revents |= POLLIN;
return 0;
}
static const snd_ctl_ext_callback_t bluealsa_snd_ctl_ext_callback = {
.close = bluealsa_close,
.elem_count = bluealsa_elem_count,
.elem_list = bluealsa_elem_list,
.find_elem = bluealsa_find_elem,
.get_attribute = bluealsa_get_attribute,
.get_integer_info = bluealsa_get_integer_info,
.read_integer = bluealsa_read_integer,
.write_integer = bluealsa_write_integer,
.subscribe_events = bluealsa_subscribe_events,
.read_event = bluealsa_read_event,
.poll_descriptors_count = bluealsa_poll_descriptors_count,
.poll_descriptors = bluealsa_poll_descriptors,
.poll_revents = bluealsa_poll_revents,
};
#if SND_CTL_EXT_VERSION >= 0x010001
#define TLV_DB_RANGE_SCALE_MIN_MAX(min, max, min_dB, max_dB) \
(min), (max), 4 /* dB min/max scale */, 2 * sizeof(int), (min_dB), (max_dB)
static int bluealsa_snd_ctl_ext_tlv_callback(snd_ctl_ext_t *ext,
snd_ctl_ext_key_t key, int op_flag, unsigned int numid,
unsigned int *tlv, unsigned int tlv_size) {
struct bluealsa_ctl *ctl = (struct bluealsa_ctl *)ext->private_data;
(void)numid;
static const unsigned int tlv_db_a2dp[] = {
3, /* dB range container */
10 * (2 /* range */ + 4 /* dB scale */) * sizeof(int),
TLV_DB_RANGE_SCALE_MIN_MAX(0, 1, -9600, -6988),
TLV_DB_RANGE_SCALE_MIN_MAX(2, 3, -5988, -5403),
TLV_DB_RANGE_SCALE_MIN_MAX(4, 5, -4988, -4666),
TLV_DB_RANGE_SCALE_MIN_MAX(6, 8, -4399, -3984),
TLV_DB_RANGE_SCALE_MIN_MAX(9, 13, -3806, -3277),
TLV_DB_RANGE_SCALE_MIN_MAX(14, 21, -3163, -2580),
TLV_DB_RANGE_SCALE_MIN_MAX(22, 35, -2504, -1837),
TLV_DB_RANGE_SCALE_MIN_MAX(36, 59, -1788, -1081),
TLV_DB_RANGE_SCALE_MIN_MAX(60, 100, -1048, -317),
TLV_DB_RANGE_SCALE_MIN_MAX(101, 127, -324, 0),
};
static const unsigned int tlv_db_sco[] = {
3, /* dB range container */
6 * (2 /* range */ + 4 /* dB scale */) * sizeof(int),
TLV_DB_RANGE_SCALE_MIN_MAX(0, 1, -9600, -3906),
TLV_DB_RANGE_SCALE_MIN_MAX(2, 3, -2906, -2321),
TLV_DB_RANGE_SCALE_MIN_MAX(4, 5, -1906, -1584),
TLV_DB_RANGE_SCALE_MIN_MAX(6, 7, -1321, -1099),
TLV_DB_RANGE_SCALE_MIN_MAX(8, 10, -904, -582),
TLV_DB_RANGE_SCALE_MIN_MAX(11, 15, -438, 0),
};
const struct ctl_elem *elem = &ctl->elem_list[key];
const unsigned int *tlv_db = NULL;
size_t tlv_db_size = 0;
switch (elem->pcm->transport) {