Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1463900
  • 博文数量: 267
  • 博客积分: 3010
  • 博客等级: 少校
  • 技术积分: 3089
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-05 17:09
个人简介

尊天命,尽人事

文章分类

全部博文(267)

文章存档

2017年(6)

2015年(4)

2014年(27)

2013年(52)

2012年(59)

2011年(120)

分类: LINUX

2013-07-09 14:26:18

我们就是去读/dev/input/mice设备节点,读取四个字节,第一个字节低三位表示button的类型,0x00表示上一次按下的按键抬起,0x01表示左键按下,0x02表示中键按下,0x3表示右键按下。第四个字节表示中键的状态,大于0x7F,表示向上滚动,否则向下滚动,


源码如下:


  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <sys/select.h>
  5. #include <string.h>

  6. // 以下代码源自
  7. // [需要在文本控制台下运行,才能获取到数据,在ubuntu 8.10 GNOME界面只能收到1个字节数据0xfa][luther.gliethttp]
  8. // libminigui-1.6.10/src/ial/native/native.c
  9. // InitIAL==>__mg_cur_input = inputs{"console", InitNativeInput, TermNativeInput},
  10. // mousedev = mousedev_IMPS2;
  11. // input->update_mouse = mouse_update;

  12. /* Mouse button bits*/
  13. #define WHEEL_UP 0x10
  14. #define WHEEL_DOWN 0x08

  15. #define BUTTON_L 0x04
  16. #define BUTTON_M 0x02
  17. #define BUTTON_R 0x01
  18. #define SCALE 3 /* default scaling factor for acceleration */
  19. #define THRESH 5 /* default threshhold for acceleration */

  20. static int xpos; /* current x position of mouse */
  21. static int ypos; /* current y position of mouse */
  22. static int minx; /* minimum allowed x position */
  23. static int maxx; /* maximum allowed x position */
  24. static int miny; /* minimum allowed y position */
  25. static int maxy; /* maximum allowed y position */
  26. static int buttons; /* current state of buttons */
  27. static int scale = SCALE; /* acceleration scale factor */
  28. static int thresh = THRESH;/* acceleration threshhold */

  29. static int mouse_update(int dx, int dy, int dz);
  30. static int IMPS2_Read (int *dx, int *dy, int *dz, int *bp);
  31. static void mouse_setposition (int newx, int newy);
  32. static void mouse_setrange (int newminx, int newminy, int newmaxx, int newmaxy);

  33. int mouse_fd;

  34. int main(void)
  35. {
  36.     int dx,dy,dz;
  37.     static unsigned char imps2_param [] = {243,200,243,100,243,80};//,242};
  38.     // 来自vnc4的xc/programs/Xserver/hw/xfree86/input/mouse/mouse.c==>PROT_IMPS2
  39.     const char *mdev="/dev/input/mice";

  40.     mouse_fd = open (mdev, O_RDWR); // | O_NONBLOCK);
  41.     if (mouse_fd < 0) {
  42.         printf("[luther.gliethttp]: RW error [please use root user]: %s\n", mdev);
  43.         mouse_fd = open (mdev, O_RDONLY); // | O_NONBLOCK);
  44.         if (mouse_fd < 0)
  45.             return -1;
  46.     } else {
  47.         write (mouse_fd, imps2_param, sizeof (imps2_param)); // 初始化序列, 这样可以读取4个字节数据
  48.         // 0x80用来表示滚轮向上还是向下滚动.

  49.         // 0xa0表示滚轮向上滚动的同时中键按下

  50.         printf("[luther.gliethttp]: imps2_param ok!\n");
  51.     }
  52.     
  53.     mouse_setrange(0, 0, 1024, 768);

  54.     for (;;) {
  55.         IMPS2_Read(&dx, &dy, &dz, &buttons);
  56.         mouse_update(dx, dy, dz);
  57.         mouse_setposition(xpos, ypos);
  58.         printf("[%04d,%04d,0x%04x]\n", xpos, ypos, buttons);
  59.     }
  60.     
  61.     return 0;
  62. }

  63. static int IMPS2_Read (int *dx, int *dy, int *dz, int *bp)
  64. {
  65.     static unsigned char buf[5];
  66.     static int buttons[7] = { 0, 1, 3, 0, 2, 0, 0}; // 1左键,2中键,3右键
  67.     static int nbytes;
  68.     int n;

  69.     while ((n = read (mouse_fd, &buf [nbytes], 4 - nbytes))) {
  70.         if (n < 0) {
  71.             if (errno == EINTR)
  72.                 continue;
  73.             else
  74.                 return -1;
  75.         }

  76.         nbytes += n;

  77.         if (nbytes == 4) {
  78.             int wheel;
  79.             // printf("[luther.gliethttp]: %02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
  80.             if ((buf[0] & 0xc0) != 0) {
  81.                 buf[0] = buf[1];
  82.                 buf[1] = buf[2];
  83.                 buf[2] = buf[3];
  84.                 nbytes = 3;

  85.                 return -1;
  86.             }

  87.             /* FORM XFree86 4.0.1 */
  88.             *bp = buttons
  89.             *dx = (buf[0] & 0x10) ? buf[1] - 256 : buf[1];
  90.             *dy = (buf[0] & 0x20) ? -(buf[2] - 256) : -buf[2];

  91.             /* Is a wheel event? */
  92.             if ((wheel = buf[3]) != 0) {
  93.                 if(wheel > 0x7f) {
  94.                     *bp |= WHEEL_UP;
  95.                 }
  96.                 else {
  97.                     *bp |= WHEEL_DOWN;
  98.                 }
  99.             }
  100.              
  101.             *dz = 0;
  102.             nbytes = 0;
  103.             return 1;
  104.         }
  105.     }
  106.     return 0;
  107. }

  108. static int mouse_update(int dx, int dy, int dz)
  109. {
  110.     int r;
  111.     int sign;
  112.     
  113.     sign = 1;
  114.     if (dx < 0) {
  115.         sign = -1;
  116.         dx = -dx;
  117.     }
  118.     if (dx > thresh)
  119.         dx = thresh + (dx - thresh) * scale;
  120.     dx *= sign;
  121.     xpos += dx;
  122.     if( xpos < minx )
  123.         xpos = minx;
  124.     if( xpos > maxx )
  125.         xpos = maxx;

  126.     sign = 1;
  127.     if (dy < 0) {
  128.         sign = -1;
  129.         dy = -dy;
  130.     }
  131.     if (dy > thresh)
  132.         dy = thresh + (dy - thresh) * scale;
  133.     dy *= sign;
  134.     ypos += dy;
  135.     if ( ypos < miny )
  136.         ypos = miny;
  137.     if ( ypos > maxy )
  138.         ypos = maxy;

  139.     return 1;
  140. }

  141. static void mouse_setposition (int newx, int newy)
  142. {
  143.     if (newx < minx)
  144.         newx = minx;
  145.     if (newx > maxx)
  146.         newx = maxx;
  147.     if (newy < miny)
  148.         newy = miny;
  149.     if (newy > maxy)
  150.         newy = maxy;
  151.     if (newx == xpos && newy == ypos)
  152.         return;
  153.     xpos = newx;
  154.     ypos = newy;
  155. }

  156. static void mouse_setrange (int newminx, int newminy, int newmaxx, int newmaxy)
  157. {
  158.     minx = newminx;
  159.     miny = newminy;
  160.     maxx = newmaxx;
  161.     maxy = newmaxy;
  162.     mouse_setposition ((newminx + newmaxx) / 2, (newminy + newmaxy) / 2);
  163. }

  164. static int mouse_getbutton (void)
  165. {
  166.     return buttons;
  167. }

  168. static void mouse_getxy (int* x, int* y)
  169. {
  170.     *x = xpos;
  171.     *y = ypos;
  172. }


阅读(1705) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~