Debian Codegraph @

utils/cli/cmd-softvol.c

raw
1 /*
2 * BlueALSA - cmd-softvol.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 <stdbool.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 #include <dbus/dbus.h>
17
18 #include "cli.h"
19 #include "shared/dbus-client-pcm.h"
20
21 static void usage(const char *command) {
22 printf("Get or set the SoftVolume property of the given PCM.\n\n");
23 cli_print_usage("%s [OPTION]... PCM-PATH [STATE]", command);
24 printf("\nOptions:\n"
25 " -h, --help\t\tShow this message and exit\n"
26 "\nPositional arguments:\n"
27 " PCM-PATH\tBlueALSA PCM D-Bus object path\n"
28 " STATE\t\tEnable or disable SoftVolume property\n"
29 );
30 }
31
32 static int cmd_softvol_func(int argc, char *argv[]) {
33
34 int opt;
35 const char *opts = "hqv";
36 const struct option longopts[] = {
37 { "help", no_argument, NULL, 'h' },
38 { "quiet", no_argument, NULL, 'q' },
39 { "verbose", no_argument, NULL, 'v' },
40 { 0 },
41 };
42
43 opterr = 0;
44 while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
45 if (cli_parse_common_options(opt))
46 continue;
47 switch (opt) {
48 case 'h' /* --help */ :
49 usage(argv[0]);
50 return EXIT_SUCCESS;
51 default:
52 cmd_print_error("Invalid argument '%s'", argv[optind - 1]);
53 return EXIT_FAILURE;
54 }
55 }
56
57 if (argc - optind < 1) {
58 cmd_print_error("Missing BlueALSA PCM path argument");
59 return EXIT_FAILURE;
60 }
61 if (argc - optind > 2) {
62 cmd_print_error("Invalid number of arguments");
63 return EXIT_FAILURE;
64 }
65
66 DBusError err = DBUS_ERROR_INIT;
67 const char *path = argv[optind];
68
69 struct ba_pcm pcm;
70 if (!cli_get_ba_pcm(path, &pcm, &err)) {
71 cmd_print_error("Couldn't get BlueALSA PCM: %s", err.message);
72 return EXIT_FAILURE;
73 }
74
75 if (argc - optind == 1) {
76 cli_print_pcm_soft_volume(&pcm);
77 return EXIT_SUCCESS;
78 }
79
80 bool state;
81 const char *value = argv[optind + 1];
82 if (!cli_parse_value_on_off(value, &state)) {
83 cmd_print_error("Invalid argument: %s", value);
84 return EXIT_FAILURE;
85 }
86
87 pcm.soft_volume = state;
88
89 if (!ba_dbus_pcm_update(&config.dbus, &pcm, BLUEALSA_PCM_SOFT_VOLUME, &err)) {
90 cmd_print_error("SoftVolume update failed: %s", err.message);
91 return EXIT_FAILURE;
92 }
93
94 return EXIT_SUCCESS;
95 }
96
97 const struct cli_command cmd_softvol = {
98 "soft-volume",
99 "Get or set PCM SoftVolume property",
100 cmd_softvol_func,
101 };