| 1 |
/* |
| 2 |
* BlueALSA - cmd-delay-adjustment.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 <errno.h> |
| 12 |
#include <getopt.h> |
| 13 |
#include <math.h> |
| 14 |
#include <stdint.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
|
| 18 |
#include <dbus/dbus.h> |
| 19 |
|
| 20 |
#include "cli.h" |
| 21 |
#include "shared/dbus-client-pcm.h" |
| 22 |
|
| 23 |
static void usage(const char *command) { |
| 24 |
printf("Get or set the delay adjustment of the given PCM.\n\n"); |
| 25 |
cli_print_usage("%s [OPTION]... PCM-PATH [ADJUSTMENT]", command); |
| 26 |
printf("\nOptions:\n" |
| 27 |
" -h, --help\t\tShow this message and exit\n" |
| 28 |
"\nPositional arguments:\n" |
| 29 |
" PCM-PATH\tBlueALSA PCM D-Bus object path\n" |
| 30 |
" ADJUSTMENT\tAdjustment value (+/-), in milliseconds\n" |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
static int cmd_delay_adjustment_func(int argc, char *argv[]) { |
| 35 |
|
| 36 |
int opt; |
| 37 |
const char *opts = "+hqv"; |
| 38 |
const struct option longopts[] = { |
| 39 |
{ "help", no_argument, NULL, 'h' }, |
| 40 |
{ "quiet", no_argument, NULL, 'q' }, |
| 41 |
{ "verbose", no_argument, NULL, 'v' }, |
| 42 |
{ 0 }, |
| 43 |
}; |
| 44 |
|
| 45 |
opterr = 0; |
| 46 |
while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1) { |
| 47 |
if (cli_parse_common_options(opt)) |
| 48 |
continue; |
| 49 |
if (opt == 'h') { /* --help */ |
| 50 |
usage(argv[0]); |
| 51 |
return EXIT_SUCCESS; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if (argc - optind < 1) { |
| 56 |
cmd_print_error("Missing BlueALSA PCM path argument"); |
| 57 |
return EXIT_FAILURE; |
| 58 |
} |
| 59 |
if (argc - optind > 2) { |
| 60 |
cmd_print_error("Invalid number of arguments"); |
| 61 |
return EXIT_FAILURE; |
| 62 |
} |
| 63 |
|
| 64 |
DBusError err = DBUS_ERROR_INIT; |
| 65 |
const char *path = argv[optind]; |
| 66 |
|
| 67 |
struct ba_pcm pcm; |
| 68 |
if (!cli_get_ba_pcm(path, &pcm, &err)) { |
| 69 |
cmd_print_error("Couldn't get BlueALSA PCM: %s", err.message); |
| 70 |
return EXIT_FAILURE; |
| 71 |
} |
| 72 |
|
| 73 |
if (argc == 2) { |
| 74 |
printf("DelayAdjustment: %#.1f ms\n", (double)pcm.delay_adjustment/ 10); |
| 75 |
return EXIT_SUCCESS; |
| 76 |
} |
| 77 |
|
| 78 |
const char *value = argv[optind + 1]; |
| 79 |
errno = 0; |
| 80 |
char *endptr = NULL; |
| 81 |
double dbl = strtod(value, &endptr); |
| 82 |
if (endptr == value || errno == ERANGE) { |
| 83 |
cmd_print_error("Invalid argument: %s", value); |
| 84 |
return EXIT_FAILURE; |
| 85 |
} |
| 86 |
|
| 87 |
int adjustment = lround(dbl * 10.0); |
| 88 |
if (adjustment < INT16_MIN || adjustment > INT16_MAX) { |
| 89 |
cmd_print_error("Invalid argument: %s", value); |
| 90 |
return EXIT_FAILURE; |
| 91 |
} |
| 92 |
|
| 93 |
if (!ba_dbus_pcm_set_delay_adjustment(&config.dbus, pcm.pcm_path, |
| 94 |
pcm.codec.name, adjustment, &err)) { |
| 95 |
cmd_print_error("DelayAdjustment update failed: %s", err.message); |
| 96 |
return EXIT_FAILURE; |
| 97 |
} |
| 98 |
|
| 99 |
return EXIT_SUCCESS; |
| 100 |
} |
| 101 |
|
| 102 |
const struct cli_command cmd_delay_adjustment = { |
| 103 |
"delay-adjustment", |
| 104 |
"Get or set PCM delay adjustment", |
| 105 |
cmd_delay_adjustment_func, |
| 106 |
}; |