Search code examples
linuxmouseeventinput-deviceswaylandevdev

Simulate mouse motion on linux wayland


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.


Solution

  • 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);