Debian Codegraph @

src/ints/mouse.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: mouse.cpp,v 1.80 2009-06-16 19:00:26 qbix79 Exp $ */
20
21 #include <string.h>
22 #include <math.h>
23
24
25 #include "dosbox.h"
26 #include "callback.h"
27 #include "mem.h"
28 #include "regs.h"
29 #include "cpu.h"
30 #include "mouse.h"
31 #include "pic.h"
32 #include "inout.h"
33 #include "int10.h"
34 #include "bios.h"
35 #include "dos_inc.h"
36
37 static Bitu call_int33,call_int74,int74_ret_callback,call_mouse_bd;
38 static Bit16u ps2cbseg,ps2cbofs;
39 static bool useps2callback,ps2callbackinit;
40 static Bitu call_ps2;
41 static RealPt ps2_callback;
42 static Bit16s oldmouseX, oldmouseY;
43 // forward
44 void WriteMouseIntVector(void);
45
46 struct button_event {
47 Bit8u type;
48 Bit8u buttons;
49 };
50
51 #define QUEUE_SIZE 32
52 #define MOUSE_BUTTONS 3
53 #define MOUSE_IRQ 12
54 #define POS_X ((Bit16s)(mouse.x) & mouse.granMask)
55 #define POS_Y (Bit16s)(mouse.y)
56
57 #define CURSORX 16
58 #define CURSORY 16
59 #define HIGHESTBIT (1<<(CURSORX-1))
60
61 static Bit16u defaultTextAndMask = 0x77FF;
62 static Bit16u defaultTextXorMask = 0x7700;
63
64 static Bit16u defaultScreenMask[CURSORY] = {
65 0x3FFF, 0x1FFF, 0x0FFF, 0x07FF,
66 0x03FF, 0x01FF, 0x00FF, 0x007F,
67 0x003F, 0x001F, 0x01FF, 0x00FF,
68 0x30FF, 0xF87F, 0xF87F, 0xFCFF
69 };
70
71 static Bit16u defaultCursorMask[CURSORY] = {
72 0x0000, 0x4000, 0x6000, 0x7000,
73 0x7800, 0x7C00, 0x7E00, 0x7F00,
74 0x7F80, 0x7C00, 0x6C00, 0x4600,
75 0x0600, 0x0300, 0x0300, 0x0000
76 };
77
78 static Bit16u userdefScreenMask[CURSORY];
79 static Bit16u userdefCursorMask[CURSORY];
80
81 static struct {
82 Bit8u buttons;
83 Bit16u times_pressed[MOUSE_BUTTONS];
84 Bit16u times_released[MOUSE_BUTTONS];
85 Bit16u last_released_x[MOUSE_BUTTONS];
86 Bit16u last_released_y[MOUSE_BUTTONS];
87 Bit16u last_pressed_x[MOUSE_BUTTONS];
88 Bit16u last_pressed_y[MOUSE_BUTTONS];
89 Bit16u hidden;
90 float add_x,add_y;
91 Bit16s min_x,max_x,min_y,max_y;
92 float mickey_x,mickey_y;
93 float x,y;
94 button_event event_queue[QUEUE_SIZE];
95 Bit8u events;//Increase if QUEUE_SIZE >255 (currently 32)
96 Bit16u sub_seg,sub_ofs;
97 Bit16u sub_mask;
98
99 bool background;
100 Bit16s backposx, backposy;
101 Bit8u backData[CURSORX*CURSORY];
102 Bit16u* screenMask;
103 Bit16u* cursorMask;
104 Bit16s clipx,clipy;
105 Bit16s hotx,hoty;
106 Bit16u textAndMask, textXorMask;
107
108 float mickeysPerPixel_x;
109 float mickeysPerPixel_y;
110 float pixelPerMickey_x;
111 float pixelPerMickey_y;
112 Bit16u senv_x_val;
113 Bit16u senv_y_val;
114 Bit16u dspeed_val;
115 float senv_x;
116 float senv_y;
117 Bit16u updateRegion_x[2];
118 Bit16u updateRegion_y[2];
119 Bit16u doubleSpeedThreshold;
120 Bit16u language;
121 Bit16u cursorType;
122 Bit16u oldhidden;
123 Bit8u page;
124 bool enabled;
125 bool inhibit_draw;
126 bool timer_in_progress;
127 bool in_UIR;
128 Bit8u mode;
129 Bit16s granMask;
130 } mouse;
131
132 bool Mouse_SetPS2State(bool use) {
133 if (use && (!ps2callbackinit)) {
134 useps2callback = false;
135 PIC_SetIRQMask(MOUSE_IRQ,true);
136 return false;
137 }
138 useps2callback = use;
139 Mouse_AutoLock(useps2callback);
140 PIC_SetIRQMask(MOUSE_IRQ,!useps2callback);
141 return true;
142 }
143
144 void Mouse_ChangePS2Callback(Bit16u pseg, Bit16u pofs) {
145 if ((pseg==0) && (pofs==0)) {
146 ps2callbackinit = false;
147 Mouse_AutoLock(false);
148 } else {
149 ps2callbackinit = true;
150 ps2cbseg = pseg;
151 ps2cbofs = pofs;
152 }
153 Mouse_AutoLock(ps2callbackinit);
154 }
155
156 void DoPS2Callback(Bit16u data, Bit16s mouseX, Bit16s mouseY) {
157 if (useps2callback) {
158 Bit16u mdat = (data & 0x03) | 0x08;
159 Bit16s xdiff = mouseX-oldmouseX;
160 Bit16s ydiff = oldmouseY-mouseY;
161 oldmouseX = mouseX;
162 oldmouseY = mouseY;
163 if ((xdiff>0xff) || (xdiff<-0xff)) mdat |= 0x40; // x overflow
164 if ((ydiff>0xff) || (ydiff<-0xff)) mdat |= 0x80; // y overflow
165 xdiff %= 256;
166 ydiff %= 256;
167 if (xdiff<0) {
168 xdiff = (0x100+xdiff);
169 mdat |= 0x10;
170 }
171 if (ydiff<0) {
172 ydiff = (0x100+ydiff);
173 mdat |= 0x20;
174 }
175 CPU_Push16((Bit16u)mdat);
176 CPU_Push16((Bit16u)(xdiff % 256));
177 CPU_Push16((Bit16u)(ydiff % 256));
178 CPU_Push16((Bit16u)0);
179 CPU_Push16(RealSeg(ps2_callback));
180 CPU_Push16(RealOff(ps2_callback));
181 SegSet16(cs, ps2cbseg);
182 reg_ip = ps2cbofs;
183 }
184 }
185
186 Bitu PS2_Handler(void) {
187 CPU_Pop16();CPU_Pop16();CPU_Pop16();CPU_Pop16();// remove the 4 words
188 return CBRET_NONE;
189 }
190
191
192 #define X_MICKEY 8
193 #define Y_MICKEY 8
194
195 #define MOUSE_HAS_MOVED 1
196 #define MOUSE_LEFT_PRESSED 2
197 #define MOUSE_LEFT_RELEASED 4
198 #define MOUSE_RIGHT_PRESSED 8
199 #define MOUSE_RIGHT_RELEASED 16
200 #define MOUSE_MIDDLE_PRESSED 32
201 #define MOUSE_MIDDLE_RELEASED 64
202 #define MOUSE_DELAY 5.0
203
204 void MOUSE_Limit_Events(Bitu /*val*/) {
205 mouse.timer_in_progress = false;
206 if (mouse.events) {
207 mouse.timer_in_progress = true;
208 PIC_AddEvent(MOUSE_Limit_Events,MOUSE_DELAY);
209 PIC_ActivateIRQ(MOUSE_IRQ);
210 }
211 }
212
213 INLINE void Mouse_AddEvent(Bit8u type) {
214 if (mouse.events<QUEUE_SIZE) {
215 if (mouse.events>0) {
216 /* Skip duplicate events */
217 if (type==MOUSE_HAS_MOVED) return;
218 /* Always put the newest element in the front as that the events are
219 * handled backwards (prevents doubleclicks while moving)
220 */
221 for(Bitu i = mouse.events ; i ; i--)
222 mouse.event_queue[i] = mouse.event_queue[i-1];
223 }
224 mouse.event_queue[0].type=type;
225 mouse.event_queue[0].buttons=mouse.buttons;
226 mouse.events++;
227 }
228 if (!mouse.timer_in_progress) {
229 mouse.timer_in_progress = true;
230 PIC_AddEvent(MOUSE_Limit_Events,MOUSE_DELAY);
231 PIC_ActivateIRQ(MOUSE_IRQ);
232 }
233 }
234
235 // ***************************************************************************
236 // Mouse cursor - text mode
237 // ***************************************************************************
238 /* Write and read directly to the screen. Do no use int_setcursorpos (LOTUS123) */
239 extern void WriteChar(Bit16u col,Bit16u row,Bit8u page,Bit8u chr,Bit8u attr,bool useattr);
240 extern void ReadCharAttr(Bit16u col,Bit16u row,Bit8u page,Bit16u * result);
241
242 void RestoreCursorBackgroundText() {
243 if (mouse.hidden || mouse.inhibit_draw) return;
244
245 if (mouse.background) {
246 WriteChar(mouse.backposx,mouse.backposy,real_readb(BIOSMEM_SEG,BIOSMEM_CURRENT_PAGE),mouse.backData[0],mouse.backData[1],true);
247 mouse.background = false;
248 }
249 }
250
251 void DrawCursorText() {
252 // Restore Background
253 RestoreCursorBackgroundText();
254
255
256 // Save Background
257 mouse.backposx = POS_X>>3;
258 mouse.backposy = POS_Y>>3;
259
260 //use current page (CV program)
261 Bit8u page = real_readb(BIOSMEM_SEG,BIOSMEM_CURRENT_PAGE);
262 Bit16u result;
263
264 ReadCharAttr(mouse.backposx,mouse.backposy,page,&result);
265 mouse.backData[0] = (Bit8u)(result & 0xFF);
266 mouse.backData[1] = (Bit8u)(result>>8);
267 mouse.background = true;
268 // Write Cursor
269 result = (result & mouse.textAndMask) ^ mouse.textXorMask;
270 WriteChar(mouse.backposx,mouse.backposy,page,(Bit8u)(result&0xFF),(Bit8u)(result>>8),true);
271 }
272
273 // ***************************************************************************
274 // Mouse cursor - graphic mode
275 // ***************************************************************************
276
277 static Bit8u gfxReg3CE[9];
278 static Bit8u index3C4,gfxReg3C5;
279 void SaveVgaRegisters() {
280 if (IS_VGA_ARCH) {
281 for (Bit8u i=0; i<9; i++) {
282 IO_Write (0x3CE,i);
283 gfxReg3CE[i] = IO_Read(0x3CF);
284 }
285 /* Setup some default values in GFX regs that should work */
286 IO_Write(0x3CE,3); IO_Write(0x3Cf,0); //disable rotate and operation
287 IO_Write(0x3CE,5); IO_Write(0x3Cf,gfxReg3CE[5]&0xf0); //Force read/write mode 0
288
289 //Set Map to all planes. Celtic Tales
290 index3C4 = IO_Read(0x3c4); IO_Write(0x3C4,2);
291 gfxReg3C5 = IO_Read(0x3C5); IO_Write(0x3C5,0xF);
292 } else if (machine==MCH_EGA) {
293 //Set Map to all planes.
294 IO_Write(0x3C4,2);
295 IO_Write(0x3C5,0xF);
296 }
297 }
298
299 void RestoreVgaRegisters() {
300 if (IS_VGA_ARCH) {
301 for (Bit8u i=0; i<9; i++) {
302 IO_Write(0x3CE,i);
303 IO_Write(0x3CF,gfxReg3CE[i]);
304 }
305
306 IO_Write(0x3C4,2);
307 IO_Write(0x3C5,gfxReg3C5);
308 IO_Write(0x3C4,index3C4);
309 }
310 }
311
312 void ClipCursorArea(Bit16s& x1, Bit16s& x2, Bit16s& y1, Bit16s& y2,
313 Bit16u& addx1, Bit16u& addx2, Bit16u& addy) {
314 addx1 = addx2 = addy = 0;
315 // Clip up
316 if (y1<0) {
317 addy += (-y1);
318 y1 = 0;
319 }
320 // Clip down
321 if (y2>mouse.clipy) {
322 y2 = mouse.clipy;
323 };
324 // Clip left
325 if (x1<0) {
326 addx1 += (-x1);
327 x1 = 0;
328 };
329 // Clip right
330 if (x2>mouse.clipx) {
331 addx2 = x2 - mouse.clipx;
332 x2 = mouse.clipx;
333 };
334 }
335
336 void RestoreCursorBackground() {
337 if (mouse.hidden || mouse.inhibit_draw) return;
338
339 SaveVgaRegisters();
340 if (mouse.background) {
341 // Restore background
342 Bit16s x,y;
343 Bit16u addx1,addx2,addy;
344 Bit16u dataPos = 0;
345 Bit16s x1 = mouse.backposx;
346 Bit16s y1 = mouse.backposy;
347 Bit16s x2 = x1 + CURSORX - 1;
348 Bit16s y2 = y1 + CURSORY - 1;
349
350 ClipCursorArea(x1, x2, y1, y2, addx1, addx2, addy);
351
352 dataPos = addy * CURSORX;
353 for (y=y1; y<=y2; y++) {
354 dataPos += addx1;
355 for (x=x1; x<=x2; x++) {
356 INT10_PutPixel(x,y,mouse.page,mouse.backData[dataPos++]);
357 };
358 dataPos += addx2;
359 };
360 mouse.background = false;
361 };
362 RestoreVgaRegisters();
363 }
364
365 void DrawCursor() {
366 if (mouse.hidden || mouse.inhibit_draw) return;
367 // In Textmode ?
368 if (CurMode->type==M_TEXT) {
369 DrawCursorText();
370 return;
371 }
372
373 // Check video page. Seems to be ignored for text mode.
374 // hence the text mode handled above this
375 if (real_readb(BIOSMEM_SEG,BIOSMEM_CURRENT_PAGE)!=mouse.page) return;
376 // Check if cursor in update region
377 /* if ((POS_X >= mouse.updateRegion_x[0]) && (POS_X <= mouse.updateRegion_x[1]) &&
378 (POS_Y >= mouse.updateRegion_y[0]) && (POS_Y <= mouse.updateRegion_y[1])) {
379 if (CurMode->type==M_TEXT16)
380 RestoreCursorBackgroundText();
381 else
382 RestoreCursorBackground();
383 mouse.shown--;
384 return;
385 }
386 */ /*Not sure yet what to do update region should be set to ??? */
387
388 // Get Clipping ranges
389
390
391 mouse.clipx = (Bit16s)((Bits)CurMode->swidth-1); /* Get from bios ? */
392 mouse.clipy = (Bit16s)((Bits)CurMode->sheight-1);
393
394 /* might be vidmode == 0x13?2:1 */
395 Bit16s xratio = 640;
396 if (CurMode->swidth>0) xratio/=CurMode->swidth;
397 if (xratio==0) xratio = 1;
398
399 RestoreCursorBackground();
400
401 SaveVgaRegisters();
402
403 // Save Background
404 Bit16s x,y;
405 Bit16u addx1,addx2,addy;
406 Bit16u dataPos = 0;
407 Bit16s x1 = POS_X / xratio - mouse.hotx;
408 Bit16s y1 = POS_Y - mouse.hoty;
409 Bit16s x2 = x1 + CURSORX - 1;
410 Bit16s y2 = y1 + CURSORY - 1;
411
412 ClipCursorArea(x1,x2,y1,y2, addx1, addx2, addy);
413
414 dataPos = addy * CURSORX;
415 for (y=y1; y<=y2; y++) {
416 dataPos += addx1;
417 for (x=x1; x<=x2; x++) {
418 INT10_GetPixel(x,y,mouse.page,&mouse.backData[dataPos++]);
419 };
420 dataPos += addx2;
421 };
422 mouse.background= true;
423 mouse.backposx = POS_X / xratio - mouse.hotx;
424 mouse.backposy = POS_Y - mouse.hoty;
425
426 // Draw Mousecursor
427 dataPos = addy * CURSORX;
428 for (y=y1; y<=y2; y++) {
429 Bit16u scMask = mouse.screenMask[addy+y-y1];
430 Bit16u cuMask = mouse.cursorMask[addy+y-y1];
431 if (addx1>0) { scMask<<=addx1; cuMask<<=addx1; dataPos += addx1; };
432 for (x=x1; x<=x2; x++) {
433 Bit8u pixel = 0;
434 // ScreenMask
435 if (scMask & HIGHESTBIT) pixel = mouse.backData[dataPos];
436 scMask<<=1;
437 // CursorMask
438 if (cuMask & HIGHESTBIT) pixel = pixel ^ 0x0F;
439 cuMask<<=1;
440 // Set Pixel
441 INT10_PutPixel(x,y,mouse.page,pixel);
442 dataPos++;
443 };
444 dataPos += addx2;
445 };
446 RestoreVgaRegisters();
447 }
448
449 void Mouse_CursorMoved(float xrel,float yrel,float x,float y,bool emulate) {
450 float dx = xrel * mouse.pixelPerMickey_x;
451 float dy = yrel * mouse.pixelPerMickey_y;
452
453 if((fabs(xrel) > 1.0) || (mouse.senv_x < 1.0)) dx *= mouse.senv_x;
454 if((fabs(yrel) > 1.0) || (mouse.senv_y < 1.0)) dy *= mouse.senv_y;
455 if (useps2callback) dy *= 2;
456
457 mouse.mickey_x += dx;
458 mouse.mickey_y += dy;
459 if (emulate) {
460 mouse.x += dx;
461 mouse.y += dy;
462 } else {
463 if (CurMode->type == M_TEXT) {
464 mouse.x = x*CurMode->swidth;
465 mouse.y = y*CurMode->sheight * 8 / CurMode->cheight;
466 } else if ((mouse.max_x < 2048) || (mouse.max_y < 2048) || (mouse.max_x != mouse.max_y)) {
467 if ((mouse.max_x > 0) && (mouse.max_y > 0)) {
468 mouse.x = x*mouse.max_x;
469 mouse.y = y*mouse.max_y;
470 } else {
471 mouse.x += xrel;
472 mouse.y += yrel;
473 }
474 } else { // Games faking relative movement through absolute coordinates. Quite surprising that this actually works..
475 mouse.x += xrel;
476 mouse.y += yrel;
477 }
478 }
479
480 /* ignore constraints if using PS2 mouse callback in the bios */
481
482 if (!useps2callback) {
483 if (mouse.x > mouse.max_x) mouse.x = mouse.max_x;
484 if (mouse.x < mouse.min_x) mouse.x = mouse.min_x;
485 if (mouse.y > mouse.max_y) mouse.y = mouse.max_y;
486 if (mouse.y < mouse.min_y) mouse.y = mouse.min_y;
487 }
488 Mouse_AddEvent(MOUSE_HAS_MOVED);
489 DrawCursor();
490 }
491
492 void Mouse_CursorSet(float x,float y) {
493 mouse.x=x;
494 mouse.y=y;
495 DrawCursor();
496 }
497
498 void Mouse_ButtonPressed(Bit8u button) {
499 switch (button) {
500 #if (MOUSE_BUTTONS >= 1)
501 case 0:
502 mouse.buttons|=1;
503 Mouse_AddEvent(MOUSE_LEFT_PRESSED);
504 break;
505 #endif
506 #if (MOUSE_BUTTONS >= 2)
507 case 1:
508 mouse.buttons|=2;
509 Mouse_AddEvent(MOUSE_RIGHT_PRESSED);
510 break;
511 #endif
512 #if (MOUSE_BUTTONS >= 3)
513 case 2:
514 mouse.buttons|=4;
515 Mouse_AddEvent(MOUSE_MIDDLE_PRESSED);
516 break;
517 #endif
518 default:
519 return;
520 }
521 mouse.times_pressed[button]++;
522 mouse.last_pressed_x[button]=POS_X;
523 mouse.last_pressed_y[button]=POS_Y;
524 }
525
526 void Mouse_ButtonReleased(Bit8u button) {
527 switch (button) {
528 #if (MOUSE_BUTTONS >= 1)
529 case 0:
530 mouse.buttons&=~1;
531 Mouse_AddEvent(MOUSE_LEFT_RELEASED);
532 break;
533 #endif
534 #if (MOUSE_BUTTONS >= 2)
535 case 1:
536 mouse.buttons&=~2;
537 Mouse_AddEvent(MOUSE_RIGHT_RELEASED);
538 break;
539 #endif
540 #if (MOUSE_BUTTONS >= 3)
541 case 2:
542 mouse.buttons&=~4;
543 Mouse_AddEvent(MOUSE_MIDDLE_RELEASED);
544 break;
545 #endif
546 default:
547 return;
548 }
549 mouse.times_released[button]++;
550 mouse.last_released_x[button]=POS_X;
551 mouse.last_released_y[button]=POS_Y;
552 }
553
554 static void Mouse_SetMickeyPixelRate(Bit16s px, Bit16s py){
555 if ((px!=0) && (py!=0)) {
556 mouse.mickeysPerPixel_x = (float)px/X_MICKEY;
557 mouse.mickeysPerPixel_y = (float)py/Y_MICKEY;
558 mouse.pixelPerMickey_x = X_MICKEY/(float)px;
559 mouse.pixelPerMickey_y = Y_MICKEY/(float)py;
560 }
561 }
562
563 static void Mouse_SetSensitivity(Bit16u px, Bit16u py, Bit16u dspeed){
564 if(px>100) px=100;
565 if(py>100) py=100;
566 if(dspeed>100) dspeed=100;
567 // save values
568 mouse.senv_x_val=px;
569 mouse.senv_y_val=py;
570 mouse.dspeed_val=dspeed;
571 if ((px!=0) && (py!=0)) {
572 px--; //Inspired by cutemouse
573 py--; //Although their cursor update routine is far more complex then ours
574 mouse.senv_x=(static_cast<float>(px)*px)/3600.0f +1.0f/3.0f;
575 mouse.senv_y=(static_cast<float>(py)*py)/3600.0f +1.0f/3.0f;
576 }
577 }
578
579
580 static void Mouse_ResetHardware(void){
581 PIC_SetIRQMask(MOUSE_IRQ,false);
582 }
583
584 //Does way to much. Many things should be moved to mouse reset one day
585 void Mouse_NewVideoMode(void) {
586 mouse.inhibit_draw=false;
587 /* Get the correct resolution from the current video mode */
588 Bit8u mode=mem_readb(BIOS_VIDEO_MODE);
589 if(mode == mouse.mode) {LOG(LOG_MOUSE,LOG_NORMAL)("New video is the same as the old"); /*return;*/}
590 switch (mode) {
591 case 0x00:
592 case 0x01:
593 case 0x02:
594 case 0x03: {
595 Bitu rows=real_readb(BIOSMEM_SEG,BIOSMEM_NB_ROWS);
596 if ((rows==0) || (rows>250)) rows=25-1;
597 mouse.max_y=8*(rows+1)-1;
598 break;
599 }
600 case 0x04:
601 case 0x05:
602 case 0x06:
603 case 0x07:
604 case 0x08:
605 case 0x09:
606 case 0x0a:
607 case 0x0d:
608 case 0x0e:
609 case 0x13:
610 mouse.max_y=199;
611 break;
612 case 0x0f:
613 case 0x10:
614 mouse.max_y=349;
615 break;
616 case 0x11:
617 case 0x12:
618 mouse.max_y=479;
619 break;
620 default:
621 LOG(LOG_MOUSE,LOG_ERROR)("Unhandled videomode %X on reset",mode);
622 mouse.inhibit_draw=true;
623 return;
624 }
625 mouse.mode = mode;
626 mouse.hidden = 1;
627 mouse.max_x = 639;
628 mouse.min_x = 0;
629 mouse.min_y = 0;
630 mouse.granMask = (mode == 0x0d || mode == 0x13) ? 0xfffe : 0xffff;
631
632 mouse.events = 0;
633 mouse.timer_in_progress = false;
634 PIC_RemoveEvents(MOUSE_Limit_Events);
635
636 mouse.hotx = 0;
637 mouse.hoty = 0;
638 mouse.background = false;
639 mouse.screenMask = defaultScreenMask;
640 mouse.cursorMask = defaultCursorMask;
641 mouse.textAndMask= defaultTextAndMask;
642 mouse.textXorMask= defaultTextXorMask;
643 mouse.language = 0;
644 mouse.page = 0;
645 mouse.doubleSpeedThreshold = 64;
646 mouse.updateRegion_x[0] = 1;
647 mouse.updateRegion_y[0] = 1;
648 mouse.updateRegion_x[1] = 1;
649 mouse.updateRegion_y[1] = 1;
650 mouse.cursorType = 0;
651 mouse.enabled=true;
652 mouse.oldhidden=1;
653
654 oldmouseX = static_cast<Bit16s>(mouse.x);
655 oldmouseY = static_cast<Bit16s>(mouse.y);
656
657
658 }
659
660 //Much too empty, Mouse_NewVideoMode contains stuff that should be in here
661 static void Mouse_Reset(void) {
662 /* Remove drawn mouse Legends of Valor */
663 if (CurMode->type!=M_TEXT) RestoreCursorBackground();
664 else RestoreCursorBackgroundText();
665 mouse.hidden = 1;
666
667 Mouse_NewVideoMode();
668 Mouse_SetMickeyPixelRate(8,16);
669
670 mouse.mickey_x = 0;
671 mouse.mickey_y = 0;
672
673 // Dont set max coordinates here. it is done by SetResolution!
674 mouse.x = static_cast<float>((mouse.max_x + 1)/ 2);
675 mouse.y = static_cast<float>((mouse.max_y + 1)/ 2);
676 mouse.sub_mask = 0;
677 mouse.in_UIR = false;
678 }
679
680 static Bitu INT33_Handler(void) {
681 // LOG(LOG_MOUSE,LOG_NORMAL)("MOUSE: %04X %X %X %d %d",reg_ax,reg_bx,reg_cx,POS_X,POS_Y);
682 switch (reg_ax) {
683 case 0x00: /* Reset Driver and Read Status */
684 Mouse_ResetHardware(); /* fallthrough */
685 case 0x21: /* Software Reset */
686 reg_ax=0xffff;
687 reg_bx=MOUSE_BUTTONS;
688 Mouse_Reset();
689 Mouse_AutoLock(true);
690 break;
691 case 0x01: /* Show Mouse */
692 if(mouse.hidden) mouse.hidden--;
693 Mouse_AutoLock(true);
694 DrawCursor();
695 break;
696 case 0x02: /* Hide Mouse */
697 {
698 if (CurMode->type!=M_TEXT) RestoreCursorBackground();
699 else RestoreCursorBackgroundText();
700 mouse.hidden++;
701 }
702 break;
703 case 0x03: /* Return position and Button Status */
704 reg_bx=mouse.buttons;
705 reg_cx=POS_X;
706 reg_dx=POS_Y;
707 break;
708 case 0x04: /* Position Mouse */
709 /* If position isn't different from current position
710 * don't change it then. (as position is rounded so numbers get
711 * lost when the rounded number is set) (arena/simulation Wolf) */
712 if ((Bit16s)reg_cx >= mouse.max_x) mouse.x = static_cast<float>(mouse.max_x);
713 else if (mouse.min_x >= (Bit16s)reg_cx) mouse.x = static_cast<float>(mouse.min_x);
714 else if ((Bit16s)reg_cx != POS_X) mouse.x = static_cast<float>(reg_cx);
715
716 if ((Bit16s)reg_dx >= mouse.max_y) mouse.y = static_cast<float>(mouse.max_y);
717 else if (mouse.min_y >= (Bit16s)reg_dx) mouse.y = static_cast<float>(mouse.min_y);
718 else if ((Bit16s)reg_dx != POS_Y) mouse.y = static_cast<float>(reg_dx);
719 DrawCursor();
720 break;
721 case 0x05: /* Return Button Press Data */
722 {
723 Bit16u but=reg_bx;
724 reg_ax=mouse.buttons;
725 if (but>=MOUSE_BUTTONS) but = MOUSE_BUTTONS - 1;
726 reg_cx=mouse.last_pressed_x[but];
727 reg_dx=mouse.last_pressed_y[but];
728 reg_bx=mouse.times_pressed[but];
729 mouse.times_pressed[but]=0;
730 break;
731 }
732 case 0x06: /* Return Button Release Data */
733 {
734 Bit16u but=reg_bx;
735 reg_ax=mouse.buttons;
736 if (but>=MOUSE_BUTTONS) but = MOUSE_BUTTONS - 1;
737 reg_cx=mouse.last_released_x[but];
738 reg_dx=mouse.last_released_y[but];
739 reg_bx=mouse.times_released[but];
740 mouse.times_released[but]=0;
741 break;
742 }
743 case 0x07: /* Define horizontal cursor range */
744 { //lemmings set 1-640 and wants that. iron seeds set 0-640 but doesn't like 640
745 //Iron seed works if newvideo mode with mode 13 sets 0-639
746 //Larry 6 actually wants newvideo mode with mode 13 to set it to 0-319
747 Bit16s max,min;
748 if ((Bit16s)reg_cx<(Bit16s)reg_dx) { min=(Bit16s)reg_cx;max=(Bit16s)reg_dx;}
749 else { min=(Bit16s)reg_dx;max=(Bit16s)reg_cx;}
750 mouse.min_x=min;
751 mouse.max_x=max;
752 /* Battlechess wants this */
753 if(mouse.x > mouse.max_x) mouse.x = mouse.max_x;
754 if(mouse.x < mouse.min_x) mouse.x = mouse.min_x;
755 /* Or alternatively this:
756 mouse.x = (mouse.max_x - mouse.min_x + 1)/2;*/
757 LOG(LOG_MOUSE,LOG_NORMAL)("Define Hortizontal range min:%d max:%d",min,max);
758 }
759 break;
760 case 0x08: /* Define vertical cursor range */
761 { // not sure what to take instead of the CurMode (see case 0x07 as well)
762 // especially the cases where sheight= 400 and we set it with the mouse_reset to 200
763 //disabled it at the moment. Seems to break syndicate who want 400 in mode 13
764 Bit16s max,min;
765 if ((Bit16s)reg_cx<(Bit16s)reg_dx) { min=(Bit16s)reg_cx;max=(Bit16s)reg_dx;}
766 else { min=(Bit16s)reg_dx;max=(Bit16s)reg_cx;}
767 mouse.min_y=min;
768 mouse.max_y=max;
769 /* Battlechess wants this */
770 if(mouse.y > mouse.max_y) mouse.y = mouse.max_y;
771 if(mouse.y < mouse.min_y) mouse.y = mouse.min_y;
772 /* Or alternatively this:
773 mouse.y = (mouse.max_y - mouse.min_y + 1)/2;*/
774 LOG(LOG_MOUSE,LOG_NORMAL)("Define Vertical range min:%d max:%d",min,max);
775 }
776 break;
777 case 0x09: /* Define GFX Cursor */
778 {
779 PhysPt src = SegPhys(es)+reg_dx;
780 MEM_BlockRead(src ,userdefScreenMask,CURSORY*2);
781 MEM_BlockRead(src+CURSORY*2,userdefCursorMask,CURSORY*2);
782 mouse.screenMask = userdefScreenMask;
783 mouse.cursorMask = userdefCursorMask;
784 mouse.hotx = reg_bx;
785 mouse.hoty = reg_cx;
786 mouse.cursorType = 2;
787 DrawCursor();
788 }
789 break;
790 case 0x0a: /* Define Text Cursor */
791 mouse.cursorType = reg_bx;
792 mouse.textAndMask = reg_cx;
793 mouse.textXorMask = reg_dx;
794 break;
795 case 0x0b: /* Read Motion Data */
796 reg_cx=(Bit16s)(mouse.mickey_x*mouse.mickeysPerPixel_x);
797 reg_dx=(Bit16s)(mouse.mickey_y*mouse.mickeysPerPixel_y);
798 mouse.mickey_x=0;
799 mouse.mickey_y=0;
800 break;
801 case 0x0c: /* Define interrupt subroutine parameters */
802 mouse.sub_mask=reg_cx;
803 mouse.sub_seg=SegValue(es);
804 mouse.sub_ofs=reg_dx;
805 Mouse_AutoLock(true); //Some games don't seem to reset the mouse before using
806 break;
807 case 0x0f: /* Define mickey/pixel rate */
808 Mouse_SetMickeyPixelRate(reg_cx,reg_dx);
809 break;
810 case 0x10: /* Define screen region for updating */
811 mouse.updateRegion_x[0]=reg_cx;
812 mouse.updateRegion_y[0]=reg_dx;
813 mouse.updateRegion_x[1]=reg_si;
814 mouse.updateRegion_y[1]=reg_di;
815 break;
816 case 0x11: /* Get number of buttons */
817 reg_ax=0xffff;
818 reg_bx=MOUSE_BUTTONS;
819 break;
820 case 0x13: /* Set double-speed threshold */
821 mouse.doubleSpeedThreshold=(reg_bx ? reg_bx : 64);
822 break;
823 case 0x14: /* Exchange event-handler */
824 {
825 Bit16u oldSeg = mouse.sub_seg;
826 Bit16u oldOfs = mouse.sub_ofs;
827 Bit16u oldMask= mouse.sub_mask;
828 // Set new values
829 mouse.sub_mask= reg_cx;
830 mouse.sub_seg = SegValue(es);
831 mouse.sub_ofs = reg_dx;
832 // Return old values
833 reg_cx = oldMask;
834 reg_dx = oldOfs;
835 SegSet16(es,oldSeg);
836 }
837 break;
838 case 0x15: /* Get Driver storage space requirements */
839 reg_bx = sizeof(mouse);
840 break;
841 case 0x16: /* Save driver state */
842 {
843 LOG(LOG_MOUSE,LOG_WARN)("Saving driver state...");
844 PhysPt dest = SegPhys(es)+reg_dx;
845 MEM_BlockWrite(dest, &mouse, sizeof(mouse));
846 }
847 break;
848 case 0x17: /* load driver state */
849 {
850 LOG(LOG_MOUSE,LOG_WARN)("Loading driver state...");
851 PhysPt src = SegPhys(es)+reg_dx;
852 MEM_BlockRead(src, &mouse, sizeof(mouse));
853 }
854 break;
855 case 0x1a: /* Set mouse sensitivity */
856 // ToDo : double mouse speed value
857 Mouse_SetSensitivity(reg_bx,reg_cx,reg_dx);
858
859 LOG(LOG_MOUSE,LOG_WARN)("Set sensitivity used with %d %d (%d)",reg_bx,reg_cx,reg_dx);
860 break;
861 case 0x1b: /* Get mouse sensitivity */
862 reg_bx = mouse.senv_x_val;
863 reg_cx = mouse.senv_y_val;
864 reg_dx = mouse.dspeed_val;
865
866 LOG(LOG_MOUSE,LOG_WARN)("Get sensitivity %d %d",reg_bx,reg_cx);
867 break;
868 case 0x1c: /* Set interrupt rate */
869 /* Can't really set a rate this is host determined */
870 break;
871 case 0x1d: /* Set display page number */
872 mouse.page=reg_bl;
873 break;
874 case 0x1e: /* Get display page number */
875 reg_bx=mouse.page;
876 break;
877 case 0x1f: /* Disable Mousedriver */
878 /* ES:BX old mouse driver Zero at the moment TODO */
879 reg_bx=0;
880 SegSet16(es,0);
881 mouse.enabled=false; /* Just for reporting not doing a thing with it */
882 mouse.oldhidden=mouse.hidden;
883 mouse.hidden=1;
884 break;
885 case 0x20: /* Enable Mousedriver */
886 mouse.enabled=true;
887 mouse.hidden=mouse.oldhidden;
888 break;
889 case 0x22: /* Set language for messages */
890 /*
891 * Values for mouse driver language:
892 *
893 * 00h English
894 * 01h French
895 * 02h Dutch
896 * 03h German
897 * 04h Swedish
898 * 05h Finnish
899 * 06h Spanish
900 * 07h Portugese
901 * 08h Italian
902 *
903 */
904 mouse.language=reg_bx;
905 break;
906 case 0x23: /* Get language for messages */
907 reg_bx=mouse.language;
908 break;
909 case 0x24: /* Get Software version and mouse type */
910 reg_bx=0x805; //Version 8.05 woohoo
911 reg_ch=0x04; /* PS/2 type */
912 reg_cl=0; /* PS/2 (unused) */
913 break;
914 case 0x26: /* Get Maximum virtual coordinates */
915 reg_bx=(mouse.enabled ? 0x0000 : 0xffff);
916 reg_cx=(Bit16u)mouse.max_x;
917 reg_dx=(Bit16u)mouse.max_y;
918 break;
919 case 0x31: /* Get Current Minimum/Maximum virtual coordinates */
920 reg_ax=(Bit16u)mouse.min_x;
921 reg_bx=(Bit16u)mouse.min_y;
922 reg_cx=(Bit16u)mouse.max_x;
923 reg_dx=(Bit16u)mouse.max_y;
924 break;
925 default:
926 LOG(LOG_MOUSE,LOG_ERROR)("Mouse Function %04X not implemented!",reg_ax);
927 break;
928 }
929 return CBRET_NONE;
930 }
931
932 static Bitu MOUSE_BD_Handler(void) {
933 // the stack contains offsets to register values
934 Bit16u raxpt=real_readw(SegValue(ss),reg_sp+0x0a);
935 Bit16u rbxpt=real_readw(SegValue(ss),reg_sp+0x08);
936 Bit16u rcxpt=real_readw(SegValue(ss),reg_sp+0x06);
937 Bit16u rdxpt=real_readw(SegValue(ss),reg_sp+0x04);
938
939 // read out the actual values, registers ARE overwritten
940 Bit16u rax=real_readw(SegValue(ds),raxpt);
941 reg_ax=rax;
942 reg_bx=real_readw(SegValue(ds),rbxpt);
943 reg_cx=real_readw(SegValue(ds),rcxpt);
944 reg_dx=real_readw(SegValue(ds),rdxpt);
945 // LOG_MSG("MOUSE BD: %04X %X %X %X %d %d",reg_ax,reg_bx,reg_cx,reg_dx,POS_X,POS_Y);
946
947 // some functions are treated in a special way (additional registers)
948 switch (rax) {
949 case 0x09: /* Define GFX Cursor */
950 case 0x16: /* Save driver state */
951 case 0x17: /* load driver state */
952 SegSet16(es,SegValue(ds));
953 break;
954 case 0x0c: /* Define interrupt subroutine parameters */
955 case 0x14: /* Exchange event-handler */
956 if (reg_bx!=0) SegSet16(es,reg_bx);
957 else SegSet16(es,SegValue(ds));
958 break;
959 case 0x10: /* Define screen region for updating */
960 reg_cx=real_readw(SegValue(ds),rdxpt);
961 reg_dx=real_readw(SegValue(ds),rdxpt+2);
962 reg_si=real_readw(SegValue(ds),rdxpt+4);
963 reg_di=real_readw(SegValue(ds),rdxpt+6);
964 break;
965 default:
966 break;
967 }
968
969 INT33_Handler();
970
971 // save back the registers, too
972 real_writew(SegValue(ds),raxpt,reg_ax);
973 real_writew(SegValue(ds),rbxpt,reg_bx);
974 real_writew(SegValue(ds),rcxpt,reg_cx);
975 real_writew(SegValue(ds),rdxpt,reg_dx);
976 switch (rax) {
977 case 0x1f: /* Disable Mousedriver */
978 real_writew(SegValue(ds),rbxpt,SegValue(es));
979 break;
980 case 0x14: /* Exchange event-handler */
981 real_writew(SegValue(ds),rcxpt,SegValue(es));
982 break;
983 default:
984 break;
985 }
986
987 reg_ax=rax;
988 return CBRET_NONE;
989 }
990
991 static Bitu INT74_Handler(void) {
992 if (mouse.events>0) {
993 mouse.events--;
994 /* Check for an active Interrupt Handler that will get called */
995 if (mouse.sub_mask & mouse.event_queue[mouse.events].type) {
996 reg_ax=mouse.event_queue[mouse.events].type;
997 reg_bx=mouse.event_queue[mouse.events].buttons;
998 reg_cx=POS_X;
999 reg_dx=POS_Y;
1000 reg_si=(Bit16s)(mouse.mickey_x*mouse.mickeysPerPixel_x);
1001 reg_di=(Bit16s)(mouse.mickey_y*mouse.mickeysPerPixel_y);
1002 CPU_Push16(RealSeg(CALLBACK_RealPointer(int74_ret_callback)));
1003 CPU_Push16(RealOff(CALLBACK_RealPointer(int74_ret_callback)));
1004 SegSet16(cs, mouse.sub_seg);
1005 reg_ip = mouse.sub_ofs;
1006 if(mouse.in_UIR) LOG(LOG_MOUSE,LOG_ERROR)("Already in UIR!");
1007 mouse.in_UIR = true;
1008 } else if (useps2callback) {
1009 CPU_Push16(RealSeg(CALLBACK_RealPointer(int74_ret_callback)));
1010 CPU_Push16(RealOff(CALLBACK_RealPointer(int74_ret_callback)));
1011 DoPS2Callback(mouse.event_queue[mouse.events].buttons, POS_X, POS_Y);
1012 } else {
1013 SegSet16(cs, RealSeg(CALLBACK_RealPointer(int74_ret_callback)));
1014 reg_ip = RealOff(CALLBACK_RealPointer(int74_ret_callback));
1015 }
1016 } else {
1017 SegSet16(cs, RealSeg(CALLBACK_RealPointer(int74_ret_callback)));
1018 reg_ip = RealOff(CALLBACK_RealPointer(int74_ret_callback));
1019 }
1020 return CBRET_NONE;
1021 }
1022
1023 Bitu MOUSE_UserInt_CB_Handler(void) {
1024 mouse.in_UIR = false;
1025 if (mouse.events) {
1026 if (!mouse.timer_in_progress) {
1027 mouse.timer_in_progress = true;
1028 PIC_AddEvent(MOUSE_Limit_Events,MOUSE_DELAY);
1029 }
1030 }
1031 return CBRET_NONE;
1032 }
1033
1034 void MOUSE_Init(Section* /*sec*/) {
1035 // Callback for mouse interrupt 0x33
1036 call_int33=CALLBACK_Allocate();
1037 // RealPt i33loc=RealMake(CB_SEG+1,(call_int33*CB_SIZE)-0x10);
1038 RealPt i33loc=RealMake(DOS_GetMemory(0x1)-1,0x10);
1039 CALLBACK_Setup(call_int33,&INT33_Handler,CB_MOUSE,Real2Phys(i33loc),"Mouse");
1040 // Wasteland needs low(seg(int33))!=0 and low(ofs(int33))!=0
1041 real_writed(0,0x33<<2,i33loc);
1042
1043 call_mouse_bd=CALLBACK_Allocate();
1044 CALLBACK_Setup(call_mouse_bd,&MOUSE_BD_Handler,CB_RETF8,
1045 PhysMake(RealSeg(i33loc),RealOff(i33loc)+2),"MouseBD");
1046 // pseudocode for CB_MOUSE (including the special backdoor entry point):
1047 // jump near i33hd
1048 // callback MOUSE_BD_Handler
1049 // retf 8
1050 // label i33hd:
1051 // callback INT33_Handler
1052 // iret
1053
1054
1055 // Callback for ps2 irq
1056 call_int74=CALLBACK_Allocate();
1057 CALLBACK_Setup(call_int74,&INT74_Handler,CB_IRQ12,"int 74");
1058 // pseudocode for CB_IRQ12:
1059 // push ds
1060 // push es
1061 // pushad
1062 // sti
1063 // callback INT74_Handler
1064 // doesn't return here, but rather to CB_IRQ12_RET
1065 // (ps2 callback/user callback inbetween if requested)
1066
1067 int74_ret_callback=CALLBACK_Allocate();
1068 CALLBACK_Setup(int74_ret_callback,&MOUSE_UserInt_CB_Handler,CB_IRQ12_RET,"int 74 ret");
1069 // pseudocode for CB_IRQ12_RET:
1070 // callback MOUSE_UserInt_CB_Handler
1071 // cli
1072 // mov al, 0x20
1073 // out 0xa0, al
1074 // out 0x20, al
1075 // popad
1076 // pop es
1077 // pop ds
1078 // iret
1079
1080 Bit8u hwvec=(MOUSE_IRQ>7)?(0x70+MOUSE_IRQ-8):(0x8+MOUSE_IRQ);
1081 RealSetVec(hwvec,CALLBACK_RealPointer(call_int74));
1082
1083 // Callback for ps2 user callback handling
1084 useps2callback = false; ps2callbackinit = false;
1085 call_ps2=CALLBACK_Allocate();
1086 CALLBACK_Setup(call_ps2,&PS2_Handler,CB_RETF,"ps2 bios callback");
1087 ps2_callback=CALLBACK_RealPointer(call_ps2);
1088
1089 memset(&mouse,0,sizeof(mouse));
1090 mouse.hidden = 1; //Hide mouse on startup
1091 mouse.timer_in_progress = false;
1092 mouse.mode = 0xFF; //Non existing mode
1093
1094 mouse.sub_mask=0;
1095 mouse.sub_seg=0x6362; // magic value
1096 mouse.sub_ofs=0;
1097
1098 Mouse_ResetHardware();
1099 Mouse_Reset();
1100 Mouse_SetSensitivity(50,50,50);
1101 }