2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // sys_win.c -- Win32 system interface code
36 HANDLE hinput, houtput;
41 static HANDLE heventParent;
42 static HANDLE heventChild;
48 ===============================================================================
52 ===============================================================================
55 void Sys_Error (const char *error, ...)
58 char text[MAX_INPUTLINE];
59 static int in_sys_error0 = 0;
60 static int in_sys_error1 = 0;
61 static int in_sys_error2 = 0;
62 static int in_sys_error3 = 0;
64 va_start (argptr, error);
65 dpvsnprintf (text, sizeof (text), error, argptr);
68 Con_Printf(CON_ERROR "Engine Error: %s\n", text);
70 // close video so the message box is visible, unless we already tried that
71 if (!in_sys_error0 && cls.state != ca_dedicated)
77 if (!in_sys_error3 && cls.state != ca_dedicated)
80 MessageBox(NULL, text, "Engine Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
89 // shut down QHOST hooks if necessary
99 void Sys_Shutdown (void)
103 CloseHandle (tevent);
106 if (cls.state == ca_dedicated)
110 // shut down QHOST hooks if necessary
115 void Sys_PrintToTerminal(const char *text)
118 extern HANDLE houtput;
120 if ((houtput != 0) && (houtput != INVALID_HANDLE_VALUE))
121 WriteFile(houtput, text, (DWORD) strlen(text), &dummy, NULL);
124 char *Sys_ConsoleInput (void)
126 static char text[MAX_INPUTLINE];
128 INPUT_RECORD recs[1024];
130 DWORD numread, numevents, dummy;
132 if (cls.state != ca_dedicated)
137 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
139 cls.state = ca_disconnected;
140 Sys_Error ("Error getting # of console events (error code %x)", (unsigned int)GetLastError());
146 if (!ReadConsoleInput(hinput, recs, 1, &numread))
148 cls.state = ca_disconnected;
149 Sys_Error ("Error reading console input (error code %x)", (unsigned int)GetLastError());
154 cls.state = ca_disconnected;
155 Sys_Error ("Couldn't read console input (error code %x)", (unsigned int)GetLastError());
158 if (recs[0].EventType == KEY_EVENT)
160 if (!recs[0].Event.KeyEvent.bKeyDown)
162 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
167 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
179 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
187 if (ch >= (int) (unsigned char) ' ')
189 WriteFile(houtput, &ch, 1, &dummy, NULL);
191 len = (len + 1) & 0xff;
204 char *Sys_GetClipboardData (void)
209 if (OpenClipboard (NULL) != 0)
211 HANDLE hClipboardData;
213 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
215 if ((cliptext = (char *)GlobalLock (hClipboardData)) != 0)
218 allocsize = GlobalSize (hClipboardData) + 1;
219 data = (char *)Z_Malloc (allocsize);
220 strlcpy (data, cliptext, allocsize);
221 GlobalUnlock (hClipboardData);
229 void Sys_InitConsole (void)
234 // initialize the windows dedicated server console if needed
235 tevent = CreateEvent(NULL, false, false, NULL);
238 Sys_Error ("Couldn't create event");
241 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
242 hinput = GetStdHandle (STD_INPUT_HANDLE);
244 // LadyHavoc: can't check cls.state because it hasn't been initialized yet
245 // if (cls.state == ca_dedicated)
246 if (COM_CheckParm("-dedicated"))
248 //if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE)) // LadyHavoc: on Windows XP this is never 0 or invalid, but hinput is invalid
250 if (!AllocConsole ())
251 Sys_Error ("Couldn't create dedicated server console (error code %x)", (unsigned int)GetLastError());
252 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
253 hinput = GetStdHandle (STD_INPUT_HANDLE);
255 if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE))
256 Sys_Error ("Couldn't create dedicated server console");
263 // give QHOST a chance to hook into the console
264 if ((t = COM_CheckParm ("-HFILE")) > 0)
267 hFile = (HANDLE)atoi (sys.argv[t+1]);
270 if ((t = COM_CheckParm ("-HPARENT")) > 0)
273 heventParent = (HANDLE)atoi (sys.argv[t+1]);
276 if ((t = COM_CheckParm ("-HCHILD")) > 0)
279 heventChild = (HANDLE)atoi (sys.argv[t+1]);
282 InitConProc (hFile, heventParent, heventChild);
286 // because sound is off until we become active
291 ==============================================================================
295 ==============================================================================
304 HINSTANCE global_hInstance;
305 const char *argv[MAX_NUM_ARGVS];
306 char program_name[MAX_OSPATH];
308 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
310 MEMORYSTATUS lpBuffer;
312 /* previous instances do not exist in Win32 */
316 global_hInstance = hInstance;
318 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
319 GlobalMemoryStatus (&lpBuffer);
321 program_name[sizeof(program_name)-1] = 0;
322 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
326 argv[0] = program_name;
328 // FIXME: this tokenizer is rather redundent, call a more general one
329 while (*lpCmdLine && (sys.argc < MAX_NUM_ARGVS))
331 while (*lpCmdLine && ISWHITESPACE(*lpCmdLine))
337 if (*lpCmdLine == '\"')
341 argv[sys.argc] = lpCmdLine;
343 while (*lpCmdLine && (*lpCmdLine != '\"'))
349 argv[sys.argc] = lpCmdLine;
351 while (*lpCmdLine && !ISWHITESPACE(*lpCmdLine))
366 /* return success of application */
371 // unused, this file is only used when building windows client and vid_wgl provides WinMain() instead
372 int main (int argc, const char* argv[])
374 MEMORYSTATUS lpBuffer;
376 global_hInstance = GetModuleHandle (0);
378 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
379 GlobalMemoryStatus (&lpBuffer);
381 program_name[sizeof(program_name)-1] = 0;
382 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
393 qboolean sys_supportsdlgetticks = false;
394 unsigned int Sys_SDL_GetTicks (void)
396 Sys_Error("Called Sys_SDL_GetTicks on non-SDL target");
399 void Sys_SDL_Delay (unsigned int milliseconds)
401 Sys_Error("Called Sys_SDL_Delay on non-SDL target");