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))
138 Sys_Error ("Error getting # of console events (error code %x)", (unsigned int)GetLastError());
143 if (!ReadConsoleInput(hinput, recs, 1, &numread))
144 Sys_Error ("Error reading console input (error code %x)", (unsigned int)GetLastError());
147 Sys_Error ("Couldn't read console input (error code %x)", (unsigned int)GetLastError());
149 if (recs[0].EventType == KEY_EVENT)
151 if (!recs[0].Event.KeyEvent.bKeyDown)
153 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
158 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
170 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
178 if (ch >= (int) (unsigned char) ' ')
180 WriteFile(houtput, &ch, 1, &dummy, NULL);
182 len = (len + 1) & 0xff;
195 char *Sys_GetClipboardData (void)
200 if (OpenClipboard (NULL) != 0)
202 HANDLE hClipboardData;
204 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
206 if ((cliptext = (char *)GlobalLock (hClipboardData)) != 0)
209 allocsize = GlobalSize (hClipboardData) + 1;
210 data = (char *)Z_Malloc (allocsize);
211 strlcpy (data, cliptext, allocsize);
212 GlobalUnlock (hClipboardData);
220 void Sys_InitConsole (void)
225 // initialize the windows dedicated server console if needed
226 tevent = CreateEvent(NULL, false, false, NULL);
229 Sys_Error ("Couldn't create event");
232 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
233 hinput = GetStdHandle (STD_INPUT_HANDLE);
235 // LadyHavoc: can't check cls.state because it hasn't been initialized yet
236 // if (cls.state == ca_dedicated)
237 if (Sys_CheckParm("-dedicated"))
239 //if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE)) // LadyHavoc: on Windows XP this is never 0 or invalid, but hinput is invalid
241 if (!AllocConsole ())
242 Sys_Error ("Couldn't create dedicated server console (error code %x)", (unsigned int)GetLastError());
243 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
244 hinput = GetStdHandle (STD_INPUT_HANDLE);
246 if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE))
247 Sys_Error ("Couldn't create dedicated server console");
254 // give QHOST a chance to hook into the console
255 if ((t = Sys_CheckParm ("-HFILE")) > 0)
258 hFile = (HANDLE)atoi (sys.argv[t+1]);
261 if ((t = Sys_CheckParm ("-HPARENT")) > 0)
264 heventParent = (HANDLE)atoi (sys.argv[t+1]);
267 if ((t = Sys_CheckParm ("-HCHILD")) > 0)
270 heventChild = (HANDLE)atoi (sys.argv[t+1]);
273 InitConProc (hFile, heventParent, heventChild);
277 // because sound is off until we become active
282 ==============================================================================
286 ==============================================================================
295 HINSTANCE global_hInstance;
296 const char *argv[MAX_NUM_ARGVS];
297 char program_name[MAX_OSPATH];
299 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
301 MEMORYSTATUS lpBuffer;
303 /* previous instances do not exist in Win32 */
307 global_hInstance = hInstance;
309 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
310 GlobalMemoryStatus (&lpBuffer);
312 program_name[sizeof(program_name)-1] = 0;
313 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
317 argv[0] = program_name;
319 // FIXME: this tokenizer is rather redundent, call a more general one
320 while (*lpCmdLine && (sys.argc < MAX_NUM_ARGVS))
322 while (*lpCmdLine && ISWHITESPACE(*lpCmdLine))
328 if (*lpCmdLine == '\"')
332 argv[sys.argc] = lpCmdLine;
334 while (*lpCmdLine && (*lpCmdLine != '\"'))
340 argv[sys.argc] = lpCmdLine;
342 while (*lpCmdLine && !ISWHITESPACE(*lpCmdLine))
355 // used by everything
362 /* return success of application */
367 // unused, this file is only used when building windows client and vid_wgl provides WinMain() instead
368 int main (int argc, const char* argv[])
370 MEMORYSTATUS lpBuffer;
372 global_hInstance = GetModuleHandle (0);
374 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
375 GlobalMemoryStatus (&lpBuffer);
377 program_name[sizeof(program_name)-1] = 0;
378 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
389 qbool sys_supportsdlgetticks = false;
390 unsigned int Sys_SDL_GetTicks (void)
392 Sys_Error("Called Sys_SDL_GetTicks on non-SDL target");
395 void Sys_SDL_Delay (unsigned int milliseconds)
397 Sys_Error("Called Sys_SDL_Delay on non-SDL target");