/* 
 * tkAppInit.c --
 *
 *	Provides a default version of the Tcl_AppInit procedure for
 *	use in wish and similar Tk-based applications.
 *
 * Copyright (c) 1993 The Regents of the University of California.
 * Copyright (c) 1994 Sun Microsystems, Inc.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

/* TkWin version 0.3 -- Tk for Windows
 * Copyright (C) 1994 K. K. Kubota
 * Mathematical Sciences Computing Facility, University of Kentucky,
 * Lexington, KY 40506, USA (e-mail: ken@ms.uky.edu)
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose is granted without fee, provided that the above copyright notice
 * and the following paragraph appear in all copies of this software
 * and supporting documentation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY, even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  In no event shall the author be
 * liable to any party for direct, indirect, special, incidental, or
 * consequential damages arising out of the use of this software and its
 * documentation.
 */

#ifndef lint
static char sccsid[] = "@(#) tkAppInit.c 1.12 94/12/17 16:30:56";
#endif /* not lint */

#ifdef TCL_MSDOS_PORT
#include "tkInt.h"
#include <winsock.h>
EXTERN char * 	tk_RcFileName;
#else
#include "tk.h"
#endif

/*
 * The following variable is a special hack that is needed in order for
 * Sun shared libraries to be used for Tcl.
 */

#ifdef NEED_MATHERR
extern int matherr();
int *tclDummyMathPtr = (int *) matherr;
#endif

/*
 *----------------------------------------------------------------------
 *
 * main --
 *
 *	This is the main program for the application.
 *
 * Results:
 *	None: Tk_Main never returns here, so this procedure never
 *	returns either.
 *
 * Side effects:
 *	Whatever the application does.
 *
 *----------------------------------------------------------------------
 */

#ifndef TCL_MSDOS_PORT
int
main(argc, argv)
    int argc;			/* Number of command-line arguments. */
    char **argv;		/* Values of command-line arguments. */
{
    Tk_Main(argc, argv);
    return 0;			/* Needed only to prevent compiler warning. */
}
#endif

#ifdef TCL_MSDOS_PORT
static int numApps = 0;			/* number of apps using the .dll */

/* Deregistration from Winsock DLL */
int _export WinsockCleanup(void){
	if(--numApps < 1){
	    WSACancelBlockingCall();
	    WSAUnhookBlockingHook();
	}
	return WSACleanup();
}

/* Blocking Hook for Winsock */
BOOL BlockingHook(void){
	return Tk_DoOneEvent(TK_ALL_EVENTS | TK_DONT_WAIT);
}
#endif

/*
 *----------------------------------------------------------------------
 *
 * Tcl_AppInit --
 *
 *	This procedure performs application-specific initialization.
 *	Most applications, especially those that incorporate additional
 *	packages, will have their own version of this procedure.
 *
 * Results:
 *	Returns a standard Tcl completion code, and leaves an error
 *	message in interp->result if an error occurs.
 *
 * Side effects:
 *	Depends on the startup script.
 *
 *----------------------------------------------------------------------
 */

#ifdef TCL_MSDOS_PORT
int
Tk_AppInit(interp, tcpFlag)
    int tcpFlag;
#else
int
Tcl_AppInit(interp)
#endif
    Tcl_Interp *interp;		/* Interpreter for application. */
{
    Tk_Window main;
#ifdef TCL_MSDOS_PORT
    WSADATA wsaData;
#endif

    main = Tk_MainWindow(interp);

    if (Tcl_Init(interp) == TCL_ERROR) {
	return TCL_ERROR;
    }
    if (Tk_Init(interp) == TCL_ERROR) {
	return TCL_ERROR;
    }

    /*
     * Call the init procedures for included packages.  Each call should
     * look like this:
     *
     * if (Mod_Init(interp) == TCL_ERROR) {
     *     return TCL_ERROR;
     * }
     *
     * where "Mod" is the name of the module.
     */
#ifdef TCL_MSDOS_PORT
        if (TkTCP_Init(interp) == TCL_ERROR) {
	    return TCL_ERROR;
        }
        /* Initialize Winsock DLL */
    if(tcpFlag) {
#define version	0x101		/* version 1.1 of Winsock */
        if(WSAStartup(version, &wsaData) != 0) {
	    return TCL_ERROR;
        }
        if(wsaData.wVersion != 0x101) return TCL_ERROR;
        if(numApps++ == 0 && WSASetBlockingHook((FARPROC)BlockingHook) == NULL)
	    return TCL_ERROR;
    }
#endif

    /*
     * Call Tcl_CreateCommand for application-specific commands, if
     * they weren't already created by the init procedures called above.
     */

    /*
     * Specify a user-specific startup file to invoke if the application
     * is run interactively.  Typically the startup file is "~/.apprc"
     * where "app" is the name of the application.  If this line is deleted
     * then no user-specific startup file will be run under any conditions.
     */

#ifdef TCL_MSDOS_PORT
    tk_RcFileName = "~/wishrc";
#else
    tcl_RcFileName = "~/.wishrc";
#endif
    return TCL_OK;
}
