/**************************************************************************
 * APLVWORK.C - apl_vopen() and apl_vclose() functions.
 *
 * 01/05/93 -	The Getrez() call that sets the system fonts for the
 *				workstation is always done now, instead of being done
 *				only on the first call to apl_vopen().	Also, the
 *				gl_vwout and gl_vxout arrays are both updated on every
 *				open call, so they always reflect the values of the most
 *				recent open.  I found a need (in Cyber Paint) to have
 *				two workstations open in two different resolutions, and
 *				these changes help.
 *************************************************************************/

#include "gemfintl.h"
#include <osbind.h>

static short GFAR work_in[11] = {
	-1, 				/* device driver (filled in at runtime) */
	1,					/* polyline type	(normal)			*/
	1,					/* polyline color	(foreground)		*/
	1,					/* polymarker type	(dot)				*/
	1,					/* polymarker color (foreground)		*/
	1,					/* text face		(standard)			*/
	1,					/* text color		(foreground)		*/
	1,					/* fill interior	(solid) 			*/
	8,					/* fill style		(solid) 			*/
	1,					/* fill color		(foreground)		*/
	2					/* use RC coordinate system 			*/
};

static short shared_handle = 0;    /* shared workstation handle 	  */

short gl_vwout[57]; 			   /* global work_out from v_opnvwk() */
short gl_vxout[57]; 			   /* global work_out from vq_extnd() */

/*-------------------------------------------------------------------------
 * apl_vclose - Close VDI virtual workstation.
 *	 if it's the shared workstation, don't really close it.
 *-----------------------------------------------------------------------*/

void apl_vclose(vdi_handle)
	register short vdi_handle;
{
	if (vdi_handle == shared_handle) {
		return;
	} else if (vdi_handle != 0) {
		v_clsvwk(vdi_handle);
	}
}

/*-------------------------------------------------------------------------
 * cleanup_shared_workstation() - Really close the shared workstation.
 *	this is called from apl_cleanup() via the _AesVCleanup vector.
 *-----------------------------------------------------------------------*/

#ifdef GEMFAST_PROTOS
  static void GSTACKARGS cleanup_shared_workstation(void)
#else
  static void GSTACKARGS cleanup_shared_workstation()
#endif
{
	if (shared_handle != 0) {
		v_clsvwk(shared_handle);
		shared_handle = 0;
	}
}

/*-------------------------------------------------------------------------
 * apl_vopen() - Routine to open a virtual workstation.
 *-----------------------------------------------------------------------*/

short apl_vopen()
{
	short vdi_handle;

	if (gl_grfhandle == 0) {
		_ApXinit();
	}

	work_in[0] = 2 + Getrez();	/* Set system fonts for workstation */

	vdi_handle = gl_grfhandle;
	v_opnvwk(work_in, &vdi_handle, gl_vwout);

	if (vdi_handle != 0) {
		vq_extnd(vdi_handle, 1, gl_vxout);
	}

	return vdi_handle;
}

/*-------------------------------------------------------------------------
 * apl_vshared() - Return the handle to the shared workstation; open the
 *				   shared workstation if necessary;
 *-----------------------------------------------------------------------*/

short apl_vshared()
{
	if (shared_handle == 0) {
		_AesVCleanup  = cleanup_shared_workstation;
		shared_handle = apl_vopen();
	}
	return shared_handle;
}
