File: monitor.c

package info (click to toggle)
alsa-utils 1.0.28-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,280 kB
  • ctags: 1,585
  • sloc: ansic: 21,693; sh: 6,035; xml: 590; makefile: 374; sed: 16
file content (145 lines) | stat: -rw-r--r-- 3,461 bytes parent folder | download
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
/*
 *  Advanced Linux Sound Architecture Control Program
 *  Copyright (c) by Takashi Iwai <tiwai@suse.de>
 *
 *   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, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include "aconfig.h"
#include "version.h"
#include <stdio.h>
#include <alsa/asoundlib.h>

static int open_ctl(const char *name, snd_ctl_t **ctlp)
{
	snd_ctl_t *ctl;
	int err;

	err = snd_ctl_open(&ctl, name, SND_CTL_READONLY);
	if (err < 0) {
		fprintf(stderr, "Cannot open ctl %s\n", name);
		return err;
	}
	err = snd_ctl_subscribe_events(ctl, 1);
	if (err < 0) {
		fprintf(stderr, "Cannot open subscribe events to ctl %s\n", name);
		snd_ctl_close(ctl);
		return err;
	}
	*ctlp = ctl;
	return 0;
}

static int print_event(int card, snd_ctl_t *ctl)
{
	snd_ctl_event_t *event;
	unsigned int mask;
	int err;

	snd_ctl_event_alloca(&event);
	err = snd_ctl_read(ctl, event);
	if (err < 0)
		return err;

	if (snd_ctl_event_get_type(event) != SND_CTL_EVENT_ELEM)
		return 0;

	if (card >= 0)
		printf("card %d, ", card);
	printf("#%d (%i,%i,%i,%s,%i)",
	       snd_ctl_event_elem_get_numid(event),
	       snd_ctl_event_elem_get_interface(event),
	       snd_ctl_event_elem_get_device(event),
	       snd_ctl_event_elem_get_subdevice(event),
	       snd_ctl_event_elem_get_name(event),
	       snd_ctl_event_elem_get_index(event));

	mask = snd_ctl_event_elem_get_mask(event);
	if (mask == SND_CTL_EVENT_MASK_REMOVE) {
		printf(" REMOVE\n");
		return 0;
	}

	if (mask & SND_CTL_EVENT_MASK_VALUE)
		printf(" VALUE");
	if (mask & SND_CTL_EVENT_MASK_INFO)
		printf(" INFO");
	if (mask & SND_CTL_EVENT_MASK_ADD)
		printf(" ADD");
	if (mask & SND_CTL_EVENT_MASK_TLV)
		printf(" TLV");
	printf("\n");
	return 0;
}

#define MAX_CARDS	256

int monitor(const char *name)
{
	snd_ctl_t *ctls[MAX_CARDS];
	int ncards = 0;
	int show_cards;
	int i, err;

	if (!name) {
		int card = -1;
		while (snd_card_next(&card) >= 0 && card >= 0) {
			char cardname[16];
			if (ncards >= MAX_CARDS) {
				fprintf(stderr, "alsactl: too many cards\n");
				err = -E2BIG;
				goto error;
			}
			sprintf(cardname, "hw:%d", card);
			err = open_ctl(cardname, &ctls[ncards]);
			if (err < 0)
				goto error;
			ncards++;
		}
		show_cards = 1;
	} else {
		err = open_ctl(name, &ctls[0]);
		if (err < 0)
			goto error;
		ncards++;
		show_cards = 0;
	}

	for (;;) {
		struct pollfd fds[ncards];

		for (i = 0; i < ncards; i++)
			snd_ctl_poll_descriptors(ctls[i], &fds[i], 1);

		err = poll(fds, ncards, -1);
		if (err <= 0) {
			err = 0;
			break;
		}

		for (i = 0; i < ncards; i++) {
			unsigned short revents;
			snd_ctl_poll_descriptors_revents(ctls[i], &fds[i], 1,
							 &revents);
			if (revents & POLLIN)
				print_event(show_cards ? i : -1, ctls[i]);
		}
	}

 error:
	for (i = 0; i < ncards; i++)
		snd_ctl_close(ctls[i]);
	return err;
}