]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_win.c
refactor timing, so that timing code is in sys_shared.c.
[xonotic/darkplaces.git] / sys_win.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // sys_win.c -- Win32 system interface code
21
22 #include "quakedef.h"
23 #include <windows.h>
24 #include <mmsystem.h>
25 #ifdef SUPPORTDIRECTX
26 #include <dsound.h>
27 #endif
28 #include "errno.h"
29 #include "resource.h"
30 #include "conproc.h"
31 #include "direct.h"
32
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)"};
34
35 HANDLE                          hinput, houtput;
36
37 #ifdef QHOST
38 static HANDLE   tevent;
39 static HANDLE   hFile;
40 static HANDLE   heventParent;
41 static HANDLE   heventChild;
42 #endif
43
44
45 /*
46 ===============================================================================
47
48 SYSTEM IO
49
50 ===============================================================================
51 */
52
53 void Sys_Error (const char *error, ...)
54 {
55         va_list         argptr;
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;
61
62         va_start (argptr, error);
63         dpvsnprintf (text, sizeof (text), error, argptr);
64         va_end (argptr);
65
66         Con_Printf ("Quake Error: %s\n", text);
67
68         // close video so the message box is visible, unless we already tried that
69         if (!in_sys_error0 && cls.state != ca_dedicated)
70         {
71                 in_sys_error0 = 1;
72                 VID_Shutdown();
73         }
74
75         if (!in_sys_error3 && cls.state != ca_dedicated)
76         {
77                 in_sys_error3 = true;
78                 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
79         }
80
81         if (!in_sys_error1)
82         {
83                 in_sys_error1 = 1;
84                 Host_Shutdown ();
85         }
86
87 // shut down QHOST hooks if necessary
88         if (!in_sys_error2)
89         {
90                 in_sys_error2 = 1;
91                 Sys_Shutdown ();
92         }
93
94         exit (1);
95 }
96
97 void Sys_Shutdown (void)
98 {
99 #ifdef QHOST
100         if (tevent)
101                 CloseHandle (tevent);
102 #endif
103
104         if (cls.state == ca_dedicated)
105                 FreeConsole ();
106
107 #ifdef QHOST
108 // shut down QHOST hooks if necessary
109         DeinitConProc ();
110 #endif
111 }
112
113 void Sys_PrintToTerminal(const char *text)
114 {
115         DWORD dummy;
116         extern HANDLE houtput;
117
118         if ((houtput != 0) && (houtput != INVALID_HANDLE_VALUE))
119                 WriteFile(houtput, text, (DWORD) strlen(text), &dummy, NULL);
120 }
121
122 char *Sys_ConsoleInput (void)
123 {
124         static char text[MAX_INPUTLINE];
125         static int len;
126         INPUT_RECORD recs[1024];
127         int ch;
128         DWORD numread, numevents, dummy;
129
130         if (cls.state != ca_dedicated)
131                 return NULL;
132
133         for ( ;; )
134         {
135                 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
136                 {
137                         cls.state = ca_disconnected;
138                         Sys_Error ("Error getting # of console events (error code %x)", (unsigned int)GetLastError());
139                 }
140
141                 if (numevents <= 0)
142                         break;
143
144                 if (!ReadConsoleInput(hinput, recs, 1, &numread))
145                 {
146                         cls.state = ca_disconnected;
147                         Sys_Error ("Error reading console input (error code %x)", (unsigned int)GetLastError());
148                 }
149
150                 if (numread != 1)
151                 {
152                         cls.state = ca_disconnected;
153                         Sys_Error ("Couldn't read console input (error code %x)", (unsigned int)GetLastError());
154                 }
155
156                 if (recs[0].EventType == KEY_EVENT)
157                 {
158                         if (!recs[0].Event.KeyEvent.bKeyDown)
159                         {
160                                 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
161
162                                 switch (ch)
163                                 {
164                                         case '\r':
165                                                 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
166
167                                                 if (len)
168                                                 {
169                                                         text[len] = 0;
170                                                         len = 0;
171                                                         return text;
172                                                 }
173
174                                                 break;
175
176                                         case '\b':
177                                                 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
178                                                 if (len)
179                                                 {
180                                                         len--;
181                                                 }
182                                                 break;
183
184                                         default:
185                                                 if (ch >= (int) (unsigned char) ' ')
186                                                 {
187                                                         WriteFile(houtput, &ch, 1, &dummy, NULL);
188                                                         text[len] = ch;
189                                                         len = (len + 1) & 0xff;
190                                                 }
191
192                                                 break;
193
194                                 }
195                         }
196                 }
197         }
198
199         return NULL;
200 }
201
202 double Sys_DoubleTime (void)
203 {
204         return Sys_DoubleTime_Shared();
205 }
206
207 void Sys_Sleep(int microseconds)
208 {
209         Sys_Sleep_Shared(microseconds);
210 }
211
212 char *Sys_GetClipboardData (void)
213 {
214         char *data = NULL;
215         char *cliptext;
216
217         if (OpenClipboard (NULL) != 0)
218         {
219                 HANDLE hClipboardData;
220
221                 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
222                 {
223                         if ((cliptext = (char *)GlobalLock (hClipboardData)) != 0)
224                         {
225                                 size_t allocsize;
226                                 allocsize = GlobalSize (hClipboardData) + 1;
227                                 data = (char *)Z_Malloc (allocsize);
228                                 strlcpy (data, cliptext, allocsize);
229                                 GlobalUnlock (hClipboardData);
230                         }
231                 }
232                 CloseClipboard ();
233         }
234         return data;
235 }
236
237 void Sys_InitConsole (void)
238 {
239 #ifdef QHOST
240         int t;
241
242         // initialize the windows dedicated server console if needed
243         tevent = CreateEvent(NULL, false, false, NULL);
244
245         if (!tevent)
246                 Sys_Error ("Couldn't create event");
247 #endif
248
249         houtput = GetStdHandle (STD_OUTPUT_HANDLE);
250         hinput = GetStdHandle (STD_INPUT_HANDLE);
251
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"))
255         {
256                 //if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE)) // LordHavoc: on Windows XP this is never 0 or invalid, but hinput is invalid
257                 {
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);
262                 }
263                 if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE))
264                         Sys_Error ("Couldn't create dedicated server console");
265
266
267 #ifdef QHOST
268 #ifdef _WIN64
269 #define atoi _atoi64
270 #endif
271         // give QHOST a chance to hook into the console
272                 if ((t = COM_CheckParm ("-HFILE")) > 0)
273                 {
274                         if (t < com_argc)
275                                 hFile = (HANDLE)atoi (com_argv[t+1]);
276                 }
277
278                 if ((t = COM_CheckParm ("-HPARENT")) > 0)
279                 {
280                         if (t < com_argc)
281                                 heventParent = (HANDLE)atoi (com_argv[t+1]);
282                 }
283
284                 if ((t = COM_CheckParm ("-HCHILD")) > 0)
285                 {
286                         if (t < com_argc)
287                                 heventChild = (HANDLE)atoi (com_argv[t+1]);
288                 }
289
290                 InitConProc (hFile, heventParent, heventChild);
291 #endif
292         }
293
294 // because sound is off until we become active
295         S_BlockSound ();
296 }
297
298 /*
299 ==============================================================================
300
301 WINDOWS CRAP
302
303 ==============================================================================
304 */
305
306
307 /*
308 ==================
309 WinMain
310 ==================
311 */
312 HINSTANCE       global_hInstance;
313 const char      *argv[MAX_NUM_ARGVS];
314 char            program_name[MAX_OSPATH];
315
316 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
317 {
318         MEMORYSTATUS lpBuffer;
319
320         /* previous instances do not exist in Win32 */
321         if (hPrevInstance)
322                 return 0;
323
324         global_hInstance = hInstance;
325
326         lpBuffer.dwLength = sizeof(MEMORYSTATUS);
327         GlobalMemoryStatus (&lpBuffer);
328
329         program_name[sizeof(program_name)-1] = 0;
330         GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
331
332         com_argc = 1;
333         com_argv = argv;
334         argv[0] = program_name;
335
336         // FIXME: this tokenizer is rather redundent, call a more general one
337         while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
338         {
339                 while (*lpCmdLine && ISWHITESPACE(*lpCmdLine))
340                         lpCmdLine++;
341
342                 if (!*lpCmdLine)
343                         break;
344
345                 if (*lpCmdLine == '\"')
346                 {
347                         // quoted string
348                         lpCmdLine++;
349                         argv[com_argc] = lpCmdLine;
350                         com_argc++;
351                         while (*lpCmdLine && (*lpCmdLine != '\"'))
352                                 lpCmdLine++;
353                 }
354                 else
355                 {
356                         // unquoted word
357                         argv[com_argc] = lpCmdLine;
358                         com_argc++;
359                         while (*lpCmdLine && !ISWHITESPACE(*lpCmdLine))
360                                 lpCmdLine++;
361                 }
362
363                 if (*lpCmdLine)
364                 {
365                         *lpCmdLine = 0;
366                         lpCmdLine++;
367                 }
368         }
369
370         Host_Main();
371
372         /* return success of application */
373         return true;
374 }
375
376 #if 0
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[])
379 {
380         MEMORYSTATUS lpBuffer;
381
382         global_hInstance = GetModuleHandle (0);
383
384         lpBuffer.dwLength = sizeof(MEMORYSTATUS);
385         GlobalMemoryStatus (&lpBuffer);
386
387         program_name[sizeof(program_name)-1] = 0;
388         GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
389
390         com_argc = argc;
391         com_argv = argv;
392
393         Host_Main();
394
395         return true;
396 }
397 #endif
398
399 qboolean sys_supportsdlgetticks = false;
400 unsigned int Sys_SDL_GetTicks (void)
401 {
402         Sys_Error("Called Sys_SDL_GetTicks on non-SDL target");
403         return 0;
404 }
405 void Sys_SDL_Delay (unsigned int milliseconds)
406 {
407         Sys_Error("Called Sys_SDL_Delay on non-SDL target");
408 }