The program below monitors mouse clicks and location. It recognizes when a user is swiping from the very left of the screen towards the center, and it's meant to be used on a touch device—in my case, a raspberry pi touch screen.

So if a user is running any program, they could just flick the side of the screen, and a program could be run, such as a command to bring the user to the desktop or pop up a menu.

The part that I need help with is to freeze the mouse's input from being used by a program (whatever program you happen to be running at the time) when the gesture is being done, so the gesture won't interfere with that program. After the gesture is completed, the mouse should be unfrozen and given back to application.

I've been trying various methods, but it seems that xlib doesn't want to be able to block input during a click.

mouse.c (must be run as root as it captures mouse events of other programs, which would be a security risk):
Code:
#include <linux/input.h>
#include <X11/Xlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>

/*

Compile:
gcc mouse.c -o mouse -lX11 && ./mouse

To get mouse device name:
cat /proc/bus/input/devices | grep mouse

*/

#define MOUSEFILE "/dev/input/event11"


int main() {

   int fd, init_clicked = false, pressing = false,
           released = false, gestureSuccessful = false;
   struct input_event ie;
   Display *dpy;
   Window root, child;
   int rootX, rootY, winX, winY;
   unsigned int mask;

   dpy = XOpenDisplay(NULL);
   XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,
              &rootX,&rootY,&winX,&winY,&mask);

   if((fd = open(MOUSEFILE, O_RDONLY)) == -1) {
      perror("opening device");
      exit(EXIT_FAILURE);
   }


   while(read(fd, &ie, sizeof(struct input_event))) {

      if (ie.type == 2) {

         if (ie.code == 0) {

            XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,
                        &rootX,&rootY,&winX,&winY,&mask);

          //rootX += ie.value;

         } else if (ie.code == 1) {
            XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,
                        &rootX,&rootY,&winX,&winY,&mask);

         // rootY += ie.value;

         }

      } else {

         //ie.time.tv_sec, ie.time.tv_usec, ie.type, ie.code, ie.value);

         if (ie.type == 1 && ie.code == 272 && ie.value == 1 &&
              pressing == false && init_clicked == false && rootX <  10) {

            init_clicked = true;

         }else if (ie.type == 1 && ie.code == 272 && ie.value == 0 &&
                     pressing == true) {
           
            released = true;

            if (rootX > 100)
               gestureSuccessful = true;
         }
      }


      if (init_clicked == true) {
         
         //XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask,
         //   GrabModeSync, GrabModeAsync, root, None, CurrentTime);

         // reset values so they only do an action once
         init_clicked = false;
         pressing = true;         

         printf("\nInitially clicked.\n");

      } else if (pressing == true && released == false) {

         //printf("Pressing.\n");

      } else if (released == true) {

         printf("Released.\n\n");

         if (gestureSuccessful == true)
            printf("Gesture successful.\n\n");

         pressing = false;
         released = false;
         gestureSuccessful = false;

         //XUngrabPointer(dpy, CurrentTime);

      }

      //printf("type %d\tcode %d\tvalue %d\n", ie.type, ie.code, ie.value);

   }

   return 0;
}
How can I mask the input from the program when the gesture is being done?

Any ideas?