Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control mouse movement in linux?

Tags:

c

linux

input

mouse

I try to control the mouse in Linux. Xlib seems to works, but when I try to use it with OpenCV, it keeps returning:

Resource temporarily unavailable

So I decide to write "/dev/psaux". The code is as following:

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main() {
    unsigned char a[5]={0, 0xff, 0, 0x28, 0xff};
    int fp = open ("/dev/psaux", O_WRONLY);
    if(!fp)printf("open error:%s\n", strerror(errno));
    for(int i = 0; i < 10; i++)
        printf("write:%d\t\t%s\n", write(fp, a, 5), strerror(errno));
    close(fp);
    return 0;
}

Compile it with:

gcc  my_psaux.c -o my_psaux -std=gnu99 -g

Run and get

$sudo ./my_psaux 
write:5    Success
write:5    Success
write:5    Success
write:5    Success
write:5    Success
write:5    Success
write:5    Success
write:5    Success
write:5    Success
write:5    Success

However the mouse doesn't move. Then I open a new terminal, type in "sudo cat /dev/psaux" and run "my_psaux". But I just cat nothing. Nothing is written into "/dev/psaux" ?

Could anyone help me?

If this is not a good method to control the mouse, could anyone tell me another one?

like image 957
thundertrick Avatar asked Oct 06 '11 13:10

thundertrick


People also ask

How do I Auto move my mouse in Linux?

# of the screen then change mousemove_relative to mousemove in the xdotool command below. # Set LENGTH to 0 if you do not want the mouse pointer to actually move. # Set LENGTH to 1 if you want the mouse pointer to move just a tiny fraction. # Set LENGTH to e.g. 100 if you want to see more easily the mouse pointer move.

How do I move the mouse in Ubuntu?

Click Accessibility to open the panel. Use the up and down arrow keys to select Mouse Keys in the Pointing & Clicking section, then press Enter to switch the Mouse Keys switch to on. Make sure that Num Lock is turned off. You will now be able to move the mouse pointer using the keypad.

Can you use a mouse in Linux?

To enable mouse support in Text-only Linux systems, install GPM package. It is available in the default repositories of most Linux distributions. In Debian-based systems, gpm service will be automatically started after you installed it, so you need not to manually start the service as shown above.

How do I make my mouse pointer automatically move?

To do so, open Control Panel > Mouse Properties > Pointer Options. Check the Automatically move the pointer to the default button in a dialog box. Click Apply > OK.


1 Answers

Great thanks to @R.. for reminding me of some other ways instead of /dev/psaux

So I tried /dev/input/mouse* and /dev/input/event*

By using

cat /proc/bus/input/devices 

I get this:

I: Bus=0003 Vendor=0461 Product=4d81 Version=0111
N: Name="USB Optical Mouse"
P: Phys=usb-0000:00:1d.0-1/input0
S: Sysfs=/devices/pci0000:00/0000:00:1d.0/usb6/6-1/6-1:1.0/input/input10
U: Uniq=
H: Handlers=mouse2 event10 
B: EV=17
B: KEY=70000 0 0 0 0 0 0 0 0
B: REL=143
B: 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 < 0) {
    printf("Errro open mouse:%s\n", strerror(errno));
    return -1;
  }
  memset(&event, 0, sizeof(event));
  memset(&event, 0, sizeof(event_end));
  gettimeofday(&event.time, NULL);
  event.type = EV_REL;
  event.code = REL_X;
  event.value = 100;
  gettimeofday(&event_end.time, NULL);
  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;
}
like image 196
thundertrick Avatar answered Sep 30 '22 03:09

thundertrick