]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_win.c
Fix client version of ambientsound
[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_Errorf ("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_PrintToTerminal(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                 {
139                         cls.state = ca_disconnected;
140                         Sys_Error ("Error getting # of console events (error code %x)", (unsigned int)GetLastError());
141                 }
142
143                 if (numevents <= 0)
144                         break;
145
146                 if (!ReadConsoleInput(hinput, recs, 1, &numread))
147                 {
148                         cls.state = ca_disconnected;
149                         Sys_Error ("Error reading console input (error code %x)", (unsigned int)GetLastError());
150                 }
151
152                 if (numread != 1)
153                 {
154                         cls.state = ca_disconnected;
155                         Sys_Error ("Couldn't read console input (error code %x)", (unsigned int)GetLastError());
156                 }
157
158                 if (recs[0].EventType == KEY_EVENT)
159                 {
160                         if (!recs[0].Event.KeyEvent.bKeyDown)
161                         {
162                                 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
163
164                                 switch (ch)
165                                 {
166                                         case '\r':
167                                                 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
168
169                                                 if (len)
170                                                 {
171                                                         text[len] = 0;
172                                                         len = 0;
173                                                         return text;
174                                                 }
175
176                                                 break;
177
178                                         case '\b':
179                                                 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
180                                                 if (len)
181                                                 {
182                                                         len--;
183                                                 }
184                                                 break;
185
186                                         default:
187                                                 if (ch >= (int) (unsigned char) ' ')
188                                                 {
189                                                         WriteFile(houtput, &ch, 1, &dummy, NULL);
190                                                         text[len] = ch;
191                                                         len = (len + 1) & 0xff;
192                                                 }
193
194                                                 break;
195
196                                 }
197                         }
198                 }
199         }
200
201         return NULL;
202 }
203
204 char *Sys_GetClipboardData (void)
205 {
206         char *data = NULL;
207         char *cliptext;
208
209         if (OpenClipboard (NULL) != 0)
210         {
211                 HANDLE hClipboardData;
212
213                 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
214                 {
215                         if ((cliptext = (char *)GlobalLock (hClipboardData)) != 0)
216                         {
217                                 size_t allocsize;
218                                 allocsize = GlobalSize (hClipboardData) + 1;
219                                 data = (char *)Z_Malloc (allocsize);
220                                 strlcpy (data, cliptext, allocsize);
221                                 GlobalUnlock (hClipboardData);
222                         }
223                 }
224                 CloseClipboard ();
225         }
226         return data;
227 }
228
229 void Sys_InitConsole (void)
230 {
231 #ifdef QHOST
232         int t;
233
234         // initialize the windows dedicated server console if needed
235         tevent = CreateEvent(NULL, false, false, NULL);
236
237         if (!tevent)
238                 Sys_Error ("Couldn't create event");
239 #endif
240
241         houtput = GetStdHandle (STD_OUTPUT_HANDLE);
242         hinput = GetStdHandle (STD_INPUT_HANDLE);
243
244         // LadyHavoc: can't check cls.state because it hasn't been initialized yet
245         // if (cls.state == ca_dedicated)
246         if (COM_CheckParm("-dedicated"))
247         {
248                 //if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE)) // LadyHavoc: on Windows XP this is never 0 or invalid, but hinput is invalid
249                 {
250                         if (!AllocConsole ())
251                                 Sys_Error ("Couldn't create dedicated server console (error code %x)", (unsigned int)GetLastError());
252                         houtput = GetStdHandle (STD_OUTPUT_HANDLE);
253                         hinput = GetStdHandle (STD_INPUT_HANDLE);
254                 }
255                 if ((houtput == 0) || (houtput == INVALID_HANDLE_VALUE))
256                         Sys_Error ("Couldn't create dedicated server console");
257
258
259 #ifdef QHOST
260 #ifdef _WIN64
261 #define atoi _atoi64
262 #endif
263         // give QHOST a chance to hook into the console
264                 if ((t = COM_CheckParm ("-HFILE")) > 0)
265                 {
266                         if (t < sys.argc)
267                                 hFile = (HANDLE)atoi (sys.argv[t+1]);
268                 }
269
270                 if ((t = COM_CheckParm ("-HPARENT")) > 0)
271                 {
272                         if (t < sys.argc)
273                                 heventParent = (HANDLE)atoi (sys.argv[t+1]);
274                 }
275
276                 if ((t = COM_CheckParm ("-HCHILD")) > 0)
277                 {
278                         if (t < sys.argc)
279                                 heventChild = (HANDLE)atoi (sys.argv[t+1]);
280                 }
281
282                 InitConProc (hFile, heventParent, heventChild);
283 #endif
284         }
285
286 // because sound is off until we become active
287         S_BlockSound ();
288 }
289
290 /*
291 ==============================================================================
292
293 WINDOWS CRAP
294
295 ==============================================================================
296 */
297
298
299 /*
300 ==================
301 WinMain
302 ==================
303 */
304 HINSTANCE       global_hInstance;
305 const char      *argv[MAX_NUM_ARGVS];
306 char            program_name[MAX_OSPATH];
307
308 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
309 {
310         MEMORYSTATUS lpBuffer;
311
312         /* previous instances do not exist in Win32 */
313         if (hPrevInstance)
314                 return 0;
315
316         global_hInstance = hInstance;
317
318         lpBuffer.dwLength = sizeof(MEMORYSTATUS);
319         GlobalMemoryStatus (&lpBuffer);
320
321         program_name[sizeof(program_name)-1] = 0;
322         GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
323
324         sys.argc = 1;
325         sys.argv = argv;
326         argv[0] = program_name;
327
328         // FIXME: this tokenizer is rather redundent, call a more general one
329         while (*lpCmdLine && (sys.argc < MAX_NUM_ARGVS))
330         {
331                 while (*lpCmdLine && ISWHITESPACE(*lpCmdLine))
332                         lpCmdLine++;
333
334                 if (!*lpCmdLine)
335                         break;
336
337                 if (*lpCmdLine == '\"')
338                 {
339                         // quoted string
340                         lpCmdLine++;
341                         argv[sys.argc] = lpCmdLine;
342                         sys.argc++;
343                         while (*lpCmdLine && (*lpCmdLine != '\"'))
344                                 lpCmdLine++;
345                 }
346                 else
347                 {
348                         // unquoted word
349                         argv[sys.argc] = lpCmdLine;
350                         sys.argc++;
351                         while (*lpCmdLine && !ISWHITESPACE(*lpCmdLine))
352                                 lpCmdLine++;
353                 }
354
355                 if (*lpCmdLine)
356                 {
357                         *lpCmdLine = 0;
358                         lpCmdLine++;
359                 }
360         }
361
362         Sys_ProvideSelfFD();
363
364         Host_Main();
365
366         /* return success of application */
367         return true;
368 }
369
370 #if 0
371 // unused, this file is only used when building windows client and vid_wgl provides WinMain() instead
372 int main (int argc, const char* argv[])
373 {
374         MEMORYSTATUS lpBuffer;
375
376         global_hInstance = GetModuleHandle (0);
377
378         lpBuffer.dwLength = sizeof(MEMORYSTATUS);
379         GlobalMemoryStatus (&lpBuffer);
380
381         program_name[sizeof(program_name)-1] = 0;
382         GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
383
384         sys.argc = argc;
385         sys.argv = argv;
386
387         Host_Main();
388
389         return true;
390 }
391 #endif
392
393 qboolean sys_supportsdlgetticks = false;
394 unsigned int Sys_SDL_GetTicks (void)
395 {
396         Sys_Error("Called Sys_SDL_GetTicks on non-SDL target");
397         return 0;
398 }
399 void Sys_SDL_Delay (unsigned int milliseconds)
400 {
401         Sys_Error("Called Sys_SDL_Delay on non-SDL target");
402 }