home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / SVGALIB / SVGALIB1.TAR / svgalib / src / mouse / mouse.c next >
Encoding:
C/C++ Source or Header  |  1994-12-30  |  3.5 KB  |  183 lines

  1. /* hhanemaa@cs.ruu.nl */
  2. /* Mouse library functions */
  3.  
  4. #include <signal.h>
  5. #include <sys/time.h>
  6. #include <sys/types.h>
  7. #include <vga.h>
  8. #include "vgamouse.h"
  9.  
  10. static void (*__mouse_eventhandler)( int, int, int );
  11.  
  12. #include "ms.c"        /* include low-level mouse driver */
  13.  
  14.  
  15. static struct sigaction oldsiga;
  16. static void (*currentinthandler)();
  17.  
  18. static void inthandler() {
  19.     mouse_close();
  20.     /* restore old interrupt */
  21.     sigaction(SIGINT, &oldsiga, NULL);
  22.     raise(SIGINT);
  23. }
  24.  
  25. static void default_handler( int, int, int );
  26.  
  27. int mouse_init_return_fd( char *dev, int type, int samplerate ) {
  28.     struct sigaction siga;
  29.  
  30.     if (strcmp(dev, "") == 0)
  31.         mdev = "/dev/mouse";
  32.     else
  33.         mdev = dev;
  34.     mtype = type & MOUSE_TYPE_MASK;
  35.     m_modem_ctl = type & ~MOUSE_TYPE_MASK;
  36.  
  37.     msample = samplerate;
  38.  
  39.     currentinthandler = NULL;
  40.  
  41.     /* Initialize mouse device. */
  42.     if (mtype < MOUSE_MICROSOFT || mtype > MOUSE_PS2)
  43.         return -1;
  44.     if (ms_init())
  45.         return -1;
  46.  
  47.     /* Install default mouse handler. Old coordinates are preserved. */
  48.     __mouse_eventhandler = default_handler;
  49.  
  50.     /* Install interrupt handler. */
  51.     currentinthandler = inthandler;
  52.     siga.sa_handler = inthandler;
  53.     siga.sa_flags = 0;
  54.     siga.sa_mask = 0;
  55.     sigaction(SIGINT, &siga, &oldsiga);
  56.  
  57.     return mfd;    /* Return mouse fd. */
  58. }
  59.  
  60. /* Old compatible mouse_init, returns 0 if succesful, -1 on error. */
  61.  
  62. int mouse_init( char *dev, int type, int samplerate ) {
  63.     if (mouse_init_return_fd(dev, type, samplerate) == -1)
  64.         return -1;
  65.     else
  66.         return 0;
  67. }
  68.  
  69. /* For now, we assume there's no console switching. */
  70.  
  71. int mouse_update() {
  72.     int result;
  73.     result = get_ms_event(0);
  74.     return result;
  75. }
  76.  
  77. void mouse_waitforupdate() {
  78. #if 0
  79.     fd_set *readfds, writefds, exceptfds;
  80.     struct timeval timeout;
  81.     FD_ZERO(readfds);
  82.     FD_ZERO(writefds);
  83.     FD_ZERO(exceptfds);
  84.     FD_SET(__svgalib_mouse_fd, readfds);
  85.     /* need to setup timeout. */
  86.     select(readfds, writefds, exceptfds, &timeout);
  87. #else
  88.     get_ms_event(1);
  89. #endif
  90.     return;
  91. }
  92.  
  93. void mouse_seteventhandler( void (*handler)( int, int, int ) ) {
  94.     __mouse_eventhandler = handler;
  95. }
  96.  
  97. void mouse_close() {
  98.     ms_close();
  99.     if (currentinthandler != NULL)
  100.         /* Restore old interrupt. */
  101.         sigaction(SIGINT, &oldsiga, NULL);
  102. }
  103.  
  104.  
  105. /* Default event handler. */
  106.  
  107. void mouse_setdefaulteventhandler() {
  108.     __mouse_eventhandler = default_handler;
  109. }
  110.  
  111. static int mouse_x = 0;
  112. static int mouse_y = 0;
  113. static int mouse_button = 0;
  114. static int scale = 1;
  115. static int minx = 0;
  116. static int maxx = 32767;
  117. static int miny = 0;
  118. static int maxy = 32767;
  119. static int wrap = 0;
  120.  
  121. void mouse_setwrap( int state ) {
  122.     wrap = state;
  123. }
  124.  
  125. static void default_handler( int button, int dx, int dy ) {
  126.     mouse_button = button;
  127.     mouse_x += dx;
  128.     mouse_y += dy;
  129.     if (mouse_x / scale > maxx)
  130.         if (wrap & MOUSE_WRAPX)
  131.             mouse_x -= (maxx - minx) * scale;
  132.         else
  133.             mouse_x = maxx * scale;
  134.             /* - 1; ??? */
  135.     if (mouse_x / scale < minx)
  136.         if (wrap & MOUSE_WRAPY)
  137.             mouse_x += (maxx - minx) * scale;
  138.         else
  139.             mouse_x = minx * scale;
  140.     if (mouse_y / scale > maxy)
  141.         if (wrap & MOUSE_WRAPX)
  142.             mouse_y -= (maxy - miny) * scale;
  143.         else
  144.             mouse_y = maxy * scale;
  145.             /*  - 1; ??? */
  146.     if (mouse_y / scale < miny)
  147.         if (wrap & MOUSE_WRAPY)
  148.             mouse_y += (maxy - miny) * scale;
  149.         else
  150.             mouse_y = miny * scale;
  151. }
  152.  
  153. void mouse_setposition( int x, int y ) {
  154.     mouse_x = x;
  155.     mouse_y = y;
  156. }
  157.  
  158. void mouse_setxrange( int x1, int x2 ) {
  159.     minx = x1;
  160.     maxx = x2;
  161. }
  162.  
  163. void mouse_setyrange( int y1, int y2 ) {
  164.     miny = y1;
  165.     maxy = y2;
  166. }
  167.  
  168. void mouse_setscale( int s ) {
  169.     scale = s;
  170. }
  171.  
  172. int mouse_getx() {
  173.     return mouse_x / scale;
  174. }
  175.  
  176. int mouse_gety() {
  177.     return mouse_y / scale;
  178. }
  179.  
  180. int mouse_getbutton() {
  181.     return mouse_button;
  182. }
  183.