#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<time.h>

#include	<unistd.h>

#include	<X11/X.h>
#include	<X11/Xlib.h>
#include	<X11/Xutil.h>
#ifdef SHAPE
#include	<X11/extensions/shape.h>
#endif

#include	"lwm.h"

extern void 
setshape(Client *c)
{
#ifdef  SHAPE
	int	n;
	int	order;
	XRectangle *rect;

	if (shape) {
		XShapeSelectInput(dpy, c->window, ShapeNotifyMask);
		rect = XShapeGetRectangles(dpy, c->window, ShapeBounding, &n, &order);
		if (n > 1)
			XShapeCombineShape(dpy, c->parent, ShapeBounding, border - 1,
			    border - 1, c->window, ShapeBounding, ShapeSet);
	}
	XFree(rect);
#else
	USED(c);
#endif
}


#ifdef  SHAPE
static void
shapenotify(XEvent *ev)
{
	Client	*c;
	XShapeEvent	*e = (XShapeEvent *)ev;

	c = Client_Get(e->window);
	if (c != 0)
		setshape(c);
}
#endif

extern int
shapeevent(XEvent *ev)
{
#ifdef  SHAPE
	if (shape && ev->type == shape_event) {
		shapenotify(ev);
		return 1;
	}
#else
	USED(ev);
#endif
	return 0;
}

extern int
isshaped(Window w)
{
#ifdef SHAPE
	int	n;
	int	order;
	XRectangle	*rect;

	rect = XShapeGetRectangles(dpy, w, ShapeBounding, &n, &order);
	XFree(rect);

	return (n > 1);
#else
	USED(w);
	return 0;
#endif
}



extern int
servershapes(void)
{
#ifdef SHAPE
	int shape_error;
	return XShapeQueryExtension(dpy, &shape_event, &shape_error);
#else
	return 0;
#endif
}
