Initial revision
[divverent/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 "winquake.h"
24 #include "errno.h"
25 #include "resource.h"
26 #include "conproc.h"
27 #include "direct.h"
28
29 // LordHavoc: raised min to 24mb (was 8.5mb)
30 #define MINIMUM_WIN_MEMORY              0x1800000
31 // LordHavoc: raised max to 24mb (was 16mb)
32 #define MAXIMUM_WIN_MEMORY              0x1800000
33
34 #define CONSOLE_ERROR_TIMEOUT   60.0    // # of seconds to wait on Sys_Error running
35                                                                                 //  dedicated before exiting
36 #define PAUSE_SLEEP             50                              // sleep time on pause or minimization
37 #define NOT_FOCUS_SLEEP 20                              // sleep time when not focus
38
39 int                     starttime;
40 qboolean        ActiveApp, Minimized;
41 qboolean        WinNT;
42
43 static double           pfreq;
44 static double           curtime = 0.0;
45 static double           lastcurtime = 0.0;
46 static int                      lowshift;
47 qboolean                        isDedicated;
48 static qboolean         sc_return_on_enter = false;
49 HANDLE                          hinput, houtput;
50
51 static char                     *tracking_tag = "Clams & Mooses";
52
53 static HANDLE   tevent;
54 static HANDLE   hFile;
55 static HANDLE   heventParent;
56 static HANDLE   heventChild;
57
58 void MaskExceptions (void);
59 void Sys_InitFloatTime (void);
60 void Sys_PushFPCW_SetHigh (void);
61 void Sys_PopFPCW (void);
62
63 volatile int                                    sys_checksum;
64
65
66 /*
67 ================
68 Sys_PageIn
69 ================
70 */
71 void Sys_PageIn (void *ptr, int size)
72 {
73         byte    *x;
74         int             m, n;
75
76 // touch all the memory to make sure it's there. The 16-page skip is to
77 // keep Win 95 from thinking we're trying to page ourselves in (we are
78 // doing that, of course, but there's no reason we shouldn't)
79         x = (byte *)ptr;
80
81         for (n=0 ; n<4 ; n++)
82         {
83                 for (m=0 ; m<(size - 16 * 0x1000) ; m += 4)
84                 {
85                         sys_checksum += *(int *)&x[m];
86                         sys_checksum += *(int *)&x[m + 16 * 0x1000];
87                 }
88         }
89 }
90
91
92 /*
93 ===============================================================================
94
95 FILE IO
96
97 ===============================================================================
98 */
99
100 // LordHavoc: 256 pak files (was 10)
101 #define MAX_HANDLES             256
102 FILE    *sys_handles[MAX_HANDLES];
103
104 int             findhandle (void)
105 {
106         int             i;
107         
108         for (i=1 ; i<MAX_HANDLES ; i++)
109                 if (!sys_handles[i])
110                         return i;
111         Sys_Error ("out of handles");
112         return -1;
113 }
114
115 /*
116 ================
117 filelength
118 ================
119 */
120 int filelength (FILE *f)
121 {
122         int             pos;
123         int             end;
124         int             t;
125
126         t = VID_ForceUnlockedAndReturnState ();
127
128         pos = ftell (f);
129         fseek (f, 0, SEEK_END);
130         end = ftell (f);
131         fseek (f, pos, SEEK_SET);
132
133         VID_ForceLockState (t);
134
135         return end;
136 }
137
138 int Sys_FileOpenRead (char *path, int *hndl)
139 {
140         FILE    *f;
141         int             i, retval;
142         int             t;
143
144         t = VID_ForceUnlockedAndReturnState ();
145
146         i = findhandle ();
147
148         f = fopen(path, "rb");
149
150         if (!f)
151         {
152                 *hndl = -1;
153                 retval = -1;
154         }
155         else
156         {
157                 sys_handles[i] = f;
158                 *hndl = i;
159                 retval = filelength(f);
160         }
161
162         VID_ForceLockState (t);
163
164         return retval;
165 }
166
167 int Sys_FileOpenWrite (char *path)
168 {
169         FILE    *f;
170         int             i;
171         int             t;
172
173         t = VID_ForceUnlockedAndReturnState ();
174         
175         i = findhandle ();
176
177         f = fopen(path, "wb");
178         if (!f)
179                 Sys_Error ("Error opening %s: %s", path,strerror(errno));
180         sys_handles[i] = f;
181         
182         VID_ForceLockState (t);
183
184         return i;
185 }
186
187 void Sys_FileClose (int handle)
188 {
189         int             t;
190
191         t = VID_ForceUnlockedAndReturnState ();
192         fclose (sys_handles[handle]);
193         sys_handles[handle] = NULL;
194         VID_ForceLockState (t);
195 }
196
197 void Sys_FileSeek (int handle, int position)
198 {
199         int             t;
200
201         t = VID_ForceUnlockedAndReturnState ();
202         fseek (sys_handles[handle], position, SEEK_SET);
203         VID_ForceLockState (t);
204 }
205
206 int Sys_FileRead (int handle, void *dest, int count)
207 {
208         int             t, x;
209
210         t = VID_ForceUnlockedAndReturnState ();
211         x = fread (dest, 1, count, sys_handles[handle]);
212         VID_ForceLockState (t);
213         return x;
214 }
215
216 int Sys_FileWrite (int handle, void *data, int count)
217 {
218         int             t, x;
219
220         t = VID_ForceUnlockedAndReturnState ();
221         x = fwrite (data, 1, count, sys_handles[handle]);
222         VID_ForceLockState (t);
223         return x;
224 }
225
226 int     Sys_FileTime (char *path)
227 {
228         FILE    *f;
229         int             t, retval;
230
231         t = VID_ForceUnlockedAndReturnState ();
232         
233         f = fopen(path, "rb");
234
235         if (f)
236         {
237                 fclose(f);
238                 retval = 1;
239         }
240         else
241         {
242                 retval = -1;
243         }
244         
245         VID_ForceLockState (t);
246         return retval;
247 }
248
249 void Sys_mkdir (char *path)
250 {
251         _mkdir (path);
252 }
253
254
255 /*
256 ===============================================================================
257
258 SYSTEM IO
259
260 ===============================================================================
261 */
262
263 /*
264 ================
265 Sys_MakeCodeWriteable
266 ================
267 */
268 void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
269 {
270         DWORD  flOldProtect;
271
272         if (!VirtualProtect((LPVOID)startaddr, length, PAGE_READWRITE, &flOldProtect))
273                 Sys_Error("Protection change failed\n");
274 }
275
276
277 //#ifndef _M_IX86
278
279 void Sys_SetFPCW (void)
280 {
281 }
282
283 void Sys_PushFPCW_SetHigh (void)
284 {
285 }
286
287 void Sys_PopFPCW (void)
288 {
289 }
290
291 void MaskExceptions (void)
292 {
293 }
294
295 //#endif
296
297 /*
298 ================
299 Sys_Init
300 ================
301 */
302 void Sys_Init (void)
303 {
304         LARGE_INTEGER   PerformanceFreq;
305         unsigned int    lowpart, highpart;
306         OSVERSIONINFO   vinfo;
307
308         MaskExceptions ();
309         Sys_SetFPCW ();
310
311         if (!QueryPerformanceFrequency (&PerformanceFreq))
312                 Sys_Error ("No hardware timer available");
313
314 // get 32 out of the 64 time bits such that we have around
315 // 1 microsecond resolution
316         lowpart = (unsigned int)PerformanceFreq.LowPart;
317         highpart = (unsigned int)PerformanceFreq.HighPart;
318         lowshift = 0;
319
320         while (highpart || (lowpart > 2000000.0))
321         {
322                 lowshift++;
323                 lowpart >>= 1;
324                 lowpart |= (highpart & 1) << 31;
325                 highpart >>= 1;
326         }
327
328         pfreq = 1.0 / (double)lowpart;
329
330         Sys_InitFloatTime ();
331
332         vinfo.dwOSVersionInfoSize = sizeof(vinfo);
333
334         if (!GetVersionEx (&vinfo))
335                 Sys_Error ("Couldn't get OS info");
336
337         if ((vinfo.dwMajorVersion < 4) ||
338                 (vinfo.dwPlatformId == VER_PLATFORM_WIN32s))
339         {
340                 Sys_Error ("WinQuake requires at least Win95 or NT 4.0");
341         }
342
343         if (vinfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
344                 WinNT = true;
345         else
346                 WinNT = false;
347 }
348
349
350 void Sys_Error (char *error, ...)
351 {
352         va_list         argptr;
353         char            text[1024], text2[1024];
354         char            *text3 = "Press Enter to exit\n";
355         char            *text4 = "***********************************\n";
356         char            *text5 = "\n";
357         DWORD           dummy;
358         double          starttime;
359         static int      in_sys_error0 = 0;
360         static int      in_sys_error1 = 0;
361         static int      in_sys_error2 = 0;
362         static int      in_sys_error3 = 0;
363
364         if (!in_sys_error3)
365         {
366                 in_sys_error3 = 1;
367                 VID_ForceUnlockedAndReturnState ();
368         }
369
370         va_start (argptr, error);
371         vsprintf (text, error, argptr);
372         va_end (argptr);
373
374         if (isDedicated)
375         {
376                 va_start (argptr, error);
377                 vsprintf (text, error, argptr);
378                 va_end (argptr);
379
380                 sprintf (text2, "ERROR: %s\n", text);
381                 WriteFile (houtput, text5, strlen (text5), &dummy, NULL);
382                 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
383                 WriteFile (houtput, text2, strlen (text2), &dummy, NULL);
384                 WriteFile (houtput, text3, strlen (text3), &dummy, NULL);
385                 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
386
387
388                 starttime = Sys_FloatTime ();
389                 sc_return_on_enter = true;      // so Enter will get us out of here
390
391                 while (!Sys_ConsoleInput () &&
392                                 ((Sys_FloatTime () - starttime) < CONSOLE_ERROR_TIMEOUT))
393                 {
394                 }
395         }
396         else
397         {
398         // switch to windowed so the message box is visible, unless we already
399         // tried that and failed
400                 if (!in_sys_error0)
401                 {
402                         in_sys_error0 = 1;
403                         VID_SetDefaultMode ();
404                         MessageBox(NULL, text, "Quake Error",
405                                            MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
406                 }
407                 else
408                 {
409                         MessageBox(NULL, text, "Double Quake Error",
410                                            MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
411                 }
412         }
413
414         if (!in_sys_error1)
415         {
416                 in_sys_error1 = 1;
417                 Host_Shutdown ();
418         }
419
420 // shut down QHOST hooks if necessary
421         if (!in_sys_error2)
422         {
423                 in_sys_error2 = 1;
424                 DeinitConProc ();
425         }
426
427         exit (1);
428 }
429
430 void Sys_Printf (char *fmt, ...)
431 {
432         va_list         argptr;
433         char            text[1024];
434         DWORD           dummy;
435         
436         if (isDedicated)
437         {
438                 va_start (argptr,fmt);
439                 vsprintf (text, fmt, argptr);
440                 va_end (argptr);
441
442                 WriteFile(houtput, text, strlen (text), &dummy, NULL);  
443         }
444 }
445
446 void Sys_Quit (void)
447 {
448
449         VID_ForceUnlockedAndReturnState ();
450
451         Host_Shutdown();
452
453         if (tevent)
454                 CloseHandle (tevent);
455
456         if (isDedicated)
457                 FreeConsole ();
458
459 // shut down QHOST hooks if necessary
460         DeinitConProc ();
461
462         exit (0);
463 }
464
465
466 /*
467 ================
468 Sys_FloatTime
469 ================
470 */
471 double Sys_FloatTime (void)
472 {
473         static int                      sametimecount;
474         static unsigned int     oldtime;
475         static int                      first = 1;
476         LARGE_INTEGER           PerformanceCount;
477         unsigned int            temp, t2;
478         double                          time;
479
480         Sys_PushFPCW_SetHigh ();
481
482         QueryPerformanceCounter (&PerformanceCount);
483
484         temp = ((unsigned int)PerformanceCount.LowPart >> lowshift) |
485                    ((unsigned int)PerformanceCount.HighPart << (32 - lowshift));
486
487         if (first)
488         {
489                 oldtime = temp;
490                 first = 0;
491         }
492         else
493         {
494         // check for turnover or backward time
495                 if ((temp <= oldtime) && ((oldtime - temp) < 0x10000000))
496                 {
497                         oldtime = temp; // so we can't get stuck
498                 }
499                 else
500                 {
501                         t2 = temp - oldtime;
502
503                         time = (double)t2 * pfreq;
504                         oldtime = temp;
505
506                         curtime += time;
507
508                         if (curtime == lastcurtime)
509                         {
510                                 sametimecount++;
511
512                                 if (sametimecount > 100000)
513                                 {
514                                         curtime += 1.0;
515                                         sametimecount = 0;
516                                 }
517                         }
518                         else
519                         {
520                                 sametimecount = 0;
521                         }
522
523                         lastcurtime = curtime;
524                 }
525         }
526
527         Sys_PopFPCW ();
528
529     return curtime;
530 }
531
532
533 /*
534 ================
535 Sys_InitFloatTime
536 ================
537 */
538 void Sys_InitFloatTime (void)
539 {
540         int             j;
541
542         Sys_FloatTime ();
543
544         j = COM_CheckParm("-starttime");
545
546         if (j)
547         {
548                 curtime = (double) (atof(com_argv[j+1]));
549         }
550         else
551         {
552                 curtime = 0.0;
553         }
554
555         lastcurtime = curtime;
556 }
557
558
559 char *Sys_ConsoleInput (void)
560 {
561         static char     text[256];
562         static int              len;
563         INPUT_RECORD    recs[1024];
564         int             dummy;
565         int             ch, numread, numevents;
566
567         if (!isDedicated)
568                 return NULL;
569
570
571         for ( ;; )
572         {
573                 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
574                         Sys_Error ("Error getting # of console events");
575
576                 if (numevents <= 0)
577                         break;
578
579                 if (!ReadConsoleInput(hinput, recs, 1, &numread))
580                         Sys_Error ("Error reading console input");
581
582                 if (numread != 1)
583                         Sys_Error ("Couldn't read console input");
584
585                 if (recs[0].EventType == KEY_EVENT)
586                 {
587                         if (!recs[0].Event.KeyEvent.bKeyDown)
588                         {
589                                 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
590
591                                 switch (ch)
592                                 {
593                                         case '\r':
594                                                 WriteFile(houtput, "\r\n", 2, &dummy, NULL);    
595
596                                                 if (len)
597                                                 {
598                                                         text[len] = 0;
599                                                         len = 0;
600                                                         return text;
601                                                 }
602                                                 else if (sc_return_on_enter)
603                                                 {
604                                                 // special case to allow exiting from the error handler on Enter
605                                                         text[0] = '\r';
606                                                         len = 0;
607                                                         return text;
608                                                 }
609
610                                                 break;
611
612                                         case '\b':
613                                                 WriteFile(houtput, "\b \b", 3, &dummy, NULL);   
614                                                 if (len)
615                                                 {
616                                                         len--;
617                                                 }
618                                                 break;
619
620                                         default:
621                                                 if (ch >= ' ')
622                                                 {
623                                                         WriteFile(houtput, &ch, 1, &dummy, NULL);       
624                                                         text[len] = ch;
625                                                         len = (len + 1) & 0xff;
626                                                 }
627
628                                                 break;
629
630                                 }
631                         }
632                 }
633         }
634
635         return NULL;
636 }
637
638 void Sys_Sleep (void)
639 {
640         Sleep (1);
641 }
642
643
644 void Sys_SendKeyEvents (void)
645 {
646     MSG        msg;
647
648         while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
649         {
650         // we always update if there are any event, even if we're paused
651                 scr_skipupdate = 0;
652
653                 if (!GetMessage (&msg, NULL, 0, 0))
654                         Sys_Quit ();
655
656         TranslateMessage (&msg);
657         DispatchMessage (&msg);
658         }
659 }
660
661
662 /*
663 ==============================================================================
664
665  WINDOWS CRAP
666
667 ==============================================================================
668 */
669
670
671 /*
672 ==================
673 WinMain
674 ==================
675 */
676 void SleepUntilInput (int time)
677 {
678
679         MsgWaitForMultipleObjects(1, &tevent, FALSE, time, QS_ALLINPUT);
680 }
681
682
683 extern cvar_t maxfps;
684
685 /*
686 ==================
687 WinMain
688 ==================
689 */
690 HINSTANCE       global_hInstance;
691 int                     global_nCmdShow;
692 char            *argv[MAX_NUM_ARGVS];
693 static char     *empty_string = "";
694 HWND            hwnd_dialog;
695
696
697 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
698 {
699         quakeparms_t    parms;
700         double                  time, oldtime, newtime, timediff;
701         MEMORYSTATUS    lpBuffer;
702         static  char    cwd[1024];
703         int                             t;
704         RECT                    rect;
705
706     /* previous instances do not exist in Win32 */
707     if (hPrevInstance)
708         return 0;
709
710         global_hInstance = hInstance;
711         global_nCmdShow = nCmdShow;
712
713         lpBuffer.dwLength = sizeof(MEMORYSTATUS);
714         GlobalMemoryStatus (&lpBuffer);
715
716         if (!GetCurrentDirectory (sizeof(cwd), cwd))
717                 Sys_Error ("Couldn't determine current directory");
718
719         if (cwd[strlen(cwd)-1] == '/')
720                 cwd[strlen(cwd)-1] = 0;
721
722         parms.basedir = cwd;
723         parms.cachedir = NULL;
724
725         parms.argc = 1;
726         argv[0] = empty_string;
727
728         while (*lpCmdLine && (parms.argc < MAX_NUM_ARGVS))
729         {
730                 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
731                         lpCmdLine++;
732
733                 if (*lpCmdLine)
734                 {
735                         argv[parms.argc] = lpCmdLine;
736                         parms.argc++;
737
738                         while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
739                                 lpCmdLine++;
740
741                         if (*lpCmdLine)
742                         {
743                                 *lpCmdLine = 0;
744                                 lpCmdLine++;
745                         }
746                         
747                 }
748         }
749
750         parms.argv = argv;
751
752         COM_InitArgv (parms.argc, parms.argv);
753
754         parms.argc = com_argc;
755         parms.argv = com_argv;
756
757         isDedicated = (COM_CheckParm ("-dedicated") != 0);
758
759         if (!isDedicated)
760         {
761                 hwnd_dialog = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, NULL);
762
763                 if (hwnd_dialog)
764                 {
765                         if (GetWindowRect (hwnd_dialog, &rect))
766                         {
767                                 if (rect.left > (rect.top * 2))
768                                 {
769                                         SetWindowPos (hwnd_dialog, 0,
770                                                 (rect.left / 2) - ((rect.right - rect.left) / 2),
771                                                 rect.top, 0, 0,
772                                                 SWP_NOZORDER | SWP_NOSIZE);
773                                 }
774                         }
775
776                         ShowWindow (hwnd_dialog, SW_SHOWDEFAULT);
777                         UpdateWindow (hwnd_dialog);
778                         SetForegroundWindow (hwnd_dialog);
779                 }
780         }
781
782 // take the greater of all the available memory or half the total memory,
783 // but at least 8 Mb and no more than 16 Mb, unless they explicitly
784 // request otherwise
785         parms.memsize = lpBuffer.dwAvailPhys;
786
787         if (parms.memsize < MINIMUM_WIN_MEMORY)
788                 parms.memsize = MINIMUM_WIN_MEMORY;
789
790         if (parms.memsize < (lpBuffer.dwTotalPhys >> 1))
791                 parms.memsize = lpBuffer.dwTotalPhys >> 1;
792
793         if (parms.memsize > MAXIMUM_WIN_MEMORY)
794                 parms.memsize = MAXIMUM_WIN_MEMORY;
795
796         if (COM_CheckParm ("-heapsize"))
797         {
798                 t = COM_CheckParm("-heapsize") + 1;
799
800                 if (t < com_argc)
801                         parms.memsize = atoi (com_argv[t]) * 1024;
802         }
803
804         if (COM_CheckParm ("-mem"))
805         {
806                 t = COM_CheckParm("-mem") + 1;
807
808                 if (t < com_argc)
809                         parms.memsize = atoi (com_argv[t]) * 1048576;
810         }
811
812         if (COM_CheckParm ("-winmem"))
813         {
814                 t = COM_CheckParm("-winmem") + 1;
815
816                 if (t < com_argc)
817                         parms.memsize = atoi (com_argv[t]) * 1048576;
818         }
819
820         parms.membase = malloc (parms.memsize);
821
822         if (!parms.membase)
823                 Sys_Error ("Not enough memory free; check disk space\n");
824
825         Sys_PageIn (parms.membase, parms.memsize);
826
827         tevent = CreateEvent(NULL, FALSE, FALSE, NULL);
828
829         if (!tevent)
830                 Sys_Error ("Couldn't create event");
831
832         if (isDedicated)
833         {
834                 if (!AllocConsole ())
835                 {
836                         Sys_Error ("Couldn't create dedicated server console");
837                 }
838
839                 hinput = GetStdHandle (STD_INPUT_HANDLE);
840                 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
841
842         // give QHOST a chance to hook into the console
843                 if ((t = COM_CheckParm ("-HFILE")) > 0)
844                 {
845                         if (t < com_argc)
846                                 hFile = (HANDLE)atoi (com_argv[t+1]);
847                 }
848                         
849                 if ((t = COM_CheckParm ("-HPARENT")) > 0)
850                 {
851                         if (t < com_argc)
852                                 heventParent = (HANDLE)atoi (com_argv[t+1]);
853                 }
854                         
855                 if ((t = COM_CheckParm ("-HCHILD")) > 0)
856                 {
857                         if (t < com_argc)
858                                 heventChild = (HANDLE)atoi (com_argv[t+1]);
859                 }
860
861                 InitConProc (hFile, heventParent, heventChild);
862         }
863
864         Sys_Init ();
865
866 // because sound is off until we become active
867         S_BlockSound ();
868
869         Sys_Printf ("Host_Init\n");
870         Host_Init (&parms);
871
872         oldtime = Sys_FloatTime ();
873
874     /* main window message loop */
875         while (1)
876         {
877                 if (maxfps.value < 5) // LordHavoc: sanity checking
878                         maxfps.value = 5;
879                 if (maxfps.value > 1000) // LordHavoc: sanity checking
880                         maxfps.value = 1000;
881                 if (isDedicated)
882                 {
883                         newtime = Sys_FloatTime ();
884                         time = newtime - oldtime;
885
886                         while (time < sys_ticrate.value )
887                         {
888                                 Sys_Sleep();
889                                 newtime = Sys_FloatTime ();
890                                 time = newtime - oldtime;
891                         }
892                 }
893                 else
894                 {
895                 // yield the CPU for a little while when paused, minimized, or not the focus
896                         if ((cl.paused && (!ActiveApp && !DDActive)) || Minimized)
897                         {
898                                 SleepUntilInput (PAUSE_SLEEP);
899                                 scr_skipupdate = 1;             // no point in bothering to draw
900                         }
901                         else if (!ActiveApp && !DDActive)
902                         {
903                                 SleepUntilInput (NOT_FOCUS_SLEEP);
904                         }
905                         else if (!cls.timedemo && time < (timediff = 1.0 / maxfps.value))
906                         {
907                                 newtime = Sys_FloatTime ();
908                                 time = newtime - oldtime;
909
910                                 while (time < timediff)
911                                 {
912                                         Sys_Sleep();
913                                         newtime = Sys_FloatTime ();
914                                         time = newtime - oldtime;
915                                 }
916                         }
917
918                         newtime = Sys_FloatTime ();
919                         time = newtime - oldtime;
920                 }
921
922                 Host_Frame (time);
923                 oldtime = newtime;
924         }
925
926     /* return success of application */
927     return TRUE;
928 }
929