asctl.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  4. * Copyright (c) 2021, David Isaksson <davidisaksson93@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Variant.h>
  9. #include <AK/Vector.h>
  10. #include <LibAudio/ConnectionToServer.h>
  11. #include <LibCore/ArgsParser.h>
  12. #include <LibCore/EventLoop.h>
  13. #include <LibCore/System.h>
  14. #include <LibMain/Main.h>
  15. #include <math.h>
  16. #include <stdio.h>
  17. #include <sys/ioctl.h>
  18. enum AudioVariable : u32 {
  19. Volume,
  20. Mute,
  21. SampleRate
  22. };
  23. // asctl: audio server control utility
  24. ErrorOr<int> serenity_main(Main::Arguments arguments)
  25. {
  26. Core::EventLoop loop;
  27. auto audio_client = TRY(Audio::ConnectionToServer::try_create());
  28. audio_client->async_pause_playback();
  29. DeprecatedString command = DeprecatedString::empty();
  30. Vector<StringView> command_arguments;
  31. bool human_mode = false;
  32. Core::ArgsParser args_parser;
  33. args_parser.set_general_help("Send control signals to the audio server and hardware.");
  34. args_parser.add_option(human_mode, "Print human-readable output", "human-readable", 'h');
  35. 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");
  36. args_parser.add_positional_argument(command_arguments, "Arguments for the command", "args", Core::ArgsParser::Required::No);
  37. args_parser.parse(arguments);
  38. TRY(Core::System::unveil(nullptr, nullptr));
  39. TRY(Core::System::pledge("stdio rpath wpath recvfd thread"));
  40. if (command.equals_ignoring_ascii_case("get"sv) || command == "g") {
  41. // Get variables
  42. Vector<AudioVariable> values_to_print;
  43. if (command_arguments.is_empty()) {
  44. values_to_print.append(AudioVariable::Volume);
  45. values_to_print.append(AudioVariable::Mute);
  46. values_to_print.append(AudioVariable::SampleRate);
  47. } else {
  48. for (auto& variable : command_arguments) {
  49. if (variable.is_one_of("v"sv, "volume"sv))
  50. values_to_print.append(AudioVariable::Volume);
  51. else if (variable.is_one_of("m"sv, "mute"sv))
  52. values_to_print.append(AudioVariable::Mute);
  53. else if (variable.is_one_of("r"sv, "samplerate"sv))
  54. values_to_print.append(AudioVariable::SampleRate);
  55. else {
  56. warnln("Error: Unrecognized variable {}", variable);
  57. return 1;
  58. }
  59. }
  60. }
  61. for (auto to_print : values_to_print) {
  62. switch (to_print) {
  63. case AudioVariable::Volume: {
  64. auto volume = lround(audio_client->get_main_mix_volume() * 100);
  65. if (human_mode)
  66. outln("Volume: {}%", volume);
  67. else
  68. out("{} ", volume);
  69. break;
  70. }
  71. case AudioVariable::Mute: {
  72. bool muted = audio_client->is_main_mix_muted();
  73. if (human_mode)
  74. outln("Muted: {}", muted ? "Yes" : "No");
  75. else
  76. out("{} ", muted ? 1 : 0);
  77. break;
  78. }
  79. case AudioVariable::SampleRate: {
  80. u32 sample_rate = audio_client->get_sample_rate();
  81. if (human_mode)
  82. outln("Sample rate: {:5d} Hz", sample_rate);
  83. else
  84. out("{} ", sample_rate);
  85. break;
  86. }
  87. }
  88. }
  89. if (!human_mode)
  90. outln();
  91. } else if (command.equals_ignoring_ascii_case("set"sv) || command == "s") {
  92. // Set variables
  93. HashMap<AudioVariable, Variant<int, bool>> values_to_set;
  94. for (size_t i = 0; i < command_arguments.size(); ++i) {
  95. if (i == command_arguments.size() - 1) {
  96. warnln("Error: value missing for last variable");
  97. return 1;
  98. }
  99. auto& variable = command_arguments[i];
  100. if (variable.is_one_of("v"sv, "volume"sv)) {
  101. auto volume = command_arguments[++i].to_int();
  102. if (!volume.has_value()) {
  103. warnln("Error: {} is not an integer volume", command_arguments[i - 1]);
  104. return 1;
  105. }
  106. if (volume.value() < 0 || volume.value() > 100) {
  107. warnln("Error: {} is not between 0 and 100", command_arguments[i - 1]);
  108. return 1;
  109. }
  110. values_to_set.set(AudioVariable::Volume, volume.value());
  111. } else if (variable.is_one_of("m"sv, "mute"sv)) {
  112. auto& mute_text = command_arguments[++i];
  113. bool mute;
  114. if (mute_text.equals_ignoring_ascii_case("true"sv) || mute_text == "1") {
  115. mute = true;
  116. } else if (mute_text.equals_ignoring_ascii_case("false"sv) || mute_text == "0") {
  117. mute = false;
  118. } else {
  119. warnln("Error: {} is not one of {{0, 1, true, false}}", mute_text);
  120. return 1;
  121. }
  122. values_to_set.set(AudioVariable::Mute, mute);
  123. } else if (variable.is_one_of("r"sv, "samplerate"sv)) {
  124. auto sample_rate = command_arguments[++i].to_int();
  125. if (!sample_rate.has_value()) {
  126. warnln("Error: {} is not an integer sample rate", command_arguments[i - 1]);
  127. return 1;
  128. }
  129. values_to_set.set(AudioVariable::SampleRate, sample_rate.value());
  130. } else {
  131. warnln("Error: Unrecognized variable {}", command_arguments[i]);
  132. return 1;
  133. }
  134. }
  135. for (auto to_set : values_to_set) {
  136. switch (to_set.key) {
  137. case AudioVariable::Volume: {
  138. int& volume = to_set.value.get<int>();
  139. audio_client->set_main_mix_volume(static_cast<double>(volume) / 100);
  140. break;
  141. }
  142. case AudioVariable::Mute: {
  143. bool& mute = to_set.value.get<bool>();
  144. audio_client->set_main_mix_muted(mute);
  145. break;
  146. }
  147. case AudioVariable::SampleRate: {
  148. int& sample_rate = to_set.value.get<int>();
  149. audio_client->set_sample_rate(sample_rate);
  150. break;
  151. }
  152. }
  153. }
  154. }
  155. return 0;
  156. }