Skip to content

Commit

Permalink
Rendering a primitive mouse cursor in window server
Browse files Browse the repository at this point in the history
  • Loading branch information
Twometer committed Apr 12, 2021
1 parent ab976d4 commit 1af69bc
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 5 deletions.
3 changes: 2 additions & 1 deletion kernel/device/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace Device;
#define PS2_PORT_BUFFER 0x60

Mouse::Mouse()
: queue(MOUSE_PACKET_NUM)
: queue(new nk::CircularQueue<MousePacket>(MOUSE_PACKET_NUM))
{
}

Expand Down Expand Up @@ -104,6 +104,7 @@ void Mouse::HandleInterrupt(unsigned int, RegisterStates *)
int buttons = mousePacket[0] & 0x07;

MousePacket packet = {x, y, w, buttons};
queue->Enqueue(packet);
}
}

Expand Down
4 changes: 2 additions & 2 deletions kernel/include/kernel/device/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <kernel/arch/interrupts.h>

// 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
Expand All @@ -29,7 +29,7 @@ namespace Device
int wheelPosition;
int buttons;

nk::CircularQueue<MousePacket> queue;
nk::CircularQueue<MousePacket> *queue;

Mouse();

Expand Down
2 changes: 2 additions & 0 deletions kernel/include/kernel/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions kernel/syscallhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
16 changes: 16 additions & 0 deletions kernel/syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
11 changes: 11 additions & 0 deletions libc/include/nekosys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion libc/nekosys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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, &param);
}

int mouse_poll(MOUSEPACKET *packet)
{
return syscall(SYS_MOUSEPOLL, packet);
}
1 change: 0 additions & 1 deletion libneko/include/nk/circularqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ namespace nk
head = tail = -1;
else
head = (head + 1) % size;

return elem;
}

Expand Down
24 changes: 24 additions & 0 deletions userland/sakura/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ FRAMEBUF framebuf;
GuiConnection *connection;
nk::Vector<window_info> windows;

int mouse_x = 0;
int mouse_y = 0;

uint8_t *read_file(const char *path, size_t *size)
{
FILE *fd = fopen(path, "r");
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions userland/testmouse/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
neko-gcc main.cpp -o ../../sysroot/bin/testmouse
16 changes: 16 additions & 0 deletions userland/testmouse/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>
#include <nekosys.h>

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

0 comments on commit 1af69bc

Please sign in to comment.