


    /*--------------------------------------------------------------*/
    /*                                                              */
    /*      Initialisierung und Verlassen eines GEM-Programms       */
    /*                                                              */
    /*          Copyright ½ 1987 by Markus Nick                     */
    /*                                                              */
    /*--------------------------------------------------------------*/

/*
	---------------------------------------------------------------

	This program is distributed in the hope that it will be useful, 
	but WITHOUT ANY WARRANTY; without even the implied warranty of 
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
	documentation `docs\ruby.tex' for more details.

	---------------------------------------------------------------
 *	Dieses Modul geh”rt in dieser Form zur Library Ruby.
 *
 *	Ruby ist Freeware.  Beachten Sie bitte die Nutzungsbedingungen in 
 *	der Dokumentation `docs\ruby.tex'.
 *
 *	Mainz 41, 4.4.92  Markus M. Nick
 */

/*

	Globale Funktionen dieses Moduls:

	calcScrSize(w_pix, h_pix, planes)
	open_vwork()
	init_appl(flg, acc_name or 0L)
	init_appl2(flg, acc_name or 0L, f_end_update)
	exit_appl()
	exit_appl2()		-- wie exit_appl(), aber ohne exit(0) am Ende

********************************************************************

    Prinzipieller Aufbau eines GEM-Programms mit diesen Routinen:

...

main()
{
    ...
    
    init_appl(TRUE, acc_menu_entry);
    		// oder init_appl(FALSE), falls das VDI nicht ben”tigt 
    		// wird - Achtung! show_error() und PopUp() nutzen 
    		// das VDI!
    
    ...
    
    exit_appl();		// Verlassen durch exit(0)
}

...

*/


/*  
    INCLUDE-FILES
*/

#define __GEM_HNDL__	1
#include <stddef.h>
#include <stdlib.h>
#include <aes.h>
#include <vdi.h>
#include <tos.h>
#include "portab.h"
#include "stuff.h"

extern	_app;


/*
    externe Variablen
*/

WORD handle,lib_handle,
	phys_handle,dummy,
	menu_id = -1,
	GDOS = FALSE,
	gl_wchar,gl_hchar,gl_wbox,gl_hbox,
	sys_fnt_pt_height = 0,
	xdesk,ydesk,wdesk,hdesk,
	msgbuff[20];

#ifdef MEGAMAX
WORD	contrl[12],	intin[128],ptsin[256], intout[128],ptsout[12];
#endif

WORD	work_out[57], lib_work_out[57];

MFDB	std_fdb;	/* standard FDB */

LONG	scr_size = 32000L;	/* size of a screen in bytes,
							   default: 32000 bytes */
WORD		scr_width, scr_height;


/*
 *	calcScrSize()
 *	calculates the size of a screen in bytes
 */
LONG calcScrSize(w_pix, h_pix, planes)
WORD	w_pix,	/* width of screen in pixels */
	h_pix,	/* height of screen in pixels */
	planes;	/* planes of screen */
{
	LONG	size;

	size = w_pix * planes / 8L;	/* bytes per line */
	size *= h_pix;				/* bytes per screen */
	scr_width  = w_pix;
	scr_height = h_pix;

	return size;	/* return bytes per screen */
}


void open_vwork(f_extra_lh)		/* TRUE: lib_handle != handle */
BOOLEAN f_extra_lh;
{
	int	i;
	WORD	work_in[12], ext_work_out[57];


	for (i = 0;i < 10;work_in[i++] = 1)
		;
	work_in[10] = 2;
	if(GDOS)
		work_in[0] = Getrez() + 2;	/* driver id */
        
	handle = phys_handle;
	v_opnvwk(work_in,&handle,work_out);

	/** handle ok? **/
	if(handle <= 0)
	{
		form_alert(1,"[3][Cannot open virtual|workstation!][Cancel]");
		appl_exit();
		exit(-1);
	}

	if(f_extra_lh)
	{
		lib_handle = phys_handle;
		v_opnvwk(work_in,&lib_handle,lib_work_out);
	
		/** lib_handle ok? **/
		if(lib_handle <= 0)
		{
			form_alert(1,"[3][Cannot open virtual|workstation for libraries!][Cancel]");
			v_clsvwk(handle);
			appl_exit();
			exit(-1);
		}
	}
	else
		lib_handle = handle;

	/*if(work_out[13])*/	/* 0 colors? */
	{
		vq_extnd(handle, TRUE, ext_work_out);
		
		/*for(i = 1; (1<<i) < work_out[13]; i++)
			;*/		/* find out number of necessary planes */
		scr_size = calcScrSize(work_out[0]+1,work_out[1]+1, ext_work_out[4]);
		/* no, calculate screen size */
	}

	/* future:
			get # planes from vq_extnd() work_out[4] */
	/*for(std_fdb.fd_nplanes = 0, i = work_out[13]-1; i >= 1; i >>= 1, std_fdb.fd_nplanes++)
		;*/
	std_fdb.fd_nplanes = ext_work_out[4];
	
	std_fdb.fd_addr = 0L;	/*Logbase();*/
	std_fdb.fd_w = work_out[0]+1;
	std_fdb.fd_h = work_out[1]+1;
	std_fdb.fd_wdwidth = std_fdb.fd_w / 16 /* * std_fdb.fd_nplanes*/ ;
	std_fdb.fd_stand = 0;	/* raster coordinate system */

	{/* system font (normale Gr”že) */
	int i = 0;
	int c_w, c_h;
	do{
		i++;
		sys_fnt_pt_height = vst_point(lib_handle, i, &dummy,&dummy, &c_w,&c_h);
	}while(c_h < gl_hchar /*&& c_w < gl_wchar*/);
	vst_point(lib_handle, sys_fnt_pt_height, &dummy, &dummy, &dummy, &dummy);
	}
}


void init_appl(flg, acc_name)
BOOLEAN flg;
char *acc_name;
{
	void init_appl2();
	init_appl2(flg, acc_name, TRUE);
}


#ifndef __TURBOC__
extern
#endif
int gl_apid;


void init_appl2(flg, acc_name, wiupd_end)
BOOLEAN flg;
char *acc_name;
BOOLEAN wiupd_end;		/* TRUE: END_UPDATE before return */ 
{
	appl_init();
#ifdef __TURBOC__
	gl_apid = _GemParBlk.global[2];
#else
	gl_apid = global[2];
#endif

	if(gl_apid < 0)
	{
		Cconws("\n\rFATALER FEHLER! Kann Programm nicht anmelden.\n\rTaste drcken...\n\r");
		Cnecin();
	}

	wind_update(BEG_UPDATE);

	/** Accessory? **/
	if(!_app && acc_name)
		menu_id = menu_register(gl_apid, acc_name);

	handle = lib_handle = -1;
	phys_handle = graf_handle(&gl_wchar,&gl_hchar,&gl_wbox,&gl_hbox);
	wind_get(0,WF_WORKXYWH,&xdesk,&ydesk,&wdesk,&hdesk);
	if(_app)
		wind_set(0,WF_NEWDESK, 0L,0);

	if(flg)
	{
		if(vq_gdos())
			GDOS = TRUE;	/* GDOS advailable? yes => GDOS == TRUE */

		open_vwork(TRUE);

		if(_app)
			v_show_c(handle, 0);
	}

	if(wiupd_end)
		wind_update(END_UPDATE);
}


void close_vwork()
{
	if(lib_handle > 0 && lib_handle != handle) {
		v_clsvwk(lib_handle);
		lib_handle = 0;
	}
	if(handle > 0) {
		v_clsvwk(handle);
		handle = 0;
	}
}

void exit_appl2()
{
	close_vwork();
	appl_exit();
}


void exit_appl()
{
	exit_appl2();
	exit(0);
}

/* -eof- */
