gzdoom/src/win32/i_system.cpp
Randy Heit 83373fba88 Note: I have not tried compiling these recent changes under Linux. I wouldn't
be surprised if it doesn't work.

- Reorganized the network startup loops so now they are event driven. There is
  a single function that gets called to drive it, and it uses callbacks to
  perform the different stages of the synchronization. This lets me have a nice,
  responsive abort button instead of the previous unannounced hit-escape-to-
  abort behavior, and I think the rearranged code is slightly easier to
  understand too.
- Increased the number of bytes for version info during D_ArbitrateNetStart(),
  in preparation for the day when NETGAMEVERSION requires more than one byte.
- I noticed an issue with Vista RC1 and the new fatal error setup. Even after
  releasing a DirectDraw or Direct3D interface, the DWM can still use the
  last image drawn using them when it composites the window. It doesn't always
  do it but it does often enough that it is a real problem. At this point, I
  don't know if it's a problem with the release version of Vista or not.
  After messing around, I discovered the problem was caused by ~Win32Video()
  hiding the window and then having it immediately shown soon after. The DWM
  kept an image of the window to do the transition effect with, and then when
  it didn't get a chance to do the transition, it didn't properly forget about
  its saved image and kept plastering it on top of everything else
  underneath.
- Added a network synchronization panel to the window during netgame startup.
- Fixed: PClass::CreateDerivedClass() must initialize StateList to NULL.
  Otherwise, classic DECORATE definitions generate a big, fat crash.
- Resurrected the R_Init progress bar, now as a standard Windows control.
- Removed the sound failure dialog. The FMOD setup already defaulted to no
  sound if initialization failed, so this only applies when snd_output is set
  to "alternate" which now also falls back to no sound. In addition, it wasn't
  working right, and I didn't feel like fixing it for the probably 0% of users
  it affected.
- Fixed: The edit control used for logging output added text in reverse order
  on Win9x.
- Went back to the roots and made graphics initialization one of the last
  things to happen during setup. Now the startup text is visible again. More
  importantly, the main window is no longer created invisible, which seems
  to cause trouble with it not always appearing in the taskbar. The fatal
  error dialog is now also embedded in the main window instead of being a
  separate modal dialog, so you can play with the log window to see any
  problems that might be reported there.
  
  Rather than completely restoring the original startup order, I tried to
  keep things as close to the way they were with early graphics startup. In
  particular, V_Init() now creates a dummy screen so that things that need
  screen dimensions can get them. It gets replaced by the real screen later
  in I_InitGraphics(). Will need to check this under Linux to make sure it
  didn't cause any problems there.
- Removed the following stubs that just called functions in Video:
    - I_StartModeIterator()
    - I_NextMode()
    - I_DisplayType()
  I_FullscreenChanged() was also removed, and a new fullscreen parameter
  was added to IVideo::StartModeIterator(), since that's all it controlled.
- Renamed I_InitHardware() back to I_InitGraphics(), since that's all it's
  initialized post-1.22.


SVN r416 (trunk)
2006-12-19 04:09:10 +00:00

774 lines
17 KiB
C++

// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// $Id:$
//
// Copyright (C) 1993-1996 by id Software, Inc.
//
// This source is available for distribution and/or modification
// only under the terms of the DOOM Source Code License as
// published by id Software. All rights reserved.
//
// The source is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
// for more details.
//
// $Log:$
//
// DESCRIPTION:
//
//-----------------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <direct.h>
#include <string.h>
#include <process.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/timeb.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#define USE_WINDOWS_DWORD
#include "hardware.h"
#include "doomerrors.h"
#include <math.h>
#include "doomtype.h"
#include "version.h"
#include "doomdef.h"
#include "cmdlib.h"
#include "m_argv.h"
#include "m_misc.h"
#include "i_video.h"
#include "i_sound.h"
#include "i_music.h"
#include "resource.h"
#include "d_main.h"
#include "d_net.h"
#include "g_game.h"
#include "i_input.h"
#include "i_system.h"
#include "c_dispatch.h"
#include "templates.h"
#include "gameconfigfile.h"
#include "stats.h"
EXTERN_CVAR (String, language)
#ifdef USEASM
extern "C" void STACK_ARGS CheckMMX (CPUInfo *cpu);
#endif
extern "C"
{
double SecondsPerCycle = 1e-8;
double CyclesPerSecond = 1e8; // 100 MHz
CPUInfo CPU;
}
extern HWND Window, ConWindow, GameTitleWindow;
extern HINSTANCE g_hInst;
UINT TimerPeriod;
UINT TimerEventID;
UINT MillisecondsPerTic;
HANDLE NewTicArrived;
uint32 LanguageIDs[4];
void CalculateCPUSpeed ();
const IWADInfo *DoomStartupInfo;
int (*I_GetTime) (bool saveMS);
int (*I_WaitForTic) (int);
os_t OSPlatform;
void I_Tactile (int on, int off, int total)
{
// UNUSED.
on = off = total = 0;
}
ticcmd_t emptycmd;
ticcmd_t *I_BaseTiccmd(void)
{
return &emptycmd;
}
static DWORD basetime = 0;
// [RH] Returns time in milliseconds
unsigned int I_MSTime (void)
{
DWORD tm;
tm = timeGetTime();
if (!basetime)
basetime = tm;
return tm - basetime;
}
static DWORD TicStart;
static DWORD TicNext;
//
// I_GetTime
// returns time in 1/35th second tics
//
int I_GetTimePolled (bool saveMS)
{
DWORD tm;
tm = timeGetTime();
if (!basetime)
basetime = tm;
if (saveMS)
{
TicStart = tm;
TicNext = (tm * TICRATE / 1000 + 1) * 1000 / TICRATE;
}
return ((tm-basetime)*TICRATE)/1000;
}
int I_WaitForTicPolled (int prevtic)
{
int time;
while ((time = I_GetTimePolled(false)) <= prevtic)
;
return time;
}
static int tics;
static DWORD ted_start, ted_next;
int I_GetTimeEventDriven (bool saveMS)
{
if (saveMS)
{
TicStart = ted_start;
TicNext = ted_next;
}
return tics;
}
int I_WaitForTicEvent (int prevtic)
{
while (prevtic >= tics)
{
WaitForSingleObject (NewTicArrived, 1000/TICRATE);
}
return tics;
}
void CALLBACK TimerTicked (UINT id, UINT msg, DWORD user, DWORD dw1, DWORD dw2)
{
tics++;
ted_start = timeGetTime ();
ted_next = ted_start + MillisecondsPerTic;
SetEvent (NewTicArrived);
}
// Returns the fractional amount of a tic passed since the most recent tic
fixed_t I_GetTimeFrac (uint32 *ms)
{
DWORD now = timeGetTime();
if (ms) *ms = TicNext;
DWORD step = TicNext - TicStart;
if (step == 0)
{
return FRACUNIT;
}
else
{
fixed_t frac = clamp<fixed_t> ((now - TicStart)*FRACUNIT/step, 0, FRACUNIT);
return frac;
}
}
void I_WaitVBL (int count)
{
// I_WaitVBL is never used to actually synchronize to the
// vertical blank. Instead, it's used for delay purposes.
Sleep (1000 * count / 70);
}
// [RH] Detect the OS the game is running under
void I_DetectOS (void)
{
OSVERSIONINFO info;
const char *osname;
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx (&info);
switch (info.dwPlatformId)
{
case VER_PLATFORM_WIN32_WINDOWS:
OSPlatform = os_Win95;
if (info.dwMinorVersion < 10)
{
osname = "95";
}
else if (info.dwMinorVersion < 90)
{
osname = "98";
}
else
{
osname = "Me";
}
break;
case VER_PLATFORM_WIN32_NT:
OSPlatform = info.dwMajorVersion < 5 ? os_WinNT4 : os_Win2k;
osname = "NT";
if (info.dwMajorVersion == 5)
{
if (info.dwMinorVersion == 0)
{
osname = "2000";
}
if (info.dwMinorVersion == 1)
{
osname = "XP";
}
else if (info.dwMinorVersion == 2)
{
osname = "Server 2003";
}
}
else if (info.dwMajorVersion == 6 && info.dwMinorVersion == 0)
{
osname = "Vista";
}
break;
default:
OSPlatform = os_unknown;
osname = "Unknown OS";
break;
}
if (OSPlatform == os_Win95)
{
Printf ("OS: Windows %s %lu.%lu.%lu %s\n",
osname,
info.dwMajorVersion, info.dwMinorVersion,
info.dwBuildNumber & 0xffff, info.szCSDVersion);
}
else
{
Printf ("OS: Windows %s %lu.%lu (Build %lu)\n %s\n",
osname,
info.dwMajorVersion, info.dwMinorVersion,
info.dwBuildNumber, info.szCSDVersion);
}
if (OSPlatform == os_unknown)
{
Printf ("(Assuming Windows 95)\n");
OSPlatform = os_Win95;
}
}
//
// SubsetLanguageIDs
//
static void SubsetLanguageIDs (LCID id, LCTYPE type, int idx)
{
char buf[8];
LCID langid;
char *idp;
if (!GetLocaleInfo (id, type, buf, 8))
return;
langid = MAKELCID (strtoul(buf, NULL, 16), SORT_DEFAULT);
if (!GetLocaleInfo (langid, LOCALE_SABBREVLANGNAME, buf, 8))
return;
idp = (char *)(&LanguageIDs[idx]);
memset (idp, 0, 4);
idp[0] = tolower(buf[0]);
idp[1] = tolower(buf[1]);
idp[2] = tolower(buf[2]);
idp[3] = 0;
}
//
// SetLanguageIDs
//
void SetLanguageIDs ()
{
size_t langlen = strlen (language);
if (langlen < 2 || langlen > 3)
{
memset (LanguageIDs, 0, sizeof(LanguageIDs));
SubsetLanguageIDs (LOCALE_USER_DEFAULT, LOCALE_ILANGUAGE, 0);
SubsetLanguageIDs (LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE, 1);
SubsetLanguageIDs (LOCALE_SYSTEM_DEFAULT, LOCALE_ILANGUAGE, 2);
SubsetLanguageIDs (LOCALE_SYSTEM_DEFAULT, LOCALE_IDEFAULTLANGUAGE, 3);
}
else
{
DWORD lang = 0;
((BYTE *)&lang)[0] = (language)[0];
((BYTE *)&lang)[1] = (language)[1];
((BYTE *)&lang)[2] = (language)[2];
LanguageIDs[0] = lang;
LanguageIDs[1] = lang;
LanguageIDs[2] = lang;
LanguageIDs[3] = lang;
}
}
//
// I_Init
//
void I_Init (void)
{
#ifndef USEASM
memset (&CPU, 0, sizeof(CPU));
#else
CheckMMX (&CPU);
CalculateCPUSpeed ();
// Why does Intel right-justify this string?
char *f = CPU.CPUString, *t = f;
while (*f == ' ')
{
++f;
}
if (f != t)
{
while (*f != 0)
{
*t++ = *f++;
}
}
#endif
if (CPU.VendorID[0])
{
Printf ("CPU Vendor ID: %s\n", CPU.VendorID);
if (CPU.CPUString[0])
{
Printf (" Name: %s\n", CPU.CPUString);
}
if (CPU.bIsAMD)
{
Printf (" Family %d (%d), Model %d, Stepping %d\n",
CPU.Family, CPU.AMDFamily, CPU.AMDModel, CPU.AMDStepping);
}
else
{
Printf (" Family %d, Model %d, Stepping %d\n",
CPU.Family, CPU.Model, CPU.Stepping);
}
Printf (" Features:");
if (CPU.bMMX) Printf (" MMX");
if (CPU.bMMXPlus) Printf (" MMX+");
if (CPU.bSSE) Printf (" SSE");
if (CPU.bSSE2) Printf (" SSE2");
if (CPU.bSSE3) Printf (" SSE3");
if (CPU.b3DNow) Printf (" 3DNow!");
if (CPU.b3DNowPlus) Printf (" 3DNow!+");
Printf ("\n");
}
// Use a timer event if possible
NewTicArrived = CreateEvent (NULL, FALSE, FALSE, NULL);
if (NewTicArrived)
{
UINT delay;
char *cmdDelay;
cmdDelay = Args.CheckValue ("-timerdelay");
delay = 0;
if (cmdDelay != 0)
{
delay = atoi (cmdDelay);
}
if (delay == 0)
{
delay = 1000/TICRATE;
}
TimerEventID = timeSetEvent
(
delay,
0,
TimerTicked,
0,
TIME_PERIODIC
);
MillisecondsPerTic = delay;
}
if (TimerEventID != 0)
{
I_GetTime = I_GetTimeEventDriven;
I_WaitForTic = I_WaitForTicEvent;
}
else
{ // If no timer event, busy-loop with timeGetTime
I_GetTime = I_GetTimePolled;
I_WaitForTic = I_WaitForTicPolled;
}
atterm (I_ShutdownSound);
I_InitSound ();
}
void CalculateCPUSpeed ()
{
LARGE_INTEGER freq;
QueryPerformanceFrequency (&freq);
if (freq.QuadPart != 0 && CPU.bRDTSC)
{
LARGE_INTEGER count1, count2;
DWORD minDiff;
cycle_t ClockCalibration = 0;
// Count cycles for at least 55 milliseconds.
// The performance counter is very low resolution compared to CPU
// speeds today, so the longer we count, the more accurate our estimate.
// On the other hand, we don't want to count too long, because we don't
// want the user to notice us spend time here, since most users will
// probably never use the performance statistics.
minDiff = freq.LowPart * 11 / 200;
// Minimize the chance of task switching during the testing by going very
// high priority. This is another reason to avoid timing for too long.
SetPriorityClass (GetCurrentProcess (), REALTIME_PRIORITY_CLASS);
SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_TIME_CRITICAL);
clock (ClockCalibration);
QueryPerformanceCounter (&count1);
do
{
QueryPerformanceCounter (&count2);
} while ((DWORD)((unsigned __int64)count2.QuadPart - (unsigned __int64)count1.QuadPart) < minDiff);
unclock (ClockCalibration);
QueryPerformanceCounter (&count2);
SetPriorityClass (GetCurrentProcess (), NORMAL_PRIORITY_CLASS);
SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_NORMAL);
CyclesPerSecond = (double)ClockCalibration *
(double)freq.QuadPart /
(double)((__int64)count2.QuadPart - (__int64)count1.QuadPart);
SecondsPerCycle = 1.0 / CyclesPerSecond;
}
else
{
Printf ("Can't determine CPU speed, so pretending.\n");
}
Printf ("CPU Speed: %f MHz\n", CyclesPerSecond / 1e6);
}
//
// I_Quit
//
static int has_exited;
void I_Quit (void)
{
has_exited = 1; /* Prevent infinitely recursive exits -- killough */
if (TimerEventID)
timeKillEvent (TimerEventID);
if (NewTicArrived)
CloseHandle (NewTicArrived);
timeEndPeriod (TimerPeriod);
if (demorecording)
G_CheckDemoStatus();
G_ClearSnapshots ();
}
//
// I_Error
//
extern FILE *Logfile;
bool gameisdead;
void STACK_ARGS I_FatalError (const char *error, ...)
{
static BOOL alreadyThrown = false;
gameisdead = true;
if (!alreadyThrown) // ignore all but the first message -- killough
{
char errortext[MAX_ERRORTEXT];
int index;
va_list argptr;
va_start (argptr, error);
index = vsprintf (errortext, error, argptr);
va_end (argptr);
// Record error to log (if logging)
if (Logfile)
fprintf (Logfile, "\n**** DIED WITH FATAL ERROR:\n%s\n", errortext);
throw CFatalError (errortext);
}
if (!has_exited) // If it hasn't exited yet, exit now -- killough
{
has_exited = 1; // Prevent infinitely recursive exits -- killough
exit(-1);
}
}
void STACK_ARGS I_Error (const char *error, ...)
{
va_list argptr;
char errortext[MAX_ERRORTEXT];
va_start (argptr, error);
vsprintf (errortext, error, argptr);
va_end (argptr);
throw CRecoverableError (errortext);
}
extern void LayoutMainWindow (HWND hWnd, HWND pane);
void I_SetIWADInfo (const IWADInfo *info)
{
DoomStartupInfo = info;
// Make the startup banner show itself
LayoutMainWindow (Window, NULL);
}
void I_PrintStr (const char *cp)
{
if (ConWindow == NULL)
return;
static bool newLine = true;
HWND edit = ConWindow;
char buf[256];
int bpos = 0;
INTBOOL visible = GetWindowLongPtr (ConWindow, GWL_STYLE) & WS_VISIBLE;
int maxsel, selstart, selend, numlines1, numlines2;
if (visible)
{
SendMessage (edit, WM_SETREDRAW, FALSE, 0);
}
numlines1 = SendMessage (edit, EM_GETLINECOUNT, 0, 0);
maxsel = SendMessage (edit, WM_GETTEXTLENGTH, 0, 0);
SendMessage (edit, EM_GETSEL, (WPARAM)&selstart, (LPARAM)&selend);
SendMessage (edit, EM_SETSEL, maxsel, maxsel);
while (*cp != 0)
{
if (*cp == 28)
{ // Skip color changes
if (*++cp != 0)
cp++;
continue;
}
if (bpos < 253)
{
// Stupid edit controls need CR-LF pairs
if (*cp == '\n')
{
buf[bpos++] = '\r';
}
}
else
{
buf[bpos] = 0;
SendMessage (edit, EM_REPLACESEL, FALSE, (LPARAM)buf);
newLine = buf[bpos-1] == '\n';
bpos = 0;
}
buf[bpos++] = *cp++;
}
if (bpos != 0)
{
buf[bpos] = 0;
SendMessage (edit, EM_REPLACESEL, FALSE, (LPARAM)buf);
newLine = buf[bpos-1] == '\n';
}
SendMessage (edit, EM_SETSEL, selstart, selend);
numlines2 = SendMessage (edit, EM_GETLINECOUNT, 0, 0);
if (numlines2 > numlines1)
{
SendMessage (edit, EM_LINESCROLL, 0, numlines2 - numlines1);
}
if (visible)
{
SendMessage (edit, WM_SETREDRAW, TRUE, 0);
I_GetEvent ();
}
}
EXTERN_CVAR (Bool, queryiwad);
CVAR (String, queryiwad_key, "shift", CVAR_GLOBALCONFIG|CVAR_ARCHIVE);
static WadStuff *WadList;
static int NumWads;
static int DefaultWad;
static void SetQueryIWad (HWND dialog)
{
HWND checkbox = GetDlgItem (dialog, IDC_DONTASKIWAD);
int state = SendMessage (checkbox, BM_GETCHECK, 0, 0);
bool query = (state != BST_CHECKED);
if (!query && queryiwad)
{
MessageBox (dialog,
"You have chosen not to show this dialog box in the future.\n"
"If you wish to see it again, hold down SHIFT while starting " GAMENAME ".",
"Don't ask me this again",
MB_OK | MB_ICONINFORMATION);
}
queryiwad = query;
}
BOOL CALLBACK IWADBoxCallback (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND ctrl;
int i;
switch (message)
{
case WM_INITDIALOG:
// Add our program name to the window title
{
TCHAR label[256];
FString newlabel;
GetWindowText (hDlg, label, countof(label));
newlabel.Format (GAMESIG " " DOTVERSIONSTR_NOREV ": %s", label);
SetWindowText (hDlg, newlabel.GetChars());
}
// Populate the list with all the IWADs found
ctrl = GetDlgItem (hDlg, IDC_IWADLIST);
for (i = 0; i < NumWads; i++)
{
FString work;
const char *filepart = strrchr (WadList[i].Path, '/');
if (filepart == NULL)
filepart = WadList[i].Path;
else
filepart++;
work.Format ("%s (%s)", IWADInfos[WadList[i].Type].Name, filepart);
SendMessage (ctrl, LB_ADDSTRING, 0, (LPARAM)work.GetChars());
SendMessage (ctrl, LB_SETITEMDATA, i, (LPARAM)i);
}
SendMessage (ctrl, LB_SETCURSEL, DefaultWad, 0);
SetFocus (ctrl);
// Set the state of the "Don't ask me again" checkbox
ctrl = GetDlgItem (hDlg, IDC_DONTASKIWAD);
SendMessage (ctrl, BM_SETCHECK, queryiwad ? BST_UNCHECKED : BST_CHECKED, 0);
// Make sure the dialog is in front. If SHIFT was pressed to force it visible,
// then the other window will normally be on top.
SetForegroundWindow (hDlg);
break;
case WM_COMMAND:
if (LOWORD(wParam) == IDCANCEL)
{
EndDialog (hDlg, -1);
}
else if (LOWORD(wParam) == IDOK ||
(LOWORD(wParam) == IDC_IWADLIST && HIWORD(wParam) == LBN_DBLCLK))
{
SetQueryIWad (hDlg);
ctrl = GetDlgItem (hDlg, IDC_IWADLIST);
EndDialog (hDlg, SendMessage (ctrl, LB_GETCURSEL, 0, 0));
}
break;
}
return FALSE;
}
int I_PickIWad (WadStuff *wads, int numwads, bool showwin, int defaultiwad)
{
int vkey;
if (stricmp (queryiwad_key, "shift") == 0)
{
vkey = VK_SHIFT;
}
else if (stricmp (queryiwad_key, "control") == 0 || stricmp (queryiwad_key, "ctrl") == 0)
{
vkey = VK_CONTROL;
}
else
{
vkey = 0;
}
if (showwin || (vkey != 0 && GetAsyncKeyState(vkey)))
{
WadList = wads;
NumWads = numwads;
DefaultWad = defaultiwad;
return DialogBox (g_hInst, MAKEINTRESOURCE(IDD_IWADDIALOG),
(HWND)Window, (DLGPROC)IWADBoxCallback);
}
return defaultiwad;
}
bool I_WriteIniFailed ()
{
char *lpMsgBuf;
FString errortext;
FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPSTR)&lpMsgBuf,
0,
NULL
);
errortext.Format ("The config file %s could not be written:\n%s", GameConfig->GetPathName(), lpMsgBuf);
LocalFree (lpMsgBuf);
return MessageBox (Window, errortext.GetChars(), GAMENAME " configuration not saved", MB_ICONEXCLAMATION | MB_RETRYCANCEL) == IDRETRY;
}
void *I_FindFirst (const char *filespec, findstate_t *fileinfo)
{
return FindFirstFileA (filespec, (LPWIN32_FIND_DATAA)fileinfo);
}
int I_FindNext (void *handle, findstate_t *fileinfo)
{
return !FindNextFileA ((HANDLE)handle, (LPWIN32_FIND_DATAA)fileinfo);
}
int I_FindClose (void *handle)
{
return FindClose ((HANDLE)handle);
}