您的位置:首页 > 运维架构 > Linux

浅析linux中鼠标数据读取

2010-03-31 12:34 351 查看
浅析linux中鼠标数据读取
luther@gliethttp:~$ ll /dev/input/mice
crw-rw---- 1 root root 13, 63 2009-07-09 15:54 /dev/input/mice
我们就是去读/dev/input/mice设备节点,源码如下:

001 #include <stdio.h>
002 #include <errno.h>
003 #include <fcntl.h>
004 #include <sys/select.h>
005 #include <string.h>
006 // 以下代码源自
007 // [需要在文本控制台下运行,才能获取到数据,在ubuntu 8.10 GNOME界面只能收到1个字节数据0xfa][luther.gliethttp]
008 // libminigui-1.6.10/src/ial/native/native.c
009 // InitIAL==>__mg_cur_input = inputs{"console", InitNativeInput, TermNativeInput},
010 // mousedev = mousedev_IMPS2;
011 // input->update_mouse = mouse_update;
012 /* Mouse button bits*/
013 #define WHEEL_UP 0x10
014 #define WHEEL_DOWN 0x08
015 #define BUTTON_L 0x04
016 #define BUTTON_M 0x02
017 #define BUTTON_R 0x01
018 #define SCALE 3 /* default scaling factor for acceleration */
019 #define THRESH 5 /* default threshhold for acceleration */
020 static int xpos; /* current x position of mouse */
021 static int ypos; /* current y position of mouse */
022 static int minx; /* minimum allowed x position */
023 static int maxx; /* maximum allowed x position */
024 static int miny; /* minimum allowed y position */
025 static int maxy; /* maximum allowed y position */
026 static int buttons; /* current state of buttons */
027 static int scale = SCALE; /* acceleration scale factor */
028 static int thresh = THRESH;/* acceleration threshhold */
029 static int mouse_update(int dx, int dy, int dz);
030 static int IMPS2_Read (int *dx, int *dy, int *dz, int *bp);
031 static void mouse_setposition (int newx, int newy);
032 static void mouse_setrange (int newminx, int newminy, int newmaxx, int newmaxy);
033 int mouse_fd;
034 int main(void)
035 {
036 int dx,dy,dz;
037 static unsigned char imps2_param [] = {243,200,243,100,243,80};//,242};
038 // 来自vnc4的xc/programs/Xserver/hw/xfree86/input/mouse/mouse.c==>PROT_IMPS2
039 const char *mdev="/dev/input/mice";
040 mouse_fd = open (mdev, O_RDWR); // | O_NONBLOCK);
041 if (mouse_fd < 0) {
042 printf("[luther.gliethttp]: RW error [please use root user]: %s/n", mdev);
043 mouse_fd = open (mdev, O_RDONLY); // | O_NONBLOCK);
044 if (mouse_fd < 0)
045 return -1;
046 } else {
047 write (mouse_fd, imps2_param, sizeof (imps2_param)); // 初始化序列, 这样可以读取4个字节数据
048 // 0x80用来表示滚轮向上还是向下滚动.de>
049 de> // 0xa0表示滚轮向上滚动的同时中键按下
050 de>
051 de> printf("[luther.gliethttp]: imps2_param ok!/n");
052 }
053 mouse_setrange(0, 0, 1024, 768);
054 for (;;) {
055 IMPS2_Read(&dx, &dy, &dz, &buttons);
056 mouse_update(dx, dy, dz);
057 mouse_setposition(xpos, ypos);
058 printf("[%04d,%04d,0x%04x]/n", xpos, ypos, buttons);
059 }
060 return 0;
061 }
062 static int IMPS2_Read (int *dx, int *dy, int *dz, int *bp)
063 {
064 static unsigned char buf[5];
065 static int buttons[7] = { 0, 1, 3, 0, 2, 0, 0}; // 1左键,2中键,3右键
066 static int nbytes;
067 int n;
068 while ((n = read (mouse_fd, &buf [nbytes], 4 - nbytes))) {
069 if (n < 0) {
070 if (errno == EINTR)
071 continue;
072 else
073 return -1;
074 }
075 nbytes += n;
076 if (nbytes == 4) {
077 int wheel;
078 // printf("[luther.gliethttp]: %02x %02x %02x %02x/n", buf[0], buf[1], buf[2], buf[3]);
079 if ((buf[0] & 0xc0) != 0) {
080 buf[0] = buf[1];
081 buf[1] = buf[2];
082 buf[2] = buf[3];
083 nbytes = 3;
084 return -1;
085 }
086 /* FORM XFree86 4.0.1 */
087 *bp = buttons[(buf[0] & 0x07)];
088 *dx = (buf[0] & 0x10) ? buf[1] - 256 : buf[1];
089 *dy = (buf[0] & 0x20) ? -(buf[2] - 256) : -buf[2];
090 /* Is a wheel event? */
091 if ((wheel = buf[3]) != 0) {
092 if(wheel > 0x7f) {
093 *bp |= WHEEL_UP;
094 }
095 else {
096 *bp |= WHEEL_DOWN;
097 }
098 }
099 *dz = 0;
100 nbytes = 0;
101 return 1;
102 }
103 }
104 return 0;
105 }
106 static int mouse_update(int dx, int dy, int dz)
107 {
108 int r;
109 int sign;
110 sign = 1;
111 if (dx < 0) {
112 sign = -1;
113 dx = -dx;
114 }
115 if (dx > thresh)
116 dx = thresh + (dx - thresh) * scale;
117 dx *= sign;
118 xpos += dx;
119 if( xpos < minx )
120 xpos = minx;
121 if( xpos > maxx )
122 xpos = maxx;
123 sign = 1;
124 if (dy < 0) {
125 sign = -1;
126 dy = -dy;
127 }
128 if (dy > thresh)
129 dy = thresh + (dy - thresh) * scale;
130 dy *= sign;
131 ypos += dy;
132 if ( ypos < miny )
133 ypos = miny;
134 if ( ypos > maxy )
135 ypos = maxy;
136 return 1;
137 }
138 static void mouse_setposition (int newx, int newy)
139 {
140 if (newx < minx)
141 newx = minx;
142 if (newx > maxx)
143 newx = maxx;
144 if (newy < miny)
145 newy = miny;
146 if (newy > maxy)
147 newy = maxy;
148 if (newx == xpos && newy == ypos)
149 return;
150 xpos = newx;
151 ypos = newy;
152 }
153 static void mouse_setrange (int newminx, int newminy, int newmaxx, int newmaxy)
154 {
155 minx = newminx;
156 miny = newminy;
157 maxx = newmaxx;
158 maxy = newmaxy;
159 mouse_setposition ((newminx + newmaxx) / 2, (newminy + newmaxy) / 2);
160 }
161 static int mouse_getbutton (void)
162 {
163 return buttons;
164 }
165 static void mouse_getxy (int* x, int* y)
166 {
167 *x = xpos;
168 *y = ypos;
169 }
170
171
172 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/linucos/archive/2010/03/31/5436767.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: