-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse.c
More file actions
83 lines (67 loc) · 1.67 KB
/
Copy pathmouse.c
File metadata and controls
83 lines (67 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "mouse.h"
#include "ports.h"
#define STATUS 0x64
#define DATA 0x60
static int mouse_x, mouse_y;
static int mouse_buttons;
static int packet_state;
static int packet[3];
static int mouse_initialized;
void mouse_init(void) {
while (inb(STATUS) & 2);
outb(STATUS, 0xA8);
io_wait();
while (inb(STATUS) & 2);
outb(STATUS, 0x20);
while (!(inb(STATUS) & 1));
uint8_t config = inb(DATA);
config |= 0x02;
config &= ~0x20;
while (inb(STATUS) & 2);
outb(STATUS, 0x60);
while (inb(STATUS) & 2);
outb(DATA, config);
while (inb(STATUS) & 2);
outb(STATUS, 0xD4);
io_wait();
while (inb(STATUS) & 2);
outb(DATA, 0xF4);
while (!(inb(STATUS) & 1));
inb(DATA);
mouse_x = 400;
mouse_y = 300;
mouse_buttons = 0;
packet_state = 0;
mouse_initialized = 1;
}
void mouse_handle_byte(uint8_t data) {
if (!mouse_initialized) return;
packet[packet_state++] = data;
if (packet_state == 3) {
int dx = packet[1];
int dy = packet[2];
if (packet[0] & 0x10) dx |= 0xFFFFFF00;
if (packet[0] & 0x20) dy |= 0xFFFFFF00;
dy = -dy;
mouse_x += dx;
mouse_y += dy;
if (mouse_x < 0) mouse_x = 0;
if (mouse_y < 0) mouse_y = 0;
if (mouse_x >= 800) mouse_x = 799;
if (mouse_y >= 600) mouse_y = 599;
mouse_buttons = packet[0] & 0x07;
packet_state = 0;
}
}
void mouse_poll(void) {
uint8_t s = inb(STATUS);
if ((s & 1) && (s & 0x20))
mouse_handle_byte(inb(DATA));
}
void mouse_get_pos(int *x, int *y) {
*x = mouse_x;
*y = mouse_y;
}
int mouse_get_buttons(void) {
return mouse_buttons;
}