-
Notifications
You must be signed in to change notification settings - Fork 1
/
control.c
2666 lines (2449 loc) · 67.8 KB
/
control.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
/**
* \file control/control.c
* \brief CTL interface - primitive controls
* \author Abramo Bagnara <abramo@alsa-project.org>
* \date 2000
*
* CTL interface is designed to access primitive controls.
* See \ref control page for more details.
*/
/*
* Control Interface - main file
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
*
*
* 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.1 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 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*! \page control Control interface
<P>Control interface is designed to access primitive controls. There is
also interface notifying about control and structure changes.
\section control_general_overview General overview
The primitive controls can be integer, inter64, boolean, enumerators, bytes
and IEC958 structure.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/poll.h>
#include "control_local.h"
/**
* \brief get identifier of CTL handle
* \param ctl CTL handle
* \return ascii identifier of CTL handle
*
* Returns the ASCII identifier of given CTL handle. It's the same
* identifier specified in snd_ctl_open().
*/
const char *snd_ctl_name(snd_ctl_t *ctl)
{
assert(ctl);
return ctl->name;
}
/**
* \brief get type of CTL handle
* \param ctl CTL handle
* \return type of CTL handle
*
* Returns the type #snd_ctl_type_t of given CTL handle.
*/
snd_ctl_type_t snd_ctl_type(snd_ctl_t *ctl)
{
assert(ctl);
return ctl->type;
}
/**
* \brief close CTL handle
* \param ctl CTL handle
* \return 0 on success otherwise a negative error code
*
* Closes the specified CTL handle and frees all associated
* resources.
*/
int snd_ctl_close(snd_ctl_t *ctl)
{
int err;
while (!list_empty(&ctl->async_handlers)) {
snd_async_handler_t *h = list_entry(&ctl->async_handlers.next, snd_async_handler_t, hlist);
snd_async_del_handler(h);
}
err = ctl->ops->close(ctl);
free(ctl->name);
snd_dlobj_cache_put(ctl->open_func);
free(ctl);
return err;
}
/**
* \brief set nonblock mode
* \param ctl CTL handle
* \param nonblock 0 = block, 1 = nonblock mode, 2 = abort
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_nonblock(snd_ctl_t *ctl, int nonblock)
{
int err;
assert(ctl);
err = ctl->ops->nonblock(ctl, nonblock);
if (err < 0)
return err;
ctl->nonblock = nonblock;
return 0;
}
#ifndef DOC_HIDDEN
int snd_ctl_new(snd_ctl_t **ctlp, snd_ctl_type_t type, const char *name)
{
snd_ctl_t *ctl;
ctl = calloc(1, sizeof(*ctl));
if (!ctl)
return -ENOMEM;
ctl->type = type;
if (name)
ctl->name = strdup(name);
INIT_LIST_HEAD(&ctl->async_handlers);
*ctlp = ctl;
return 0;
}
/**
* \brief set async mode
* \param ctl CTL handle
* \param sig Signal to raise: < 0 disable, 0 default (SIGIO)
* \param pid Process ID to signal: 0 current
* \return 0 on success otherwise a negative error code
*
* A signal is raised when a change happens.
*/
int snd_ctl_async(snd_ctl_t *ctl, int sig, pid_t pid)
{
assert(ctl);
if (sig == 0)
sig = SIGIO;
if (pid == 0)
pid = getpid();
return ctl->ops->async(ctl, sig, pid);
}
#endif
/**
* \brief get count of poll descriptors for CTL handle
* \param ctl CTL handle
* \return count of poll descriptors
*/
int snd_ctl_poll_descriptors_count(snd_ctl_t *ctl)
{
assert(ctl);
if (ctl->ops->poll_descriptors_count)
return ctl->ops->poll_descriptors_count(ctl);
if (ctl->poll_fd < 0)
return 0;
return 1;
}
/**
* \brief get poll descriptors
* \param ctl CTL handle
* \param pfds array of poll descriptors
* \param space space in the poll descriptor array
* \return count of filled descriptors
*/
int snd_ctl_poll_descriptors(snd_ctl_t *ctl, struct pollfd *pfds, unsigned int space)
{
assert(ctl && pfds);
if (ctl->ops->poll_descriptors)
return ctl->ops->poll_descriptors(ctl, pfds, space);
if (ctl->poll_fd < 0)
return 0;
if (space > 0) {
pfds->fd = ctl->poll_fd;
pfds->events = POLLIN|POLLERR|POLLNVAL;
return 1;
}
return 0;
}
/**
* \brief get returned events from poll descriptors
* \param ctl CTL handle
* \param pfds array of poll descriptors
* \param nfds count of poll descriptors
* \param revents returned events
* \return zero if success, otherwise a negative error code
*/
int snd_ctl_poll_descriptors_revents(snd_ctl_t *ctl, struct pollfd *pfds, unsigned int nfds, unsigned short *revents)
{
assert(ctl && pfds && revents);
if (ctl->ops->poll_revents)
return ctl->ops->poll_revents(ctl, pfds, nfds, revents);
if (nfds == 1) {
*revents = pfds->revents;
return 0;
}
return -EINVAL;
}
/**
* \brief Ask to be informed about events (poll, #snd_async_add_ctl_handler, #snd_ctl_read)
* \param ctl CTL handle
* \param subscribe 0 = unsubscribe, 1 = subscribe, -1 = check subscribe or not
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_subscribe_events(snd_ctl_t *ctl, int subscribe)
{
assert(ctl);
return ctl->ops->subscribe_events(ctl, subscribe);
}
/**
* \brief Get card related information
* \param ctl CTL handle
* \param info Card info pointer
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_card_info(snd_ctl_t *ctl, snd_ctl_card_info_t *info)
{
assert(ctl && info);
return ctl->ops->card_info(ctl, info);
}
/**
* \brief Get a list of element identifiers
* \param ctl CTL handle
* \param list CTL element identifiers list pointer
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_elem_list(snd_ctl_t *ctl, snd_ctl_elem_list_t *list)
{
assert(ctl && list);
assert(list->space == 0 || list->pids);
return ctl->ops->element_list(ctl, list);
}
/**
* \brief Get CTL element information
* \param ctl CTL handle
* \param info CTL element id/information pointer
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_elem_info(snd_ctl_t *ctl, snd_ctl_elem_info_t *info)
{
assert(ctl && info && (info->id.name[0] || info->id.numid));
return ctl->ops->element_info(ctl, info);
}
/**
* \brief Create and add an user INTEGER CTL element
* \param ctl CTL handle
* \param id CTL element id to add
* \param count number of elements
* \param min minimum value
* \param max maximum value
* \param step value step
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_elem_add_integer(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id,
unsigned int count, long min, long max, long step)
{
snd_ctl_elem_info_t *info;
snd_ctl_elem_value_t *val;
unsigned int i;
int err;
assert(ctl && id && id->name[0]);
snd_ctl_elem_info_alloca(&info);
info->id = *id;
info->type = SND_CTL_ELEM_TYPE_INTEGER;
info->access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE;
info->count = count;
info->value.integer.min = min;
info->value.integer.max = max;
info->value.integer.step = step;
err = ctl->ops->element_add(ctl, info);
if (err < 0)
return err;
snd_ctl_elem_value_alloca(&val);
val->id = info->id;
for (i = 0; i < count; i++)
val->value.integer.value[i] = min;
err = ctl->ops->element_write(ctl, val);
return err;
}
/**
* \brief Create and add an user INTEGER64 CTL element
* \param ctl CTL handle
* \param id CTL element id to add
* \param count number of elements
* \param min minimum value
* \param max maximum value
* \param step value step
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_elem_add_integer64(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id,
unsigned int count, long long min, long long max,
long long step)
{
snd_ctl_elem_info_t *info;
snd_ctl_elem_value_t *val;
unsigned int i;
int err;
assert(ctl && id && id->name[0]);
snd_ctl_elem_info_alloca(&info);
info->id = *id;
info->type = SND_CTL_ELEM_TYPE_INTEGER64;
info->count = count;
info->value.integer64.min = min;
info->value.integer64.max = max;
info->value.integer64.step = step;
err = ctl->ops->element_add(ctl, info);
if (err < 0)
return err;
snd_ctl_elem_value_alloca(&val);
val->id = info->id;
for (i = 0; i < count; i++)
val->value.integer64.value[i] = min;
err = ctl->ops->element_write(ctl, val);
return err;
}
/**
* \brief Create and add an user BOOLEAN CTL element
* \param ctl CTL handle
* \param id CTL element id to add
* \param count number of elements
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_elem_add_boolean(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id,
unsigned int count)
{
snd_ctl_elem_info_t *info;
assert(ctl && id && id->name[0]);
snd_ctl_elem_info_alloca(&info);
info->id = *id;
info->type = SND_CTL_ELEM_TYPE_BOOLEAN;
info->count = count;
info->value.integer.min = 0;
info->value.integer.max = 1;
return ctl->ops->element_add(ctl, info);
}
/**
* \brief Create and add a user-defined control element of type enumerated.
* \param[in] ctl Control device handle.
* \param[in] id ID of the new control element.
* \param[in] count Number of element values.
* \param[in] items Range of possible values (0 ... \a items - 1).
* \param[in] names An array containing \a items strings.
* \return Zero on success, otherwise a negative error code.
*
* This function creates a user element, i.e., a control element that is not
* controlled by the control device's driver but that is just stored together
* with the other elements of \a ctl.
*
* The fields of \a id, except numid, must be set to unique values that
* identify the new element.
*
* The new element is locked; its value is initialized as zero.
*
* \par Errors:
* <dl>
* <dt>-EBUSY<dd>A control element with ID \a id already exists.
* <dt>-EINVAL<dd>\a count is not at least one or greater than 128, or \a items
* is not at least one, or a string in \a names is empty or longer than 63
* bytes, or the strings in \a names require more than 64 KB storage.
* <dt>-ENOMEM<dd>Out of memory, or there are too many user control elements.
* <dt>-ENXIO<dd>This driver does not support (enumerated) user controls.
* <dt>-ENODEV<dd>Device unplugged.
* </dl>
*
* \par Compatibility:
* snd_ctl_elem_add_enumerated() was introduced in ALSA 1.0.25.
*/
int snd_ctl_elem_add_enumerated(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id,
unsigned int count, unsigned int items,
const char *const names[])
{
snd_ctl_elem_info_t *info;
unsigned int i, bytes;
char *buf, *p;
int err;
assert(ctl && id && id->name[0] && names);
snd_ctl_elem_info_alloca(&info);
info->id = *id;
info->type = SND_CTL_ELEM_TYPE_ENUMERATED;
info->count = count;
info->value.enumerated.items = items;
bytes = 0;
for (i = 0; i < items; ++i)
bytes += strlen(names[i]) + 1;
buf = bytes ? malloc(bytes) : NULL;
if (!buf)
return -ENOMEM;
info->value.enumerated.names_ptr = (uintptr_t)buf;
info->value.enumerated.names_length = bytes;
p = buf;
for (i = 0; i < items; ++i) {
strcpy(p, names[i]);
p += strlen(names[i]) + 1;
}
err = ctl->ops->element_add(ctl, info);
free(buf);
return err;
}
/**
* \brief Create and add an user IEC958 CTL element
* \param ctl CTL handle
* \param id CTL element info to add
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_elem_add_iec958(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id)
{
snd_ctl_elem_info_t *info;
assert(ctl && id && id->name[0]);
snd_ctl_elem_info_alloca(&info);
info->id = *id;
info->type = SND_CTL_ELEM_TYPE_IEC958;
info->count = 1;
return ctl->ops->element_add(ctl, info);
}
/**
* \brief Remove an user CTL element
* \param ctl CTL handle
* \param id CTL element identification
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_elem_remove(snd_ctl_t *ctl, snd_ctl_elem_id_t *id)
{
assert(ctl && id && (id->name[0] || id->numid));
return ctl->ops->element_remove(ctl, id);
}
/**
* \brief Get CTL element value
* \param ctl CTL handle
* \param control CTL element id/value pointer
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_elem_read(snd_ctl_t *ctl, snd_ctl_elem_value_t *control)
{
assert(ctl && control && (control->id.name[0] || control->id.numid));
return ctl->ops->element_read(ctl, control);
}
/**
* \brief Set CTL element value
* \param ctl CTL handle
* \param control CTL element id/value pointer
* \retval 0 on success
* \retval >0 on success when value was changed
* \retval <0 a negative error code
*/
int snd_ctl_elem_write(snd_ctl_t *ctl, snd_ctl_elem_value_t *control)
{
assert(ctl && control && (control->id.name[0] || control->id.numid));
return ctl->ops->element_write(ctl, control);
}
static int snd_ctl_tlv_do(snd_ctl_t *ctl, int op_flag,
const snd_ctl_elem_id_t *id,
unsigned int *tlv, unsigned int tlv_size)
{
snd_ctl_elem_info_t *info = NULL;
int err;
if (id->numid == 0) {
info = calloc(1, sizeof(*info));
if (info == NULL)
return -ENOMEM;
info->id = *id;
id = &info->id;
err = snd_ctl_elem_info(ctl, info);
if (err < 0)
goto __err;
if (id->numid == 0) {
err = -ENOENT;
goto __err;
}
}
err = ctl->ops->element_tlv(ctl, op_flag, id->numid, tlv, tlv_size);
__err:
if (info)
free(info);
return err;
}
/**
* \brief Get CTL element TLV value
* \param ctl CTL handle
* \param id CTL element id pointer
* \param tlv TLV array pointer to store
* \param tlv_size TLV array size in bytes
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_elem_tlv_read(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id,
unsigned int *tlv, unsigned int tlv_size)
{
int err;
assert(ctl && id && (id->name[0] || id->numid) && tlv);
if (tlv_size < 2 * sizeof(int))
return -EINVAL;
/* 1.0.12 driver doesn't return the error even if the user TLV
* is empty. So, initialize TLV here with an invalid type
* and compare the returned value after ioctl for checking
* the validity of TLV.
*/
tlv[0] = -1;
tlv[1] = 0;
err = snd_ctl_tlv_do(ctl, 0, id, tlv, tlv_size);
if (err >= 0 && tlv[0] == (unsigned int)-1)
err = -ENXIO;
return err;
}
/**
* \brief Set CTL element TLV value
* \param ctl CTL handle
* \param id CTL element id pointer
* \param tlv TLV array pointer to store
* \retval 0 on success
* \retval >0 on success when value was changed
* \retval <0 a negative error code
*/
int snd_ctl_elem_tlv_write(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id,
const unsigned int *tlv)
{
assert(ctl && id && (id->name[0] || id->numid) && tlv);
return snd_ctl_tlv_do(ctl, 1, id, (unsigned int *)tlv, tlv[1] + 2 * sizeof(unsigned int));
}
/**
* \brief Process CTL element TLV command
* \param ctl CTL handle
* \param id CTL element id pointer
* \param tlv TLV array pointer to process
* \retval 0 on success
* \retval >0 on success when value was changed
* \retval <0 a negative error code
*/
int snd_ctl_elem_tlv_command(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id,
const unsigned int *tlv)
{
assert(ctl && id && (id->name[0] || id->numid) && tlv);
return snd_ctl_tlv_do(ctl, -1, id, (unsigned int *)tlv, tlv[1] + 2 * sizeof(unsigned int));
}
/**
* \brief Lock CTL element
* \param ctl CTL handle
* \param id CTL element id pointer
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_elem_lock(snd_ctl_t *ctl, snd_ctl_elem_id_t *id)
{
assert(ctl && id);
return ctl->ops->element_lock(ctl, id);
}
/**
* \brief Unlock CTL element
* \param ctl CTL handle
* \param id CTL element id pointer
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_elem_unlock(snd_ctl_t *ctl, snd_ctl_elem_id_t *id)
{
assert(ctl && id);
return ctl->ops->element_unlock(ctl, id);
}
/**
* \brief Get next hardware dependent device number
* \param ctl CTL handle
* \param device current device on entry and next device on return
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_hwdep_next_device(snd_ctl_t *ctl, int *device)
{
assert(ctl && device);
return ctl->ops->hwdep_next_device(ctl, device);
}
/**
* \brief Get info about a hardware dependent device
* \param ctl CTL handle
* \param info Hardware dependent device id/info pointer
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_hwdep_info(snd_ctl_t *ctl, snd_hwdep_info_t * info)
{
assert(ctl && info);
return ctl->ops->hwdep_info(ctl, info);
}
/**
* \brief Get next PCM device number
* \param ctl CTL handle
* \param device current device on entry and next device on return
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_pcm_next_device(snd_ctl_t *ctl, int * device)
{
assert(ctl && device);
return ctl->ops->pcm_next_device(ctl, device);
}
/**
* \brief Get info about a PCM device
* \param ctl CTL handle
* \param info PCM device id/info pointer
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_pcm_info(snd_ctl_t *ctl, snd_pcm_info_t * info)
{
assert(ctl && info);
return ctl->ops->pcm_info(ctl, info);
}
/**
* \brief Set preferred PCM subdevice number of successive PCM open
* \param ctl CTL handle
* \param subdev Preferred PCM subdevice number
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_pcm_prefer_subdevice(snd_ctl_t *ctl, int subdev)
{
assert(ctl);
return ctl->ops->pcm_prefer_subdevice(ctl, subdev);
}
/**
* \brief Get next RawMidi device number
* \param ctl CTL handle
* \param device current device on entry and next device on return
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_rawmidi_next_device(snd_ctl_t *ctl, int * device)
{
assert(ctl && device);
return ctl->ops->rawmidi_next_device(ctl, device);
}
/**
* \brief Get info about a RawMidi device
* \param ctl CTL handle
* \param info RawMidi device id/info pointer
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_rawmidi_info(snd_ctl_t *ctl, snd_rawmidi_info_t * info)
{
assert(ctl && info);
return ctl->ops->rawmidi_info(ctl, info);
}
/**
* \brief Set preferred RawMidi subdevice number of successive RawMidi open
* \param ctl CTL handle
* \param subdev Preferred RawMidi subdevice number
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_rawmidi_prefer_subdevice(snd_ctl_t *ctl, int subdev)
{
assert(ctl);
return ctl->ops->rawmidi_prefer_subdevice(ctl, subdev);
}
/**
* \brief Set Power State to given SND_CTL_POWER_* value and do the power management
* \param ctl CTL handle
* \param state Desired Power State
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_set_power_state(snd_ctl_t *ctl, unsigned int state)
{
assert(ctl);
if (ctl->ops->set_power_state)
return ctl->ops->set_power_state(ctl, state);
return -ENXIO;
}
/**
* \brief Get actual Power State
* \param ctl CTL handle
* \param state Destination value
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_get_power_state(snd_ctl_t *ctl, unsigned int *state)
{
assert(ctl);
if (ctl->ops->get_power_state)
return ctl->ops->get_power_state(ctl, state);
return -ENXIO;
}
/**
* \brief Read an event
* \param ctl CTL handle
* \param event Event pointer
* \return number of events read otherwise a negative error code on failure
*/
int snd_ctl_read(snd_ctl_t *ctl, snd_ctl_event_t *event)
{
assert(ctl && event);
return (ctl->ops->read)(ctl, event);
}
/**
* \brief Wait for a CTL to become ready (i.e. at least one event pending)
* \param ctl CTL handle
* \param timeout maximum time in milliseconds to wait
* \return 0 otherwise a negative error code on failure
*/
int snd_ctl_wait(snd_ctl_t *ctl, int timeout)
{
struct pollfd *pfd;
unsigned short revents;
int npfds, err, err_poll;
npfds = snd_ctl_poll_descriptors_count(ctl);
if (npfds <= 0 || npfds >= 16) {
SNDERR("Invalid poll_fds %d\n", npfds);
return -EIO;
}
pfd = alloca(sizeof(*pfd) * npfds);
err = snd_ctl_poll_descriptors(ctl, pfd, npfds);
if (err < 0)
return err;
if (err != npfds) {
SNDMSG("invalid poll descriptors %d\n", err);
return -EIO;
}
for (;;) {
err_poll = poll(pfd, npfds, timeout);
if (err_poll < 0)
return -errno;
if (! err_poll)
return 0;
err = snd_ctl_poll_descriptors_revents(ctl, pfd, npfds, &revents);
if (err < 0)
return err;
if (revents & (POLLERR | POLLNVAL))
return -EIO;
if (revents & (POLLIN | POLLOUT))
return 1;
}
}
/**
* \brief Add an async handler for a CTL
* \param handler Returned handler handle
* \param ctl CTL handle
* \param callback Callback function
* \param private_data Callback private data
* \return 0 otherwise a negative error code on failure
*/
int snd_async_add_ctl_handler(snd_async_handler_t **handler, snd_ctl_t *ctl,
snd_async_callback_t callback, void *private_data)
{
int err;
int was_empty;
snd_async_handler_t *h;
err = snd_async_add_handler(&h, _snd_ctl_async_descriptor(ctl),
callback, private_data);
if (err < 0)
return err;
h->type = SND_ASYNC_HANDLER_CTL;
h->u.ctl = ctl;
was_empty = list_empty(&ctl->async_handlers);
list_add_tail(&h->hlist, &ctl->async_handlers);
if (was_empty) {
err = snd_ctl_async(ctl, snd_async_handler_get_signo(h), getpid());
if (err < 0) {
snd_async_del_handler(h);
return err;
}
}
*handler = h;
return 0;
}
/**
* \brief Return CTL handle related to an async handler
* \param handler Async handler handle
* \return CTL handle
*/
snd_ctl_t *snd_async_handler_get_ctl(snd_async_handler_t *handler)
{
assert(handler->type == SND_ASYNC_HANDLER_CTL);
return handler->u.ctl;
}
static const char *const build_in_ctls[] = {
"hw", "shm", NULL
};
static int snd_ctl_open_conf(snd_ctl_t **ctlp, const char *name,
snd_config_t *ctl_root, snd_config_t *ctl_conf, int mode)
{
const char *str;
char *buf = NULL, *buf1 = NULL;
int err;
snd_config_t *conf, *type_conf = NULL;
snd_config_iterator_t i, next;
const char *lib = NULL, *open_name = NULL;
const char *id;
int (*open_func)(snd_ctl_t **, const char *, snd_config_t *, snd_config_t *, int) = NULL;
#ifndef PIC
extern void *snd_control_open_symbols(void);
#endif
if (snd_config_get_type(ctl_conf) != SND_CONFIG_TYPE_COMPOUND) {
if (name)
SNDERR("Invalid type for CTL %s definition", name);
else
SNDERR("Invalid type for CTL definition");
return -EINVAL;
}
err = snd_config_search(ctl_conf, "type", &conf);
if (err < 0) {
SNDERR("type is not defined");
return err;
}
err = snd_config_get_id(conf, &id);
if (err < 0) {
SNDERR("unable to get id");
return err;
}
err = snd_config_get_string(conf, &str);
if (err < 0) {
SNDERR("Invalid type for %s", id);
return err;
}
err = snd_config_search_definition(ctl_root, "ctl_type", str, &type_conf);
if (err >= 0) {
if (snd_config_get_type(type_conf) != SND_CONFIG_TYPE_COMPOUND) {
SNDERR("Invalid type for CTL type %s definition", str);
err = -EINVAL;
goto _err;
}
snd_config_for_each(i, next, type_conf) {
snd_config_t *n = snd_config_iterator_entry(i);
const char *id;
if (snd_config_get_id(n, &id) < 0)
continue;
if (strcmp(id, "comment") == 0)
continue;
if (strcmp(id, "lib") == 0) {
err = snd_config_get_string(n, &lib);
if (err < 0) {
SNDERR("Invalid type for %s", id);
goto _err;
}
continue;
}
if (strcmp(id, "open") == 0) {
err = snd_config_get_string(n, &open_name);
if (err < 0) {
SNDERR("Invalid type for %s", id);
goto _err;
}
continue;
}
SNDERR("Unknown field %s", id);
err = -EINVAL;
goto _err;
}
}
if (!open_name) {
buf = malloc(strlen(str) + 32);
if (buf == NULL) {
err = -ENOMEM;
goto _err;
}
open_name = buf;
sprintf(buf, "_snd_ctl_%s_open", str);
}
if (!lib) {
const char *const *build_in = build_in_ctls;
while (*build_in) {
if (!strcmp(*build_in, str))
break;
build_in++;
}
if (*build_in == NULL) {
buf1 = malloc(strlen(str) + sizeof(ALSA_PLUGIN_DIR) + 32);
if (buf1 == NULL) {
err = -ENOMEM;
goto _err;
}
lib = buf1;
sprintf(buf1, "%s/libasound_module_ctl_%s.so", ALSA_PLUGIN_DIR, str);
}
}
#ifndef PIC
snd_control_open_symbols();
#endif
open_func = snd_dlobj_cache_get(lib, open_name,
SND_DLSYM_VERSION(SND_CONTROL_DLSYM_VERSION), 1);
if (open_func) {
err = open_func(ctlp, name, ctl_root, ctl_conf, mode);
if (err >= 0) {
(*ctlp)->open_func = open_func;
err = 0;
} else {
snd_dlobj_cache_put(open_func);
}
} else {
err = -ENXIO;
}
_err:
if (type_conf)
snd_config_delete(type_conf);
free(buf);
free(buf1);
return err;
}
static int snd_ctl_open_noupdate(snd_ctl_t **ctlp, snd_config_t *root, const char *name, int mode)
{
int err;
snd_config_t *ctl_conf;
err = snd_config_search_definition(root, "ctl", name, &ctl_conf);
if (err < 0) {
SNDERR("Invalid CTL %s", name);
return err;
}
err = snd_ctl_open_conf(ctlp, name, root, ctl_conf, mode);
snd_config_delete(ctl_conf);
return err;
}
/**
* \brief Opens a CTL
* \param ctlp Returned CTL handle
* \param name ASCII identifier of the CTL handle
* \param mode Open mode (see #SND_CTL_NONBLOCK, #SND_CTL_ASYNC)
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_open(snd_ctl_t **ctlp, const char *name, int mode)
{
int err;
assert(ctlp && name);
err = snd_config_update();
if (err < 0)
return err;
return snd_ctl_open_noupdate(ctlp, snd_config, name, mode);
}
/**
* \brief Opens a CTL using local configuration
* \param ctlp Returned CTL handle
* \param name ASCII identifier of the CTL handle
* \param mode Open mode (see #SND_CTL_NONBLOCK, #SND_CTL_ASYNC)
* \param lconf Local configuration
* \return 0 on success otherwise a negative error code
*/
int snd_ctl_open_lconf(snd_ctl_t **ctlp, const char *name,
int mode, snd_config_t *lconf)
{
assert(ctlp && name && lconf);
return snd_ctl_open_noupdate(ctlp, lconf, name, mode);
}
/**
* \brief Opens a fallback CTL
* \param ctlp Returned CTL handle
* \param root Configuration root
* \param name ASCII identifier of the CTL handle used as fallback
* \param orig_name The original ASCII name
* \param mode Open mode (see #SND_CTL_NONBLOCK, #SND_CTL_ASYNC)