summaryrefslogtreecommitdiff
path: root/driver/mouse.c
diff options
context:
space:
mode:
Diffstat (limited to 'driver/mouse.c')
-rw-r--r--driver/mouse.c97
1 files changed, 53 insertions, 44 deletions
diff --git a/driver/mouse.c b/driver/mouse.c
index c0e71d5..14782f7 100644
--- a/driver/mouse.c
+++ b/driver/mouse.c
@@ -1,3 +1,6 @@
+//https://wiki.osdev.org/PS/2_Mouse
+//TODO: ignore last packet for 4 packets mouse?
+
#include <stdint.h>
#include "mouse.h"
@@ -12,16 +15,24 @@
static volatile uint8_t mouse_cycle;
static volatile uint8_t mouse_byte[3];
-static volatile int16_t mouse_x;
-static volatile int16_t mouse_y;
+// data stream coming from the mouse will be waiting inside this buffer
static ringbuffer mouse_in;
+// called on each interrupt (keep it small)
+// just pushes mouse data into the buffer
+uint32_t mouse_interrupt(uint32_t esp)
+{
+ if(!ringbuffer_put(&mouse_in,x86_inb(0x60)))kpanic("full");
+ return esp;
+}
+
+// process data in buffer (return true if anything was processed)
bool mouse_worker()
{
if(ringbuffer_full(&mouse_in))
{
- klog("mouse buffer full");
+ kpanic("mouse buffer full"); // heavy debugging
}
bool wake=false;
@@ -32,7 +43,7 @@ bool mouse_worker()
return wake;
}
-
+//
void mouse_wait(uint8_t a_type) //unsigned char
{
uint32_t _time_out=100000; //unsigned int
@@ -79,11 +90,6 @@ static void mouse_write(uint8_t a_write)
x86_outb(0x60, a_write);
}
-uint32_t mouse_interrupt(uint32_t esp)
-{
- if(!ringbuffer_put(&mouse_in,x86_inb(0x60)))kpanic("full");
- return esp;
-}
void mouse_init()
{
@@ -117,44 +123,45 @@ void mouse_init()
interrupt_register(INTERRUPT_MOUSE,&mouse_interrupt);
}
-// called as we filled the 3 bytes, everytime.
+// called every time after the 3 bytes have been filled.
void mouse_action()
{
- //klog("mouse %02x / %02x / %02x ",mouse_byte[0], mouse_byte[1],mouse_byte[2]);
- //TODO: ignore last packet for 4packets mouse and resync if you get out of sync
-
- if(mouse_byte[0]&0x80||mouse_byte[0]&0x40)
+ /* 3rd byte bits have this meaning:
+ *
+ * key 0 left button 1
+ * key 1 right button 2
+ * key 2 middle button 4
+ * key 3 always one! 8
+ *
+ * key 4 x-axis sign bit 16
+ * key 5 y-axis sign bit 32
+ *
+ * key 6 x-axis overflow 64
+ * key 7 y-axis overflow 128
+ */
+
+ int rel_x=0;
+ int rel_y=0;
+
+ /* ignore overflows? or assume max? */
+
+ if(mouse_byte[0]&0x80)
{
- // klog("mouse overflow, skipping packet");
- return; //skip packet on overflow
+ return;
+ // klog("x overflow");
}
- //OOOMG!!//
- if(mouse_byte[1]>127){
- mouse_x-=256;
- mouse_x+=mouse_byte[1];
- }
- else
+ if(mouse_byte[0]&0x40)
{
- mouse_x+=mouse_byte[1];
+ return;
+ // klog("y overflow");
}
- if(mouse_byte[2]>127){
- mouse_y-=256;
- mouse_y+=mouse_byte[2];
- }
- else
- {
- mouse_y+=mouse_byte[2];
- }
+ rel_x = mouse_byte[1] - ((mouse_byte[0] << 4) & 0x100);
+ rel_y = mouse_byte[2] - ((mouse_byte[0] << 3) & 0x100);
- if(mouse_x<0)mouse_x=0;
- if(mouse_y<0)mouse_y=0;
-
- if(mouse_x>=1920)mouse_x=1920-1;
- if(mouse_y>=1080)mouse_y=1080-1;
-
- compositor_mouse_handle(mouse_x,1080-mouse_y-1, mouse_byte[0]);
+ // we pass the state through
+ compositor_mouse_handle(rel_x,-rel_y,mouse_byte[0]);
}
void mouse_handler(uint8_t byte)//struct regs *a_r) //struct regs *a_r (not used but just there)
@@ -166,12 +173,14 @@ void mouse_handler(uint8_t byte)//struct regs *a_r) //struct regs *a_r (not used
case 0:
mouse_byte[0]=byte;
mouse_cycle++;
- if(!(mouse_byte[0]&0x8))
- {
- //klog("mouse out of sync, try resync");
- mouse_cycle--;
- // kpanic("mouse packets out of sync!?"); // this bit is always 1, otherwise panic!
- }
+
+ // this bit is ALWAYS one, otherwise we are out of sync!
+ if(!(mouse_byte[0]&0x8))
+ {
+// klog("mouse out of sync (try next packet) ...");
+ mouse_cycle--;
+ }
+
break;
case 1:
mouse_byte[1]=byte;