generic mouse interface

This commit is contained in:
Matheus
2023-01-05 23:29:03 -03:00
parent 2b4e97735b
commit b980ab94c2
5 changed files with 116 additions and 1 deletions
+2
View File
@@ -90,6 +90,8 @@ void kmain(){
smp_init();
keyboard_init();
mouse_init();
ps2_init();
+1
View File
@@ -11,6 +11,7 @@
#define MAJOR_FB 5
#define MAJOR_KB 6
#define MAJOR_E9OUT 7
#define MAJOR_MOUSE 8
typedef struct{
int (*read)(int* error, int minor, void* buff, size_t count, size_t offset);
+28
View File
@@ -0,0 +1,28 @@
#ifndef _MOUSE_H_INCLUDE
#define _MOUSE_H_INCLUDE
#define MOUSE_FLAG_RB 1
#define MOUSE_FLAG_MB 2
#define MOUSE_FLAG_LB 4
#define MOUSE_FLAG_B4 8
#define MOUSE_FLAG_B5 16
#include <ringbuffer.h>
#include <kernel/event.h>
typedef struct {
int flags;
int x,y,z;
} mousepacket_t;
typedef struct {
ringbuffer_t buffer;
event_t event;
int flags;
} mouse_t;
void mouse_init();
int mouse_getnew();
void mouse_packet(int, mousepacket_t);
#endif
+84
View File
@@ -0,0 +1,84 @@
#include <kernel/mouse.h>
#include <kernel/devman.h>
#include <arch/panic.h>
#include <poll.h>
#include <arch/interrupt.h>
#define MOUSE_COUNT 32
static int mouseinuse;
mouse_t mouse[MOUSE_COUNT];
ringbuffer_t devbuffer;
void mouse_packet(int id, mousepacket_t packet){
ringbuffer_write(&devbuffer, &packet, sizeof(mousepacket_t));
ringbuffer_write(&mouse[id].buffer, &packet, sizeof(mousepacket_t));
event_signal(&mouse[id].event, false);
}
int mouse_getnew(){
int id;
for(id = 0; id < 32; ++id)
if(((mouseinuse >> id) & 1) == 0)
break;
if(id == 32)
return -1;
mouseinuse |= 1 << id;
return id;
}
static int read(int *error, int minor, void* buff, size_t count, size_t offset){
count /= sizeof(mousepacket_t);
if(count == 0){
*error = EINVAL;
return -1;
}
arch_interrupt_disable();
count = ringbuffer_read(&devbuffer, buff, count*sizeof(mousepacket_t));
arch_interrupt_enable();
*error = 0;
return count;
}
static int poll(int minor, pollfd* fd){
if((fd->events & POLLIN) && devbuffer.read != devbuffer.write)
fd->revents |= POLLIN;
return 0;
}
static devcalls calls = {
.read = read,
.poll = poll
};
void mouse_init(){
for(uintmax_t i = 0; i < MOUSE_COUNT; ++i){
if(ringbuffer_init(&mouse[i].buffer, sizeof(mousepacket_t)*100))
_panic("Out of memory!", NULL);
}
if(ringbuffer_init(&devbuffer, sizeof(mousepacket_t)*100))
_panic("Out of memory", NULL);
if(devman_newdevice("mouse", TYPE_CHARDEV, MAJOR_MOUSE, 0, &calls))
_panic("Failed to create mouse device", NULL);
}
+1 -1
View File
@@ -278,7 +278,7 @@ static int poll(int minor, pollfd* fd){
}
devcalls calls = {
static devcalls calls = {
read, write, isatty, isseekable, ioctl, poll
};