home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / src.zoo / src / mouse_get.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-17  |  2.1 KB  |  81 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: mouse_get.c,v 1.1 89/03/17 08:21:16 sau Exp $
  9.     $Source: /m1/mgr.new/src/RCS/mouse_get.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /m1/mgr.new/src/RCS/mouse_get.c,v $$Revision: 1.1 $";
  12.  
  13. /* get the mouse input */
  14.  
  15. #include <sys/ioctl.h>
  16. #include <stdio.h>
  17.  
  18. #define MOUSE_BUFF    90
  19. #define DECODE(X)    ( (X) &0x80 ? (X) - 256 : (X) )
  20. #define get_char(fd)    (index<cnt ? 0xff & mouse_buff[index++] :mouse_fill(fd))
  21.  
  22. static unsigned char mouse_buff[MOUSE_BUFF];
  23. static int cnt=0;
  24. static int index=0;
  25. static int button_map[8] = { 0,1,2,3,4,5,6,7};
  26.  
  27. int
  28. mouse_fill(mouse)
  29. int mouse;
  30.    {
  31.    cnt = read(mouse,mouse_buff,MOUSE_BUFF);
  32.    index = 1;
  33.    return ( ((int)(*mouse_buff) & 0xff));
  34.    }
  35.    
  36. /* primary mouse interface
  37.    Mouse sample begins with a byte, 0x80 through 0x87, followed by pairs of
  38.    bytes giving motion changes.  Currently we handle 3- or 5-byte mice.
  39. */
  40.  
  41. int
  42. mouse_get(mouse,x_delta,y_delta)
  43. register int mouse;
  44. register int *x_delta, *y_delta;
  45.    {
  46.    static int mouse_type = 3;
  47.    register int delta, button;
  48.  
  49.    while(((button = get_char(mouse))&0xf0) != 0x80)
  50.       if( mouse_type < 5 )
  51.          mouse_type++;
  52.       ;
  53.    delta = get_char(mouse); *x_delta = DECODE(delta);
  54.    delta = get_char(mouse); *y_delta = DECODE(delta);
  55.    if (mouse_type >= 5) {
  56.       /* 5-or-more bytes per sample */
  57.       delta = get_char(mouse), *x_delta += DECODE(delta);
  58.       delta = get_char(mouse), *y_delta += DECODE(delta);
  59.       }
  60.    return(button_map[(~button)&0x7]);
  61.    }
  62.  
  63. /* map mouse buttons (for non-left handers) */
  64.  
  65. int *
  66. map_mouse(button,map)
  67. int button, map;
  68.    {
  69.    if (button > 0 && button < 8)
  70.       button_map[button] = map;
  71.    return(button_map);
  72.    }
  73.  
  74. /* how many chars are sitting in the mouse buffer */
  75.  
  76. int
  77. mouse_count()
  78.    {
  79.    return(cnt>index);
  80.    }
  81.