summaryrefslogtreecommitdiff
path: root/alsaucm/usecase.c
blob: bb894c6e270c9bf55e5e43e517ec46afc82f0951 (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
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
/*
 *  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 of the License, or (at your option) any later version.
 *
 *  This library 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 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.
 *
 *  Support for the verb/device/modifier core logic and API, 
 *  command line tool and file parser was kindly sponsored by 
 *  Texas Instruments Inc.
 *  Support for multiple active modifiers and devices, 
 *  transition sequences, multiple client access and user defined use
 *  cases was kindly sponsored by Wolfson Microelectronics PLC.
 * 
 *  Copyright (C) 2008-2010 SlimLogic Ltd
 *  Copyright (C) 2010 Wolfson Microelectronics PLC
 *  Copyright (C) 2010 Texas Instruments Inc.
 *  Copyright (C) 2010 Red Hat Inc.
 *  Authors: Liam Girdwood <lrg@slimlogic.co.uk>
 *           Stefan Schmidt <stefan@slimlogic.co.uk>
 *           Justin Xu <justinx@slimlogic.co.uk>
 *           Jaroslav Kysela <perex@perex.cz>
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <getopt.h>
#include <alsa/asoundlib.h>
#include <alsa/use-case.h>
#include "aconfig.h"
#include "version.h"

#define MAX_BUF 256

struct context {
	snd_use_case_mgr_t *uc_mgr;
	const char *command;
	char *card;
	char **argv;
	int argc;
	unsigned int interactive:1;
	unsigned int no_open:1;
	unsigned int do_exit:1;
};

enum uc_cmd {
	/* management */
	OM_UNKNOWN = 0,
	OM_OPEN,
	OM_RESET,
	OM_RELOAD,
	OM_LISTCARDS,
	OM_LIST,

	/* set/get */
	OM_SET,
	OM_GET,
	OM_GETI,

	/* misc */
	OM_HELP,
	OM_QUIT,
};

struct cmd {
	int code;
	int args;
	unsigned int opencard:1;
	const char *id;
};

static struct cmd cmds[] = {
	{ OM_OPEN, 1, 0, "open" },
	{ OM_RESET, 0, 1, "reset" },
	{ OM_RELOAD, 0, 1, "reload" },
	{ OM_LISTCARDS, 0, 0, "listcards" },
	{ OM_LIST, 0, 1, "list" },
	{ OM_SET, 2, 1, "set" },
	{ OM_GET, 1, 1, "get" },
	{ OM_GETI, 1, 1, "geti" },
	{ OM_HELP, 0, 0, "help" },
	{ OM_QUIT, 0, 0, "quit" },
	{ OM_HELP, 0, 0, "h" },
	{ OM_QUIT, 0, 0, "q" },
	{ OM_UNKNOWN, 0, 0, NULL }
};

static void dump_help(struct context *context)
{
	if (context->command)
		printf("Usage: %s <options> [command]\n", context->command);
	printf(
"\nAvailable options:\n"
"  -h,--help                  this help\n"
"  -c,--card NAME             open card NAME\n"
"  -i,--interactive           interactive mode\n"
"  -n,--no-open               do not open first card found\n"
"\nAvailable commands:\n"
"  open NAME                  open card NAME\n"
"  reset                      reset sound card to default state\n"
"  reload                     reload configuration\n"
"  listcards                  list available cards\n"
"  list IDENTIFIER            list command\n"
"  get IDENTIFIER             get string value\n"
"  geti IDENTIFIER            get integer value\n"
"  set IDENTIFIER VALUE       set string value\n"
"  h,help                     help\n"
"  q,quit                     quit\n"
);
}

static int parse_line(struct context *context, char *line)
{
	char *start;
	int c;

	context->argc = 0;
	while (*line) {
		while (*line && (*line == ' ' || *line == '\t' ||
							*line == '\n'))
			line++;
		c = *line;
		if (c == '\"' || c == '\'') {
			start = ++line;
			while (*line && *line != c)
				line++;
			if (*line) {
				*line = '\0';
				line++;
			}
		} else {
			start = line;
			while (*line && *line != ' ' && *line != '\t' &&
			       *line != '\n')
				line++;
			if (*line) {
				*line = '\0';
				line++;
			}
		}
		context->argv[context->argc++] = start;
	}
	return 0;
}

static int do_one(struct context *context, struct cmd *cmd, char **argv)
{
	const char **list, *str;
	long lval;
	int err, i;

	if (cmd->opencard && context->uc_mgr == NULL) {
		fprintf(stderr, "%s: command '%s' requires an open card\n",
				context->command, cmd->id);
		return 0;
	}
	switch (cmd->code) {
	case OM_OPEN:
		if (context->uc_mgr)
			snd_use_case_mgr_close(context->uc_mgr);
		context->uc_mgr = NULL;
		free(context->card);
		context->card = strdup(argv[0]);
		err = snd_use_case_mgr_open(&context->uc_mgr, context->card);
		if (err < 0) {
			fprintf(stderr,
				"%s: error failed to open sound card %s: %s\n",
				context->command, context->card,
				snd_strerror(err));
			return err;
		}
		break;
	case OM_RESET:
		err = snd_use_case_mgr_reset(context->uc_mgr);
		if (err < 0) {
			fprintf(stderr,
				"%s: error failed to reset sound card %s: %s\n",
				context->command, context->card,
				snd_strerror(err));
			return err;
		}
		break;
	case OM_RELOAD:
		err = snd_use_case_mgr_reload(context->uc_mgr);
		if (err < 0) {
			fprintf(stderr,
				"%s: error failed to reload manager %s: %s\n",
				context->command, context->card,
				snd_strerror(err));
			return err;
		}
		break;
	case OM_LISTCARDS:
		err = snd_use_case_card_list(&list);
		if (err < 0) {
			fprintf(stderr,
				"%s: error failed to get card list: %s\n",
				context->command,
				snd_strerror(err));
			return err;
		}
		if (err == 0)
			printf("  list is empty\n");
		for (i = 0; i < err / 2; i++) {
			printf("  %i: %s\n", i, list[i*2]);
			if (list[i*2+1])
				printf("    %s\n", list[i*2+1]);
		}
		snd_use_case_free_list(list, err);
		break;
	case OM_LIST:
		err = snd_use_case_get_list(context->uc_mgr,
					    argv[0],
					    &list);
		if (err < 0) {
			fprintf(stderr,
				"%s: error failed to get list %s: %s\n",
				context->command, argv[0],
				snd_strerror(err));
			return err;
		}
		if (err == 0)
			printf("  list is empty\n");
		for (i = 0; i < err; i++)
			printf("  %i: %s\n", i, list[i]);
		snd_use_case_free_list(list, err);
		break;
	case OM_SET:
		err = snd_use_case_set(context->uc_mgr, argv[0], argv[1]);
		if (err < 0) {
			fprintf(stderr,
				"%s: error failed to set %s=%s: %s\n",
				context->command, argv[0], argv[1],
				snd_strerror(err));
			return err;
		}
		break;
	case OM_GET:
		err = snd_use_case_get(context->uc_mgr, argv[0], &str);
		if (err < 0) {
			fprintf(stderr,
				"%s: error failed to get %s: %s\n",
				context->command, argv[0],
				snd_strerror(err));
			return err;
		}
		printf("  %s=%s\n", argv[0], str);
		free((void *)str);
		break;
	case OM_GETI:
		err = snd_use_case_geti(context->uc_mgr, argv[0], &lval);
		if (err < 0) {
			fprintf(stderr,
				"%s: error failed to get integer %s: %s\n",
				context->command, argv[0],
				snd_strerror(err));
			return lval;
		}
		printf("  %s=%li\n", argv[0], lval);
		break;
	case OM_QUIT:
		context->do_exit = 1;
		break;
	case OM_HELP:
		dump_help(context);
		break;
	default:
		fprintf(stderr, "%s: unimplemented command '%s'\n",
				context->command, cmd->id);
		return -EINVAL;
	}
	return 0;
}

static int do_commands(struct context *context)
{
	char *command, **argv;
	struct cmd *cmd;
	int i, acnt, err;

	for (i = 0; i < context->argc && !context->do_exit; i++) {
		command = context->argv[i];
		for (cmd = cmds; cmd->id != NULL; cmd++) {
			if (strcmp(cmd->id, command) == 0)
				break;
		}
		if (cmd->id == NULL) {
			fprintf(stderr, "%s: unknown command '%s'\n",
						context->command, command);
			return -EINVAL;
		}
		acnt = context->argc - (i + 1);
		if (acnt < cmd->args) {
			fprintf(stderr, "%s: expected %i arguments (got %i)\n",
					context->command, cmd->args, acnt);
			return -EINVAL;
		}
		argv = context->argv + i + 1;
		err = do_one(context, cmd, argv);
		if (err < 0)
			return err;
		i += cmd->args;
	}
	return 0;
}

static void my_exit(struct context *context, int exitcode)
{
	if (context->uc_mgr)
		snd_use_case_mgr_close(context->uc_mgr);
	free(context);
	exit(exitcode);
}

enum {
	OPT_VERSION = 1,
};

int main(int argc, char *argv[])
{
	static const char short_options[] = "hc:in";
	static const struct option long_options[] = {
		{"help", 0, 0, 'h'},
		{"version", 0, 0, OPT_VERSION},
		{"card", 1, 0, 'c'},
		{"interactive", 0, 0, 'i'},
		{"no-open", 0, 0, 'n'},
		{0, 0, 0, 0}
	};
	struct context *context;
	const char *command = argv[0];
	const char **list;
	int c, err, option_index;
	char cmd[MAX_BUF];

	context = calloc(1, sizeof(*context));
	if (context == NULL)
		return EXIT_FAILURE;
	context->command = command;
	while ((c = getopt_long(argc, argv, short_options,
				 long_options, &option_index)) != -1) {
		switch (c) {
		case 'h':
			dump_help(context);
			break;
		case OPT_VERSION:
			printf("%s: version " SND_UTIL_VERSION_STR "\n", command);
			break;
		case 'c':
			if (context->card)
				free(context->card);
			context->card = strdup(optarg);
			break;
		case 'i':
			context->interactive = 1;
			break;
		case 'n':
			context->no_open = 1;
			break;
		default:
			fprintf(stderr, "Try '%s --help' for more information.\n", command);
			my_exit(context, EXIT_FAILURE);
		}
	}

	if (!context->no_open && context->card == NULL) {
		err = snd_use_case_card_list(&list);
		if (err < 0) {
			fprintf(stderr, "%s: unable to obtain card list: %s\n", command, snd_strerror(err));
			my_exit(context, EXIT_FAILURE);
		}
		if (err == 0) {
			printf("No card found\n");
			my_exit(context, EXIT_SUCCESS);
		}
		context->card = strdup(list[0]);
		snd_use_case_free_list(list, err);
	}

	/* open library */
	if (!context->no_open) {
		err = snd_use_case_mgr_open(&context->uc_mgr,
					    context->card);
		if (err < 0) {
			fprintf(stderr,
				"%s: error failed to open sound card %s: %s\n",
				command, context->card, snd_strerror(err));
			my_exit(context, EXIT_FAILURE);
		}
	}

	/* parse and execute any command line commands */
	if (argc > optind) {
		context->argv = argv + optind;
		context->argc = argc - optind;
		err = do_commands(context);
		if (err < 0)
			my_exit(context, EXIT_FAILURE);
	}

	if (!context->interactive)
		my_exit(context, EXIT_SUCCESS);

	printf("%s: Interacive mode - 'q' to quit\n", command);
	/* run the interactive command parser and handler */
	while (!context->do_exit) {
		printf("%s>> ", argv[0]);
		fflush(stdin);
		if (fgets(cmd, MAX_BUF, stdin) == NULL)
			break;
		err = parse_line(context, cmd);
		if (err < 0) {
			fprintf(stderr, "%s: unable to parse line\n",
				command);
			my_exit(context, EXIT_FAILURE);
		}
		err = do_commands(context);
		if (err < 0)
			my_exit(context, EXIT_FAILURE);
	}

	my_exit(context, EXIT_SUCCESS);
	return EXIT_SUCCESS;
}