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
|
/*
* VBMouse - win16 mouse driver entry points
* Copyright (C) 2022 Javier S. Pedro
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <windows.h>
#include "dlog.h"
#include "utils.h"
#include "int33.h"
#include "int2fwin.h"
#include "w16mouse.h"
/** If 1, hook int2f to detect fullscreen DOSBoxes and auto-disable this driver. */
#define HOOK_INT2F 0
#define MOUSE_NUM_BUTTONS 2
/** The routine Windows gave us which we should use to report events. */
static LPFN_MOUSEEVENT eventproc;
/** Current status of the mouse driver (see MOUSEFLAGS_*). */
static unsigned char mouseflags;
enum {
MOUSEFLAGS_ENABLED = 1 << 0,
MOUSEFLAGS_HAS_VBOX = 1 << 1,
MOUSEFLAGS_VBOX_ENABLED = 1 << 2,
MOUSEFLAGS_HAS_WIN386 = 1 << 3,
MOUSEFLAGS_INT2F_HOOKED = 1 << 4
};
#if HOOK_INT2F
/** Existing interrupt2f handler. */
static LPFN prev_int2f_handler;
#endif
/* This is how events are delivered to Windows */
static void send_event(unsigned short Status, short deltaX, short deltaY, short ButtonCount, short extra1, short extra2);
#pragma aux (MOUSEEVENTPROC) send_event = \
"call dword ptr [eventproc]"
#pragma code_seg ( "CALLBACKS" )
static void FAR int33_mouse_callback(uint16_t events, uint16_t buttons, int16_t x, int16_t y, int16_t delta_x, int16_t delta_y)
#pragma aux (INT33_CB) int33_mouse_callback
{
int status = 0;
if (events & INT33_EVENT_MASK_LEFT_BUTTON_PRESSED) status |= SF_B1_DOWN;
if (events & INT33_EVENT_MASK_LEFT_BUTTON_RELEASED) status |= SF_B1_UP;
if (events & INT33_EVENT_MASK_RIGHT_BUTTON_PRESSED) status |= SF_B2_DOWN;
if (events & INT33_EVENT_MASK_RIGHT_BUTTON_RELEASED) status |= SF_B2_UP;
if (events & INT33_EVENT_MASK_MOVEMENT) {
status |= SF_MOVEMENT | SF_ABSOLUTE;
}
(void) buttons;
(void) delta_x;
(void) delta_y;
#if 0
dlog_print("w16mouse: event status=");
dlog_printx(status);
dlog_print(" x=");
dlog_printx(x);
dlog_print(" y=");
dlog_printx(y);
dlog_endline();
#endif
send_event(status, (uint16_t)(x) * 2, (uint16_t)(y) * 2, MOUSE_NUM_BUTTONS, 0, 0);
}
#if HOOK_INT2F
static void display_switch_handler(int function)
#pragma aux display_switch_handler parm caller [ax] modify [ax bx cx dx si di]
{
if (!(mouseflags & MOUSEFLAGS_ENABLED) || !(mouseflags & MOUSEFLAGS_VBOX_ENABLED)) {
return;
}
switch (function) {
case INT2F_NOTIFY_BACKGROUND_SWITCH:
dlog_puts("Going background\n");
break;
case INT2F_NOTIFY_FOREGROUND_SWITCH:
dlog_puts("Going foreground\n");
break;
}
}
/** Interrupt 2F handler, which will be called on some Windows 386 mode events.
* This is more complicated than it should be becaused we have to fetch our DS
* without clobbering any registers whatsoever, and chain back to the previous handler.
* @todo OpenWatcom 2.x insists on calling GETDS from a different code segment, so we can't use __interrupt. */
static void __declspec(naked) __far int2f_handler(void)
{
_asm {
; Preserve data segment
push ds
; Load our data segment
push ax
mov ax, SEG prev_int2f_handler ; Let's hope that Windows relocates segments with interrupts disabled
mov ds, ax
pop ax
; Check functions we are interested in hooking
cmp ax, 0x4001 ; Notify Background Switch
je handle_it
cmp ax, 0x4002 ; Notify Foreground Switch
je handle_it
; Otherwise directly jump to next handler
jmp next_handler
handle_it:
pushad ; Save and restore 32-bit registers, we may clobber them from C
call display_switch_handler
popad
next_handler:
; Store the address of the previous handler
push dword ptr [prev_int2f_handler]
; Restore original data segment without touching the stack,
; since we want to keep the prev handler address at the top
push bp
mov bp, sp
mov ds, [bp + 6] ; Stack looks like 0: bp, 2: prev_int2f_handler, 6: ds
pop bp
retf 2
}
}
#endif /* HOOK_INT2F */
#pragma code_seg ()
/* Driver exported functions. */
/** DLL entry point (or driver initialization routine).
* The initialization routine should check whether a mouse exists.
* @return nonzero value indicates a mouse exists.
*/
#pragma off (unreferenced);
BOOL FAR PASCAL LibMain(HINSTANCE hInstance, WORD wDataSegment,
WORD wHeapSize, LPSTR lpszCmdLine)
#pragma pop (unreferenced);
{
// We are not going to bother checking whether a PS2 mouse exists and just assume it does
#if HOOK_INT2F
// Check now whether we are running under protected mode windows
if (windows_386_enhanced_mode()) {
mouseflags |= MOUSEFLAGS_HAS_WIN386;
}
#endif
// When running under protected mode Windows, let's tell VMD (the mouse virtualizer)
// what type of mouse we are going to be using
if (mouseflags & MOUSEFLAGS_HAS_WIN386) {
LPFN vmd_entry = win_get_vxd_api_entry(VMD_DEVICE_ID);
if (vmd_entry) {
vmd_set_mouse_type(&vmd_entry, VMD_TYPE_PS2, 0x33, 0);
}
}
return 1;
}
/** Called by Windows to retrieve information about the mouse hardware. */
WORD FAR PASCAL Inquire(LPMOUSEINFO lpMouseInfo)
{
lpMouseInfo->msExist = 1;
lpMouseInfo->msRelative = 0;
lpMouseInfo->msNumButtons = MOUSE_NUM_BUTTONS;
lpMouseInfo->msRate = 80;
return sizeof(MOUSEINFO);
}
/** Called by Windows to enable the mouse driver.
* @param lpEventProc Callback function to call when a mouse event happens. */
VOID FAR PASCAL Enable(LPFN_MOUSEEVENT lpEventProc)
{
// Store the windows-given callback
cli(); // Write to far pointer may not be atomic, and we could be interrupted mid-write
eventproc = lpEventProc;
sti();
if (!(mouseflags & MOUSEFLAGS_ENABLED)) {
dlog_puts("w16mouse: enable");
int33_reset();
int33_set_horizontal_window(0, 0x7FFF);
int33_set_vertical_window(0, 0x7FFF);
int33_set_event_handler(INT33_EVENT_MASK_ALL, int33_mouse_callback);
dlog_puts("w16mouse: int33 enabled");
mouseflags |= MOUSEFLAGS_ENABLED;
#if HOOK_INT2F
if ((mouseflags & MOUSEFLAGS_HAS_WIN386) && (mouseflags & MOUSEFLAGS_VBOX_ENABLED)) {
cli();
hook_int2f(&prev_int2f_handler, int2f_handler);
sti();
dlog_puts("int2F hooked!\n");
mouseflags |= MOUSEFLAGS_INT2F_HOOKED;
}
#endif
}
}
/** Called by Windows to disable the mouse driver. */
VOID FAR PASCAL Disable(VOID)
{
if (mouseflags & MOUSEFLAGS_ENABLED) {
dlog_puts("w16mouse: disable");
#if HOOK_INT2F
if (mouseflags & MOUSEFLAGS_INT2F_HOOKED) {
cli();
unhook_int2f(prev_int2f_handler);
sti();
dlog_puts("int2F unhooked!\n");
mouseflags &= ~MOUSEFLAGS_INT2F_HOOKED;
}
#endif
int33_reset();
dlog_puts("w16mouse: int33 reset");
mouseflags &= ~MOUSEFLAGS_ENABLED;
}
}
/** Called by Window to retrieve the interrupt vector number used by this driver, or -1. */
int FAR PASCAL MouseGetIntVect(VOID)
{
return 0x33;
}
|