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

  1.  /*
  2.     MOUDEMO2.C --- Simple OS/2 Demo of Mouse Subsystem
  3.     Copyright (C) 1989 Ziff Davis Communications
  4.     PC Magazine * Ray Duncan
  5.  
  6.     Compile:    CL MOUDEMO2.C
  7.  
  8.     Usage:      MOUDEMO2  (press both mouse buttons to exit)
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. void cls(void);                     /* local function prototypes */
  14. void gotoxy(int, int);
  15. int  mopen(int *);
  16. void mclose(int);
  17. void mread(int, int *, int *, int *);
  18. void mhide(int);
  19. void munhide(int);
  20.  
  21. #define API unsigned extern far pascal
  22.  
  23. API MouClose(int);                  /* OS/2 function prototypes */
  24. API MouDrawPtr(int);
  25. API MouOpen(void far *, int far *);
  26. API MouReadEventQue(void far *, int far *, int);
  27. API MouRemovePtr(void far *, int);
  28.  
  29. struct _MouEvent {  unsigned Flags;
  30.                     long Timestamp;
  31.                     int Row;
  32.                     int Col;
  33.                  }  ;
  34.  
  35. main()
  36. {
  37.     int x,y,buttons;                /* mouse state variables */
  38.     int mhandle;                    /* mouse handle from mopen */
  39.  
  40.     if(! mopen(&mhandle))           /* exit if no mouse */
  41.     {   
  42.         printf("\nMouse not available\n");
  43.         exit(1);
  44.     }
  45.  
  46.     cls();                          /* clear the screen */  
  47.     gotoxy(45,0);                   /* and show help info */
  48.     puts("Press Both Mouse Buttons To Exit");
  49.  
  50.     munhide(mhandle);               /* make mouse cursor visible */
  51.    
  52.     do {                            /* read mouse position & buttons */
  53.         mread(mhandle, &x, &y, &buttons);
  54.         gotoxy(0,0);                /* display mouse status */
  55.         printf("X = %3d  Y = %3d", x, y);
  56.     }   while(buttons != 3);        /* exit if both buttons down */
  57.  
  58.     mhide(mhandle);                 /* hide mouse cursor */
  59.     mclose(mhandle);                /* release mouse driver */
  60.     cls();                          /* clear screen and exit */
  61. }
  62.  
  63. /*
  64.     Clear the screen and position cursor at (x,y) = (0,0)
  65. */
  66. void cls(void)
  67. {
  68.     printf("\x01b[2J");
  69. }
  70.  
  71. /*
  72.     Position cursor to (x,y); ANSI driver uses 1-based coordinates.
  73. */
  74. void gotoxy(int x, int y)
  75. {
  76.     printf("\x01b[%d;%dH",y+1,x+1);
  77. }
  78.  
  79. /*
  80.     Initialize mouse driver, return mouse status and handle.
  81.     Status is true if mouse available, false if not.  
  82. */
  83. int mopen(int *handle)
  84. {
  85.     return(! MouOpen(0L, handle));  /* open mouse, save handle, */
  86. }                                   /* and return status */                 
  87.  
  88. /*
  89.     Make mouse pointer visible. 
  90. */
  91. void munhide(handle)
  92. {
  93.     MouDrawPtr(handle);
  94. }
  95.  
  96. /*
  97.     Hide mouse pointer.  For the purposes of this demo, we 
  98.     assume an 80-by-25 text mode.
  99. */
  100. void mhide(handle)
  101. {
  102.     static int HideArea[4] = { 0, 0, 24, 79 } ;
  103.     
  104.     MouRemovePtr(HideArea, handle);
  105. }
  106.  
  107. /*
  108.     Read mouse position and button status.  
  109. */
  110. void mread(int handle, int *x, int *y, int *buttons)
  111. {
  112.     int WaitOption = 1 ;            /* wait for a mouse event */
  113.     struct _MouEvent MouEvent;      /* receives mouse information */
  114.  
  115.     MouReadEventQue(&MouEvent, &WaitOption, handle);
  116.  
  117.     *x = MouEvent.Col;              /* return mouse coordinates */
  118.     *y = MouEvent.Row;              /* and button status */
  119.     *buttons = ((MouEvent.Flags&4)>>2) | ((MouEvent.Flags&0x10)>>3) & 3;
  120. }
  121.  
  122. /*
  123.     Release mouse driver.  
  124. */
  125. void mclose(handle)
  126. {
  127.     MouClose(handle);
  128. }
  129.  
  130.