Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Last active July 2, 2021 12:22
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/3d7afdfd435758d24904670e130c20db to your computer and use it in GitHub Desktop.
Save Hermann-SW/3d7afdfd435758d24904670e130c20db to your computer and use it in GitHub Desktop.
Show and parse all mouse/trackball related events
/* gcc -Wall -Wextra mstr_events.c -o mstr_events
/dev/input/eventX (X=0 mouse, X=3 trackball)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <linux/input.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd;
struct input_event ie;
assert(argc==2);
assert(strncmp(argv[1], "/dev/input/event", 16)==0);
if((fd = open(argv[1], O_RDONLY)) == -1) {
perror("opening device");
exit(EXIT_FAILURE);
}
while(read(fd, &ie, sizeof(struct input_event))) {
printf("time%ld.%06ld\ttype %d\tcode %d\tvalue %d\n",
ie.time.tv_sec, ie.time.tv_usec, ie.type, ie.code, ie.value);
if (ie.type == EV_SYN)
{
if (ie.code == SYN_REPORT) { printf("syn report\n"); }
else { printf("syn (%d)\n", ie.code); }
}
else if (ie.type == EV_MSC)
{
if (ie.code == MSC_SCAN) { printf("msc scan\n"); }
else { printf("msc (%d)\n", ie.code); }
}
else if (ie.type == EV_REL)
{
if (ie.code==REL_X) { printf("relX\t %d\n", ie.value); }
else if (ie.code==REL_Y) { printf("relY\t %d\n", ie.value); }
else if (ie.code==REL_WHEEL)
{
printf("rel wheel\t");
assert(0!=read(fd, &ie, sizeof(struct input_event)));
assert(ie.type == EV_REL);
if (ie.value > 0) { printf("up\n"); }
else { printf("down\n"); }
}
else if (ie.code == REL_HWHEEL)
{
printf("rel wheel\t");
assert(0!=read(fd, &ie, sizeof(struct input_event)));
assert(ie.type==EV_REL);
if (ie.value > 0) { printf("right\n"); }
else { printf("left\n"); }
}
}
else if (ie.type == EV_KEY)
{
if (ie.code == BTN_LEFT) { printf("Left mouse button "); }
else if (ie.code == BTN_RIGHT) { printf("Right mouse button "); }
else if (ie.code == BTN_MIDDLE) { printf("Wheel/Middle "); }
if (ie.value == 0) { printf("released\n"); }
else if (ie.value == 1) { printf("pressed\n"); }
else { printf("??? key\n"); }
}
}
return 0;
}
@Hermann-SW
Copy link
Author

Hermann-SW commented Jul 2, 2021

compile instructions in 1st line.
Need to pass "/dev/input/eventX" as arg, on Pi400 mouse is X=0 and trackball is X=3.
All events are the same for both, beside REL_HWHEEL (moving wheel to left or right):

...
time1625227496.693346	type 2	code 6	value 1
rel wheel	right
time1625227496.693346	type 0	code 0	value 0
syn report
time1625227497.959491	type 2	code 6	value -1
rel wheel	left
time1625227497.959491	type 0	code 0	value 0
syn report
...

This is an event analysis tool, so always event type+code+value are reported.
Program deals with SYN and MSC events as far as they happen when only using mouse/trackball.

Relative moves:

time1625227683.617134	type 2	code 0	value 1
relX	 1
time1625227683.617134	type 0	code 0	value 0
syn report
time1625227683.683148	type 2	code 0	value -1
relX	 -1
ime1625227683.683148	type 0	code 0	value 0
syn report
time1625227776.298033	type 2	code 1	value -4
relY	 -4
time1625227776.298033	type 0	code 0	value 0
syn report
time1625227776.305962	type 2	code 1	value -1
relY	 -1
time1625227776.305962	type 0	code 0	value 0
syn report
time1625227780.116489	type 2	code 1	value 1
relY	 1
time1625227780.116489	type 0	code 0	value 0
syn report

Left button press+release:

time1625227866.824575	type 1	code 272	value 1
Left mouse button pressed
time1625227866.824575	type 0	code 0	value 0
syn report
time1625227866.946586	type 4	code 4	value 589825
msc scan
time1625227866.946586	type 1	code 272	value 0
Left mouse button released
time1625227866.946586	type 0	code 0	value 0
syn report

Wheel can be pressed as well:

time1625228046.321427	type 1	code 274	value 1
Wheel/Middle pressed
time1625228046.321427	type 0	code 0	value 0
syn report
time1625228046.465361	type 4	code 4	value 589827
msc scan
time1625228046.465361	type 1	code 274	value 0
Wheel/Middle released
time1625228046.465361	type 0	code 0	value 0
syn report

Wheel can move up+down as well:

time1625228116.629121	type 2	code 8	value 1
rel wheel	up
time1625228116.629121	type 0	code 0	value 0
syn report
time1625228117.663267	type 2	code 8	value -1
rel wheel	down
time1625228117.663267	type 0	code 0	value 0
syn report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment