/* 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.
 */

/*
 *
 * Modified some time in 2006 by Anton Berezin <tobez@tobez.org>
 * to support "switch to screen number N" functionality.
 *
 */

// Version 0.2 20050128 - added support for Window Focus
// Version 0.1 20041014
//
// This program allows you to move the mouse cursor between non-xinerama screens
// in a dual head configuration.
//
/* Compile with:
   gcc -g dualmouse.c -o dualmouse -I/usr/X11R6/include -L/usr/X11R6/lib -lX11 -lXtst -lXext
*/
// Stores previous screen's pointer location in ~/.pointer-<screen>

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


#define REMEMBER_FILE ".pointer"


void save(int screen, int x, int y, Window focus)
{
    FILE * f;
    char acFile[100];
    char acData[100];
    char * home;

    home = getenv("HOME");
    snprintf(acFile, 100, "%s/%s-%d", home, REMEMBER_FILE, screen);
    f = fopen(acFile, "w");

    if (f != NULL)
    {
        fprintf(stderr, "Saving %s\n", acFile);
        snprintf(acData, 100, "%d %d %d\n", x, y, focus);

        if (fputs(acData, f) < 0)
        {
            perror("fputs");
        }

        fclose(f);
    }
    else
    {
        perror("fopen");
    }
}


void load(int screen, int * px, int * py, Window *pfocus)
{
    FILE * f;
    char acFile[100];
    char acData[100];
    char * home;
    int x = 0;
    int y = 0;
    int focus = 0;

    home = getenv("HOME");
    snprintf(acFile, 100, "%s/%s-%d", home, REMEMBER_FILE, screen);
    f = fopen(acFile, "r");

    if (f != NULL)
    {
        fprintf(stderr, "Loading %s\n", acFile);
        if (fgets(acData, 100, f) == NULL)
        {
            perror("fgets");
        }
        else
        {
            sscanf(acData, "%d %d %d\n", &x, &y, &focus);
        }

        fclose(f);
    }

    *px = x;
    *py = y;
    *pfocus = focus;
}


int main(int argc, char ** argv)
{
    Display * display;
    int majorOpcode, firstEvent, firstError;
	char * displayName;

    displayName = getenv("DISPLAY");
    if (displayName == NULL)
    {
        displayName = ":0.0";
    }

    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);
    }
    else
    {
        int root_x, root_y;
        int win_x, win_y;
        unsigned int mask;
        Window root, child;
        int screen, otherScreen, wantScreen;
        Window focus = 0;
        int revert_to = 0;

        screen = DefaultScreen(display);
        otherScreen = 1 - DefaultScreen(display);

		if (argc >= 2) {
			wantScreen = atoi(argv[1]);
			if (wantScreen == screen)
				goto byebye;
		}

        // get current mouse coordinates
        XQueryPointer(display,
                      RootWindow(display, screen),
                      &root,
                      &child,
                      &root_x,
                      &root_y,
                      &win_x,
                      &win_y,
                      &mask);

        // get ID of window with focus
        XGetInputFocus(display,
                       &focus,
                       &revert_to);

        fprintf(stderr, "Window ID is %x, focus is %x\n", focus, revert_to);

        // save to .pointer<screen>
        save(screen, root_x, root_y, focus);

        // load from .pointer<otherScreen>
        load(otherScreen, &root_x, &root_y, &focus);

        fprintf(stderr, "Moving to screen %d, x = %d, y = %d\n", otherScreen, root_x, root_y);

        // move mouse to other screen
        XTestFakeMotionEvent(display,
                             otherScreen,
                             root_x,
                             root_y,
                             CurrentTime);

        // set ID of window with focus
        XSetInputFocus(display,
                       focus,
                       1,   // revert_to
                       CurrentTime);
    }

byebye:
    XCloseDisplay(display);

    return (0);
}
