-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse.c
212 lines (187 loc) · 4.17 KB
/
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "types.h"
#include "x86.h"
#include "defs.h"
#include "msg.h"
#include "spinlock.h"
#include "traps.h"
static struct spinlock mouselock;
static struct {
int x_sgn, y_sgn, x_mov, y_mov;
int l_btn, r_btn, m_btn;
int x_overflow, y_overflow;
uint tick;
} packet;
static int count;
static int recovery;
static int lastbtn, lastdowntick, lastclicktick;
void
mouse_wait(uchar type)
{
uint time_out = 100000;
if(type == 0)
{
while(--time_out)
{
if((inb(0x64) & 1) == 1)
return;
}
}
else
{
while(--time_out)
{
if((inb(0x64) & 2) == 0)
return;
}
}
}
void
mouse_write(uchar word)
{
mouse_wait(1);
outb(0x64, 0xd4);
mouse_wait(1);
outb(0x60, word);
}
uint
mouse_read()
{
mouse_wait(0);
return inb(0x60);
}
void
mouseinit(void)
{
uchar statustemp;
mouse_wait(1);
outb(0x64, 0xa8); //激活鼠标接口
mouse_wait(1); //激活中断
outb(0x64, 0x20);
mouse_wait(0);
statustemp = (inb(0x60) | 2);
mouse_wait(0);
outb(0x64, 0x60);
mouse_wait(1);
outb(0x60, statustemp);
mouse_write(0xf6); //设置鼠标为默认设置
mouse_read();
mouse_write(0xfe); //设置鼠标采样率
mouse_read();
mouse_write(10);
mouse_read();
mouse_write(0xf4);
mouse_read();
initlock(&mouselock, "mouse");
picenable(IRQ_MOUSE);
ioapicenable(IRQ_MOUSE, 0);
count = 0;
lastclicktick = lastdowntick = -1000;
}
void genMouseUpMessage(int btns)
{
message msg;
msg.msg_type = M_MOUSE_UP;
msg.params[0] = btns;
handleMessage(&msg);
}
void
genMouseMessage()
{
if (packet.x_overflow || packet.y_overflow) return;
int x = packet.x_sgn ? (0xffffff00 | (packet.x_mov & 0xff)) : (packet.x_mov & 0xff);
int y = packet.y_sgn ? (0xffffff00 | (packet.y_mov & 0xff)) : (packet.y_mov & 0xff);
/* if(x == 127 || x == -127 || y == 127 || y == -127){
x = 0;
y = 0;
}*/
packet.x_mov = x;
packet.y_mov = y;
int btns = packet.l_btn | (packet.r_btn << 1) | (packet.m_btn << 2);
message msg;
if (packet.x_mov || packet.y_mov)
{
msg.msg_type = M_MOUSE_MOVE;
msg.params[0] = packet.x_mov;
msg.params[1] = packet.y_mov;
msg.params[2] = btns;
lastdowntick = lastclicktick = -1000;
if (btns != lastbtn) genMouseUpMessage(btns);
}
else if (btns)
{
msg.msg_type = M_MOUSE_DOWN;
msg.params[0] = btns;
lastdowntick = packet.tick;
}
else if (packet.tick - lastdowntick < 30)
{
if (lastbtn & 1) msg.msg_type = M_MOUSE_LEFT_CLICK;
else msg.msg_type = M_MOUSE_RIGHT_CLICK;
if (packet.tick - lastclicktick < 60)
{
msg.msg_type = M_MOUSE_DBCLICK;
lastclicktick = -1000;
}
else lastclicktick = packet.tick;
}
else
{
genMouseUpMessage(btns);
}
lastbtn = btns;
handleMessage(&msg);
}
void
mouseintr(uint tick)
{
acquire(&mouselock);
int state;
while (((state = inb(0x64)) & 1) == 1) {
int data = inb(0x60);
count++;
if (recovery == 0 && (data & 255) == 0)
recovery = 1;
else if (recovery == 1 && (data & 255) == 0)
recovery = 2;
else if ((data & 255) == 12)
recovery = 0;
else
recovery = -1;
switch(count)
{
case 1: if(data & 0x08)
{
packet.y_overflow = (data >> 7) & 0x1;
packet.x_overflow = (data >> 6) & 0x1;
packet.y_sgn = (data >> 5) & 0x1;
packet.x_sgn = (data >> 4) & 0x1;
packet.m_btn = (data >> 2) & 0x1;
packet.r_btn = (data >> 1) & 0x1;
packet.l_btn = (data >> 0) & 0x1;
break;
}
else
{
count = 0;
break;
}
case 2: packet.x_mov = data;
break;
case 3: packet.y_mov = data;
packet.tick = tick;
break;
default: count=0; break;
}
if (recovery == 2)
{
count = 0;
recovery = -1;
}
else if (count == 3)
{
count = 0;
genMouseMessage();
}
}
release(&mouselock);
}