quakespasm/Quake/sys_sdl_unix.c
Ozkan Sezer d098087606 Reorganize files for platform-specific code:
* sys_sdl_win.c: Copied from sys_sdl.c. Windows-only. Remove unix code.
(Sys_Init): New procedure. Call AllocConsole() and get input and output
handles for dedicated servers.
(Sys_ConsoleInput): Add a windows version.
(Sys_Error): Adjust for the allocated console and use windows api.
Adjust message output. Remove the console timeout, doesn't work somehow.
Just display the console for 3 seconds and then exit. Rely on SDL that
it redirects/logs the stdout/stderr to files.
* sys_sdl_unix.c: Rename from sys_sdl.c. Unix-only. Remove windows code.
(Sys_Init): New procedure.
(Sys_Error): Remove Windows-oriented dedicated server specific code.
Adjust message output.
* sys_sdl.c: Delete.
* sys.h (Sys_Init): Add prototype.
* main_sdl.c (main): Call Sys_Init().
* pl_linux.c (PL_ErrorDialog): Remove terminal printing which Sys_Error
already does.
* pl_osx.m (PL_ErrorDialog): Update from uhexen2.
* pl_win.c: Make icon handle static. whitespace and formatting tidy-up.
* Makefile, Makefile.darwin, Makefile.w32, Makefile.w64: Adjust for the
sys_sdl.c name change to sys_sdl_unix.c and sys_sdl_win.c.


git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@223 af15c1b1-3010-417e-b628-4374ebc0bcbd
2010-06-22 11:01:24 +00:00

288 lines
4.5 KiB
C

/*
Copyright (C) 1996-2001 Id Software, Inc.
Copyright (C) 2002-2005 John Fitzgibbons and others
Copyright (C) 2007-2008 Kristian Duske
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
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
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "quakedef.h"
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <time.h>
#include "SDL.h"
qboolean isDedicated;
#define MAX_HANDLES 32 /* johnfitz -- was 10 */
static FILE *sys_handles[MAX_HANDLES];
static int findhandle (void)
{
int i;
for (i = 1; i < MAX_HANDLES; i++)
{
if (!sys_handles[i])
return i;
}
Sys_Error ("out of handles");
return -1;
}
int Sys_filelength (FILE *f)
{
int pos;
int end;
pos = ftell (f);
fseek (f, 0, SEEK_END);
end = ftell (f);
fseek (f, pos, SEEK_SET);
return end;
}
int Sys_FileOpenRead (char *path, int *hndl)
{
FILE *f;
int i, retval;
i = findhandle ();
f = fopen(path, "rb");
if (!f)
{
*hndl = -1;
retval = -1;
}
else
{
sys_handles[i] = f;
*hndl = i;
retval = Sys_filelength(f);
}
return retval;
}
int Sys_FileOpenWrite (char *path)
{
FILE *f;
int i;
i = findhandle ();
f = fopen(path, "wb");
if (!f)
Sys_Error ("Error opening %s: %s", path, strerror(errno));
sys_handles[i] = f;
return i;
}
void Sys_FileClose (int handle)
{
fclose (sys_handles[handle]);
sys_handles[handle] = NULL;
}
void Sys_FileSeek (int handle, int position)
{
fseek (sys_handles[handle], position, SEEK_SET);
}
int Sys_FileRead (int handle, void *dest, int count)
{
return fread (dest, 1, count, sys_handles[handle]);
}
int Sys_FileWrite (int handle, void *data, int count)
{
return fwrite (data, 1, count, sys_handles[handle]);
}
int Sys_FileTime (char *path)
{
FILE *f;
f = fopen(path, "rb");
if (f)
{
fclose(f);
return 1;
}
return -1;
}
void Sys_Init (void)
{
}
void Sys_mkdir (char *path)
{
int rc = mkdir (path, 0777);
if (rc != 0 && errno != EEXIST)
Sys_Error("Unable to create directory %s", path);
}
static const char errortxt1[] = "\nERROR-OUT BEGIN\n\n";
static const char errortxt2[] = "\nQUAKE ERROR: ";
void Sys_Error (const char *error, ...)
{
va_list argptr;
char text[1024];
fputs (errortxt1, stderr);
Host_Shutdown ();
va_start (argptr, error);
vsprintf (text, error, argptr);
va_end (argptr);
fputs (errortxt2, stderr);
fputs (text, stderr);
fputs ("\n\n", stderr);
if (!isDedicated)
PL_ErrorDialog(text);
exit (1);
}
void Sys_Printf (const char *fmt, ...)
{
va_list argptr;
va_start(argptr, fmt);
vprintf(fmt, argptr);
va_end(argptr);
}
void Sys_Quit (void)
{
Host_Shutdown();
exit (0);
}
double Sys_FloatTime (void)
{
return SDL_GetTicks() / 1000.0;
}
char *Sys_ConsoleInput (void)
{
static char con_text[256];
static int textlen;
char c;
fd_set set;
struct timeval timeout;
if (!isDedicated)
return NULL; // no stdin necessary in graphical mode
FD_ZERO (&set);
FD_SET (0, &set); // stdin
timeout.tv_sec = 0;
timeout.tv_usec = 0;
while (select (1, &set, NULL, NULL, &timeout))
{
read (0, &c, 1);
if (c == '\n' || c == '\r')
{
con_text[textlen] = '\0';
textlen = 0;
return con_text;
}
else if (c == 8)
{
if (textlen)
{
textlen--;
con_text[textlen] = '\0';
}
continue;
}
con_text[textlen] = c;
textlen++;
if (textlen < sizeof(con_text))
con_text[textlen] = '\0';
else
{
// buffer is full
textlen = 0;
con_text[0] = '\0';
Sys_Printf("\nConsole input too long!\n");
break;
}
}
return NULL;
}
void Sys_Sleep (void)
{
}
void Sys_SendKeyEvents (void)
{
SDL_Event event;
SDL_PumpEvents();
while (SDL_PollEvent (&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
case SDL_KEYUP:
Key_Event(Key_Map(&(event.key)), event.key.type == SDL_KEYDOWN);
return;
case SDL_QUIT:
Sys_Quit();
break;
default:
SDL_PumpEvents();
break;
}
}
}
void Sys_LowFPPrecision (void)
{
}
void Sys_HighFPPrecision (void)
{
}
void Sys_SetFPCW (void)
{
}