Subversion Repositories Kolibri OS

Rev

Rev 8663 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include <unistd.h>
  2. #include <signal.h>
  3. #include <stdlib.h>
  4. #include <limits.h>
  5. #include <sys/time.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <stdarg.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include <errno.h>
  14. #if !defined(__WIN32__) && !defined(_KOLIBRI)
  15. #include <sys/ipc.h>
  16. #include <sys/shm.h>
  17. #include <sys/stat.h>
  18. #include <sys/wait.h>
  19. #include <sys/mman.h>
  20. #endif
  21.  
  22. #ifdef _KOLIBRI
  23. #include <sys/ksys.h>
  24. #include <libgen.h>
  25. #endif
  26.  
  27. #include "quakedef.h"
  28.  
  29. qboolean                        isDedicated;
  30.  
  31. int noconinput = 0;
  32.  
  33. char *basedir = ".";
  34.  
  35. cvar_t  sys_linerefresh = {"sys_linerefresh","0"};// set for entity display
  36. cvar_t  sys_nostdout = {"sys_nostdout","0"};
  37.  
  38. // =======================================================================
  39. // General routines
  40. // =======================================================================
  41.  
  42. void Sys_DebugNumber(int y, int val)
  43. {
  44. }
  45.  
  46. void Sys_Printf (char *fmt, ...)
  47. {
  48.         va_list         argptr;
  49.         char            text[1024];
  50.        
  51.         va_start (argptr,fmt);
  52.         vsprintf (text,fmt,argptr);
  53.         va_end (argptr);
  54.  
  55.         fprintf(stderr, "%s", text);
  56.        
  57.         //Con_Print (text);
  58. }
  59.  
  60. void Sys_Quit (void)
  61. {
  62.         Host_Shutdown();
  63.         exit(0);
  64. }
  65.  
  66. void Sys_Init(void)
  67. {
  68. #if id386
  69.         Sys_SetFPCW();
  70. #endif
  71. }
  72.  
  73. #if !id386
  74.  
  75. /*
  76. ================
  77. Sys_LowFPPrecision
  78. ================
  79. */
  80. void Sys_LowFPPrecision (void)
  81. {
  82. // causes weird problems on Nextstep
  83. }
  84.  
  85.  
  86. /*
  87. ================
  88. Sys_HighFPPrecision
  89. ================
  90. */
  91. void Sys_HighFPPrecision (void)
  92. {
  93. // causes weird problems on Nextstep
  94. }
  95.  
  96. #endif  // !id386
  97.  
  98.  
  99. void Sys_Error (char *error, ...)
  100. {
  101.     va_list     argptr;
  102.     char        string[1024];
  103.  
  104.     va_start (argptr,error);
  105.     vsprintf (string,error,argptr);
  106.     va_end (argptr);
  107.  
  108.         fprintf(stderr, "Error: %s\n", string);
  109.  
  110.         Host_Shutdown ();
  111.         exit (1);
  112. }
  113.  
  114. void Sys_Warn (char *warning, ...)
  115. {
  116.     va_list     argptr;
  117.     char        string[1024];
  118.    
  119.     va_start (argptr,warning);
  120.     vsprintf (string,warning,argptr);
  121.     va_end (argptr);
  122.  
  123.         fprintf(stderr, "Warning: %s", string);
  124. }
  125.  
  126. /*
  127. ===============================================================================
  128.  
  129. FILE IO
  130.  
  131. ===============================================================================
  132. */
  133.  
  134. #define MAX_HANDLES             10
  135. FILE    *sys_handles[MAX_HANDLES];
  136.  
  137. int             findhandle (void)
  138. {
  139.         int             i;
  140.        
  141.         for (i=1 ; i<MAX_HANDLES ; i++)
  142.                 if (!sys_handles[i])
  143.                         return i;
  144.         Sys_Error ("out of handles");
  145.         return -1;
  146. }
  147.  
  148. /*
  149. ================
  150. Qfilelength
  151. ================
  152. */
  153. static int Qfilelength (FILE *f)
  154. {
  155.         int             pos;
  156.         int             end;
  157.  
  158.         pos = ftell (f);
  159.         fseek (f, 0, SEEK_END);
  160.         end = ftell (f);
  161.         fseek (f, pos, SEEK_SET);
  162.  
  163.         return end;
  164. }
  165.  
  166. int Sys_FileOpenRead (char *path, int *hndl)
  167. {
  168.         FILE    *f;
  169.         int             i;
  170.        
  171.         i = findhandle ();
  172.  
  173.         f = fopen(path, "rb");
  174.         if (!f)
  175.         {
  176.                 *hndl = -1;
  177.                 return -1;
  178.         }
  179.         sys_handles[i] = f;
  180.         *hndl = i;
  181.        
  182.         return Qfilelength(f);
  183. }
  184.  
  185. int Sys_FileOpenWrite (char *path)
  186. {
  187.         FILE    *f;
  188.         int             i;
  189.        
  190.         i = findhandle ();
  191.  
  192.         f = fopen(path, "wb");
  193.         if (!f)
  194.                 Sys_Error ("Error opening %s: %s", path,strerror(errno));
  195.         sys_handles[i] = f;
  196.        
  197.         return i;
  198. }
  199.  
  200. void Sys_FileClose (int handle)
  201. {
  202.         if ( handle >= 0 ) {
  203.                 fclose (sys_handles[handle]);
  204.                 sys_handles[handle] = NULL;
  205.         }
  206. }
  207.  
  208. void Sys_FileSeek (int handle, int position)
  209. {
  210.         if ( handle >= 0 ) {
  211.                 fseek (sys_handles[handle], position, SEEK_SET);
  212.         }
  213. }
  214.  
  215. int Sys_FileRead (int handle, void *dst, int count)
  216. {
  217.         char *data;
  218.         int size, done;
  219.  
  220.         size = 0;
  221.         if ( handle >= 0 ) {
  222.                 data = dst;
  223.                 while ( count > 0 ) {
  224.                         done = fread (data, 1, count, sys_handles[handle]);
  225.                         if ( done == 0 ) {
  226.                                 break;
  227.                         }
  228.                         data += done;
  229.                         count -= done;
  230.                         size += done;
  231.                 }
  232.         }
  233.         return size;
  234.                
  235. }
  236.  
  237. int Sys_FileWrite (int handle, void *src, int count)
  238. {
  239.         char *data;
  240.         int size, done;
  241.  
  242.         size = 0;
  243.         if ( handle >= 0 ) {
  244.                 data = src;
  245.                 while ( count > 0 ) {
  246.                         done = fread (data, 1, count, sys_handles[handle]);
  247.                         if ( done == 0 ) {
  248.                                 break;
  249.                         }
  250.                         data += done;
  251.                         count -= done;
  252.                         size += done;
  253.                 }
  254.         }
  255.         return size;
  256. }
  257.  
  258. int     Sys_FileTime (char *path)
  259. {
  260.         FILE    *f;
  261.        
  262.         f = fopen(path, "rb");
  263.         if (f)
  264.         {
  265.                 fclose(f);
  266.                 return 1;
  267.         }
  268.        
  269.         return -1;
  270. }
  271.  
  272. void Sys_mkdir (char *path)
  273. {
  274. #ifdef __WIN32__
  275.     mkdir (path);
  276. #else
  277.     mkdir (path, 0777);
  278. #endif
  279. }
  280.  
  281. void Sys_DebugLog(char *file, char *fmt, ...)
  282. {
  283.     va_list argptr;
  284.     static char data[1024];
  285.     FILE *fp;
  286.    
  287.     va_start(argptr, fmt);
  288.     vsprintf(data, fmt, argptr);
  289.     va_end(argptr);
  290.     fp = fopen(file, "a");
  291.     fwrite(data, strlen(data), 1, fp);
  292.     fclose(fp);
  293. }
  294.  
  295. double Sys_FloatTime (void)
  296. {
  297. #if defined(_KOLIBRI)
  298.         static int starttime = 0;
  299.  
  300.         if (!starttime) {
  301.                 starttime = _ksys_get_tick_count();
  302.         }
  303.  
  304.         int curtime = _ksys_get_tick_count();
  305.         return (curtime-starttime)*0.01;
  306. #elif defined(__WIN32__)
  307.  
  308.         static int starttime = 0;
  309.  
  310.         if ( ! starttime )
  311.                 starttime = clock();
  312.  
  313.         return (clock()-starttime)*1.0/1024;
  314.  
  315. #else
  316.  
  317.     struct timeval tp;
  318.     struct timezone tzp;
  319.     static int      secbase;
  320.    
  321.     gettimeofday(&tp, &tzp);  
  322.  
  323.     if (!secbase)
  324.     {
  325.         secbase = tp.tv_sec;
  326.         return tp.tv_usec/1000000.0;
  327.     }
  328.  
  329.     return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
  330.  
  331. #endif
  332. }
  333.  
  334. // =======================================================================
  335. // Sleeps for microseconds
  336. // =======================================================================
  337.  
  338. static volatile int oktogo;
  339.  
  340. void alarm_handler(int x)
  341. {
  342.         oktogo=1;
  343. }
  344.  
  345. byte *Sys_ZoneBase (int *size)
  346. {
  347.  
  348.         char *QUAKEOPT = getenv("QUAKEOPT");
  349.  
  350.         *size = 0xc00000;
  351.         if (QUAKEOPT)
  352.         {
  353.                 while (*QUAKEOPT)
  354.                         if (tolower(*QUAKEOPT++) == 'm')
  355.                         {
  356.                                 *size = atof(QUAKEOPT) * 1024*1024;
  357.                                 break;
  358.                         }
  359.         }
  360.         return malloc (*size);
  361.  
  362. }
  363.  
  364. void Sys_LineRefresh(void)
  365. {
  366. }
  367.  
  368. void Sys_Sleep(void)
  369. {
  370. #ifdef _KOLIBRI
  371.         _ksys_delay(1);
  372. #else
  373.         SDL_Delay(1);
  374. #endif
  375. }
  376.  
  377. #ifndef _KOLIBRI
  378. void floating_point_exception_handler(int whatever)
  379. {
  380. //      Sys_Warn("floating point exception\n");
  381.         signal(SIGFPE, floating_point_exception_handler);
  382. }
  383. #endif
  384.  
  385. void moncontrol(int x)
  386. {
  387. }
  388.  
  389. int main (int c, char **v)
  390. {
  391.         double time, oldtime, newtime;
  392.         quakeparms_t parms;
  393.         extern int vcrFile;
  394.         extern qboolean recording;
  395.         static int frame;
  396.  
  397.         moncontrol(0);
  398.  
  399. #ifndef _KOLIBRI
  400. //      signal(SIGFPE, floating_point_exception_handler);
  401.         signal(SIGFPE, SIG_IGN);
  402. #else
  403.         basedir = dirname(v[0]);
  404. #endif
  405.  
  406.         parms.memsize = 8*1024*1024;
  407.         parms.membase = malloc (parms.memsize);
  408.         parms.basedir = basedir;
  409.         parms.cachedir = NULL;
  410.  
  411.         COM_InitArgv(c, v);
  412.         parms.argc = com_argc;
  413.         parms.argv = com_argv;
  414.  
  415.         Sys_Init();
  416.  
  417.     Host_Init(&parms);
  418.  
  419.         Cvar_RegisterVariable (&sys_nostdout);
  420.  
  421.     oldtime = Sys_FloatTime () - 0.1;
  422.     while (1)
  423.     {
  424. // find time spent rendering last frame
  425.         newtime = Sys_FloatTime ();
  426.         time = newtime - oldtime;
  427.  
  428.         if (cls.state == ca_dedicated)
  429.         {   // play vcrfiles at max speed
  430.             if (time < sys_ticrate.value && (vcrFile == -1 || recording) )
  431.             {
  432. #ifdef _KOLIBRI
  433.                 _ksys_delay(1);
  434. #else
  435.                 SDL_Delay (1);
  436. #endif
  437.                 continue;       // not time to run a server only tic yet
  438.             }
  439.             time = sys_ticrate.value;
  440.         }
  441.  
  442.         if (time > sys_ticrate.value*2)
  443.             oldtime = newtime;
  444.         else
  445.             oldtime += time;
  446.  
  447.         if (++frame > 10)
  448.             moncontrol(1);      // profile only while we do each Quake frame
  449.         Host_Frame (time);
  450.         moncontrol(0);
  451.  
  452. // graphic debugging aids
  453.         if (sys_linerefresh.value)
  454.             Sys_LineRefresh ();
  455.     }
  456.  
  457. }
  458.  
  459.  
  460. /*
  461. ================
  462. Sys_MakeCodeWriteable
  463. ================
  464. */
  465. void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
  466. {
  467. #ifndef _KOLIBRI
  468.         int r;
  469.         unsigned long addr;
  470.         int psize = getpagesize();
  471.  
  472.         fprintf(stderr, "writable code %lx-%lx\n", startaddr, startaddr+length);
  473.  
  474.         addr = startaddr & ~(psize-1);
  475.  
  476.         r = mprotect((char*)addr, length + startaddr - addr, 7);
  477.  
  478.         if (r < 0)
  479.                 Sys_Error("Protection change failed\n");
  480. #endif
  481. }
  482.  
  483.