gdevpm.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. /* Copyright (C) 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gdevpm.c,v 1.10 2004/09/20 22:14:59 dan Exp $ */
  14. /*
  15. * OS/2 Presentation manager driver
  16. *
  17. * By Russell Lang (based on gdevmswn.c and gdevwdib.c)
  18. *
  19. * If Ghostscript is a PM application, stdin/stdout are not
  20. * provided and so no text window is available.
  21. * If Ghostscript is a windowed text application, a message queue
  22. * can't be created so a PM window for graphics can't be created.
  23. * The solution used here is to have two programs - gsos2.exe is a
  24. * text application and the outboard PM driver is gspmdrv.exe.
  25. * Another solution may be to make Ghostscript a PM application
  26. * and use VIO calls to provide a text window.
  27. *
  28. * If PM GSview starts Ghostscript, PM GSview displays the
  29. * bitmap instead of the PM driver (gspmdrv.exe).
  30. *
  31. * Since Ghostscript is not a PM application, this driver creates a
  32. * BMP bitmap in a named shared memory block and a second process
  33. * gspmdrv.exe reads this memory block and provides the PM window.
  34. * Communication to gspmdrv.exe is via the shared memory block
  35. * and semaphores.
  36. */
  37. #define INCL_DOS
  38. #define INCL_DOSERRORS
  39. #define INCL_WINWINDOWMGR
  40. #define INCL_DEV
  41. #define INCL_GPIBITMAPS
  42. #include <os2.h>
  43. #include "string_.h"
  44. #include <stdlib.h>
  45. #include "gx.h"
  46. #include "gserrors.h"
  47. #include "gxdevice.h"
  48. #include "gp.h"
  49. #include "gpcheck.h"
  50. #include "gsparam.h"
  51. #include "gdevpccm.h"
  52. #include "gxdevmem.h"
  53. #include "gdevpm.h"
  54. #ifdef __DLL__
  55. #include "gsdll.h"
  56. #include "gsdllos2.h"
  57. #endif
  58. #define MIN_COMMIT 4096 /* memory is committed in these size chunks */
  59. #define ID_NAME "GSPMDRV_%u_%u"
  60. /* Initial values for width and height */
  61. #define INITIAL_RESOLUTION 96
  62. #define INITIAL_WIDTH ((INITIAL_RESOLUTION * 85 + 5) / 10)
  63. #define INITIAL_HEIGHT ((INITIAL_RESOLUTION * 110 + 5) / 10)
  64. /* A macro for casting the device argument */
  65. #define pmdev ((gx_device_pm *)dev)
  66. #define pm_gsview_sizeof 80
  67. typedef struct gx_device_pm_s gx_device_pm;
  68. #define gx_device_pm_common\
  69. int BitsPerPixel;\
  70. int UpdateInterval;\
  71. char GSVIEW[pm_gsview_sizeof];\
  72. BOOL dll;\
  73. int nColors;\
  74. BOOL updating;\
  75. HTIMER update_timer;\
  76. HEV sync_event;\
  77. HEV next_event;\
  78. HMTX bmp_mutex;\
  79. HQUEUE drv_queue;\
  80. HQUEUE term_queue;\
  81. ULONG session_id;\
  82. PID process_id;\
  83. PID gspid;\
  84. unsigned char *bitmap;\
  85. ULONG committed;\
  86. PBITMAPINFO2 bmi
  87. /* The device descriptor */
  88. struct gx_device_pm_s {
  89. gx_device_common;
  90. gx_device_pm_common;
  91. gx_device_memory mdev;
  92. };
  93. /* Device procedures */
  94. /* See gxdevice.h for the definitions of the procedures. */
  95. private dev_proc_open_device(pm_open);
  96. private dev_proc_get_initial_matrix(pm_get_initial_matrix);
  97. private dev_proc_sync_output(pm_sync_output);
  98. private dev_proc_output_page(pm_output_page);
  99. private dev_proc_close_device(pm_close);
  100. private dev_proc_map_rgb_color(pm_map_rgb_color);
  101. private dev_proc_map_color_rgb(pm_map_color_rgb);
  102. private dev_proc_fill_rectangle(pm_fill_rectangle);
  103. private dev_proc_copy_mono(pm_copy_mono);
  104. private dev_proc_copy_color(pm_copy_color);
  105. private dev_proc_get_bits(pm_get_bits);
  106. private dev_proc_get_params(pm_get_params);
  107. private dev_proc_put_params(pm_put_params);
  108. private gx_device_procs pm_procs =
  109. {
  110. pm_open,
  111. pm_get_initial_matrix,
  112. pm_sync_output,
  113. pm_output_page,
  114. pm_close,
  115. pm_map_rgb_color,
  116. pm_map_color_rgb,
  117. pm_fill_rectangle,
  118. NULL, /* tile rectangle */
  119. pm_copy_mono,
  120. pm_copy_color,
  121. NULL, /* draw line */
  122. pm_get_bits,
  123. pm_get_params,
  124. pm_put_params,
  125. NULL, /* map_cmyk_color */
  126. gx_default_get_xfont_procs,
  127. NULL, /* get_xfont_device */
  128. NULL, /* map_rgb_alpha_color */
  129. gx_page_device_get_page_device
  130. };
  131. #ifdef __DLL__
  132. gx_device_pm far_data gs_os2dll_device =
  133. {
  134. std_device_std_body(gx_device_pm, &pm_procs, "os2dll",
  135. INITIAL_WIDTH, INITIAL_HEIGHT,
  136. INITIAL_RESOLUTION, INITIAL_RESOLUTION),
  137. {0}, /* std_procs */
  138. 24, /* BitsPerPixel */
  139. 5000, /* UpdateInterval */
  140. "\0", /* GSVIEW */
  141. 1 /* is DLL device */
  142. };
  143. #endif
  144. gx_device_pm far_data gs_os2pm_device =
  145. {
  146. std_device_std_body(gx_device_pm, &pm_procs, "os2pm",
  147. INITIAL_WIDTH, INITIAL_HEIGHT,
  148. INITIAL_RESOLUTION, INITIAL_RESOLUTION),
  149. {0}, /* std_procs */
  150. 24, /* BitsPerPixel */
  151. 5000, /* UpdateInterval */
  152. "\0", /* GSVIEW */
  153. 0 /* is not DLL device */
  154. };
  155. /* Compress a gx_color_value into an 8-bit PM color value, */
  156. /* using only the high order 5 bits. */
  157. #define pm_color_value(z)\
  158. ((((z) >> (gx_color_value_bits - 5)) << 3) +\
  159. ((z) >> (gx_color_value_bits - 3)))
  160. /* prototypes for internal procedures */
  161. private void pm_makepalette(gx_device_pm *);
  162. private void pm_update(gx_device_pm *);
  163. private uint pm_set_bits_per_pixel(gx_device_pm *, int);
  164. private uint pm_palette_size(gx_device_pm *);
  165. private int pm_alloc_bitmap(gx_device_pm *, gx_device *);
  166. private int pm_run_gspmdrv(gx_device_pm *);
  167. private void pm_write_bmp(gx_device_pm *);
  168. /* Open the pm driver */
  169. int
  170. pm_open(gx_device * dev)
  171. {
  172. int ccode;
  173. CHAR id[128];
  174. CHAR name[128];
  175. PTIB pptib;
  176. PPIB pppib;
  177. if (!pmdev->dll && (_osmode == DOS_MODE)) {
  178. eprintf("os2pm driver can't be used under DOS\n");
  179. return gs_error_limitcheck;
  180. }
  181. if (DosGetInfoBlocks(&pptib, &pppib)) {
  182. eprintf("\npm_open: Couldn't get pid\n");
  183. return gs_error_limitcheck;
  184. }
  185. #ifdef __DLL__
  186. if (pppib->pib_ultype == 3) /* if caller is PM app */
  187. pmdev->gspid = pppib->pib_ulpid; /* use caller pid */
  188. else
  189. #endif
  190. pmdev->gspid = pppib->pib_ulppid; /* use parent (CMD.EXE) pid */
  191. sprintf(id, ID_NAME, pmdev->gspid, (ULONG) dev);
  192. /* Allocate, but don't commit, enough memory for the largest */
  193. /* possible bitmap (13Mbytes = A3 x 150dpi x 24bits) */
  194. #ifdef __DLL__
  195. if (pmdev->dll) {
  196. /* We don't need to use shared memory for the DLL */
  197. if (DosAllocMem((PPVOID) & pmdev->bitmap,
  198. 13 * 1024 * 1024, PAG_READ | PAG_WRITE)) {
  199. eprintf("pm_open: failed allocating BMP memory\n");
  200. return gs_error_limitcheck;
  201. }
  202. } else
  203. #endif
  204. {
  205. /* Shared memory is common to all processes so we don't want to allocate too much */
  206. sprintf(name, SHARED_NAME, *pmdev->GSVIEW ? pmdev->GSVIEW : id);
  207. if (DosAllocSharedMem((PPVOID) & pmdev->bitmap, name,
  208. 13 * 1024 * 1024, PAG_READ | PAG_WRITE)) {
  209. eprintf1("pm_open: failed allocating shared BMP memory %s\n", name);
  210. return gs_error_limitcheck;
  211. }
  212. }
  213. /* commit one page so there is enough storage for a */
  214. /* bitmap header and palette */
  215. if (DosSetMem(pmdev->bitmap, MIN_COMMIT, PAG_COMMIT | PAG_DEFAULT)) {
  216. DosFreeMem(pmdev->bitmap);
  217. eprintf("pm_open: failed committing BMP memory\n");
  218. return gs_error_limitcheck;
  219. }
  220. pmdev->committed = MIN_COMMIT;
  221. if (pmdev->dll) {
  222. /* Create mutex - used for preventing another thread from accessing */
  223. /* bitmap while we are changing the bitmap size. Initially unowned. */
  224. sprintf(name, MUTEX_NAME, id);
  225. if (DosCreateMutexSem(name, &(pmdev->bmp_mutex), 0, FALSE)) {
  226. DosFreeMem(pmdev->bitmap);
  227. DosCloseEventSem(pmdev->sync_event);
  228. DosCloseQueue(pmdev->drv_queue);
  229. eprintf1("pm_open: failed to create mutex semaphore %s\n", name);
  230. return gs_error_limitcheck;
  231. }
  232. } else {
  233. if (*pmdev->GSVIEW) {
  234. APIRET rc;
  235. /* GSview has already created the necessary objects */
  236. /* so we use Open instead of Create */
  237. rc = 0;
  238. if (!rc) {
  239. sprintf(name, NEXT_NAME, pmdev->GSVIEW);
  240. rc = DosOpenEventSem(name, &pmdev->next_event);
  241. }
  242. if (!rc) {
  243. sprintf(name, MUTEX_NAME, pmdev->GSVIEW);
  244. rc = DosOpenMutexSem(name, &pmdev->bmp_mutex);
  245. }
  246. if (!rc) {
  247. PID owner_pid;
  248. sprintf(name, QUEUE_NAME, pmdev->GSVIEW);
  249. rc = DosOpenQueue(&owner_pid, &pmdev->drv_queue, name);
  250. }
  251. if (rc) {
  252. DosFreeMem(pmdev->bitmap);
  253. DosCloseEventSem(pmdev->next_event);
  254. eprintf2("pm_open: failed to open %s, rc = %u\n", name, rc);
  255. return gs_error_limitcheck;
  256. }
  257. } else { /* not GSVIEW */
  258. /* Create update event semaphore */
  259. sprintf(name, SYNC_NAME, id);
  260. if (DosCreateEventSem(name, &(pmdev->sync_event), 0, FALSE)) {
  261. DosFreeMem(pmdev->bitmap);
  262. eprintf1("pm_open: failed to create event semaphore %s\n", name);
  263. return gs_error_limitcheck;
  264. }
  265. /* Create mutex - used for preventing gspmdrv from accessing */
  266. /* bitmap while we are changing the bitmap size. Initially unowned. */
  267. sprintf(name, MUTEX_NAME, id);
  268. if (DosCreateMutexSem(name, &(pmdev->bmp_mutex), 0, FALSE)) {
  269. DosFreeMem(pmdev->bitmap);
  270. DosCloseEventSem(pmdev->sync_event);
  271. DosCloseQueue(pmdev->drv_queue);
  272. eprintf1("pm_open: failed to create mutex semaphore %s\n", name);
  273. return gs_error_limitcheck;
  274. }
  275. }
  276. }
  277. if ((pm_set_bits_per_pixel(pmdev, pmdev->BitsPerPixel) < 0) ||
  278. (gdev_mem_device_for_bits(dev->color_info.depth) == 0)) {
  279. if (!pmdev->dll) {
  280. if (*pmdev->GSVIEW) {
  281. DosCloseQueue(pmdev->drv_queue);
  282. DosCloseEventSem(pmdev->next_event);
  283. } else
  284. DosCloseEventSem(pmdev->sync_event);
  285. }
  286. DosCloseMutexSem(pmdev->bmp_mutex);
  287. DosFreeMem(pmdev->bitmap);
  288. return gs_error_limitcheck;
  289. }
  290. /* initialise bitmap header */
  291. pmdev->bmi = (PBITMAPINFO2) pmdev->bitmap;
  292. pmdev->bmi->cbFix = 40; /* OS/2 2.0 and Windows 3.0 compatible */
  293. pmdev->bmi->cx = dev->width;
  294. pmdev->bmi->cy = dev->height;
  295. pmdev->bmi->cPlanes = 1;
  296. pmdev->bmi->cBitCount = dev->color_info.depth;
  297. pmdev->bmi->ulCompression = BCA_UNCOMP;
  298. pmdev->bmi->cbImage = 0;
  299. pmdev->bmi->cxResolution = (ULONG) (dev->x_pixels_per_inch / 25.4 * 1000);
  300. pmdev->bmi->cyResolution = (ULONG) (dev->y_pixels_per_inch / 25.4 * 1000);
  301. if (pmdev->BitsPerPixel <= 8) {
  302. pmdev->bmi->cclrUsed = 1 << (pmdev->BitsPerPixel);
  303. pmdev->bmi->cclrImportant = pmdev->nColors;
  304. } else {
  305. pmdev->bmi->cclrUsed = 0;
  306. pmdev->bmi->cclrImportant = 0;
  307. }
  308. pm_makepalette(pmdev);
  309. /* commit pages */
  310. ccode = pm_alloc_bitmap((gx_device_pm *) dev, dev);
  311. if (ccode < 0) {
  312. if (!pmdev->dll) {
  313. if (*pmdev->GSVIEW) {
  314. DosCloseQueue(pmdev->drv_queue);
  315. DosCloseEventSem(pmdev->next_event);
  316. } else
  317. DosCloseEventSem(pmdev->sync_event);
  318. }
  319. DosCloseMutexSem(pmdev->bmp_mutex);
  320. DosFreeMem(pmdev->bitmap);
  321. return ccode;
  322. }
  323. if (*pmdev->GSVIEW)
  324. return 0; /* GSview will handle displaying */
  325. #ifdef __DLL__
  326. if (pmdev->dll && pgsdll_callback) {
  327. /* notify caller about new device */
  328. (*pgsdll_callback) (GSDLL_DEVICE, (unsigned char *)pmdev, 1);
  329. return 0; /* caller will handle displaying */
  330. }
  331. #endif
  332. ccode = pm_run_gspmdrv(pmdev);
  333. if (ccode < 0) {
  334. DosFreeMem(pmdev->bitmap);
  335. DosCloseEventSem(pmdev->sync_event);
  336. DosCloseMutexSem(pmdev->bmp_mutex);
  337. }
  338. return ccode;
  339. }
  340. /* Get the initial matrix. BMPs, unlike most displays, */
  341. /* put (0,0) in the lower left corner. */
  342. private void
  343. pm_get_initial_matrix(gx_device * dev, gs_matrix * pmat)
  344. {
  345. pmat->xx = dev->x_pixels_per_inch / 72.0;
  346. pmat->xy = 0;
  347. pmat->yx = 0;
  348. pmat->yy = dev->y_pixels_per_inch / 72.0;
  349. pmat->tx = 0;
  350. pmat->ty = 0;
  351. if (*pmdev->GSVIEW)
  352. pm_update((gx_device_pm *) dev); /* let GSVIEW know we are drawing */
  353. }
  354. /* Make the output appear on the screen. */
  355. int
  356. pm_sync_output(gx_device * dev)
  357. {
  358. #ifdef __DLL__
  359. if (pmdev->dll && pgsdll_callback) {
  360. (*pgsdll_callback) (GSDLL_SYNC, (unsigned char *)dev, 0);
  361. return 0;
  362. }
  363. #endif
  364. /* tell gspmdrv or GSview process to update display */
  365. if (*pmdev->GSVIEW) {
  366. APIRET rc;
  367. rc = DosWriteQueue(pmdev->drv_queue, GS_SYNC, 0, NULL, 0);
  368. if (rc)
  369. eprintf1("pm_sync_output: DosWriteQueue error %d\n", rc);
  370. } else {
  371. if (pmdev->updating)
  372. DosStopTimer(pmdev->update_timer);
  373. DosPostEventSem(pmdev->sync_event);
  374. }
  375. pmdev->updating = FALSE;
  376. return (0);
  377. }
  378. /* Make the output appear on the screen */
  379. /* and bring image window to foreground. */
  380. private int
  381. pm_do_output_page(gx_device * dev, int copies, int flush)
  382. {
  383. int code;
  384. APIRET rc;
  385. #ifdef DEBUG
  386. pm_write_bmp(pmdev);
  387. #endif
  388. #ifdef __DLL__
  389. if (pmdev->dll && pgsdll_callback) {
  390. (*pgsdll_callback) (GSDLL_PAGE, (unsigned char *)dev, 0);
  391. return 0;
  392. }
  393. #endif
  394. if (*pmdev->GSVIEW) {
  395. if (copies == -2) {
  396. rc = DosWriteQueue(pmdev->drv_queue, GS_END, 0, NULL, 0);
  397. if (rc)
  398. eprintf1("pm_output_page: DosWriteQueue error %d\n", rc);
  399. } else if (copies == -1) {
  400. rc = DosWriteQueue(pmdev->drv_queue, GS_BEGIN, 0, NULL, 0);
  401. if (rc)
  402. eprintf1("pm_output_page: DosWriteQueue error %d\n", rc);
  403. } else {
  404. ULONG count;
  405. pmdev->updating = FALSE;
  406. /* signal GSview that another page is ready */
  407. rc = DosWriteQueue(pmdev->drv_queue, GS_PAGE, 0, NULL, 0);
  408. if (rc)
  409. eprintf1("pm_output_page: DosWriteQueue error %d\n", rc);
  410. /* wait for GSview to signal we can move on to next page */
  411. DosWaitEventSem(pmdev->next_event, SEM_INDEFINITE_WAIT);
  412. DosResetEventSem(pmdev->next_event, &count);
  413. }
  414. code = 0;
  415. } else {
  416. code = pm_sync_output(dev);
  417. rc = DosSelectSession(pmdev->session_id);
  418. if (rc) {
  419. DosSleep(2000); /* give gspmdrv.exe a chance to run */
  420. rc = DosSelectSession(pmdev->session_id);
  421. if (rc == ERROR_SMG_NO_TARGET_WINDOW) {
  422. DosSleep(5000); /* give gspmdrv.exe a chance to run */
  423. rc = DosSelectSession(pmdev->session_id); /* try yet again */
  424. }
  425. if ((rc == ERROR_SMG_SESSION_NOT_FOUND) ||
  426. (rc == ERROR_SMG_INVALID_SESSION_ID)) {
  427. /* someone has killed the session */
  428. REQUESTDATA Request;
  429. ULONG DataLength;
  430. PVOID DataAddress;
  431. PULONG QueueEntry;
  432. BYTE ElemPriority;
  433. /* Close gspmdrv driver */
  434. DosStopSession(STOP_SESSION_SPECIFIED, pmdev->session_id);
  435. Request.pid = pmdev->gspid;
  436. Request.ulData = 0;
  437. /* wait for termination queue, queue is then closed by session manager */
  438. DosReadQueue(pmdev->term_queue, &Request, &DataLength,
  439. &DataAddress, 0, DCWW_WAIT, &ElemPriority, (HEV) NULL);
  440. DosCloseQueue(pmdev->term_queue);
  441. pmdev->term_queue = (HQUEUE) 0;
  442. /* restart it */
  443. pm_run_gspmdrv(pmdev);
  444. DosSleep(2000); /* give gspmdrv.exe a chance to run */
  445. rc = DosSelectSession(pmdev->session_id);
  446. }
  447. if (rc == ERROR_SMG_SESSION_NOT_FOREGRND)
  448. DosBeep(400, 50);
  449. else if (rc)
  450. eprintf1("pm_output_page: Select Session error code %u\n", rc);
  451. }
  452. }
  453. return code;
  454. }
  455. int
  456. pm_output_page(gx_device * dev, int copies, int flush)
  457. {
  458. int code = pm_do_output_page(dev, copies, flush);
  459. if (code >= 0)
  460. code = gx_finish_output_page(dev, copies, flush);
  461. return code;
  462. }
  463. /* Close the pm driver */
  464. int
  465. pm_close(gx_device * dev)
  466. {
  467. APIRET rc;
  468. #ifdef __DLL__
  469. if (pmdev->dll) {
  470. DosRequestMutexSem(pmdev->bmp_mutex, 60000);
  471. if (pgsdll_callback)
  472. (*pgsdll_callback) (GSDLL_DEVICE, (unsigned char *)dev, 0);
  473. DosReleaseMutexSem(pmdev->bmp_mutex);
  474. } else
  475. #endif
  476. {
  477. if (*pmdev->GSVIEW) {
  478. rc = DosWriteQueue(pmdev->drv_queue, GS_CLOSE, 0, NULL, 0);
  479. if (rc)
  480. eprintf1("pm_close: DosWriteQueue error %d\n", rc);
  481. } else {
  482. REQUESTDATA Request;
  483. ULONG DataLength;
  484. PVOID DataAddress;
  485. PULONG QueueEntry;
  486. BYTE ElemPriority;
  487. /* Close gspmdrv driver */
  488. DosStopSession(STOP_SESSION_SPECIFIED, pmdev->session_id);
  489. Request.pid = pmdev->gspid;
  490. Request.ulData = 0;
  491. /* wait for termination queue, queue is then closed by session manager */
  492. DosReadQueue(pmdev->term_queue, &Request, &DataLength,
  493. &DataAddress, 0, DCWW_WAIT, &ElemPriority, (HEV) NULL);
  494. /* queue needs to be closed by us */
  495. DosCloseQueue(pmdev->term_queue);
  496. }
  497. }
  498. /* release memory */
  499. DosFreeMem(pmdev->bitmap);
  500. pmdev->bitmap = (unsigned char *)NULL;
  501. pmdev->committed = 0;
  502. if (!pmdev->dll) {
  503. /* close objects */
  504. if (*pmdev->GSVIEW) {
  505. DosCloseQueue(pmdev->drv_queue);
  506. DosCloseEventSem(pmdev->next_event);
  507. } else {
  508. DosCloseEventSem(pmdev->sync_event);
  509. /* stop update timer */
  510. if (pmdev->updating)
  511. DosStopTimer(pmdev->update_timer);
  512. pmdev->updating = FALSE;
  513. }
  514. }
  515. DosCloseMutexSem(pmdev->bmp_mutex);
  516. return (0);
  517. }
  518. /* Map a r-g-b color to the colors available under PM */
  519. gx_color_index
  520. pm_map_rgb_color(gx_device * dev, const gx_color_value cv[])
  521. {
  522. gx_color_value r = cv[0];
  523. gx_color_value g = cv[1];
  524. gx_color_value b = cv[2];
  525. switch (dev->color_info.depth) {
  526. case 24:
  527. return ((b >> (gx_color_value_bits - 8)) << 16) +
  528. ((g >> (gx_color_value_bits - 8)) << 8) +
  529. ((r >> (gx_color_value_bits - 8)));
  530. case 8:{
  531. int i;
  532. RGB2 *prgb;
  533. byte cr, cg, cb;
  534. /* map colors to 0->255 in 32 steps */
  535. cr = pm_color_value(r);
  536. cg = pm_color_value(g);
  537. cb = pm_color_value(b);
  538. prgb = (RGB2 *) ((PBYTE) pmdev->bmi + pmdev->bmi->cbFix);
  539. /* search in palette */
  540. for (i = 0; i < pmdev->nColors; i++, prgb++) {
  541. if (!((cr ^ prgb->bRed) & 0xf8) &&
  542. !((cg ^ prgb->bGreen) & 0xf8) &&
  543. !((cb ^ prgb->bBlue) & 0xf8)
  544. )
  545. return ((gx_color_index) i); /* found it */
  546. }
  547. /* next try adding it to palette */
  548. if (i < 230) { /* allow 26 for PM and other apps */
  549. prgb->bRed = cr;
  550. prgb->bGreen = cg;
  551. prgb->bBlue = cb;
  552. prgb->fcOptions = 0;
  553. pmdev->nColors = i + 1;
  554. pmdev->bmi->cclrImportant = pmdev->nColors;
  555. if (*pmdev->GSVIEW) {
  556. APIRET rc;
  557. rc = DosWriteQueue(pmdev->drv_queue, GS_PALCHANGE, 0, NULL, 0);
  558. if (rc)
  559. eprintf1("pm_sync_output: DosWriteQueue error %d\n", rc);
  560. }
  561. return ((gx_color_index) i); /* return new palette index */
  562. }
  563. return (gx_no_color_index); /* not found - dither instead */
  564. }
  565. case 4:
  566. return pc_4bit_map_rgb_color(dev, cv);
  567. }
  568. return (gx_default_map_rgb_color(dev, cv));
  569. }
  570. /* Map a color code to r-g-b. */
  571. int
  572. pm_map_color_rgb(gx_device * dev, gx_color_index color,
  573. gx_color_value prgb[3])
  574. {
  575. gx_color_value one;
  576. switch (dev->color_info.depth) {
  577. case 24:
  578. one = (gx_color_value) (gx_max_color_value / 255);
  579. prgb[0] = ((color) & 255) * one;
  580. prgb[1] = ((color >> 8) & 255) * one;
  581. prgb[2] = ((color >> 16) & 255) * one;
  582. break;
  583. case 8:
  584. if (!dev->is_open)
  585. return -1;
  586. {
  587. RGB2 *argb = (RGB2 *) ((PBYTE) pmdev->bmi + pmdev->bmi->cbFix);
  588. one = (gx_color_value) (gx_max_color_value / 255);
  589. prgb[0] = argb[(int)color].bRed * one;
  590. prgb[1] = argb[(int)color].bGreen * one;
  591. prgb[2] = argb[(int)color].bBlue * one;
  592. }
  593. break;
  594. case 4:
  595. pc_4bit_map_color_rgb(dev, color, prgb);
  596. break;
  597. default:
  598. prgb[0] = prgb[1] = prgb[2] =
  599. (int)color ? gx_max_color_value : 0;
  600. }
  601. return 0;
  602. }
  603. #define pmmdev ((gx_device *)&pmdev->mdev)
  604. #define pmmproc(proc) (*dev_proc(&pmdev->mdev, proc))
  605. /* Fill a rectangle. */
  606. private int
  607. pm_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  608. gx_color_index color)
  609. {
  610. pmmproc(fill_rectangle) (pmmdev, x, y, w, h, color);
  611. pm_update((gx_device_pm *) dev);
  612. return 0;
  613. }
  614. /* Copy a monochrome bitmap. The colors are given explicitly. */
  615. /* Color = gx_no_color_index means transparent (no effect on the image). */
  616. private int
  617. pm_copy_mono(gx_device * dev,
  618. const byte * base, int sourcex, int raster, gx_bitmap_id id,
  619. int x, int y, int w, int h,
  620. gx_color_index zero, gx_color_index one)
  621. {
  622. pmmproc(copy_mono) (pmmdev, base, sourcex, raster, id,
  623. x, y, w, h, zero, one);
  624. pm_update((gx_device_pm *) dev);
  625. return 0;
  626. }
  627. /* Copy a color pixel map. This is just like a bitmap, except that */
  628. /* each pixel takes 8 or 4 bits instead of 1 when device driver has color. */
  629. private int
  630. pm_copy_color(gx_device * dev,
  631. const byte * base, int sourcex, int raster, gx_bitmap_id id,
  632. int x, int y, int w, int h)
  633. {
  634. pmmproc(copy_color) (pmmdev, base, sourcex, raster, id,
  635. x, y, w, h);
  636. pm_update((gx_device_pm *) dev);
  637. return 0;
  638. }
  639. int
  640. pm_get_bits(gx_device * dev, int y, byte * str, byte ** actual_data)
  641. {
  642. return pmmproc(get_bits) (pmmdev, y, str, actual_data);
  643. }
  644. /* Get PM parameters */
  645. int
  646. pm_get_params(gx_device * dev, gs_param_list * plist)
  647. {
  648. int code = gx_default_get_params(dev, plist);
  649. gs_param_string gvs;
  650. gvs.data = pmdev->GSVIEW, gvs.size = strlen(gvs.data),
  651. gvs.persistent = false;
  652. code < 0 ||
  653. (code = param_write_int(plist, "UpdateInterval", &pmdev->UpdateInterval)) < 0 ||
  654. (code = param_write_string(plist, "GSVIEW", &gvs)) < 0;
  655. return code;
  656. }
  657. /* Put parameters. */
  658. /* Set PM parameters -- size and resolution. */
  659. /* We implement this ourselves so that we can do it without */
  660. /* closing and opening the device. */
  661. /* Also set BitsPerPixel and GSVIEW if device not open */
  662. int
  663. pm_put_params(gx_device * dev, gs_param_list * plist)
  664. {
  665. int ecode = 0, code;
  666. bool reopen = false;
  667. bool is_open = dev->is_open;
  668. int width = dev->width;
  669. int height = dev->height;
  670. int old_bpp = dev->color_info.depth;
  671. int bpp = old_bpp;
  672. int uii = pmdev->UpdateInterval;
  673. gs_param_string gsvs;
  674. /* Handle extra parameters */
  675. switch (code = param_read_string(plist, "GSVIEW", &gsvs)) {
  676. case 0:
  677. if (gsvs.size == strlen(pmdev->GSVIEW) &&
  678. !memcmp(pmdev->GSVIEW, gsvs.data, gsvs.size)
  679. ) {
  680. gsvs.data = 0;
  681. break;
  682. }
  683. if (dev->is_open)
  684. ecode = gs_error_rangecheck;
  685. else if (gsvs.size >= pm_gsview_sizeof)
  686. ecode = gs_error_limitcheck;
  687. else
  688. break;
  689. goto gsve;
  690. default:
  691. ecode = code;
  692. gsve:param_signal_error(plist, "GSVIEW", ecode);
  693. case 1:
  694. gsvs.data = 0;
  695. break;
  696. }
  697. switch (code = param_read_int(plist, "UpdateInterval", &uii)) {
  698. case 0:
  699. if (uii < 0)
  700. ecode = gs_error_rangecheck;
  701. else
  702. break;
  703. goto uie;
  704. default:
  705. ecode = code;
  706. uie:param_signal_error(plist, "UpdateInterval", ecode);
  707. case 1:
  708. break;
  709. }
  710. switch (code = param_read_int(plist, "BitsPerPixel", &bpp)) {
  711. case 0:
  712. if (dev->is_open && bpp != old_bpp)
  713. ecode = gs_error_rangecheck;
  714. else {
  715. code = pm_set_bits_per_pixel(pmdev, bpp);
  716. if (code < 0)
  717. ecode = code;
  718. else
  719. break;
  720. }
  721. goto bppe;
  722. default:
  723. ecode = code;
  724. bppe:param_signal_error(plist, "BitsPerPixel", ecode);
  725. case 1:
  726. break;
  727. }
  728. if (ecode >= 0) { /* Prevent gx_default_put_params from closing the device. */
  729. dev->is_open = false;
  730. ecode = gx_default_put_params(dev, plist);
  731. dev->is_open = is_open;
  732. }
  733. if (ecode < 0) {
  734. if (bpp != old_bpp)
  735. pm_set_bits_per_pixel(pmdev, old_bpp);
  736. return ecode;
  737. }
  738. /* Hand off the change to the implementation. */
  739. /* obtain mutex - to prevent gspmdrv from using bitmap */
  740. /* while we change its size */
  741. if (DosRequestMutexSem(pmdev->bmp_mutex, 20000) == ERROR_TIMEOUT)
  742. eprintf("pm_put_params: mutex timeout\n");
  743. if (is_open && (old_bpp != bpp ||
  744. dev->width != width || dev->height != height)
  745. ) {
  746. int ccode;
  747. ccode = pm_alloc_bitmap(pmdev, dev);
  748. if (ccode < 0) { /* Bad news! Some of the other device parameters */
  749. /* may have changed. We don't handle this. */
  750. /* This is ****** WRONG ******. */
  751. dev->width = width;
  752. dev->height = height;
  753. pm_set_bits_per_pixel(pmdev, old_bpp);
  754. pm_alloc_bitmap(pmdev, dev);
  755. DosReleaseMutexSem(pmdev->bmp_mutex);
  756. return ccode;
  757. }
  758. reopen = true;
  759. }
  760. pmdev->UpdateInterval = uii;
  761. if (gsvs.data != 0) {
  762. memcpy(pmdev->GSVIEW, gsvs.data, gsvs.size);
  763. pmdev->GSVIEW[gsvs.size] = 0;
  764. }
  765. if (dev->is_open && reopen) {
  766. /* need to update bitmap info header also */
  767. pmdev->bmi->cx = dev->width;
  768. pmdev->bmi->cy = dev->height;
  769. /* update bit count and palette */
  770. pmdev->bmi->cBitCount = dev->color_info.depth;
  771. pmdev->bmi->cclrUsed = 1 << (pmdev->BitsPerPixel);
  772. pmdev->bmi->cclrImportant = pmdev->nColors;
  773. pm_makepalette(pmdev);
  774. /* erase bitmap - before window gets redrawn */
  775. {
  776. int i;
  777. gx_color_value cv[GX_DEVICE_COLOR_MAX_COMPONENTS];
  778. for (i=0; i<GX_DEVICE_COLOR_MAX_COMPONENTS; i++)
  779. cv[i] = (pmdev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE)
  780. ? gx_max_color_value : 0;
  781. dev_proc(pmdev, fill_rectangle)((gx_device *)pmdev,
  782. 0, 0, pmdev->width, pmdev->height,
  783. pmdev->procs.encode_color((gx_device *)pmdev, cv));
  784. }
  785. /* cause scroll bars to be redrawn */
  786. /* need to signal gspmdrv that bitmap size has changed */
  787. /* or perhaps gspmdrv can check if the bitmap size has */
  788. /* before each use */
  789. #ifdef __DLL__
  790. if (pmdev->dll && pgsdll_callback)
  791. (*pgsdll_callback) (GSDLL_SIZE, (unsigned char *)dev,
  792. (dev->width & 0xffff) + ((dev->height & 0xffff) << 16));
  793. #endif
  794. }
  795. /* release bmp mutex */
  796. DosReleaseMutexSem(pmdev->bmp_mutex);
  797. return 0;
  798. }
  799. #ifdef __DLL__
  800. /* ------ DLL routines ------ */
  801. /* store at pbitmap the address of the bitmap */
  802. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  803. unsigned long GSDLLAPI
  804. gsdll_get_bitmap(unsigned char *device, unsigned char **pbitmap)
  805. {
  806. gx_device *dev = (gx_device *) device;
  807. *pbitmap = (unsigned char *)(pmdev->bmi);
  808. return 0;
  809. }
  810. /* Lock the device (so it's size cannot be changed) if flag = TRUE */
  811. /* or unlock the device if flag = FALSE */
  812. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  813. int GSDLLAPI
  814. gsdll_lock_device(unsigned char *device, int flag)
  815. {
  816. gx_device *dev = (gx_device *) device;
  817. APIRET rc;
  818. if (flag)
  819. rc = DosRequestMutexSem(pmdev->bmp_mutex, 60000);
  820. else
  821. rc = DosReleaseMutexSem(pmdev->bmp_mutex);
  822. return rc;
  823. }
  824. #endif /* __DLL__ */
  825. /* ------ Internal routines ------ */
  826. #undef pmdev
  827. /* start gspmdrv.exe */
  828. private int
  829. pm_run_gspmdrv(gx_device_pm * pmdev)
  830. {
  831. int ccode;
  832. PCHAR pdrvname = "gspmdrv.exe";
  833. CHAR error_message[256];
  834. CHAR term_queue_name[128];
  835. CHAR id[128];
  836. CHAR arg[1024];
  837. STARTDATA sdata;
  838. APIRET rc;
  839. PTIB pptib;
  840. PPIB pppib;
  841. CHAR progname[256];
  842. PCHAR tail;
  843. sprintf(id, ID_NAME, pmdev->gspid, (ULONG) pmdev);
  844. /* Create termination queue - used to find out when gspmdrv terminates */
  845. sprintf(term_queue_name, "\\QUEUES\\TERMQ_%s", id);
  846. if (DosCreateQueue(&(pmdev->term_queue), QUE_FIFO, term_queue_name)) {
  847. eprintf("pm_run_gspmdrv: failed to create termination queue\n");
  848. return gs_error_limitcheck;
  849. }
  850. /* get full path to gsos2.exe and hence path to gspmdrv.exe */
  851. if ((rc = DosGetInfoBlocks(&pptib, &pppib)) != 0) {
  852. eprintf1("pm_run_gspmdrv: Couldn't get module handle, rc = %d\n", rc);
  853. return gs_error_limitcheck;
  854. }
  855. if ((rc = DosQueryModuleName(pppib->pib_hmte, sizeof(progname) - 1, progname)) != 0) {
  856. eprintf1("pm_run_gspmdrv: Couldn't get module name, rc = %d\n", rc);
  857. return gs_error_limitcheck;
  858. }
  859. if ((tail = strrchr(progname, '\\')) != (PCHAR) NULL) {
  860. tail++;
  861. *tail = '\0';
  862. } else
  863. tail = progname;
  864. strcat(progname, pdrvname);
  865. /* Open the PM driver session gspmdrv.exe */
  866. /* arguments are: */
  867. /* (1) -d (display) option */
  868. /* (2) id string */
  869. sprintf(arg, "-d %s", id);
  870. /* because gspmdrv.exe is a different EXE type to gs.exe,
  871. * we must use start session not DosExecPgm() */
  872. sdata.Length = sizeof(sdata);
  873. sdata.Related = SSF_RELATED_CHILD; /* to be a child */
  874. sdata.FgBg = SSF_FGBG_BACK; /* start in background */
  875. sdata.TraceOpt = 0;
  876. sdata.PgmTitle = "Ghostscript PM driver session";
  877. sdata.PgmName = progname;
  878. sdata.PgmInputs = arg;
  879. sdata.TermQ = term_queue_name;
  880. sdata.Environment = pppib->pib_pchenv; /* use Parent's environment */
  881. sdata.InheritOpt = 0; /* Can't inherit from parent because different sesison type */
  882. sdata.SessionType = SSF_TYPE_DEFAULT; /* default is PM */
  883. sdata.IconFile = NULL;
  884. sdata.PgmHandle = 0;
  885. sdata.PgmControl = 0;
  886. sdata.InitXPos = 0;
  887. sdata.InitYPos = 0;
  888. sdata.InitXSize = 0;
  889. sdata.InitYSize = 0;
  890. sdata.ObjectBuffer = error_message;
  891. sdata.ObjectBuffLen = sizeof(error_message);
  892. rc = DosStartSession(&sdata, &pmdev->session_id, &pmdev->process_id);
  893. if (rc == ERROR_FILE_NOT_FOUND) {
  894. sdata.PgmName = pdrvname;
  895. rc = DosStartSession(&sdata, &pmdev->session_id, &pmdev->process_id);
  896. }
  897. if (rc) {
  898. eprintf2("pm_run_gspmdrv: failed to run %s, rc = %d\n", sdata.PgmName, rc);
  899. eprintf1("pm_run_gspmdrv: error_message: %s\n", error_message);
  900. return gs_error_limitcheck;
  901. }
  902. return 0;
  903. }
  904. /* Allocate the backing bitmap. */
  905. private int
  906. pm_alloc_bitmap(gx_device_pm * pmdev, gx_device * param_dev)
  907. {
  908. gx_device_memory mdev;
  909. byte *base;
  910. ulong data_size;
  911. uint ptr_size;
  912. uint pal_size;
  913. uint raster;
  914. ULONG rc;
  915. ULONG needed;
  916. /* Finish initializing the bitmap. */
  917. gs_make_mem_device(&mdev, gdev_mem_device_for_bits(pmdev->color_info.depth), 0, 0, (gx_device *) pmdev);
  918. mdev.width = param_dev->width;
  919. mdev.height = param_dev->height;
  920. /* BMP files need width rounded up so that a scan line is */
  921. /* a multiple of 4 bytes. */
  922. /* This is currently done by gdev_mem_raster(). */
  923. /* It may be better to do it here explicitly in case */
  924. /* gdev_mem_raster changes. */
  925. raster = gdev_mem_raster(&mdev);
  926. data_size = (ulong) raster *mdev.height;
  927. ptr_size = sizeof(byte **) * mdev.height;
  928. pal_size = pm_palette_size(pmdev);
  929. needed = pmdev->bmi->cbFix + pal_size + data_size + ptr_size;
  930. /* round up to page boundary */
  931. needed = (needed + MIN_COMMIT - 1) & (~(MIN_COMMIT - 1));
  932. if (needed > pmdev->committed) {
  933. /* commit more memory */
  934. if (rc = DosSetMem(pmdev->bitmap + pmdev->committed,
  935. needed - pmdev->committed,
  936. PAG_COMMIT | PAG_DEFAULT)) {
  937. eprintf1("No memory in pm_alloc_bitmap, rc = %d\n", rc);
  938. return gs_error_limitcheck;
  939. }
  940. pmdev->committed = needed;
  941. }
  942. /* Shared memory can't be decommitted */
  943. #ifdef __DLL__
  944. if (pmdev->dll && (needed < pmdev->committed)) {
  945. /* decommit memory */
  946. if (rc = DosSetMem(pmdev->bitmap + needed,
  947. pmdev->committed - needed,
  948. PAG_DECOMMIT)) {
  949. eprintf1("Failed to decommit memory in pm_alloc_bitmap, rc = %d\n", rc);
  950. return gs_error_limitcheck;
  951. }
  952. pmdev->committed = needed;
  953. }
  954. #endif
  955. /* Nothing can go wrong now.... */
  956. base = pmdev->bitmap + pmdev->bmi->cbFix + pm_palette_size(pmdev);
  957. pmdev->mdev = mdev;
  958. pmdev->mdev.base = (byte *) base;
  959. pmmproc(open_device) ((gx_device *) & pmdev->mdev);
  960. pmdev->bmi->cbImage = data_size;
  961. return 0;
  962. }
  963. private void
  964. pm_makepalette(gx_device_pm * pmdev)
  965. {
  966. int i, val;
  967. RGB2 *argb = (RGB2 *) ((PBYTE) pmdev->bmi + pmdev->bmi->cbFix);
  968. if (pmdev->BitsPerPixel > 8)
  969. return; /* these don't use a palette */
  970. for (i = 0; i < pmdev->nColors; i++) {
  971. switch (pmdev->nColors) {
  972. case 64:
  973. /* colors are rrggbb */
  974. argb[i].bRed = ((i & 0x30) >> 4) * 85;
  975. argb[i].bGreen = ((i & 0xC) >> 2) * 85;
  976. argb[i].bBlue = (i & 3) * 85;
  977. argb[i].fcOptions = 0;
  978. /* zero unused entries */
  979. argb[i + 64].bRed = argb[i + 64].bGreen = argb[i + 64].bBlue = 0;
  980. argb[i + 64].fcOptions = 0;
  981. argb[i + 128].bRed = argb[i + 128].bGreen = argb[i + 128].bBlue = 0;
  982. argb[i + 128].fcOptions = 0;
  983. argb[i + 192].bRed = argb[i + 192].bGreen = argb[i + 192].bBlue = 0;
  984. argb[i + 192].fcOptions = 0;
  985. break;
  986. case 16:
  987. /* colors are irgb */
  988. val = (i & 8 ? 255 : 128);
  989. argb[i].bRed = i & 4 ? val : 0;
  990. argb[i].bGreen = i & 2 ? val : 0;
  991. argb[i].bBlue = i & 1 ? val : 0;
  992. if (i == 8) { /* light gray */
  993. argb[i].bRed =
  994. argb[i].bGreen =
  995. argb[i].bBlue = 192;
  996. argb[i].fcOptions = 0;
  997. }
  998. break;
  999. case 2:
  1000. argb[i].bRed =
  1001. argb[i].bGreen =
  1002. argb[i].bBlue = (i ? 255 : 0);
  1003. argb[i].fcOptions = 0;
  1004. break;
  1005. }
  1006. }
  1007. }
  1008. /* Cause display to be updated periodically */
  1009. private void
  1010. pm_update(gx_device_pm * pmdev)
  1011. {
  1012. if (pmdev->updating)
  1013. return;
  1014. if (!pmdev->UpdateInterval)
  1015. return;
  1016. if (*pmdev->GSVIEW) {
  1017. APIRET rc;
  1018. rc = DosWriteQueue(pmdev->drv_queue, GS_UPDATING, 0, NULL, 0);
  1019. if (rc)
  1020. eprintf1("pm_update: DosWriteQueue error %d\n", rc);
  1021. } else {
  1022. DosStartTimer(pmdev->UpdateInterval, (HSEM) pmdev->sync_event,
  1023. &pmdev->update_timer);
  1024. }
  1025. pmdev->updating = TRUE;
  1026. }
  1027. /*
  1028. * This is a utility routine to build the display device's color_info
  1029. * structure (except for the anti alias info).
  1030. */
  1031. private void
  1032. set_color_info(gx_device_color_info * pdci, int nc, int depth, int maxgray, int maxcolor)
  1033. {
  1034. pdci->num_components = pdci->max_components = nc;
  1035. pdci->depth = depth;
  1036. pdci->gray_index = 0;
  1037. pdci->max_gray = maxgray;
  1038. pdci->max_color = maxcolor;
  1039. pdci->dither_grays = maxgray + 1;
  1040. pdci->dither_colors = maxcolor + 1;
  1041. pdci->separable_and_linear = GX_CINFO_UNKNOWN_SEP_LIN;
  1042. switch (nc) {
  1043. case 1:
  1044. pdci->polarity = GX_CINFO_POLARITY_ADDITIVE;
  1045. pdci->cm_name = "DeviceGray";
  1046. break;
  1047. case 3:
  1048. pdci->polarity = GX_CINFO_POLARITY_ADDITIVE;
  1049. pdci->cm_name = "DeviceRGB";
  1050. break;
  1051. case 4:
  1052. pdci->polarity = GX_CINFO_POLARITY_SUBTRACTIVE;
  1053. pdci->cm_name = "DeviceCMYK";
  1054. break;
  1055. default:
  1056. break;
  1057. }
  1058. }
  1059. /*
  1060. * This is an utility routine to set up the color procs for the display
  1061. * device. The display device can change its setup.
  1062. */
  1063. private void
  1064. set_color_procs(gx_device * pdev,
  1065. dev_t_proc_encode_color((*encode_color), gx_device),
  1066. dev_t_proc_decode_color((*decode_color), gx_device),
  1067. dev_t_proc_get_color_mapping_procs((*get_color_mapping_procs), gx_device),
  1068. dev_t_proc_get_color_comp_index((*get_color_comp_index), gx_device))
  1069. {
  1070. #if 0 /* These procs are no longer used */
  1071. pdev->procs.map_rgb_color = encode_color;
  1072. pdev->procs.map_color_rgb = decode_color;
  1073. #endif
  1074. pdev->procs.get_color_mapping_procs = get_color_mapping_procs;
  1075. pdev->procs.get_color_comp_index = get_color_comp_index;
  1076. pdev->procs.encode_color = encode_color;
  1077. pdev->procs.decode_color = decode_color;
  1078. }
  1079. /*
  1080. * This is an utility routine to set up the color procs for the display
  1081. * device. This routine is used when the display device is Gray.
  1082. */
  1083. private void
  1084. set_gray_color_procs(gx_device * pdev,
  1085. dev_t_proc_encode_color((*encode_color), gx_device),
  1086. dev_t_proc_decode_color((*decode_color), gx_device))
  1087. {
  1088. set_color_procs(pdev, encode_color, decode_color,
  1089. gx_default_DevGray_get_color_mapping_procs,
  1090. gx_default_DevGray_get_color_comp_index);
  1091. }
  1092. /*
  1093. * This is an utility routine to set up the color procs for the display
  1094. * device. This routine is used when the display device is RGB.
  1095. */
  1096. private void
  1097. set_rgb_color_procs(gx_device * pdev,
  1098. dev_t_proc_encode_color((*encode_color), gx_device),
  1099. dev_t_proc_decode_color((*decode_color), gx_device))
  1100. {
  1101. set_color_procs(pdev, encode_color, decode_color,
  1102. gx_default_DevRGB_get_color_mapping_procs,
  1103. gx_default_DevRGB_get_color_comp_index);
  1104. }
  1105. private uint
  1106. pm_set_bits_per_pixel(gx_device_pm * pmdev, int bpp)
  1107. {
  1108. gx_device * pdev = (gx_device *) pmdev;
  1109. gx_device_color_info dci = pmdev->color_info;
  1110. switch (bpp) {
  1111. case 24:
  1112. set_color_info(&dci, 3, bpp, 255, 255);
  1113. set_rgb_color_procs(pdev, pm_map_rgb_color, pm_map_color_rgb);
  1114. pmdev->nColors = (1 << 24);
  1115. break;
  1116. case 8:
  1117. /* use 64 static colors and 166 dynamic colors from 8 planes */
  1118. set_color_info(&dci, 3, 8, 7, 31);
  1119. set_rgb_color_procs(pdev, pm_map_rgb_color, pm_map_color_rgb);
  1120. pmdev->nColors = 64;
  1121. break;
  1122. case 4:
  1123. set_color_info(&dci, 3, 4, 1, 1);
  1124. set_rgb_color_procs(pdev, pm_map_rgb_color, pm_map_color_rgb);
  1125. pmdev->nColors = 16;
  1126. break;
  1127. case 1:
  1128. set_color_info(&dci, 1, 1, 1, 0);
  1129. set_gray_color_procs(pdev, gx_default_gray_encode,
  1130. gx_default_w_b_map_color_rgb);
  1131. pmdev->nColors = 2;
  1132. break;
  1133. default:
  1134. return (gs_error_rangecheck);
  1135. }
  1136. pmdev->BitsPerPixel = bpp;
  1137. /* restore old anti_alias info */
  1138. dci.anti_alias = pmdev->color_info.anti_alias;
  1139. pmdev->color_info = dci;
  1140. /* Set the mask bits, etc. even though we are setting linear: unknown */
  1141. set_linear_color_bits_mask_shift(pdev);
  1142. return 0;
  1143. }
  1144. /* return length of BMP palette in bytes */
  1145. private uint
  1146. pm_palette_size(gx_device_pm * pmdev)
  1147. {
  1148. switch (pmdev->color_info.depth) {
  1149. case 24:
  1150. return 0;
  1151. case 8:
  1152. return 256 * sizeof(RGB2);
  1153. case 4:
  1154. return 16 * sizeof(RGB2);
  1155. }
  1156. /* must be two color */
  1157. return 2 * sizeof(RGB2);
  1158. }
  1159. /* This is used for testing */
  1160. /* Write out a BMP file to "out.bmp" */
  1161. private void
  1162. pm_write_bmp(gx_device_pm * pmdev)
  1163. {
  1164. BITMAPFILEHEADER2 bmfh;
  1165. uint bmfh_length = sizeof(BITMAPFILEHEADER2) - sizeof(BITMAPINFOHEADER2);
  1166. uint length; /* bitmap length */
  1167. ULONG fh; /* file handle */
  1168. ULONG action;
  1169. ULONG count;
  1170. bmfh.usType = 0x4d42; /* "BM" */
  1171. length = pmdev->bmi->cbFix + pm_palette_size(pmdev)
  1172. + ((gdev_mem_raster(&pmdev->mdev) * pmdev->mdev.height));
  1173. bmfh.cbSize = bmfh_length + length;
  1174. bmfh.xHotspot = bmfh.yHotspot = 0;
  1175. bmfh.offBits = bmfh_length + pmdev->bmi->cbFix + pm_palette_size(pmdev);
  1176. if (DosOpen("out.bmp", /* filename */
  1177. &fh, /* pointer to handle */
  1178. &action, /* pointer to result */
  1179. 0, /* initial length */
  1180. FILE_NORMAL, /* normal file */
  1181. OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS,
  1182. OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYREADWRITE,
  1183. 0)) {
  1184. eprintf("error opening out.bmp\n");
  1185. return;
  1186. }
  1187. if (DosWrite(fh, (PBYTE) & bmfh, bmfh_length, &count))
  1188. eprintf("error writing header for out.bmp\n");
  1189. if (DosWrite(fh, pmdev->bitmap, length, &count))
  1190. eprintf("error writing out.bmp\n");
  1191. if (DosClose(fh))
  1192. eprintf("error closing out.bmp\n");
  1193. }