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;
47 ===============================================================================
51 ===============================================================================
54 void Sys_Error (const char *error, ...)
57 char text[MAX_INPUTLINE];
58 static int in_sys_error0 = 0;
59 static int in_sys_error1 = 0;
60 static int in_sys_error2 = 0;
61 static int in_sys_error3 = 0;
63 va_start (argptr, error);
64 dpvsnprintf (text, sizeof (text), error, argptr);
67 Con_Printf ("Quake Error: %s\n", text);
69 // close video so the message box is visible, unless we already tried that
70 if (!in_sys_error0 && cls.state != ca_dedicated)
76 if (!in_sys_error3 && cls.state != ca_dedicated)
79 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
88 // shut down QHOST hooks if necessary
98 void Sys_Shutdown (void)
102 CloseHandle (tevent);
105 if (cls.state == ca_dedicated)
109 // shut down QHOST hooks if necessary
114 void Sys_PrintToTerminal(const char *text)
117 extern HANDLE houtput;
119 if ((houtput != 0) && (houtput != INVALID_HANDLE_VALUE))
120 WriteFile(houtput, text, (DWORD) strlen(text), &dummy, NULL);
123 char *Sys_ConsoleInput (void)
125 static char text[MAX_INPUTLINE];
127 INPUT_RECORD recs[1024];
129 DWORD numread, numevents, dummy;
131 if (cls.state != ca_dedicated)
136 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
138 cls.state = ca_disconnected;
139 Sys_Error ("Error getting # of console events (error code %x)", (unsigned int)GetLastError());
145 if (!ReadConsoleInput(hinput, recs, 1, &numread))
147 cls.state = ca_disconnected;
148 Sys_Error ("Error reading console input (error code %x)", (unsigned int)GetLastError());
153 cls.state = ca_disconnected;
154 Sys_Error ("Couldn't read console input (error code %x)", (unsigned int)GetLastError());
157 if (recs[0].EventType == KEY_EVENT)
159 if (!recs[0].Event.KeyEvent.bKeyDown)
161 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
166 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
178 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
186 if (ch >= (int) (unsigned char) ' ')
188 WriteFile(houtput, &ch, 1, &dummy, NULL);
190 len = (len + 1) & 0xff;
203 char *Sys_GetClipboardData (void)
208 if (OpenClipboard (NULL) != 0)
210 HANDLE hClipboardData;
212 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
214 if ((cliptext = (char *)GlobalLock (hClipboardData)) != 0)
217 allocsize = GlobalSize (hClipboardData) + 1;
218 data = (char *)Z_Malloc (allocsize);
219 strlcpy (data, cliptext, allocsize);
220 GlobalUnlock (hClipboardData);
228 void Sys_InitConsole (void)
233 // initialize the windows dedicated server console if needed
234 tevent = CreateEvent(NULL, false, false, NULL);
237 Sys_Error ("Couldn't create event");
240 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
241 hinput = GetStdHandle (STD_INPUT_HANDLE);
243 // LadyHavoc: can't check cls.state because it hasn't been initialized yet
244 // if (cls.state == ca_dedicated)
245 if (COM_CheckParm("-dedicated"))
247 //if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE)) // LadyHavoc: on Windows XP this is never 0 or invalid, but hinput is invalid
249 if (!AllocConsole ())
250 Sys_Error ("Couldn't create dedicated server console (error code %x)", (unsigned int)GetLastError());
251 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
252 hinput = GetStdHandle (STD_INPUT_HANDLE);
254 if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE))
255 Sys_Error ("Couldn't create dedicated server console");
262 // give QHOST a chance to hook into the console
263 if ((t = COM_CheckParm ("-HFILE")) > 0)
266 hFile = (HANDLE)atoi (com_argv[t+1]);
269 if ((t = COM_CheckParm ("-HPARENT")) > 0)
272 heventParent = (HANDLE)atoi (com_argv[t+1]);
275 if ((t = COM_CheckParm ("-HCHILD")) > 0)
278 heventChild = (HANDLE)atoi (com_argv[t+1]);
281 InitConProc (hFile, heventParent, heventChild);
285 // because sound is off until we become active
290 ==============================================================================
294 ==============================================================================
303 HINSTANCE global_hInstance;
304 const char *argv[MAX_NUM_ARGVS];
305 char program_name[MAX_OSPATH];
307 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
309 MEMORYSTATUS lpBuffer;
311 /* previous instances do not exist in Win32 */
315 global_hInstance = hInstance;
317 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
318 GlobalMemoryStatus (&lpBuffer);
320 program_name[sizeof(program_name)-1] = 0;
321 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
325 argv[0] = program_name;
327 // FIXME: this tokenizer is rather redundent, call a more general one
328 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
330 while (*lpCmdLine && ISWHITESPACE(*lpCmdLine))
336 if (*lpCmdLine == '\"')
340 argv[com_argc] = lpCmdLine;
342 while (*lpCmdLine && (*lpCmdLine != '\"'))
348 argv[com_argc] = lpCmdLine;
350 while (*lpCmdLine && !ISWHITESPACE(*lpCmdLine))
365 /* return success of application */
370 // unused, this file is only used when building windows client and vid_wgl provides WinMain() instead
371 int main (int argc, const char* argv[])
373 MEMORYSTATUS lpBuffer;
375 global_hInstance = GetModuleHandle (0);
377 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
378 GlobalMemoryStatus (&lpBuffer);
380 program_name[sizeof(program_name)-1] = 0;
381 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
392 qboolean sys_supportsdlgetticks = false;
393 unsigned int Sys_SDL_GetTicks (void)
395 Sys_Error("Called Sys_SDL_GetTicks on non-SDL target");
398 void Sys_SDL_Delay (unsigned int milliseconds)
400 Sys_Error("Called Sys_SDL_Delay on non-SDL target");