/***********************************************************
Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
and the Massachusetts Institute of Technology, Cambridge, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Digital or MIT not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/
/* $Header: main.c,v 1.105 87/06/22 20:48:15 toddb Exp $ */
#include "X.h"
#include "Xproto.h"
#include "input.h"
#include "scrnintstr.h"
#include "misc.h"
#include "os.h"
#include "osstruct.h"
#include "windowstr.h"
#include "resource.h"
#include "dixstruct.h"
#include "gcstruct.h"
#include "extension.h"
#include "colormap.h"
#include "cursorstr.h"
#include "opaque.h"
#include "vendor.h"

extern char *display;
char *ConnectionInfo;
char *SwappedConnInfo;
xConnSetupPrefix connSetupPrefix;

extern WindowRec WindowTable[];
extern xColorItem screenWhite, screenBlack;
extern FontPtr defaultFont;
extern void SetInputCheck();
extern void InitExtensions();
PaddingInfo PixmapWidthPaddingInfo[32];

unsigned char *minfree;

static int restart = 0;


NotImplemented()
{
    FatalError("Not implemented");
}

/*
 * This array encodes the answer to the question "what is the log base 2
 * of the number of pixels that fit in a scanline pad unit?"
 * Note that ~0 is an invalid entry (mostly for the benefit of the reader).
 */
static int answer[6][3] = {
	/* pad   pad   pad */
	/*  8     16    32 */

	{   3,     4,    5 },	/* 1 bit per pixel */
	{   1,     2,    4 },	/* 4 bits per pixel */
	{   0,     1,    2 },	/* 8 bits per pixel */
	{   ~0,    0,    1 },	/* 16 bits per pixel */
	{   ~0,    ~0,   0 },	/* 24 bits per pixel */
	{   ~0,    ~0,   0 }	/* 32 bits per pixel */
};

/*
 * This array gives the answer to the question "what is the first index for
 * the answer array above given the number of bits per pixel?"
 * Note that ~0 is an invalid entry (mostly for the benefit of the reader).
 */
static int indexForBitsPerPixel[ 33 ] = {
	~0, 0, ~0, ~0,	/* 1 bit per pixel */
	1, ~0, ~0, ~0,	/* 4 bits per pixel */
	2, ~0, ~0, ~0,	/* 8 bits per pixel */
	~0,~0, ~0, ~0,
	3, ~0, ~0, ~0,	/* 16 bits per pixel */
	~0,~0, ~0, ~0,
	4, ~0, ~0, ~0,	/* 24 bits per pixel */
	~0,~0, ~0, ~0,
	5		/* 32 bits per pixel */
};

/*
 * This array gives the answer to the question "what is the second index for
 * the answer array above given the number of bits per scanline pad unit?"
 * Note that ~0 is an invalid entry (mostly for the benefit of the reader).
 */
static int indexForScanlinePad[ 33 ] = {
	~0, ~0, ~0, ~0,
	~0, ~0, ~0, ~0,
	0,  ~0, ~0, ~0,	/* 8 bits per scanline pad unit */
	~0, ~0, ~0, ~0,
	1,  ~0, ~0, ~0,	/* 16 bits per scanline pad unit */
	~0, ~0, ~0, ~0,
	~0, ~0, ~0, ~0,
	~0, ~0, ~0, ~0,
	2		/* 32 bits per scanline pad unit */
};

main(argc, argv)
    int		argc;
    char	*argv[];
{
    int		i, j, k, looping;
    long	alwaysCheckForInput[2];

    /* Notice if we're restart.  Probably this is because we jumped through
     * uninitialized pointer */
#ifdef notdef
    minfree = (unsigned char *)sbrk(0);               /* FOR DEBUG       XXX */
#endif

    if (restart)
	FatalError("server restarted. Jumped through uninitialized pointer?\n");
    else
	restart = 1;
    ErrorF(  "Hello, this is the X server\n");
    display = "0";
    ProcessCommandLine(argc, argv);

    alwaysCheckForInput[0] = 0;
    alwaysCheckForInput[1] = 1;
    looping = 0;
    while(1)
    {
	/* Perform any operating system dependent initializations you'd like */
	OsInit();		
	if(!looping)
	{
	    CreateWellKnownSockets();
	    InitProcVectors();
	    serverClient = (ClientPtr)Xalloc(sizeof(ClientRec));
	    serverClient->index = 0; /* reserved for server */
            serverClient->sequence = 0;
            serverClient->closeDownMode = RetainPermanent;
            serverClient->clientGone = FALSE;
            serverClient->lastDrawable = (pointer)NULL;
	    serverClient->lastDrawableID = None;
            serverClient->lastGC = (pointer)NULL;
	    serverClient->lastGCID = None;
	    serverClient->numSaved = None;
	    serverClient->saveSet = (pointer *)NULL;
	}

	InitClientResources(serverClient);      /* for root resources */

	SetInputCheck(&alwaysCheckForInput[0], &alwaysCheckForInput[1]);
	screenInfo.arraySize = 0;
	screenInfo.numScreens = 0;
	screenInfo.screen = (ScreenPtr)NULL;
	/*
	 * Just in case the ddx doesnt supply a format for depth 1 (like qvss).
	 */
	j = indexForBitsPerPixel[ 1 ];
	k = indexForScanlinePad[ BITMAP←SCANLINE←PAD ];
	PixmapWidthPaddingInfo[1].scanlinePad = BITMAP←SCANLINE←PAD-1;
	PixmapWidthPaddingInfo[1].bitmapPadLog2 = answer[j][k];

	InitOutput(&screenInfo, argc, argv);
	if (screenInfo.numScreens < 1)
	    FatalError("no screens found\n");
	InitEvents();
	InitInput(argc, argv);
	InitAndStartDevices(argc, argv);
	InitAtoms();
	InitExtensions(); 

	SetDefaultFontPath(defaultFontPath);	/* default path has no nulls */
	if ( ! SetDefaultFont(defaultTextFont))
	    ErrorF( "main: Could not open default font '%s'\n",
	        defaultTextFont);
	if ( ! (rootCursor = CreateRootCursor(defaultCursorFont, 0)))
	    ErrorF( "main: Could not open default cursor font '%s'\n",
		defaultCursorFont);

	for (i=0; i<screenInfo.numScreens; i++) 
	{
	    CreateGCperDepthArray(i);
	    CreateRootWindow(i);
	}
        DefineInitialRootWindow(&WindowTable[0]);
	if(!looping)
	{
	    CreateConnectionBlock();
	}

	ErrorF(  "Now I call dispatch\n");
	Dispatch();

	/* Now free up whatever must be freed */
	FreeAllResources();
	for ( i = 0; i < screenInfo.numScreens; i++)
        {
	    FreeGCperDepth(i);
	    DestroyRootWindow(i);
	}
	CloseDownDevices(argc, argv);
	for (i = 0; i < screenInfo.numScreens; i++)
	    (screenInfo.screen[i].CloseScreen)(i, &screenInfo.screen[i]);
	Xfree(screenInfo.screen);

        CloseFont(defaultFont);
        defaultFont = (FontPtr)NULL;

	ResetHosts(display);
	looping = 1;
    }
}

static int padlength[4] = {0, 3, 2, 1};

CreateConnectionBlock()
{
    xConnSetup setup;
    xWindowRoot root;
    xDepth	depth;
    xVisualType visual;
    xPixmapFormat format;
    int i, j, k,  
        lenofblock=0,
        sizesofar = 0;
    long vid;
    char *pBuf;
    KeyCode tmpmin, tmpmax;
    KeyCode tmpcode[2];

    
    /* Leave off the ridBase and ridMask, these must be sent with 
       connection */

    setup.release = 11;
    /*
     * per-server image and bitmap parameters are defined in Xmd.h
     */
    setup.imageByteOrder = screenInfo.imageByteOrder;
    setup.bitmapScanlineUnit  = screenInfo.bitmapScanlineUnit;
    setup.bitmapScanlinePad = screenInfo.bitmapScanlinePad;
    setup.bitmapBitOrder = screenInfo.bitmapBitOrder;
    setup.motionBufferSize = NumMotionEvents();
    setup.numRoots = screenInfo.numScreens;
    setup.nbytesVendor = strlen(VENDOR←STRING); 
    setup.numFormats = screenInfo.numPixmapFormats;
    setup.maxRequestSize = MAX←REQUEST←SIZE;
    QueryMinMaxKeyCodes(&tmpcode[0], &tmpcode[1]);
    setup.minKeyCode = tmpcode[0];
    setup.maxKeyCode = tmpcode[1];
    lenofblock = sizeof(xConnSetup) + 
            ((setup.nbytesVendor + 3) & ~3) +
	    (setup.numFormats * sizeof(xPixmapFormat)) +
            (setup.numRoots * sizeof(xWindowRoot));
    ConnectionInfo = (char *) Xalloc(lenofblock);

    bcopy((char *)&setup, ConnectionInfo, sizeof(xConnSetup));
    sizesofar = sizeof(xConnSetup);
    pBuf = ConnectionInfo + sizeof(xConnSetup);

    bcopy(VENDOR←STRING, pBuf, (int)setup.nbytesVendor);
    sizesofar += setup.nbytesVendor;
    pBuf += setup.nbytesVendor;
    i = padlength[setup.nbytesVendor & 3];
    if (i)
    {
        char pad[4];
        bcopy(pad, pBuf, i);
        pBuf += i;
	sizesofar += i;
    }    

    /*
     * Just in case the ddx doesnt supply a format for depth 1 (like qvss).
     */
    j = indexForBitsPerPixel[ 1 ];
    k = indexForScanlinePad[ BITMAP←SCANLINE←PAD ];
    PixmapWidthPaddingInfo[1].scanlinePad = BITMAP←SCANLINE←PAD-1;
    PixmapWidthPaddingInfo[1].bitmapPadLog2 = answer[j][k];

    for (i=0; i<screenInfo.numPixmapFormats; i++)
    {
	format.depth = screenInfo.formats[i].depth;
	format.bitsPerPixel = screenInfo.formats[i].bitsPerPixel;
	format.scanLinePad = screenInfo.formats[i].scanlinePad;;
	PixmapWidthPaddingInfo[format.depth].scanlinePad = format.scanLinePad-1;
	j = indexForBitsPerPixel[ format.depth ];
	k = indexForScanlinePad[ format.scanLinePad ];
	PixmapWidthPaddingInfo[format.depth].bitmapPadLog2 = answer[j][k];
	bcopy((char *)&format, pBuf, sizeof(xPixmapFormat));
	pBuf += sizeof(xPixmapFormat);
	sizesofar += sizeof(xPixmapFormat);
    }

    for (i=0; i<screenInfo.numScreens; i++) 
    {
	ScreenPtr	pScreen;
	DepthPtr	pDepth;
	VisualPtr	pVisual;

	pScreen = &(screenInfo.screen[i]);
        root.windowId = WindowTable[i].wid;
        root.defaultColormap = pScreen->defColormap;
        root.whitePixel = pScreen->whitePixel;
	root.blackPixel = pScreen->blackPixel;
        root.currentInputMask = 0;
        root.pixWidth = pScreen->width;
        root.pixHeight = pScreen->height;
        root.mmWidth = pScreen->mmWidth;
	root.mmHeight = pScreen->mmHeight;
        root.minInstalledMaps = pScreen->minInstalledCmaps;
        root.maxInstalledMaps = pScreen->maxInstalledCmaps; 
        root.rootVisualID = pScreen->rootVisual;		
        root.backingStore = pScreen->backingStoreSupport;
        root.saveUnders = pScreen->saveUnderSupport;
        root.rootDepth = pScreen->rootDepth;
	root.nDepths = pScreen->numDepths;
        bcopy((char *)&root, pBuf, sizeof(xWindowRoot));
	sizesofar += sizeof(xWindowRoot);
        pBuf += sizeof(xWindowRoot);

	pDepth = pScreen->allowedDepths;
	for(j = 0; j < pScreen->numDepths; j++, pDepth++)
	{
	    lenofblock += sizeof(xDepth) + 
		    (pDepth->numVids * sizeof(xVisualType));
            ConnectionInfo = (char *)Xrealloc(ConnectionInfo, lenofblock);
            pBuf = ConnectionInfo + sizesofar;            
	    depth.depth = pDepth->depth;
	    depth.nVisuals = pDepth->numVids;
	    bcopy((char *)&depth, pBuf, sizeof(xDepth));
	    pBuf += sizeof(xDepth);
	    sizesofar += sizeof(xDepth);
	    for(k = 0; k < pDepth->numVids; k++)
	    {
		vid = pDepth->vids[k];
		pVisual = (VisualPtr) LookupID(vid, RT←VISUALID, RC←CORE);
		visual.visualID = pVisual->vid;
		visual.class = pVisual->class;
		visual.bitsPerRGB = pVisual->bitsPerRGBValue;
		visual.colormapEntries = pVisual->ColormapEntries;
		visual.redMask = pVisual->redMask;
		visual.greenMask = pVisual->greenMask;
		visual.blueMask = pVisual->blueMask;
		bcopy((char *)&visual, pBuf, sizeof(xVisualType));
		pBuf += sizeof(xVisualType);
	        sizesofar += sizeof(xVisualType);
	    }
	}
    }
    connSetupPrefix.success = xTrue;
    connSetupPrefix.length = lenofblock/4;
    connSetupPrefix.majorVersion = X←PROTOCOL;
    connSetupPrefix.minorVersion = X←PROTOCOL←REVISION;
    ErrorF(  "Connection Prefix: len == %d\n",
	    connSetupPrefix.length);
}

/* VARARGS */
FatalError (msg, v0, v1, v2, v3, v4, v5, v6, v7, v8)
    char *msg;
    int v0, v1, v2, v3, v4, v5, v6, v7, v8;
{
    char *foo;
    ErrorF("\nFatal server bug!\n");
    ErrorF(msg, v0, v1, v2, v3, v4, v5, v6, v7, v8);
    ErrorF("\n");
    foo = (char *)NULL;
    *foo = 'A';
}

/*
	grow the array of screenRecs if necessary.
	call the device-supplied initialization procedure 
with its screen number, a pointer to its ScreenRec, argc, and argv.
	return the number of successfully installed screens.

*/

AddScreen(pfnInit, argc, argv)
    Bool	(* pfnInit)();
    int argc;
    char **argv;
{

    int i = screenInfo.numScreens;
    int scanlinepad, format, depth, j, k;
#ifdef DEBUG
    void	(**jNI) ();
#endif /* DEBUG */

    if (screenInfo.numScreens == screenInfo.arraySize)
    {
	screenInfo.arraySize += 5;
	screenInfo.screen = (ScreenPtr)Xrealloc(
	    screenInfo.screen, 
	    screenInfo.arraySize * sizeof(ScreenRec));
    }

#ifdef DEBUG
	    for (jNI = &screenInfo.screen[i].QueryBestSize; 
		 jNI < (void (**) ()) &screenInfo.screen[i].RegionExtents; 
		 jNI++)
		*jNI = (void (*) ())NotImplemented;
#endif /* DEBUG */

    /*
     * This loop gets run once for every Screen that gets added,
     * but thats ok.  If the ddx layer initializes the formats
     * one at a time calling AddScreen() after each, then each
     * iteration will make it a little more acurate.  Worst case
     * we do this loop N * numPixmapFormats where N is # of screens.
     * Anyway, this must be called after InitOutput and before the
     * screen init routine is called.
     */
    for (format=0; format<screenInfo.numPixmapFormats; format++)
    {
	depth = screenInfo.formats[format].depth;
	scanlinepad = screenInfo.formats[format].scanlinePad;
	PixmapWidthPaddingInfo[ depth ].scanlinePad = scanlinepad - 1;
	j = indexForBitsPerPixel[ depth ];
	k = indexForScanlinePad[ scanlinepad ];
	PixmapWidthPaddingInfo[ depth ].bitmapPadLog2 = answer[j][k];
    }

    /* This is where screen specific stuff gets initialized.  Load the
       screen structure, call the hardware, whatever.
       This is also where the default colormap should be allocated and
       also pixel values for blackPixel, whitePixel, and the cursor
       Note that InitScreen is NOT allowed to modify argc, argv, or
       any of the strings pointed to by argv.  They may be passed to
       multiple screens. 
    */ 
    screenInfo.screen[i].rgf = ~0;  /* there are no scratch GCs yet*/
    if ((*pfnInit)(i, &screenInfo.screen[i], argc, argv))
    {
	screenInfo.numScreens++;
	/* grow connection block.... */
    }
    else
	ErrorF("screen %d failed initialization\n", i);

    return screenInfo.numScreens;
}