~ubuntu-branches/ubuntu/jaunty/openarena/jaunty

« back to all changes in this revision

Viewing changes to code/sys/sys_main.c

  • Committer: Bazaar Package Importer
  • Author(s): Bruno "Fuddl" Kleinert, Bruno "Fuddl" Kleinert
  • Date: 2008-04-24 14:33:54 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080424143354-0cuxsalv98ajw2js
Tags: 0.7.6-1
[ Bruno "Fuddl" Kleinert ]
* New upstream release
* Freshen 10_fix_build_and_binary_on_alpha.dpatch to apply to latest
  upstream sources
* Remove 10-fix_menudef.h_includes.dpatch which pulled in a missing header
  file. The header is now included in the upstream tarball.
* Remove debian/watch, because upstream places its new releases too often to
  different download locations
* Updated debian/copyright to reflect the download location
* Expand copyright years in debian/copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
===========================================================================
 
3
Copyright (C) 1999-2005 Id Software, Inc.
 
4
 
 
5
This file is part of Quake III Arena source code.
 
6
 
 
7
Quake III Arena source code is free software; you can redistribute it
 
8
and/or modify it under the terms of the GNU General Public License as
 
9
published by the Free Software Foundation; either version 2 of the License,
 
10
or (at your option) any later version.
 
11
 
 
12
Quake III Arena source code is distributed in the hope that it will be
 
13
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
GNU General Public License for more details.
 
16
 
 
17
You should have received a copy of the GNU General Public License
 
18
along with Quake III Arena source code; if not, write to the Free Software
 
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
20
===========================================================================
 
21
*/
 
22
 
 
23
#include <signal.h>
 
24
#include <stdlib.h>
 
25
#include <limits.h>
 
26
#include <sys/types.h>
 
27
#include <stdarg.h>
 
28
#include <stdio.h>
 
29
#include <sys/stat.h>
 
30
#include <string.h>
 
31
#include <ctype.h>
 
32
#include <errno.h>
 
33
 
 
34
#ifndef DEDICATED
 
35
#ifdef USE_LOCAL_HEADERS
 
36
#       include "SDL.h"
 
37
#       include "SDL_cpuinfo.h"
 
38
#else
 
39
#       include <SDL.h>
 
40
#       include <SDL_cpuinfo.h>
 
41
#endif
 
42
#endif
 
43
 
 
44
#include "sys_local.h"
 
45
#include "sys_loadlib.h"
 
46
 
 
47
#include "../qcommon/q_shared.h"
 
48
#include "../qcommon/qcommon.h"
 
49
 
 
50
static char binaryPath[ MAX_OSPATH ] = { 0 };
 
51
static char installPath[ MAX_OSPATH ] = { 0 };
 
52
 
 
53
/*
 
54
=================
 
55
Sys_SetBinaryPath
 
56
=================
 
57
*/
 
58
void Sys_SetBinaryPath(const char *path)
 
59
{
 
60
        Q_strncpyz(binaryPath, path, sizeof(binaryPath));
 
61
}
 
62
 
 
63
/*
 
64
=================
 
65
Sys_BinaryPath
 
66
=================
 
67
*/
 
68
char *Sys_BinaryPath(void)
 
69
{
 
70
        return binaryPath;
 
71
}
 
72
 
 
73
/*
 
74
=================
 
75
Sys_SetDefaultInstallPath
 
76
=================
 
77
*/
 
78
void Sys_SetDefaultInstallPath(const char *path)
 
79
{
 
80
        Q_strncpyz(installPath, path, sizeof(installPath));
 
81
}
 
82
 
 
83
/*
 
84
=================
 
85
Sys_DefaultInstallPath
 
86
=================
 
87
*/
 
88
char *Sys_DefaultInstallPath(void)
 
89
{
 
90
        if (*installPath)
 
91
                return installPath;
 
92
        else
 
93
                return Sys_Cwd();
 
94
}
 
95
 
 
96
/*
 
97
=================
 
98
Sys_DefaultAppPath
 
99
=================
 
100
*/
 
101
char *Sys_DefaultAppPath(void)
 
102
{
 
103
        return Sys_BinaryPath();
 
104
}
 
105
 
 
106
/*
 
107
=================
 
108
Sys_In_Restart_f
 
109
 
 
110
Restart the input subsystem
 
111
=================
 
112
*/
 
113
void Sys_In_Restart_f( void )
 
114
{
 
115
        IN_Shutdown();
 
116
        IN_Init();
 
117
}
 
118
 
 
119
/*
 
120
=================
 
121
Sys_ConsoleInput
 
122
 
 
123
Handle new console input
 
124
=================
 
125
*/
 
126
char *Sys_ConsoleInput(void)
 
127
{
 
128
        return CON_Input( );
 
129
}
 
130
 
 
131
/*
 
132
=================
 
133
Sys_Exit
 
134
 
 
135
Single exit point (regular exit or in case of error)
 
136
=================
 
137
*/
 
138
void Sys_Exit( int ex )
 
139
{
 
140
        CON_Shutdown( );
 
141
 
 
142
#ifndef DEDICATED
 
143
        SDL_Quit( );
 
144
#endif
 
145
 
 
146
#ifdef NDEBUG
 
147
        exit( ex );
 
148
#else
 
149
        // Cause a backtrace on error exits
 
150
        assert( ex == 0 );
 
151
        exit( ex );
 
152
#endif
 
153
}
 
154
 
 
155
/*
 
156
=================
 
157
Sys_Quit
 
158
=================
 
159
*/
 
160
void Sys_Quit( void )
 
161
{
 
162
        CL_Shutdown( );
 
163
        Sys_Exit( 0 );
 
164
}
 
165
 
 
166
/*
 
167
=================
 
168
Sys_GetProcessorFeatures
 
169
=================
 
170
*/
 
171
cpuFeatures_t Sys_GetProcessorFeatures( void )
 
172
{
 
173
        cpuFeatures_t features = 0;
 
174
 
 
175
#ifndef DEDICATED
 
176
        if( SDL_HasRDTSC( ) )    features |= CF_RDTSC;
 
177
        if( SDL_HasMMX( ) )      features |= CF_MMX;
 
178
        if( SDL_HasMMXExt( ) )   features |= CF_MMX_EXT;
 
179
        if( SDL_Has3DNow( ) )    features |= CF_3DNOW;
 
180
        if( SDL_Has3DNowExt( ) ) features |= CF_3DNOW_EXT;
 
181
        if( SDL_HasSSE( ) )      features |= CF_SSE;
 
182
        if( SDL_HasSSE2( ) )     features |= CF_SSE2;
 
183
        if( SDL_HasAltiVec( ) )  features |= CF_ALTIVEC;
 
184
#endif
 
185
 
 
186
        return features;
 
187
}
 
188
 
 
189
/*
 
190
=================
 
191
Sys_Init
 
192
=================
 
193
*/
 
194
void Sys_Init(void)
 
195
{
 
196
        Cmd_AddCommand( "in_restart", Sys_In_Restart_f );
 
197
        Cvar_Set( "arch", OS_STRING " " ARCH_STRING );
 
198
        Cvar_Set( "username", Sys_GetCurrentUser( ) );
 
199
}
 
200
 
 
201
/*
 
202
=================
 
203
Sys_AnsiColorPrint
 
204
 
 
205
Transform Q3 colour codes to ANSI escape sequences
 
206
=================
 
207
*/
 
208
void Sys_AnsiColorPrint( const char *msg )
 
209
{
 
210
        static char buffer[ MAXPRINTMSG ];
 
211
        int         length = 0;
 
212
        static int  q3ToAnsi[ 8 ] =
 
213
        {
 
214
                30, // COLOR_BLACK
 
215
                31, // COLOR_RED
 
216
                32, // COLOR_GREEN
 
217
                33, // COLOR_YELLOW
 
218
                34, // COLOR_BLUE
 
219
                36, // COLOR_CYAN
 
220
                35, // COLOR_MAGENTA
 
221
                0   // COLOR_WHITE
 
222
        };
 
223
 
 
224
        while( *msg )
 
225
        {
 
226
                if( Q_IsColorString( msg ) || *msg == '\n' )
 
227
                {
 
228
                        // First empty the buffer
 
229
                        if( length > 0 )
 
230
                        {
 
231
                                buffer[ length ] = '\0';
 
232
                                fputs( buffer, stderr );
 
233
                                length = 0;
 
234
                        }
 
235
 
 
236
                        if( *msg == '\n' )
 
237
                        {
 
238
                                // Issue a reset and then the newline
 
239
                                fputs( "\033[0m\n", stderr );
 
240
                                msg++;
 
241
                        }
 
242
                        else
 
243
                        {
 
244
                                // Print the color code
 
245
                                Com_sprintf( buffer, sizeof( buffer ), "\033[%dm",
 
246
                                                q3ToAnsi[ ColorIndex( *( msg + 1 ) ) ] );
 
247
                                fputs( buffer, stderr );
 
248
                                msg += 2;
 
249
                        }
 
250
                }
 
251
                else
 
252
                {
 
253
                        if( length >= MAXPRINTMSG - 1 )
 
254
                                break;
 
255
 
 
256
                        buffer[ length ] = *msg;
 
257
                        length++;
 
258
                        msg++;
 
259
                }
 
260
        }
 
261
 
 
262
        // Empty anything still left in the buffer
 
263
        if( length > 0 )
 
264
        {
 
265
                buffer[ length ] = '\0';
 
266
                fputs( buffer, stderr );
 
267
        }
 
268
}
 
269
 
 
270
/*
 
271
=================
 
272
Sys_Print
 
273
=================
 
274
*/
 
275
void Sys_Print( const char *msg )
 
276
{
 
277
        CON_LogWrite( msg );
 
278
        CON_Print( msg );
 
279
}
 
280
 
 
281
/*
 
282
=================
 
283
Sys_Error
 
284
=================
 
285
*/
 
286
void Sys_Error( const char *error, ... )
 
287
{
 
288
        va_list argptr;
 
289
        char    string[1024];
 
290
 
 
291
        CL_Shutdown ();
 
292
 
 
293
        va_start (argptr,error);
 
294
        Q_vsnprintf (string, sizeof(string), error, argptr);
 
295
        va_end (argptr);
 
296
 
 
297
        Sys_ErrorDialog( string );
 
298
 
 
299
        Sys_Exit( 1 );
 
300
}
 
301
 
 
302
/*
 
303
=================
 
304
Sys_Warn
 
305
=================
 
306
*/
 
307
void Sys_Warn( char *warning, ... )
 
308
{
 
309
        va_list argptr;
 
310
        char    string[1024];
 
311
 
 
312
        va_start (argptr,warning);
 
313
        Q_vsnprintf (string, sizeof(string), warning, argptr);
 
314
        va_end (argptr);
 
315
 
 
316
        CON_Print( va( "Warning: %s", string ) );
 
317
}
 
318
 
 
319
/*
 
320
============
 
321
Sys_FileTime
 
322
 
 
323
returns -1 if not present
 
324
============
 
325
*/
 
326
int Sys_FileTime( char *path )
 
327
{
 
328
        struct stat buf;
 
329
 
 
330
        if (stat (path,&buf) == -1)
 
331
                return -1;
 
332
 
 
333
        return buf.st_mtime;
 
334
}
 
335
 
 
336
/*
 
337
=================
 
338
Sys_UnloadDll
 
339
=================
 
340
*/
 
341
void Sys_UnloadDll( void *dllHandle )
 
342
{
 
343
        if( !dllHandle )
 
344
        {
 
345
                Com_Printf("Sys_UnloadDll(NULL)\n");
 
346
                return;
 
347
        }
 
348
 
 
349
        Sys_UnloadLibrary(dllHandle);
 
350
}
 
351
 
 
352
/*
 
353
=================
 
354
Sys_TryLibraryLoad
 
355
=================
 
356
*/
 
357
static void* Sys_TryLibraryLoad(const char* base, const char* gamedir, const char* fname, char* fqpath )
 
358
{
 
359
        void* libHandle;
 
360
        char* fn;
 
361
 
 
362
        *fqpath = 0;
 
363
 
 
364
        fn = FS_BuildOSPath( base, gamedir, fname );
 
365
        Com_Printf( "Sys_LoadDll(%s)... \n", fn );
 
366
 
 
367
        libHandle = Sys_LoadLibrary(fn);
 
368
 
 
369
        if(!libHandle) {
 
370
                Com_Printf( "Sys_LoadDll(%s) failed:\n\"%s\"\n", fn, Sys_LibraryError() );
 
371
                return NULL;
 
372
        }
 
373
 
 
374
        Com_Printf ( "Sys_LoadDll(%s): succeeded ...\n", fn );
 
375
        Q_strncpyz ( fqpath , fn , MAX_QPATH ) ;
 
376
 
 
377
        return libHandle;
 
378
}
 
379
 
 
380
/*
 
381
=================
 
382
Sys_LoadDll
 
383
 
 
384
Used to load a development dll instead of a virtual machine
 
385
#1 look down current path
 
386
#2 look in fs_homepath
 
387
#3 look in fs_basepath
 
388
=================
 
389
*/
 
390
void *Sys_LoadDll( const char *name, char *fqpath ,
 
391
        intptr_t (**entryPoint)(int, ...),
 
392
        intptr_t (*systemcalls)(intptr_t, ...) )
 
393
{
 
394
        void  *libHandle;
 
395
        void  (*dllEntry)( intptr_t (*syscallptr)(intptr_t, ...) );
 
396
        char  fname[MAX_OSPATH];
 
397
        char  *basepath;
 
398
        char  *homepath;
 
399
        char  *pwdpath;
 
400
        char  *gamedir;
 
401
 
 
402
        assert( name );
 
403
 
 
404
        Q_snprintf (fname, sizeof(fname), "%s" ARCH_STRING DLL_EXT, name);
 
405
 
 
406
        // TODO: use fs_searchpaths from files.c
 
407
        pwdpath = Sys_Cwd();
 
408
        basepath = Cvar_VariableString( "fs_basepath" );
 
409
        homepath = Cvar_VariableString( "fs_homepath" );
 
410
        gamedir = Cvar_VariableString( "fs_game" );
 
411
 
 
412
        libHandle = Sys_TryLibraryLoad(pwdpath, gamedir, fname, fqpath);
 
413
 
 
414
        if(!libHandle && homepath)
 
415
                libHandle = Sys_TryLibraryLoad(homepath, gamedir, fname, fqpath);
 
416
 
 
417
        if(!libHandle && basepath)
 
418
                libHandle = Sys_TryLibraryLoad(basepath, gamedir, fname, fqpath);
 
419
 
 
420
        if(!libHandle) {
 
421
                Com_Printf ( "Sys_LoadDll(%s) failed to load library\n", name );
 
422
                return NULL;
 
423
        }
 
424
 
 
425
        dllEntry = Sys_LoadFunction( libHandle, "dllEntry" );
 
426
        *entryPoint = Sys_LoadFunction( libHandle, "vmMain" );
 
427
 
 
428
        if ( !*entryPoint || !dllEntry )
 
429
        {
 
430
                Com_Printf ( "Sys_LoadDll(%s) failed to find vmMain function:\n\"%s\" !\n", name, Sys_LibraryError( ) );
 
431
                Sys_UnloadLibrary(libHandle);
 
432
 
 
433
                return NULL;
 
434
        }
 
435
 
 
436
        Com_Printf ( "Sys_LoadDll(%s) found vmMain function at %p\n", name, *entryPoint );
 
437
        dllEntry( systemcalls );
 
438
 
 
439
        return libHandle;
 
440
}
 
441
 
 
442
/*
 
443
=================
 
444
Sys_Idle
 
445
=================
 
446
*/
 
447
static void Sys_Idle( void )
 
448
{
 
449
#ifndef DEDICATED
 
450
        int appState = SDL_GetAppState( );
 
451
        int sleep = 0;
 
452
 
 
453
        // If we have no input focus at all, sleep a bit
 
454
        if( !( appState & ( SDL_APPMOUSEFOCUS | SDL_APPINPUTFOCUS ) ) )
 
455
        {
 
456
                Cvar_SetValue( "com_unfocused", 1 );
 
457
                sleep += 16;
 
458
        }
 
459
        else
 
460
                Cvar_SetValue( "com_unfocused", 0 );
 
461
 
 
462
        // If we're minimised, sleep a bit more
 
463
        if( !( appState & SDL_APPACTIVE ) )
 
464
        {
 
465
                Cvar_SetValue( "com_minimized", 1 );
 
466
                sleep += 32;
 
467
        }
 
468
        else
 
469
                Cvar_SetValue( "com_minimized", 0 );
 
470
 
 
471
        if( !com_dedicated->integer && sleep )
 
472
                SDL_Delay( sleep );
 
473
#else
 
474
        // Dedicated server idles via NET_Sleep
 
475
#endif
 
476
}
 
477
 
 
478
/*
 
479
=================
 
480
Sys_ParseArgs
 
481
=================
 
482
*/
 
483
void Sys_ParseArgs( int argc, char **argv )
 
484
{
 
485
        if( argc == 2 )
 
486
        {
 
487
                if( !strcmp( argv[1], "--version" ) ||
 
488
                                !strcmp( argv[1], "-v" ) )
 
489
                {
 
490
                        const char* date = __DATE__;
 
491
#ifdef DEDICATED
 
492
                        fprintf( stdout, Q3_VERSION " dedicated server (%s)\n", date );
 
493
#else
 
494
                        fprintf( stdout, Q3_VERSION " client (%s)\n", date );
 
495
#endif
 
496
                        Sys_Exit(0);
 
497
                }
 
498
        }
 
499
}
 
500
 
 
501
#ifndef DEFAULT_BASEDIR
 
502
#       ifdef MACOS_X
 
503
#               define DEFAULT_BASEDIR Sys_StripAppBundle(Sys_BinaryPath())
 
504
#       else
 
505
#               define DEFAULT_BASEDIR Sys_BinaryPath()
 
506
#       endif
 
507
#endif
 
508
 
 
509
/*
 
510
=================
 
511
Sys_SigHandler
 
512
=================
 
513
*/
 
514
void Sys_SigHandler( int signal )
 
515
{
 
516
        static qboolean signalcaught = qfalse;
 
517
 
 
518
        if( signalcaught )
 
519
        {
 
520
                fprintf( stderr, "DOUBLE SIGNAL FAULT: Received signal %d, exiting...\n",
 
521
                        signal );
 
522
        }
 
523
        else
 
524
        {
 
525
                signalcaught = qtrue;
 
526
                fprintf( stderr, "Received signal %d, exiting...\n", signal );
 
527
#ifndef DEDICATED
 
528
                CL_Shutdown();
 
529
#endif
 
530
                SV_Shutdown( "Signal caught" );
 
531
        }
 
532
 
 
533
        Sys_Exit( 0 ); // Exit with 0 to avoid recursive signals
 
534
}
 
535
 
 
536
/*
 
537
=================
 
538
main
 
539
=================
 
540
*/
 
541
int main( int argc, char **argv )
 
542
{
 
543
        int   i;
 
544
        char  commandLine[ MAX_STRING_CHARS ] = { 0 };
 
545
 
 
546
#ifndef DEDICATED
 
547
        // SDL version check
 
548
 
 
549
        // Compile time
 
550
#       if !SDL_VERSION_ATLEAST(MINSDL_MAJOR,MINSDL_MINOR,MINSDL_PATCH)
 
551
#               error A more recent version of SDL is required
 
552
#       endif
 
553
 
 
554
        // Run time
 
555
        const SDL_version *ver = SDL_Linked_Version( );
 
556
 
 
557
#define STRING(s) #s
 
558
#define XSTRING(s) STRING(s)
 
559
#define MINSDL_VERSION \
 
560
        XSTRING(MINSDL_MAJOR) "." \
 
561
        XSTRING(MINSDL_MINOR) "." \
 
562
        XSTRING(MINSDL_PATCH)
 
563
 
 
564
        if( SDL_VERSIONNUM( ver->major, ver->minor, ver->patch ) <
 
565
                        SDL_VERSIONNUM( MINSDL_MAJOR, MINSDL_MINOR, MINSDL_PATCH ) )
 
566
        {
 
567
                Sys_Print( "SDL version " MINSDL_VERSION " or greater required\n" );
 
568
                Sys_Exit( 1 );
 
569
        }
 
570
#endif
 
571
 
 
572
        Sys_ParseArgs( argc, argv );
 
573
        Sys_SetBinaryPath( Sys_Dirname( argv[ 0 ] ) );
 
574
        Sys_SetDefaultInstallPath( DEFAULT_BASEDIR );
 
575
 
 
576
        // Concatenate the command line for passing to Com_Init
 
577
        for( i = 1; i < argc; i++ )
 
578
        {
 
579
                Q_strcat( commandLine, sizeof( commandLine ), argv[ i ] );
 
580
                Q_strcat( commandLine, sizeof( commandLine ), " " );
 
581
        }
 
582
 
 
583
        Com_Init( commandLine );
 
584
        NET_Init( );
 
585
 
 
586
        CON_Init( );
 
587
 
 
588
#ifndef _WIN32
 
589
        // Windows doesn't have these signals
 
590
        // see CON_CtrlHandler() in con_win32.c
 
591
        signal( SIGHUP, Sys_SigHandler );
 
592
        signal( SIGQUIT, Sys_SigHandler );
 
593
        signal( SIGTRAP, Sys_SigHandler );
 
594
        signal( SIGIOT, Sys_SigHandler );
 
595
        signal( SIGBUS, Sys_SigHandler );
 
596
#endif
 
597
 
 
598
        signal( SIGILL, Sys_SigHandler );
 
599
        signal( SIGFPE, Sys_SigHandler );
 
600
        signal( SIGSEGV, Sys_SigHandler );
 
601
        signal( SIGTERM, Sys_SigHandler );
 
602
 
 
603
        while( 1 )
 
604
        {
 
605
                Sys_Idle( );
 
606
                IN_Frame( );
 
607
                Com_Frame( );
 
608
        }
 
609
 
 
610
        return 0;
 
611
}
 
612