-
Notifications
You must be signed in to change notification settings - Fork 3
/
zlox_mouse.c
171 lines (154 loc) · 3.66 KB
/
zlox_mouse.c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// zlox_mouse.c -- something relate to the mouse driver
#include "zlox_mouse.h"
#include "zlox_isr.h"
#include "zlox_task.h"
#include "zlox_my_windows.h"
#include "zlox_ata.h"
#include "zlox_monitor.h"
#define MOUSE_PORT 0x60
#define MOUSE_STATUS 0x64
#define MOUSE_ABIT 0x02
#define MOUSE_BBIT 0x01
#define MOUSE_WRITE 0xD4
#define MOUSE_F_BIT 0x20
#define MOUSE_V_BIT 0x08
// zlox_my_windows.c
extern ZLOX_MY_WINDOW * mywin_list_header;
ZLOX_UINT8 mouse_cycle=0; //unsigned char
ZLOX_UINT8 mouse_byte[3]; //signed char
ZLOX_FLOAT mouse_scale_factor = 1.0;
static ZLOX_VOID zlox_mouse_clear()
{
mouse_cycle=0;
mouse_byte[0] = mouse_byte[1] = mouse_byte[2] = 0;
return;
}
//Mouse functions
ZLOX_VOID zlox_mouse_handler(/*ZLOX_ISR_REGISTERS * regs*/)
{
ZLOX_UINT8 status = zlox_inb(MOUSE_STATUS);
while (status & MOUSE_BBIT) {
ZLOX_UINT8 mouse_in = zlox_inb(MOUSE_PORT);
if (status & MOUSE_F_BIT) {
switch(mouse_cycle)
{
case 0:
mouse_byte[0] = mouse_in;
if (!(mouse_in & MOUSE_V_BIT))
{
zlox_mouse_clear();
return;
}
mouse_cycle++;
break;
case 1:
mouse_byte[1] = mouse_in;
mouse_cycle++;
break;
case 2:
mouse_byte[2] = mouse_in;
/* We now have a full mouse packet ready to use */
if (mouse_byte[0] & 0x80 || mouse_byte[0] & 0x40) {
/* x/y overflow? bad packet! */
zlox_mouse_clear();
break;
}
ZLOX_TASK_MSG msg = {0};
msg.type = ZLOX_MT_MOUSE;
msg.mouse.state = mouse_byte[0];
if((msg.mouse.state & 0x10) != 0)
msg.mouse.rel_x = (ZLOX_SINT32)((ZLOX_UINT32)(mouse_byte[1] & 0x0FF) | (0xFFFFFF00));
else
msg.mouse.rel_x = (ZLOX_SINT32)((ZLOX_UINT32)(mouse_byte[1] & 0x0FF));
if((msg.mouse.state & 0x20) != 0)
msg.mouse.rel_y = (ZLOX_SINT32)((ZLOX_UINT32)(mouse_byte[2] & 0x0FF) | (0xFFFFFF00));
else
msg.mouse.rel_y = (ZLOX_SINT32)((ZLOX_UINT32)(mouse_byte[2] & 0x0FF));
if(mouse_scale_factor > 1.0)
{
if(msg.mouse.rel_x > 10 || msg.mouse.rel_x < -10)
msg.mouse.rel_x = msg.mouse.rel_x * mouse_scale_factor;
if(msg.mouse.rel_y > 10 || msg.mouse.rel_y < -10)
msg.mouse.rel_y = msg.mouse.rel_y * mouse_scale_factor;
}
if(mywin_list_header != ZLOX_NULL)
{
zlox_update_for_mymouse(&msg);
}
zlox_mouse_clear();
break;
default:
zlox_mouse_clear();
break;
}
}
status = zlox_inb(MOUSE_STATUS);
}
}
ZLOX_VOID zlox_mouse_wait(ZLOX_UINT8 a_type) //unsigned char
{
ZLOX_UINT32 _time_out=100000; //unsigned int
if(a_type==0)
{
while(_time_out--) //Data
{
if((zlox_inb(0x64) & 1)==1)
{
return;
}
}
return;
}
else
{
while(_time_out--) //Signal
{
if((zlox_inb(0x64) & 2)==0)
{
return;
}
}
return;
}
}
ZLOX_VOID zlox_mouse_write(ZLOX_UINT8 a_write) //unsigned char
{
//Wait to be able to send a command
zlox_mouse_wait(1);
//Tell the mouse we are sending a command
zlox_outb(0x64, 0xD4);
//Wait for the final part
zlox_mouse_wait(1);
//Finally write
zlox_outb(0x60, a_write);
}
ZLOX_UINT8 zlox_mouse_read()
{
//Get's response from mouse
zlox_mouse_wait(0);
return zlox_inb(0x60);
}
ZLOX_VOID zlox_mouse_install()
{
// Flush The Output Buffer
while(zlox_inb(0x64) & 0x1)
zlox_inb(0x60);
//Tell the mouse to use default settings
zlox_mouse_write(0xF6);
zlox_mouse_read(); //Acknowledge
//Enable Packet Streaming
zlox_mouse_write(0xF4);
zlox_mouse_read(); //Acknowledge
zlox_mouse_clear();
//Setup the mouse handler
zlox_register_interrupt_callback(ZLOX_IRQ12, zlox_mouse_handler);
}
ZLOX_SINT32 zlox_mouse_set_scale(ZLOX_FLOAT * scale)
{
if((*scale) >= 1.0 && (*scale) <= 4.0)
{
mouse_scale_factor = (*scale);
return 0;
}
return -1;
}