Debian Codegraph @

utils/cli/cmd-mute.c

raw
1 /*
2 * BlueALSA - cmd-mute.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 mute switch of the given PCM.\n\n");
23 cli_print_usage("%s [OPTION]... PCM-PATH [STATE [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 mute switch\n"
29 );
30 }
31
32 static int cmd_mute_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 > 3) {
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_mute(&pcm);
77 return EXIT_SUCCESS;
78 }
79
80 const char *value;
81 bool state;
82
83 value = argv[optind + 1];
84 if (!cli_parse_value_on_off(value, &state)) {
85 cmd_print_error("Invalid argument: %s", value);
86 return EXIT_FAILURE;
87 }
88
89 pcm.volume.ch1_muted = state;
90 pcm.volume.ch2_muted = state;
91
92 if (pcm.channels == 2 && argc - optind == 3) {
93
94 value = argv[optind + 2];
95 if (!cli_parse_value_on_off(value, &state)) {
96 cmd_print_error("Invalid argument: %s", value);
97 return EXIT_FAILURE;
98 }
99
100 pcm.volume.ch2_muted = state;
101
102 }
103
104 if (!ba_dbus_pcm_update(&config.dbus, &pcm, BLUEALSA_PCM_VOLUME, &err)) {
105 cmd_print_error("Volume mute update failed: %s", err.message);
106 return EXIT_FAILURE;
107 }
108
109 return EXIT_SUCCESS;
110 }
111
112 const struct cli_command cmd_mute = {
113 "mute",
114 "Get or set PCM mute switch",
115 cmd_mute_func,
116 };