2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
15 * $Source: /cvs/cvsroot/d2x/input/dos_mouse.c,v $
18 * $Date: 2001-01-31 14:04:44 $
20 * Functions to access Mouse and Cyberman...
22 * $Log: not supported by cvs2svn $
23 * Revision 1.2 2001/01/29 14:03:57 bradleyb
24 * Fixed build, minor fixes
26 * Revision 1.1.1.2 2001/01/19 03:33:52 bradleyb
27 * Import of d2x-0.0.9-pre1
29 * Revision 1.1.1.1 1999/06/14 21:58:38 donut
30 * Import of d1x 1.37 source.
32 * Revision 1.11 1995/02/10 18:52:17 john
33 * Fixed bug with mouse not getting closed.
35 * Revision 1.10 1995/02/02 11:10:33 john
36 * Changed a bunch of mouse stuff around to maybe get
37 * around PS/2 mouse hang.
39 * Revision 1.9 1995/01/14 19:19:52 john
40 * Fixed signed short error cmp with -1 that caused mouse
41 * to break under Watcom 10.0
43 * Revision 1.8 1994/12/27 12:38:23 john
44 * Made mouse use temporary dos buffer instead of
49 * Revision 1.7 1994/12/05 23:54:53 john
50 * Fixed bug with mouse_get_delta only returning positive numbers..
52 * Revision 1.6 1994/11/18 23:18:18 john
53 * Changed some shorts to ints.
55 * Revision 1.5 1994/09/13 12:34:02 john
56 * Added functions to get down count and state.
58 * Revision 1.4 1994/08/29 20:52:19 john
59 * Added better cyberman support; also, joystick calibration
60 * value return funcctiionn,
62 * Revision 1.3 1994/08/24 18:54:32 john
63 * *** empty log message ***
65 * Revision 1.2 1994/08/24 18:53:46 john
66 * Made Cyberman read like normal mouse; added dpmi module; moved
67 * mouse from assembly to c. Made mouse buttons return time_down.
69 * Revision 1.1 1994/08/24 13:56:37 john
81 #define _BORLAND_DOS_REGS 1
83 _go32_dpmi_registers handler_regs;
101 #define ME_CURSOR_MOVED (1<<0)
102 #define ME_LB_P (1<<1)
103 #define ME_LB_R (1<<2)
104 #define ME_RB_P (1<<3)
105 #define ME_RB_R (1<<4)
106 #define ME_MB_P (1<<5)
107 #define ME_MB_R (1<<6)
108 #define ME_OB_P (1<<7)
109 #define ME_OB_R (1<<8)
110 #define ME_X_C (1<<9)
111 #define ME_Y_C (1<<10)
112 #define ME_Z_C (1<<11)
113 #define ME_P_C (1<<12)
114 #define ME_B_C (1<<13)
115 #define ME_H_C (1<<14)
116 #define ME_O_C (1<<15)
118 #define MOUSE_MAX_BUTTONS 11
120 typedef struct event_info {
127 ushort button_status;
128 ushort device_dependant;
131 typedef struct mouse_info {
135 ubyte pressed[MOUSE_MAX_BUTTONS];
136 fix time_went_down[MOUSE_MAX_BUTTONS];
137 fix time_held_down[MOUSE_MAX_BUTTONS];
138 uint num_downs[MOUSE_MAX_BUTTONS];
139 uint num_ups[MOUSE_MAX_BUTTONS];
141 ushort button_status;
144 typedef struct cyberman_info {
151 ubyte pitch_descriptor;
152 ubyte roll_descriptor;
153 ubyte yaw_descriptor;
157 static mouse_info Mouse;
159 static int Mouse_installed = 0;
162 #define m_ax r->d.eax
168 void mouse_handler (_go32_dpmi_registers *r)
171 #pragma off (check_stack)
172 void _loadds far mouse_handler (int m_ax, int mbx, int mcx, int mdx, int msi, int mdi)
174 #pragma aux mouse_handler parm [EAX] [EBX] [ECX] [EDX] [ESI] [EDI]
176 Mouse.ctime = timer_get_fixed_secondsX();
178 if (m_ax & ME_LB_P) { // left button pressed
179 if (!Mouse.pressed[MB_LEFT]) {
180 Mouse.pressed[MB_LEFT] = 1;
181 Mouse.time_went_down[MB_LEFT] = Mouse.ctime;
183 Mouse.num_downs[MB_LEFT]++;
184 } else if (m_ax & ME_LB_R ) { // left button released
185 if (Mouse.pressed[MB_LEFT]) {
186 Mouse.pressed[MB_LEFT] = 0;
187 Mouse.time_held_down[MB_LEFT] += Mouse.ctime-Mouse.time_went_down[MB_LEFT];
189 Mouse.num_ups[MB_LEFT]++;
192 if (m_ax & ME_RB_P ) { // right button pressed
193 if (!Mouse.pressed[MB_RIGHT]) {
194 Mouse.pressed[MB_RIGHT] = 1;
195 Mouse.time_went_down[MB_RIGHT] = Mouse.ctime;
197 Mouse.num_downs[MB_RIGHT]++;
198 } else if (m_ax & ME_RB_R ) {// right button released
199 if (Mouse.pressed[MB_RIGHT]) {
200 Mouse.pressed[MB_RIGHT] = 0;
201 Mouse.time_held_down[MB_RIGHT] += Mouse.ctime-Mouse.time_went_down[MB_RIGHT];
203 Mouse.num_ups[MB_RIGHT]++;
206 if (m_ax & ME_MB_P ) { // middle button pressed
207 if (!Mouse.pressed[MB_MIDDLE]) {
208 Mouse.pressed[MB_MIDDLE] = 1;
209 Mouse.time_went_down[MB_MIDDLE] = Mouse.ctime;
211 Mouse.num_downs[MB_MIDDLE]++;
212 } else if (m_ax & ME_MB_R ) { // middle button released
213 if (Mouse.pressed[MB_MIDDLE]) {
214 Mouse.pressed[MB_MIDDLE] = 0;
215 Mouse.time_held_down[MB_MIDDLE] += Mouse.ctime-Mouse.time_went_down[MB_MIDDLE];
217 Mouse.num_ups[MB_MIDDLE]++;
220 if (Mouse.cyberman && (m_ax & (ME_Z_C|ME_P_C|ME_B_C|ME_H_C))) {
221 Mouse.x_info = (event_info *)((msi & 0xFFFF) << 4);
223 if (m_ax & ME_Z_C ) { // z axis changed
224 if (Mouse.pressed[MB_Z_UP]) {
226 Mouse.pressed[MB_Z_UP] = 0;
227 Mouse.time_held_down[MB_Z_UP] += Mouse.ctime-Mouse.time_went_down[MB_Z_UP];
228 Mouse.num_ups[MB_Z_UP]++;
229 } else if ( Mouse.x_info->z>0 ) {
231 Mouse.pressed[MB_Z_UP] = 1;
232 Mouse.time_went_down[MB_Z_UP]=Mouse.ctime;
233 Mouse.num_downs[MB_Z_UP]++;
235 if (Mouse.pressed[MB_Z_DOWN]) {
237 Mouse.pressed[MB_Z_DOWN] = 0;
238 Mouse.time_held_down[MB_Z_DOWN] += Mouse.ctime-Mouse.time_went_down[MB_Z_DOWN];
239 Mouse.num_ups[MB_Z_DOWN]++;
240 } else if ( Mouse.x_info->z<0 ) {
242 Mouse.pressed[MB_Z_DOWN] = 1;
243 Mouse.time_went_down[MB_Z_DOWN]=Mouse.ctime;
244 Mouse.num_downs[MB_Z_DOWN]++;
247 if (m_ax & ME_P_C ) { // pitch changed
248 if (Mouse.pressed[MB_PITCH_BACKWARD]) {
249 // pitch backward released
250 Mouse.pressed[MB_PITCH_BACKWARD] = 0;
251 Mouse.time_held_down[MB_PITCH_BACKWARD] += Mouse.ctime-Mouse.time_went_down[MB_PITCH_BACKWARD];
252 Mouse.num_ups[MB_PITCH_BACKWARD]++;
253 } else if ( Mouse.x_info->pitch>0 ) {
254 // pitch backward pressed
255 Mouse.pressed[MB_PITCH_BACKWARD] = 1;
256 Mouse.time_went_down[MB_PITCH_BACKWARD]=Mouse.ctime;
257 Mouse.num_downs[MB_PITCH_BACKWARD]++;
259 if (Mouse.pressed[MB_PITCH_FORWARD]) {
260 // pitch forward released
261 Mouse.pressed[MB_PITCH_FORWARD] = 0;
262 Mouse.time_held_down[MB_PITCH_FORWARD] += Mouse.ctime-Mouse.time_went_down[MB_PITCH_FORWARD];
263 Mouse.num_ups[MB_PITCH_FORWARD]++;
264 } else if ( Mouse.x_info->pitch<0 ) {
265 // pitch forward pressed
266 Mouse.pressed[MB_PITCH_FORWARD] = 1;
267 Mouse.time_went_down[MB_PITCH_FORWARD]=Mouse.ctime;
268 Mouse.num_downs[MB_PITCH_FORWARD]++;
272 if (m_ax & ME_B_C ) { // bank changed
273 if (Mouse.pressed[MB_BANK_LEFT]) {
274 // bank left released
275 Mouse.pressed[MB_BANK_LEFT] = 0;
276 Mouse.time_held_down[MB_BANK_LEFT] += Mouse.ctime-Mouse.time_went_down[MB_BANK_LEFT];
277 Mouse.num_ups[MB_BANK_LEFT]++;
278 } else if ( Mouse.x_info->bank>0 ) {
280 Mouse.pressed[MB_BANK_LEFT] = 1;
281 Mouse.time_went_down[MB_BANK_LEFT]=Mouse.ctime;
282 Mouse.num_downs[MB_BANK_LEFT]++;
284 if (Mouse.pressed[MB_BANK_RIGHT]) {
285 // bank right released
286 Mouse.pressed[MB_BANK_RIGHT] = 0;
287 Mouse.time_held_down[MB_BANK_RIGHT] += Mouse.ctime-Mouse.time_went_down[MB_BANK_RIGHT];
288 Mouse.num_ups[MB_BANK_RIGHT]++;
289 } else if ( Mouse.x_info->bank<0 ) {
290 // bank right pressed
291 Mouse.pressed[MB_BANK_RIGHT] = 1;
292 Mouse.time_went_down[MB_BANK_RIGHT]=Mouse.ctime;
293 Mouse.num_downs[MB_BANK_RIGHT]++;
297 if (m_ax & ME_H_C ) { // heading changed
298 if (Mouse.pressed[MB_HEAD_LEFT]) {
299 // head left released
300 Mouse.pressed[MB_HEAD_LEFT] = 0;
301 Mouse.time_held_down[MB_HEAD_LEFT] += Mouse.ctime-Mouse.time_went_down[MB_HEAD_LEFT];
302 Mouse.num_ups[MB_HEAD_LEFT]++;
303 } else if ( Mouse.x_info->heading>0 ) {
305 Mouse.pressed[MB_HEAD_LEFT] = 1;
306 Mouse.time_went_down[MB_HEAD_LEFT]=Mouse.ctime;
307 Mouse.num_downs[MB_HEAD_LEFT]++;
309 if (Mouse.pressed[MB_HEAD_RIGHT]) {
310 // head right released
311 Mouse.pressed[MB_HEAD_RIGHT] = 0;
312 Mouse.time_held_down[MB_HEAD_RIGHT] += Mouse.ctime-Mouse.time_went_down[MB_HEAD_RIGHT];
313 Mouse.num_ups[MB_HEAD_RIGHT]++;
314 } else if ( Mouse.x_info->heading<0 ) {
315 // head right pressed
316 Mouse.pressed[MB_HEAD_RIGHT] = 1;
317 Mouse.time_went_down[MB_HEAD_RIGHT]=Mouse.ctime;
318 Mouse.num_downs[MB_HEAD_RIGHT]++;
328 void mouse_handler_end (void) // dummy functions
332 #pragma on (check_stack)
335 //--------------------------------------------------------
336 // returns 0 if no mouse
337 // else number of buttons
338 int mouse_init(int enable_cyberman)
345 union REGS inregs, outregs;
346 ubyte *Mouse_dos_mem;
349 return Mouse.num_buttons;
352 if (_farpeekl(_dos_ds, 0x33 * 4) == 0) {
354 if (_dos_getvect(0x33) == NULL) {
356 // No mouse driver loaded
360 // Reset the mouse driver
361 memset( &inregs, 0, sizeof(inregs) );
363 int386(0x33, &inregs, &outregs);
364 if (outregs.w.ax != 0xffff)
367 Mouse.num_buttons = outregs.w.bx;
370 // Enable mouse driver
371 memset( &inregs, 0, sizeof(inregs) );
372 inregs.w.ax = 0x0020;
373 int386(0x33, &inregs, &outregs);
374 if (outregs.w.ax != 0xffff )
377 if ( enable_cyberman ) {
378 Mouse_dos_mem = dpmi_get_temp_low_buffer( 64 );
379 if (Mouse_dos_mem==NULL) {
380 printf( "Unable to allocate DOS buffer in mouse.c\n" );
382 // Check for Cyberman...
383 memset( &rr, 0, sizeof(dpmi_real_regs) );
384 rr.es = DPMI_real_segment(Mouse_dos_mem);
385 rr.edx = DPMI_real_offset(Mouse_dos_mem);
387 dpmi_real_int386x( 0x33, &rr );
389 // SWIFT functions supported
390 ci = (cyberman_info *)Mouse_dos_mem;
391 if (ci->device_type==1) { // Cyberman
393 //printf( "Cyberman mouse detected\n" );
394 Mouse.num_buttons = 11;
400 if (!dpmi_lock_region(&Mouse,sizeof(mouse_info))) {
401 printf( "Unable to lock mouse data region" );
404 if (!dpmi_lock_region((void near *)mouse_handler,(char *)mouse_handler_end - (char near *)mouse_handler)) {
405 printf( "Unable to lock mouse handler" );
409 // Install mouse handler
412 dpmi_real_regs rregs;
413 _go32_dpmi_seginfo info;
414 memset(&rregs, 0, sizeof(rregs));
415 info.pm_offset = (unsigned int)&mouse_handler;
416 if (_go32_dpmi_allocate_real_mode_callback_retf(&info, &handler_regs)) {
417 printf( "Unable allocate mouse handler callback" );
421 rregs.ecx = ME_LB_P|ME_LB_R|ME_RB_P|ME_RB_R|ME_MB_P|ME_MB_R; // watch all 3 button ups/downs
423 rregs.ecx |= ME_Z_C| ME_P_C| ME_B_C| ME_H_C; // if using a cyberman, also watch z, pitch, bank, heading.
424 rregs.edx = info.rm_offset;
425 rregs.es = info.rm_segment;
426 dpmi_real_int386x( 0x33, &rregs );
429 memset( &inregs, 0, sizeof(inregs));
430 memset( &sregs, 0, sizeof(sregs));
432 inregs.w.cx = ME_LB_P|ME_LB_R|ME_RB_P|ME_RB_R|ME_MB_P|ME_MB_R; // watch all 3 button ups/downs
434 inregs.w.cx |= ME_Z_C| ME_P_C| ME_B_C| ME_H_C; // if using a cyberman, also watch z, pitch, bank, heading.
435 inregs.x.edx = FP_OFF(mouse_handler);
436 sregs.es = FP_SEG(mouse_handler);
437 int386x(0x33, &inregs, &outregs, &sregs);
442 atexit( mouse_close );
446 return Mouse.num_buttons;
454 union REGS inregs, outregs;
456 if (Mouse_installed) {
458 // clear mouse handler by setting flags to 0.
459 memset( &inregs, 0, sizeof(inregs));
460 memset( &sregs, 0, sizeof(sregs));
462 inregs.w.cx = 0; // disable event handler by setting to zero.
465 int386x(0x33, &inregs, &outregs, &sregs);
470 void mouse_set_limits( int x1, int y1, int x2, int y2 )
472 union REGS inregs, outregs;
474 if (!Mouse_installed) return;
476 memset( &inregs, 0, sizeof(inregs));
477 inregs.w.ax = 0x7; // Set Horizontal Limits for Pointer
480 int386(0x33, &inregs, &outregs);
482 memset( &inregs, 0, sizeof(inregs));
483 inregs.w.ax = 0x8; // Set Vertical Limits for Pointer
486 int386(0x33, &inregs, &outregs);
489 void mouse_get_pos( int *x, int *y)
491 union REGS inregs, outregs;
493 if (!Mouse_installed) {
497 memset( &inregs, 0, sizeof(inregs));
498 inregs.w.ax = 0x3; // Get Mouse Position and Button Status
499 int386(0x33, &inregs, &outregs);
500 *x = (short)outregs.w.cx;
501 *y = (short)outregs.w.dx;
504 void mouse_get_delta( int *dx, int *dy )
506 union REGS inregs, outregs;
508 if (!Mouse_installed) {
513 memset( &inregs, 0, sizeof(inregs));
514 inregs.w.ax = 0xb; // Read Mouse motion counters
515 int386(0x33, &inregs, &outregs);
516 *dx = (short)outregs.w.cx;
517 *dy = (short)outregs.w.dx;
526 if (!Mouse_installed)
529 for (i=0; i<MOUSE_MAX_BUTTONS; i++ ) {
530 if (Mouse.pressed[i])
537 void mouse_set_pos( int x, int y)
539 union REGS inregs, outregs;
541 if (!Mouse_installed)
544 memset( &inregs, 0, sizeof(inregs));
545 inregs.w.ax = 0x4; // Set Mouse Pointer Position
548 int386(0x33, &inregs, &outregs);
557 if (!Mouse_installed)
562 //Clear the mouse data
563 CurTime =timer_get_fixed_secondsX();
564 for (i=0; i<MOUSE_MAX_BUTTONS; i++ ) {
565 Mouse.pressed[i] = 0;
566 Mouse.time_went_down[i] = CurTime;
567 Mouse.time_held_down[i] = 0;
568 Mouse.num_downs[i]=0;
575 // Returns how many times this button has went down since last call.
576 int mouse_button_down_count(int button)
580 if (!Mouse_installed)
585 count = Mouse.num_downs[button];
586 Mouse.num_downs[button]=0;
593 // Returns 1 if this button is currently down
594 int mouse_button_state(int button)
598 if (!Mouse_installed)
603 state = Mouse.pressed[button];
612 // Returns how long this button has been down since last call.
613 fix mouse_button_down_time(int button)
617 if (!Mouse_installed)
622 if ( !Mouse.pressed[button] ) {
623 time_down = Mouse.time_held_down[button];
624 Mouse.time_held_down[button] = 0;
626 time = timer_get_fixed_secondsX();
627 time_down = time - Mouse.time_went_down[button];
628 Mouse.time_went_down[button] = time;
636 void mouse_get_cyberman_pos( int *x, int *y )
640 ubyte *Mouse_dos_mem;
642 if ( (!Mouse_installed) || (!Mouse.cyberman) ) {
647 Mouse_dos_mem = dpmi_get_temp_low_buffer( 64 );
649 if ( !Mouse_dos_mem ) {
655 memset( &rr, 0, sizeof(dpmi_real_regs) );
656 rr.es = DPMI_real_segment(Mouse_dos_mem);
657 rr.edx = DPMI_real_offset(Mouse_dos_mem);
659 dpmi_real_int386x( 0x33, &rr );
661 ei = (event_info *)Mouse_dos_mem;
663 *x = (((ei->x+8128)*256)/(8064+8128+1)) - 127;
664 *y = (((ei->y+8128)*256)/(8064+8128+1)) - 127;