1 /* Copyright (C) 2001-2006 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied, modified
8    or distributed except as expressly authorized under the terms of that
9    license.  Refer to licensing information at http://www.artifex.com/
10    or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
11    San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
12 */
13 
14 /* idisp.c */
15 /* $Id: idisp.c 9043 2008-08-28 22:48:19Z giles $ */
16 
17 /*
18  * This allows the interpreter to set the callback structure
19  * of the "display" device.  This must set at the end of
20  * initialization and before the device is used.
21  * This is harmless if the 'display' device is not included.
22  * If gsapi_set_display_callback() is not called, this code
23  * won't even be used.
24  */
25 
26 #include "stdio_.h"
27 #include "stdpre.h"
28 #include "iapi.h"
29 #include "ghost.h"
30 #include "gp.h"
31 #include "gscdefs.h"
32 #include "gsmemory.h"
33 #include "gstypes.h"
34 #include "gsdevice.h"
35 #include "iref.h"
36 #include "imain.h"
37 #include "iminst.h"
38 #include "oper.h"
39 #include "ostack.h"
40 #include "gx.h"
41 #include "gxdevice.h"
42 #include "gxdevmem.h"
43 #include "idisp.h"
44 #include "gdevdevn.h"
45 #include "gsequivc.h"
46 #include "gdevdsp.h"
47 #include "gdevdsp2.h"
48 
49 int
display_set_callback(gs_main_instance * minst,display_callback * callback)50 display_set_callback(gs_main_instance *minst, display_callback *callback)
51 {
52     i_ctx_t *i_ctx_p = minst->i_ctx_p;
53     bool was_open;
54     int code;
55     int exit_code = 0;
56     os_ptr op = osp;
57     gx_device *dev;
58     gx_device_display *ddev;
59 
60     /* If display device exists, copy prototype if needed and return
61      *  device true
62      * If it doesn't exist, return
63      *  false
64      */
65     const char getdisplay[] =
66       "devicedict /display known dup { /display finddevice exch } if";
67     code = gs_main_run_string(minst, getdisplay, 0, &exit_code,
68 	&minst->error_object);
69     if (code < 0)
70        return code;
71 
72     op = osp;
73     check_type(*op, t_boolean);
74     if (op->value.boolval) {
75 	/* display device was included in Ghostscript so we need
76 	 * to set the callback structure pointer within it.
77 	 * If the device is already open, close it before
78 	 * setting callback, then reopen it.
79 	 */
80 	check_read_type(op[-1], t_device);
81 	dev = op[-1].value.pdevice;
82 
83 	was_open = dev->is_open;
84 	if (was_open) {
85 	    code = gs_closedevice(dev);
86 	    if (code < 0)
87 		return_error(code);
88 	}
89 
90 	ddev = (gx_device_display *) dev;
91 	ddev->callback = callback;
92 
93 	if (was_open) {
94 	    code = gs_opendevice(dev);
95 	    if (code < 0) {
96 		dprintf("**** Unable to open the display device, quitting.\n");
97 		return_error(code);
98 	    }
99 	}
100 	pop(1);	/* device */
101     }
102     pop(1);	/* boolean */
103     return 0;
104 }
105 
106 
107