summaryrefslogtreecommitdiffstats
path: root/spa/plugins/alsa/acp-tool.c
blob: 0fc92a4f32b1c22e31c990e8d352d818bffffcd2 (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
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
/* ALSA card profile test
 *
 * Copyright © 2020 Wim Taymans
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <stdbool.h>
#include <getopt.h>

#include <spa/utils/string.h>

#include <acp/acp.h>

#define WHITESPACE	"\n\r\t "

struct data {
	int verbose;
	int card_index;
	char *properties;
	struct acp_card *card;
	bool quit;
};

static void acp_debug_dict(struct acp_dict *dict, int indent)
{
	const struct acp_dict_item *it;
	fprintf(stderr, "%*sproperties: (%d)\n", indent, "", dict->n_items);
	acp_dict_for_each(it, dict) {
		fprintf(stderr, "%*s%s = \"%s\"\n", indent+4, "", it->key, it->value);
	}
}

static char *split_walk(char *str, const char *delimiter, size_t *len, char **state)
{
	char *s = *state ? *state : str;

	if (*s == '\0')
		return NULL;

	*len = strcspn(s, delimiter);
	*state = s + *len;
	*state += strspn(*state, delimiter);
	return s;
}

static int split_ip(char *str, const char *delimiter, int max_tokens, char *tokens[])
{
	char *state = NULL, *s;
	size_t len;
	int n = 0;

	while (true) {
		if ((s = split_walk(str, delimiter, &len, &state)) == NULL)
			break;
		tokens[n++] = s;
		if (n >= max_tokens)
			break;
		s[len] = '\0';
	}
	return n;
}


static void card_props_changed(void *data)
{
	struct data *d = data;
	struct acp_card *card = d->card;
	fprintf(stderr, "*** properties changed:\n");
	acp_debug_dict(&card->props, 4);
	fprintf(stderr, "***\n");
}

static void card_profile_changed(void *data, uint32_t old_index, uint32_t new_index)
{
	struct data *d = data;
	struct acp_card *card = d->card;
	struct acp_card_profile *op = card->profiles[old_index];
	struct acp_card_profile *np = card->profiles[new_index];
	fprintf(stderr, "*** profile changed from %s to %s\n", op->name, np->name);
}

static void card_profile_available(void *data, uint32_t index,
		enum acp_available old, enum acp_available available)
{
	struct data *d = data;
	struct acp_card *card = d->card;
	struct acp_card_profile *p = card->profiles[index];
	fprintf(stderr, "*** profile %s available %s\n", p->name, acp_available_str(available));
}

static void card_port_available(void *data, uint32_t index,
		enum acp_available old, enum acp_available available)
{
	struct data *d = data;
	struct acp_card *card = d->card;
	struct acp_port *p = card->ports[index];
	fprintf(stderr, "*** port %s available %s\n", p->name, acp_available_str(available));
}

static void on_volume_changed(void *data, struct acp_device *dev)
{
	float vol;
	acp_device_get_volume(dev, &vol, 1);
	fprintf(stderr, "*** volume %s changed to %f\n", dev->name, vol);
}

static void on_mute_changed(void *data, struct acp_device *dev)
{
	bool mute;
	acp_device_get_mute(dev, &mute);
	fprintf(stderr, "*** mute %s changed to %d\n", dev->name, mute);
}

static const struct acp_card_events card_events = {
	ACP_VERSION_CARD_EVENTS,
        .props_changed = card_props_changed,
        .profile_changed = card_profile_changed,
        .profile_available = card_profile_available,
        .port_available = card_port_available,
	.volume_changed = on_volume_changed,
	.mute_changed = on_mute_changed,
};

static ACP_PRINTF_FUNC(6,0) void log_func(void *data,
		int level, const char *file, int line, const char *func,
		const char *fmt, va_list arg)
{
	vfprintf(stderr, fmt, arg);
	fprintf(stderr, "\n");
}

static void show_prompt(struct data *data)
{
	fprintf(stderr, ">>>");
}

struct command {
        const char *name;
        const char *args;
        const char *alias;
        const char *description;
        int (*func) (struct data *data, const struct command *cmd, int argc, char *argv[]);
	void *extra;
};

static int cmd_help(struct data *data, const struct command *cmd, int argc, char *argv[]);

static int cmd_quit(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	data->quit = true;
	return 0;
}

static void print_profile(struct data *data, struct acp_card_profile *p, int indent, int level);
static void print_device(struct data *data, struct acp_device *d, int indent, int level);

static void print_port(struct data *data, struct acp_port *p, int indent, int level)
{
	uint32_t i;

	fprintf(stderr, "%*s  %c port %u: name:\"%s\" direction:%s prio:%d (available: %s)\n",
			indent, "", p->flags & ACP_PORT_ACTIVE ? '*' : ' ', p->index,
			p->name, acp_direction_str(p->direction), p->priority,
			acp_available_str(p->available));
	if (level > 0) {
		acp_debug_dict(&p->props, indent + 8);
	}
	if (level > 1) {
		fprintf(stderr, "%*sprofiles: (%d)\n", indent+8, "", p->n_profiles);
		for (i = 0; i < p->n_profiles; i++) {
			struct acp_card_profile *pr = p->profiles[i];
			print_profile(data, pr, indent + 8, 0);
		}
		fprintf(stderr, "%*sdevices: (%d)\n", indent+8, "", p->n_devices);
		for (i = 0; i < p->n_devices; i++) {
			struct acp_device *d = p->devices[i];
			print_device(data, d, indent + 8, 0);
		}
	}
}

static void print_device(struct data *data, struct acp_device *d, int indent, int level)
{
	const char **s;
	uint32_t i;

	fprintf(stderr, "%*s  %c device %u: direction:%s name:\"%s\" prio:%d flags:%08x devices: ",
			indent, "", d->flags & ACP_DEVICE_ACTIVE ? '*' : ' ', d->index,
			acp_direction_str(d->direction), d->name, d->priority, d->flags);
	for (s = d->device_strings; *s; s++)
		fprintf(stderr, "\"%s\" ", *s);
	fprintf(stderr, "\n");
	if (level > 0) {
		fprintf(stderr, "%*srate:%d channels:%d\n", indent+8, "",
				d->format.rate_mask, d->format.channels);
		acp_debug_dict(&d->props, indent + 8);
	}
	if (level > 1) {
		fprintf(stderr, "%*sports: (%d)\n", indent+8, "", d->n_ports);
		for (i = 0; i < d->n_ports; i++) {
			struct acp_port *p = d->ports[i];
			print_port(data, p, indent + 8, 0);
		}
	}
}

static void print_profile(struct data *data, struct acp_card_profile *p, int indent, int level)
{
	uint32_t i;

	fprintf(stderr, "%*s  %c profile %u: name:\"%s\" prio:%d (available: %s)\n",
			indent, "", p->flags & ACP_PROFILE_ACTIVE ? '*' : ' ', p->index,
			p->name, p->priority, acp_available_str(p->available));
	if (level > 0) {
		fprintf(stderr, "%*sdescription:\"%s\"\n",
				indent+8, "", p->description);
	}
	if (level > 1) {
		fprintf(stderr, "%*sdevices: (%d)\n", indent+8, "", p->n_devices);
		for (i = 0; i < p->n_devices; i++) {
			struct acp_device *d = p->devices[i];
			print_device(data, d, indent + 8, 0);
		}
	}
}

static void print_card(struct data *data, struct acp_card *card, int indent, int level)
{
	fprintf(stderr, "%*scard %d: profiles:%d devices:%d ports:%d\n", indent, "",
			card->index, card->n_profiles, card->n_devices, card->n_ports);
	if (level > 0) {
		acp_debug_dict(&card->props, 4);
	}
}

static int cmd_info(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	struct acp_card *card = data->card;
	print_card(data, card, 0, 2);
	return 0;
}

static int cmd_card(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	if (argc < 2) {
		fprintf(stderr, "arguments: <card_index> missing\n");
		return -EINVAL;
	}
	return 0;
}

static int cmd_list(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	struct acp_card *card = data->card;
	uint32_t i;
	int level = 0;

	if (spa_streq(cmd->name, "list-verbose"))
		level = 2;

	print_card(data, card, 0, level);
	for (i = 0; i < card->n_profiles; i++)
		print_profile(data, card->profiles[i], 0, level);

	for (i = 0; i < card->n_ports; i++)
		print_port(data, card->ports[i], 0, level);

	for (i = 0; i < card->n_devices; i++)
		print_device(data, card->devices[i], 0, level);

	return 0;
}

static int cmd_list_profiles(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	uint32_t i;
	struct acp_card *card = data->card;

	if (argc > 1) {
		i = atoi(argv[1]);
		if (i >= card->n_profiles)
			return -EINVAL;
		print_profile(data, card->profiles[i], 0, 2);
	} else {
		for (i = 0; i < card->n_profiles; i++)
			print_profile(data, card->profiles[i], 0, 0);
	}
	return 0;
}

static int cmd_set_profile(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	struct acp_card *card = data->card;
	uint32_t index;

	if (argc > 1)
		index = atoi(argv[1]);
	else
		index = card->active_profile_index;

	return acp_card_set_profile(card, index, 0);
}

static int cmd_list_ports(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	uint32_t i;
	struct acp_card *card = data->card;

	if (argc > 1) {
		i = atoi(argv[1]);
		if (i >= card->n_ports)
			return -EINVAL;
		print_port(data, card->ports[i], 0, 2);
	} else {
		for (i = 0; i < card->n_ports; i++)
			print_port(data, card->ports[i], 0, 0);
	}
	return 0;
}

static int cmd_set_port(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	struct acp_card *card = data->card;
	uint32_t dev_id, port_id;

	if (argc < 3) {
		fprintf(stderr, "arguments: <device_id> <port_id> missing\n");
		return -EINVAL;
	}
	dev_id = atoi(argv[1]);
	port_id = atoi(argv[2]);

	if (dev_id >= card->n_devices)
		return -EINVAL;

	return acp_device_set_port(card->devices[dev_id], port_id, 0);
}

static int cmd_list_devices(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	uint32_t i;
	struct acp_card *card = data->card;

	if (argc > 1) {
		i = atoi(argv[1]);
		if (i >= card->n_devices)
			return -EINVAL;
		print_device(data, card->devices[i], 0, 2);
	} else {
		for (i = 0; i < card->n_devices; i++)
			print_device(data, card->devices[i], 0, 0);
	}
	return 0;
}

static int cmd_get_volume(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	struct acp_card *card = data->card;
	uint32_t dev_id;
	float vol;

	if (argc < 2) {
		fprintf(stderr, "arguments: <device_id> missing\n");
		return -EINVAL;
	}
	dev_id = atoi(argv[1]);
	if (dev_id >= card->n_devices)
		return -EINVAL;

	acp_device_get_volume(card->devices[dev_id], &vol, 1);

	fprintf(stderr, "volume: %f\n", vol);
	return 0;
}

static int cmd_set_volume(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	struct acp_card *card = data->card;
	uint32_t dev_id;
	float vol;

	if (argc < 3) {
		fprintf(stderr, "arguments: <device_id> <volume> missing\n");
		return -EINVAL;
	}
	dev_id = atoi(argv[1]);
	vol = atof(argv[2]);

	if (dev_id >= card->n_devices)
		return -EINVAL;

	return acp_device_set_volume(card->devices[dev_id], &vol, 1);
}

static int adjust_volume(struct data *data, const struct command *cmd, int argc, char *argv[], float adjust)
{
	struct acp_card *card = data->card;
	uint32_t dev_id;
	float vol;

	if (argc < 2) {
		fprintf(stderr, "arguments: <device_id> missing\n");
		return -EINVAL;
	}
	dev_id = atoi(argv[1]);
	if (dev_id >= card->n_devices)
		return -EINVAL;
	acp_device_get_volume(card->devices[dev_id], &vol, 1);
	vol += adjust;
	acp_device_set_volume(card->devices[dev_id], &vol, 1);
	fprintf(stderr, "volume: %f\n", vol);
	return 0;
}

static int cmd_inc_volume(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	return adjust_volume(data, cmd, argc, argv, 0.2);
}

static int cmd_dec_volume(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	return adjust_volume(data, cmd, argc, argv, -0.2);
}

static int cmd_get_mute(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	struct acp_card *card = data->card;
	uint32_t dev_id;
	bool mute;

	if (argc < 2) {
		fprintf(stderr, "arguments: <device_id> missing\n");
		return -EINVAL;
	}
	dev_id = atoi(argv[1]);
	if (dev_id >= card->n_devices)
		return -EINVAL;

	acp_device_get_mute(card->devices[dev_id], &mute);

	fprintf(stderr, "muted: %s\n", mute ? "yes" : "no");
	return 0;
}

static int cmd_set_mute(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	struct acp_card *card = data->card;
	uint32_t dev_id;
	bool mute;

	if (argc < 3) {
		fprintf(stderr, "arguments: <device_id> <mute> missing\n");
		return -EINVAL;
	}
	dev_id = atoi(argv[1]);
	mute = atoi(argv[2]);
	if (dev_id >= card->n_devices)
		return -EINVAL;

	acp_device_set_mute(card->devices[dev_id], mute);
	fprintf(stderr, "muted: %s\n", mute ? "yes" : "no");
	return 0;
}

static int cmd_toggle_mute(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	struct acp_card *card = data->card;
	uint32_t dev_id;
	bool mute;

	if (argc < 2) {
		fprintf(stderr, "arguments: <device_id> missing\n");
		return -EINVAL;
	}
	dev_id = atoi(argv[1]);
	if (dev_id >= card->n_devices)
		return -EINVAL;
	acp_device_get_mute(card->devices[dev_id], &mute);
	mute = !mute;
	acp_device_set_mute(card->devices[dev_id], mute);
	fprintf(stderr, "muted: %s\n", mute ? "yes" : "no");
	return 0;
}

static const struct command command_list[] = {
	{ "help",          "",           "h",   "Show available commands", cmd_help },
	{ "quit",          "",           "q",   "Quit", cmd_quit },
	{ "card",          "<id>",       "c",   "Probe card", cmd_card },
	{ "info",          "",           "i",   "List card info", cmd_info },
	{ "list",          "",           "l",   "List all objects", cmd_list },
	{ "list-verbose",  "",           "lv",  "List all data", cmd_list },
	{ "list-profiles", "[id]",       "lpr", "List profiles", cmd_list_profiles },
	{ "set-profile",   "<id>",       "spr", "Activate a profile", cmd_set_profile },
	{ "list-ports",    "[id]",       "lp",  "List ports", cmd_list_ports },
	{ "set-port",      "<id>",       "sp",  "Activate a port", cmd_set_port },
	{ "list-devices",  "[id]",       "ld",  "List available devices", cmd_list_devices },
	{ "get-volume",    "<id>",       "gv",  "Get volume from device", cmd_get_volume },
	{ "set-volume",    "<id> <vol>", "v",   "Set volume on device", cmd_set_volume },
	{ "inc-volume",    "<id>",       "v+",  "Increase volume on device", cmd_inc_volume },
	{ "dec-volume",    "<id>",       "v-",  "Decrease volume on device", cmd_dec_volume },
	{ "get-mute",      "<id>",       "gm",  "Get mute state from device", cmd_get_mute },
	{ "set-mute",      "<id> <val>", "sm",  "Set mute on device", cmd_set_mute },
	{ "toggle-mute",   "<id>",       "m",   "Toggle mute on device", cmd_toggle_mute },
};
#define N_COMMANDS	sizeof(command_list)/sizeof(command_list[0])

static const struct command *find_command(struct data *data, const char *cmd)
{
	size_t i;
	for (i = 0; i < N_COMMANDS; i++) {
		if (spa_streq(command_list[i].name, cmd) ||
		    spa_streq(command_list[i].alias, cmd))
			return &command_list[i];
	}
	return NULL;
}

static int cmd_help(struct data *data, const struct command *cmd, int argc, char *argv[])
{
	size_t i;
	fprintf(stderr, "Available commands:\n");
	for (i = 0; i < N_COMMANDS; i++) {
		fprintf(stdout, "\t%-15.15s %-10.10s\t%s (%s)\n",
				command_list[i].name,
				command_list[i].args,
				command_list[i].description,
				command_list[i].alias);
	}
	return 0;
}

static int run_command(struct data *data, int argc, char *argv[64])
{
	const struct command *command;
	int res;

	command = find_command(data, argv[0]);
	if (command == NULL) {
		fprintf(stderr, "unknown command %s\n", argv[0]);
		cmd_help(data, NULL, argc, argv);
		res = -EINVAL;
	} else if (command->func) {
		res = command->func(data, command, argc, argv);
		if (res < 0) {
			fprintf(stderr, "error: %s\n", strerror(-res));
		}
	} else {
		res = -ENOTSUP;
	}
	return res;
}

static int handle_input(struct data *data)
{
	char buf[4096] = { 0, }, *p, *argv[64];
	ssize_t r;
	int res, argc;

	if ((r = read(STDIN_FILENO, buf, sizeof(buf)-1)) < 0)
		return -errno;
	buf[r] = 0;

	if (r == 0)
		return -EPIPE;

	if ((p = strchr(buf, '#')))
                *p = '\0';

	argc = split_ip(buf, WHITESPACE, 64, argv);
	if (argc < 1)
		return -EINVAL;

	res = run_command(data, argc, argv);

	if (!data->quit)
		show_prompt(data);

	return res;
}

static int do_probe(struct data *data)
{
	uint32_t n_items = 0;
	struct acp_dict_item items[64];
	struct acp_dict props;

	acp_set_log_func(log_func, data);
	acp_set_log_level(data->verbose);

	items[n_items++] = ACP_DICT_ITEM_INIT("use-ucm", "true");
	items[n_items++] = ACP_DICT_ITEM_INIT("verbose", data->verbose ? "true" : "false");
	if (data->properties != NULL) {
		char *p = data->properties, *e, f;

		while (*p) {
			const char *k, *v;

			if ((e = strchr(p, '=')) == NULL)
				break;
			*e = '\0';
			k = p;
			p = e+1;

			if (*p == '\"') {
				p++;
				f = '\"';
			} else {
				f = ' ';
			}
			if ((e = strchr(p, f)) == NULL &&
			    (e = strchr(p, '\0')) == NULL)
				break;
			*e = '\0';
			v = p;
			p = e+1;
			items[n_items++] = ACP_DICT_ITEM_INIT(k, v);
			if (n_items == 64)
				break;
		}
	}
	props = ACP_DICT_INIT(items, n_items);

	data->card = acp_card_new(data->card_index, &props);
	if (data->card == NULL)
		return -errno;
	return 0;
}

static int do_prompt(struct data *data)
{
	struct pollfd *pfds;
	int err, count;

	acp_card_add_listener(data->card, &card_events, data);

	count = acp_card_poll_descriptors_count(data->card);
	if (count == 0)
		fprintf(stderr, "card has no events\n");

	count++;
	pfds = alloca(sizeof(struct pollfd) * count);
	pfds[0].fd = STDIN_FILENO;
	pfds[0].events = POLLIN;

	print_card(data, data->card, 0, 0);

	fprintf(stderr, "type 'help' for usage.\n");
	show_prompt(data);

	while (!data->quit) {
		unsigned short revents;

		err = acp_card_poll_descriptors(data->card, &pfds[1], count-1);
		if (err < 0)
			return err;

		err = poll(pfds, (unsigned int) count, -1);
		if (err < 0)
			return -errno;

		if (pfds[0].revents & POLLIN) {
			if ((err = handle_input(data)) < 0) {
				if (err == -EPIPE)
					break;
			}
		}

		if (count < 2)
			continue;

		err = acp_card_poll_descriptors_revents(data->card, &pfds[1], count-1, &revents);
		if (err < 0)
			return err;

		if (revents)
			acp_card_handle_events(data->card);
	}
	return 0;
}

#define OPTIONS		"hvc:p:"
static const struct option long_options[] = {
	{ "help",	no_argument,		NULL, 'h'},
	{ "verbose",	no_argument,		NULL, 'v'},

	{ "card",	required_argument,	NULL, 'c' },
	{ "properties",	required_argument,	NULL, 'p' },

        { NULL, 0, NULL, 0 }
};

static void show_usage(struct data *data, const char *name, bool is_error)
{
	FILE *fp;

	fp = is_error ? stderr : stdout;

	fprintf(fp, "%s [options] [COMMAND]\n", name);
	fprintf(fp,
		"  -h, --help                            Show this help\n"
		"  -v  --verbose                         Be verbose\n"
		"  -c  --card                            Card number\n"
		"  -p  --properties                      Extra properties:\n"
		"                                         'key=value ... '\n"
		"\n");
	cmd_help(data, NULL, 0, NULL);
}

int main(int argc, char *argv[])
{
	int c, res;
	int longopt_index = 0, ret;
	struct data data = { 0, };

	data.verbose = 1;

	while ((c = getopt_long(argc, argv, OPTIONS, long_options, &longopt_index)) != -1) {
		switch (c) {
		case 'h':
                        show_usage(&data, argv[0], false);
                        return EXIT_SUCCESS;
		case 'v':
			data.verbose++;
			break;
		case 'c':
			ret = atoi(optarg);
			if (ret < 0) {
				fprintf(stderr, "error: bad card %s\n", optarg);
                                goto error_usage;
			}
			data.card_index = ret;
			break;
		case 'p':
			data.properties = strdup(optarg);
			break;
                default:
			fprintf(stderr, "error: unknown option '%c'\n", c);
			goto error_usage;
		}
	}

	if ((res = do_probe(&data)) < 0) {
		fprintf(stderr, "failed to probe card: %s\n", strerror(-res));
		return res;
	}

	if (optind < argc)
		run_command(&data, argc - optind, &argv[optind]);
	else
		do_prompt(&data);

	if (data.card)
		acp_card_destroy(data.card);

	free(data.properties);

	return 0;

error_usage:
	show_usage(&data, argv[0], true);
	return EXIT_FAILURE;
}