mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:14:58 +00:00
asctl: Port to LibMain :^)
This commit is contained in:
parent
880888fcc7
commit
8c4625e3b1
2 changed files with 18 additions and 17 deletions
|
@ -54,7 +54,7 @@ endforeach()
|
|||
target_link_libraries(allocate LibMain)
|
||||
target_link_libraries(aplay LibAudio LibMain)
|
||||
target_link_libraries(arp LibMain)
|
||||
target_link_libraries(asctl LibAudio)
|
||||
target_link_libraries(asctl LibAudio LibMain)
|
||||
target_link_libraries(base64 LibMain)
|
||||
target_link_libraries(basename LibMain)
|
||||
target_link_libraries(bt LibSymbolication)
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibMain/Main.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
@ -24,31 +25,31 @@ enum AudioVariable : u32 {
|
|||
};
|
||||
|
||||
// asctl: audio server control utility
|
||||
int main(int argc, char** argv)
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
Core::EventLoop loop;
|
||||
auto audio_client = Audio::ClientConnection::construct();
|
||||
|
||||
String command = String::empty();
|
||||
Vector<StringView> arguments;
|
||||
Vector<StringView> command_arguments;
|
||||
bool human_mode = false;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.set_general_help("Send control signals to the audio server and hardware.");
|
||||
args_parser.add_option(human_mode, "Print human-readable output", "human-readable", 'h');
|
||||
args_parser.add_positional_argument(command, "Command, either (g)et or (s)et\n\n\tThe get command accepts a list of variables to print.\n\tThey are printed in the given order.\n\tIf no value is specified, all are printed.\n\n\tThe set command accepts a any number of variables\n\tfollowed by the value they should be set to.\n\n\tPossible variables are (v)olume, (m)ute, sample(r)ate.\n", "command");
|
||||
args_parser.add_positional_argument(arguments, "Arguments for the command", "args", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(argc, argv);
|
||||
args_parser.add_positional_argument(command_arguments, "Arguments for the command", "args", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
||||
if (command.equals_ignoring_case("get") || command == "g") {
|
||||
// Get variables
|
||||
Vector<AudioVariable> values_to_print;
|
||||
if (arguments.is_empty()) {
|
||||
if (command_arguments.is_empty()) {
|
||||
values_to_print.append(AudioVariable::Volume);
|
||||
values_to_print.append(AudioVariable::Mute);
|
||||
values_to_print.append(AudioVariable::SampleRate);
|
||||
} else {
|
||||
for (auto& variable : arguments) {
|
||||
for (auto& variable : command_arguments) {
|
||||
if (variable.is_one_of("v"sv, "volume"sv))
|
||||
values_to_print.append(AudioVariable::Volume);
|
||||
else if (variable.is_one_of("m"sv, "mute"sv))
|
||||
|
@ -95,25 +96,25 @@ int main(int argc, char** argv)
|
|||
} else if (command.equals_ignoring_case("set") || command == "s") {
|
||||
// Set variables
|
||||
HashMap<AudioVariable, Variant<int, bool>> values_to_set;
|
||||
for (size_t i = 0; i < arguments.size(); ++i) {
|
||||
if (i == arguments.size() - 1) {
|
||||
for (size_t i = 0; i < command_arguments.size(); ++i) {
|
||||
if (i == command_arguments.size() - 1) {
|
||||
warnln("Error: value missing for last variable");
|
||||
return 1;
|
||||
}
|
||||
auto& variable = arguments[i];
|
||||
auto& variable = command_arguments[i];
|
||||
if (variable.is_one_of("v"sv, "volume"sv)) {
|
||||
auto volume = arguments[++i].to_int();
|
||||
auto volume = command_arguments[++i].to_int();
|
||||
if (!volume.has_value()) {
|
||||
warnln("Error: {} is not an integer volume", arguments[i - 1]);
|
||||
warnln("Error: {} is not an integer volume", command_arguments[i - 1]);
|
||||
return 1;
|
||||
}
|
||||
if (volume.value() < 0 || volume.value() > 100) {
|
||||
warnln("Error: {} is not between 0 and 100", arguments[i - 1]);
|
||||
warnln("Error: {} is not between 0 and 100", command_arguments[i - 1]);
|
||||
return 1;
|
||||
}
|
||||
values_to_set.set(AudioVariable::Volume, volume.value());
|
||||
} else if (variable.is_one_of("m"sv, "mute"sv)) {
|
||||
auto& mute_text = arguments[++i];
|
||||
auto& mute_text = command_arguments[++i];
|
||||
bool mute;
|
||||
if (mute_text.equals_ignoring_case("true") || mute_text == "1") {
|
||||
mute = true;
|
||||
|
@ -125,14 +126,14 @@ int main(int argc, char** argv)
|
|||
}
|
||||
values_to_set.set(AudioVariable::Mute, mute);
|
||||
} else if (variable.is_one_of("r"sv, "samplerate"sv)) {
|
||||
auto sample_rate = arguments[++i].to_int();
|
||||
auto sample_rate = command_arguments[++i].to_int();
|
||||
if (!sample_rate.has_value()) {
|
||||
warnln("Error: {} is not an integer sample rate", arguments[i - 1]);
|
||||
warnln("Error: {} is not an integer sample rate", command_arguments[i - 1]);
|
||||
return 1;
|
||||
}
|
||||
values_to_set.set(AudioVariable::SampleRate, sample_rate.value());
|
||||
} else {
|
||||
warnln("Error: Unrecognized variable {}", arguments[i]);
|
||||
warnln("Error: Unrecognized variable {}", command_arguments[i]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue