Luna/apps/mouse.cpp
apio d0ceec6952
All checks were successful
continuous-integration/drone/push Build is passing
mouse: Show textual description of buttons
2023-08-02 13:58:17 +02:00

33 lines
1.0 KiB
C++

#include <luna/String.h>
#include <moon/Mouse.h>
#include <os/File.h>
Result<int> luna_main(int, char**)
{
auto mouse = TRY(os::File::open("/dev/mouse", os::File::ReadOnly));
mouse->set_buffer(os::File::NotBuffered);
while (1)
{
moon::MousePacket packet;
TRY(mouse->read_typed(packet));
bool right = packet.buttons & moon::MouseButton::Right;
bool left = packet.buttons & moon::MouseButton::Left;
bool middle = packet.buttons & moon::MouseButton::Middle;
Vector<StringView> buttons;
if (right) TRY(buttons.try_append("RIGHT"_sv));
if (left) TRY(buttons.try_append("LEFT"_sv));
if (middle) TRY(buttons.try_append("MIDDLE"_sv));
String button_string;
if (!buttons.size()) button_string = TRY(String::from_cstring("NONE"));
else
button_string = TRY(String::join(buttons, " | "_sv));
os::println("Mouse packet: xdelta=%d, ydelta=%d, buttons=(%s)", packet.xdelta, packet.ydelta,
button_string.chars());
}
}