summaryrefslogtreecommitdiffstats
path: root/alsa-lib/test/seq.c
blob: 34b000fd879f5feee04338b80062e8f393b2b18c (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <getopt.h>
#include "../include/asoundlib.h"

#include "seq-decoder.c"
#include "seq-sender.c"

#define SEQ_VERSION "0.0.1"

#define HELPID_HELP             1000
#define HELPID_DEBUG            1001
#define HELPID_VERBOSE		1002
#define HELPID_VERSION          1003

int max_clients;
int max_ports;
int max_queues;
int debug = 0;
int verbose = 0;

void set_name(snd_seq_t *handle)
{
	int err;
	char name[64];
	
	sprintf(name, "SeqUtil - %i", getpid());
	if ((err = snd_seq_set_client_name(handle, name)) < 0) {
		fprintf(stderr, "Set client info error: %s\n", snd_strerror(err));
		exit(0);
	}
}

void system_info(snd_seq_t *handle)
{
	int err;
	snd_seq_system_info_t *sysinfo;
	
	snd_seq_system_info_alloca(&sysinfo);
	if ((err = snd_seq_system_info(handle, sysinfo))<0) {
		fprintf(stderr, "System info error: %s\n", snd_strerror(err));
		exit(0);
	}
	max_clients = snd_seq_system_info_get_clients(sysinfo);
	max_ports = snd_seq_system_info_get_ports(sysinfo);
	max_queues = snd_seq_system_info_get_ports(sysinfo);
}

void show_system_info(snd_seq_t *handle ATTRIBUTE_UNUSED)
{
	printf("System info\n");
	printf("  Max queues    : %i\n", max_queues);
	printf("  Max clients   : %i\n", max_clients);
	printf("  Max ports     : %i\n", max_ports);
}

void show_queue_status(snd_seq_t *handle, int queue)
{
	int err, idx, min, max;
	snd_seq_queue_status_t *status;

	snd_seq_queue_status_alloca(&status);
	min = queue < 0 ? 0 : queue;
	max = queue < 0 ? max_queues : queue + 1;
	for (idx = min; idx < max; idx++) {
		if ((err = snd_seq_get_queue_status(handle, idx, status))<0) {
			if (err == -ENOENT)
				continue;
			fprintf(stderr, "Client %i info error: %s\n", idx, snd_strerror(err));
			exit(0);
		}
		printf("Queue %i info\n", snd_seq_queue_status_get_queue(status));
		printf("  Tick          : %u\n", snd_seq_queue_status_get_tick_time(status)); 
		printf("  Realtime      : %i.%i\n",
		       snd_seq_queue_status_get_real_time(status)->tv_sec,
		       snd_seq_queue_status_get_real_time(status)->tv_nsec);
		printf("  Flags         : 0x%x\n", snd_seq_queue_status_get_status(status));
	}
}

void show_port_info(snd_seq_t *handle, int client, int port)
{
	int err, idx, min, max;
	snd_seq_port_info_t *info;

	snd_seq_port_info_alloca(&info);
	min = port < 0 ? 0 : port;
	max = port < 0 ? max_ports : port + 1;
	for (idx = min; idx < max; idx++) {
		if ((err = snd_seq_get_any_port_info(handle, client, idx, info))<0) {
			if (err == -ENOENT)
				continue;
			fprintf(stderr, "Port %i/%i info error: %s\n", client, idx, snd_strerror(err));
			exit(0);
		}
		printf("  Port %i info\n", idx);
		printf("    Client        : %i\n", snd_seq_port_info_get_client(info));
		printf("    Port          : %i\n", snd_seq_port_info_get_port(info));
		printf("    Name          : %s\n", snd_seq_port_info_get_name(info));
		printf("    Capability    : 0x%x\n", snd_seq_port_info_get_capability(info));
		printf("    Type          : 0x%x\n", snd_seq_port_info_get_type(info));
		//printf("    Midi channels : %i\n", info.midi_channels);
		//printf("    Synth voices  : %i\n", info.synth_voices);
		printf("    Output subs   : %i\n", snd_seq_port_info_get_write_use(info));
		printf("    Input subs    : %i\n", snd_seq_port_info_get_read_use(info));
	}
}

void show_client_info(snd_seq_t *handle, int client)
{
	int err, idx, min, max;
	snd_seq_client_info_t *info;

	snd_seq_client_info_alloca(&info);
	min = client < 0 ? 0 : client;
	max = client < 0 ? max_clients : client + 1;
	for (idx = min; idx < max; idx++) {
		if ((err = snd_seq_get_any_client_info(handle, idx, info))<0) {
			if (err == -ENOENT)
				continue;
			fprintf(stderr, "Client %i info error: %s\n", idx, snd_strerror(err));
			exit(0);
		}
		printf("Client %i info\n", idx);
		if (verbose)
			printf("  Client        : %i\n", snd_seq_client_info_get_client(info));
		printf("  Type          : %s\n", snd_seq_client_info_get_type(info) == SND_SEQ_KERNEL_CLIENT ? "kernel" : "user");
		printf("  Name          : %s\n", snd_seq_client_info_get_name(info));
	}
}

static void help(void)
{
	printf("Usage: seq <options> command\n");
	printf("\nAvailable options:\n");
	printf("  -h,--help       this help\n");
	printf("  -d,--debug      debug mode\n");
	printf("  -v,--verbose    verbose mode\n");
	printf("  -V,--version    print version of this program\n");
	printf("\nAvailable commands:\n");
	printf("  system          show basic sequencer info\n");
	printf("  queue [#]       show all queues or specified queue\n");
	printf("  client [#]      show all clients or specified client\n");
	printf("  port <client> [#]  show all ports or specified port for specified client\n");
	printf("  decoder         event decoder\n");
	printf("  sender <client.port> [<client.port>] ...   event sender\n");
}

int main(int argc, char *argv[])
{
	int morehelp, err, arg, arg1;
	snd_seq_t *handle;
	static struct option long_option[] =
	{
		{"help", 0, NULL, HELPID_HELP},
		{"debug", 0, NULL, HELPID_DEBUG},
		{"verbose", 0, NULL, HELPID_VERBOSE},
		{"version", 0, NULL, HELPID_VERSION},
		{NULL, 0, NULL, 0},
        };
        
        morehelp = 0;
	
	while (1) {
		int c;

		if ((c = getopt_long(argc, argv, "hdvV", long_option, NULL)) < 0)
			break;
		switch (c) {
		case 'h':
		case HELPID_HELP:
			morehelp++;
			break;
		case 'd':
		case HELPID_DEBUG:
			debug = 1;
			break;
		case 'v':
		case HELPID_VERBOSE:
			verbose = 1;
			break;
		case 'V':
		case HELPID_VERSION:
			printf("alsactl version " SEQ_VERSION "\n");
			return 1;
		default:
			fprintf(stderr, "\07Invalid switch or option needs an argument.\n");
			morehelp++;
		}
	}
        if (morehelp) {
                help();
                return 1;
        }
	if (argc - optind <= 0) {
		fprintf(stderr, "seq: Specify command...\n");
		return 0;
	}
	if ((err = snd_seq_open(&handle, "hw", SND_SEQ_OPEN_DUPLEX, 0))<0) {
		fprintf(stderr, "Open error: %s\n", snd_strerror(err));
		exit(0);
	}
	set_name(handle);
	system_info(handle);

        if (!strcmp(argv[optind], "system")) {
		show_system_info(handle);
	} else if (!strcmp(argv[optind], "queue")) {
		arg = argc - optind > 1 ? atoi(argv[optind + 1]) : -1;
		show_queue_status(handle, arg);
	} else if (!strcmp(argv[optind], "client")) {
		arg = argc - optind > 1 ? atoi(argv[optind + 1]) : -1;
		show_client_info(handle, arg);
	} else if (!strcmp(argv[optind], "port")) {
		arg = argc - optind > 1 ? atoi(argv[optind + 1]) : -1;
		if (arg < 0) {
			fprintf(stderr, "Specify port...\n");
			exit(0);
		}
		arg1 = argc - optind > 2 ? atoi(argv[optind + 2]) : -1;
		show_port_info(handle, arg, arg1);
	} else if (!strcmp(argv[optind], "decoder")) {
		event_decoder(handle, argc - optind - 1, argv + optind + 1);
	} else if (!strcmp(argv[optind], "sender")) {
		event_sender(handle, argc - optind - 1, argv + optind + 1);
	} else {
		help();
	}
	exit(1);
}