Quake-2

Quake 2 GPL Source Release
Log | Files | Refs

sys_solaris.c (6168B)


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