typedef struct arch_window_s
{
	XSetWindowAttributes attr;
	XVisualInfo		*visinfo;
	Display			*dpy;
	Cursor			null_cursor;
	vboolean		cursor_hidden;
	vboolean		pointer_captured;
	Window 			root;
	int				scrnum;
	int				width, height;
	char			title[128];		/* window title */
	Window			win;			/* window handle */
	Window			parent;			/* used for child windows */
}	arch_window_t, *arch_window_p;

/*
 * Original copyright (C) Id Software.
 */
V_LOCAL Cursor
X_CreateNullCursor(Display *display,Window root)
{
	Pixmap cursormask;
    XGCValues xgc;
    GC gc;
    XColor dummycolour;
    Cursor cursor;
    cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
    xgc.function = GXclear;
    gc =  XCreateGC(display, cursormask, GCFunction, &xgc);
    XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
    dummycolour.pixel = 0;
    dummycolour.red = 0;
    dummycolour.flags = 04;
    cursor = XCreatePixmapCursor(display, cursormask, cursormask,
		&dummycolour,&dummycolour, 0,0);
    XFreePixmap(display,cursormask);
    XFreeGC(display,gc);
    return cursor;
}

void V_CALL
archSetCursorPos(arch_window_p win,int x,int y)
{
	XWarpPointer(win->dpy,None,win->win,
		0, 0, 0, 0, x, y);
}

void V_CALL
archCapturePointer(arch_window_p win,vboolean capture)
{
	if(capture)
	{
		if(!win->pointer_captured)
		{
			XGrabPointer(win->dpy,win->win,True,0,GrabModeAsync,
				GrabModeAsync,win->win,None,CurrentTime);
			win->pointer_captured = true;
		}
	}
	else
	{
		if(win->pointer_captured)
		{
			XUngrabPointer(win->dpy,CurrentTime);
			win->pointer_captured = false;
		}
	}
}

void V_CALL
archShowCursor(arch_window_p win,vboolean show)
{
	if(show)
	{
		if(win->cursor_hidden)
		{
			XUndefineCursor(win->dpy,win->win);
			win->cursor_hidden = false;
		}
	}
	else
	{
		if(!win->cursor_hidden)
		{
			XDefineCursor(win->dpy,win->win,win->null_cursor);
			win->cursor_hidden = true;
		}
	}
}

void V_CALL
archCaptureKeyboard(arch_window_p win,vboolean capture)
{
	if(capture)
	{
		if(!win->keyboard_captured)
		{
			XGrabKeyboard(win->dpy,win->win,False,GrabModeAsync,
				GrabModeAsync,CurrentTime);
			win->keyboard_captured = true;
		}
	}
	else
	{
		if(win->keyboard_captured)
		{
			XUngrabKeyboard(win->dpy,CurrentTime);
			win->keyboard_captured = false;
		}
	}
}

