Debian Codegraph @

utils/cli/cmd-list-pcms.c

raw
1 /*
2 * BlueALSA - cmd-list-pcms.c
3 * Copyright (c) 2016-2024 Arkadiusz Bokowy
4 *
5 * This file is a part of bluez-alsa.
6 *
7 * This project is licensed under the terms of the MIT license.
8 *
9 */
10
11 #include <getopt.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 #include <dbus/dbus.h>
16
17 #include "cli.h"
18 #include "shared/dbus-client-pcm.h"
19
20 static void usage(const char *command) {
21 printf("List all BlueALSA PCM paths.\n\n");
22 cli_print_usage("%s [OPTION]...", command);
23 printf("\nOptions:\n"
24 " -h, --help\t\tShow this message and exit\n"
25 );
26 }
27
28 static int cmd_list_pcms_func(int argc, char *argv[]) {
29
30 int opt;
31 const char *opts = "hqv";
32 const struct option longopts[] = {
33 { "help", no_argument, NULL, 'h' },
34 { "quiet", no_argument, NULL, 'q' },
35 { "verbose", no_argument, NULL, 'v' },
36 { 0 },
37 };
38
39 opterr = 0;
40 while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
41 if (cli_parse_common_options(opt))
42 continue;
43 switch (opt) {
44 case 'h' /* --help */ :
45 usage(argv[0]);
46 return EXIT_SUCCESS;
47 default:
48 cmd_print_error("Invalid argument '%s'", argv[optind - 1]);
49 return EXIT_FAILURE;
50 }
51 }
52
53 if (argc != optind) {
54 cmd_print_error("Invalid number of arguments");
55 return EXIT_FAILURE;
56 }
57
58 struct ba_pcm *pcms = NULL;
59 size_t pcms_count = 0;
60
61 DBusError err = DBUS_ERROR_INIT;
62 if (!ba_dbus_pcm_get_all(&config.dbus, &pcms, &pcms_count, &err)) {
63 cmd_print_error("Couldn't get BlueALSA PCM list: %s", err.message);
64 return EXIT_FAILURE;
65 }
66
67 for (size_t i = 0; i < pcms_count; i++) {
68 printf("%s\n", pcms[i].pcm_path);
69 if (config.verbose) {
70 cli_print_pcm_properties(&pcms[i], &err);
71 printf("\n");
72 }
73 }
74
75 free(pcms);
76 return EXIT_SUCCESS;
77 }
78
79 const struct cli_command cmd_list_pcms = {
80 "list-pcms",
81 "List all BlueALSA PCM paths",
82 cmd_list_pcms_func,
83 };