File: mou.c

package info (click to toggle)
mgt 2.31-7
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 460 kB
  • ctags: 582
  • sloc: ansic: 4,852; sh: 411; pascal: 329; makefile: 88; csh: 3
file content (822 lines) | stat: -rw-r--r-- 24,639 bytes parent folder | download | duplicates (6)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/*****************************************************************************
 * PROJECT:  Mouse routines with 'real' graphic cursor in text mode.
 *****************************************************************************
 * MODULE:  MOU.C
 *****************************************************************************
 * DESCRIPTION:
 *   Main file for the mouse routines.
 *
 *****************************************************************************
 * MODIFICATION NOTES:
 *    Date     Version Author Comment
 * 15-Mar-1991	 1.04	 dk   Added function to set mouse position.  From a
 *			      suggestion by Gary Tepel.
 * 17-Jan-1991	 1.02	 ta   Diffs sent to me from Tony Acero for middle
 *			      mouse button support.
 * 10-Jan-1991	 1.01	 dk   Made points variable global throughout module.
 * 07-Jan-1991	 1.00	 dk   Fixed bugs and set up for release to Usenet.
 * 26-Oct-1990	 0.00	 dk   Initial file.
 *****************************************************************************
 *
 * DISCLAIMER:
 *
 * Programmers may incorporate any or all code into their programs,
 * giving proper credit within the source. Publication of the
 * source routines is permitted so long as proper credit is given
 * to Dave Kirsch.
 *
 * Copyright (C) 1990, 1991 by Dave Kirsch.  You may use this program, or
 * code or tables extracted from it, as desired without restriction.
 * I can not and will not be held responsible for any damage caused from
 * the use of this software.
 *
 * Author: a563@mindlink.UUCP  -or-  Zoid@cc.sfu.ca
 *
 *****************************************************************************
 * This source works with Turbo C 2.0 and MSC 6.0 and above.
 *****************************************************************************/

#ifdef __TURBOC__
#pragma inline
#endif

#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <string.h>

#include "mou.h"

#define HEIGHT 16

int vgapresent=0;
word mousehidden = 0;           /* Is the mouse on? Additive flag */
mouseboolean mouseinstalled = FALSE; /* Is the mouse installed? */

volatile word mousex=0, mousey=0; /* Character position of mouse */

#ifndef __TURBOC__
STATIC word _based(_segname("_CODE")) DGroupSeg;
#endif

STATIC volatile word mbufin = 0, mbufout = 0; /* Mouse buffer pointers */

STATIC word mousefreeze = 0;           /* Is mouse frozen in place? */

STATIC MOUINFOREC mbuf[MOUSEBUFFERSIZE]; /* Mouse buffer */

/* Save information for non EGA/VGA */
STATIC word oldword;
STATIC word newword;
STATIC mouseboolean saved = FALSE;
STATIC word oldmx, oldmy;

/* Save information for EGA/VGA displays */
  mouseboolean egavga = FALSE; /* Do we have an EGA/VGA adapter? */
STATIC byte savechars[3][3]; /* The saved characters we overwrote */
STATIC dword mousecursormask[HEIGHT] =  {
  0x00000000L,  /*0000000000000000*/
  0x40000000L,  /*0100000000000000*/
  0x60000000L,  /*0110000000000000*/
  0x70000000L,  /*0111000000000000*/
  0x78000000L,  /*0111100000000000*/
  0x7c000000L,  /*0111110000000000*/
  0x7e000000L,  /*0111111000000000*/
  0x7f000000L,  /*0111111100000000*/
  0x7f800000L,  /*0111111110000000*/
  0x7f000000L,  /*0111111100000000*/
  0x7c000000L,  /*0111110000000000*/
  0x46000000L,  /*0100011000000000*/
  0x06000000L,  /*0000011000000000*/
  0x03000000L,  /*0000001100000000*/
  0x03000000L,  /*0000001100000000*/
  0x00000000L   /*0000000000000000*/
};

STATIC dword mousescreenmask[HEIGHT] =  {
  0x3fffffffL,  /*0011111111111111*/
  0x1fffffffL,  /*0001111111111111*/
  0x0fffffffL,  /*0000111111111111*/
  0x07ffffffL,  /*0000011111111111*/
  0x03ffffffL,  /*0000001111111111*/
  0x01ffffffL,  /*0000000111111111*/
  0x00ffffffL,  /*0000000011111111*/
  0x007fffffL,  /*0000000001111111*/
  0x003fffffL,  /*0000000000111111*/
  0x007fffffL,  /*0000000001111111*/
  0x01ffffffL,  /*0000000111111111*/
  0x10ffffffL,  /*0001000011111111*/
  0xb0ffffffL,  /*1011000011111111*/
  0xf87fffffL,  /*1111100001111111*/
  0xf87fffffL,  /*1111100001111111*/
  0xfcffffffL   /*1111110011111111*/
};

STATIC byte chardefs[32 * 9]; /* 9 character definitons. */
STATIC word mousepx, mousepy; /* Mouse pixel coordinates */

STATIC mouseboolean conditionalhidemouse = FALSE;
STATIC word conx1, cony1, conx2, cony2;

STATIC word vseg; /* Segment of video ram. */
STATIC word mcols, mrows;
STATIC byte savevmode;
STATIC word points;

STATIC word maxx, maxy;

STATIC mouseboolean desqview = FALSE;

#define POKEATTRIB(x, y, a) pokeb(vseg, (y) * (mcols * 2) + ((x) << 1) + 1, a)
#define PEEKATTRIB(x, y)    peekb(vseg, (y) * (mcols * 2) + ((x) << 1) + 1)

#define POINTS *((byte far *) 0x00000485)
#define COLS *((byte far *) 0x0040004AL)
#define ROWS *((byte far *) 0x00400084L)

#define DEFCHAR 0xd0

/*********************************************************************/
/* Mon 07-Jan-1991 - dk                                              */
/*                                                                   */
/*  Plot the cursor on the screen, save background, draw grid, etc.  */
/*                                                                   */
/*********************************************************************/
PRIVATE void LOCAL FAST plotegavgacursor(word func)
{
word off;
word width, height, i, j;
word disp;
word x = 0, y = 0;
static word lsavex = 0, lsavey = 0;

  switch (func) {
    case 0 : /* erase grid, put back save info */
      x = lsavex;
      y = lsavey;
      break;
    case 1 : /* draw grid */
      x = mousex;
      y = mousey;
      break;
    case 2 : /* save grid */
      lsavex = x = mousex;
      lsavey = y = mousey;
      break;
  }

  width = mcols - x;
  if (width > 3)
    width = 3;
  height = mrows - y;
  if (height > 3)
    height = 3;

  off = y * (mcols * 2) + x * 2;
  disp = (mcols * 2) - width * 2;

  switch (func) {
    case 0 : /* erase grid, put back save info */
      for (i = 0; i < height; i++, off += disp)
        for (j = 0; j < width; j++, off += 2)
          pokeb(vseg, off, savechars[i][j]);
      break;
    case 1 : /* draw grid. */
      for (i = 0; i < height; i++, off += disp)
        for (j = 0; j < width; j++, off += 2)
          pokeb(vseg, off, DEFCHAR + i * 3 + j);
      break;
    case 2 : /* save grid. */
      for (i = 0; i < height; i++, off += disp)
        for (j = 0; j < width; j++, off += 2)
          savechars[i][j] = peekb(vseg, off);
      break;
  }
}

PRIVATE void LOCAL FAST drawegavgacursor(void)
{
word off;
word i, j;
word s1, s2, s3;
dword *defs;
dword *masks;
word shift;
dword addmask;

  plotegavgacursor(2); /* Save current grid that is there. */

  /* Time for some assembler.  Program the EGA/VGA Sequencer and Graphics
     Controller for direct access to the character definition tables.
     Then read in the definitions for the characters we are changing, AND
     the screen mask, then OR the cursor mask to them.  Then copy those
     defintions into the location of the mouse cursor defintions
     and set the Sequencer and Graphics Controller back to normal <whew!>.
  */

  /* Program the Sequencer */

  asm pushf; /* Disable interrupts */
  asm cli;
  asm mov dx, 3c4h; /* Sequencer port address */
  asm mov ax, 0704h; /* Sequential addressing */
  asm out dx, ax;

  /* Program the Graphics Controller */
  asm mov dx, 3ceh; /* Graphics Controller port address */
  asm mov ax, 0204h; /* Select map 2 for CPU reads */
  asm out dx, ax;
  asm mov ax, 0005h; /* Disable odd-even addressing */
  asm out dx, ax;
  asm mov ax, 0406h; /* Map starts at A000:0000 (64K mode) */
  asm out dx, ax;
  asm popf;

  /* Ok, now we have direct access to the character defintion tables, copy
     over the defintions for the characters we are changing */

  off = 0;
  for (i = 0; i < 9; i += 3) { /* Grid is three characters high. */
    s1 = ((byte *)savechars)[i    ] * 32;
    s2 = ((byte *)savechars)[i + 1] * 32;
    s3 = ((byte *)savechars)[i + 2] * 32;
    for (j = 0; j < points; j++) {
      off++; /* 4th byte, that we don't need. */
      chardefs[off++] = peekb(0xa000, s3++);
      chardefs[off++] = peekb(0xa000, s2++);
      chardefs[off++] = peekb(0xa000, s1++);
    }
  }

  /* Ok, we've got the defintions for the characters that we are drawing the
     cursor on.  AND the screen mask and OR the cursor mask to them, thereby
     'drawing' the cursor.  Since the cursor is 16 pixels wide and 16 pixels
     high, we have to save a 3 by 3 character grid where the mouse cursor is
     going.  We use dwords (32 bits) to do the bit AND and OR.  This could
     be made alot faster on a 386 by using 32 bit registers. */

  shift = mousepx % 8;
  addmask = 0xff000000L << (8 - shift);

  masks = mousescreenmask;
  defs = ((dword *)chardefs) + mousepy % points;
  for (i = 0; i < HEIGHT; i++)
    *defs++ &= (*masks++ >> shift) | addmask;

  masks = mousecursormask;
  defs = ((dword *)chardefs) + mousepy % points;
  for (i = 0; i < HEIGHT; i++)
    *defs++ |= *masks++ >> shift;

  /* Ok, Everything is setup, now copy the modifed character definitions
     to their new location. */

  asm mov dx, 3c4h; /* Sequencer port address */
  asm mov ax, 0402h; /* CPU writes only to map 2 */
  asm out dx, ax;

  off = 0;
  for (i = 0; i < 9; i += 3) { /* Grid is three characters high. */
    s1 = (DEFCHAR + i    ) * 32;
    s2 = (DEFCHAR + i + 1) * 32;
    s3 = (DEFCHAR + i + 2) * 32;
    for (j = 0; j < points; j++) {
      off++; /* 4th byte, that we don't need. */
      pokeb(0xa000, s3++, chardefs[off++]);
      pokeb(0xa000, s2++, chardefs[off++]);
      pokeb(0xa000, s1++, chardefs[off++]);
    }
  }

  /* Ok, put the Sequencer and Graphics Controller back to normal */

  /* Program the Sequencer */
  asm pushf; /* Disable interrupts */
  asm cli;
  asm mov dx, 3c4h; /* Sequencer port address */
  asm mov ax, 0302h; /* CPU writes to maps 0 and 1 */
  asm out dx, ax;
  asm mov ax, 0304h; /* Odd-even addressing */
  asm out dx, ax;

  /* Program the Graphics Controller */
  asm mov dx, 3ceh; /* Graphics Controller port address */
  asm mov ax, 0004h; /* Select map 0 for CPU reads */
  asm out dx, ax;
  asm mov ax, 1005h; /* Enable odd-even addressing */
  asm out dx, ax;
  asm sub ax, ax;
  asm mov es, ax; /* Segment 0 */
  asm mov ax, 0e06h; /* Map starts at B800:0000 */
  asm mov bl, 7;
  asm cmp es:[49h], bl; /* Get current video mode */
  asm jne notmono;
  asm mov ax, 0806h; /* Map starts at B000:0000 */
notmono:
  asm out dx, ax;
  asm popf;

  /* Ok, now put the bytes on the screen */

  plotegavgacursor(1); /* Plot the new grid on the screen. */
}

/*******************************************************/
/* 27-Oct-1990 - dk                                    */
/*                                                     */
/*  This function checks for the presense of EGA/VGA.  */
/*                                                     */
/*******************************************************/
 mouseboolean isegavga(void)
{
  asm mov ax, 1a00h; /* ROM BIOS video function 1ah -- Read Display Code */
  asm int 10h;
  asm cmp ah, 1ah; /* Is this call supported? */
  asm je checkega; /* Not supported */
  asm cmp bl, 7; /* VGA w/monochrome display? */
  asm je isvga; /* Yup. */
  asm cmp bl, 8; /* VGA w/color display? */
  asm jne checkega; /* Nope */
isvga:
  vgapresent=1;
isega:
  return TRUE; /* EGA/VGA is installed */
checkega:
  asm mov ah, 12h; /* EGA BIOS function */
  asm mov bl, 10h;
  asm int 10h;
  asm cmp bl, 10h; /* Is EGA BIOS present? */
  asm jne isega; /* There is an EGA in the system. */
  return FALSE; /* Not EGA or VGA in system. */
}

/************************************************/
/* 26-Oct-1990 - dk                             */
/*                                              */
/*  Mouse handler -- called from mouse driver.  */
/*                                              */
/************************************************/
PRIVATE void far mousehandler(void)
/* This function is called whenever a button is pressed.  Do not call this
   function directly!!
*/
{
register word conditionmask;

  /* Get our data segment */
  asm push ds
#ifdef __TURBOC__
  asm push ax
  asm mov ax, DGROUP
  asm mov ds, ax
  asm pop ax
#else
  asm mov ds, cs:DGroupSeg
#endif

  asm mov conditionmask,ax

  if (!mousefreeze) {
    /* save mouse info passed to us from driver */
    asm mov mousex, cx
    asm mov mousey, dx
    asm mov mousepx, cx
    asm mov mousepy, dx

    mousex /= 8; /* Characters are 8 pixels wide */
    mousey /= points; /* Scale mousey down */

    /* See if the mouse has moved. */
    if (conditionmask & MOUSEMOVE) {
      if (saved) {
        if (egavga)
          plotegavgacursor(0);
        else
          POKEATTRIB(oldmx, oldmy, oldword);
        saved = FALSE;
      }

      if (!mousehidden && conditionalhidemouse) /* Check to see if we need to hide */
        if (mousex >= conx1 && mousex <= conx2 &&
            mousey >= cony1 && mousey <= cony2) {
          mousehidden++;
          conditionalhidemouse = FALSE;
        }

      if (!mousehidden) {
        if (egavga)
          drawegavgacursor();
        else {
          oldword = PEEKATTRIB(mousex, mousey);
	  asm mov ax, oldword;	/* Prepare to rotate attrib byte */
          asm and al, 0f7h; /* Clear high bit */
          asm mov cl, 4   /* We want to rotate 4 bits */
          asm rol al, cl  /* Rotate it */
	  asm mov newword, ax;

          POKEATTRIB(mousex, mousey, newword); /* Write out new mouse cursor */
        }

        oldmx = mousex;
        oldmy = mousey;
        saved = TRUE;

      }
    }
  }

  /* Now, see if a mouse button was whacked */
  if (conditionmask & ~MOUSEMOVE)
    if (((mbufin + 1) % MOUSEBUFFERSIZE) == mbufout) { /* Buffer full? */
#ifdef __TURBOC__
      sound(1760); /* Make some noise. */
      delay(10);
      nosound();
#endif
    } else {
      mbuf[mbufin].buttonstat = conditionmask & ~MOUSEMOVE;
      mbuf[mbufin].cx = mousex;
      mbuf[mbufin].cy = mousey;
      mbuf[mbufin].shiftstate = peekb(0, 0x417); /* Get shift byte */
      mbufin = (mbufin + 1) % MOUSEBUFFERSIZE;
    }

  asm pop ds;
}

/************************************/
/* 26-Oct-1990 - dk                 */
/*                                  */
/*  Initialize the mouse routines.  */
/*                                  */
/************************************/
void FAST MOUinit(void)
{
extern int graphicalCursor;
byte v;

#ifndef __TURBOC__
  asm mov cs:DGroupSeg,ds   /* save for interrupt handler to use */
#endif

  asm sub ax,ax;    /* Mouse driver function 0 -- reset and detect */
  asm int 33h
  asm mov mouseinstalled, AX;

  if (mouseinstalled) { /* If a mouse is installed then activate driver */

    mousefreeze++; /* Make sure handler doesn't do things, yet */

    asm mov ax,0F00h;
    asm int 10h;
    asm mov v,al;

    if (v == 7) {
      vseg = 0xb000u;
    } else {
      vseg = 0xb800u;
      v = 3;
    }

    if (ROWS == 0) { /* No value, assume 80x25. */
      mrows = 25;
      mcols = 80;
      points = 8;
    } else {
      mrows = ROWS + 1;
      mcols = COLS;
      points = POINTS;
    }

    /* Check to see if we are running in DESQview.  If so, don't try to
       use the 'true' EGA/VGA cursor (DV doesn't like it at ALL). */

    asm mov ax, 2b01h;
    asm mov cx, 4445h;
    asm mov dx, 5351h;
    asm int 21h;

    asm cmp al, 0ffh;
    asm je notdv;

    desqview = TRUE;

notdv:

    /* Do we have an EGA or VGA?  If so, and we are not in monochrome mode
       and we are not in DESQview then setup to draw a 'true' mouse cursor
       on an EGA/VGA */
    egavga = isegavga() && vseg != 0xb000u && !desqview && graphicalCursor;

    if (egavga) {
      /* We are going to use our 'true' mouse cursor and we need pixel
         resolution, not character resolution from the mouse driver
         (In text mode, the mouse driver only returns coordinates in multiples
         of 8, which we don't want.  We want multiples of 1, i.e. pixel
         resolution).  To get the mouse driver to return coordinates in pixel
         resolution, we 'trick' it into thinking it's in graphics mode by
         setting the low memory byte indicating mode to mode 6 (CGA 640x200x2).
         Then we reset the mouse driver.  The mouse driver will get the video
         mode then act as if it was in graphics mode, not text mode. */
      savevmode = peekb(0x40, 0x49);
      pokeb(0x40, 0x49, 6); /* Does this work ?!?!?!?!? */

      /* Reset driver for change in video mode to take effect. */
      asm sub ax,ax
      asm int 33h

      /* Now that we've tricked the mouse driver into a grapics mode thereby
         causing it to give us pixel resolutions, put the old mode back. */
      pokeb(0x40, 0x49, savevmode);
    }

    /* Set up max x and y ranges. */

    maxx = mcols * 8 - 1; /* Pixels horizontally */
    maxy = mrows * points - 1; /* Pixels vertically */

    asm mov dx,maxx     /* Pixels horizontally */
    asm mov ax,7        /* mouse driver function 7 -- set max x range */
    asm sub cx,cx       /* Minimum range */
    asm int 33h

    asm mov dx,maxy     /* Pixels veritcally */
    asm mov ax,8        /* mouse driver function 8 -- set max y range */
    asm sub cx,cx       /* Minimum range */
    asm int 33h

    /* Now install user routine */

    asm mov ax,cs
    asm mov es,ax
    asm mov dx, offset mousehandler
    /* Setup up bits for calling routine */
#ifdef __TURBOC__
    _CX = LEFTBPRESS | LEFTBRELEASE | RIGHTBPRESS | RIGHTBRELEASE | MIDBPRESS |
	  MIDBRELEASE | MOUSEMOVE;
#else
    asm mov cx, LEFTBPRESS | LEFTBRELEASE | RIGHTBPRESS | RIGHTBRELEASE | MIDBPRESS | MIDBRELEASE | MOUSEMOVE;
#endif
    asm mov ax,12       /* Function 12 -- set user routine */
    asm int 33h

    mousex = mousey = mousepx = mousepy = 0;
    asm mov cx,mousepx	 /* xcoord */
    asm mov dx,mousepy	 /* ycoord */
    asm mov ax,4    /* mouse driver function 4 -- set mouse position */
    asm int 33h

    MOUshow(); /* Call it twice just to make sure */

    mousefreeze--; /* Handler can get into business, now */
  }
}

/****************************/
/* 26-Oct-1990 - dk         */
/*                          */
/*  Hide the mouse cursor.  */
/*                          */
/****************************/
void FAST MOUhide(void)
/* This function turns off the mouse cursor, the mouse still responds
   to button presses */
{
  if (!mouseinstalled)
    return;

  mousefreeze++; /* don't have the handler doing weird things */

  mousehidden++; /* indicate it's hidden now */

  if (saved) {
    if (egavga)
      plotegavgacursor(0);
    else
      POKEATTRIB(oldmx, oldmy, oldword);
    saved = FALSE;
  }

  mousefreeze--; /* reactivate handler */
}

/****************************/
/* 26-Oct-1990 - dk         */
/*                          */
/*  Show the mouse cursor.  */
/*                          */
/****************************/
void FAST MOUshow(void)
{
  if (!mouseinstalled)
    return;

  mousefreeze++; /* don't have the handler doing weird things */

  /* Just in case we were in a conditionalhide */
  if (conditionalhidemouse) {
    /* We were about to conditional hide, but we didn't, don't reactive
       mouse cursor. */
    conditionalhidemouse = FALSE;
    mousefreeze--; /* Reactivate handler */
    return;
  }

  if (mousehidden)
    mousehidden--;
  else {
    mousefreeze--; /* Reactivate handler */
    return;  /* It isn't hidden! */
  }

  if (mousehidden) {
    mousefreeze--; /* reactivate handler */
    return; /* still hidden! */
  }

  /* Draw mouse cursor */

  if (egavga)
    drawegavgacursor();
  else {
    oldword = PEEKATTRIB(mousex, mousey);
    asm mov ax, oldword;  /* Prepare to rotate attrib byte */
    asm and al, 0f7h; /* Clear high bit */
    asm mov cl, 4   /* We want to rotate 4 bits */
    asm rol al, cl  /* Rotate it */
    asm mov newword, ax;

    POKEATTRIB(mousex, mousey, newword); /* Write out new mouse cursor */
  }

  oldmx = mousex;
  oldmy = mousey;
  saved = TRUE;

  mousefreeze--; /* Reactivate handler */
}

/*************************************************************/
/* 27-Oct-1990 - dk                                          */
/*                                                           */
/*  Returns true if there is something in the mouse buffer.  */
/*                                                           */
/*************************************************************/
mouseboolean FAST MOUcheck(void)
{
  return mbufin != mbufout;
}

/**************************************************************/
/* 26-Oct-1990 - dk                                           */
/*                                                            */
/*  Take a copy of the mouse event at the head of the queue.  */
/*                                                            */
/**************************************************************/
void FAST MOUpreview(MOUINFOREC *mouinforec)
{
  if (!mouseinstalled)
    return;

  if (mbufin != mbufout) /* if something is in buffer */
    *mouinforec = mbuf[mbufout];
  else {
    /* Nothing to pull, just report mouse position */
    mouinforec -> cx = mousex;
    mouinforec -> cy = mousey;
    mouinforec -> buttonstat = 0;
    mouinforec -> shiftstate = peekb(0, 0x417);
  }
}

/****************************************************************/
/* 26-Oct-1990 - dk                                             */
/*                                                              */
/*  Get (and remove) the mouse event at the head of the queue.  */
/*                                                              */
/****************************************************************/
void FAST MOUget(MOUINFOREC *mouinforec)
{
  if (!mouseinstalled)
    return;

  if (mbufin != mbufout) { /* if something is in buffer */
    if (mouinforec != NULL)
      *mouinforec = mbuf[mbufout];
    mbufout = (mbufout + 1) % MOUSEBUFFERSIZE;
  } else {
    /* Nothing to pull, just report mouse position */
    mouinforec -> cx = mousex;
    mouinforec -> cy = mousey;
    mouinforec -> buttonstat = 0;
    mouinforec -> shiftstate = peekb(0, 0x417);
  }
}

/**************************************/
/* 26-Oct-1990 - dk                   */
/*                                    */
/*  Deinitialize the mouse routines.  */
/*                                    */
/**************************************/
void FAST MOUdeinit(void)
{
  if (!mouseinstalled)
    return;

  MOUhide();

  asm sub ax,ax
  asm int 33h
}

/**************************************************/
/* 26-Oct-1990 - dk                               */
/*                                                */
/*  Returns the bits for the button status info.  */
/*                                                */
/**************************************************/
word FAST MOUbuttonstatus(void)
{
word buts;

  if (!mouseinstalled)
    return 0;

  asm mov ax,3
  asm int 33h
  asm mov buts,bx
  return buts;
}

/************************************************************************/
/* 26-Oct-1990 - dk                                                     */
/*                                                                      */
/*  Hide the mouse *if* it enters a certain screen area, automatically. */
/*                                                                      */
/************************************************************************/
void FAST MOUconditionalhide(int x1, int y1, int x2, int y2)
{
  if (!mouseinstalled)
    return;

  mousefreeze++; /* hold the handler */

  if (mousehidden) {
    mousefreeze--; /* reactivate handler */
    return; /* already hidden! */
  }

  conditionalhidemouse = TRUE;

  x1 -= 2;
  if (x1 < 0)
    x1 = 0;
  y1 -= 2;
  if (y1 < 0)
    y1 = 0;
  x2 += 2;
  y2 += 2;

  conx1 = x1;
  cony1 = y1;
  conx2 = x2;
  cony2 = y2;

  if (mousex >= conx1 && mousex <= conx2 &&
      mousey >= cony1 && mousey <= cony2) {
    conditionalhidemouse = FALSE; /* We've already hidden it */
    MOUhide(); /* turn it off now if it's there. */
  }

  mousefreeze--;
}

/*********************************************************/
/* Fri 15-Mar-1991 - dk 				 */
/*							 */
/*  Set the mouse cursor to a specific screen position.  */
/*							 */
/*********************************************************/
void FAST MOUsetpos(word x, word y)
{
  if (!mouseinstalled)
    return;

  mousefreeze++;

  MOUhide();

  mousex = x;
  mousey = y;
  mousepx = mousex * 8;
  mousepy = mousey * points;
  asm mov cx, mousepx	/* xcoord */
  asm mov dx, mousepy	/* ycoord */
  asm mov ax, 4 	/* mouse driver function 4 -- set mouse position */
  asm int 33h

  MOUshow();

  mousefreeze--;
}