117 lines
2.7 KiB
C
117 lines
2.7 KiB
C
|
#include <stdio.h>
|
||
|
#include <debugio.h>
|
||
|
#include "mouse.h"
|
||
|
#include "../keyboard/keyboard.h"
|
||
|
|
||
|
uint8 MouseCycle = 0;
|
||
|
uint8 MouseCycleExpected = 3;
|
||
|
uint8 Packets[4];
|
||
|
|
||
|
Point MousePosition = {0,0};
|
||
|
Point MouseMinimumPosition = {0,0}, MouseMaximumPosition = {80, 25};
|
||
|
Point Prev = {0,0};
|
||
|
|
||
|
#define SpeedLimit 0x8
|
||
|
|
||
|
// IRQ12
|
||
|
void MouseHandler (_RegsStack32* UNUSED(r))
|
||
|
{
|
||
|
uint8 data = inportb(0x60);
|
||
|
Point delta;
|
||
|
|
||
|
if (MouseCycle == 0 && (data == 0 || data == 0xFA || data == 0xFF || data == 0xAA)) return;
|
||
|
Packets[MouseCycle++] = data;
|
||
|
|
||
|
// Cycle ended
|
||
|
if (MouseCycle >= MouseCycleExpected)
|
||
|
{
|
||
|
MouseCycle = 0;
|
||
|
if (Packets[0] & 0xC0) return; // Discard packet
|
||
|
|
||
|
// Update X position
|
||
|
if (Packets[0] & 0x10) delta.X = (int32) (Packets[1] | 0xFFFFFF00);
|
||
|
else delta.X = (int32) Packets[1];
|
||
|
|
||
|
// Update Y position
|
||
|
if (Packets[0] & 0x20) delta.Y = -1 * (int32) (Packets[2] | 0xFFFFFF00);
|
||
|
else delta.Y = -1 * (int32)Packets[2];
|
||
|
|
||
|
if (delta.X >= SpeedLimit) delta.X = SpeedLimit;
|
||
|
if (delta.X <= -SpeedLimit) delta.X = -SpeedLimit;
|
||
|
if (delta.Y >= SpeedLimit) delta.Y = SpeedLimit;
|
||
|
if (delta.Y <= -SpeedLimit) delta.Y = -SpeedLimit;
|
||
|
|
||
|
MousePosition.X += delta.X;
|
||
|
MousePosition.Y += delta.Y;
|
||
|
|
||
|
// Check limits
|
||
|
if (MousePosition.X < MouseMinimumPosition.X) MousePosition.X = MouseMinimumPosition.X;
|
||
|
if (MousePosition.Y < MouseMinimumPosition.Y) MousePosition.Y = MouseMinimumPosition.Y;
|
||
|
if (MousePosition.X >= MouseMaximumPosition.X) MousePosition.X = MouseMaximumPosition.X - 1;
|
||
|
if (MousePosition.Y >= MouseMaximumPosition.Y) MousePosition.Y = MouseMaximumPosition.Y - 1;
|
||
|
|
||
|
/* TESTING ONLY */
|
||
|
ConsoleSetColor(Prev, 0xFF - ConsoleGetColor(Prev));
|
||
|
ConsoleSetColor(MousePosition, 0xFF - ConsoleGetColor(MousePosition));
|
||
|
Prev = MousePosition;
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MouseSendCommand(uint8 command)
|
||
|
{
|
||
|
KeyboardWaitOutport();
|
||
|
outportb(0x64, 0xD4);
|
||
|
KeyboardWaitOutport();
|
||
|
outportb(0x60, command);
|
||
|
}
|
||
|
|
||
|
uint8 MouseReadData ()
|
||
|
{
|
||
|
KeyboardWaitInport();
|
||
|
return inportb(0x60);
|
||
|
}
|
||
|
|
||
|
void MouseInstall()
|
||
|
{
|
||
|
KeyboardWaitOutport();
|
||
|
outportb(0x64, 0xA8);
|
||
|
|
||
|
// Enable interrupts
|
||
|
KeyboardWaitOutport();
|
||
|
outportb(0x64, 0x20);
|
||
|
|
||
|
KeyboardWaitInport();
|
||
|
uint8 temp = inportb(0x60) | 2;
|
||
|
temp &= ~0x20;
|
||
|
|
||
|
KeyboardWaitOutport();
|
||
|
outportb(0x64, 0x60);
|
||
|
|
||
|
KeyboardWaitOutport();
|
||
|
outportb(0x60, temp);
|
||
|
|
||
|
// Reset mouse, and enable it
|
||
|
MouseSendCommand(MouseCommandReset);
|
||
|
MouseReadData(); MouseReadData();
|
||
|
|
||
|
MouseSendCommand(MouseCommandSetDefaults);
|
||
|
MouseReadData(); // Read ack
|
||
|
|
||
|
MouseSendCommand(MouseCommandEnableDataReporting);
|
||
|
MouseReadData(); // Read ack
|
||
|
|
||
|
}
|
||
|
|
||
|
void MouseSetLimits (Point min_pos, Point max_pos)
|
||
|
{
|
||
|
MouseMinimumPosition = min_pos;
|
||
|
MouseMaximumPosition = max_pos;
|
||
|
}
|
||
|
|
||
|
MouseState MouseGetState()
|
||
|
{
|
||
|
MouseState ret = { Packets[0] & 0x7, MousePosition};
|
||
|
return ret;
|
||
|
}
|