gp_mac.c 8.2 KB

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