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

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