/* $Id: Init.c,v 1.1 1997/04/15 10:09:36 archer Exp $
 *	INIT.C - Initialize Widget Library
 *
 * This is the entry point for the library. This is also the exit point.
 */
#include <Widget/WidgetsP.h>
#include <Widget/EventsP.h>
#include <Widget/drawP.h>
#include <Widget/IconP.h>
#include <Widget/SignalP.h>
#include <Widget/PerrorP.h>
#include <Widget/colorsP.h>


/**
 ** Global variables
 **/
Display		*x11__screen;
X11_widget	x11__widget_root;
Window		x11__root_id;
unsigned long	x11__white_pixel,
		x11__black_pixel;
boolean		x11__is_monochrome;


/**
 ** Private variables
 **/
static x11__core	x11__root;


/**
 ** X11_INITIALIZE
 **	Initialize the widget library
 **/
boolean X11_Initialize(char *name, char *file, int *argc, char**argv)
{
Display		*dpy;
char		*s;
int		i, scr;
/*
 * Figure out a "-display XXXX" option
 */
	s = 0;
	scr = *argc - 1;
	for (i = 1; i < scr; i++)
		if (strcmp(argv[i], "-display") == 0) {
/*
 * Found!
 */
			s = argv[i+1];
			*argc -= 2;
			for (scr--; i < scr; i++)
				argv[i] = argv[i+2];
			argv[i] = 0;
			break;
		}
/*
 * Open the X11 display
 */
	if ((dpy = XOpenDisplay(s)) == 0)
		return False;
	x11__screen = dpy;
	scr = DefaultScreen(dpy);
/*
 * Clear watch list
 */
	FD_ZERO(&x11__widget_ifds);
	FD_ZERO(&x11__widget_ofds);
/*
 * Open root display and resources
 */
	x11__widget_root = &x11__root;
	x11__root_id     = RootWindow(dpy, scr);
	x11__black_pixel = BlackPixel(dpy, scr);
	x11__white_pixel = WhitePixel(dpy, scr);
	x11__default_gc  = DefaultGC(dpy, scr);
	if (DisplayPlanes(dpy, scr) <= 1)
		x11__is_monochrome = True;
/*
 * Fill in with data
 */
	x11__root.id           = x11__root_id;
	x11__root.w_min        =
	x11__root.w            = DisplayWidth(dpy, scr);
	x11__root.h_min        =
	x11__root.h            = DisplayHeight(dpy, scr);
/*
 * Add to the list of fds to watch for...
 */
	FD_SET(ConnectionNumber(dpy), &x11__widget_ifds);
/*
 * End of initializations
 */
	x11__resource_init(name, file, argc, argv);
/*
 * Go to synchonous?
 */
	if ((s=x11__resource_value("Synchronous")) != 0 && !strcasecmp(s, "on"))
		XSynchronize(dpy, True);
/*
 * Avoid sigalarms (for debugging purposes)
 */
	if ((s = x11__resource_value("Signal")) != 0 && !strcasecmp(s, "off"))
		x11__signal_init(False);
	else
		x11__signal_init(True);
/*
 * End of init
 */
	x11__color_init();
	x11__draw_init();
	x11__icon_init();
	x11__error_setup();
/*
 * Generic resources loaded last
 */
	x11__resource_basic(x11__widget_root);
	return True;
}
