Skip to content

Instantly share code, notes, and snippets.

@yasar11732
Created January 29, 2013 21:36
Show Gist options
  • Save yasar11732/4668130 to your computer and use it in GitHub Desktop.
Save yasar11732/4668130 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/extensions/XInput2.h>
#include <signal.h>
#include <unistd.h>
FILE *f;
int working = 1;
/* This functions recieves XI_RawButtonPress event */
void handle_raw_event(XIRawEvent *ev) {
if(ev->detail == 1) {/* I am only interested in left button clicks */
fprintf(f,"%u\n",time(NULL));
fflush(f);
}
}
int main() {
Display *dpy = XOpenDisplay(NULL);
/* XInput Extension available? */
int opcode, event, error;
if (!XQueryExtension(dpy, "XInputExtension", &opcode, &event, &error)) {
printf("X Input extension not available.\n");
return -1;
}
/* Which version of XI2? We support 2.0 */
int major = 2, minor = 1;
if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
printf("XI2 not available. Server supports %d.%d\n", major, minor);
return -1;
}
f = fopen("/home/yasar/.mouselogs/log","a");
XIEventMask eventmask;
unsigned char mask[2] = { 0 }; /* the actual mask */
eventmask.deviceid = XIAllMasterDevices;
eventmask.mask_len = sizeof(mask); /* always in bytes */
eventmask.mask = mask;
/* now set the mask */
XISetMask(mask, XI_RawButtonPress);
/* select on the window */
XISelectEvents(dpy, DefaultRootWindow(dpy), &eventmask, 1);
while(working) {
XEvent ev;
usleep(10000);
while(XPending(dpy)) {
XNextEvent(dpy, &ev);
if (XGetEventData(dpy, &ev.xcookie))
{
switch(ev.xcookie.evtype)
{
case XI_RawButtonPress:
handle_raw_event(ev.xcookie.data);
break;
}
}
XFreeEventData(dpy, &ev.xcookie);
}
}
fflush(f);
fclose(f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment