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 cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1", "use windows timeGetTime function (which has issues on some motherboards) for timing rather than QueryPerformanceCounter timer (which has issues on multicore/multiprocessor machines and processors which are designed to conserve power)"};
35 HANDLE hinput, houtput;
40 static HANDLE heventParent;
41 static HANDLE heventChild;
46 ===============================================================================
50 ===============================================================================
53 void Sys_Error (const char *error, ...)
56 char text[MAX_INPUTLINE];
57 static int in_sys_error0 = 0;
58 static int in_sys_error1 = 0;
59 static int in_sys_error2 = 0;
60 static int in_sys_error3 = 0;
62 va_start (argptr, error);
63 dpvsnprintf (text, sizeof (text), error, argptr);
66 Con_Printf ("Quake Error: %s\n", text);
68 // close video so the message box is visible, unless we already tried that
69 if (!in_sys_error0 && cls.state != ca_dedicated)
75 if (!in_sys_error3 && cls.state != ca_dedicated)
78 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
87 // shut down QHOST hooks if necessary
97 void Sys_Shutdown (void)
101 CloseHandle (tevent);
104 if (cls.state == ca_dedicated)
108 // shut down QHOST hooks if necessary
113 void Sys_PrintToTerminal(const char *text)
116 extern HANDLE houtput;
118 if ((houtput != 0) && (houtput != INVALID_HANDLE_VALUE))
119 WriteFile(houtput, text, (DWORD) strlen(text), &dummy, NULL);
122 char *Sys_ConsoleInput (void)
124 static char text[MAX_INPUTLINE];
126 INPUT_RECORD recs[1024];
128 DWORD numread, numevents, dummy;
130 if (cls.state != ca_dedicated)
135 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
137 cls.state = ca_disconnected;
138 Sys_Error ("Error getting # of console events (error code %x)", (unsigned int)GetLastError());
144 if (!ReadConsoleInput(hinput, recs, 1, &numread))
146 cls.state = ca_disconnected;
147 Sys_Error ("Error reading console input (error code %x)", (unsigned int)GetLastError());
152 cls.state = ca_disconnected;
153 Sys_Error ("Couldn't read console input (error code %x)", (unsigned int)GetLastError());
156 if (recs[0].EventType == KEY_EVENT)
158 if (!recs[0].Event.KeyEvent.bKeyDown)
160 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
165 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
177 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
185 if (ch >= (int) (unsigned char) ' ')
187 WriteFile(houtput, &ch, 1, &dummy, NULL);
189 len = (len + 1) & 0xff;
202 double Sys_DoubleTime (void)
204 return Sys_DoubleTime_Shared();
207 void Sys_Sleep(int microseconds)
209 Sys_Sleep_Shared(microseconds);
212 char *Sys_GetClipboardData (void)
217 if (OpenClipboard (NULL) != 0)
219 HANDLE hClipboardData;
221 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
223 if ((cliptext = (char *)GlobalLock (hClipboardData)) != 0)
226 allocsize = GlobalSize (hClipboardData) + 1;
227 data = (char *)Z_Malloc (allocsize);
228 strlcpy (data, cliptext, allocsize);
229 GlobalUnlock (hClipboardData);
237 void Sys_InitConsole (void)
242 // initialize the windows dedicated server console if needed
243 tevent = CreateEvent(NULL, false, false, NULL);
246 Sys_Error ("Couldn't create event");
249 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
250 hinput = GetStdHandle (STD_INPUT_HANDLE);
252 // LordHavoc: can't check cls.state because it hasn't been initialized yet
253 // if (cls.state == ca_dedicated)
254 if (COM_CheckParm("-dedicated"))
256 //if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE)) // LordHavoc: on Windows XP this is never 0 or invalid, but hinput is invalid
258 if (!AllocConsole ())
259 Sys_Error ("Couldn't create dedicated server console (error code %x)", (unsigned int)GetLastError());
260 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
261 hinput = GetStdHandle (STD_INPUT_HANDLE);
263 if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE))
264 Sys_Error ("Couldn't create dedicated server console");
271 // give QHOST a chance to hook into the console
272 if ((t = COM_CheckParm ("-HFILE")) > 0)
275 hFile = (HANDLE)atoi (com_argv[t+1]);
278 if ((t = COM_CheckParm ("-HPARENT")) > 0)
281 heventParent = (HANDLE)atoi (com_argv[t+1]);
284 if ((t = COM_CheckParm ("-HCHILD")) > 0)
287 heventChild = (HANDLE)atoi (com_argv[t+1]);
290 InitConProc (hFile, heventParent, heventChild);
294 // because sound is off until we become active
299 ==============================================================================
303 ==============================================================================
312 HINSTANCE global_hInstance;
313 const char *argv[MAX_NUM_ARGVS];
314 char program_name[MAX_OSPATH];
316 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
318 MEMORYSTATUS lpBuffer;
320 /* previous instances do not exist in Win32 */
324 global_hInstance = hInstance;
326 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
327 GlobalMemoryStatus (&lpBuffer);
329 program_name[sizeof(program_name)-1] = 0;
330 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
334 argv[0] = program_name;
336 // FIXME: this tokenizer is rather redundent, call a more general one
337 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
339 while (*lpCmdLine && ISWHITESPACE(*lpCmdLine))
345 if (*lpCmdLine == '\"')
349 argv[com_argc] = lpCmdLine;
351 while (*lpCmdLine && (*lpCmdLine != '\"'))
357 argv[com_argc] = lpCmdLine;
359 while (*lpCmdLine && !ISWHITESPACE(*lpCmdLine))
372 /* return success of application */
377 // unused, this file is only used when building windows client and vid_wgl provides WinMain() instead
378 int main (int argc, const char* argv[])
380 MEMORYSTATUS lpBuffer;
382 global_hInstance = GetModuleHandle (0);
384 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
385 GlobalMemoryStatus (&lpBuffer);
387 program_name[sizeof(program_name)-1] = 0;
388 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
399 qboolean sys_supportsdlgetticks = false;
400 unsigned int Sys_SDL_GetTicks (void)
402 Sys_Error("Called Sys_SDL_GetTicks on non-SDL target");
405 void Sys_SDL_Delay (unsigned int milliseconds)
407 Sys_Error("Called Sys_SDL_Delay on non-SDL target");