2011年10月6日星期四

Control mouse movement in linux


Related topic from my stackoverflow:
I used to write /dev/psaux to directly change the mouse movement. But it failed.
Xlib is also a good idea when not working with OpenCV. It seems some OpenCV stuff, maybe highgui, does not go well with Xlib.
So I try to write
 /dev/input/mouse* 
OR 
/dev/input/event*
By using
cat /proc/bus/input/devices
I get this:


I: Bus=0003 Vendor=0461 Product=4d81 Version=0111N: Name="USB Optical Mouse" P: Phys=usb-0000:00:1d.0-1/input0S: Sysfs=/devices/pci0000:00/0000:00:1d.0/usb6/6-1/6-1:1.0/input/input10U: Uniq= H: Handlers=mouse2 event10B: EV=17B: KEY=70000 0 0 0 0 0 0 0 0B: REL=143B: MSC=10
After testing, only
/dev/input/event10
works. The code is as following:


#include <stdio.h>
#include <unistd.h>
#include <linux/input.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>

int main(){
  struct input_event event, event_end;
  int fd = open("/dev/input/event10", O_RDWR);
  if(!fd){
    printf("Errro open mouse:%s\n", strerror(errno));
    return -1;
  }
  memset(&event, 0, sizeof(event));
  gettimeofday(&event.time, NULL);
  event.type = EV_REL;
  event.code = REL_X;
  event.value = 100;
  event_end.type = EV_SYN;
  event_end.code = SYN_REPORT;
  event_end.value = 0;
  for(int i=0; i<5; i++){
    write(fd, &event, sizeof(event));// Move the mouse
    write(fd, &event_end, sizeof(event_end));// Show move
    sleep(1);// wait
  }
  close(fd);
  return 0;
}

没有评论:

发表评论