sys_rhap.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include <libc.h>
  2. #import <AppKit/AppKit.h>
  3. #include "../qcommon/qcommon.h"
  4. int curtime;
  5. int sys_frame_time;
  6. void Sys_UnloadGame (void)
  7. {
  8. }
  9. void *GetGameAPI (void *import);
  10. void *Sys_GetGameAPI (void *parms)
  11. {
  12. // we are hard-linked in, so no need to load anything
  13. return GetGameAPI (parms);
  14. }
  15. void Sys_CopyProtect (void)
  16. {
  17. }
  18. char *Sys_GetClipboardData( void )
  19. {
  20. return NULL;
  21. }
  22. //===========================================================================
  23. int hunkcount;
  24. byte *membase;
  25. int hunkmaxsize;
  26. int cursize;
  27. //#define VIRTUAL_ALLOC
  28. void *Hunk_Begin (int maxsize)
  29. {
  30. // reserve a huge chunk of memory, but don't commit any yet
  31. cursize = 0;
  32. hunkmaxsize = maxsize;
  33. #ifdef VIRTUAL_ALLOC
  34. membase = VirtualAlloc (NULL, maxsize, MEM_RESERVE, PAGE_NOACCESS);
  35. #else
  36. membase = malloc (maxsize);
  37. memset (membase, 0, maxsize);
  38. #endif
  39. if (!membase)
  40. Sys_Error ("VirtualAlloc reserve failed");
  41. return (void *)membase;
  42. }
  43. void *Hunk_Alloc (int size)
  44. {
  45. void *buf;
  46. // round to cacheline
  47. size = (size+31)&~31;
  48. #ifdef VIRTUAL_ALLOC
  49. // commit pages as needed
  50. // buf = VirtualAlloc (membase+cursize, size, MEM_COMMIT, PAGE_READWRITE);
  51. buf = VirtualAlloc (membase, cursize+size, MEM_COMMIT, PAGE_READWRITE);
  52. if (!buf)
  53. {
  54. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &buf, 0, NULL);
  55. Sys_Error ("VirtualAlloc commit failed.\n%s", buf);
  56. }
  57. #endif
  58. cursize += size;
  59. if (cursize > hunkmaxsize)
  60. Sys_Error ("Hunk_Alloc overflow");
  61. return (void *)(membase+cursize-size);
  62. }
  63. int Hunk_End (void)
  64. {
  65. // free the remaining unused virtual memory
  66. #if 0
  67. void *buf;
  68. // write protect it
  69. buf = VirtualAlloc (membase, cursize, MEM_COMMIT, PAGE_READONLY);
  70. if (!buf)
  71. Sys_Error ("VirtualAlloc commit failed");
  72. #endif
  73. hunkcount++;
  74. //Com_Printf ("hunkcount: %i\n", hunkcount);
  75. return cursize;
  76. }
  77. void Hunk_Free (void *base)
  78. {
  79. if ( base )
  80. #ifdef VIRTUAL_ALLOC
  81. VirtualFree (base, 0, MEM_RELEASE);
  82. #else
  83. free (base);
  84. #endif
  85. hunkcount--;
  86. }
  87. //===========================================================================
  88. void Sys_Mkdir (char *path)
  89. {
  90. if (mkdir (path, 0777) != -1)
  91. return;
  92. if (errno != EEXIST)
  93. Com_Error (ERR_FATAL, "mkdir %s: %s",path, strerror(errno));
  94. }
  95. char *Sys_FindFirst (char *path, unsigned musthave, unsigned canthave)
  96. {
  97. return NULL;
  98. }
  99. char *Sys_FindNext (unsigned musthave, unsigned canthave)
  100. {
  101. return NULL;
  102. }
  103. void Sys_FindClose (void)
  104. {
  105. }
  106. /*
  107. ================
  108. Sys_Milliseconds
  109. ================
  110. */
  111. int Sys_Milliseconds (void)
  112. {
  113. struct timeval tp;
  114. struct timezone tzp;
  115. static int secbase;
  116. gettimeofday(&tp, &tzp);
  117. if (!secbase)
  118. {
  119. secbase = tp.tv_sec;
  120. return tp.tv_usec/1000;
  121. }
  122. curtime = (tp.tv_sec - secbase)*1000 + tp.tv_usec/1000;
  123. return curtime;
  124. }
  125. /*
  126. ================
  127. Sys_Error
  128. ================
  129. */
  130. void Sys_Error (char *error, ...)
  131. {
  132. va_list argptr;
  133. char string[1024];
  134. // change stdin to non blocking
  135. fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
  136. va_start (argptr,error);
  137. vsprintf (string,error,argptr);
  138. va_end (argptr);
  139. printf ("Fatal error: %s\n",string);
  140. if (!NSApp)
  141. { // appkit isn't running, so don't try to pop up a panel
  142. exit (1);
  143. }
  144. NSRunAlertPanel (@"Fatal error",[NSString stringWithCString: string]
  145. ,@"exit",NULL,NULL);
  146. [NSApp terminate: NULL];
  147. exit(1);
  148. }
  149. /*
  150. ================
  151. Sys_Printf
  152. ================
  153. */
  154. void Sys_ConsoleOutput (char *text)
  155. {
  156. char *t_p;
  157. int l, r;
  158. l = strlen(text);
  159. t_p = text;
  160. // make sure everything goes through, even though we are non-blocking
  161. while (l)
  162. {
  163. r = write (1, text, l);
  164. if (r != l)
  165. sleep (0);
  166. if (r > 0)
  167. {
  168. t_p += r;
  169. l -= r;
  170. }
  171. }
  172. }
  173. /*
  174. ================
  175. Sys_Quit
  176. ================
  177. */
  178. void Sys_Quit (void)
  179. {
  180. // change stdin to blocking
  181. fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
  182. if (!NSApp)
  183. exit (0); // appkit isn't running
  184. [NSApp terminate:nil];
  185. }
  186. /*
  187. ================
  188. Sys_Init
  189. ================
  190. */
  191. void Sys_Init(void)
  192. {
  193. moncontrol(0); // turn off profiling except during real Quake work
  194. // change stdin to non blocking
  195. fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
  196. }
  197. extern NSWindow *vid_window_i;
  198. void Sys_AppActivate (void)
  199. {
  200. [vid_window_i makeKeyAndOrderFront: nil];
  201. }
  202. /*
  203. ================
  204. Sys_SendKeyEvents
  205. service any pending appkit events
  206. ================
  207. */
  208. void Sys_SendKeyEvents (void)
  209. {
  210. NSEvent *event;
  211. NSDate *date;
  212. date = [NSDate date];
  213. do
  214. {
  215. event = [NSApp
  216. nextEventMatchingMask: 0xffffffff
  217. untilDate: date
  218. inMode: @"NSDefaultRunLoopMode"
  219. dequeue: YES];
  220. if (event)
  221. [NSApp sendEvent: event];
  222. } while (event);
  223. // grab frame time
  224. sys_frame_time = Sys_Milliseconds();
  225. }
  226. /*
  227. ================
  228. Sys_ConsoleInput
  229. Checks for a complete line of text typed in at the console, then forwards
  230. it to the host command processor
  231. ================
  232. */
  233. char *Sys_ConsoleInput (void)
  234. {
  235. static char text[256];
  236. int len;
  237. len = read (0, text, sizeof(text));
  238. if (len < 1)
  239. return NULL;
  240. text[len-1] = 0; // rip off the /n and terminate
  241. return text;
  242. }
  243. /*
  244. =============
  245. main
  246. =============
  247. */
  248. void main (int argc, char **argv)
  249. {
  250. int frame;
  251. NSAutoreleasePool *pool;
  252. int oldtime, t;
  253. pool = [[NSAutoreleasePool alloc] init];
  254. Qcommon_Init (argc, argv);
  255. [pool release];
  256. oldtime = Sys_Milliseconds ();
  257. while (1)
  258. {
  259. pool =[[NSAutoreleasePool alloc] init];
  260. if (++frame > 10)
  261. moncontrol(1);// profile only while we do each Quake frame
  262. t = Sys_Milliseconds ();
  263. Qcommon_Frame (t - oldtime);
  264. oldtime = t;
  265. moncontrol(0);
  266. [pool release];
  267. }
  268. }