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