/* Copyright (C) 2004,2005 David Antliff <dave.antliff@paradise.net.nz>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 *
 *
 * This program allows you to block the mouse into one monitor
 * in a dual head configuration.
 *
 * Compile with:
 *  gcc -g Jail.c -o Jail -L /usr/X11R6/lib -lX11 -lXtst -lXext
 * 
 * This program was heavily based on switchscreen, by David Antliff.
 * Created by Sebastien Marion <seb.marion@gmail.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <X11/X.h>
#include <X11/Xutil.h>
#include <X11/extensions/XTest.h>



/******************************************************************************
FUNCTION    : main
DESCRIPTION :
******************************************************************************/
int main(int argc, char ** argv)
{
    Display * display;
    int majorOpcode, firstEvent, firstError;
	char * displayName;
    int thisScreen;
    int otherScreen = -1;

    displayName = getenv("DISPLAY");
    if (displayName == NULL)
    {
        fprintf(stderr, "DISPLAY is not set\n");
        exit(1);
    }

    fprintf(stderr, "Opening display %s\n", displayName);
    display = XOpenDisplay(displayName);


    if (!XQueryExtension(display,
                         XTestExtensionName,
                         &majorOpcode,
                         &firstEvent,
                         &firstError))
    {
        fprintf(stderr, "XTEST extension not available\n");
        return (1);
    }


    thisScreen = DefaultScreen(display);
    if (otherScreen == -1)
    {
        otherScreen = 1 - DefaultScreen(display);
    }

    if (otherScreen != thisScreen)
    {
 
        int root_x, root_y;
        int previous_x, previous_y;
        int win_x, win_y;
        unsigned int mask;
        Window root, child, OriginalRoot;
//        Window focus = 0;
//        int revert_to = 0;

//		fprintf(stderr, "Window ID is %x, focus is %x\n", (int)focus, revert_to);
		
		int this_width  = DisplayWidth (display, thisScreen);
		int this_height = DisplayHeight(display, thisScreen);
		int other_width  = DisplayWidth (display, otherScreen);
		int other_height = DisplayHeight(display, otherScreen);
		
//        fprintf(stderr, "width: %d, height: %d", width, height);
		int modif = 0;
        XQueryPointer(display,
                      RootWindow(display, thisScreen),
                      &root,
                      &child,
                      &root_x,
                      &root_y,
                      &win_x,
                      &win_y,
                      &mask);
	
  		OriginalRoot = root;
	    int modCount = 0;

		while (1){
	        // get current mouse coordinates
	        XQueryPointer(display,
	                      RootWindow(display, thisScreen),
	                      &root,
	                      &child,
	                      &root_x,
	                      &root_y,
	                      &win_x,
	                      &win_y,
	                      &mask);
           
           //fprintf(stderr,"display: %d, thisScreen: %d, root: %d, root_x: %d, root_y: %d\n",display,thisScreen,root, root_x,root_y);               
		   if (OriginalRoot != root)
           {
		   		modif=1;
				if (modCount > 4)
				{
					fprintf(stderr, "Failed to set cursor position\n");
					exit(1);
				}
				modCount++;
		   }
		   else {
				if (previous_x != root_x)
				{
					previous_x = root_x;
				}
				if (previous_y != root_y)
				{
					previous_y = root_y;
				}
				
				modCount = 0;
		   }
	       
	       if (modif == 1){
				XWarpPointer(display, root, OriginalRoot, root_x, root_y, other_width, other_height, previous_x, previous_y);
		       	fprintf(stderr,"MODIFYING COORDINATES to screen:%d, root_x=%d, root_y=%d\n",thisScreen,root_x,root_y);               
	       }
	       //sleeps for 1ms	       		
	       usleep(1000);
	       modif=0;
		}
	
    }
    XCloseDisplay(display);

    return (0);
}

