/* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.

This file is part of Ghostscript.

Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing.  Refer
to the Ghostscript General Public License for full details.

Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License.  A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities.  It should be in a file named COPYING.  Among other
things, the copyright notice and this notice must be preserved on all
copies.  */

/* gdevpm.c */
/*
 * OS/2 Presentation manager driver
 * By Russell Lang (based on gdevmswn.c and gdevwdib.c)
 *
 * If Ghostscript is a PM application, stdin/stdout are not
 * provided and so no text window is available.
 * If Ghostscript is a windowed text application, a message queue
 * can't be created (or so it seems) so a PM window for graphics 
 * can't be created.
 * The solution is to have two programs - gsos2.exe is a text application
 * and the outboard PM driver is gspmdrv.exe 
 * Another solution may be to make Ghostscript a PM application
 * and use VIO calls to provide a text window.
 *
 * In future, if Ghostview starts Ghostscript, instead of the PM driver
 * displaying the bitmap, Ghostview will be responsible for it.
 *
 * Since Ghostscript is not a PM application, this driver creates a
 * BMP bitmap in a named shared memory block and a second process
 * gspmdrv.exe reads this memory block and provides the PM window.  
 * Communication to gspmdrv.exe is via the shared memory block
 * and semaphores.
 */

#define INCL_DOS
#define INCL_DOSERRORS
#define INCL_WINWINDOWMGR
#define INCL_DEV

#include <os2.h>
#include "string_.h"
#include <stdlib.h>
#include "gx.h"
#include "gserrors.h"
#include "gxdevice.h"

#include "gp.h"
#include "gpcheck.h"
#include "gsprops.h"
#include "gdevpccm.h"

#include "gxdevmem.h"
#include "gdevpm.h"

extern int gs_exit_status;
extern int gs_exit_code;

#define MIN_COMMIT 4096    /* memory is committed in these size chunks */
#define ID_NAME "GSPMDRV_%u_%u"

/* Initial values for width and height */
#define INITIAL_RESOLUTION 96
#define INITIAL_WIDTH (INITIAL_RESOLUTION * 85 / 10 + 1)
#define INITIAL_HEIGHT (INITIAL_RESOLUTION * 11 + 1)

/* A macro for casting the device argument */
#define pmdev ((gx_device_pm *)dev)


#define pm_gsview_sizeof 80
typedef struct gx_device_pm_s gx_device_pm;

#define gx_device_pm_common\
	int BitsPerPixel;\
	int UpdateInterval;\
	char GSVIEW[pm_gsview_sizeof];\
	int nColors;\
	BOOL updating;\
	HTIMER update_timer;\
	HEV sync_event;\
	HEV next_event;\
	HMTX bmp_mutex;\
	HQUEUE drv_queue;\
	HQUEUE term_queue;\
	ULONG session_id;\
	PID process_id;\
	PID gspid;\
	unsigned char *bitmap;\
	ULONG committed;\
	PBITMAPINFO2 bmi;

/* The device descriptor */
struct gx_device_pm_s {
	gx_device_common;
	gx_device_pm_common;
	gx_device_memory mdev;
};

/* Device procedures */

/* See gxdevice.h for the definitions of the procedures. */
private dev_proc_open_device(pm_open);
private dev_proc_get_initial_matrix(pm_get_initial_matrix);
private dev_proc_sync_output(pm_sync_output);
private dev_proc_output_page(pm_output_page);
private dev_proc_close_device(pm_close);
private dev_proc_map_rgb_color(pm_map_rgb_color);
private dev_proc_map_color_rgb(pm_map_color_rgb);
private dev_proc_fill_rectangle(pm_fill_rectangle);
private dev_proc_copy_mono(pm_copy_mono);
private dev_proc_copy_color(pm_copy_color);
private dev_proc_get_props(pm_get_props);
private dev_proc_put_props(pm_put_props);

private gx_device_procs pm_procs = {
	pm_open,
	pm_get_initial_matrix,
	pm_sync_output,
	pm_output_page,
	pm_close,
	pm_map_rgb_color,
	pm_map_color_rgb,
	pm_fill_rectangle,
	gx_default_tile_rectangle,
	pm_copy_mono,
	pm_copy_color,
	gx_default_draw_line,
	gx_default_get_bits,
	pm_get_props,
	pm_put_props,
	gx_default_map_cmyk_color,
	gx_default_get_xfont_procs
};
gx_device_pm gs_os2pm_device = {
	sizeof(gx_device_pm),
	&pm_procs,
	"os2pm",
	INITIAL_WIDTH, INITIAL_HEIGHT,
	INITIAL_RESOLUTION, INITIAL_RESOLUTION,
	no_margins,
	dci_black_and_white,
	0,		/* not open yet */
	8,		/* BitsPerPixel */
	5000,		/* UpdateInterval */
	"\0"		/* GSVIEW */
};

/* Compress a gx_color_value into an 8-bit PM color value, */
/* using only the high order 5 bits. */
#define pm_color_value(z)\
  ((((z) >> (gx_color_value_bits - 5)) << 3) +\
   ((z) >> (gx_color_value_bits - 3)))

/* prototypes for internal procedures */
private void pm_makepalette(gx_device_pm *);
private void pm_update(gx_device_pm *);
private uint pm_set_bits_per_pixel(gx_device_pm *, int);
private uint pm_palette_size(gx_device_pm *);
private int pm_alloc_bitmap(gx_device_pm *, gx_device *);
private int pm_run_gspmdrv(gx_device_pm *);
private void pm_write_bmp(gx_device_pm *);

/* Open the pm driver */
int
pm_open(gx_device *dev)
{
	int ccode;
	CHAR id[128];
	CHAR name[128];
	PTIB pptib;
	PPIB pppib;

	if (_osmode == DOS_MODE) {
		fprintf(stderr,"os2pm driver can't be used under DOS\n");
		return gs_error_limitcheck;
	}

	if (DosGetInfoBlocks(&pptib, &pppib)) {
		fprintf(stderr,"\npm_open: Couldn't get pid\n");
		return gs_error_limitcheck;
	}
	pmdev->gspid = pppib->pib_ulppid; /* use parent (CMD.EXE) pid */
	sprintf(id, ID_NAME, pmdev->gspid, (ULONG)dev);

	/* Allocate, but don't commit, enough memory for the largest */
	/* possible bitmap (13Mbytes = A3 x 150dpi x 24bits) */
	/* Shared memory is common to all processes so we don't want to allocate too much */
	sprintf(name, SHARED_NAME, *pmdev->GSVIEW ? pmdev->GSVIEW : id);
	if (DosAllocSharedMem((PPVOID)&pmdev->bitmap, name,
		13*1024*1024, PAG_READ | PAG_WRITE)) {
		fprintf(stderr,"pm_open: failed allocating shared BMP memory %s\n", name);
		return gs_error_limitcheck;
	}
	
	/* commit one page so there is enough storage for a */
	/* bitmap header and palette */
	if (DosSetMem(pmdev->bitmap, MIN_COMMIT, PAG_COMMIT | PAG_DEFAULT)) {
		DosFreeMem(pmdev->bitmap);
		fprintf(stderr,"pm_open: failed committing shared BMP memory\n");
		return gs_error_limitcheck;
	}
	pmdev->committed = MIN_COMMIT;

	if (*pmdev->GSVIEW) {
	    APIRET rc;
	    /* Ghostview has already created the necessary objects */
	    /* so we use Open instead of Create */
	    rc = 0;
	    if (!rc) {
	    	sprintf(name, NEXT_NAME, pmdev->GSVIEW);
	        rc = DosOpenEventSem(name, &pmdev->next_event);
	    }
	    if (!rc) {
		sprintf(name, MUTEX_NAME, pmdev->GSVIEW);
		rc = DosOpenMutexSem(name, &pmdev->bmp_mutex);
	    }
	    if (!rc) {
		PID owner_pid;
	        sprintf(name, QUEUE_NAME, pmdev->GSVIEW);
		rc = DosOpenQueue(&owner_pid, &pmdev->drv_queue, name);
	    }
	    if (rc) {
		DosFreeMem(pmdev->bitmap);
		DosCloseEventSem(pmdev->next_event);
		fprintf(stderr, "pm_open: failed to open %s, rc = %u\n", name, rc);
		return gs_error_limitcheck;
	    }
	}
	else {
	    /* Create update event semaphore */
	    sprintf(name, SYNC_NAME, id);
	    if (DosCreateEventSem(name, &(pmdev->sync_event), 0, FALSE)) {
		DosFreeMem(pmdev->bitmap);
		fprintf(stderr,"pm_open: failed to create event semaphore %s\n", name);
		return gs_error_limitcheck;
	    }
	    /* Create mutex - used for preventing gspmdrv from accessing */
	    /* bitmap while we are changing the bitmap size. Initially unowned. */
	    sprintf(name, MUTEX_NAME, id);
	    if (DosCreateMutexSem(name, &(pmdev->bmp_mutex), 0, FALSE)) {
		DosFreeMem(pmdev->bitmap);
		DosCloseEventSem(pmdev->sync_event);
		DosCloseQueue(pmdev->drv_queue);
		fprintf(stderr,"pm_open: failed to create mutex semaphore %s\n", name);
		return gs_error_limitcheck;
	    }
	}

	if ( (pm_set_bits_per_pixel(pmdev, pmdev->BitsPerPixel) < 0) ||
	     (gdev_mem_device_for_bits(dev->color_info.depth) == 0) )
	{
		if (*pmdev->GSVIEW) {
		    DosCloseQueue(pmdev->drv_queue);
		    DosCloseEventSem(pmdev->next_event);
		}
		else
		    DosCloseEventSem(pmdev->sync_event);
		DosFreeMem(pmdev->bitmap);
		DosCloseMutexSem(pmdev->bmp_mutex);
		return gs_error_limitcheck;
	}

	/* initialise bitmap header */
	pmdev->bmi = (PBITMAPINFO2)pmdev->bitmap;
	pmdev->bmi->cbFix = 40;		/* OS/2 2.0 and Windows 3.0 compatible */
	pmdev->bmi->cx = dev->width;
	pmdev->bmi->cy = dev->height;
	pmdev->bmi->cPlanes = 1;
	pmdev->bmi->cBitCount = dev->color_info.depth;
	pmdev->bmi->ulCompression = BCA_UNCOMP;
	pmdev->bmi->cbImage = 0;
	pmdev->bmi->cxResolution = (ULONG)(dev->x_pixels_per_inch / 25.4 * 1000);
	pmdev->bmi->cyResolution = (ULONG)(dev->y_pixels_per_inch / 25.4 * 1000);
	pmdev->bmi->cclrUsed = 1<<(pmdev->BitsPerPixel);
	pmdev->bmi->cclrImportant = pmdev->nColors;

	pm_makepalette(pmdev);

	/* commit pages */
	ccode = pm_alloc_bitmap((gx_device_pm *)dev, dev);
	if (ccode < 0) {
		if (*pmdev->GSVIEW) {
		    DosCloseQueue(pmdev->drv_queue);
		    DosCloseEventSem(pmdev->next_event);
		}
		else
		    DosCloseEventSem(pmdev->sync_event);
		DosFreeMem(pmdev->bitmap);
		DosCloseMutexSem(pmdev->bmp_mutex);
		return ccode;
	}

	if (*pmdev->GSVIEW)
		return 0;	/* Ghostview will handle displaying */

	ccode = pm_run_gspmdrv(pmdev);
	if (ccode < 0) {
		DosFreeMem(pmdev->bitmap);
		DosCloseEventSem(pmdev->sync_event);
		DosCloseMutexSem(pmdev->bmp_mutex);
	}
		
	return ccode;
}

/* Get the initial matrix.  BMPs, unlike most displays, */
/* put (0,0) in the lower left corner. */
private void
pm_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
{	pmat->xx = dev->x_pixels_per_inch / 72.0;
	pmat->xy = 0;
	pmat->yx = 0;
	pmat->yy = dev->y_pixels_per_inch / 72.0;
	pmat->tx = 0;
	pmat->ty = 0;
	if (*pmdev->GSVIEW)
	   pm_update((gx_device_pm *)dev);  /* let GSVIEW know we are drawing */
}

/* Make the output appear on the screen. */
int
pm_sync_output(gx_device *dev)
{
    /* tell gspmdrv or Ghostview process to update display */
    if (*pmdev->GSVIEW) {
	APIRET rc;
	rc = DosWriteQueue(pmdev->drv_queue, GS_SYNC, 0, NULL, 0);
	if (rc)
	    fprintf(stderr,"pm_sync_output: DosWriteQueue error %d\n",rc);
    }
    else {
    	if (pmdev->updating)
    	    DosStopTimer(pmdev->update_timer);
	DosPostEventSem(pmdev->sync_event);
    }
    pmdev->updating = FALSE;
    return(0);
}

/* Make the output appear on the screen  */
/* and bring image window to foreground. */
int
pm_output_page(gx_device *dev, int copies, int flush)
{
int code;
APIRET rc;
#ifdef DEBUG
	pm_write_bmp(pmdev);
#endif
	if (*pmdev->GSVIEW) {
	    ULONG count;
	    pmdev->updating = FALSE;
	    /* signal Ghostview that another page is ready */
    	    rc = DosWriteQueue(pmdev->drv_queue, GS_PAGE, 0, NULL, 0);
    	    if (rc)
		fprintf(stderr,"pm_output_page: DosWriteQueue error %d\n",rc);
	    /* wait for Ghostview to signal we can move on to next page */
	    DosWaitEventSem(pmdev->next_event, SEM_INDEFINITE_WAIT);
	    DosResetEventSem(pmdev->next_event, &count);
	    code = 0;
	}
	else {
	    code = pm_sync_output(dev);
	    rc = DosSelectSession(pmdev->session_id);
	    if (rc) {
	      DosSleep(2000);	/* give gspmdrv.exe a chance to run */
	      rc = DosSelectSession(pmdev->session_id);
	      if (rc == ERROR_SMG_NO_TARGET_WINDOW) {
	          DosSleep(5000);	/* give gspmdrv.exe a chance to run */
	          rc = DosSelectSession(pmdev->session_id);  /* try yet again */
	      }
	      if ( (rc == ERROR_SMG_SESSION_NOT_FOUND) ||
		 (rc == ERROR_SMG_INVALID_SESSION_ID) ){
		/* someone has killed the session */
		REQUESTDATA Request;
		ULONG DataLength;
		PVOID DataAddress;
		PULONG QueueEntry;
		BYTE ElemPriority;
		/* Close gspmdrv driver */
		DosStopSession(STOP_SESSION_SPECIFIED, pmdev->session_id);
		Request.pid = pmdev->gspid;
		Request.ulData = 0;
		/* wait for termination queue, queue is then closed by session manager */
		DosReadQueue(pmdev->term_queue, &Request, &DataLength, 
			&DataAddress, 0, DCWW_WAIT, &ElemPriority, (HEV)NULL);
		DosCloseQueue(pmdev->term_queue);
		pmdev->term_queue = (HQUEUE)0;
		/* restart it */
		pm_run_gspmdrv(pmdev);
	        DosSleep(2000);	/* give gspmdrv.exe a chance to run */
	        rc = DosSelectSession(pmdev->session_id);
	      }
	      if (rc == ERROR_SMG_SESSION_NOT_FOREGRND)
		DosBeep(400,50);
	      else if (rc)
		fprintf(stderr,"pm_output_page: Select Session error code %u\n", rc);
	    }
	}
	return code;
}

/* Close the pm driver */
int
pm_close(gx_device *dev)
{
APIRET rc;
	if (*pmdev->GSVIEW) {
	    if (gs_exit_status) {
		ULONG count;
	        /* pause so error messages can be read */
	        DosResetEventSem(pmdev->next_event, &count);
	        DosWriteQueue(pmdev->drv_queue, GS_ERROR, 0, NULL, 0);
	        DosWaitEventSem(pmdev->next_event, SEM_INDEFINITE_WAIT);
	        DosResetEventSem(pmdev->next_event, &count);
	    }
    	    rc = DosWriteQueue(pmdev->drv_queue, GS_CLOSE, 0, NULL, 0);
    	    if (rc)
		fprintf(stderr,"pm_close: DosWriteQueue error %d\n",rc);
	}
	else {
	    REQUESTDATA Request;
	    ULONG DataLength;
	    PVOID DataAddress;
	    PULONG QueueEntry;
	    BYTE ElemPriority;
	    /* Close gspmdrv driver */
	    DosStopSession(STOP_SESSION_SPECIFIED, pmdev->session_id);
	    Request.pid = pmdev->gspid;
	    Request.ulData = 0;
	    /* wait for termination queue, queue is then closed by session manager */
	    DosReadQueue(pmdev->term_queue, &Request, &DataLength, 
		&DataAddress, 0, DCWW_WAIT, &ElemPriority, (HEV)NULL);
	}
	/* release memory */
	DosFreeMem(pmdev->bitmap);
	pmdev->bitmap = (unsigned char *)NULL;
	pmdev->committed = 0;
	/* close objects */
	if (*pmdev->GSVIEW) {
	    DosCloseQueue(pmdev->drv_queue);
	    DosCloseEventSem(pmdev->next_event);
	}
	else {
	    DosCloseEventSem(pmdev->sync_event);
	    /* stop update timer */
	    if (pmdev->updating)
	        DosStopTimer(pmdev->update_timer);
	    pmdev->updating = FALSE;
	}
	DosCloseMutexSem(pmdev->bmp_mutex);

	return(0);
}


/* Map a r-g-b color to the colors available under PM */
gx_color_index
pm_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  gx_color_value b)
{
	switch(pmdev->BitsPerPixel) {
	  case 24:
		return	((b >> (gx_color_value_bits - 8)) << 16) +
			((g >> (gx_color_value_bits - 8)) << 8) +
			((r >> (gx_color_value_bits - 8)));
	  case 16:
		return	(pm_color_value(b) << 16) +
			(pm_color_value(g) << 8) +
			(pm_color_value(r));
	  case 8: {
		int i;
		RGB2 *prgb;
		byte cr, cg, cb;

		/* map colors to 0->255 in 32 steps */
		cr = pm_color_value(r);
		cg = pm_color_value(g);
		cb = pm_color_value(b);

		prgb = (RGB2 *)((PBYTE)pmdev->bmi + pmdev->bmi->cbFix);
		/* search in palette */
		for ( i = 0; i < pmdev->nColors; i++, prgb++ )
		{	if ( !((cr ^ prgb->bRed) & 0xf8) &&
			     !((cg ^ prgb->bGreen) & 0xf8) &&
			     !((cb ^ prgb->bBlue) & 0xf8)
			   )
				return((gx_color_index)i);	/* found it */
		}

		/* next try adding it to palette */
		if (i < 230) { /* allow 26 for PM and other apps */
			prgb->bRed = cr;
			prgb->bGreen = cg;
			prgb->bBlue = cb;
			prgb->fcOptions = 0;
			pmdev->nColors = i+1;
			pmdev->bmi->cclrImportant = pmdev->nColors;
    			if (*pmdev->GSVIEW) {
			    APIRET rc;
			    rc = DosWriteQueue(pmdev->drv_queue, GS_PALCHANGE, 0, NULL, 0);
			    if (rc)
	    			fprintf(stderr,"pm_sync_output: DosWriteQueue error %d\n",rc);
    			}
			return((gx_color_index)i);	/* return new palette index */
		}

		return(gx_no_color_index);  /* not found - dither instead */
		}
	  case 4:
		if ((r == g) && (g == b) && (r >= gx_max_color_value / 3 * 2 - 1)
		   && (r < gx_max_color_value / 4 * 3))
			return ((gx_color_index)8);	/* light gray */
		return pc_4bit_map_rgb_color(dev, r, g, b);
	}
	return (gx_default_map_rgb_color(dev,r,g,b));
}

/* Map a color code to r-g-b. */
int
pm_map_color_rgb(gx_device *dev, gx_color_index color,
  gx_color_value prgb[3])
{
gx_color_value one;
	switch(pmdev->BitsPerPixel) {
	  case 24:
	  case 16:
		/* untested */
		one = (gx_color_value) (gx_max_color_value / 255);
		prgb[0] = ((color)     & 255) * one;
		prgb[1] = ((color>>8)  & 255) * one;
		prgb[2] = ((color>>16) & 255) * one;
		break;
	  case 8:
		one = (gx_color_value) (gx_max_color_value / 255);
		prgb[0] = pmdev->bmi->argbColor[(int)color].bRed * one;
		prgb[1] = pmdev->bmi->argbColor[(int)color].bGreen * one;
		prgb[2] = pmdev->bmi->argbColor[(int)color].bBlue * one;
		break;
	  case 4:
		if (color == 8)	/* VGA light gray */
		    prgb[0] = prgb[1] = prgb[2] = (gx_max_color_value / 4 * 3);
		else
		    pc_4bit_map_color_rgb(dev, color, prgb);
		break;
	  default:
		prgb[0] = prgb[1] = prgb[2] = 
			(int)color ? gx_max_color_value : 0;
	}
	return 0;
}

#define pmmdev ((gx_device *)&pmdev->mdev)
#define pmmproc(proc) (*pmdev->mdev.procs->proc)

/* Fill a rectangle. */
private int
pm_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  gx_color_index color)
{
	pmmproc(fill_rectangle)(pmmdev, x, y, w, h, color);
	pm_update((gx_device_pm *)dev);
	return 0;
}

/* Copy a monochrome bitmap.  The colors are given explicitly. */
/* Color = gx_no_color_index means transparent (no effect on the image). */
private int
pm_copy_mono(gx_device *dev,
  const byte *base, int sourcex, int raster, gx_bitmap_id id,
  int x, int y, int w, int h,
  gx_color_index zero, gx_color_index one)
{
	pmmproc(copy_mono)(pmmdev, base, sourcex, raster, id,
					 x, y, w, h, zero, one);
	pm_update((gx_device_pm *)dev);
	return 0;
}

/* Copy a color pixel map.  This is just like a bitmap, except that */
/* each pixel takes 8 or 4 bits instead of 1 when device driver has color. */
private int
pm_copy_color(gx_device *dev,
  const byte *base, int sourcex, int raster, gx_bitmap_id id,
  int x, int y, int w, int h)
{
	pmmproc(copy_color)(pmmdev, base, sourcex, raster, id,
					  x, y, w, h);
	pm_update((gx_device_pm *)dev);
	return 0;
}


/* Standard properties for PM. */
private const gs_prop_item pm_props_std[] = {
	prop_def("HWResolution", prt_float_array),
	prop_def("HWSize", prt_int_array),
	prop_def("BitsPerPixel", prt_int),
		/* Slots for arrays */
	prop_float, prop_float,
	prop_int, prop_int
};

/* Extra properties for PM */
private const gs_prop_item pm_props_extra[] = {
	prop_def("BitsPerPixel", prt_int),
	prop_def("UpdateInterval", prt_int),
	prop_def("GSVIEW", prt_string)
};


/* Get PM properties */
int
pm_get_props(gx_device *dev, gs_prop_item *plist)
{	int start = gx_default_get_props(dev, plist);
	if ( plist != 0 )
	   {	register gs_prop_item *pi = plist + start;
		memcpy(pi, pm_props_extra, sizeof(pm_props_extra));
		pi[0].value.i = pmdev->BitsPerPixel;
		pi[1].value.i = pmdev->UpdateInterval;
		pi[2].value.a.p.s = pmdev->GSVIEW;
		pi[2].value.a.size = -1;
	   }
	return start + sizeof(pm_props_extra) / sizeof(gs_prop_item);
}

/* Set PM properties -- size and resolution. */
/* We implement this ourselves so that we can do it without */
/* closing and opening the device. */
/* Also set BitsPerPixel and GSVIEW if device not open */
int
pm_put_props(gx_device *dev, gs_prop_item *plist, int count)
{	gs_prop_item *known[3];
	int code = 0;
	gx_device_pm temp_dev;
	temp_dev = *pmdev;
	props_extract(plist, count, pm_props_std, 2, known, 0);
	temp_dev = *pmdev;
	if ( known[1] != 0 )
	   {	if ( known[1]->value.a.size != 2 )
			known[1]->status = pv_typecheck,
			code = gs_error_typecheck;
		else
		   {	gs_prop_item *ap = known[1]->value.a.p.v;
			if ( ap[0].value.i <= 0 || ap[0].value.i > 0x7fff ||
			     ap[1].value.i <= 0 || ap[1].value.i > 0x7fff
			   )
				known[1]->status = pv_rangecheck,
				code = gs_error_rangecheck;
			else
			   {	temp_dev.width = ap[0].value.i;
				temp_dev.height = ap[1].value.i;
			   }
			if ( code == 0 ) code = 1;
		   }
	   }
	if ( known[0] != 0 )
	   {	if ( known[0]->value.a.size != 2 )
			known[0]->status = pv_typecheck,
			code = gs_error_typecheck;
		else
		   {	gs_prop_item *ap = known[0]->value.a.p.v;
			if ( ap[0].value.f <= 0 || ap[1].value.f <= 0 )
				known[0]->status = pv_rangecheck,
				code = gs_error_rangecheck;
			else
			   {	temp_dev.x_pixels_per_inch = ap[0].value.f;
				temp_dev.y_pixels_per_inch = ap[1].value.f;
			   }
			if ( code == 0 ) code = 1;
		   }
	   }
	if ( code < 0 )
		return_error(code);

	props_extract(plist, count, pm_props_extra, 3, known, 1);
	/* get extra properties */
	if ( known[2] != 0 )
   	   {	gs_prop_item *pn = known[2];
	        int size = pn->value.a.size;
		if ( dev->is_open ) {
		    known[2]->status = pv_limitcheck;
		    code = gs_error_limitcheck;
		} else {
		    if ( size >= pm_gsview_sizeof ) {
			pn->status = pv_limitcheck,
			code = gs_error_limitcheck;
		    }
		    else {
			memcpy(temp_dev.GSVIEW, pn->value.a.p.s, size);
			temp_dev.GSVIEW[size] = 0;
		    }
		}
	   }
	if ( known[1] != 0 )
	   {
		if ( dev->is_open ) {
			known[1]->status = pv_limitcheck;
			code = gs_error_limitcheck;
		}
		else if ( known[1]->value.i < 0 ) {
			known[1]->status = pv_rangecheck;
			code = gs_error_rangecheck;
		}
		else {
			temp_dev.UpdateInterval = known[1]->value.i;
		}
	   }
	if ( known[0] != 0 )
	   {
		if ( dev->is_open ) {
		    known[0]->status = pv_limitcheck;
		    code = gs_error_limitcheck;
		}
		else if ( known[0]->value.i < 0 ) {
			known[0]->status = pv_rangecheck;
			code = gs_error_rangecheck;
		}
		else {
			code = pm_set_bits_per_pixel(&temp_dev, known[0]->value.i);
			if (code < 0)
				return_error(code);
			code = 1;
		}
	   }

	if ( code < 0 )
		return_error(code);

	/* obtain mutex - to prevent gspmdrv from using bitmap */
	/* while we change its size */
	DosRequestMutexSem(pmdev->bmp_mutex, 20000);
	if ( dev->is_open && code )
	{	int ccode;
		ccode = pm_alloc_bitmap(pmdev, (gx_device *)&temp_dev);
		if ( ccode < 0 )
		{	pm_alloc_bitmap(pmdev, dev);
			DosReleaseMutexSem(pmdev->bmp_mutex);
			return ccode;
		}
	}
	dev->x_pixels_per_inch = temp_dev.x_pixels_per_inch;
	dev->y_pixels_per_inch = temp_dev.y_pixels_per_inch;
	dev->width = temp_dev.width;
	dev->height = temp_dev.height;
	dev->color_info = temp_dev.color_info;
	pmdev->BitsPerPixel = temp_dev.BitsPerPixel;
	pmdev->UpdateInterval = temp_dev.UpdateInterval;
	strcpy(pmdev->GSVIEW, temp_dev.GSVIEW);

	if ( dev->is_open && code ) {
	    /* need to update bitmap info header also */
	    pmdev->bmi->cx = dev->width;
	    pmdev->bmi->cy = dev->height;
	    /* update bit count and palette */
	    pmdev->bmi->cBitCount = dev->color_info.depth;
	    pmdev->bmi->cclrUsed = 1<<(pmdev->BitsPerPixel);
	    pmdev->bmi->cclrImportant = pmdev->nColors;
	    pm_makepalette(pmdev);
	    /* erase bitmap - before window gets redrawn */
	    (*dev->procs->fill_rectangle)(dev, 0, 0, dev->width, dev->height,
		pm_map_rgb_color(dev, gx_max_color_value, 
		    gx_max_color_value, gx_max_color_value));
	    /* cause scroll bars to be redrawn */
	    /* need to signal gspmdrv that bitmap size has changed */
	    /* or perhaps gspmdrv can check if the bitmap size has */
	    /* before each use */
	}

	/* release bmp mutex */
	DosReleaseMutexSem(pmdev->bmp_mutex);
	return 0;
}

/* ------ Internal routines ------ */

#undef pmdev


/* start gspmdrv.exe */
private int
pm_run_gspmdrv(gx_device_pm *pmdev)
{
	int ccode;
	PCHAR pdrvname = "gspmdrv.exe";
	CHAR error_message[256];
	CHAR term_queue_name[128];
	CHAR id[128];
	CHAR arg[1024];
	STARTDATA sdata;
	APIRET rc;
	PTIB pptib;
	PPIB pppib;
	CHAR progname[256];
	PCHAR tail;

	sprintf(id, ID_NAME, pmdev->gspid, (ULONG)pmdev);

	/* Create termination queue - used to find out when gspmdrv terminates */
	sprintf(term_queue_name, "\\QUEUES\\TERMQ_%s", id);
	if (DosCreateQueue(&(pmdev->term_queue), QUE_FIFO, term_queue_name)) {
		fprintf(stderr,"pm_run_gspmdrv: failed to create termination queue\n");
		return gs_error_limitcheck;
	}

	/* get full path to gsos2.exe and hence path to gspmdrv.exe */
	if ( (rc = DosGetInfoBlocks(&pptib, &pppib)) != 0 ) {
		fprintf(stderr,"pm_run_gspmdrv: Couldn't get module handle, rc = %d\n", rc);
		return gs_error_limitcheck;
	}
	if ( (rc = DosQueryModuleName(pppib->pib_hmte, 256, progname)) != 0 ) {
		fprintf(stderr,"pm_run_gspmdrv: Couldn't get module name, rc = %d\n", rc);
		return gs_error_limitcheck;
	}
	if ((tail = strrchr(progname,'\\')) != (PCHAR)NULL)
	{
		tail++;
		*tail = '\0';
	}
	else 
		tail = progname;
	strcat(progname, pdrvname);

	/* Open the PM driver session gspmdrv.exe */
	/* arguments are: */
	/*  (1) -d (display) option */
	/*  (2) id string */
	sprintf(arg, "-d %s", id);

	/* because gspmdrv.exe is a different EXE type to gs.exe, 
	 * we must use start session not DosExecPgm() */
	sdata.Length = sizeof(sdata);
	sdata.Related = SSF_RELATED_CHILD;	/* to be a child  */
	sdata.FgBg = SSF_FGBG_BACK;		/* start in background */
	sdata.TraceOpt = 0;
	sdata.PgmTitle = "Ghostscript PM driver session";
	sdata.PgmName = progname;
	sdata.PgmInputs = arg;
	sdata.TermQ = term_queue_name;
	sdata.Environment = pppib->pib_pchenv;	/* use Parent's environment */
	sdata.InheritOpt = 0;			/* Can't inherit from parent because different sesison type */
	sdata.SessionType = SSF_TYPE_DEFAULT;	/* default is PM */
	sdata.IconFile = NULL;
	sdata.PgmHandle = 0;
	sdata.PgmControl = 0;
	sdata.InitXPos = 0;
	sdata.InitYPos = 0;
	sdata.InitXSize = 0;
	sdata.InitYSize = 0;
	sdata.ObjectBuffer = error_message;
	sdata.ObjectBuffLen = sizeof(error_message);

	rc = DosStartSession(&sdata, &pmdev->session_id, &pmdev->process_id);
	if (rc == ERROR_FILE_NOT_FOUND) {
	    sdata.PgmName = pdrvname;
	    rc = DosStartSession(&sdata, &pmdev->session_id, &pmdev->process_id);
	}
	if (rc) {
	    fprintf(stderr,"pm_run_gspmdrv: failed to run %s, rc = %d\n", sdata.PgmName, rc);
	    fprintf(stderr,"pm_run_gspmdrv: error_message: %s\n", error_message);
	    return gs_error_limitcheck;
	}

	return 0;

}

/* Allocate the backing bitmap. */
private int
pm_alloc_bitmap(gx_device_pm *pmdev, gx_device *param_dev)
{
	gx_device_memory mdev;
	byte *base;
	ulong data_size;
	uint ptr_size;
	uint pal_size;
	uint raster;
	ULONG rc;
	ULONG needed;

	/* Finish initializing the bitmap. */

	mdev = *gdev_mem_device_for_bits(pmdev->color_info.depth);
	mdev.width = param_dev->width;
	mdev.height = param_dev->height;
	mdev.target = (gx_device *)pmdev;
	/* BMP files need width rounded up so that a scan line is */
	/* a multiple of 4 bytes. */
	/* This is currently done by gdev_mem_raster(). */
	/* It may be better to do it here explicitly in case */
	/* gdev_mem_raster changes. */
	raster = gdev_mem_raster(&mdev);
	data_size = (ulong)raster * mdev.height;
	ptr_size = sizeof(byte **) * mdev.height;
	pal_size = pm_palette_size(pmdev);
	needed = pmdev->bmi->cbFix + pal_size + data_size + ptr_size;
	/* round up to page boundary */
	needed = (needed + MIN_COMMIT - 1) & (~(MIN_COMMIT-1));
	if (needed > pmdev->committed) {
		/* commit more memory */
		if (rc = DosSetMem(pmdev->bitmap + pmdev->committed, 
			needed - pmdev->committed, 
			PAG_COMMIT | PAG_DEFAULT)) {
			fprintf(stderr,"No memory in pm_alloc_bitmap, rc = %d\n",rc);
			return gs_error_limitcheck;
		}
		pmdev->committed = needed;
	}
	/* Shared memory can't be decommitted */

	/* Nothing can go wrong now.... */
	base = pmdev->bitmap + pmdev->bmi->cbFix + pm_palette_size(pmdev);
	pmdev->mdev = mdev;
	pmdev->mdev.base = (byte *)base;
	(*pmdev->mdev.procs->open_device)((gx_device *)&pmdev->mdev);
	return 0;
}

private void
pm_makepalette(gx_device_pm *pmdev)
{
	int i, val;
	RGB2 *argb = (RGB2 *)( (PBYTE)pmdev->bmi + pmdev->bmi->cbFix );
	if (pmdev->BitsPerPixel > 8)
		return;	/* these don't use a palette */

	for (i=0; i<pmdev->nColors; i++) {
	  switch (pmdev->nColors) {
		case 64:
		  /* colors are rrggbb */
		  argb[i].bRed   = ((i & 0x30)>>4)*85;
		  argb[i].bGreen = ((i & 0xC)>>2)*85;
		  argb[i].bBlue  = (i & 3)*85;
		  argb[i].fcOptions = 0;
		  /* zero unused entries */
		  argb[i+64].bRed = argb[i+64].bGreen = argb[i+64].bBlue  = 0;
		  argb[i+64].fcOptions = 0;
		  argb[i+128].bRed = argb[i+128].bGreen = argb[i+128].bBlue  = 0;
		  argb[i+128].fcOptions = 0;
		  argb[i+192].bRed = argb[i+192].bGreen = argb[i+192].bBlue  = 0;
		  argb[i+192].fcOptions = 0;
		  break;
		case 16:
		  /* colors are irgb */
		  val = (i & 8 ? 255 : 128);
		  argb[i].bRed   = i & 4 ? val : 0;
		  argb[i].bGreen = i & 2 ? val : 0;
		  argb[i].bBlue  = i & 1 ? val : 0;
		  if (i == 8) { /* light gray */
		      argb[i].bRed   = 
		      argb[i].bGreen = 
		      argb[i].bBlue  = 192;
		      argb[i].fcOptions = 0;
		  }
		  break;
		case 2:
		  argb[i].bRed =
		    argb[i].bGreen =
		    argb[i].bBlue = (i ? 255 : 0);
		  argb[i].fcOptions = 0;
		  break;
	  }
	}
}


/* Cause display to be updated periodically */
private void 
pm_update(gx_device_pm *pmdev)
{
	if (pmdev->updating)
		return;
	if (!pmdev->UpdateInterval)
		return;
	if (*pmdev->GSVIEW) {
	    APIRET rc;
	    rc = DosWriteQueue(pmdev->drv_queue, GS_UPDATING, 0, NULL, 0);
            if (rc)
	        fprintf(stderr,"pm_update: DosWriteQueue error %d\n",rc);
	}
	else {
	    DosStartTimer(pmdev->UpdateInterval, (HSEM)pmdev->sync_event, 
		&pmdev->update_timer);
	}
	pmdev->updating = TRUE;
}

private uint
pm_set_bits_per_pixel(gx_device_pm *pmdev, int bpp)
{
static const gx_device_color_info pm_24bit_color = dci_color(24,255,255);
static const gx_device_color_info pm_16bit_color = dci_color(24,31,31);
static const gx_device_color_info pm_8bit_color = dci_color(8,31,4);
static const gx_device_color_info pm_4bit_color = dci_vga;
static const gx_device_color_info pm_2color = dci_black_and_white;
	switch (bpp) {
	    case 24:
		pmdev->color_info = pm_24bit_color;
		pmdev->nColors = (1<<24);
		break;
	    case 16:
		/* bitmap is stored as 24 bits/pixel */
		pmdev->color_info = pm_16bit_color;
		pmdev->nColors = 32768;
		break;
	    case 8:
		/* use 64 static colors and 166 dynamic colors from 8 planes */
		pmdev->color_info = pm_8bit_color;
		pmdev->nColors = 64;
		break;
	    case 4:
		pmdev->color_info = pm_4bit_color;
		pmdev->nColors = 16;
		break;
	    case 1:
		pmdev->color_info = pm_2color;
		pmdev->nColors = 2;
		break;
	    default:
		return (gs_error_rangecheck);
	}
	pmdev->BitsPerPixel = bpp;
	return 0;
}

/* return length of BMP palette in bytes */
private uint
pm_palette_size(gx_device_pm *pmdev)
{
	switch(pmdev->color_info.depth) {
	    case 24:
		return 0;
	    case 8:
		return 256*sizeof(RGB2);
	    case 4:
		return 16*sizeof(RGB2);
	}
	/* must be two color */
	return 2*sizeof(RGB2);
}

/* This is used for testing */
/* Write out a BMP file to "out.bmp" */
private void 
pm_write_bmp(gx_device_pm *pmdev)
{
	BITMAPFILEHEADER2 bmfh;
	uint bmfh_length = sizeof(BITMAPFILEHEADER2) - sizeof(BITMAPINFOHEADER2);
	uint length;	/* bitmap length */
	ULONG fh;	/* file handle */
	ULONG action;
	ULONG count;
	bmfh.usType = 0x4d42;	/* "BM" */
	length = bmfh_length + pmdev->bmi->cbFix + pm_palette_size(pmdev)
	   + ( (gdev_mem_raster(&pmdev->mdev) * pmdev->mdev.height) );
	bmfh.cbSize = length;
	bmfh.xHotspot = bmfh.yHotspot = 0;
	bmfh.offBits = bmfh_length + pmdev->bmi->cbFix + pm_palette_size(pmdev);
	if (DosOpen("out.bmp",	/* filename */
		&fh,		/* pointer to handle */
		&action,	/* pointer to result */
		0,		/* initial length */
		FILE_NORMAL,	/* normal file */
		OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS,
		OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYREADWRITE,
		0)) {
		fprintf(stderr,"error opening out.bmp\n");
		return;
	}
	if (DosWrite(fh, (PBYTE)&bmfh, bmfh_length, &count))
		fprintf(stderr,"error writing header for out.bmp\n");
	if (DosWrite(fh, pmdev->bitmap, length, &count))
		fprintf(stderr,"error writing out.bmp\n");
	if (DosClose(fh))
		fprintf(stderr,"error closing out.bmp\n");
}
