/*
 * Small program to find out the mouse position in global screen
 * coordinates.
 *
 * (C) 2004 by Jochen Eppler <eppler@mindzoo.de>
 * This program may be redistributed under the terms of the GPL
 * (see http://www.fsf.org/licenses/gpl.html)
 *
 * You have to compile with something like
 * g++ mousepos.cc -o mousepos -I/usr/include/c++/3.3/ -lstdc++
 * -L/usr/X11R6/lib/ -L/usr/lib/gcc-lib/i486-linux/3.3.4/ -lX11
 */

#include <X11/Xlib.h>
#include <iostream>
#include <unistd.h>

int main()
{
  Display* display = XOpenDisplay(NULL);
  if (display == NULL)
  {
    std::cerr << "Cannot connect to X server" << std::endl;
    return 1;
  }

  std::cout << "Connection to X server established" << std::endl;

  Window root, child;
  int x = 0, y = 0, win_x = 0, win_y = 0;
  int x_old = 0, y_old = 0;
  unsigned int mask = 0;

  while (true)
  {
    XQueryPointer(display, RootWindow(display, 0), &root, &child,
                  &x, &y, &win_x, &win_y, &mask);
    if (x != x_old || y != y_old)
    {
      std::cout << "Mouse position (X, Y): " << x  << ", "
                << y << std::endl;
      x_old = x;
      y_old = y;
    }
    usleep(500);
  }
  
  return 0;
}
