/* Copyright (C) 1992, 1993, 1994 Aladdin Enterprises. All rights reserved. This file is part of Aladdin Ghostscript. Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or distributor accepts any responsibility for the consequences of using it, or for whether it serves any particular purpose or works at all, unless he or she says so in writing. Refer to the Aladdin Ghostscript Free Public License (the "License") for full details. Every copy of Aladdin Ghostscript must include a copy of the License, normally in a plain ASCII text file named PUBLIC. The License grants you the right to copy, modify and redistribute Aladdin Ghostscript, but only under certain conditions described in the License. Among other things, the License requires that the copyright notice and this notice 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 so a PM window for graphics can't be created. * The solution used here 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. * * If PM GSview starts Ghostscript, PM GSview displays the * bitmap instead of the PM driver (gspmdrv.exe). * * 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#define INCL_GPIBITMAPS #include <os2.h>#include "string_.h"#include <stdlib.h>#include "gx.h"#include "gserrors.h"#include "gsexit.h" /* for gs_exit_status */#include "gxdevice.h" #include "gp.h"#include "gpcheck.h"#include "gsparam.h"#include "gdevpccm.h" #include "gxdevmem.h"#include "gdevpm.h"#ifdef __DLL__#include "gsdll.h"#endif #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 80typedef struct gx_device_pm_s gx_device_pm; #define gx_device_pm_common\ int BitsPerPixel;\ int UpdateInterval;\ char GSVIEW[pm_gsview_sizeof];\ BOOL dll;\ 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_params(pm_get_params);private dev_proc_put_params(pm_put_params); 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_params, pm_put_params, gx_default_map_cmyk_color, gx_default_get_xfont_procs};#ifdef __DLL__gx_device_pm gs_os2dll_device = { std_device_std_body(gx_device_pm, &pm_procs, "os2dll", INITIAL_WIDTH, INITIAL_HEIGHT, INITIAL_RESOLUTION, INITIAL_RESOLUTION), { 0 }, /* std_procs */ 8, /* BitsPerPixel */ 5000, /* UpdateInterval */ "\0", /* GSVIEW */ 1 /* is DLL device */};#endifgx_device_pm gs_os2pm_device = { std_device_std_body(gx_device_pm, &pm_procs, "os2pm", INITIAL_WIDTH, INITIAL_HEIGHT, INITIAL_RESOLUTION, INITIAL_RESOLUTION), { 0 }, /* std_procs */ 8, /* BitsPerPixel */ 5000, /* UpdateInterval */ "\0", /* GSVIEW */ 0 /* is not DLL device */}; /* 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 */intpm_open(gx_device *dev){ int ccode; CHAR id[128]; CHAR name[128]; PTIB pptib; PPIB pppib; if (!pmdev->dll && (_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->dll) { /* Create mutex - used for preventing another thread 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; } } else { if (*pmdev->GSVIEW) { APIRET rc; /* GSview 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 { /* not GSVIEW */ /* 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->dll) { if (*pmdev->GSVIEW) { DosCloseQueue(pmdev->drv_queue); DosCloseEventSem(pmdev->next_event); } else DosCloseEventSem(pmdev->sync_event); } DosCloseMutexSem(pmdev->bmp_mutex); DosFreeMem(pmdev->bitmap); 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); if (pmdev->BitsPerPixel <= 8) { pmdev->bmi->cclrUsed = 1<<(pmdev->BitsPerPixel); pmdev->bmi->cclrImportant = pmdev->nColors; } else { pmdev->bmi->cclrUsed = 0; pmdev->bmi->cclrImportant = 0; } pm_makepalette(pmdev); /* commit pages */ ccode = pm_alloc_bitmap((gx_device_pm *)dev, dev); if (ccode < 0) { if (!pmdev->dll) { if (*pmdev->GSVIEW) { DosCloseQueue(pmdev->drv_queue); DosCloseEventSem(pmdev->next_event); } else DosCloseEventSem(pmdev->sync_event); } DosCloseMutexSem(pmdev->bmp_mutex); DosFreeMem(pmdev->bitmap); return ccode; } if (*pmdev->GSVIEW) return 0; /* GSview will handle displaying */ #ifdef __DLL__ if (pmdev->dll) { /* notify caller about new device */ (*pgsdll_callback)(GSDLL_DEVICE, (unsigned char *)pmdev, 1); return 0; /* caller will handle displaying */ }#endif 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 voidpm_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. */intpm_sync_output(gx_device *dev){#ifdef __DLL__ if (pmdev->dll) { (*pgsdll_callback)(GSDLL_SYNC, (unsigned char *)dev, 0); return 0; }#endif /* tell gspmdrv or GSview 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. */intpm_output_page(gx_device *dev, int copies, int flush){int code;APIRET rc;#ifdef DEBUG pm_write_bmp(pmdev);#endif#ifdef __DLL__ if (pmdev->dll) { (*pgsdll_callback)(GSDLL_PAGE, (unsigned char *)dev, 0); return 0; }#endif if (*pmdev->GSVIEW) { if (copies == -2) { rc = DosWriteQueue(pmdev->drv_queue, GS_END, 0, NULL, 0); if (rc) fprintf(stderr,"pm_output_page: DosWriteQueue error %d\n",rc); } else if (copies == -1) { rc = DosWriteQueue(pmdev->drv_queue, GS_BEGIN, 0, NULL, 0); if (rc) fprintf(stderr,"pm_output_page: DosWriteQueue error %d\n",rc); } else { ULONG count; pmdev->updating = FALSE; /* signal GSview 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 GSview 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 */intpm_close(gx_device *dev){APIRET rc;#ifdef __DLL__ if (pmdev->dll) { (*pgsdll_callback)(GSDLL_DEVICE, (unsigned char *)dev, 0); } else#endif { 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);Lines 1–500 of 1176. Load more
/* Copyright (C) 1992, 1993, 1994 Aladdin Enterprises. All rights reserved. This file is part of Aladdin Ghostscript. Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or distributor accepts any responsibility for the consequences of using it, or for whether it serves any particular purpose or works at all, unless he or she says so in writing. Refer to the Aladdin Ghostscript Free Public License (the "License") for full details. Every copy of Aladdin Ghostscript must include a copy of the License, normally in a plain ASCII text file named PUBLIC. The License grants you the right to copy, modify and redistribute Aladdin Ghostscript, but only under certain conditions described in the License. Among other things, the License requires that the copyright notice and this notice 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 so a PM window for graphics can't be created. * The solution used here 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. * * If PM GSview starts Ghostscript, PM GSview displays the * bitmap instead of the PM driver (gspmdrv.exe). * * 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 #define INCL_GPIBITMAPS #include <os2.h> #include "string_.h" #include <stdlib.h> #include "gx.h" #include "gserrors.h" #include "gsexit.h" /* for gs_exit_status */ #include "gxdevice.h" #include "gp.h" #include "gpcheck.h" #include "gsparam.h" #include "gdevpccm.h" #include "gxdevmem.h" #include "gdevpm.h" #ifdef __DLL__ #include "gsdll.h" #endif #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];\ BOOL dll;\ 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_params(pm_get_params); private dev_proc_put_params(pm_put_params); 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_params, pm_put_params, gx_default_map_cmyk_color, gx_default_get_xfont_procs }; #ifdef __DLL__ gx_device_pm gs_os2dll_device = { std_device_std_body(gx_device_pm, &pm_procs, "os2dll", INITIAL_WIDTH, INITIAL_HEIGHT, INITIAL_RESOLUTION, INITIAL_RESOLUTION), { 0 }, /* std_procs */ 8, /* BitsPerPixel */ 5000, /* UpdateInterval */ "\0", /* GSVIEW */ 1 /* is DLL device */ }; #endif gx_device_pm gs_os2pm_device = { std_device_std_body(gx_device_pm, &pm_procs, "os2pm", INITIAL_WIDTH, INITIAL_HEIGHT, INITIAL_RESOLUTION, INITIAL_RESOLUTION), { 0 }, /* std_procs */ 8, /* BitsPerPixel */ 5000, /* UpdateInterval */ "\0", /* GSVIEW */ 0 /* is not DLL device */ }; /* 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 (!pmdev->dll && (_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->dll) { /* Create mutex - used for preventing another thread 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; } } else { if (*pmdev->GSVIEW) { APIRET rc; /* GSview 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 { /* not GSVIEW */ /* 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->dll) { if (*pmdev->GSVIEW) { DosCloseQueue(pmdev->drv_queue); DosCloseEventSem(pmdev->next_event); } else DosCloseEventSem(pmdev->sync_event); } DosCloseMutexSem(pmdev->bmp_mutex); DosFreeMem(pmdev->bitmap); 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); if (pmdev->BitsPerPixel <= 8) { pmdev->bmi->cclrUsed = 1<<(pmdev->BitsPerPixel); pmdev->bmi->cclrImportant = pmdev->nColors; } else { pmdev->bmi->cclrUsed = 0; pmdev->bmi->cclrImportant = 0; } pm_makepalette(pmdev); /* commit pages */ ccode = pm_alloc_bitmap((gx_device_pm *)dev, dev); if (ccode < 0) { if (!pmdev->dll) { if (*pmdev->GSVIEW) { DosCloseQueue(pmdev->drv_queue); DosCloseEventSem(pmdev->next_event); } else DosCloseEventSem(pmdev->sync_event); } DosCloseMutexSem(pmdev->bmp_mutex); DosFreeMem(pmdev->bitmap); return ccode; } if (*pmdev->GSVIEW) return 0; /* GSview will handle displaying */ #ifdef __DLL__ if (pmdev->dll) { /* notify caller about new device */ (*pgsdll_callback)(GSDLL_DEVICE, (unsigned char *)pmdev, 1); return 0; /* caller will handle displaying */ } #endif 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) { #ifdef __DLL__ if (pmdev->dll) { (*pgsdll_callback)(GSDLL_SYNC, (unsigned char *)dev, 0); return 0; } #endif /* tell gspmdrv or GSview 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 #ifdef __DLL__ if (pmdev->dll) { (*pgsdll_callback)(GSDLL_PAGE, (unsigned char *)dev, 0); return 0; } #endif if (*pmdev->GSVIEW) { if (copies == -2) { rc = DosWriteQueue(pmdev->drv_queue, GS_END, 0, NULL, 0); if (rc) fprintf(stderr,"pm_output_page: DosWriteQueue error %d\n",rc); } else if (copies == -1) { rc = DosWriteQueue(pmdev->drv_queue, GS_BEGIN, 0, NULL, 0); if (rc) fprintf(stderr,"pm_output_page: DosWriteQueue error %d\n",rc); } else { ULONG count; pmdev->updating = FALSE; /* signal GSview 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 GSview 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; #ifdef __DLL__ if (pmdev->dll) { (*pgsdll_callback)(GSDLL_DEVICE, (unsigned char *)dev, 0); } else #endif { 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);