home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / quakeworld_src / client / sys_linux.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-17  |  7.9 KB  |  429 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20. #include <unistd.h>
  21. #include <signal.h>
  22. #include <stdlib.h>
  23. #include <limits.h>
  24. #include <sys/time.h>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <sys/ipc.h>
  31. #include <sys/shm.h>
  32. #include <sys/stat.h>
  33. #include <string.h>
  34. #include <ctype.h>
  35. #include <sys/wait.h>
  36. #include <sys/mman.h>
  37. #include <errno.h>
  38.  
  39. #include "quakedef.h"
  40.  
  41. int noconinput = 0;
  42. int nostdout = 0;
  43.  
  44. char *basedir = ".";
  45. char *cachedir = "/tmp";
  46.  
  47. cvar_t  sys_linerefresh = {"sys_linerefresh","0"};// set for entity display
  48.  
  49. // =======================================================================
  50. // General routines
  51. // =======================================================================
  52.  
  53. void Sys_DebugNumber(int y, int val)
  54. {
  55. }
  56.  
  57. /*
  58. void Sys_Printf (char *fmt, ...)
  59. {
  60.   va_list   argptr;
  61.   char    text[1024];
  62.   
  63.   va_start (argptr,fmt);
  64.   vsprintf (text,fmt,argptr);
  65.   va_end (argptr);
  66.   fprintf(stderr, "%s", text);
  67.   
  68.   Con_Print (text);
  69. }
  70.  
  71. void Sys_Printf (char *fmt, ...)
  72. {
  73.  
  74.     va_list     argptr;
  75.     char        text[1024], *t_p;
  76.     int         l, r;
  77.  
  78.     if (nostdout)
  79.         return;
  80.  
  81.     va_start (argptr,fmt);
  82.     vsprintf (text,fmt,argptr);
  83.     va_end (argptr);
  84.  
  85.     l = strlen(text);
  86.     t_p = text;
  87.  
  88. // make sure everything goes through, even though we are non-blocking
  89.     while (l)
  90.     {
  91.         r = write (1, text, l);
  92.         if (r != l)
  93.             sleep (0);
  94.         if (r > 0)
  95.         {
  96.             t_p += r;
  97.             l -= r;
  98.         }
  99.     }
  100.  
  101. }
  102. */
  103.  
  104. void Sys_Printf (char *fmt, ...)
  105. {
  106.   va_list   argptr;
  107.   char    text[2048];
  108.   unsigned char   *p;
  109.  
  110.   va_start (argptr,fmt);
  111.   vsprintf (text,fmt,argptr);
  112.   va_end (argptr);
  113.  
  114.   if (strlen(text) > sizeof(text))
  115.     Sys_Error("memory overwrite in Sys_Printf");
  116.  
  117.     if (nostdout)
  118.         return;
  119.  
  120.   for (p = (unsigned char *)text; *p; p++)
  121.     if ((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9)
  122.       printf("[%02x]", *p);
  123.     else
  124.       putc(*p, stdout);
  125. }
  126.  
  127. void Sys_Quit (void)
  128. {
  129.   Host_Shutdown();
  130.     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
  131.   exit(0);
  132. }
  133.  
  134. void Sys_Init(void)
  135. {
  136. #if id386
  137.   Sys_SetFPCW();
  138. #endif
  139. }
  140.  
  141. void Sys_Error (char *error, ...)
  142.     va_list     argptr;
  143.     char        string[1024];
  144.  
  145. // change stdin to non blocking
  146.     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
  147.     
  148.     va_start (argptr,error);
  149.     vsprintf (string,error,argptr);
  150.     va_end (argptr);
  151.   fprintf(stderr, "Error: %s\n", string);
  152.  
  153.   Host_Shutdown ();
  154.   exit (1);
  155.  
  156.  
  157. void Sys_Warn (char *warning, ...)
  158.     va_list     argptr;
  159.     char        string[1024];
  160.     
  161.     va_start (argptr,warning);
  162.     vsprintf (string,warning,argptr);
  163.     va_end (argptr);
  164.   fprintf(stderr, "Warning: %s", string);
  165.  
  166. /*
  167. ============
  168. Sys_FileTime
  169.  
  170. returns -1 if not present
  171. ============
  172. */
  173. int Sys_FileTime (char *path)
  174. {
  175.   struct  stat  buf;
  176.   
  177.   if (stat (path,&buf) == -1)
  178.     return -1;
  179.   
  180.   return buf.st_mtime;
  181. }
  182.  
  183.  
  184. void Sys_mkdir (char *path)
  185. {
  186.     mkdir (path, 0777);
  187. }
  188.  
  189. int Sys_FileOpenRead (char *path, int *handle)
  190. {
  191.   int h;
  192.   struct stat fileinfo;
  193.     
  194.   
  195.   h = open (path, O_RDONLY, 0666);
  196.   *handle = h;
  197.   if (h == -1)
  198.     return -1;
  199.   
  200.   if (fstat (h,&fileinfo) == -1)
  201.     Sys_Error ("Error fstating %s", path);
  202.  
  203.   return fileinfo.st_size;
  204. }
  205.  
  206. int Sys_FileOpenWrite (char *path)
  207. {
  208.   int     handle;
  209.  
  210.   umask (0);
  211.   
  212.   handle = open(path,O_RDWR | O_CREAT | O_TRUNC
  213.   , 0666);
  214.  
  215.   if (handle == -1)
  216.     Sys_Error ("Error opening %s: %s", path,strerror(errno));
  217.  
  218.   return handle;
  219. }
  220.  
  221. int Sys_FileWrite (int handle, void *src, int count)
  222. {
  223.   return write (handle, src, count);
  224. }
  225.  
  226. void Sys_FileClose (int handle)
  227. {
  228.   close (handle);
  229. }
  230.  
  231. void Sys_FileSeek (int handle, int position)
  232. {
  233.   lseek (handle, position, SEEK_SET);
  234. }
  235.  
  236. int Sys_FileRead (int handle, void *dest, int count)
  237. {
  238.     return read (handle, dest, count);
  239. }
  240.  
  241. void Sys_DebugLog(char *file, char *fmt, ...)
  242. {
  243.     va_list argptr; 
  244.     static char data[1024];
  245.     int fd;
  246.     
  247.     va_start(argptr, fmt);
  248.     vsprintf(data, fmt, argptr);
  249.     va_end(argptr);
  250. //    fd = open(file, O_WRONLY | O_BINARY | O_CREAT | O_APPEND, 0666);
  251.     fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0666);
  252.     write(fd, data, strlen(data));
  253.     close(fd);
  254. }
  255.  
  256. void Sys_EditFile(char *filename)
  257. {
  258.  
  259.   char cmd[256];
  260.   char *term;
  261.   char *editor;
  262.  
  263.   term = getenv("TERM");
  264.   if (term && !strcmp(term, "xterm"))
  265.   {
  266.     editor = getenv("VISUAL");
  267.     if (!editor)
  268.       editor = getenv("EDITOR");
  269.     if (!editor)
  270.       editor = getenv("EDIT");
  271.     if (!editor)
  272.       editor = "vi";
  273.     sprintf(cmd, "xterm -e %s %s", editor, filename);
  274.     system(cmd);
  275.   }
  276.  
  277. }
  278.  
  279. double Sys_DoubleTime (void)
  280. {
  281.     struct timeval tp;
  282.     struct timezone tzp; 
  283.     static int      secbase; 
  284.     
  285.     gettimeofday(&tp, &tzp);  
  286.  
  287.     if (!secbase)
  288.     {
  289.         secbase = tp.tv_sec;
  290.         return tp.tv_usec/1000000.0;
  291.     }
  292.  
  293.     return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
  294. }
  295.  
  296. // =======================================================================
  297. // Sleeps for microseconds
  298. // =======================================================================
  299.  
  300. static volatile int oktogo;
  301.  
  302. void alarm_handler(int x)
  303. {
  304.   oktogo=1;
  305. }
  306.  
  307. void Sys_LineRefresh(void)
  308. {
  309. }
  310.  
  311. void floating_point_exception_handler(int whatever)
  312. {
  313. //  Sys_Warn("floating point exception\n");
  314.   signal(SIGFPE, floating_point_exception_handler);
  315. }
  316.  
  317. char *Sys_ConsoleInput(void)
  318. {
  319. #if 0
  320.     static char text[256];
  321.     int     len;
  322.  
  323.   if (cls.state == ca_dedicated) {
  324.     len = read (0, text, sizeof(text));
  325.     if (len < 1)
  326.       return NULL;
  327.     text[len-1] = 0;    // rip off the /n and terminate
  328.  
  329.     return text;
  330.   }
  331. #endif
  332.   return NULL;
  333. }
  334.  
  335. #if !id386
  336. void Sys_HighFPPrecision (void)
  337. {
  338. }
  339.  
  340. void Sys_LowFPPrecision (void)
  341. {
  342. }
  343. #endif
  344.  
  345. int   skipframes;
  346.  
  347. int main (int c, char **v)
  348. {
  349.  
  350.   double    time, oldtime, newtime;
  351.   quakeparms_t parms;
  352.   int j;
  353.  
  354. //  static char cwd[1024];
  355.  
  356. //  signal(SIGFPE, floating_point_exception_handler);
  357.   signal(SIGFPE, SIG_IGN);
  358.  
  359.   memset(&parms, 0, sizeof(parms));
  360.  
  361.   COM_InitArgv(c, v);
  362.   parms.argc = com_argc;
  363.   parms.argv = com_argv;
  364.  
  365.   parms.memsize = 16*1024*1024;
  366.  
  367.   j = COM_CheckParm("-mem");
  368.   if (j)
  369.     parms.memsize = (int) (Q_atof(com_argv[j+1]) * 1024 * 1024);
  370.   parms.membase = malloc (parms.memsize);
  371.  
  372.   parms.basedir = basedir;
  373. // caching is disabled by default, use -cachedir to enable
  374. //  parms.cachedir = cachedir;
  375.  
  376.   noconinput = COM_CheckParm("-noconinput");
  377.   if (!noconinput)
  378.     fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
  379.  
  380.   if (COM_CheckParm("-nostdout"))
  381.     nostdout = 1;
  382.  
  383.   Sys_Init();
  384.  
  385.     Host_Init(&parms);
  386.  
  387.     oldtime = Sys_DoubleTime ();
  388.     while (1)
  389.     {
  390. // find time spent rendering last frame
  391.         newtime = Sys_DoubleTime ();
  392.         time = newtime - oldtime;
  393.  
  394.     Host_Frame(time);
  395.     oldtime = newtime;
  396.     }
  397.  
  398. }
  399.  
  400.  
  401. /*
  402. ================
  403. Sys_MakeCodeWriteable
  404. ================
  405. */
  406. void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
  407. {
  408.  
  409.   int r;
  410.   unsigned long addr;
  411.   int psize = getpagesize();
  412.  
  413.   addr = (startaddr & ~(psize-1)) - psize;
  414.  
  415. //  fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
  416. //      addr, startaddr+length, length);
  417.  
  418.   r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
  419.  
  420.   if (r < 0)
  421.         Sys_Error("Protection change failed\n");
  422.  
  423. }
  424.  
  425.