1 /* Copyright (C) 1989 - 1995, 2001-2003 artofcode LLC. All rights reserved.
2
3 This software is provided AS-IS with no warranty, either express or
4 implied.
5
6 This software is distributed under license and may not be copied,
7 modified or distributed except as expressly authorized under the terms
8 of the license contained in the file LICENSE in this distribution.
9
10 For more information about licensing, please refer to
11 http://www.ghostscript.com/licensing/. For information on
12 commercial licensing, go to http://www.artifex.com/licensing/ or
13 contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14 San Rafael, CA 94903, U.S.A., +1(415)492-9861.
15 */
16
17 /* $Id: gp_mac.c,v 1.17 2004/12/08 05:04:29 giles Exp $ */
18 /* platform-specific routines for MacOS */
19
20 #ifndef __CARBON__
21 #include <Palettes.h>
22 #include <Aliases.h>
23 #include <Quickdraw.h>
24 #include <QDOffscreen.h>
25 #include <AppleEvents.h>
26 #include <Fonts.h>
27 #include <Controls.h>
28 #include <Script.h>
29 #include <Timer.h>
30 #include <Time.h>
31 #include <Folders.h>
32 #include <Resources.h>
33 #include <Sound.h>
34 #include <ToolUtils.h>
35 #include <Menus.h>
36 #include <LowMem.h>
37 #include <Devices.h>
38 #include <Scrap.h>
39 #include <StringCompare.h>
40 #include <Fonts.h>
41 #include <FixMath.h>
42 #include <Resources.h>
43 #else
44 #include <Carbon.h>
45 #endif
46
47 #include <stdlib.h>
48 #include "math_.h"
49 #include "string_.h"
50 #include "time_.h"
51 #include "memory_.h"
52 #include "string_.h"
53
54 #include "gx.h"
55 #include "gp.h"
56 #include "gsdll.h"
57 #include "gpcheck.h"
58 #include "gp_mac.h"
59 #include "gdebug.h"
60 /*#include "gpgetenv.h"*/
61 #include "gsexit.h"
62
63 HWND hwndtext; /* used as identifier for the dll instance */
64
65
66 char *
mygetenv(const char * env)67 mygetenv(const char * env)
68 {
69 return (NULL);
70 }
71
72 void
gp_init(void)73 gp_init (void)
74 {
75 extern char *gs_lib_default_path;
76 extern char *gs_init_file;
77
78 #if 0
79 /*...Initialize Ghostscript's default library paths and initialization file...*/
80
81 {
82 int i;
83 char **p;
84
85
86 for (i = iGSLibPathStr, p = &gs_lib_default_path;
87 i <= iGSInitFileStr;
88 i++, p = &gs_init_file)
89 {
90 GetIndString (string, MACSTRS_RES_ID, i);
91 (void) PtoCstr (string);
92 *p = malloc ((size_t) (strlen ((char *) string) + 1));
93 strcpy (*p, (char *) string);
94 }
95 }
96 #endif
97 }
98
99 void
gp_exit(int exit_status,int code)100 gp_exit(int exit_status, int code)
101 {
102 }
103
104 /* Exit the program. */
105 void
gp_do_exit(int exit_status)106 gp_do_exit(int exit_status)
107 {
108 exit(exit_status);
109 }
110
111 /* gettimeofday */
112 #ifndef HZ
113 # define HZ 100 /* see sys/param.h */
114 #endif
115 int
gettimeofday(struct timeval * tvp)116 gettimeofday(struct timeval *tvp)
117 {
118 struct tm tms;
119 static long offset = 0;
120 long ticks;
121
122 if (!offset) {
123 time(&offset);
124 offset -= (time((long *)&tms) / HZ);
125 }
126 ticks = time((long *)&tms);
127 tvp->tv_sec = ticks / HZ + offset;
128 tvp->tv_usec = (ticks % HZ) * (1000 * 1000 / HZ);
129 return 0;
130 }
131
132 /* ------ Date and time ------ */
133
134 #define gettimeofday_no_timezone 0
135
136 /* Read the current time (in seconds since Jan. 1, 1970) */
137 /* and fraction (in nanoseconds). */
138 void
gp_get_realtime(long * pdt)139 gp_get_realtime(long *pdt)
140 {
141 struct timeval tp;
142
143 if (gettimeofday(&tp) == -1) {
144 lprintf("Ghostscript: gettimeofday failed!\n");
145 gs_abort(NULL);
146 }
147
148 /* tp.tv_sec is #secs since Jan 1, 1970 */
149 pdt[0] = tp.tv_sec;
150
151 /* Some Unix systems (e.g., Interactive 3.2 r3.0) return garbage */
152 /* in tp.tv_usec. Try to filter out the worst of it here. */
153 pdt[1] = tp.tv_usec >= 0 && tp.tv_usec < 1000000 ? tp.tv_usec * 1000 : 0;
154
155 #ifdef DEBUG_CLOCK
156 printf("tp.tv_sec = %d tp.tv_usec = %d pdt[0] = %ld pdt[1] = %ld\n",
157 tp.tv_sec, tp.tv_usec, pdt[0], pdt[1]);
158 #endif
159 }
160
161 /* Read the current user CPU time (in seconds) */
162 /* and fraction (in nanoseconds). */
163 void
gp_get_usertime(long * pdt)164 gp_get_usertime(long *pdt)
165 {
166 gp_get_realtime(pdt); /* Use an approximation on other hosts. */
167 pdt[0] -= (char)rand(); // was needed, if used for random generator seed (g3 is too fast)
168 }
169
170
171 /*
172 * Get the string corresponding to an OS error number.
173 * If no string is available, return NULL. The caller may assume
174 * the string is allocated statically and permanently.
175 */
gp_strerror(int)176 const char * gp_strerror(int)
177 {
178 return NULL;
179 }
180
181
182 /* ------ Date and time ------ */
183
184 /* Read the current date (in days since Jan. 1, 1980) */
185 /* and time (in milliseconds since midnight). */
186
187 void
gp_get_clock(long * pdt)188 gp_get_clock (long *pdt) {
189
190 gp_get_realtime(pdt); /* Use an approximation on other hosts. */
191
192 }
193
194 void
gpp_get_clock(long * pdt)195 gpp_get_clock (long *pdt)
196
197 {
198 long secs;
199 DateTimeRec dateRec;
200 static DateTimeRec baseDateRec = {1980, 1, 1, 0, 0, 0, 1};
201 long pdtmp[2];
202 void do_get_clock (DateTimeRec *dateRec, long *pdt);
203
204
205 GetDateTime ((unsigned long *) &secs);
206 // SecsondsToDate (secs, &dateRec);
207
208 do_get_clock (&dateRec , pdt);
209 do_get_clock (&baseDateRec, pdtmp);
210
211 /* If the date is reasonable, subtract the days since Jan. 1, 1980 */
212
213 if (pdtmp[0] < pdt[0])
214 pdt[0] -= pdtmp[0];
215
216 #ifdef DEBUG_CLOCK
217 printf("pdt[0] = %ld pdt[1] = %ld\n", pdt[0], pdt[1]);
218 #endif
219 }
220
221 UnsignedWide beginMicroTickCount={0,0};
222
223 /* Read the current date (in days since Jan. 1, 1980) */
224 /* and time (in nanoseconds since midnight). */
225
226 void
gpp_get_realtime(long * pdt)227 gpp_get_realtime (long *pdt)
228 {
229
230 UnsignedWide microTickCount,
231 nMicroTickCount;
232
233 long idate;
234 static const int mstart[12] =
235 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
236 long secs;
237 DateTimeRec dateRec;
238 static DateTimeRec baseDateRec = {1980, 1, 1, 0, 0, 0, 1};
239 void do_get_clock (DateTimeRec *dateRec, long *pdt);
240
241
242 if ((beginMicroTickCount.lo == 0)&&(beginMicroTickCount.hi == 0) ) {
243 Microseconds(&beginMicroTickCount);
244 }
245
246
247 Microseconds(µTickCount);
248
249 nMicroTickCount.lo = microTickCount.lo - beginMicroTickCount.lo;
250 nMicroTickCount.hi = microTickCount.hi - beginMicroTickCount.hi;
251
252 GetDateTime ((unsigned long *) &secs);
253 SecondsToDate (secs, &dateRec);
254
255
256 /* If the date is reasonable, subtract the days since Jan. 1, 1980 */
257
258 idate = ((long) dateRec.year - 1980) * 365 + /* days per year */
259 (((long) dateRec.year - 1)/4 - 1979/4) + /* intervening leap days */
260 (1979/100 - ((long) dateRec.year - 1)/100) +
261 (((long) dateRec.month - 1)/400 - 1979/400) +
262 mstart[dateRec.month - 1] + /* month is 1-origin */
263 dateRec.day - 1; /* day of month is 1-origin */
264 idate += (2 < dateRec.month
265 && (dateRec.year % 4 == 0
266 && (dateRec.year % 100 != 0 || dateRec.year % 400 == 0)));
267 pdt[0] = ((idate*24 + dateRec.hour) * 60 + dateRec.minute) * 60 + dateRec.second;
268 pdt[1] = nMicroTickCount.lo * 100;
269
270 //#define DEBUG_CLOCK 1
271 #ifdef DEBUG_CLOCK
272 fprintf(stderr,"pdt[0] = %ld pdt[1] = %ld\n", pdt[0], pdt[1]);
273 fprintf(stderr,"b hi[0] = %ld lo[1] = %ld\n", beginMicroTickCount.hi, beginMicroTickCount.lo);
274 fprintf(stderr,"m hi[0] = %ld lo[1] = %ld\n", microTickCount.hi, microTickCount.lo);
275 #endif
276 }
277
278
279 static void
do_get_clock(DateTimeRec * dateRec,long * pdt)280 do_get_clock (DateTimeRec *dateRec, long *pdt)
281
282 {
283 long idate;
284 static const int mstart[12] =
285 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
286 /* This gets UTC, not local time */
287 /* We have no way of knowing the time zone correction */
288 idate = ((long) dateRec->year - 1980) * 365 + /* days per year */
289 (((long) dateRec->year - 1)/4 - 1979/4) + /* intervening leap days */
290 (1979/100 - ((long) dateRec->year - 1)/100) +
291 (((long) dateRec->month - 1)/400 - 1979/400) +
292 mstart[dateRec->month - 1] + /* month is 1-origin */
293 dateRec->day - 1; /* day of month is 1-origin */
294 idate += (2 < dateRec->month
295 && (dateRec->year % 4 == 0
296 && (dateRec->year % 100 != 0 || dateRec->year % 400 == 0)));
297 pdt[0] = ((idate*24 + dateRec->hour) * 60 + dateRec->minute) * 60 + dateRec->second;
298 pdt[1] = 0; //dateRec->milisecond * 1000000;
299
300 }
301
302 void
gpp_get_usertime(long * pdt)303 gpp_get_usertime(long *pdt)
304 {
305 gp_get_realtime(pdt); /* Use an approximation for now. */
306 }
307
308
309 /* ------ Persistent data cache ------*/
310
311 /* insert a buffer under a (type, key) pair */
gp_cache_insert(int type,byte * key,int keylen,void * buffer,int buflen)312 int gp_cache_insert(int type, byte *key, int keylen, void *buffer, int buflen)
313 {
314 /* not yet implemented */
315 return 0;
316 }
317
318 /* look up a (type, key) in the cache */
gp_cache_query(int type,byte * key,int keylen,void ** buffer,gp_cache_alloc alloc,void * userdata)319 int gp_cache_query(int type, byte* key, int keylen, void **buffer,
320 gp_cache_alloc alloc, void *userdata)
321 {
322 /* not yet implemented */
323 return -1;
324 }
325
326
327 /* ------ Screen management ------ */
328
329 /* Initialize the console. */
330 /* do nothing, we did it in gp_init()! */
331 void
gp_init_console(void)332 gp_init_console(void)
333 {
334 }
335
336
337 /* Write a string to the console. */
338
339 void
gp_console_puts(const char * str,uint size)340 gp_console_puts (const char *str, uint size)
341 {
342 /* fwrite (str, 1, size, stdout);*/
343 return;
344 }
345
346 const char *
gp_getenv_display(void)347 gp_getenv_display(void)
348 {
349 return NULL;
350 }
351
352