gp_mac.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* Copyright (C) 1989 - 1995, 1997 Artifex Software, Inc. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author
  4. or distributor accepts any responsibility for the consequences of using it,
  5. or for whether it serves any particular purpose or works at all, unless he
  6. or she says so in writing. Refer to the Aladdin Ghostscript Free Public
  7. License (the "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License,
  9. normally in a plain ASCII text file named PUBLIC. The License grants you
  10. the right to copy, modify and redistribute AFPL Ghostscript, but only
  11. under certain conditions described in the License. Among other things, the
  12. License requires that the copyright notice and this notice be preserved on
  13. all copies.
  14. */
  15. #include <Palettes.h>
  16. #include <Aliases.h>
  17. #include <Quickdraw.h>
  18. #include <QDOffscreen.h>
  19. #include <AppleEvents.h>
  20. #include <Fonts.h>
  21. #include <Controls.h>
  22. #include <Script.h>
  23. #include <Timer.h>
  24. #include <Folders.h>
  25. #include <Resources.h>
  26. #include <Sound.h>
  27. #include <ToolUtils.h>
  28. #include <Menus.h>
  29. #include <LowMem.h>
  30. #include <Devices.h>
  31. #include <Scrap.h>
  32. #include <StringCompare.h>
  33. #include <Fonts.h>
  34. #include <FixMath.h>
  35. #include <Resources.h>
  36. #include "math_.h"
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <Time.h>
  40. #include <sys/time.h>
  41. #include <sys/types.h>
  42. #include "memory_.h"
  43. #include "string_.h"
  44. #include "gx.h"
  45. #include "gp.h"
  46. #include "gsdll.h"
  47. #include "gpcheck.h"
  48. #include "gp_mac.h"
  49. #include "gdebug.h"
  50. #include "sys/stat.h"
  51. #include <stdarg.h>
  52. /*#include "gpgetenv.h"*/
  53. #include "gsexit.h"
  54. HWND hwndtext; /* used as identifier for the dll instance */
  55. void
  56. noMemoryExit(void)
  57. {
  58. Alert(307, NULL);
  59. ExitToShell();
  60. }
  61. char *
  62. mygetenv(const char * env) {
  63. char *p;
  64. FSSpec pFile;
  65. OSErr err = 0;
  66. char fpath[256]="";
  67. return 0;
  68. }
  69. void
  70. gp_init (void)
  71. {
  72. extern char *gs_lib_default_path;
  73. extern char *gs_init_file;
  74. #if 0
  75. /*...Initialize Ghostscript's default library paths and initialization file...*/
  76. {
  77. int i;
  78. char **p;
  79. for (i = iGSLibPathStr, p = &gs_lib_default_path;
  80. i <= iGSInitFileStr;
  81. i++, p = &gs_init_file)
  82. {
  83. GetIndString (string, MACSTRS_RES_ID, i);
  84. (void) PtoCstr (string);
  85. *p = gs_malloc (1, (size_t) (strlen ((char *) string) + 1), "gp_init");
  86. strcpy (*p, (char *) string);
  87. }
  88. }
  89. #endif
  90. }
  91. void
  92. gp_exit(int exit_status, int code)
  93. {
  94. }
  95. /* Exit the program. */
  96. void
  97. gp_do_exit(int exit_status)
  98. {
  99. /* exit(exit_status);*/
  100. }
  101. /* gettimeofday */
  102. #ifndef HZ
  103. # define HZ 100 /* see sys/param.h */
  104. #endif
  105. int
  106. gettimeofday(struct timeval *tvp)
  107. {
  108. struct tm tms;
  109. static unsigned long offset = 0;
  110. long ticks;
  111. if (!offset) {
  112. time(&offset);
  113. offset -= (time((unsigned long *)&tms) / HZ);
  114. }
  115. ticks = time((unsigned long *)&tms);
  116. tvp->tv_sec = ticks / HZ + offset;
  117. tvp->tv_usec = (ticks % HZ) * (1000 * 1000 / HZ);
  118. return 0;
  119. }
  120. /* ------ Date and time ------ */
  121. #define gettimeofday_no_timezone 0
  122. /* Read the current time (in seconds since Jan. 1, 1970) */
  123. /* and fraction (in nanoseconds). */
  124. void
  125. gp_get_realtime(long *pdt)
  126. {
  127. struct timeval tp;
  128. if (gettimeofday(&tp) == -1) {
  129. lprintf("Ghostscript: gettimeofday failed!\n");
  130. gs_exit(1);
  131. }
  132. /* tp.tv_sec is #secs since Jan 1, 1970 */
  133. pdt[0] = tp.tv_sec;
  134. /* Some Unix systems (e.g., Interactive 3.2 r3.0) return garbage */
  135. /* in tp.tv_usec. Try to filter out the worst of it here. */
  136. pdt[1] = tp.tv_usec >= 0 && tp.tv_usec < 1000000 ? tp.tv_usec * 1000 : 0;
  137. #ifdef DEBUG_CLOCK
  138. printf("tp.tv_sec = %d tp.tv_usec = %d pdt[0] = %ld pdt[1] = %ld\n",
  139. tp.tv_sec, tp.tv_usec, pdt[0], pdt[1]);
  140. #endif
  141. }
  142. /* Read the current user CPU time (in seconds) */
  143. /* and fraction (in nanoseconds). */
  144. void
  145. gp_get_usertime(long *pdt)
  146. {
  147. gp_get_realtime(pdt); /* Use an approximation on other hosts. */
  148. pdt[0] -= (char)rand(); // was needed, if used for random generator seed (g3 is too fast)
  149. }
  150. /*
  151. * Get the string corresponding to an OS error number.
  152. * If no string is available, return NULL. The caller may assume
  153. * the string is allocated statically and permanently.
  154. */
  155. const char * gp_strerror(P1(int))
  156. {
  157. return NULL;
  158. }
  159. /* ------ Date and time ------ */
  160. /* Read the current date (in days since Jan. 1, 1980) */
  161. /* and time (in milliseconds since midnight). */
  162. void
  163. gp_get_clock (long *pdt) {
  164. gp_get_realtime(pdt); /* Use an approximation on other hosts. */
  165. }
  166. void
  167. gpp_get_clock (long *pdt)
  168. {
  169. long secs;
  170. DateTimeRec dateRec;
  171. static DateTimeRec baseDateRec = {1980, 1, 1, 0, 0, 0, 1};
  172. long pdtmp[2];
  173. void do_get_clock (DateTimeRec *dateRec, long *pdt);
  174. GetDateTime ((unsigned long *) &secs);
  175. // SecsondsToDate (secs, &dateRec);
  176. do_get_clock (&dateRec , pdt);
  177. do_get_clock (&baseDateRec, pdtmp);
  178. /* If the date is reasonable, subtract the days since Jan. 1, 1980 */
  179. if (pdtmp[0] < pdt[0])
  180. pdt[0] -= pdtmp[0];
  181. #ifdef DEBUG_CLOCK
  182. printf("pdt[0] = %ld pdt[1] = %ld\n", pdt[0], pdt[1]);
  183. #endif
  184. }
  185. UnsignedWide beginMicroTickCount={0,0};
  186. /* Read the current date (in days since Jan. 1, 1980) */
  187. /* and time (in nanoseconds since midnight). */
  188. void
  189. gpp_get_realtime (long *pdt)
  190. {
  191. UnsignedWide microTickCount,
  192. nMicroTickCount;
  193. long idate;
  194. static const int mstart[12] =
  195. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  196. long secs;
  197. DateTimeRec dateRec;
  198. static DateTimeRec baseDateRec = {1980, 1, 1, 0, 0, 0, 1};
  199. void do_get_clock (DateTimeRec *dateRec, long *pdt);
  200. if ((beginMicroTickCount.lo == 0)&&(beginMicroTickCount.hi == 0) ) {
  201. Microseconds(&beginMicroTickCount);
  202. }
  203. Microseconds(&microTickCount);
  204. nMicroTickCount.lo = microTickCount.lo - beginMicroTickCount.lo;
  205. nMicroTickCount.hi = microTickCount.hi - beginMicroTickCount.hi;
  206. GetDateTime ((unsigned long *) &secs);
  207. //SecondsToDate (secs, &dateRec);
  208. /* If the date is reasonable, subtract the days since Jan. 1, 1980 */
  209. idate = ((long) dateRec.year - 1980) * 365 + /* days per year */
  210. (((long) dateRec.year - 1)/4 - 1979/4) + /* intervening leap days */
  211. (1979/100 - ((long) dateRec.year - 1)/100) +
  212. (((long) dateRec.month - 1)/400 - 1979/400) +
  213. mstart[dateRec.month - 1] + /* month is 1-origin */
  214. dateRec.day - 1; /* day of month is 1-origin */
  215. idate += (2 < dateRec.month
  216. && (dateRec.year % 4 == 0
  217. && (dateRec.year % 100 != 0 || dateRec.year % 400 == 0)));
  218. pdt[0] = ((idate*24 + dateRec.hour) * 60 + dateRec.minute) * 60 + dateRec.second;
  219. pdt[1] = nMicroTickCount.lo * 100;
  220. //#define DEBUG_CLOCK 1
  221. #ifdef DEBUG_CLOCK
  222. fprintf(stderr,"pdt[0] = %ld pdt[1] = %ld\n", pdt[0], pdt[1]);
  223. fprintf(stderr,"b hi[0] = %ld lo[1] = %ld\n", beginMicroTickCount.hi, beginMicroTickCount.lo);
  224. fprintf(stderr,"m hi[0] = %ld lo[1] = %ld\n", microTickCount.hi, microTickCount.lo);
  225. #endif
  226. }
  227. static void
  228. do_get_clock (DateTimeRec *dateRec, long *pdt)
  229. {
  230. long idate;
  231. static const int mstart[12] =
  232. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  233. /* This gets UTC, not local time */
  234. /* We have no way of knowing the time zone correction */
  235. idate = ((long) dateRec->year - 1980) * 365 + /* days per year */
  236. (((long) dateRec->year - 1)/4 - 1979/4) + /* intervening leap days */
  237. (1979/100 - ((long) dateRec->year - 1)/100) +
  238. (((long) dateRec->month - 1)/400 - 1979/400) +
  239. mstart[dateRec->month - 1] + /* month is 1-origin */
  240. dateRec->day - 1; /* day of month is 1-origin */
  241. idate += (2 < dateRec->month
  242. && (dateRec->year % 4 == 0
  243. && (dateRec->year % 100 != 0 || dateRec->year % 400 == 0)));
  244. pdt[0] = ((idate*24 + dateRec->hour) * 60 + dateRec->minute) * 60 + dateRec->second;
  245. pdt[1] = 0; //dateRec->milisecond * 1000000;
  246. }
  247. void
  248. gpp_get_usertime(long *pdt)
  249. {
  250. gp_get_realtime(pdt); /* Use an approximation for now. */
  251. }
  252. /* ------ Screen management ------ */
  253. /* Initialize the console. */
  254. /* do nothing, we did it in gp_init()! */
  255. void
  256. gp_init_console(P0())
  257. {
  258. }
  259. /* Write a string to the console. */
  260. void
  261. gp_console_puts (const char *str, uint size)
  262. {
  263. /* fwrite (str, 1, size, stdout);*/
  264. return;
  265. }
  266. /* Make the console current on the screen. */
  267. /*
  268. int
  269. gp_make_console_current (gx_device *dev)
  270. {
  271. return 0;
  272. }
  273. */
  274. /* Make the graphics current on the screen. */
  275. /*
  276. int
  277. gp_make_graphics_current (gx_device *dev)
  278. {
  279. return 0;
  280. }
  281. */
  282. const char *
  283. gp_getenv_display(P0())
  284. {
  285. return NULL;
  286. }
  287. int
  288. gp_check_interrupts(void)
  289. {
  290. static unsigned long lastYieldTicks = 0;
  291. if ((TickCount() - lastYieldTicks) > 2) {
  292. lastYieldTicks = TickCount();
  293. /* the hwnd parameter which is submitted in gsdll_init to the DLL */
  294. /* is returned in every gsdll_poll message in the count parameter */
  295. return (*pgsdll_callback) (GSDLL_POLL, 0, (long) hwndtext);
  296. return 0;
  297. } else {
  298. return 0;
  299. }
  300. }