I figured it out:
#include <stdio.h>
#include <stdlib.h>
main(){
FILE *fmouse;
char b[3];
fmouse = fopen("/dev/input/mice","r");
int xd=0,yd=0; //x/y movement delta
int xo=0,yo=0; //x/y overflow (out of range -255 to +255)
int lb=0,mb=0,rb=0; //left/middle/right mousebutton
while(!mb){
fread(b,sizeof(char),3,fmouse);
lb=(b[0]&1)>0;
rb=(b[0]&2)>0;
mb=(b[0]&4)>0;
xo=(b[0]&64)>0;
yo=(b[0]&128)>0;
xd=b[1];
yd=b[2];
printf("lb=%d rm=%d mb=%d xo=%d yo=%d xd=%d yd=%d\n",lb,rb,mb,xo,yo,xd,yd);
}
fclose(fmouse);
}
PS: you have to call it with sudo or else you just get a segfault (at least I did).