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
33 HANDLE hinput, houtput;
38 static HANDLE heventParent;
39 static HANDLE heventChild;
44 ===============================================================================
48 ===============================================================================
51 void Sys_Error (const char *error, ...)
54 char text[MAX_INPUTLINE];
55 static int in_sys_error0 = 0;
56 static int in_sys_error1 = 0;
57 static int in_sys_error2 = 0;
58 static int in_sys_error3 = 0;
60 va_start (argptr, error);
61 dpvsnprintf (text, sizeof (text), error, argptr);
64 Con_Printf ("Quake Error: %s\n", text);
66 // close video so the message box is visible, unless we already tried that
67 if (!in_sys_error0 && cls.state != ca_dedicated)
73 if (!in_sys_error3 && cls.state != ca_dedicated)
76 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
85 // shut down QHOST hooks if necessary
95 void Sys_Shutdown (void)
102 if (cls.state == ca_dedicated)
106 // shut down QHOST hooks if necessary
111 void Sys_PrintToTerminal(const char *text)
114 extern HANDLE houtput;
116 if ((houtput != 0) && (houtput != INVALID_HANDLE_VALUE))
117 WriteFile(houtput, text, (DWORD) strlen(text), &dummy, NULL);
120 char *Sys_ConsoleInput (void)
122 static char text[MAX_INPUTLINE];
124 INPUT_RECORD recs[1024];
126 DWORD numread, numevents, dummy;
128 if (cls.state != ca_dedicated)
133 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
135 cls.state = ca_disconnected;
136 Sys_Error ("Error getting # of console events (error code %x)", (unsigned int)GetLastError());
142 if (!ReadConsoleInput(hinput, recs, 1, &numread))
144 cls.state = ca_disconnected;
145 Sys_Error ("Error reading console input (error code %x)", (unsigned int)GetLastError());
150 cls.state = ca_disconnected;
151 Sys_Error ("Couldn't read console input (error code %x)", (unsigned int)GetLastError());
154 if (recs[0].EventType == KEY_EVENT)
156 if (!recs[0].Event.KeyEvent.bKeyDown)
158 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
163 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
175 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
183 if (ch >= (int) (unsigned char) ' ')
185 WriteFile(houtput, &ch, 1, &dummy, NULL);
187 len = (len + 1) & 0xff;
200 char *Sys_GetClipboardData (void)
205 if (OpenClipboard (NULL) != 0)
207 HANDLE hClipboardData;
209 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
211 if ((cliptext = (char *)GlobalLock (hClipboardData)) != 0)
214 allocsize = GlobalSize (hClipboardData) + 1;
215 data = (char *)Z_Malloc (allocsize);
216 strlcpy (data, cliptext, allocsize);
217 GlobalUnlock (hClipboardData);
225 void Sys_InitConsole (void)
230 // initialize the windows dedicated server console if needed
231 tevent = CreateEvent(NULL, false, false, NULL);
234 Sys_Error ("Couldn't create event");
237 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
238 hinput = GetStdHandle (STD_INPUT_HANDLE);
240 // LordHavoc: can't check cls.state because it hasn't been initialized yet
241 // if (cls.state == ca_dedicated)
242 if (COM_CheckParm("-dedicated"))
244 //if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE)) // LordHavoc: on Windows XP this is never 0 or invalid, but hinput is invalid
246 if (!AllocConsole ())
247 Sys_Error ("Couldn't create dedicated server console (error code %x)", (unsigned int)GetLastError());
248 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
249 hinput = GetStdHandle (STD_INPUT_HANDLE);
251 if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE))
252 Sys_Error ("Couldn't create dedicated server console");
259 // give QHOST a chance to hook into the console
260 if ((t = COM_CheckParm ("-HFILE")) > 0)
263 hFile = (HANDLE)atoi (com_argv[t+1]);
266 if ((t = COM_CheckParm ("-HPARENT")) > 0)
269 heventParent = (HANDLE)atoi (com_argv[t+1]);
272 if ((t = COM_CheckParm ("-HCHILD")) > 0)
275 heventChild = (HANDLE)atoi (com_argv[t+1]);
278 InitConProc (hFile, heventParent, heventChild);
282 // because sound is off until we become active
287 ==============================================================================
291 ==============================================================================
300 HINSTANCE global_hInstance;
301 const char *argv[MAX_NUM_ARGVS];
302 char program_name[MAX_OSPATH];
304 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
306 MEMORYSTATUS lpBuffer;
308 /* previous instances do not exist in Win32 */
312 global_hInstance = hInstance;
314 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
315 GlobalMemoryStatus (&lpBuffer);
317 program_name[sizeof(program_name)-1] = 0;
318 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
322 argv[0] = program_name;
324 // FIXME: this tokenizer is rather redundent, call a more general one
325 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
327 while (*lpCmdLine && ISWHITESPACE(*lpCmdLine))
333 if (*lpCmdLine == '\"')
337 argv[com_argc] = lpCmdLine;
339 while (*lpCmdLine && (*lpCmdLine != '\"'))
345 argv[com_argc] = lpCmdLine;
347 while (*lpCmdLine && !ISWHITESPACE(*lpCmdLine))
360 /* return success of application */
365 // unused, this file is only used when building windows client and vid_wgl provides WinMain() instead
366 int main (int argc, const char* argv[])
368 MEMORYSTATUS lpBuffer;
370 global_hInstance = GetModuleHandle (0);
372 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
373 GlobalMemoryStatus (&lpBuffer);
375 program_name[sizeof(program_name)-1] = 0;
376 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
387 qboolean sys_supportsdlgetticks = false;
388 unsigned int Sys_SDL_GetTicks (void)
390 Sys_Error("Called Sys_SDL_GetTicks on non-SDL target");
393 void Sys_SDL_Delay (unsigned int milliseconds)
395 Sys_Error("Called Sys_SDL_Delay on non-SDL target");