-
Notifications
You must be signed in to change notification settings - Fork 1
/
mouse.c
286 lines (246 loc) · 7.04 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "types.h"
#include "defs.h"
#include "spinlock.h"
#include "x86.h"
#include "traps.h"
#include "mouse.h"
#include "gui.h"
#include "message.h"
#include "queue.h"
#include "windows.h"
unsigned char
mouse_shape[Mouse_Shape_Height][Mouse_Shape_Height] =
{
{2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0},
{2, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0},
{2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 0, 0, 0},
{2, 1, 1, 1, 2, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0},
{2, 1, 1, 2, 0, 2, 1, 1, 2, 0, 0, 0, 0, 0, 0},
{2, 1, 2, 0, 0, 2, 1, 1, 2, 0, 0, 0, 0, 0, 0},
{2, 2, 0, 0, 0, 0, 2, 1, 1, 2, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0}
};
static struct spinlock mouse_lock;
struct mouse_info_struct mouse_info;
static struct mouse_dec_struct mouse_dec;
#define Buf_Size 128
struct fifo_buf device_buf;
fifo_type _device_buf[Buf_Size];
void mouseWait(char type);
void mouseWrite(unsigned char _cmd);
uchar mouseRead();
void mouseInit();
void mouseHandle();
void mouseIntr();
int mouseInfoDecode(uint code);
void mouseWait(char type)
{
int time_out = 100000;
if (type == 0)//Data
{
for (;time_out > 0; time_out--)
if ((inb(Port_KeyStatus) & 1) == 1)
return;
}
else//Signal
{
for (;time_out > 0; time_out--)
if ((inb(Port_KeyStatus) & 2) == 0)
return;
}
return;
}
void mouseWrite(unsigned char _cmd)
{
//Wait to be able to send a command
mouseWait(1);
//Tell the mouse we are sending a command
outb(Port_KeyCmd, 0xD4);
//Wait for the final part
mouseWait(1);
//Finally write
outb(Port_KeyData, _cmd);
}
uchar mouseRead()
{
//Get response from mouse
mouseWait(0);
return inb(Port_KeyData);
}
void mouseInit()
{
//enable the auxiliary mouse device
mouseWait(1);
outb(Port_KeyCmd, 0xA8);
//modify the "compaq status byte", to enable the interrupt
mouseWait(1);
outb(Port_KeyCmd, 0x20);
//store status byte
uchar status_byte;
mouseWait(0);
status_byte = (inb(Port_KeyData) | 2);//sets the bit 1 (value = 2), enabling the IRQ12
// telling the controller that we will be sending a status byte
mouseWait(1);
outb(Port_KeyCmd, 0x60);
//send the modified status byte
mouseWait(1);
outb(Port_KeyData, status_byte);
//default settings,
//Disables streaming, sets the packet rate to 100 per second,
//and resolution to 4 pixels per mm.
mouseWrite(0xF6);
mouseRead();
mouseWrite(0xFE); //set Sample Rate
mouseRead();
mouseWrite(200);
mouseRead();
mouseWrite(0xE8); //set Resolution
mouseRead();
mouseWrite(0x01);
mouseRead();
//Enable Packet Streaming
mouseWrite(0xF4);
mouseRead();
fifoInit(&device_buf, Buf_Size, _device_buf);
mouse_dec.phase = 1;
mouse_dec.click_flag = 0;
initlock(&mouse_lock, "mouse");
mouse_info.x = 16;
mouse_info.y = 16;
picenable(IRQ_MOUSE);
ioapicenable(IRQ_MOUSE, 0);
}
void mouseIntr()
{
acquire(&mouse_lock);
uint st = inb(Port_KeyStatus);
//0x01 means 0x64 first bit, 0 = empty, 1 = full
if((st & 0x20) == 0)
{
//cprintf("\nWrong\n");
;
}
else
{
uint data = (uint)(inb(Port_KeyData) + Mouse_Offset);
//cprintf("\n mouse data: %x\n", data);
fifoPut(&device_buf, data);
//uint data1 = fifoGet(&device_buf);
deviceMessageProc();
//mouseHandle(0, data1 - Mouse_Offset);
}
release(&mouse_lock);
}
int mouseInfoDecode(uint code)
{
switch (mouse_dec.phase)
{
case 0:
//cprintf("code: %d", code);
if (code == 0xFA)
{
mouse_dec.phase = 1;
//cprintf("0XFA received");
}
return 0;
case 1:
if ((code & 0xc8) == 0x08)
{
mouse_dec.phase = 2;
mouse_dec.buf[0] = code;
////cprintf("Phase1 Complete");
}
return 0;
case 2:
mouse_dec.phase = 3;
mouse_dec.buf[1] = code;
return 0;
case 3:
mouse_dec.phase = 1;
mouse_dec.buf[2] = code;
mouse_dec.button = mouse_dec.buf[0] & 0x07;//0x07 means lowest three digits
mouse_dec.delta_x = mouse_dec.buf[1];
mouse_dec.delta_y = mouse_dec.buf[2];
if ((mouse_dec.buf[0] & 0x10) != 0)
mouse_dec.delta_x |= 0xFFFFFF00;
if ((mouse_dec.buf[0] & 0x20) != 0)
mouse_dec.delta_y |= 0xFFFFFF00;
mouse_dec.delta_y = -mouse_dec.delta_y;
return 1;
default:
;//Nothing
}
return -1;
}
void mouseHandle(uint ticks, uint data)
{
// uint i = fifoGet(&mouse_buf);
if (mouseInfoDecode(data) != 0)
{
//cprintf("\nmouse_dec: %d %d %d\n", mouse_dec.buf[0],
//mouse_dec.buf[1], mouse_dec.buf[2]);
struct message msg;
msg.type = WM_MOUSEMOVE;
if ((mouse_dec.button & 0x01) != 0)
{
msg.type = WM_LBTNDOWN;
if (!mouse_dec.click_flag)
{
mouse_dec.click_flag = 1;
mouse_dec.last_clcik = ticks;
}
else
{
if (ticks - mouse_dec.last_clcik < Double_Click_Intervals)
{
cprintf("\nmouse double: %d", ticks - mouse_dec.last_clcik);
msg.type = WM_LDOUBLE;
}
mouse_dec.click_flag = 0;
}
} //mouse_info.btn = 0;
//printInfo("MouseLeft");
if ((mouse_dec.button & 0x02) != 0)
msg.type = WM_RBTNDOWN;
//mouse_info.btn = 1;
//printInfo("MouseRight");
if ((mouse_dec.button & 0x04) != 0)
msg.type = WM_MBTNDOWN;
//mouse_info.btn = 2;
//printInfo("MouseMiddle");
//cprintf("\nmouse_pos before: %d %d\n", mouse_info.x, mouse_info.y);
drawScreenToScreen(video_info.cache2, video_info.video_memory, mouse_info.x,
mouse_info.y, Mouse_Shape_Width, Mouse_Shape_Height, mouse_info.x,
mouse_info.y);
// drawContentToContent(video_info.cache2, video_info.video_memory, 0,
// 0, video_info.screen_width, video_info.screen_height, 0,
// 0, video_info.screen_width, video_info.screen_height);
mouse_info.x += mouse_dec.delta_x;
mouse_info.y += mouse_dec.delta_y;
//cprintf("\nmouse_pos after: %d %d\n", mouse_info.x, mouse_info.y);
if (mouse_info.x < 0)
mouse_info.x = 0;
if (mouse_info.x + Mouse_Shape_Width > video_info.screen_width)
mouse_info.x = video_info.screen_width - Mouse_Shape_Width - 1;
if (mouse_info.y < 0)
mouse_info.y = 0;
if (mouse_info.y + Mouse_Shape_Height > video_info.screen_height)
mouse_info.y = video_info.screen_height - Mouse_Shape_Height - 1;
msg.params[0] = mouse_info.x;
msg.params[1] = mouse_info.y;
drawCursor(video_info.video_memory, mouse_info.x, mouse_info.y);
messageHandle(msg);
//drawContentToContent(video_info.cache2, video_info.memory, )
//cprintf("\nmouse_pos: %d %d\n", mouse_info.x, mouse_info.y);
}
}