sys_solaris.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <sys/ipc.h>
  12. #include <sys/shm.h>
  13. #include <sys/stat.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <sys/wait.h>
  17. #include <sys/mman.h>
  18. #include <errno.h>
  19. #include <sys/file.h>
  20. #include <dlfcn.h>
  21. #include "../qcommon/qcommon.h"
  22. cvar_t *nostdout;
  23. unsigned sys_frame_time;
  24. qboolean stdin_active = true;
  25. // =======================================================================
  26. // General routines
  27. // =======================================================================
  28. void Sys_ConsoleOutput (char *string)
  29. {
  30. if (nostdout && nostdout->value)
  31. return;
  32. fputs(string, stdout);
  33. }
  34. void Sys_Printf (char *fmt, ...)
  35. {
  36. va_list argptr;
  37. char text[1024];
  38. unsigned char *p;
  39. va_start (argptr,fmt);
  40. vsprintf (text,fmt,argptr);
  41. va_end (argptr);
  42. if (strlen(text) > sizeof(text))
  43. Sys_Error("memory overwrite in Sys_Printf");
  44. if (nostdout && nostdout->value)
  45. return;
  46. for (p = (unsigned char *)text; *p; p++) {
  47. *p &= 0x7f;
  48. if ((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9)
  49. printf("[%02x]", *p);
  50. else
  51. putc(*p, stdout);
  52. }
  53. }
  54. void Sys_Quit (void)
  55. {
  56. CL_Shutdown ();
  57. Qcommon_Shutdown ();
  58. fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
  59. _exit(0);
  60. }
  61. void Sys_Init(void)
  62. {
  63. #if id386
  64. // Sys_SetFPCW();
  65. #endif
  66. }
  67. void Sys_Error (char *error, ...)
  68. {
  69. va_list argptr;
  70. char string[1024];
  71. // change stdin to non blocking
  72. fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
  73. va_start (argptr,error);
  74. vsprintf (string,error,argptr);
  75. va_end (argptr);
  76. fprintf(stderr, "Error: %s\n", string);
  77. CL_Shutdown ();
  78. Qcommon_Shutdown ();
  79. _exit (1);
  80. }
  81. void Sys_Warn (char *warning, ...)
  82. {
  83. va_list argptr;
  84. char string[1024];
  85. va_start (argptr,warning);
  86. vsprintf (string,warning,argptr);
  87. va_end (argptr);
  88. fprintf(stderr, "Warning: %s", string);
  89. }
  90. /*
  91. ============
  92. Sys_FileTime
  93. returns -1 if not present
  94. ============
  95. */
  96. int Sys_FileTime (char *path)
  97. {
  98. struct stat buf;
  99. if (stat (path,&buf) == -1)
  100. return -1;
  101. return buf.st_mtime;
  102. }
  103. void floating_point_exception_handler(int whatever)
  104. {
  105. // Sys_Warn("floating point exception\n");
  106. signal(SIGFPE, floating_point_exception_handler);
  107. }
  108. char *Sys_ConsoleInput(void)
  109. {
  110. static char text[256];
  111. int len;
  112. fd_set fdset;
  113. struct timeval timeout;
  114. if (!dedicated || !dedicated->value)
  115. return NULL;
  116. if (!stdin_active)
  117. return NULL;
  118. FD_ZERO(&fdset);
  119. FD_SET(0, &fdset); // stdin
  120. timeout.tv_sec = 0;
  121. timeout.tv_usec = 0;
  122. if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
  123. return NULL;
  124. len = read (0, text, sizeof(text));
  125. if (len == 0) { // eof!
  126. stdin_active = false;
  127. return NULL;
  128. }
  129. if (len < 1)
  130. return NULL;
  131. text[len-1] = 0; // rip off the /n and terminate
  132. return text;
  133. }
  134. /*****************************************************************************/
  135. static void *game_library;
  136. /*
  137. =================
  138. Sys_UnloadGame
  139. =================
  140. */
  141. void Sys_UnloadGame (void)
  142. {
  143. if (game_library)
  144. dlclose (game_library);
  145. game_library = NULL;
  146. }
  147. /*
  148. =================
  149. Sys_GetGameAPI
  150. Loads the game dll
  151. =================
  152. */
  153. void *Sys_GetGameAPI (void *parms)
  154. {
  155. void *(*GetGameAPI) (void *);
  156. char name[MAX_OSPATH];
  157. char curpath[MAX_OSPATH];
  158. char *path;
  159. #ifdef __i386__
  160. const char *gamename = "gamei386.so";
  161. #elif defined __sun__
  162. const char *gamename = "gamesparc.so";
  163. #else
  164. #error Unknown arch
  165. #endif
  166. if (game_library)
  167. Com_Error (ERR_FATAL, "Sys_GetGameAPI without Sys_UnloadingGame");
  168. getcwd(curpath, sizeof(curpath));
  169. Com_Printf("------- Loading %s -------", gamename);
  170. // now run through the search paths
  171. path = NULL;
  172. while (1)
  173. {
  174. path = FS_NextPath (path);
  175. if (!path)
  176. return NULL; // couldn't find one anywhere
  177. sprintf (name, "%s/%s/%s", curpath, path, gamename);
  178. game_library = dlopen (name, RTLD_NOW );
  179. if (game_library)
  180. {
  181. Com_DPrintf ("LoadLibrary (%s)\n",name);
  182. break;
  183. } else
  184. Com_Printf("error: %s\n", dlerror());
  185. }
  186. GetGameAPI = (void *)dlsym (game_library, "GetGameAPI");
  187. if (!GetGameAPI)
  188. {
  189. Sys_UnloadGame ();
  190. return NULL;
  191. }
  192. return GetGameAPI (parms);
  193. }
  194. /*****************************************************************************/
  195. void Sys_AppActivate (void)
  196. {
  197. }
  198. void Sys_SendKeyEvents (void)
  199. {
  200. // grab frame time
  201. sys_frame_time = Sys_Milliseconds();
  202. }
  203. /*****************************************************************************/
  204. char *Sys_GetClipboardData(void)
  205. {
  206. return NULL;
  207. }
  208. int main (int argc, char **argv)
  209. {
  210. int time, oldtime, newtime;
  211. #if 0
  212. int newargc;
  213. char **newargv;
  214. int i;
  215. // force dedicated
  216. newargc = argc;
  217. newargv = malloc((argc + 3) * sizeof(char *));
  218. newargv[0] = argv[0];
  219. newargv[1] = "+set";
  220. newargv[2] = "dedicated";
  221. newargv[3] = "1";
  222. for (i = 1; i < argc; i++)
  223. newargv[i + 3] = argv[i];
  224. newargc += 3;
  225. Qcommon_Init(newargc, newargv);
  226. #else
  227. Qcommon_Init(argc, argv);
  228. #endif
  229. fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
  230. nostdout = Cvar_Get("nostdout", "0", 0);
  231. if (!nostdout->value) {
  232. fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
  233. // printf ("Linux Quake -- Version %0.3f\n", LINUX_VERSION);
  234. }
  235. oldtime = Sys_Milliseconds ();
  236. while (1)
  237. {
  238. // find time spent rendering last frame
  239. do {
  240. newtime = Sys_Milliseconds ();
  241. time = newtime - oldtime;
  242. } while (time < 1);
  243. Qcommon_Frame (time);
  244. oldtime = newtime;
  245. }
  246. }
  247. void Sys_CopyProtect(void)
  248. {
  249. return;
  250. }
  251. #if 0
  252. /*
  253. ================
  254. Sys_MakeCodeWriteable
  255. ================
  256. */
  257. void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
  258. {
  259. int r;
  260. unsigned long addr;
  261. int psize = getpagesize();
  262. addr = (startaddr & ~(psize-1)) - psize;
  263. // fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
  264. // addr, startaddr+length, length);
  265. r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
  266. if (r < 0)
  267. Sys_Error("Protection change failed\n");
  268. }
  269. #endif