8

I receive xy data from my network and i would like to control the mouse position using linux on wayland.

I've seen many source codes using X libs or X apps but it will not work on wayland. I also have a look on libinput and evedev but i don't find any code sample about how to create/simulate mouse.

1
  • I haven't tested the solution yet, but thank you for post, I ran into the same problem in our jUnit tests.. Commented May 4, 2017 at 16:16

1 Answer 1

7

Uinput is the answer.

void initMouse(){
  fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
  ioctl(fd, UI_SET_EVBIT, EV_KEY);
  ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
  ioctl(fd, UI_SET_EVBIT, EV_ABS);
  ioctl(fd, UI_SET_ABSBIT, ABS_X);
  ioctl(fd, UI_SET_ABSBIT, ABS_Y);

  struct uinput_user_dev uidev;
  memset(&uidev,0,sizeof(uidev));
  snprintf(uidev.name,UINPUT_MAX_NAME_SIZE,"HolusionMouse");
  uidev.id.bustype = BUS_USB;
  uidev.id.version = 1;
  uidev.id.vendor = 0x1;
  uidev.id.product = 0x1;
  uidev.absmin[ABS_X] = 0;
  uidev.absmax[ABS_X] = 1080;
  uidev.absmin[ABS_Y] = 0;
  uidev.absmax[ABS_Y] = 1080;
  write(fd, &uidev, sizeof(uidev));
  ioctl(fd, UI_DEV_CREATE);

  usleep(100000);
}

And update:

struct input_event ev[2], evS;
 memset(ev, 0, sizeof(ev ));
 ev[0].type = EV_ABS;
 ev[0].code = ABS_X;
 ev[0].value = 100;
 ev[1].type = EV_ABS;
 ev[1].code = ABS_Y;
 ev[1].value = 100;
 write(fd, ev, sizeof(ev));

 memset(&evS,0,sizeof(struct input_event));
 evS.type = EV_SYN;
 write(fd, &evS, sizeof(struct input_event));

 usleep(10000);
Sign up to request clarification or add additional context in comments.

2 Comments

Any idea how to perform such task without the necessity of using external libraries/tools?
@Jewenile I don't think so. UInput has the avantage to work for X and wayland.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.