home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MOUSE.ZIP / MOUDEMO1.C next >
Text File  |  1989-01-02  |  3KB  |  121 lines

  1.  /*
  2.     MOUDEMO1.C --- Simple MS-DOS Demo of Mouse Driver
  3.     Copyright (C) 1989 Ziff Davis Communications
  4.     PC Magazine * Ray Duncan
  5.  
  6.     Compile:    CL MOUDEMO1.C
  7.  
  8.     Usage:      MOUDEMO1  (press both mouse buttons to exit)
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <dos.h>
  13.  
  14. union REGS regs;
  15.  
  16. void cls(void);                     /* function prototypes */
  17. void gotoxy(int, int);
  18. int  mopen(int *);
  19. void mclose(int);
  20. void mread(int, int *, int *, int *);
  21. void mhide(int);
  22. void munhide(int);
  23.  
  24. main(int argc, char *argv[])
  25. {
  26.     int x,y,buttons;                /* mouse state variables */
  27.     int mhandle;                    /* mouse handle from mopen */
  28.  
  29.     if(! mopen(&mhandle))           /* exit if no mouse */
  30.     {   
  31.         printf("\nMouse not available\n");
  32.         exit(1);
  33.     }
  34.  
  35.     cls();                          /* clear the screen */  
  36.     gotoxy(45,0);                   /* and show help info */
  37.     puts("Press Both Mouse Buttons To Exit");
  38.  
  39.     munhide(mhandle);               /* make mouse cursor visible */
  40.    
  41.     do {                            /* read mouse position & buttons */
  42.         mread(mhandle, &x, &y, &buttons);
  43.         gotoxy(0,0);                /* display mouse status */
  44.         printf("X = %3d  Y = %3d", x, y);
  45.     }   while(buttons != 3);        /* exit if both buttons down */
  46.  
  47.     mhide(mhandle);                 /* hide mouse cursor */
  48.     mclose(mhandle);                /* release mouse driver */
  49.     cls();                          /* clear screen and exit */
  50. }
  51.  
  52. /*
  53.     Clear the screen and position cursor at (x,y) = (0,0)
  54. */
  55. void cls(void)
  56. {
  57.     printf("\x01b[2J");
  58. }
  59.  
  60. /*
  61.     Position cursor to (x,y); ANSI driver uses 1-based coordinates.
  62. */
  63. void gotoxy(int x, int y)
  64. {
  65.     printf("\x01b[%d;%dH",y+1,x+1);
  66. }
  67.  
  68. /*
  69.     Initialize mouse driver, return mouse status and handle.
  70.     Status is true if mouse available, false if not.  Handle is
  71.     dummy value in MS-DOS version.
  72. */
  73. int mopen(int *handle)
  74. {
  75.     regs.x.ax = 0;                  /* function 0 = reset driver */
  76.     int86(0x33, ®s, ®s);
  77.     *handle = 0;                    /* set dummy mouse handle */
  78.     return(regs.x.ax);              /* and return status */
  79. }
  80.  
  81. /*
  82.     Make mouse pointer visible. Mouse handle is dummy in MS-DOS version.
  83. */
  84. void munhide(handle)
  85. {
  86.     regs.x.ax = 1;                  /* function 1 = display pointer */
  87.     int86(0x33, ®s, ®s);
  88. }
  89.  
  90. /*
  91.     Hide mouse pointer.  Mouse handle is dummy in MS-DOS version.
  92. */
  93. void mhide(handle)
  94. {
  95.     regs.x.ax = 2;                  /* function 2 = hide pointer */
  96.     int86(0x33, ®s, ®s);
  97. }
  98.  
  99. /*
  100.     Read mouse position and button status.  Mouse handle is dummy
  101.     in MS-DOS version.
  102. */
  103. void mread(int handle, int *x, int *y, int *buttons)
  104. {
  105.     regs.x.ax = 3;                  /* function 3 = get position */    
  106.     int86(0x33, ®s, ®s);      /* and button status */
  107.     *buttons = regs.x.bx & 3;
  108.     *x = regs.x.cx;
  109.     *y = regs.x.dx;
  110. }
  111.  
  112. /*
  113.     Release mouse driver.  Mouse handle is dummy in MS-DOS version.
  114. */
  115. void mclose(handle)
  116. {
  117.     regs.x.ax = 0;                  /* function 0 = reset driver */
  118.     int86(0x33, ®s, ®s);
  119. }
  120.  
  121.