From b65383bf2de5c18b074ffeb50498d18577246dac Mon Sep 17 00:00:00 2001 From: sezero Date: Tue, 22 Jun 2010 11:01:24 +0000 Subject: [PATCH] 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: http://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@223 af15c1b1-3010-417e-b628-4374ebc0bcbd --- Quake/Makefile | 2 +- Quake/Makefile.darwin | 2 +- Quake/Makefile.w32 | 2 +- Quake/Makefile.w64 | 2 +- Quake/main_sdl.c | 2 + Quake/pl_linux.c | 3 +- Quake/pl_osx.m | 7 +- Quake/pl_win.c | 35 +-- Quake/sys.h | 2 + Quake/{sys_sdl.c => sys_sdl_unix.c} | 105 ++------- Quake/sys_sdl_win.c | 351 ++++++++++++++++++++++++++++ 11 files changed, 405 insertions(+), 108 deletions(-) rename Quake/{sys_sdl.c => sys_sdl_unix.c} (70%) create mode 100644 Quake/sys_sdl_win.c diff --git a/Quake/Makefile b/Quake/Makefile index c645a4d3..5b9e1d52 100644 --- a/Quake/Makefile +++ b/Quake/Makefile @@ -142,7 +142,7 @@ SYSOBJ_NET := net_sdl.o net_sdlnet.o else SYSOBJ_NET := net_bsd.o net_udp.o endif -SYSOBJ_SYS := pl_linux.o sys_sdl.o +SYSOBJ_SYS := pl_linux.o sys_sdl_unix.o SYSOBJ_MAIN:= main_sdl.o SYSOBJ_RES := diff --git a/Quake/Makefile.darwin b/Quake/Makefile.darwin index 654877c1..5da06706 100644 --- a/Quake/Makefile.darwin +++ b/Quake/Makefile.darwin @@ -150,7 +150,7 @@ SYSOBJ_NET := net_sdl.o net_sdlnet.o else SYSOBJ_NET := net_bsd.o net_udp.o endif -SYSOBJ_SYS := pl_osx.o sys_sdl.o +SYSOBJ_SYS := pl_osx.o sys_sdl_unix.o SYSOBJ_MAIN:= main_sdl.o SYSOBJ_RES := diff --git a/Quake/Makefile.w32 b/Quake/Makefile.w32 index 4132052b..8071d324 100644 --- a/Quake/Makefile.w32 +++ b/Quake/Makefile.w32 @@ -107,7 +107,7 @@ SYSOBJ_NET := net_sdl.o net_sdlnet.o else SYSOBJ_NET := net_win.o net_wins.o net_wipx.o endif -SYSOBJ_SYS := pl_win.o sys_sdl.o +SYSOBJ_SYS := pl_win.o sys_sdl_win.o SYSOBJ_MAIN:= main_sdl.o SYSOBJ_RES := QuakeSpasm.res diff --git a/Quake/Makefile.w64 b/Quake/Makefile.w64 index f3ddcd5b..d495f1d1 100644 --- a/Quake/Makefile.w64 +++ b/Quake/Makefile.w64 @@ -107,7 +107,7 @@ SYSOBJ_NET := net_sdl.o net_sdlnet.o else SYSOBJ_NET := net_win.o net_wins.o net_wipx.o endif -SYSOBJ_SYS := pl_win.o sys_sdl.o +SYSOBJ_SYS := pl_win.o sys_sdl_win.o SYSOBJ_MAIN:= main_sdl.o SYSOBJ_RES := QuakeSpasm.res diff --git a/Quake/main_sdl.c b/Quake/main_sdl.c index 37f04bf6..e1938a46 100644 --- a/Quake/main_sdl.c +++ b/Quake/main_sdl.c @@ -43,6 +43,8 @@ int main(int argc, char *argv[]) isDedicated = (COM_CheckParm("-dedicated") != 0); + Sys_Init(); + // default memory size parms.memsize = DEFAULT_MEMORY; diff --git a/Quake/pl_linux.c b/Quake/pl_linux.c index 014ff9bd..07968b13 100644 --- a/Quake/pl_linux.c +++ b/Quake/pl_linux.c @@ -48,10 +48,9 @@ void PL_VID_Shutdown (void) { } -void PL_ErrorDialog(const char *text) +void PL_ErrorDialog (const char *errorMsg) { // TODO: we can dlopen gtk for an error // dialog window. would it be worth it? - fprintf(stderr, "%s\n", text); } diff --git a/Quake/pl_osx.m b/Quake/pl_osx.m index e934f2df..61af8b2a 100644 --- a/Quake/pl_osx.m +++ b/Quake/pl_osx.m @@ -33,9 +33,10 @@ void PL_VID_Shutdown (void) { } -void PL_ErrorDialog(const char *text) +void PL_ErrorDialog(const char *errorMsg) { - NSString *msg = [NSString stringWithCString:text encoding:NSASCIIStringEncoding]; - NSRunAlertPanel(nil, msg, nil, nil, nil); + NSRunCriticalAlertPanel(@"Quake Error", + [NSString stringWithUTF8String:errorMsg], + @"OK", nil, nil); } diff --git a/Quake/pl_win.c b/Quake/pl_win.c index c16a0d25..78506fc4 100644 --- a/Quake/pl_win.c +++ b/Quake/pl_win.c @@ -25,40 +25,41 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "SDL.h" #include "SDL_syswm.h" -HICON icon; +static HICON icon; void PL_SetWindowIcon (void) { - HINSTANCE handle; - SDL_SysWMinfo wminfo; - HWND hwnd; + HINSTANCE handle; + SDL_SysWMinfo wminfo; + HWND hwnd; - handle = GetModuleHandle(NULL); - icon = LoadIcon(handle, "icon"); + handle = GetModuleHandle(NULL); + icon = LoadIcon(handle, "icon"); - if (!icon) - return; // no icon in executable + if (!icon) + return; /* no icon in the exe */ - SDL_VERSION(&wminfo.version); + SDL_VERSION(&wminfo.version); - if (SDL_GetWMInfo(&wminfo) != 1) - return; // wrong SDL version + if (SDL_GetWMInfo(&wminfo) != 1) + return; /* wrong SDL version */ - hwnd = wminfo.window; + hwnd = wminfo.window; #ifdef _WIN64 - SetClassLongPtr(hwnd, GCLP_HICON, (LONG_PTR) icon); + SetClassLongPtr(hwnd, GCLP_HICON, (LONG_PTR) icon); #else - SetClassLong(hwnd, GCL_HICON, (LONG) icon); + SetClassLong(hwnd, GCL_HICON, (LONG) icon); #endif } void PL_VID_Shutdown (void) { - DestroyIcon(icon); + DestroyIcon(icon); } -void PL_ErrorDialog(const char *text) +void PL_ErrorDialog(const char *errorMsg) { - MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP); + MessageBox (NULL, errorMsg, "Quake Error", + MB_OK | MB_SETFOREGROUND | MB_ICONSTOP); } diff --git a/Quake/sys.h b/Quake/sys.h index 7e74651e..ba8680f3 100644 --- a/Quake/sys.h +++ b/Quake/sys.h @@ -24,6 +24,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // sys.h -- non-portable functions +void Sys_Init (void); + // // file IO // diff --git a/Quake/sys_sdl.c b/Quake/sys_sdl_unix.c similarity index 70% rename from Quake/sys_sdl.c rename to Quake/sys_sdl_unix.c index 1e821ab6..409d6427 100644 --- a/Quake/sys_sdl.c +++ b/Quake/sys_sdl_unix.c @@ -23,36 +23,31 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.h" #include -#ifdef _WIN32 -#include -#include -#include -#else #include #include #include #include #include #include -#endif #include "SDL.h" -#define CONSOLE_ERROR_TIMEOUT 60.0 /* # of seconds to wait on Sys_Error running */ + qboolean isDedicated; -static qboolean sc_return_on_enter = false; #define MAX_HANDLES 32 /* johnfitz -- was 10 */ -FILE *sys_handles[MAX_HANDLES]; +static FILE *sys_handles[MAX_HANDLES]; -int findhandle (void) + +static int findhandle (void) { int i; - for (i=1 ; i + +#include "quakedef.h" + +#include +#include +#include +#include + +#include "SDL.h" + + +qboolean isDedicated; + +static HANDLE hinput, houtput; + +#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) +{ + if (isDedicated) + { + if (!AllocConsole ()) + { + isDedicated = false; /* so that we have a graphical error dialog */ + Sys_Error ("Couldn't create dedicated server console"); + } + + hinput = GetStdHandle (STD_INPUT_HANDLE); + houtput = GetStdHandle (STD_OUTPUT_HANDLE); + } +} + +void Sys_mkdir (char *path) +{ + int rc = _mkdir (path); + 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]; + DWORD dummy; + + va_start (argptr, error); + vsprintf (text, error, argptr); + va_end (argptr); + + if (isDedicated) + WriteFile (houtput, errortxt1, strlen(errortxt1), &dummy, NULL); + /* SDL will put these into its own stderr log, + so print to stderr even in graphical mode. */ + fputs (errortxt1, stderr); + Host_Shutdown (); + fputs (errortxt2, stderr); + fputs (text, stderr); + fputs ("\n\n", stderr); + if (!isDedicated) + PL_ErrorDialog(text); + else + { + WriteFile (houtput, errortxt2, strlen(errortxt2), &dummy, NULL); + WriteFile (houtput, text, strlen(text), &dummy, NULL); + WriteFile (houtput, "\r\n", 2, &dummy, NULL); + SDL_Delay (3000); /* show the console 3 more seconds */ + } + +// shut down QHOST hooks if necessary +// DeinitConProc (); + + exit (1); +} + +void Sys_Printf (const char *fmt, ...) +{ + va_list argptr; + char text[1024]; + DWORD dummy; + + va_start (argptr,fmt); + vsprintf (text, fmt, argptr); + va_end (argptr); + + if (isDedicated) + { + WriteFile(houtput, text, strlen(text), &dummy, NULL); + } + else + { + /* SDL will put these into its own stdout log, + so print to stdout even in graphical mode. */ + fputs (text, stdout); + } +} + +void Sys_Quit (void) +{ + Host_Shutdown(); + + if (isDedicated) + FreeConsole (); + + exit (0); +} + +double Sys_FloatTime (void) +{ + return SDL_GetTicks() / 1000.0; +} + +char *Sys_ConsoleInput (void) +{ + static char con_text[256]; + static int textlen; + INPUT_RECORD recs[1024]; + int ch; + DWORD dummy, numread, numevents; + + if (!isDedicated) + return NULL; // no stdin necessary in graphical mode + + for ( ;; ) + { + if (!GetNumberOfConsoleInputEvents (hinput, &numevents)) + Sys_Error ("Error getting # of console events"); + + if (numevents <= 0) + break; + + if (!ReadConsoleInput(hinput, recs, 1, &numread)) + Sys_Error ("Error reading console input"); + + if (numread != 1) + Sys_Error ("Couldn't read console input"); + + if (recs[0].EventType == KEY_EVENT) + { + if (!recs[0].Event.KeyEvent.bKeyDown) + { + ch = recs[0].Event.KeyEvent.uChar.AsciiChar; + + switch (ch) + { + case '\r': + WriteFile(houtput, "\r\n", 2, &dummy, NULL); + + if (textlen) + { + con_text[textlen] = 0; + textlen = 0; + return con_text; + } + + break; + + case '\b': + WriteFile(houtput, "\b \b", 3, &dummy, NULL); + if (textlen) + { + textlen--; + } + break; + + default: + if (ch >= ' ') + { + WriteFile(houtput, &ch, 1, &dummy, NULL); + con_text[textlen] = ch; + textlen = (textlen + 1) & 0xff; + } + + 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) +{ +} +