Last active
July 2, 2021 12:22
-
-
Save Hermann-SW/3d7afdfd435758d24904670e130c20db to your computer and use it in GitHub Desktop.
Show and parse all mouse/trackball related events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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):
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:
Left button press+release:
Wheel can be pressed as well:
Wheel can move up+down as well: