Browse Source

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

Liav A 1 year ago
parent
commit
26f96d2a42

+ 3 - 0
Base/usr/share/man/man1/beep.md

@@ -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

+ 1 - 0
Kernel/API/BeepInstruction.h

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

+ 3 - 1
Kernel/Devices/Generic/PCSpeakerDevice.cpp

@@ -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);

+ 2 - 2
Userland/Libraries/LibCore/System.cpp

@@ -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 {};
 }

+ 1 - 1
Userland/Libraries/LibCore/System.h

@@ -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);

+ 3 - 1
Userland/Utilities/beep.cpp

@@ -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;
 }