récupérer l'état de la souris depuis le driver Linux [résolu]

récupérer l'état de la souris depuis le driver Linux [résolu] - C - Programmation

Marsh Posté le 23-08-2004 à 08:34:05    

Bonjour,  
pour compléter le titre, je dirais que j'ai tout d'abord fait mon affaire via le serveur de souris gpm ce qui a pour le moment bien fonctionné. Cependant, j'ai porté mon application sur une autre carte mère pc104 qui partage son port ps/2 entre clavier et souris, et la ca marche bcp moins bien: il faut absolument que je bouge la souris lorsque gpm se lance, faute de quoi  cette dernière n'est pas reconnue...ce n'est évidement pas souhaité.
Je me suis donc attaqué à la récupération de ces informations au niveau du driver Linux "/usr/src/linux/drivers/input/mousedev.c" dont je vous fais une copie  ici :  
 

Code :
  1. /*
  2. * $Id: mousedev.c,v 1.24 2000/11/15 10:57:45 vojtech Exp $
  3. *
  4. *  Copyright (c) 1999-2000 Vojtech Pavlik
  5. *
  6. *  Input driver to ImExPS/2 device driver module.
  7. *
  8. *  Sponsored by SuSE
  9. */
  10. /*
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or  
  14. * (at your option) any later version.
  15. *  
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. * GNU General Public License for more details.
  20. *  
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *  
  25. * Should you need to contact me, the author, you can do so either by
  26. * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
  27. * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
  28. */
  29. #define MOUSEDEV_MINOR_BASE  32
  30. #define MOUSEDEV_MINORS  32
  31. #define MOUSEDEV_MIX  31
  32. #include <linux/slab.h>
  33. #include <linux/poll.h>
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/input.h>
  37. #include <linux/config.h>
  38. #include <linux/smp_lock.h>
  39. #include <linux/random.h>
  40. #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
  41. #define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
  42. #endif
  43. #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
  44. #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
  45. #endif
  46. struct mousedev {
  47. int exist;
  48. int open;
  49. int minor;
  50. wait_queue_head_t wait;
  51. struct mousedev_list *list;
  52. struct input_handle handle;
  53. devfs_handle_t devfs;
  54. };
  55. struct mousedev_list {
  56. struct fasync_struct *fasync;
  57. struct mousedev *mousedev;
  58. struct mousedev_list *next;
  59. int dx, dy, dz, oldx, oldy;
  60. signed char ps2[6];
  61. unsigned long buttons;
  62. unsigned char ready, buffer, bufsiz;
  63. unsigned char mode, imexseq, impsseq;
  64. };
  65. #define MOUSEDEV_SEQ_LEN 6
  66. static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
  67. static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
  68. static struct input_handler mousedev_handler;
  69. static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
  70. static struct mousedev mousedev_mix;
  71. static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X;
  72. static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y;
  73. static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
  74. {
  75. struct mousedev *mousedevs[3] = { handle->private, &mousedev_mix, NULL };
  76. struct mousedev **mousedev = mousedevs;
  77. struct mousedev_list *list;
  78. int index, size;
  79. add_mouse_randomness((type << 4) ^ code ^ (code >> 4) ^ value);
  80. while (*mousedev) {
  81.  list = (*mousedev)->list;
  82.  while (list) {
  83.   switch (type) {
  84.    case EV_ABS:
  85.     if (test_bit(BTN_TRIGGER, handle->dev->keybit))
  86.      break;
  87.     switch (code) {
  88.      case ABS_X:
  89.       size = handle->dev->absmax[ABS_X] - handle->dev->absmin[ABS_X];
  90.       list->dx += (value * xres - list->oldx) / size;
  91.       list->oldx += list->dx * size;
  92.       break;
  93.      case ABS_Y:
  94.       size = handle->dev->absmax[ABS_Y] - handle->dev->absmin[ABS_Y];
  95.       list->dy -= (value * yres - list->oldy) / size;
  96.       list->oldy -= list->dy * size;
  97.       break;
  98.     }
  99.     break;
  100.    case EV_REL:
  101.     switch (code) {
  102.      case REL_X: list->dx += value; break;
  103.      case REL_Y: list->dy -= value; break;
  104.      case REL_WHEEL: if (list->mode) list->dz -= value; break;
  105.     }
  106.     break;
  107.    case EV_KEY:
  108.     switch (code) {
  109.      case BTN_0:
  110.      case BTN_TOUCH:
  111.      case BTN_LEFT:   index = 0; break;
  112.      case BTN_4:
  113.      case BTN_EXTRA:  if (list->mode == 2) { index = 4; break; }
  114.      case BTN_STYLUS:
  115.      case BTN_1:
  116.      case BTN_RIGHT:  index = 1; break;
  117.      case BTN_3:
  118.      case BTN_SIDE:   if (list->mode == 2) { index = 3; break; }
  119.      case BTN_2:
  120.      case BTN_STYLUS2:
  121.      case BTN_MIDDLE: index = 2; break;
  122.      default: return;
  123.     }
  124.     switch (value) {
  125.      case 0: clear_bit(index, &list->buttons); break;
  126.      case 1: set_bit(index, &list->buttons); break;
  127.      case 2: return;
  128.     }
  129.     break;
  130.   }
  131.   list->ready = 1;
  132.   kill_fasync(&list->fasync, SIGIO, POLL_IN);
  133.   list = list->next;
  134.  }
  135.  wake_up_interruptible(&((*mousedev)->wait));
  136.  mousedev++;
  137. }
  138. }
  139. static int mousedev_fasync(int fd, struct file *file, int on)
  140. {
  141. int retval;
  142. struct mousedev_list *list = file->private_data;
  143. retval = fasync_helper(fd, file, on, &list->fasync);
  144. return retval < 0 ? retval : 0;
  145. }
  146. static int mousedev_release(struct inode * inode, struct file * file)
  147. {
  148. struct mousedev_list *list = file->private_data;
  149. struct mousedev_list **listptr;
  150. lock_kernel();
  151. listptr = &list->mousedev->list;
  152. mousedev_fasync(-1, file, 0);
  153. while (*listptr && (*listptr != list))
  154.  listptr = &((*listptr)->next);
  155. *listptr = (*listptr)->next;
  156. if (!--list->mousedev->open) {
  157.  if (list->mousedev->minor == MOUSEDEV_MIX) {
  158.   struct input_handle *handle = mousedev_handler.handle;
  159.   while (handle) {
  160.    struct mousedev *mousedev = handle->private;
  161.    if (!mousedev->open) {
  162.     if (mousedev->exist) {
  163.      input_close_device(&mousedev->handle);
  164.     } else {
  165.      input_unregister_minor(mousedev->devfs);
  166.      mousedev_table[mousedev->minor] = NULL;
  167.      kfree(mousedev);
  168.     }
  169.    }
  170.    handle = handle->hnext;
  171.   }
  172.  } else {
  173.   if (!mousedev_mix.open) {
  174.    if (list->mousedev->exist) {
  175.     input_close_device(&list->mousedev->handle);
  176.    } else {
  177.     input_unregister_minor(list->mousedev->devfs);
  178.     mousedev_table[list->mousedev->minor] = NULL;
  179.     kfree(list->mousedev);
  180.    }
  181.   }
  182.  }
  183. }
  184. kfree(list);
  185. unlock_kernel();
  186. return 0;
  187. }
  188. static int mousedev_open(struct inode * inode, struct file * file)
  189. {
  190. struct mousedev_list *list;
  191. int i = MINOR(inode->i_rdev) - MOUSEDEV_MINOR_BASE;
  192. if (i >= MOUSEDEV_MINORS || !mousedev_table[i])
  193.  return -ENODEV;
  194. if (!(list = kmalloc(sizeof(struct mousedev_list), GFP_KERNEL)))
  195.  return -ENOMEM;
  196. memset(list, 0, sizeof(struct mousedev_list));
  197. list->mousedev = mousedev_table[i];
  198. list->next = mousedev_table[i]->list;
  199. mousedev_table[i]->list = list;
  200. file->private_data = list;
  201. if (!list->mousedev->open++) {
  202.  if (list->mousedev->minor == MOUSEDEV_MIX) {
  203.   struct input_handle *handle = mousedev_handler.handle;
  204.   while (handle) {
  205.    struct mousedev *mousedev = handle->private;
  206.    if (!mousedev->open)
  207.     if (mousedev->exist)
  208.      input_open_device(handle);
  209.    handle = handle->hnext;
  210.   }
  211.  } else {
  212.   if (!mousedev_mix.open)
  213.    if (list->mousedev->exist)
  214.     input_open_device(&list->mousedev->handle);
  215.  }
  216. }
  217. return 0;
  218. }
  219. static void mousedev_packet(struct mousedev_list *list, unsigned char off)
  220. {
  221. list->ps2[off] = 0x08 | ((list->dx < 0) << 4) | ((list->dy < 0) << 5) | (list->buttons & 0x07);
  222. list->ps2[off + 1] = (list->dx > 127 ? 127 : (list->dx < -127 ? -127 : list->dx));
  223. list->ps2[off + 2] = (list->dy > 127 ? 127 : (list->dy < -127 ? -127 : list->dy));
  224. list->dx -= list->ps2[off + 1];
  225. list->dy -= list->ps2[off + 2];
  226. list->bufsiz = off + 3;
  227. if (list->mode == 2) {
  228.  list->ps2[off + 3] = (list->dz > 7 ? 7 : (list->dz < -7 ? -7 : list->dz));
  229.  list->dz -= list->ps2[off + 3];
  230.  list->ps2[off + 3] = (list->ps2[off + 3] & 0x0f) | ((list->buttons & 0x18) << 1);
  231.  list->bufsiz++;
  232. }
  233. if (list->mode == 1) {
  234.  list->ps2[off + 3] = (list->dz > 127 ? 127 : (list->dz < -127 ? -127 : list->dz));
  235.  list->dz -= list->ps2[off + 3];
  236.  list->bufsiz++;
  237. }
  238. if (!list->dx && !list->dy && (!list->mode || !list->dz)) list->ready = 0;
  239. list->buffer = list->bufsiz;
  240. }
  241. static ssize_t mousedev_write(struct file * file, const char * buffer, size_t count, loff_t *ppos)
  242. {
  243. struct mousedev_list *list = file->private_data;
  244. unsigned char c;
  245. int i;
  246. for (i = 0; i < count; i++) {
  247.  if (get_user(c, buffer + i))
  248.   return -EFAULT;
  249.  if (c == mousedev_imex_seq[list->imexseq]) {
  250.   if (++list->imexseq == MOUSEDEV_SEQ_LEN) {
  251.    list->imexseq = 0;
  252.    list->mode = 2;
  253.   }
  254.  } else list->imexseq = 0;
  255.  if (c == mousedev_imps_seq[list->impsseq]) {
  256.   if (++list->impsseq == MOUSEDEV_SEQ_LEN) {
  257.    list->impsseq = 0;
  258.    list->mode = 1;
  259.   }
  260.  } else list->impsseq = 0;
  261.  list->ps2[0] = 0xfa;
  262.  list->bufsiz = 1;
  263.  list->ready = 1;
  264.  switch (c) {
  265.   case 0xeb: /* Poll */
  266.    mousedev_packet(list, 1);
  267.    break;
  268.   case 0xf2: /* Get ID */
  269.    switch (list->mode) {
  270.     case 0: list->ps2[1] = 0; break;
  271.     case 1: list->ps2[1] = 3; break;
  272.     case 2: list->ps2[1] = 4; break;
  273.    }
  274.    list->bufsiz = 2;
  275.    break;
  276.   case 0xe9: /* Get info */
  277.    list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200;
  278.    list->bufsiz = 4;
  279.    break;
  280.  }
  281.  list->buffer = list->bufsiz;
  282. }
  283. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  284. wake_up_interruptible(&list->mousedev->wait);
  285. return count;
  286. }
  287. static ssize_t mousedev_read(struct file * file, char * buffer, size_t count, loff_t *ppos)
  288. {
  289. DECLARE_WAITQUEUE(wait, current);
  290. struct mousedev_list *list = file->private_data;
  291. int retval = 0;
  292. if (!list->ready && !list->buffer) {
  293.  add_wait_queue(&list->mousedev->wait, &wait);
  294.  current->state = TASK_INTERRUPTIBLE;
  295.  while (!list->ready) {
  296.   if (file->f_flags & O_NONBLOCK) {
  297.    retval = -EAGAIN;
  298.    break;
  299.   }
  300.   if (signal_pending(current)) {
  301.    retval = -ERESTARTSYS;
  302.    break;
  303.   }
  304.   schedule();
  305.  }
  306.  current->state = TASK_RUNNING;
  307.  remove_wait_queue(&list->mousedev->wait, &wait);
  308. }
  309. if (retval)
  310.  return retval;
  311. if (!list->buffer)
  312.  mousedev_packet(list, 0);
  313. if (count > list->buffer)
  314.  count = list->buffer;
  315. if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer, count))
  316.  return -EFAULT;
  317. list->buffer -= count;
  318. return count;
  319. }
  320. /* No kernel lock - fine */
  321. static unsigned int mousedev_poll(struct file *file, poll_table *wait)
  322. {
  323. struct mousedev_list *list = file->private_data;
  324. poll_wait(file, &list->mousedev->wait, wait);
  325. if (list->ready || list->buffer)
  326.  return POLLIN | POLLRDNORM;
  327. return 0;
  328. }
  329. struct file_operations mousedev_fops = {
  330. owner:  THIS_MODULE,
  331. read:  mousedev_read,
  332. write:  mousedev_write,
  333. poll:  mousedev_poll,
  334. open:  mousedev_open,
  335. release: mousedev_release,
  336. fasync:  mousedev_fasync,
  337. };
  338. static struct input_handle *mousedev_connect(struct input_handler *handler, struct input_dev *dev)
  339. {
  340. struct mousedev *mousedev;
  341. int minor = 0;
  342. if (!test_bit(EV_KEY, dev->evbit) ||
  343.     (!test_bit(BTN_LEFT, dev->keybit) &&
  344.      !test_bit(BTN_MIDDLE, dev->keybit) &&
  345.      !test_bit(BTN_TOUCH, dev->keybit)))
  346.  return NULL;
  347. if ((!test_bit(EV_REL, dev->evbit) || !test_bit(REL_X, dev->relbit)) &&
  348.     (!test_bit(EV_REL, dev->evbit) || !test_bit(REL_WHEEL, dev->relbit)) &&
  349.     (!test_bit(EV_ABS, dev->evbit) || !test_bit(ABS_X, dev->absbit)))
  350.  return NULL;
  351. for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
  352. if (minor == MOUSEDEV_MINORS) {
  353.  printk(KERN_ERR "mousedev: no more free mousedev devices\n" );
  354.  return NULL;
  355. }
  356. if (!(mousedev = kmalloc(sizeof(struct mousedev), GFP_KERNEL)))
  357.  return NULL;
  358. memset(mousedev, 0, sizeof(struct mousedev));
  359. init_waitqueue_head(&mousedev->wait);
  360. mousedev->exist = 1;
  361. mousedev->minor = minor;
  362. mousedev_table[minor] = mousedev;
  363. mousedev->handle.dev = dev;
  364. mousedev->handle.handler = handler;
  365. mousedev->handle.private = mousedev;
  366. mousedev->devfs = input_register_minor("mouse%d", minor, MOUSEDEV_MINOR_BASE);
  367. if (mousedev_mix.open)
  368.  input_open_device(&mousedev->handle);
  369. // printk(KERN_INFO "mouse%d: PS/2 mouse device for input%d\n", minor, dev->number);
  370. return &mousedev->handle;
  371. }
  372. static void mousedev_disconnect(struct input_handle *handle)
  373. {
  374. struct mousedev *mousedev = handle->private;
  375. mousedev->exist = 0;
  376. if (mousedev->open) {
  377.  input_close_device(handle);
  378. } else {
  379.  if (mousedev_mix.open)
  380.   input_close_device(handle);
  381.  input_unregister_minor(mousedev->devfs);
  382.  mousedev_table[mousedev->minor] = NULL;
  383.  kfree(mousedev);
  384. }
  385. }
  386. static struct input_handler mousedev_handler = {
  387. event:  mousedev_event,
  388. connect: mousedev_connect,
  389. disconnect: mousedev_disconnect,
  390. fops:  &mousedev_fops,
  391. minor:  MOUSEDEV_MINOR_BASE,
  392. };
  393. static int __init mousedev_init(void)
  394. {
  395. input_register_handler(&mousedev_handler);
  396. memset(&mousedev_mix, 0, sizeof(struct mousedev));
  397. init_waitqueue_head(&mousedev_mix.wait);
  398. mousedev_table[MOUSEDEV_MIX] = &mousedev_mix;
  399. mousedev_mix.exist = 1;
  400. mousedev_mix.minor = MOUSEDEV_MIX;
  401. mousedev_mix.devfs = input_register_minor("mice", MOUSEDEV_MIX, MOUSEDEV_MINOR_BASE);
  402. printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n" );
  403. return 0;
  404. }
  405. static void __exit mousedev_exit(void)
  406. {
  407. input_unregister_minor(mousedev_mix.devfs);
  408. input_unregister_handler(&mousedev_handler);
  409. }
  410. module_init(mousedev_init);
  411. module_exit(mousedev_exit);
  412. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>" );
  413. MODULE_DESCRIPTION("Input driver to PS/2 or ImPS/2 device driver" );
  414. MODULE_LICENSE("GPL" );
  415. MODULE_PARM(xres, "i" );
  416. MODULE_PARM_DESC(xres, "Horizontal screen resolution" );
  417. MODULE_PARM(yres, "i" );
  418. MODULE_PARM_DESC(yres, "Vertical screen resolution" );


 
En me basant la dessus j'arrive à récupérer les boutons sans trop de problème via le code suivant:

Code :
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <sys/time.h>
  5. #include <sys/ioctl.h>
  6. #include <asm/types.h>
  7. #include <errno.h>
  8. #include <linux/input.h>
  9. /*
  10. * The event structure itself
  11. */
  12. struct input_event {
  13.   struct timeval time;
  14.   __u16 type;
  15.   __u16 code;
  16.   __s32 value;
  17. };
  18. struct input_event buffer;
  19. #define uc unsigned char
  20. unsigned short last_down_right=0;
  21. unsigned short last_down_left=0;
  22. int main()
  23. {
  24.   int fd;
  25.   size_t len;
  26.   fd = open("/dev/psaux",O_RDONLY);
  27.   if (fd < 0)
  28.     {
  29.       printf ("fd = %d\nerrno = %d\n", fd, errno);
  30.       return -1;
  31.     }
  32.   while(1)
  33.     {
  34.       uc c;
  35.       len = read(fd, &c, 1);
  36.       if (len>0)
  37. {
  38.   int i;
  39.   if ((c&0x08) == 0x08)
  40.     {
  41.       //printf("Debut de message\n" );
  42.       if ((c&0x09) == 0x09 )
  43.  {
  44.   printf("Bouton 1 enfonce\n" );
  45.   last_down_left=1;
  46.  }
  47.       else if (last_down_left==1)
  48.    {
  49.     printf("bouton 1 relache\n" );
  50.     last_down_left=0;
  51.  }
  52.       if ((c&0x0a) == 0x0a )
  53.  {
  54.   printf("Bouton 2 enfonce\n" );
  55.   last_down_right=1;
  56.  }
  57.       else if (last_down_right==1)
  58.       {
  59.        printf("bouton 2 relache\n" );
  60.        last_down_right=0;
  61.  }
  62.       len = read(fd, &c, 1);
  63.       len = read(fd, &c, 1);
  64.     }
  65.   else
  66.     {
  67.       printf("Message errone\n" );
  68.     }
  69. }
  70.       //usleep(100000);
  71.     }
  72.   return 0;
  73. }


Il me manque juste à récupérer les mouvements suivant dx et dy.
D'après ce que j'ai compris au niveau du driver, ce dernier envoie une trame comprtant un tableau de 6 signed char, et en mode poll( mode normal je pense)on a buffer[1] qui contient un évènement souris 0x08, le bouton gauche cliqué 0x01, le bouton droit cliqué 0x02, ou encore un movement de souris 0x10.
buffer[2] permet de récupérer la valeur du déplacement sur dx et buffer[3] celle du déplacement sur dy.
PB, le code source suivant ne fonctionne pas :

Code :
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <sys/time.h>
  5. #include <sys/ioctl.h>
  6. #include <asm/types.h>
  7. #include <errno.h>
  8. #include <linux/input.h>
  9. struct input_event buffer;
  10. unsigned char mbuf[6];
  11. unsigned short last_down_right=0;
  12. unsigned short last_down_left=0;
  13. int main()
  14. {
  15.   int fd;
  16.   int evt=0;
  17.   int evt_bouton=0;
  18.   int evt_deplacement_haut=0,evt_deplacement_bas=0;
  19.   int evt_deplacement_droite=0,evt_deplacement_gauche=0;
  20.   size_t len;
  21.   fd = open("/dev/psaux",O_RDONLY);
  22.   if (fd < 0)
  23.     {
  24.       printf ("fd = %d\nerrno = %d\n", fd, errno);
  25.       return -1;
  26.     }
  27.   while(1)
  28.     {
  29. usleep(150000);
  30.            len = read(fd, mbuf, sizeof mbuf);
  31. if (!len)
  32. {
  33.  printf("rien en lecture\n" );
  34. }
  35. else
  36. {
  37. printf("\n\n0:%x\n1:%x\n2:%x\n3:%x\n\n",mbuf[0],mbuf[1],mbuf[2],mbuf[3]);
  38. if((mbuf[1]&0x08)==0x08)
  39. {
  40.  evt=1;
  41. }
  42. else evt=0;
  43. /*si evt et que c'est un evt bouton*/
  44. if(evt==1 && ((mbuf[1]&0x01==0x01) || (mbuf[1]&0x02==0x02)))
  45. {
  46.  evt_bouton=1;
  47. //  printf("\n\t evt bouton\n" );
  48. }
  49. else evt_bouton=0;
  50. if(evt==1 && (mbuf[2]&0xff==0x1))
  51. {
  52.  evt_deplacement_haut=1;
  53.  printf("\n\t\thaut\n" );
  54. }
  55. else evt_deplacement_haut=0;
  56. if(evt==1 &&(mbuf[2]&0xff>0x1))
  57. {
  58.  evt_deplacement_bas=1;
  59.  printf("\n\tbas\n" );
  60. }
  61. else evt_deplacement_bas=0;
  62. if(evt==1 &&(mbuf[3]&0xff==0x1))
  63. {
  64.  evt_deplacement_droite=1;
  65.  printf("\n\tdroite\n" );
  66. }
  67. else evt_deplacement_droite=0;
  68. if(evt==1 &&(mbuf[3]&0xff>0x1))
  69. {
  70.  evt_deplacement_gauche=1;
  71.  printf("\n\tgauche\n" );
  72. }
  73. else evt_deplacement_gauche=0;
  74. if(evt_bouton)
  75. {
  76.   if ((mbuf[1]&0x01) == 0x01 )
  77.   {
  78.    printf("Bouton 1 enfonce\n" );
  79.    last_down_left=1;
  80.   }
  81.        else if (last_down_left==1)
  82.   {
  83.      printf("bouton 1 relache\n" );
  84.      last_down_left=0;
  85.   }
  86.        if ((mbuf[1]&0x02) == 0x02 )
  87.   {
  88.    printf("Bouton 2 enfonce\n" );
  89.    last_down_right=1;
  90.   }
  91.        else if (last_down_right==1)
  92.         {
  93.         printf("bouton 2 relache\n" );
  94.         last_down_right=0;
  95.   }
  96. }
  97.   }//else
  98.   }//while
  99.   return 0;
  100. }


si qqun a eu le courage de lire jusqu'au bout et peut m'aider, il est le bien venu.
 :hello:


Message édité par psebcopathe le 24-08-2004 à 09:50:38
Reply

Marsh Posté le 23-08-2004 à 08:34:05   

Reply

Marsh Posté le 24-08-2004 à 09:18:00    

Bon ben en fait j'ai trouvé.
Le driver n'envoie pas tout le temps 6 octets.
En effet, lorsqu'il n'y a pas de déplacement, seulement un évènement bouton il n'envoie qu'un seul octect en position buffer[0].
Si il y a un déplacement, il en envoie 3 buffer[0],buffer[1] et buffer[2].
Donc voilà, pour ceux que ca intéresse, celà fonctionne avec le code suivant :
 

Code :
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <sys/time.h>
  5. #include <sys/ioctl.h>
  6. #include <asm/types.h>
  7. #include <errno.h>
  8. #include <linux/input.h>
  9. struct input_event buffer;
  10. #define uc unsigned char
  11. unsigned short last_down_right=0;
  12. unsigned short last_down_left=0;
  13. int main()
  14. {
  15.   int fd;
  16.   size_t len;
  17.   char dx, dy, x=0, y=0;
  18.   fd = open("/dev/psaux",O_RDONLY);
  19.   if (fd < 0)
  20.     {
  21.       printf ("fd = %d\nerrno = %d\n", fd, errno);
  22.       return -1;
  23.     }
  24.   while(1)
  25.     {
  26.       uc c;
  27.       len = read(fd, &c, 1);
  28.       if (len>0)
  29. {
  30.   if ((c&0x08) == 0x08)
  31.     {
  32.       if ((c&0x09) == 0x09 )
  33.  {
  34.   printf("Bouton 1 enfonce\n" );
  35.   last_down_left=1;
  36.  }
  37.       else if (last_down_left==1)
  38.    {
  39.     printf("bouton 1 relache\n" );
  40.     last_down_left=0;
  41.  }
  42.       if ((c&0x0a) == 0x0a )
  43.  {
  44.   printf("Bouton 2 enfonce\n" );
  45.   last_down_right=1;
  46.  }
  47.       else if (last_down_right==1)
  48.       {
  49.        printf("bouton 2 relache\n" );
  50.        last_down_right=0;
  51.  }
  52.       len = read(fd, &c, 1);
  53.       if (len == 1)
  54.  {
  55.    memcpy(&dx, &c, 1);
  56.    printf("mouvement sur x: %d\n", dx);
  57.  }
  58.       len = read(fd, &c, 1);
  59.       if (len == 1)
  60.  {
  61.    memcpy(&dy, &c, 1);
  62.    printf("mouvement sur y: %d\n", dy);
  63.  }   
  64.     }
  65.   else
  66.     {
  67.       printf("Message errone\n" );
  68.     }
  69. }
  70.     }
  71.   return 0;
  72. }


 :jap:

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed