For a Rube Goldberg device I needed a way to know if a mouse button was pressed on the root window of an X11 screen. Here is the code for it:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main() {
Display *dsp = XOpenDisplay( NULL );
if( !dsp ){ return 1; }
int screenNumber = DefaultScreen(dsp);
long eventMask = StructureNotifyMask;
XSelectInput( dsp, DefaultRootWindow(dsp), eventMask );
XEvent evt;
eventMask = ButtonPressMask|ButtonReleaseMask;
XSelectInput(dsp, DefaultRootWindow(dsp), eventMask); // override prev
do{
XNextEvent( dsp, &evt ); // calls XFlush()
if ( (evt.type == ButtonRelease) || (evt.type == ButtonPress) )
exit(2);
} while( 1 );
return 0;
}
Compile: gcc -lX11 -L/usr/local/include x11.c -o x11-mousebutton
Upon a mouse button being pressed it will exit 2, so using that return value in some script you can execute a command. For example, to open a CD rom drive so a domino falls over.