织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>嵌入式开发>Android>

Android原生(Native)C开发之三 鼠标事件篇(捕鼠记)

落鹤生 发布于 2011-05-12 09:20 点击:次 
在做SDL至Android的移植时,键盘事件是能正常捕获到,看了SLD的源码,发现用的device是 /dev/tty0,但是鼠标叫是不能成功捕获,总是得到 0,运行命令查看devices时,显示如下
TAG:

在做SDL至Android的移植时,键盘事件是能正常捕获到,看了SDL的源码,发现用的device是 /dev/tty0,但是鼠标叫是不能成功捕获,总是得到 0,运行命令查看devices时,显示如下:

  1. # cat /proc/bus/input/devices 
  2. cat /proc/bus/input/devices 
  3. I: Bus=0000 Vendor=0000 Product=0000 Version=0000 
  4. N: Name=”qwerty” 
  5. P: Phys= 
  6. S: Sysfs=/class/input/input0 
  7. U: Uniq= 
  8. H: Handlers=kbd mouse0 event0 
  9. B: EV=2f 
  10. B: KEY=ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff f 
  11. fffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe 
  12. B: REL=3 
  13. B: ABS=7 
  14. B: SW=1  

进入 /dev/input 目录,发现在3个device文件:mice,mouse0,event0,分别 cat这3个文件,发现只有 event0 有反应,如下图:


Android Native 鼠标事件截图1

而且不管是点击鼠标还是按键,都有反应,但显示的是一堆乱码,而且点击鼠标出来的东西要多一点,难道这就是传说是的 touchscreen ?!

为了分析 event0 的返回值,写了一段代码 testmice.c,如下:

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <fcntl.h> 
  4. #include <linux/input.h> 
  5.  
  6. static int event0_fd = -1; 
  7. struct input_event ev0[64]; 
  8.  
  9. //for handling event0, mouse/key/ts 
  10. static int handle_event0() { 
  11. int button = 0, realx = 0, realy = 0, i, rd; 
  12.  
  13. rd = read(event0_fd, ev0, sizeof(struct input_event) * 64); 
  14. if ( rd < sizeof(struct input_event) ) return 0; 
  15.  
  16. for (i = 0; i < rd / sizeof(struct input_event); i++) { 
  17. printf(”", ev0[i].type, ev0[i].code, ev0[i].value); 
  18. if (ev0[i].type == 3 && ev0[i].code == 0) 
  19. realx = ev0[i].value; 
  20. else if (ev0[i].type == 3 && ev0[i].code == 1) 
  21. realy = ev0[i].value; 
  22. else if (ev0[i].type == 1) { 
  23. if (ev0[i].code == 158) { 
  24. //if key esc then exit 
  25. return 0; 
  26. else if (ev0[i].type == 0 && ev0[i].code == 0 && ev0[i].value == 0) { 
  27. realx = 0, realy = 0; 
  28. printf(”event(%d): type: %d; code: %3d; value: %3d; realx: %3d; realy: %3d\n”, i, 
  29. ev0[i].type, ev0[i].code, ev0[i].value, realx, realy); 
  30.  
  31. return 1; 
  32.  
  33. int main(void) { 
  34. int done = 1; 
  35. printf(”sizeof(struct input_event) = %d\n”, sizeof(struct input_event)); 
  36.  
  37. event0_fd = open(”/dev/input/event0″, O_RDWR); 
  38.  
  39. if ( event0_fd < 0 ) 
  40. return -1; 
  41.  
  42. while ( done ) { 
  43. printf(”begin handel_event0…\n”); 
  44. done = handle_event0(); 
  45. printf(”end handel_event0…\n”); 
  46.  
  47. if ( event0_fd > 0 ) { 
  48. close(event0_fd); 
  49. event0_fd = -1; 
  50.  
  51. return 0; 

用交叉编译器编译好后(编译过程就不再详述,请参见 blog:Android原生(Native)C开发之一:环境搭建篇),push至 emulator后执行后,切换到android 模拟器,在模拟器上点几下mouse,程序就会打出你点击的信息,效果如下,果然能正确得到点击的 mouse pos,如下图:


Android Native 鼠标事件截图2

分析上面的返回值,发现当按下 mouse left button 时,会得到4个事件,2个 type = 3 的事件返回了 pos x, pos y 的值,即mouse click pos, 另外1个 type = 1 的事件是按键事件(keydown),value就是按下的键的key,为0的应该就是 key的release事件,当松开 mouse时,也会得到两个 type = 1, 0 的事件,没有仔细去看它们的返回值,反正已经正确得到了 mosue的事件,下一步就是改SDL的事件驱动源码了…

参考链接: USB Mouse and Touch Screen (TS) Input(EN)[http://www.webkinesia.com/games/embedded.php]

文章转自:http://emck.avaw.com/?p=212

(emck)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201104/11308.html]
本文出处:emck.avaw.com 作者:emck
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容