• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <ctype.h>
6 #include <getopt.h>
7 #include "../include/asoundlib.h"
8 
9 #include "seq-decoder.c"
10 #include "seq-sender.c"
11 
12 #define SEQ_VERSION "0.0.1"
13 
14 #define HELPID_HELP             1000
15 #define HELPID_DEBUG            1001
16 #define HELPID_VERBOSE		1002
17 #define HELPID_VERSION          1003
18 
19 int max_clients;
20 int max_ports;
21 int max_queues;
22 int debug = 0;
23 int verbose = 0;
24 
set_name(snd_seq_t * handle)25 void set_name(snd_seq_t *handle)
26 {
27 	int err;
28 	char name[64];
29 
30 	sprintf(name, "SeqUtil - %i", getpid());
31 	if ((err = snd_seq_set_client_name(handle, name)) < 0) {
32 		fprintf(stderr, "Set client info error: %s\n", snd_strerror(err));
33 		exit(0);
34 	}
35 }
36 
system_info(snd_seq_t * handle)37 void system_info(snd_seq_t *handle)
38 {
39 	int err;
40 	snd_seq_system_info_t *sysinfo;
41 
42 	snd_seq_system_info_alloca(&sysinfo);
43 	if ((err = snd_seq_system_info(handle, sysinfo))<0) {
44 		fprintf(stderr, "System info error: %s\n", snd_strerror(err));
45 		exit(0);
46 	}
47 	max_clients = snd_seq_system_info_get_clients(sysinfo);
48 	max_ports = snd_seq_system_info_get_ports(sysinfo);
49 	max_queues = snd_seq_system_info_get_ports(sysinfo);
50 }
51 
show_system_info(snd_seq_t * handle ATTRIBUTE_UNUSED)52 void show_system_info(snd_seq_t *handle ATTRIBUTE_UNUSED)
53 {
54 	printf("System info\n");
55 	printf("  Max queues    : %i\n", max_queues);
56 	printf("  Max clients   : %i\n", max_clients);
57 	printf("  Max ports     : %i\n", max_ports);
58 }
59 
show_queue_status(snd_seq_t * handle,int queue)60 void show_queue_status(snd_seq_t *handle, int queue)
61 {
62 	int err, idx, min, max;
63 	snd_seq_queue_status_t *status;
64 
65 	snd_seq_queue_status_alloca(&status);
66 	min = queue < 0 ? 0 : queue;
67 	max = queue < 0 ? max_queues : queue + 1;
68 	for (idx = min; idx < max; idx++) {
69 		if ((err = snd_seq_get_queue_status(handle, idx, status))<0) {
70 			if (err == -ENOENT)
71 				continue;
72 			fprintf(stderr, "Client %i info error: %s\n", idx, snd_strerror(err));
73 			exit(0);
74 		}
75 		printf("Queue %i info\n", snd_seq_queue_status_get_queue(status));
76 		printf("  Tick          : %u\n", snd_seq_queue_status_get_tick_time(status));
77 		printf("  Realtime      : %i.%i\n",
78 		       snd_seq_queue_status_get_real_time(status)->tv_sec,
79 		       snd_seq_queue_status_get_real_time(status)->tv_nsec);
80 		printf("  Flags         : 0x%x\n", snd_seq_queue_status_get_status(status));
81 	}
82 }
83 
show_port_info(snd_seq_t * handle,int client,int port)84 void show_port_info(snd_seq_t *handle, int client, int port)
85 {
86 	int err, idx, min, max;
87 	snd_seq_port_info_t *info;
88 
89 	snd_seq_port_info_alloca(&info);
90 	min = port < 0 ? 0 : port;
91 	max = port < 0 ? max_ports : port + 1;
92 	for (idx = min; idx < max; idx++) {
93 		if ((err = snd_seq_get_any_port_info(handle, client, idx, info))<0) {
94 			if (err == -ENOENT)
95 				continue;
96 			fprintf(stderr, "Port %i/%i info error: %s\n", client, idx, snd_strerror(err));
97 			exit(0);
98 		}
99 		printf("  Port %i info\n", idx);
100 		printf("    Client        : %i\n", snd_seq_port_info_get_client(info));
101 		printf("    Port          : %i\n", snd_seq_port_info_get_port(info));
102 		printf("    Name          : %s\n", snd_seq_port_info_get_name(info));
103 		printf("    Capability    : 0x%x\n", snd_seq_port_info_get_capability(info));
104 		printf("    Type          : 0x%x\n", snd_seq_port_info_get_type(info));
105 		//printf("    Midi channels : %i\n", info.midi_channels);
106 		//printf("    Synth voices  : %i\n", info.synth_voices);
107 		printf("    Output subs   : %i\n", snd_seq_port_info_get_write_use(info));
108 		printf("    Input subs    : %i\n", snd_seq_port_info_get_read_use(info));
109 	}
110 }
111 
show_client_info(snd_seq_t * handle,int client)112 void show_client_info(snd_seq_t *handle, int client)
113 {
114 	int err, idx, min, max;
115 	snd_seq_client_info_t *info;
116 
117 	snd_seq_client_info_alloca(&info);
118 	min = client < 0 ? 0 : client;
119 	max = client < 0 ? max_clients : client + 1;
120 	for (idx = min; idx < max; idx++) {
121 		if ((err = snd_seq_get_any_client_info(handle, idx, info))<0) {
122 			if (err == -ENOENT)
123 				continue;
124 			fprintf(stderr, "Client %i info error: %s\n", idx, snd_strerror(err));
125 			exit(0);
126 		}
127 		printf("Client %i info\n", idx);
128 		if (verbose)
129 			printf("  Client        : %i\n", snd_seq_client_info_get_client(info));
130 		printf("  Type          : %s\n", snd_seq_client_info_get_type(info) == SND_SEQ_KERNEL_CLIENT ? "kernel" : "user");
131 		printf("  Name          : %s\n", snd_seq_client_info_get_name(info));
132 	}
133 }
134 
help(void)135 static void help(void)
136 {
137 	printf("Usage: seq <options> command\n");
138 	printf("\nAvailable options:\n");
139 	printf("  -h,--help       this help\n");
140 	printf("  -d,--debug      debug mode\n");
141 	printf("  -v,--verbose    verbose mode\n");
142 	printf("  -V,--version    print version of this program\n");
143 	printf("\nAvailable commands:\n");
144 	printf("  system          show basic sequencer info\n");
145 	printf("  queue [#]       show all queues or specified queue\n");
146 	printf("  client [#]      show all clients or specified client\n");
147 	printf("  port <client> [#]  show all ports or specified port for specified client\n");
148 	printf("  decoder         event decoder\n");
149 	printf("  sender <client.port> [<client.port>] ...   event sender\n");
150 }
151 
main(int argc,char * argv[])152 int main(int argc, char *argv[])
153 {
154 	int morehelp, err, arg, arg1;
155 	snd_seq_t *handle;
156 	static struct option long_option[] =
157 	{
158 		{"help", 0, NULL, HELPID_HELP},
159 		{"debug", 0, NULL, HELPID_DEBUG},
160 		{"verbose", 0, NULL, HELPID_VERBOSE},
161 		{"version", 0, NULL, HELPID_VERSION},
162 		{NULL, 0, NULL, 0},
163         };
164 
165         morehelp = 0;
166 
167 	while (1) {
168 		int c;
169 
170 		if ((c = getopt_long(argc, argv, "hdvV", long_option, NULL)) < 0)
171 			break;
172 		switch (c) {
173 		case 'h':
174 		case HELPID_HELP:
175 			morehelp++;
176 			break;
177 		case 'd':
178 		case HELPID_DEBUG:
179 			debug = 1;
180 			break;
181 		case 'v':
182 		case HELPID_VERBOSE:
183 			verbose = 1;
184 			break;
185 		case 'V':
186 		case HELPID_VERSION:
187 			printf("alsactl version " SEQ_VERSION "\n");
188 			return 1;
189 		default:
190 			fprintf(stderr, "\07Invalid switch or option needs an argument.\n");
191 			morehelp++;
192 		}
193 	}
194         if (morehelp) {
195                 help();
196                 return 1;
197         }
198 	if (argc - optind <= 0) {
199 		fprintf(stderr, "seq: Specify command...\n");
200 		return 0;
201 	}
202 	if ((err = snd_seq_open(&handle, "hw", SND_SEQ_OPEN_DUPLEX, 0))<0) {
203 		fprintf(stderr, "Open error: %s\n", snd_strerror(err));
204 		exit(0);
205 	}
206 	set_name(handle);
207 	system_info(handle);
208 
209         if (!strcmp(argv[optind], "system")) {
210 		show_system_info(handle);
211 	} else if (!strcmp(argv[optind], "queue")) {
212 		arg = argc - optind > 1 ? atoi(argv[optind + 1]) : -1;
213 		show_queue_status(handle, arg);
214 	} else if (!strcmp(argv[optind], "client")) {
215 		arg = argc - optind > 1 ? atoi(argv[optind + 1]) : -1;
216 		show_client_info(handle, arg);
217 	} else if (!strcmp(argv[optind], "port")) {
218 		arg = argc - optind > 1 ? atoi(argv[optind + 1]) : -1;
219 		if (arg < 0) {
220 			fprintf(stderr, "Specify port...\n");
221 			exit(0);
222 		}
223 		arg1 = argc - optind > 2 ? atoi(argv[optind + 2]) : -1;
224 		show_port_info(handle, arg, arg1);
225 	} else if (!strcmp(argv[optind], "decoder")) {
226 		event_decoder(handle, argc - optind - 1, argv + optind + 1);
227 	} else if (!strcmp(argv[optind], "sender")) {
228 		event_sender(handle, argc - optind - 1, argv + optind + 1);
229 	} else {
230 		help();
231 	}
232 	exit(1);
233 }
234