Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created July 3, 2021 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hermann-SW/a3785309ca0b85887bd417270bc56e31 to your computer and use it in GitHub Desktop.
Save Hermann-SW/a3785309ca0b85887bd417270bc56e31 to your computer and use it in GitHub Desktop.
Make mouse and trackball connected to Pi400 work for laptop over netcat session
/* gcc -Wall -pedantic -Wextra mstr.c -o mstr
Works together with "Raspberry Pi400 as a USB HID Keyboard":
https://gist.github.com/Hermann-SW/19c7522220a4ec3c8d6ecc89de2b0a60
Raspberry mouse and trackball, both connected to Pi400, work on laptop!
On laptop:
$ nc -l 8888 | sudo ./mstr -nodelay
On Pi400:
( cat /dev/input/event0 & cat /dev/input/event3 & wait ) | nc laptopip 8888
*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <linux/uinput.h>
#include <assert.h>
int main(int argc, char *argv[])
{
struct uinput_setup usetup;
int nodelay = (argc==2) && !strcmp(argv[1], "-nodelay");
int 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_KEYBIT, BTN_MIDDLE);
ioctl(fd, UI_SET_KEYBIT, BTN_RIGHT);
ioctl(fd, UI_SET_EVBIT, EV_REL);
ioctl(fd, UI_SET_RELBIT, REL_X);
ioctl(fd, UI_SET_RELBIT, REL_Y);
ioctl(fd, UI_SET_RELBIT, REL_WHEEL);
ioctl(fd, UI_SET_RELBIT, REL_HWHEEL);
memset(&usetup, 0, sizeof(usetup));
usetup.id.bustype = BUS_USB;
usetup.id.vendor = 0x1234; /* sample vendor */
usetup.id.product = 0x5678; /* sample product */
strcpy(usetup.name, "mouse replay");
ioctl(fd, UI_DEV_SETUP, &usetup);
ioctl(fd, UI_DEV_CREATE);
sleep(1);
const int pi_ie_sz = 16;
const int ie_sz = sizeof(struct input_event);
char buf[ie_sz];
void *q = buf + ie_sz - pi_ie_sz;
while (!feof(stdin))
{
if (fread(q, pi_ie_sz, 1, stdin) == 0) break;
bzero(buf, ie_sz - 8);
assert(write(fd, buf, ie_sz) == ie_sz);
if (!nodelay) usleep(15000);
}
sleep(1);
ioctl(fd, UI_DEV_DESTROY);
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment