diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 16267f7a..4d8865ff 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -44,4 +44,4 @@ luna_app(gol.cpp gol) luna_app(buffer-test.cpp buffer-test) luna_app(socket-test.cpp socket-test) luna_app(socket-client.cpp socket-client) -luna_app(mouse.cpp mouse) +luna_app(input.cpp input) diff --git a/apps/input.cpp b/apps/input.cpp new file mode 100644 index 00000000..6893f847 --- /dev/null +++ b/apps/input.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +Result process_mouse_packet(const moon::MousePacket& packet) +{ + bool right = packet.buttons & moon::MouseButton::Right; + bool left = packet.buttons & moon::MouseButton::Left; + bool middle = packet.buttons & moon::MouseButton::Middle; + + Vector 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()); + + return {}; +} + +Result luna_main(int, char**) +{ + auto mouse = TRY(os::File::open("/dev/mouse", os::File::ReadOnly)); + mouse->set_buffer(os::File::NotBuffered); + + auto keyboard = TRY(os::File::open("/dev/kbd", os::File::ReadOnly)); + keyboard->set_buffer(os::File::NotBuffered); + + ioctl(STDIN_FILENO, TTYSETGFX, 1); + + while (1) + { + struct pollfd fds[] = { + { .fd = mouse->fd(), .events = POLLIN, .revents = 0 }, + { .fd = keyboard->fd(), .events = POLLIN, .revents = 0 }, + }; + + int rc = poll(fds, 2, 1000); + if (!rc) continue; + if (rc < 0) { os::println("poll: error: %s", strerror(errno)); } + + if (fds[0].revents & POLLIN) + { + moon::MousePacket packet; + TRY(mouse->read_typed(packet)); + TRY(process_mouse_packet(packet)); + } + if (fds[1].revents & POLLIN) + { + moon::KeyboardPacket packet; + TRY(keyboard->read_typed(packet)); + os::println("Keyboard packet: %s key %u", packet.released ? "released" : "pressed", packet.key); + } + } + + return 0; +} diff --git a/apps/mouse.cpp b/apps/mouse.cpp deleted file mode 100644 index d41aef4b..00000000 --- a/apps/mouse.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include - -Result 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 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()); - } -}