2009-02-28 14:41:18 +00:00
|
|
|
/*
|
2010-10-18 13:04:28 +00:00
|
|
|
* Copyright (C) 1997-2001 Id Software, Inc.
|
|
|
|
*
|
2010-10-18 13:19:17 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
2010-10-18 13:04:28 +00:00
|
|
|
*
|
2010-10-18 13:19:17 +00:00
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
2010-10-18 13:04:28 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-10-18 13:19:17 +00:00
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
* 02111-1307, USA.
|
2010-10-18 13:04:28 +00:00
|
|
|
*
|
2010-10-18 13:19:17 +00:00
|
|
|
* =======================================================================
|
|
|
|
*
|
|
|
|
* This file implements all system dependend generic funktions
|
|
|
|
*
|
|
|
|
* =======================================================================
|
2010-10-18 13:04:28 +00:00
|
|
|
*/
|
2010-10-18 13:19:17 +00:00
|
|
|
|
2009-02-28 14:41:18 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dlfcn.h>
|
2010-10-18 13:19:17 +00:00
|
|
|
#include <dirent.h>
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-08-01 11:58:10 +00:00
|
|
|
#include "../../common/header/common.h"
|
|
|
|
#include "../../common/header/glob.h"
|
2012-08-01 14:53:40 +00:00
|
|
|
#include "../generic/header/input.h"
|
2010-10-18 13:28:14 +00:00
|
|
|
#include "header/unix.h"
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
unsigned sys_frame_time;
|
2010-10-18 13:19:17 +00:00
|
|
|
int curtime;
|
2010-10-18 13:04:28 +00:00
|
|
|
static void *game_library;
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
static char findbase[MAX_OSPATH];
|
|
|
|
static char findpath[MAX_OSPATH];
|
|
|
|
static char findpattern[MAX_OSPATH];
|
|
|
|
static DIR *fdir;
|
2010-10-18 13:19:17 +00:00
|
|
|
|
2009-02-28 14:41:18 +00:00
|
|
|
qboolean stdin_active = true;
|
2012-04-29 13:57:33 +00:00
|
|
|
extern FILE *logfile;
|
2010-10-18 16:06:40 +00:00
|
|
|
|
|
|
|
static qboolean
|
2012-06-08 11:01:56 +00:00
|
|
|
CompareAttributes(char *path, char *name, unsigned musthave, unsigned canthave)
|
2010-10-18 16:06:40 +00:00
|
|
|
{
|
|
|
|
/* . and .. never match */
|
2012-06-08 11:01:56 +00:00
|
|
|
if ((strcmp(name, ".") == 0) || (strcmp(name, "..") == 0))
|
2010-10-18 16:06:40 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
return false;
|
2010-10-18 16:06:40 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
return true;
|
2010-10-18 16:06:40 +00:00
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-03 20:26:55 +00:00
|
|
|
void
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_Init(void)
|
2012-06-03 20:26:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-10-18 13:19:17 +00:00
|
|
|
int
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_Milliseconds(void)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
|
|
|
struct timeval tp;
|
|
|
|
struct timezone tzp;
|
|
|
|
static int secbase;
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
gettimeofday(&tp, &tzp);
|
2010-10-18 13:19:17 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (!secbase)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
|
|
|
secbase = tp.tv_sec;
|
2012-06-08 11:01:56 +00:00
|
|
|
return tp.tv_usec / 1000;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
curtime = (tp.tv_sec - secbase) * 1000 + tp.tv_usec / 1000;
|
2010-10-18 13:19:17 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
return curtime;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_Mkdir(char *path)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
mkdir(path, 0755);
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_GetCurrentDirectory(void)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
static char dir[MAX_OSPATH];
|
2010-10-18 13:19:17 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (!getcwd(dir, sizeof(dir)))
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_Error("Couldn't get current working directory");
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
return dir;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_FindFirst(char *path, unsigned musthave, unsigned canhave)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
|
|
|
struct dirent *d;
|
|
|
|
char *p;
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (fdir)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_Error("Sys_BeginFind without close");
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
strcpy(findbase, path);
|
2010-10-18 13:19:17 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if ((p = strrchr(findbase, '/')) != NULL)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
|
|
|
*p = 0;
|
2012-06-08 11:01:56 +00:00
|
|
|
strcpy(findpattern, p + 1);
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
strcpy(findpattern, "*");
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (strcmp(findpattern, "*.*") == 0)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
strcpy(findpattern, "*");
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if ((fdir = opendir(findbase)) == NULL)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
while ((d = readdir(fdir)) != NULL)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
if (!*findpattern || glob_match(findpattern, d->d_name))
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
if (CompareAttributes(findbase, d->d_name, musthave, canhave))
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2013-05-18 16:59:12 +00:00
|
|
|
sprintf(findpath, "%s/%s", findbase, d->d_name);
|
2012-06-08 11:01:56 +00:00
|
|
|
return findpath;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_FindNext(unsigned musthave, unsigned canhave)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
|
|
|
struct dirent *d;
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (fdir == NULL)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
while ((d = readdir(fdir)) != NULL)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
if (!*findpattern || glob_match(findpattern, d->d_name))
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
if (CompareAttributes(findbase, d->d_name, musthave, canhave))
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2013-05-18 16:59:12 +00:00
|
|
|
sprintf(findpath, "%s/%s", findbase, d->d_name);
|
2012-06-08 11:01:56 +00:00
|
|
|
return findpath;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_FindClose(void)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
if (fdir != NULL)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
closedir(fdir);
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fdir = NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
void
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_ConsoleOutput(char *string)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2012-06-20 10:46:29 +00:00
|
|
|
fputs(string, stdout);
|
2009-02-28 14:41:18 +00:00
|
|
|
}
|
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
void
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_Printf(char *fmt, ...)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2010-10-18 13:04:28 +00:00
|
|
|
va_list argptr;
|
2012-06-08 11:01:56 +00:00
|
|
|
char text[1024];
|
|
|
|
unsigned char *p;
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
va_start(argptr, fmt);
|
|
|
|
vsnprintf(text, 1024, fmt, argptr);
|
|
|
|
va_end(argptr);
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
for (p = (unsigned char *)text; *p; p++)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2009-02-28 14:41:18 +00:00
|
|
|
*p &= 0x7f;
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (((*p > 128) || (*p < 32)) && (*p != 10) && (*p != 13) && (*p != 9))
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
printf("[%02x]", *p);
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
else
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
putc(*p, stdout);
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
void
|
2012-06-20 10:51:34 +00:00
|
|
|
Sys_Quit(void)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2009-03-04 16:24:55 +00:00
|
|
|
#ifndef DEDICATED_ONLY
|
2010-10-18 13:04:28 +00:00
|
|
|
CL_Shutdown();
|
2009-03-04 16:24:55 +00:00
|
|
|
#endif
|
2012-04-29 13:57:33 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (logfile)
|
2010-10-19 16:21:02 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
fclose(logfile);
|
2010-10-19 16:21:02 +00:00
|
|
|
logfile = NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
Qcommon_Shutdown();
|
2012-06-08 11:01:56 +00:00
|
|
|
fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~FNDELAY);
|
2010-10-20 06:10:33 +00:00
|
|
|
|
|
|
|
printf("------------------------------------\n");
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
exit(0);
|
2009-02-28 14:41:18 +00:00
|
|
|
}
|
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
void
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_Error(char *error, ...)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2010-10-18 13:04:28 +00:00
|
|
|
va_list argptr;
|
2012-06-08 11:01:56 +00:00
|
|
|
char string[1024];
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
/* change stdin to non blocking */
|
2012-06-08 11:01:56 +00:00
|
|
|
fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~FNDELAY);
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2009-03-04 16:24:55 +00:00
|
|
|
#ifndef DEDICATED_ONLY
|
2010-10-18 13:04:28 +00:00
|
|
|
CL_Shutdown();
|
2009-03-04 16:24:55 +00:00
|
|
|
#endif
|
2010-10-18 13:04:28 +00:00
|
|
|
Qcommon_Shutdown();
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
va_start(argptr, error);
|
|
|
|
vsnprintf(string, 1024, error, argptr);
|
|
|
|
va_end(argptr);
|
|
|
|
fprintf(stderr, "Error: %s\n", string);
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
exit(1);
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
|
|
|
|
/*
|
2010-10-18 13:04:28 +00:00
|
|
|
* returns -1 if not present
|
|
|
|
*/
|
|
|
|
int
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_FileTime(char *path)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2010-10-18 13:04:28 +00:00
|
|
|
struct stat buf;
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (stat(path, &buf) == -1)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
return -1;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
return buf.st_mtime;
|
2009-02-28 14:41:18 +00:00
|
|
|
}
|
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
void
|
2012-06-08 11:01:56 +00:00
|
|
|
floating_point_exception_handler(int whatever)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
signal(SIGFPE, floating_point_exception_handler);
|
2009-02-28 14:41:18 +00:00
|
|
|
}
|
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
char *
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_ConsoleInput(void)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
static char text[256];
|
2010-10-18 13:04:28 +00:00
|
|
|
int len;
|
|
|
|
fd_set fdset;
|
2009-02-28 14:41:18 +00:00
|
|
|
struct timeval timeout;
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (!dedicated || !dedicated->value)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (!stdin_active)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
FD_ZERO(&fdset);
|
|
|
|
FD_SET(0, &fdset); /* stdin */
|
2009-02-28 14:41:18 +00:00
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if ((select(1, &fdset, NULL, NULL, &timeout) == -1) || !FD_ISSET(0, &fdset))
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
len = read(0, text, sizeof(text));
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (len == 0) /* eof! */
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2009-02-28 14:41:18 +00:00
|
|
|
stdin_active = false;
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2009-02-28 14:41:18 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (len < 1)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
text[len - 1] = 0; /* rip off the /n and terminate */
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
return text;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
void
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_UnloadGame(void)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
if (game_library)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
dlclose(game_library);
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 14:41:18 +00:00
|
|
|
game_library = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-10-18 13:04:28 +00:00
|
|
|
* Loads the game dll
|
|
|
|
*/
|
|
|
|
void *
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_GetGameAPI(void *parms)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
void *(*GetGameAPI)(void *);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
FILE *fp;
|
|
|
|
char name[MAX_OSPATH];
|
|
|
|
char *path;
|
|
|
|
char *str_p;
|
2015-08-12 16:55:35 +00:00
|
|
|
#ifdef __APPLE__
|
2015-08-09 03:19:03 +00:00
|
|
|
const char *gamename = "game.dylib";
|
|
|
|
#else
|
|
|
|
const char *gamename = "game.so";
|
|
|
|
#endif
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
setreuid(getuid(), getuid());
|
|
|
|
setegid(getgid());
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (game_library)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
Com_Error(ERR_FATAL, "Sys_GetGameAPI without Sys_UnloadingGame");
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
Com_Printf("LoadLibrary(\"%s\")\n", gamename);
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
/* now run through the search paths */
|
2009-02-28 14:41:18 +00:00
|
|
|
path = NULL;
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
while (1)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
path = FS_NextPath(path);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (!path)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL; /* couldn't find one anywhere */
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
snprintf(name, MAX_OSPATH, "%s/%s", path, gamename);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2009-02-28 14:41:18 +00:00
|
|
|
/* skip it if it just doesn't exist */
|
2012-06-08 11:01:56 +00:00
|
|
|
fp = fopen(name, "rb");
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (fp == NULL)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2009-02-28 14:41:18 +00:00
|
|
|
continue;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
fclose(fp);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
game_library = dlopen(name, RTLD_NOW);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (game_library)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
Com_MDPrintf("LoadLibrary (%s)\n", name);
|
2009-02-28 14:41:18 +00:00
|
|
|
break;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
|
|
|
else
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
Com_Printf("LoadLibrary (%s):", name);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
path = (char *)dlerror();
|
|
|
|
str_p = strchr(path, ':'); /* skip the path (already shown) */
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (str_p == NULL)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2009-02-28 14:41:18 +00:00
|
|
|
str_p = path;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
else
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2009-02-28 14:41:18 +00:00
|
|
|
str_p++;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
Com_Printf("%s\n", str_p);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2009-02-28 14:41:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
GetGameAPI = (void *)dlsym(game_library, "GetGameAPI");
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (!GetGameAPI)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2010-10-18 13:04:28 +00:00
|
|
|
Sys_UnloadGame();
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2009-02-28 14:41:18 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
return GetGameAPI(parms);
|
2009-02-28 14:41:18 +00:00
|
|
|
}
|
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
void
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_SendKeyEvents(void)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
|
|
|
#ifndef DEDICATED_ONLY
|
2013-04-28 19:33:08 +00:00
|
|
|
IN_Update();
|
2009-02-28 14:41:18 +00:00
|
|
|
#endif
|
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
/* grab frame time */
|
2009-02-28 14:41:18 +00:00
|
|
|
sys_frame_time = Sys_Milliseconds();
|
|
|
|
}
|
2012-06-11 07:55:54 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
Sys_GetHomeDir(void)
|
|
|
|
{
|
|
|
|
static char gdir[MAX_OSPATH];
|
|
|
|
char *home;
|
2013-04-28 19:33:08 +00:00
|
|
|
|
2012-06-11 07:55:54 +00:00
|
|
|
home = getenv("HOME");
|
|
|
|
|
|
|
|
if (!home)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(gdir, sizeof(gdir), "%s/%s/", home, CFGDIR);
|
|
|
|
|
|
|
|
return gdir;
|
|
|
|
}
|
|
|
|
|
2012-08-01 12:54:18 +00:00
|
|
|
void *
|
|
|
|
Sys_GetProcAddress(void *handle, const char *sym)
|
|
|
|
{
|
2013-04-28 19:33:08 +00:00
|
|
|
return dlsym(handle, sym);
|
2012-08-01 12:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
Sys_LoadLibrary(const char *path, const char *sym, void **handle)
|
|
|
|
{
|
|
|
|
void *module, *entry;
|
|
|
|
|
|
|
|
*handle = NULL;
|
|
|
|
|
|
|
|
module = dlopen(path, RTLD_LAZY);
|
|
|
|
|
|
|
|
if (!module)
|
|
|
|
{
|
2012-08-03 12:35:31 +00:00
|
|
|
Com_Printf("%s failed: %s\n", __func__, dlerror());
|
2012-08-01 12:54:18 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sym)
|
|
|
|
{
|
|
|
|
entry = dlsym(module, sym);
|
|
|
|
|
|
|
|
if (!entry)
|
|
|
|
{
|
2012-08-03 12:35:31 +00:00
|
|
|
Com_Printf("%s failed: %s\n", __func__, dlerror());
|
2012-08-01 12:54:18 +00:00
|
|
|
dlclose(module);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
entry = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Com_DPrintf("%s succeeded: %s\n", __func__, path);
|
|
|
|
|
|
|
|
*handle = module;
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Sys_FreeLibrary(void *handle)
|
|
|
|
{
|
|
|
|
if (handle && dlclose(handle))
|
|
|
|
{
|
|
|
|
Com_Error(ERR_FATAL, "dlclose failed on %p: %s", handle, dlerror());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|