-
Notifications
You must be signed in to change notification settings - Fork 0
/
DGLMOUSE.C
executable file
·217 lines (174 loc) · 5.36 KB
/
DGLMOUSE.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
#include "dglmouse.h"
#include "dgl.h"
#include "dglevent.h"
#include <string.h>
#include <dos.h>
static bool _installed = false;
static bool _has_mouse = false;
static INPUTEVENT *mouse_event;
volatile int mouse_x;
volatile int mouse_y;
volatile int mouse_buttons;
volatile int mouse_delta_x;
volatile int mouse_delta_y;
volatile int mouse_prev_buttons;
static void reset_mouse_state(void) {
mouse_x = 0;
mouse_y = 0;
mouse_buttons = 0;
mouse_delta_x = 0;
mouse_delta_y = 0;
mouse_prev_buttons = 0;
}
static bool init_mouse_driver(void) {
union REGS regs;
memset(®s, 0, sizeof(regs));
regs.w.ax = 0x00;
int386(0x33, ®s, ®s);
return (regs.w.ax != 0);
}
static void update_mouse_state(void) {
union REGS regs;
memset(®s, 0, sizeof(regs));
regs.w.ax = 0x03;
int386(0x33, ®s, ®s);
mouse_x = (regs.w.cx / 2);
mouse_y = regs.w.dx;
mouse_prev_buttons = mouse_buttons;
mouse_buttons = regs.w.bx;
mouse_delta_x = 0;
mouse_delta_y = 0;
}
static void push_motion_event(void) {
_events_push(&mouse_event);
mouse_event->type = EVENT_TYPE_MOUSE_MOTION;
mouse_event->mouse_motion.x = mouse_x;
mouse_event->mouse_motion.y = mouse_y;
mouse_event->mouse_motion.x_delta = mouse_delta_x;
mouse_event->mouse_motion.y_delta = mouse_delta_y;
mouse_event->mouse_motion.buttons = mouse_buttons;
}
static void push_button_event(EVENT_ACTION action, MOUSE_BUTTON button) {
_events_push(&mouse_event);
mouse_event->type = EVENT_TYPE_MOUSE_BUTTON;
mouse_event->mouse_button.x = mouse_x;
mouse_event->mouse_button.y = mouse_y;
mouse_event->mouse_button.action = action;
mouse_event->mouse_button.button = button;
}
#pragma off (check_stack)
void __loadds far mouse_int_handler(int eax, int ebx, int ecx, int edx) {
#pragma aux mouse_int_handler parm [eax] [ebx] [ecx] [edx]
mouse_delta_x = (ecx / 2) - mouse_x;
mouse_delta_y = edx - mouse_y;
mouse_x = (ecx / 2);
mouse_y = edx;
mouse_prev_buttons = mouse_buttons;
mouse_buttons = ebx;
if (_events_enabled) {
if (mouse_delta_x || mouse_delta_y) {
push_motion_event();
}
if (mouse_buttons != mouse_prev_buttons) {
if ((mouse_buttons & MOUSE_LEFTBUTTON) !=
(mouse_prev_buttons & MOUSE_LEFTBUTTON)) {
if (mouse_buttons & MOUSE_LEFTBUTTON)
push_button_event(EVENT_ACTION_PRESSED, MOUSE_LEFTBUTTON);
else
push_button_event(EVENT_ACTION_RELEASED, MOUSE_LEFTBUTTON);
}
if ((mouse_buttons & MOUSE_RIGHTBUTTON) !=
(mouse_prev_buttons & MOUSE_RIGHTBUTTON)) {
if (mouse_buttons & MOUSE_RIGHTBUTTON)
push_button_event(EVENT_ACTION_PRESSED, MOUSE_RIGHTBUTTON);
else
push_button_event(EVENT_ACTION_RELEASED, MOUSE_RIGHTBUTTON);
}
if ((mouse_buttons & MOUSE_CENTERBUTTON) !=
(mouse_prev_buttons & MOUSE_CENTERBUTTON)) {
if (mouse_buttons & MOUSE_CENTERBUTTON)
push_button_event(EVENT_ACTION_PRESSED, MOUSE_CENTERBUTTON);
else
push_button_event(EVENT_ACTION_RELEASED, MOUSE_CENTERBUTTON);
}
}
}
}
#pragma on (check_stack)
bool mouse_init(void) {
union REGS regs;
struct SREGS sregs;
if (_installed) {
dgl_set_error(DGL_MOUSE_ALREADY_INITIALIZED);
return false;
}
reset_mouse_state();
_has_mouse = init_mouse_driver();
if (!_has_mouse) {
_installed = true;
return true;
}
update_mouse_state();
memset(®s, 0, sizeof(regs));
memset(&sregs, 0, sizeof(sregs));
regs.w.ax = 0x0c;
regs.w.cx = 31;
regs.x.edx = FP_OFF(mouse_int_handler);
sregs.es = FP_SEG(mouse_int_handler);
int386x(0x33, ®s, ®s, &sregs);
_installed = true;
return true;
}
bool mouse_shutdown(void) {
union REGS regs;
if (!_installed)
return true; // don't care
if (!_has_mouse) {
_installed = false;
return true;
}
memset(®s, 0, sizeof(regs));
regs.w.ax = 0x0c;
regs.w.cx = 0;
int386(0x33, ®s, ®s);
reset_mouse_state();
init_mouse_driver();
_installed = false;
return true;
}
bool mouse_is_initialized(void) {
return _installed;
}
bool mouse_is_present(void) {
return _has_mouse;
}
void mouse_show(void) {
union REGS regs;
if (!_has_mouse)
return;
memset(®s, 0, sizeof(regs));
regs.w.ax = 0x01;
int386(0x33, ®s, ®s);
}
void mouse_hide(void) {
union REGS regs;
if (!_has_mouse)
return;
memset(®s, 0, sizeof(regs));
regs.w.ax = 0x02;
int386(0x33, ®s, ®s);
}
void mouse_set_bounds(int min_x, int min_y, int max_x, int max_y) {
union REGS regs;
if (!_has_mouse)
return;
memset(®s, 0, sizeof(regs));
regs.w.ax = 0x07;
regs.w.cx = min_x;
regs.w.dx = max_x;
int386(0x33, ®s, ®s);
regs.w.ax = 0x08;
regs.w.cx = min_y;
regs.w.dx = max_y;
int386(0x33, ®s, ®s);
}