mirror of
https://github.com/Alvin-Zilverstand/bob16.git
synced 2026-11-27 05:57:49 +01:00
d
d
This commit is contained in:
1 parent
3d06f94e48
commit
57787bfc7f
59 files changed
+9967
-410
No files matched your search
+91
-25
@@ -1,11 +1,38 @@
|
||||
#include "mouse.h"
|
||||
|
||||
static BOB64_MOUSE_STATE mouse_state;
|
||||
static u8 mouse_ps2_buttons;
|
||||
static u8 mouse_usb_buttons[BOB64_MOUSE_USB_DEVICE_LIMIT];
|
||||
#define MOUSE_EVENT_QUEUE_SIZE 64u
|
||||
static BOB64_EVENT mouse_event_queue[MOUSE_EVENT_QUEUE_SIZE];
|
||||
static volatile u32 mouse_event_head,mouse_event_tail;
|
||||
static volatile u8 mouse_irq_active;
|
||||
|
||||
static int mouse_queue_event(const BOB64_EVENT *event) {
|
||||
#if defined(BOB64_UEFI_ABI)
|
||||
u64 flags;
|
||||
#endif
|
||||
u32 head,tail;
|
||||
if(!event)return -1;
|
||||
#if defined(BOB64_UEFI_ABI)
|
||||
__asm__ volatile("pushfq; popq %0; cli":"=r"(flags)::"memory");
|
||||
#endif
|
||||
head=mouse_event_head;tail=mouse_event_tail;
|
||||
if((u32)(head-tail)>=MOUSE_EVENT_QUEUE_SIZE) {
|
||||
#if defined(BOB64_UEFI_ABI)
|
||||
if(flags&(1ULL<<9))__asm__ volatile("sti":::"memory");
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
mouse_event_queue[head&(MOUSE_EVENT_QUEUE_SIZE-1)]=*event;
|
||||
__asm__ volatile("":::"memory");
|
||||
mouse_event_head=head+1;
|
||||
#if defined(BOB64_UEFI_ABI)
|
||||
if(flags&(1ULL<<9))__asm__ volatile("sti":::"memory");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void out8(u16 port,u8 value) {
|
||||
__asm__ volatile("outb %0,%1"::"a"(value),"Nd"(port));
|
||||
}
|
||||
@@ -64,6 +91,8 @@ int bob64_mouse_reset(BOB64_MOUSE_STATE *state,u32 width,u32 height) {
|
||||
|
||||
int bob64_mouse_irq_queue_init(u32 width,u32 height) {
|
||||
if(bob64_mouse_reset(&mouse_state,width,height))return -1;
|
||||
mouse_ps2_buttons=0;
|
||||
for(u32 i=0;i<BOB64_MOUSE_USB_DEVICE_LIMIT;i++)mouse_usb_buttons[i]=0;
|
||||
mouse_event_head=mouse_event_tail=0;mouse_irq_active=0;
|
||||
return 0;
|
||||
}
|
||||
@@ -75,13 +104,8 @@ void bob64_mouse_irq_set_active(int active) {
|
||||
|
||||
int bob64_mouse_irq_capture(u8 value) {
|
||||
BOB64_EVENT event;
|
||||
u32 head=mouse_event_head,tail=mouse_event_tail;
|
||||
if(bob64_mouse_decode(&mouse_state,value,&event))return -1;
|
||||
if((u32)(head-tail)>=MOUSE_EVENT_QUEUE_SIZE)return -1;
|
||||
mouse_event_queue[head&(MOUSE_EVENT_QUEUE_SIZE-1)]=event;
|
||||
__asm__ volatile("":::"memory");
|
||||
mouse_event_head=head+1;
|
||||
return 0;
|
||||
return mouse_queue_event(&event);
|
||||
}
|
||||
|
||||
int bob64_mouse_irq_pop_event(BOB64_EVENT *event) {
|
||||
@@ -106,24 +130,18 @@ static s32 signed_byte(u8 value) {
|
||||
return value<128?(s32)value:(s32)value-256;
|
||||
}
|
||||
|
||||
int bob64_mouse_decode(BOB64_MOUSE_STATE *state,u8 value,BOB64_EVENT *event) {
|
||||
u8 flags,buttons;
|
||||
s32 dx,dy,wheel=0,old_x,old_y,new_x,new_y;
|
||||
static u8 mouse_combined_buttons(void) {
|
||||
u8 buttons=mouse_ps2_buttons;
|
||||
for(u32 i=0;i<BOB64_MOUSE_USB_DEVICE_LIMIT;i++)
|
||||
buttons|=mouse_usb_buttons[i];
|
||||
return buttons;
|
||||
}
|
||||
|
||||
static int mouse_apply_report(BOB64_MOUSE_STATE *state,u8 buttons,s32 dx,
|
||||
s32 dy,s32 wheel,BOB64_EVENT *event) {
|
||||
s32 old_x,old_y,new_x,new_y;
|
||||
s64 proposed_x,proposed_y;
|
||||
if(!state||!event||!state->Width||!state->Height)return -1;
|
||||
if(!state->PacketLength&&!(value&8))return -1;
|
||||
if(state->PacketSize!=3&&state->PacketSize!=4)return -1;
|
||||
state->Packet[state->PacketLength++]=value;
|
||||
if(state->PacketLength<state->PacketSize)return -1;
|
||||
state->PacketLength=0;
|
||||
flags=state->Packet[0];buttons=(u8)(flags&7);
|
||||
if(state->PacketSize==4) {
|
||||
u8 wheel_nibble=(u8)(state->Packet[3]&15);
|
||||
wheel=(wheel_nibble&8)?(s32)wheel_nibble-16:(s32)wheel_nibble;
|
||||
if(state->DeviceId==4)buttons|=(u8)((state->Packet[3]&0x30)>>1);
|
||||
}
|
||||
dx=(flags&0x40)?0:signed_byte(state->Packet[1]);
|
||||
dy=(flags&0x80)?0:-signed_byte(state->Packet[2]);
|
||||
old_x=state->X;old_y=state->Y;
|
||||
proposed_x=(s64)old_x+dx;proposed_y=(s64)old_y+dy;
|
||||
if(proposed_x<0)proposed_x=0;
|
||||
@@ -139,11 +157,58 @@ int bob64_mouse_decode(BOB64_MOUSE_STATE *state,u8 value,BOB64_EVENT *event) {
|
||||
event->X=new_x;event->Y=new_y;
|
||||
event->DeltaX=new_x-old_x;event->DeltaY=new_y-old_y;
|
||||
event->Buttons=buttons;event->Wheel=wheel;event->WindowHandle=0;
|
||||
event->ScreenX=state->X;event->ScreenY=state->Y;
|
||||
event->ScreenX=new_x;event->ScreenY=new_y;
|
||||
state->Buttons=buttons;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bob64_mouse_decode(BOB64_MOUSE_STATE *state,u8 value,BOB64_EVENT *event) {
|
||||
u8 flags,buttons;
|
||||
s32 dx,dy,wheel=0;
|
||||
if(!state||!event||!state->Width||!state->Height)return -1;
|
||||
if(!state->PacketLength&&!(value&8))return -1;
|
||||
if(state->PacketSize!=3&&state->PacketSize!=4)return -1;
|
||||
state->Packet[state->PacketLength++]=value;
|
||||
if(state->PacketLength<state->PacketSize)return -1;
|
||||
state->PacketLength=0;
|
||||
flags=state->Packet[0];buttons=(u8)(flags&7);
|
||||
if(state->PacketSize==4) {
|
||||
u8 wheel_nibble=(u8)(state->Packet[3]&15);
|
||||
wheel=(wheel_nibble&8)?(s32)wheel_nibble-16:(s32)wheel_nibble;
|
||||
if(state->DeviceId==4)buttons|=(u8)((state->Packet[3]&0x30)>>1);
|
||||
}
|
||||
dx=(flags&0x40)?0:signed_byte(state->Packet[1]);
|
||||
dy=(flags&0x80)?0:-signed_byte(state->Packet[2]);
|
||||
if(state==&mouse_state) {
|
||||
mouse_ps2_buttons=buttons;
|
||||
buttons=mouse_combined_buttons();
|
||||
}
|
||||
return mouse_apply_report(state,buttons,dx,dy,wheel,event);
|
||||
}
|
||||
|
||||
int bob64_mouse_usb_report_device(u32 device_index,u8 buttons,s8 delta_x,
|
||||
s8 delta_y,s8 wheel) {
|
||||
BOB64_EVENT event;
|
||||
int result;
|
||||
if(device_index>=BOB64_MOUSE_USB_DEVICE_LIMIT)return -1;
|
||||
#if defined(BOB64_UEFI_ABI)
|
||||
u64 flags;
|
||||
__asm__ volatile("pushfq; popq %0; cli":"=r"(flags)::"memory");
|
||||
#endif
|
||||
mouse_usb_buttons[device_index]=(u8)(buttons&7u);
|
||||
result=mouse_apply_report(&mouse_state,mouse_combined_buttons(),
|
||||
(s32)delta_x,(s32)delta_y,(s32)wheel,&event);
|
||||
if(!result)result=mouse_queue_event(&event);
|
||||
#if defined(BOB64_UEFI_ABI)
|
||||
if(flags&(1ULL<<9))__asm__ volatile("sti":::"memory");
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
int bob64_mouse_usb_report(u8 buttons,s8 delta_x,s8 delta_y,s8 wheel) {
|
||||
return bob64_mouse_usb_report_device(0,buttons,delta_x,delta_y,wheel);
|
||||
}
|
||||
|
||||
int bob64_mouse_init(u32 width,u32 height) {
|
||||
u8 device_id;
|
||||
if(bob64_mouse_irq_queue_init(width,height))return -1;
|
||||
@@ -162,8 +227,9 @@ int bob64_mouse_init(u32 width,u32 height) {
|
||||
|
||||
int bob64_mouse_poll_event(BOB64_EVENT *event) {
|
||||
u8 status,value;
|
||||
if(!event||!mouse_state.Enabled)return -1;
|
||||
if(mouse_irq_active)return bob64_mouse_irq_pop_event(event);
|
||||
if(!event)return -1;
|
||||
if(!bob64_mouse_irq_pop_event(event))return 0;
|
||||
if(mouse_irq_active||!mouse_state.Enabled)return -1;
|
||||
status=in8(0x64);
|
||||
if(status==0xff||!(status&1)||!(status&0x20))return -1;
|
||||
value=in8(0x60);
|
||||
|
||||
Reference in new issue
Block a user