Kernel+Userland: Add option for duration of /dev/beep producing sound

This commit is contained in:
Liav A 2023-08-25 20:26:18 +03:00 committed by Tim Schumacher
parent 1b00618fd9
commit 26f96d2a42
Notes: sideshowbarker 2024-07-16 22:58:46 +09:00
6 changed files with 13 additions and 5 deletions

View File

@ -15,6 +15,7 @@ beep allows the user to beep the PC speaker.
## Options
* `-f frequency`, `--beep-tone frequency`: Beep tone (frequency in Hz)
* `-n N`, `--duration N`: Duration (N in milliseconds)
## Notes
@ -28,6 +29,8 @@ will fail to use the PC speaker.
$ beep
# Use beep with tone of 1000Hz
$ beep -f 1000
# Use beep with tone of 1000Hz for 1 second
$ beep -f 1000 -n 1000
```
## See also

View File

@ -10,4 +10,5 @@
struct BeepInstruction {
u16 tone;
u16 milliseconds_duration;
};

View File

@ -48,9 +48,11 @@ ErrorOr<size_t> PCSpeakerDevice::write(OpenFileDescription&, u64, UserOrKernelBu
TRY(buffer.read(&instruction, sizeof(BeepInstruction)));
if (instruction.tone < 20 || instruction.tone > 20000)
return Error::from_errno(EINVAL);
if (instruction.milliseconds_duration == 0)
return Error::from_errno(EINVAL);
#if ARCH(X86_64)
PCSpeaker::tone_on(instruction.tone);
auto result = Thread::current()->sleep(Duration::from_nanoseconds(200'000'000));
auto result = Thread::current()->sleep(Duration::from_milliseconds(instruction.milliseconds_duration));
PCSpeaker::tone_off();
if (result.was_interrupted())
return Error::from_errno(EINTR);

View File

@ -149,12 +149,12 @@ namespace Core::System {
#ifdef AK_OS_SERENITY
ErrorOr<void> beep(u16 tone)
ErrorOr<void> beep(u16 tone, u16 milliseconds_duration)
{
static Optional<int> beep_fd;
if (!beep_fd.has_value())
beep_fd = TRY(Core::System::open("/dev/beep"sv, O_RDWR));
BeepInstruction instruction { tone };
BeepInstruction instruction { tone, milliseconds_duration };
TRY(Core::System::write(beep_fd.value(), Span<u8 const>(&instruction, sizeof(BeepInstruction))));
return {};
}

View File

@ -51,7 +51,7 @@
namespace Core::System {
#ifdef AK_OS_SERENITY
ErrorOr<void> beep(u16 tone = 440);
ErrorOr<void> beep(u16 tone = 440, u16 milliseconds_duration = 200);
ErrorOr<void> pledge(StringView promises, StringView execpromises = {});
ErrorOr<void> unveil(StringView path, StringView permissions);
ErrorOr<void> unveil_after_exec(StringView path, StringView permissions);

View File

@ -11,9 +11,11 @@
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
Optional<size_t> tone;
Optional<size_t> milliseconds_duration;
Core::ArgsParser args_parser;
args_parser.add_option(tone, "Beep tone", "beep-tone", 'f', "Beep tone (frequency in Hz)");
args_parser.add_option(milliseconds_duration, "Duration", "duration", 'n', "Duration (in milliseconds)");
args_parser.parse(arguments);
TRY(Core::System::beep(tone.value_or(440)));
TRY(Core::System::beep(tone.value_or(440), milliseconds_duration.value_or(200)));
return 0;
}