Debian Codegraph @

utils/cli/cmd-volume.c

raw
1 /*
2 * BlueALSA - cmd-volume.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("Get or set the volume value of the given PCM.\n\n");
22 cli_print_usage("%s [OPTION]... PCM-PATH [VOLUME [VOLUME]]", command);
23 printf("\nOptions:\n"
24 " -h, --help\t\tShow this message and exit\n"
25 "\nPositional arguments:\n"
26 " PCM-PATH\tBlueALSA PCM D-Bus object path\n"
27 " VOLUME\tVolume value (range depends on BT transport)\n"
28 );
29 }
30
31 static int cmd_volume_func(int argc, char *argv[]) {
32
33 int opt;
34 const char *opts = "hqv";
35 const struct option longopts[] = {
36 { "help", no_argument, NULL, 'h' },
37 { "quiet", no_argument, NULL, 'q' },
38 { "verbose", no_argument, NULL, 'v' },
39 { 0 },
40 };
41
42 opterr = 0;
43 while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
44 if (cli_parse_common_options(opt))
45 continue;
46 switch (opt) {
47 case 'h' /* --help */ :
48 usage(argv[0]);
49 return EXIT_SUCCESS;
50 default:
51 cmd_print_error("Invalid argument '%s'", argv[optind - 1]);
52 return EXIT_FAILURE;
53 }
54 }
55
56 if (argc - optind < 1) {
57 cmd_print_error("Missing BlueALSA PCM path argument");
58 return EXIT_FAILURE;
59 }
60 if (argc - optind > 3) {
61 cmd_print_error("Invalid number of arguments");
62 return EXIT_FAILURE;
63 }
64
65 DBusError err = DBUS_ERROR_INIT;
66 const char *path = argv[optind];
67
68 struct ba_pcm pcm;
69 if (!cli_get_ba_pcm(path, &pcm, &err)) {
70 cmd_print_error("Couldn't get BlueALSA PCM: %s", err.message);
71 return EXIT_FAILURE;
72 }
73
74 if (argc - optind == 1) {
75 cli_print_pcm_volume(&pcm);
76 return EXIT_SUCCESS;
77 }
78
79 int vol1, vol2;
80 vol1 = vol2 = atoi(argv[optind + 1]);
81 if (argc - optind == 3)
82 vol2 = atoi(argv[optind + 2]);
83
84 if (pcm.transport & BA_PCM_TRANSPORT_MASK_A2DP) {
85 if (vol1 < 0 || vol1 > 127) {
86 cmd_print_error("Invalid volume [0, 127]: %d", vol1);
87 return EXIT_FAILURE;
88 }
89 pcm.volume.ch1_volume = vol1;
90 if (pcm.channels == 2) {
91 if (vol2 < 0 || vol2 > 127) {
92 cmd_print_error("Invalid volume [0, 127]: %d", vol2);
93 return EXIT_FAILURE;
94 }
95 pcm.volume.ch2_volume = vol2;
96 }
97 }
98 else {
99 if (vol1 < 0 || vol1 > 15) {
100 cmd_print_error("Invalid volume [0, 15]: %d", vol1);
101 return EXIT_FAILURE;
102 }
103 pcm.volume.ch1_volume = vol1;
104 }
105
106 if (!ba_dbus_pcm_update(&config.dbus, &pcm, BLUEALSA_PCM_VOLUME, &err)) {
107 cmd_print_error("Volume loudness update failed: %s", err.message);
108 return EXIT_FAILURE;
109 }
110
111 return EXIT_SUCCESS;
112 }
113
114 const struct cli_command cmd_volume = {
115 "volume",
116 "Get or set PCM audio volume",
117 cmd_volume_func,
118 };