diff --git a/kernel/device/mouse.cpp b/kernel/device/mouse.cpp index 0e7f57a..c2f6a62 100644 --- a/kernel/device/mouse.cpp +++ b/kernel/device/mouse.cpp @@ -10,7 +10,7 @@ using namespace Device; #define PS2_PORT_BUFFER 0x60 Mouse::Mouse() - : queue(MOUSE_PACKET_NUM) + : queue(new nk::CircularQueue(MOUSE_PACKET_NUM)) { } @@ -104,6 +104,7 @@ void Mouse::HandleInterrupt(unsigned int, RegisterStates *) int buttons = mousePacket[0] & 0x07; MousePacket packet = {x, y, w, buttons}; + queue->Enqueue(packet); } } diff --git a/kernel/include/kernel/device/mouse.h b/kernel/include/kernel/device/mouse.h index f0c2162..46a7dd3 100644 --- a/kernel/include/kernel/device/mouse.h +++ b/kernel/include/kernel/device/mouse.h @@ -5,7 +5,7 @@ #include // Number of mouse packets the mouse saves before it overwrites the oldest one. -#define MOUSE_PACKET_NUM 256 +#define MOUSE_PACKET_NUM 16 #define MOUSEBTN_LEFT 1 #define MOUSEBTN_RIGHT 2 @@ -29,7 +29,7 @@ namespace Device int wheelPosition; int buttons; - nk::CircularQueue queue; + nk::CircularQueue *queue; Mouse(); diff --git a/kernel/include/kernel/syscalls.h b/kernel/include/kernel/syscalls.h index 9562912..f7e7a01 100644 --- a/kernel/include/kernel/syscalls.h +++ b/kernel/include/kernel/syscalls.h @@ -36,6 +36,7 @@ #define SYS_PIPESEND 27 #define SYS_THCREATE 28 #define SYS_THJOIN 29 +#define SYS_MOUSEPOLL 30 #define PARAM_VALUE(param, type) (*(type *)(param)) @@ -167,5 +168,6 @@ int sys$$pipe_recv(void *param); int sys$$pipe_send(void *param); int sys$$thread_create(void *param); int sys$$thread_join(void *param); +int sys$$mouse_poll(void *param); #endif diff --git a/kernel/syscallhandler.cpp b/kernel/syscallhandler.cpp index a99dafb..54f8fda 100644 --- a/kernel/syscallhandler.cpp +++ b/kernel/syscallhandler.cpp @@ -48,6 +48,7 @@ namespace Kernel AddSyscall(SYS_PIPERECV, sys$$pipe_recv); AddSyscall(SYS_THCREATE, sys$$thread_create); AddSyscall(SYS_THJOIN, sys$$thread_join); + AddSyscall(SYS_MOUSEPOLL, sys$$mouse_poll); Interrupts::AddHandler(0x80, this); } diff --git a/kernel/syscalls.cpp b/kernel/syscalls.cpp index 8cbc1a7..2f0a3e7 100644 --- a/kernel/syscalls.cpp +++ b/kernel/syscalls.cpp @@ -418,4 +418,20 @@ int sys$$thread_join(void *param) { Thread::Current()->Block(new ThreadJoinBlocker(thread)); } +} + +int sys$$mouse_poll(void *param) +{ + MOUSEPACKET *packet = (MOUSEPACKET*)param; + auto queue = Device::DeviceManager::mouse->queue; + if (queue->IsEmpty()) + return -ENODATA; + + auto kernelPacket = queue->Dequeue(); + packet->dx = kernelPacket.dx; + packet->dy = kernelPacket.dy; + packet->dwheel = kernelPacket.dwheel; + packet->buttons = kernelPacket.buttons; + + return 0; } \ No newline at end of file diff --git a/libc/include/nekosys.h b/libc/include/nekosys.h index 7d93d59..985fcf4 100644 --- a/libc/include/nekosys.h +++ b/libc/include/nekosys.h @@ -27,6 +27,14 @@ extern "C" size_t pixelStride; } FRAMEBUF; + typedef struct + { + int dx; + int dy; + int dwheel; + int buttons; + } MOUSEPACKET; + /* FIXME: hacky stdin readline - should be redone */ int readln(char *dst, size_t maxSize); @@ -55,6 +63,9 @@ extern "C" int thread_create(void (*entryPoint)()); int thread_join(int threadId); int thread_die(int retcode); + + /* Mouse */ + int mouse_poll(MOUSEPACKET *packet); #ifdef __cplusplus } #endif diff --git a/libc/nekosys.c b/libc/nekosys.c index a825ed9..b444adb 100644 --- a/libc/nekosys.c +++ b/libc/nekosys.c @@ -74,7 +74,8 @@ int thread_create(void (*entryPoint)()) return syscall(SYS_THCREATE, entryPoint); } -int thread_join(int threadId) { +int thread_join(int threadId) +{ return syscall(SYS_THJOIN, &threadId); } @@ -111,4 +112,9 @@ int pipe_send(int pipeid, pid_t dst, size_t size, uint8_t *data) param.size = size; param.data = data; return syscall(SYS_PIPESEND, ¶m); +} + +int mouse_poll(MOUSEPACKET *packet) +{ + return syscall(SYS_MOUSEPOLL, packet); } \ No newline at end of file diff --git a/libneko/include/nk/circularqueue.h b/libneko/include/nk/circularqueue.h index 787888d..d46cfe7 100644 --- a/libneko/include/nk/circularqueue.h +++ b/libneko/include/nk/circularqueue.h @@ -46,7 +46,6 @@ namespace nk head = tail = -1; else head = (head + 1) % size; - return elem; } diff --git a/userland/sakura/main.cpp b/userland/sakura/main.cpp index 35f9ec6..3e963bf 100644 --- a/userland/sakura/main.cpp +++ b/userland/sakura/main.cpp @@ -25,6 +25,9 @@ FRAMEBUF framebuf; GuiConnection *connection; nk::Vector windows; +int mouse_x = 0; +int mouse_y = 0; + uint8_t *read_file(const char *path, size_t *size) { FILE *fd = fopen(path, "r"); @@ -115,6 +118,8 @@ int main(int argc, char **argv) spawnp(nullptr, conf->GetProperty("StartupApp").CStr(), 0, nullptr); printf("[info] demo application started.\n"); + MOUSEPACKET mouse{}; + /* compositor test */ while (true) { @@ -152,6 +157,25 @@ int main(int argc, char **argv) } } + if (mouse_poll(&mouse) == 0) + { + mouse_x += mouse.dx; + mouse_y -= mouse.dy; + if (mouse_x < 0) + mouse_x = 0; + if (mouse_y < 0) + mouse_y = 0; + if (mouse_x > width - 1) + mouse_x = width - 1; + if (mouse_y > height - 1) + mouse_y = height - 1; + } + + size_t baseidx = mouse_y * framebuf.pitch + mouse_x * 4; + framebuf.buffer[baseidx] = 0xff; + framebuf.buffer[baseidx + 1] = 0x00; + framebuf.buffer[baseidx + 2] = 0x00; + framebuf_flush_all(); sleep(0); } diff --git a/userland/testmouse/build.sh b/userland/testmouse/build.sh new file mode 100755 index 0000000..9d4c41e --- /dev/null +++ b/userland/testmouse/build.sh @@ -0,0 +1,2 @@ +#!/bin/bash +neko-gcc main.cpp -o ../../sysroot/bin/testmouse diff --git a/userland/testmouse/main.cpp b/userland/testmouse/main.cpp new file mode 100644 index 0000000..2d2c34d --- /dev/null +++ b/userland/testmouse/main.cpp @@ -0,0 +1,16 @@ +#include +#include + +int main(int argc, char **argv) +{ + MOUSEPACKET packet; + while (1) + { + if (mouse_poll(&packet) == 0) + { + printf("%d %d %d\n", packet.dx, packet.dy, packet.buttons); + } + } + + return 0; +}