#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/keysymdef.h>
/* #include "XInput.h" */
#include <XInput.h>

#include <stdio.h>
char *sprintf();
#include <math.h>

#define BITMAPDEPTH 1
#define PI 3.141593

int             screen;
GC              gc;
unsigned int    width, height;
unsigned int    width2, height2;

extern char    *optarg;
extern int      optind, opterr;

#define SMALL 1
#define OK 0

int
  motion_notify_type,
  button_press_type,
  button_release_type,
  focus_in_type,
  focus_out_type;
static XEventClass
  motion_notify_class,
  button_press_class,
  button_release_class,
  focus_in_class,
  focus_out_class;

static    Display	*display;
static    Window	dial_win;
static    XDevice       *xdhandle;
static	  int		select_class[5];


void
init_x(pDisplay,pDial_win)
     Display	*pDisplay;
     Window	pDial_win;
{
  long            strtol();
  int             qmajor, qminor, qfirsterror;
  XID             dial_id;
  XDeviceInfo    *xdevlist;
  char          **answer;
  int             nextentions, ndevices;
  int		  notfound = 1;
  int		  i;


  display = pDisplay;
  dial_win = pDial_win;


  answer = XListExtensions(display, &nextentions);
  debug(1, "Number of extentions %d\n", nextentions);

  /* Extensions */
  for (i = 0; i < nextentions; i++) {
    if (notfound)
      notfound = strcmp("XInputDeviceEvents",answer[i]);
    debug(1, " Extention %d [%s]\n", i, answer[i]);
    if (XQueryExtension(display, answer[i], &qmajor,
			&qminor, &qfirsterror)) {
      debug(1, " Major %d minor %d firsterror %d\n",
	    qmajor, qminor, qfirsterror);
    } else
      debug(1, "Query failed.\n");
  }
  if (notfound) {
    fprintf(stderr,"Input Extension not found on this server\n");
    return;
  }
  /* Input extensions */
  xdevlist = XListInputDevices(display, &ndevices);
  debug(2, "Number of input devices %d\n", ndevices);

  {
    int             i, j, k;
    XAnyClassPtr    classInfo;
    XKeyInfoPtr     key;
    XButtonInfoPtr  but;
    XValuatorInfoPtr val;
    for (i = 0; i < ndevices;
	 i++) {
      debug(3, "<%s> %d classes. Type %s\n", xdevlist[i].name,
	    xdevlist[i].num_classes, XGetAtomName(display, xdevlist[i].type));
      classInfo = xdevlist[i].inputclassinfo;
      for (j = 0; j <
	   xdevlist[i].num_classes; j++) {
	switch (classInfo->class) {
	case KeyClass:
	  key = (XKeyInfoPtr) classInfo;
	  debug(3, "  Key class min_key = %d max_key = %d numkeys = %d \n "
		, key->min_keycode, key->max_keycode, key->num_keys);
	  break;
	case ButtonClass:
	  but = (XButtonInfoPtr)
	    classInfo;
	  if (!strcmp(xdevlist[i].name,
		      "sundials"))
	    dial_id = xdevlist[i].id;
	  debug(3, "  Button class num_buttons = %d\n",
		but->num_buttons);
	  break;
	case ValuatorClass:

	  val = (XValuatorInfoPtr) classInfo;
	  if (!strcmp(xdevlist[i].name,
		      "sundials"))
	    dial_id = xdevlist[i].id;
	  debug(3, "  Valuator class num_axes = %d, mode = %d \n "
		, val->num_axes, val->mode);
	  debug(3, " motion_buffer = %d \n ", val->motion_buffer);
	  for (k = 0; k < val->num_axes; k++)
	    debug(3, "     Axis[%d] resolution = %d min = %d max = %d \n "
		  , k, val->axes[k].resolution, val->axes[k].min_value,
		  val->axes[k].max_value);
	  break;
	default:
	  debug(3, "Unknown class %d\n",
		xdevlist[i].inputclassinfo->class);
	  break;
	}
	classInfo = (XAnyClassPtr)
	  (((char *) classInfo) + classInfo->length);
      }
    }
  }
  XFreeDeviceList(xdevlist);
  xdhandle = XOpenDevice(display, dial_id);
  DeviceMotionNotify(xdhandle, motion_notify_type, motion_notify_class);
  DeviceButtonPress(xdhandle, button_press_type, button_press_class);
  DeviceButtonRelease(xdhandle, button_release_type, button_release_class);
  DeviceFocusIn(xdhandle, focus_in_type, focus_in_class);
  DeviceFocusOut(xdhandle, focus_out_type, focus_out_class);

  /* Window set up */
  screen = DefaultScreen(display);
  width = 250;
  height = 400;
  width2 = 450;
  height2 = 450;

  XSelectInput(display, dial_win, 0xffffff );

  select_class[0] = motion_notify_class;
  select_class[1] = button_press_class;
  select_class[2] = button_release_class;
  select_class[3] = focus_in_class;
  select_class[4] = focus_out_class;

  select_dev(dial_win);
  /* select_dev(RootWindow(display, screen));*/

  gc = DefaultGC(display, screen);
  debug(6, "Map window\n");
  CheckExtInit(display, XInput_Initial_Release);
}

select_dev(win)
     Window win;
{
   XSelectExtensionEvent(display, win, select_class, 3); 
}

do_focus(which)
Window which;
{

	XSetInputFocus(display, which, RevertToPointerRoot, CurrentTime );
   	XSetDeviceFocus(display, xdhandle, which,
		    RevertToNone, CurrentTime); 
}
