1
0
forked from 0ad/0ad

# more SwEng improvements in snd_mgr / wdetect

snd_mgr: replace al_check("funcname") with AL_CHECK macro; allow
disabling WIN_LOADLIBRARY_HACK.
wcpu: move sys_on_each_cpu here from wsysdep
split up wdetect into wgfx, wsnd, wdll_ver

This was SVN commit r3748.
This commit is contained in:
janwas 2006-04-11 01:45:07 +00:00
parent bd431d25f6
commit cde3c55eae
11 changed files with 586 additions and 566 deletions

View File

@ -52,7 +52,7 @@
// convenience "class" that simplifies successively appending a filename to
// its parent directory. this avoids needing to allocate memory and calling
// strlen/strcat. used by wdetect and dir_next_ent.
// strlen/strcat. used by wdll_ver and dir_next_ent.
// we want to maintain C compatibility, so this isn't a C++ class.
// write the given directory path into our buffer and set end/chars_left
@ -312,7 +312,7 @@ LibError dir_open(const char* P_path, DirIterator* d_)
char n_path[PATH_MAX];
// HACK: allow calling with a full (absolute) native path.
// (required by wdetect).
// (required by wdll_ver).
#if OS_WIN
if(P_path[1] == ':' && P_path[2] == '\\')
strcpy_s(n_path, ARRAY_SIZE(n_path), P_path);

View File

@ -26,7 +26,7 @@ extern LibError file_init();
// convenience "class" that simplifies successively appending a filename to
// its parent directory. this avoids needing to allocate memory and calling
// strlen/strcat. used by wdetect and dir_next_ent.
// strlen/strcat. used by wdll_ver and dir_next_ent.
// we want to maintain C compatibility, so this isn't a C++ class.
struct PathPackage

View File

@ -51,7 +51,6 @@
#include "app_hooks.h"
#define OGG_HACK
#include "ogghack.h"
@ -60,6 +59,13 @@
# pragma comment(lib, "alut.lib") // alutLoadWAVMemory
#endif
// HACK: OpenAL loads and unloads certain DLLs several times on Windows.
// that looks unnecessary and wastes 100..400 ms on startup.
// we hold a reference to prevent the actual unload. everything works ATM;
// hopefully, OpenAL doesn't rely on them actually being unloaded.
#define WIN_LOADLIBRARY_HACK 1
// components:
// - alc*: OpenAL context
// readies OpenAL for use; allows specifying the device.
@ -118,10 +124,13 @@ static void al_check(const char* caller = "(unknown)")
return;
const char* str = (const char*)alGetString(err);
debug_printf("openal error: %s; called from %s\n", str, caller);
debug_printf("OpenAL error: %s; called from %s\n", str, caller);
debug_warn("OpenAL error");
}
// convenience version that automatically passes in function name.
#define AL_CHECK al_check(__func__)
///////////////////////////////////////////////////////////////////////////////
//
@ -202,12 +211,7 @@ static LibError alc_init()
{
LibError ret = ERR_OK;
// HACK: OpenAL loads and unloads these DLLs several times on Windows.
// we hold a reference to prevent the actual unload,
// thus speeding up startup by 100..400 ms. everything works ATM;
// hopefully, OpenAL doesn't rely on them actually being unloaded.
#if OS_WIN
#if WIN_LOADLIBRARY_HACK
HMODULE dlls[3];
dlls[0] = LoadLibrary("wrap_oal.dll");
dlls[1] = LoadLibrary("setupapi.dll");
@ -257,8 +261,8 @@ static LibError alc_init()
swprintf(buf, ARRAY_SIZE(buf), L"SND| alc_init: success, using %hs\n", dev_name);
ah_log(buf);
#if WIN_LOADLIBRARY_HACK
// release DLL references, so BoundsChecker doesn't complain at exit.
#if OS_WIN
for(int i = 0; i < ARRAY_SIZE(dlls); i++)
if(dlls[i] != INVALID_HANDLE_VALUE)
FreeLibrary(dlls[i]);
@ -288,11 +292,9 @@ static void al_listener_latch()
if(al_initialized)
{
alListenerf(AL_GAIN, al_listener_gain);
al_check("al_listener_latch: gain");
alListenerfv(AL_POSITION, al_listener_pos);
al_check("al_listener_latch: pos");
alListenerfv(AL_ORIENTATION, al_listener_orientation);
al_check("al_listener_latch: orientation");
AL_CHECK;
}
}
@ -355,7 +357,7 @@ static ALuint al_buf_alloc(ALvoid* data, ALsizei size, ALenum al_fmt, ALsizei al
ALuint al_buf;
alGenBuffers(1, &al_buf);
alBufferData(al_buf, al_fmt, data, size, al_freq);
al_check("al_buf_alloc");
AL_CHECK;
al_bufs_outstanding++;
return al_buf;
@ -371,7 +373,7 @@ static void al_buf_free(ALuint al_buf)
debug_assert(alIsBuffer(al_buf));
alDeleteBuffers(1, &al_buf);
al_check("al_buf_free");
AL_CHECK;
al_bufs_outstanding--;
}
@ -440,7 +442,7 @@ static void al_src_shutdown()
debug_assert(al_src_used == 0);
alDeleteSources(al_src_allocated, al_srcs);
al_src_allocated = 0;
al_check("al_src_shutdown");
AL_CHECK;
}
@ -1581,7 +1583,7 @@ static void vsrc_latch(VSrc* vs)
alSourcef (vs->al_src, AL_GAIN, vs->gain);
alSourcef (vs->al_src, AL_PITCH, vs->pitch);
alSourcei (vs->al_src, AL_LOOPING, vs->loop);
al_check("vsrc_latch");
AL_CHECK;
}
@ -1598,7 +1600,7 @@ static int vsrc_deque_finished_bufs(VSrc* vs)
snd_data_buf_free(vs->hsd, al_buf);
}
al_check("vsrc_deque_finished_bufs");
AL_CHECK;
return num_processed;
}
@ -1694,7 +1696,7 @@ static LibError vsrc_grant(VSrc* vs)
alSourcefv(vs->al_src, AL_DIRECTION, zero3);
alSourcef (vs->al_src, AL_ROLLOFF_FACTOR, 0.0f);
alSourcei (vs->al_src, AL_SOURCE_RELATIVE, AL_TRUE);
al_check("vsrc_grant");
AL_CHECK;
// set remaining (user-specifiable) properties.
vsrc_latch(vs);
@ -1718,7 +1720,7 @@ static LibError vsrc_reclaim(VSrc* vs)
return ERR_FAIL; // NOWARN
alSourceStop(vs->al_src);
al_check("src_stop");
AL_CHECK;
// (note: all queued buffers are now considered 'processed')
// deque and free remaining buffers (if sound was closed abruptly).

View File

@ -118,6 +118,52 @@ LibError win_get_cpu_info()
}
//-----------------------------------------------------------------------------
// execute the specified function once on each CPU.
// this includes logical HT units and proceeds serially (function
// is never re-entered) in order of increasing OS CPU ID.
// note: implemented by switching thread affinity masks and forcing
// a reschedule, which is apparently not possible with POSIX.
// return 0 on success or a negative error code on failure
// (e.g. if OS is preventing us from running on some CPUs).
// called from ia32.cpp get_cpu_count
LibError sys_on_each_cpu(void (*cb)())
{
const HANDLE hProcess = GetCurrentProcess();
DWORD process_affinity, system_affinity;
if(!GetProcessAffinityMask(hProcess, &process_affinity, &system_affinity))
WARN_RETURN(ERR_FAIL);
// our affinity != system affinity: OS is limiting the CPUs that
// this process can run on. fail (cannot call back for each CPU).
if(process_affinity != system_affinity)
WARN_RETURN(ERR_CPU_RESTRICTED_AFFINITY);
for(DWORD cpu_bit = 1; cpu_bit != 0 && cpu_bit <= process_affinity; cpu_bit *= 2)
{
// check if we can switch to target CPU
if(!(process_affinity & cpu_bit))
continue;
// .. and do so.
if(!SetProcessAffinityMask(hProcess, process_affinity))
{
WARN_ERR(ERR_CPU_RESTRICTED_AFFINITY);
continue;
}
// reschedule, to make sure we switch CPUs
Sleep(1);
cb();
}
// restore to original value
SetProcessAffinityMask(hProcess, process_affinity);
return ERR_OK;
}
//////////////////////////////////////////////////////////////////////////////
//
//

View File

@ -1,500 +0,0 @@
// Windows-specific system detect
//
// Copyright (c) 2004 Jan Wassenberg
//
// This file is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// Contact info:
// Jan.Wassenberg@stud.uni-karlsruhe.de
// http://www.stud.uni-karlsruhe.de/~urkt/
#include "precompiled.h"
#include "sysdep/gfx.h"
#include "sysdep/snd.h"
#include "lib.h"
#include "lib/res/file/file.h" // file_enum
#include "win_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <set>
#include <string>
// DirectSound header
// HACK: workaround for "subwtype.h not found" errors on VC6/7 hybrid.
// (subwtype.h <- d3dtypes.h <- dsound.h)
// since we're only using one DS function (DirectSoundEnumerate),
// we forward-declare it rather than fix/mess with the system headers.
#if 0
// mmsystem.h is necessary for dsound.h; we cut out unnecessary junk
# define MMNODRV // Installable driver support
# define MMNOSOUND // Sound support
//# define MMNOWAVE // Waveform support
# define MMNOMIDI // MIDI support
# define MMNOAUX // Auxiliary audio support
# define MMNOMIXER // Mixer support
# define MMNOTIMER // Timer support
# define MMNOJOY // Joystick support
# define MMNOMCI // MCI support
# define MMNOMMIO // Multimedia file I/O support
# define MMNOMMSYSTEM // General MMSYSTEM functions
# include <MMSystem.h>
# define DIRECTSOUND_VERSION 0x0500
# include <dsound.h>
#else
# define DS_OK 0
typedef BOOL (CALLBACK* LPDSENUMCALLBACKA)(void*, const char*, const char*, void*);
extern "C" __declspec(dllimport) HRESULT WINAPI DirectSoundEnumerateA(LPDSENUMCALLBACKA, void*);
#endif
// these are all delay-loaded - they're not needed if
// system information is never queried.
#if MSC_VERSION
#pragma comment(lib, "version.lib") // DLL version
#pragma comment(lib, "advapi32.lib") // registry
#pragma comment(lib, "dsound.lib") // sound card name
#endif
// EnumDisplayDevices is not available on Win95 or NT.
// try to import it manually here; return -1 if not available.
static BOOL (WINAPI *pEnumDisplayDevicesA)(LPCSTR, DWORD, LPDISPLAY_DEVICEA, DWORD);
static LibError import_EnumDisplayDevices()
{
if(!pEnumDisplayDevicesA)
{
static HMODULE hUser32Dll = LoadLibrary("user32.dll");
*(void**)&pEnumDisplayDevicesA = GetProcAddress(hUser32Dll, "EnumDisplayDevicesA");
// do not free the DLL reference! if this happens to be the first
// use of user32 (possible if it's delay-loaded or otherwise unused),
// it would actually be unloaded. that apparently breaks the
// Windows cursor.
// unfortunately there's no way to get at the reference count,
// so this resource leak is unavoidable.
}
return pEnumDisplayDevicesA? ERR_OK : ERR_FAIL;
}
// useful for choosing a video mode.
// if we fail, outputs are unchanged (assumed initialized to defaults)
LibError gfx_get_video_mode(int* xres, int* yres, int* bpp, int* freq)
{
// don't use EnumDisplaySettingsW - BoundsChecker reports it causes
// a memory overrun (even if called as the very first thing, before
// static CRT initialization).
DEVMODEA dm;
memset(&dm, 0, sizeof(dm));
dm.dmSize = sizeof(dm);
// dm.dmDriverExtra already set to 0 by memset
if(!EnumDisplaySettingsA(0, ENUM_CURRENT_SETTINGS, &dm))
WARN_RETURN(ERR_FAIL);
if(dm.dmFields & (DWORD)DM_PELSWIDTH && xres)
*xres = (int)dm.dmPelsWidth;
if(dm.dmFields & (DWORD)DM_PELSHEIGHT && yres)
*yres = (int)dm.dmPelsHeight;
if(dm.dmFields & (DWORD)DM_BITSPERPEL && bpp)
*bpp = (int)dm.dmBitsPerPel;
if(dm.dmFields & (DWORD)DM_DISPLAYFREQUENCY && freq)
*freq = (int)dm.dmDisplayFrequency;
return ERR_OK;
}
// useful for determining aspect ratio.
// if we fail, outputs are unchanged (assumed initialized to defaults)
LibError gfx_get_monitor_size(int& width_mm, int& height_mm)
{
// (DC for the primary monitor's entire screen)
HDC dc = GetDC(0);
width_mm = GetDeviceCaps(dc, HORZSIZE);
height_mm = GetDeviceCaps(dc, VERTSIZE);
ReleaseDC(0, dc);
return ERR_OK;
}
//----------------------------------------------------------------------------
// support routines for getting DLL version
//----------------------------------------------------------------------------
static LibError get_ver(const char* module_path, char* out_ver, size_t out_ver_len)
{
WIN_SAVE_LAST_ERROR; // GetFileVersion*, Ver*
// determine size of and allocate memory for version information.
DWORD unused;
const DWORD ver_size = GetFileVersionInfoSize(module_path, &unused);
if(!ver_size)
WARN_RETURN(ERR_FAIL);
void* buf = malloc(ver_size);
if(!buf)
WARN_RETURN(ERR_NO_MEM);
LibError ret = ERR_FAIL; // single point of exit (for free())
if(GetFileVersionInfo(module_path, 0, ver_size, buf))
{
u16* lang; // -> 16 bit language ID, 16 bit codepage
uint lang_len;
const BOOL ok = VerQueryValue(buf, "\\VarFileInfo\\Translation", (void**)&lang, &lang_len);
if(ok && lang && lang_len == 4)
{
char subblock[64];
sprintf(subblock, "\\StringFileInfo\\%04X%04X\\FileVersion", lang[0], lang[1]);
const char* in_ver;
uint in_ver_len;
if(VerQueryValue(buf, subblock, (void**)&in_ver, &in_ver_len))
{
strcpy_s(out_ver, out_ver_len, in_ver);
ret = ERR_OK;
}
}
}
free(buf);
WIN_RESTORE_LAST_ERROR;
return ret;
}
//
// build a string containing DLL filename(s) and their version info.
//
static char* dll_list_buf;
static size_t dll_list_chars;
static char* dll_list_pos;
static void dll_list_init(char* buf, size_t chars)
{
dll_list_pos = dll_list_buf = buf;
dll_list_chars = chars;
}
// read DLL file version and append that and its name to the list.
// return 0 on success or a negative error code.
//
// name should preferably be the complete path to DLL, to make sure
// we don't inadvertently load another one on the library search path.
// we add the .dll extension if necessary.
static LibError dll_list_add(const char* name)
{
// not be called before dll_list_init or after failure
if(!dll_list_pos)
WARN_RETURN(ERR_LOGIC);
// some driver names are stored in the registry without .dll extension.
// if necessary, copy to new buffer and add it there.
// note: do not change extension if present; some drivers have a
// ".sys" extension, so always appending ".dll" is incorrect.
char buf[MAX_PATH];
const char* dll_name = name;
const char* ext = strrchr(name, '.');
if(!ext)
{
snprintf(buf, ARRAY_SIZE(buf), "%s.dll", name);
dll_name = buf;
}
// read file version.
char dll_ver[128] = "unknown"; // enclosed in () below
(void)get_ver(dll_name, dll_ver, sizeof(dll_ver));
// if this fails, default is already set and we don't want to abort.
const ssize_t max_chars_to_write = (ssize_t)dll_list_chars - (dll_list_pos-dll_list_buf) - 10;
// reserves enough room for subsequent comma and "..." strings.
// not first time: prepend comma to string (room was reserved above).
if(dll_list_pos != dll_list_buf)
dll_list_pos += sprintf(dll_list_pos, ", ");
// extract filename.
const char* slash = strrchr(dll_name, '\\');
const char* dll_fn = slash? slash+1 : dll_name;
int len = snprintf(dll_list_pos, max_chars_to_write, "%s (%s)", dll_fn, dll_ver);
// success
if(len > 0)
{
dll_list_pos += len;
return ERR_OK;
}
// didn't fit; complain
sprintf(dll_list_pos, "..."); // (room was reserved above)
dll_list_pos = 0; // poison pill, prevent further calls
WARN_RETURN(ERR_BUF_SIZE);
}
//////////////////////////////////////////////////////////////////////////////
//
// graphics card / driver version
//
//////////////////////////////////////////////////////////////////////////////
static LibError win_get_gfx_card()
{
// make sure EnumDisplayDevices is available (as pEnumDisplayDevicesA)
if(import_EnumDisplayDevices() >= 0)
{
// loop through all display adapters and put the primary device's
// identifier in gfx_card.
// (we don't bother returning a list of all of them because it's
// likely to be redundant, e.g. "Radeon" and "Radeon Secondary";
// we're only interested in the 'main' card anyway)
DISPLAY_DEVICEA dd = { sizeof(DISPLAY_DEVICEA) };
for(DWORD dev_num = 0; ; dev_num++)
{
if(!pEnumDisplayDevicesA(0, dev_num, &dd, 0))
break;
// ignore mirror pseudo-devices (installed e.g. by NetMeeting)
if(dd.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER)
continue;
if(dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
{
strcpy_s(gfx_card, ARRAY_SIZE(gfx_card), (const char*)dd.DeviceString);
return ERR_OK;
}
}
}
WARN_RETURN(ERR_FAIL);
}
// note: this implementation doesn't require OpenGL to be initialized.
static LibError win_get_gfx_drv_ver()
{
// don't overwrite existing information
if(gfx_drv_ver[0] != '\0')
return INFO_SKIPPED;
// rationale:
// - we could easily determine the 2d driver via EnumDisplaySettings,
// but we want to query the actual OpenGL driver. see
// http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/009679.html ;
// in short, we need the exact OpenGL driver version because some
// driver packs (e.g. Omega) mix and match DLLs.
// - an alternative implementation would be to enumerate all
// DLLs loaded into the process, and check for a glBegin export.
// that requires toolhelp/PSAPI (a bit complicated) and telling
// ICD/opengl32.dll apart (not future-proof).
// - therefore, we stick with the OpenGLDrivers approach. since there is
// no good way to determine which of the subkeys (e.g. nvoglnt) is
// active (several may exist due to previously removed drivers),
// we just display all of them. it is obvious from looking at
// gfx_card which one is correct; we thus avoid driver-specific
// name checks and reporting incorrectly.
LibError ret = ERR_FAIL; // single point of exit (for RegCloseKey)
DWORD i;
char drv_name[MAX_PATH+1];
dll_list_init(gfx_drv_ver, GFX_DRV_VER_LEN);
HKEY hkOglDrivers;
const char* key = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\OpenGLDrivers";
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hkOglDrivers) != 0)
WARN_RETURN(ERR_FAIL);
// for each subkey (i.e. set of installed OpenGL drivers):
for(i = 0; ; i++)
{
char set_name[32];
DWORD set_name_len = ARRAY_SIZE(set_name);
LONG err = RegEnumKeyEx(hkOglDrivers, i, set_name, &set_name_len, 0, 0,0, 0);
if(err != ERROR_SUCCESS) // error or no more items - bail
break;
HKEY hkSet;
if(RegOpenKeyEx(hkOglDrivers, set_name, 0, KEY_QUERY_VALUE, &hkSet) == 0)
{
DWORD drv_name_len = ARRAY_SIZE(drv_name)-5; // for ".dll"
if(RegQueryValueEx(hkSet, "Dll", 0, 0, (LPBYTE)drv_name, &drv_name_len) == 0)
ret = dll_list_add(drv_name);
RegCloseKey(hkSet);
}
} // for each subkey
// for each value:
// (some old drivers, e.g. S3 Super Savage, store their ICD name in a
// single REG_SZ value. we therefore include those as well.)
for(i = 0; ; i++)
{
char value_name[100]; // we don't need this, but RegEnumValue fails otherwise.
DWORD value_name_len = ARRAY_SIZE(value_name);
DWORD type;
DWORD drv_name_len = ARRAY_SIZE(drv_name)-5; // for ".dll"
DWORD err = RegEnumValue(hkOglDrivers, i, value_name,&value_name_len,
0, &type, (LPBYTE)drv_name,&drv_name_len);
if(err != ERROR_SUCCESS) // error or no more items - bail
break;
if(type == REG_SZ)
ret = dll_list_add(drv_name);
} // for each value
RegCloseKey(hkOglDrivers);
return ret;
}
LibError win_get_gfx_info()
{
LibError err1 = win_get_gfx_card();
LibError err2 = win_get_gfx_drv_ver();
// don't exit before trying both
CHECK_ERR(err1);
CHECK_ERR(err2);
return ERR_OK;
}
//////////////////////////////////////////////////////////////////////////////
//
// sound card / driver version
//
//////////////////////////////////////////////////////////////////////////////
// note: OpenAL alGetString is worthless: it only returns OpenAL API version
// and renderer (e.g. "Software").
// ensures each OpenAL DLL is only listed once (even if present in several
// directories on our search path).
typedef std::set<std::string> StringSet;
// if this directory entry is an OpenAL DLL, add it to our list.
// (matches "*oal.dll" and "*OpenAL*", as with OpenAL router's search)
// called by add_oal_dlls_in_dir.
//
// note: we need the full DLL path for dll_list_add but DirEnt only gives us
// the name. for efficiency, we append this in a PathPackage allocated by
// add_oal_dlls_in_dir.
static LibError add_if_oal_dll(const DirEnt* ent, PathPackage* pp, StringSet* dlls)
{
const char* fn = ent->name;
// skip non-files.
if(!DIRENT_IS_DIR(ent))
return ERR_OK;
// skip if not an OpenAL DLL.
const size_t len = strlen(fn);
const bool oal = len >= 7 && !stricmp(fn+len-7, "oal.dll");
const bool openal = strstr(fn, "OpenAL") != 0;
if(!oal && !openal)
return ERR_OK;
// skip if already in StringSet (i.e. has already been dll_list_add-ed)
std::pair<StringSet::iterator, bool> ret = dlls->insert(fn);
if(!ret.second) // insert failed - element already there
return ERR_OK;
RETURN_ERR(pp_append_file(pp, fn));
return dll_list_add(pp->path);
}
// find all OpenAL DLLs in a dir (via file_enum and add_if_oal_dll).
// call in library search order (exe dir, then win sys dir); otherwise,
// DLLs in the executable's starting directory hide those of the
// same name in the system directory.
//
// <dir>: no trailing.
static LibError add_oal_dlls_in_dir(const char* dir, StringSet* dlls)
{
PathPackage pp;
RETURN_ERR(pp_set_dir(&pp, dir));
DirIterator d;
RETURN_ERR(dir_open(dir, &d));
DirEnt ent;
for(;;) // instead of while to avoid warning
{
LibError err = dir_next_ent(&d, &ent);
if(err != ERR_OK)
break;
(void)add_if_oal_dll(&ent, &pp, dlls);
}
(void)dir_close(&d);
return ERR_OK;
}
// DS3D driver path; filled by ds_enum, used by win_get_snd_info.
// side effect: remains zeroed if there's no sound card installed.
static char ds_drv_path[MAX_PATH+1];
// store sound card name and path to DirectSound driver.
// called for each DirectSound driver, but aborts after first valid driver.
static BOOL CALLBACK ds_enum(void* UNUSED(guid), const char* description,
const char* module, void* UNUSED(ctx))
{
// skip first (dummy) entry, where description == "Primary Sound Driver".
if(module[0] == '\0')
return TRUE; // continue calling
strcpy_s(snd_card, SND_CARD_LEN, description);
snprintf(ds_drv_path, ARRAY_SIZE(ds_drv_path), "%s\\drivers\\%s", win_sys_dir, module);
// note: this directory is not in LoadLibrary's search list,
// so we have to give the full pathname.
// we assume the first "driver name" (sound card) is the one we want;
// stick with that and stop calling.
return FALSE;
}
LibError win_get_snd_info()
{
// get sound card name and DS driver path.
if(DirectSoundEnumerateA((LPDSENUMCALLBACKA)ds_enum, (void*)0) != DS_OK)
debug_warn("DirectSoundEnumerate failed");
// there are apparently no sound card/drivers installed; so indicate.
// (the code below would fail and not produce reasonable output)
if(ds_drv_path[0] == '\0')
{
strcpy_s(snd_card, SND_CARD_LEN, "(none)");
strcpy_s(snd_drv_ver, SND_DRV_VER_LEN, "(none)");
return ERR_OK;
}
// find all DLLs related to OpenAL, retrieve their versions,
// and store in snd_drv_ver string.
dll_list_init(snd_drv_ver, SND_DRV_VER_LEN);
(void)dll_list_add(ds_drv_path);
StringSet dlls; // ensures uniqueness
(void)add_oal_dlls_in_dir(win_exe_dir, &dlls);
(void)add_oal_dlls_in_dir(win_sys_dir, &dlls);
return ERR_OK;
}

View File

@ -0,0 +1,142 @@
// Windows-specific system detect
//
// Copyright (c) 2004 Jan Wassenberg
//
// This file is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// Contact info:
// Jan.Wassenberg@stud.uni-karlsruhe.de
// http://www.stud.uni-karlsruhe.de/~urkt/
#include "precompiled.h"
#include <stdio.h>
#include <stdlib.h>
#include "win_internal.h"
#include "wdll_ver.h"
#if MSC_VERSION
#pragma comment(lib, "version.lib") // DLL version
#endif
// get version information for the specified DLL.
static LibError get_ver(const char* module_path, char* out_ver, size_t out_ver_len)
{
WIN_SAVE_LAST_ERROR; // GetFileVersion*, Ver*
// determine size of and allocate memory for version information.
DWORD unused;
const DWORD ver_size = GetFileVersionInfoSize(module_path, &unused);
if(!ver_size)
WARN_RETURN(ERR_FAIL);
void* buf = malloc(ver_size);
if(!buf)
WARN_RETURN(ERR_NO_MEM);
LibError ret = ERR_FAIL; // single point of exit (for free())
if(GetFileVersionInfo(module_path, 0, ver_size, buf))
{
u16* lang; // -> 16 bit language ID, 16 bit codepage
uint lang_len;
const BOOL ok = VerQueryValue(buf, "\\VarFileInfo\\Translation", (void**)&lang, &lang_len);
if(ok && lang && lang_len == 4)
{
char subblock[64];
sprintf(subblock, "\\StringFileInfo\\%04X%04X\\FileVersion", lang[0], lang[1]);
const char* in_ver;
uint in_ver_len;
if(VerQueryValue(buf, subblock, (void**)&in_ver, &in_ver_len))
{
strcpy_s(out_ver, out_ver_len, in_ver);
ret = ERR_OK;
}
}
}
free(buf);
WIN_RESTORE_LAST_ERROR;
return ret;
}
//
// build a string containing DLL filename(s) and their version info.
//
static char* dll_list_buf;
static size_t dll_list_chars;
static char* dll_list_pos;
// set output buffer into which DLL names and their versions will be written.
void dll_list_init(char* buf, size_t chars)
{
dll_list_pos = dll_list_buf = buf;
dll_list_chars = chars;
}
// read DLL file version and append that and its name to the list.
// return 0 on success or a negative error code.
//
// name should preferably be the complete path to DLL, to make sure
// we don't inadvertently load another one on the library search path.
// we add the .dll extension if necessary.
LibError dll_list_add(const char* name)
{
// not be called before dll_list_init or after failure
if(!dll_list_pos)
WARN_RETURN(ERR_LOGIC);
// some driver names are stored in the registry without .dll extension.
// if necessary, copy to new buffer and add it there.
// note: do not change extension if present; some drivers have a
// ".sys" extension, so always appending ".dll" is incorrect.
char buf[MAX_PATH];
const char* dll_name = name;
const char* ext = strrchr(name, '.');
if(!ext)
{
snprintf(buf, ARRAY_SIZE(buf), "%s.dll", name);
dll_name = buf;
}
// read file version.
char dll_ver[128] = "unknown"; // enclosed in () below
(void)get_ver(dll_name, dll_ver, sizeof(dll_ver));
// if this fails, default is already set and we don't want to abort.
const ssize_t max_chars_to_write = (ssize_t)dll_list_chars - (dll_list_pos-dll_list_buf) - 10;
// reserves enough room for subsequent comma and "..." strings.
// not first time: prepend comma to string (room was reserved above).
if(dll_list_pos != dll_list_buf)
dll_list_pos += sprintf(dll_list_pos, ", ");
// extract filename.
const char* slash = strrchr(dll_name, '\\');
const char* dll_fn = slash? slash+1 : dll_name;
int len = snprintf(dll_list_pos, max_chars_to_write, "%s (%s)", dll_fn, dll_ver);
// success
if(len > 0)
{
dll_list_pos += len;
return ERR_OK;
}
// didn't fit; complain
sprintf(dll_list_pos, "..."); // (room was reserved above)
dll_list_pos = 0; // poison pill, prevent further calls
WARN_RETURN(ERR_BUF_SIZE);
}

View File

@ -0,0 +1,17 @@
#ifndef WDETECT_H__
#define WDETECT_H__
// note: this module is not re-entrant or thread-safe!
// set output buffer into which DLL names and their versions will be written.
extern void dll_list_init(char* buf, size_t chars);
// read DLL file version and append that and its name to the list.
// return 0 on success or a negative error code.
//
// name should preferably be the complete path to DLL, to make sure
// we don't inadvertently load another one on the library search path.
// we add the .dll extension if necessary.
extern LibError dll_list_add(const char* name);
#endif // #ifndef WDETECT_H__

View File

@ -0,0 +1,196 @@
#include "precompiled.h"
#include "lib/sysdep/gfx.h"
#include "wdll_ver.h" // dll_list_*
#include "win_internal.h"
#if MSC_VERSION
#pragma comment(lib, "advapi32.lib") // registry
#endif
// useful for choosing a video mode.
// if we fail, outputs are unchanged (assumed initialized to defaults)
LibError gfx_get_video_mode(int* xres, int* yres, int* bpp, int* freq)
{
// don't use EnumDisplaySettingsW - BoundsChecker reports it causes
// a memory overrun (even if called as the very first thing, before
// static CRT initialization).
DEVMODEA dm;
memset(&dm, 0, sizeof(dm));
dm.dmSize = sizeof(dm);
// dm.dmDriverExtra already set to 0 by memset
if(!EnumDisplaySettingsA(0, ENUM_CURRENT_SETTINGS, &dm))
WARN_RETURN(ERR_FAIL);
if(dm.dmFields & (DWORD)DM_PELSWIDTH && xres)
*xres = (int)dm.dmPelsWidth;
if(dm.dmFields & (DWORD)DM_PELSHEIGHT && yres)
*yres = (int)dm.dmPelsHeight;
if(dm.dmFields & (DWORD)DM_BITSPERPEL && bpp)
*bpp = (int)dm.dmBitsPerPel;
if(dm.dmFields & (DWORD)DM_DISPLAYFREQUENCY && freq)
*freq = (int)dm.dmDisplayFrequency;
return ERR_OK;
}
// useful for determining aspect ratio.
// if we fail, outputs are unchanged (assumed initialized to defaults)
LibError gfx_get_monitor_size(int& width_mm, int& height_mm)
{
// (DC for the primary monitor's entire screen)
HDC dc = GetDC(0);
width_mm = GetDeviceCaps(dc, HORZSIZE);
height_mm = GetDeviceCaps(dc, VERTSIZE);
ReleaseDC(0, dc);
return ERR_OK;
}
//-----------------------------------------------------------------------------
// EnumDisplayDevices is not available on Win95 or NT.
// try to import it manually here; return -1 if not available.
static BOOL (WINAPI *pEnumDisplayDevicesA)(LPCSTR, DWORD, LPDISPLAY_DEVICEA, DWORD);
static LibError import_EnumDisplayDevices()
{
if(!pEnumDisplayDevicesA)
{
static HMODULE hUser32Dll = LoadLibrary("user32.dll");
*(void**)&pEnumDisplayDevicesA = GetProcAddress(hUser32Dll, "EnumDisplayDevicesA");
// do not free the DLL reference! if this happens to be the first
// use of user32 (possible if it's delay-loaded or otherwise unused),
// it would actually be unloaded. that apparently breaks the
// Windows cursor.
// unfortunately there's no way to get at the reference count,
// so this resource leak is unavoidable.
}
return pEnumDisplayDevicesA? ERR_OK : ERR_FAIL;
}
static LibError win_get_gfx_card()
{
// make sure EnumDisplayDevices is available (as pEnumDisplayDevicesA)
if(import_EnumDisplayDevices() >= 0)
{
// loop through all display adapters and put the primary device's
// identifier in gfx_card.
// (we don't bother returning a list of all of them because it's
// likely to be redundant, e.g. "Radeon" and "Radeon Secondary";
// we're only interested in the 'main' card anyway)
DISPLAY_DEVICEA dd = { sizeof(DISPLAY_DEVICEA) };
for(DWORD dev_num = 0; ; dev_num++)
{
if(!pEnumDisplayDevicesA(0, dev_num, &dd, 0))
break;
// ignore mirror pseudo-devices (installed e.g. by NetMeeting)
if(dd.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER)
continue;
if(dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
{
strcpy_s(gfx_card, ARRAY_SIZE(gfx_card), (const char*)dd.DeviceString);
return ERR_OK;
}
}
}
WARN_RETURN(ERR_FAIL);
}
// note: this implementation doesn't require OpenGL to be initialized.
static LibError win_get_gfx_drv_ver()
{
// don't overwrite existing information
if(gfx_drv_ver[0] != '\0')
return INFO_SKIPPED;
// rationale:
// - we could easily determine the 2d driver via EnumDisplaySettings,
// but we want to query the actual OpenGL driver. see
// http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/009679.html ;
// in short, we need the exact OpenGL driver version because some
// driver packs (e.g. Omega) mix and match DLLs.
// - an alternative implementation would be to enumerate all
// DLLs loaded into the process, and check for a glBegin export.
// that requires toolhelp/PSAPI (a bit complicated) and telling
// ICD/opengl32.dll apart (not future-proof).
// - therefore, we stick with the OpenGLDrivers approach. since there is
// no good way to determine which of the subkeys (e.g. nvoglnt) is
// active (several may exist due to previously removed drivers),
// we just display all of them. it is obvious from looking at
// gfx_card which one is correct; we thus avoid driver-specific
// name checks and reporting incorrectly.
LibError ret = ERR_FAIL; // single point of exit (for RegCloseKey)
DWORD i;
char drv_name[MAX_PATH+1];
dll_list_init(gfx_drv_ver, GFX_DRV_VER_LEN);
HKEY hkOglDrivers;
const char* key = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\OpenGLDrivers";
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hkOglDrivers) != 0)
WARN_RETURN(ERR_FAIL);
// for each subkey (i.e. set of installed OpenGL drivers):
for(i = 0; ; i++)
{
char set_name[32];
DWORD set_name_len = ARRAY_SIZE(set_name);
LONG err = RegEnumKeyEx(hkOglDrivers, i, set_name, &set_name_len, 0, 0,0, 0);
if(err != ERROR_SUCCESS) // error or no more items - bail
break;
HKEY hkSet;
if(RegOpenKeyEx(hkOglDrivers, set_name, 0, KEY_QUERY_VALUE, &hkSet) == 0)
{
DWORD drv_name_len = ARRAY_SIZE(drv_name)-5; // for ".dll"
if(RegQueryValueEx(hkSet, "Dll", 0, 0, (LPBYTE)drv_name, &drv_name_len) == 0)
ret = dll_list_add(drv_name);
RegCloseKey(hkSet);
}
} // for each subkey
// for each value:
// (some old drivers, e.g. S3 Super Savage, store their ICD name in a
// single REG_SZ value. we therefore include those as well.)
for(i = 0; ; i++)
{
char value_name[100]; // we don't need this, but RegEnumValue fails otherwise.
DWORD value_name_len = ARRAY_SIZE(value_name);
DWORD type;
DWORD drv_name_len = ARRAY_SIZE(drv_name)-5; // for ".dll"
DWORD err = RegEnumValue(hkOglDrivers, i, value_name,&value_name_len,
0, &type, (LPBYTE)drv_name,&drv_name_len);
if(err != ERROR_SUCCESS) // error or no more items - bail
break;
if(type == REG_SZ)
ret = dll_list_add(drv_name);
} // for each value
RegCloseKey(hkOglDrivers);
return ret;
}
LibError win_get_gfx_info()
{
LibError err1 = win_get_gfx_card();
LibError err2 = win_get_gfx_drv_ver();
// don't exit before trying both
CHECK_ERR(err1);
CHECK_ERR(err2);
return ERR_OK;
}

View File

@ -364,7 +364,7 @@ static bool is_normal_dir(const char* path)
if((fa & FILE_ATTRIBUTE_DIRECTORY) == 0)
return false;
// .. hidden or system attribute(s) set
// this check is now disabled because wdetect's add_oal_dlls_in_dir
// this check is now disabled because wsnd's add_oal_dlls_in_dir
// needs to open the Windows system directory, which sometimes has
// these attributes set.
//if((fa & hs) != 0)

View File

@ -0,0 +1,160 @@
#include "precompiled.h"
#include <string>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include "lib/sysdep/snd.h"
#include "wdll_ver.h" // dll_list_*
#include "win_internal.h"
#include "lib/res/file/file.h"
// DirectSound header
// HACK: workaround for "subwtype.h not found" errors on VC6/7 hybrid.
// (subwtype.h <- d3dtypes.h <- dsound.h)
// since we're only using one DS function (DirectSoundEnumerate),
// we forward-declare it rather than fix/mess with the system headers.
#if 0
// mmsystem.h is necessary for dsound.h; we cut out unnecessary junk
# define MMNODRV // Installable driver support
# define MMNOSOUND // Sound support
//# define MMNOWAVE // Waveform support
# define MMNOMIDI // MIDI support
# define MMNOAUX // Auxiliary audio support
# define MMNOMIXER // Mixer support
# define MMNOTIMER // Timer support
# define MMNOJOY // Joystick support
# define MMNOMCI // MCI support
# define MMNOMMIO // Multimedia file I/O support
# define MMNOMMSYSTEM // General MMSYSTEM functions
# include <MMSystem.h>
# define DIRECTSOUND_VERSION 0x0500
# include <dsound.h>
#else
# define DS_OK 0
typedef BOOL (CALLBACK* LPDSENUMCALLBACKA)(void*, const char*, const char*, void*);
extern "C" __declspec(dllimport) HRESULT WINAPI DirectSoundEnumerateA(LPDSENUMCALLBACKA, void*);
#endif
#if MSC_VERSION
#pragma comment(lib, "dsound.lib")
#endif
// note: OpenAL alGetString is worthless: it only returns OpenAL API version
// and renderer (e.g. "Software").
// ensures each OpenAL DLL is only listed once (even if present in several
// directories on our search path).
typedef std::set<std::string> StringSet;
// if this directory entry is an OpenAL DLL, add it to our list.
// (matches "*oal.dll" and "*OpenAL*", as with OpenAL router's search)
// called by add_oal_dlls_in_dir.
//
// note: we need the full DLL path for dll_list_add but DirEnt only gives us
// the name. for efficiency, we append this in a PathPackage allocated by
// add_oal_dlls_in_dir.
static LibError add_if_oal_dll(const DirEnt* ent, PathPackage* pp, StringSet* dlls)
{
const char* fn = ent->name;
// skip non-files.
if(!DIRENT_IS_DIR(ent))
return ERR_OK;
// skip if not an OpenAL DLL.
const size_t len = strlen(fn);
const bool oal = len >= 7 && !stricmp(fn+len-7, "oal.dll");
const bool openal = strstr(fn, "OpenAL") != 0;
if(!oal && !openal)
return ERR_OK;
// skip if already in StringSet (i.e. has already been dll_list_add-ed)
std::pair<StringSet::iterator, bool> ret = dlls->insert(fn);
if(!ret.second) // insert failed - element already there
return ERR_OK;
RETURN_ERR(pp_append_file(pp, fn));
return dll_list_add(pp->path);
}
// find all OpenAL DLLs in a dir (via file_enum and add_if_oal_dll).
// call in library search order (exe dir, then win sys dir); otherwise,
// DLLs in the executable's starting directory hide those of the
// same name in the system directory.
//
// <dir>: no trailing.
static LibError add_oal_dlls_in_dir(const char* dir, StringSet* dlls)
{
PathPackage pp;
RETURN_ERR(pp_set_dir(&pp, dir));
DirIterator d;
RETURN_ERR(dir_open(dir, &d));
DirEnt ent;
for(;;) // instead of while to avoid warning
{
LibError err = dir_next_ent(&d, &ent);
if(err != ERR_OK)
break;
(void)add_if_oal_dll(&ent, &pp, dlls);
}
(void)dir_close(&d);
return ERR_OK;
}
// DS3D driver path; filled by ds_enum, used by win_get_snd_info.
// side effect: remains zeroed if there's no sound card installed.
static char ds_drv_path[MAX_PATH+1];
// store sound card name and path to DirectSound driver.
// called for each DirectSound driver, but aborts after first valid driver.
static BOOL CALLBACK ds_enum(void* UNUSED(guid), const char* description,
const char* module, void* UNUSED(ctx))
{
// skip first (dummy) entry, where description == "Primary Sound Driver".
if(module[0] == '\0')
return TRUE; // continue calling
strcpy_s(snd_card, SND_CARD_LEN, description);
snprintf(ds_drv_path, ARRAY_SIZE(ds_drv_path), "%s\\drivers\\%s", win_sys_dir, module);
// note: this directory is not in LoadLibrary's search list,
// so we have to give the full pathname.
// we assume the first "driver name" (sound card) is the one we want;
// stick with that and stop calling.
return FALSE;
}
LibError win_get_snd_info()
{
// get sound card name and DS driver path.
if(DirectSoundEnumerateA((LPDSENUMCALLBACKA)ds_enum, (void*)0) != DS_OK)
debug_warn("DirectSoundEnumerate failed");
// there are apparently no sound card/drivers installed; so indicate.
// (the code below would fail and not produce reasonable output)
if(ds_drv_path[0] == '\0')
{
strcpy_s(snd_card, SND_CARD_LEN, "(none)");
strcpy_s(snd_drv_ver, SND_DRV_VER_LEN, "(none)");
return ERR_OK;
}
// find all DLLs related to OpenAL, retrieve their versions,
// and store in snd_drv_ver string.
dll_list_init(snd_drv_ver, SND_DRV_VER_LEN);
(void)dll_list_add(ds_drv_path);
StringSet dlls; // ensures uniqueness
(void)add_oal_dlls_in_dir(win_exe_dir, &dlls);
(void)add_oal_dlls_in_dir(win_sys_dir, &dlls);
return ERR_OK;
}

View File

@ -640,46 +640,3 @@ LibError sys_pick_directory(char* path, size_t buf_size)
return LibError_from_win32(ok);
}
// execute the specified function once on each CPU.
// this includes logical HT units and proceeds serially (function
// is never re-entered) in order of increasing OS CPU ID.
// note: implemented by switching thread affinity masks and forcing
// a reschedule, which is apparently not possible with POSIX.
// return 0 on success or a negative error code on failure
// (e.g. if OS is preventing us from running on some CPUs).
// called from ia32.cpp get_cpu_count
LibError sys_on_each_cpu(void (*cb)())
{
const HANDLE hProcess = GetCurrentProcess();
DWORD process_affinity, system_affinity;
if(!GetProcessAffinityMask(hProcess, &process_affinity, &system_affinity))
WARN_RETURN(ERR_FAIL);
// our affinity != system affinity: OS is limiting the CPUs that
// this process can run on. fail (cannot call back for each CPU).
if(process_affinity != system_affinity)
WARN_RETURN(ERR_CPU_RESTRICTED_AFFINITY);
for(DWORD cpu_bit = 1; cpu_bit != 0 && cpu_bit <= process_affinity; cpu_bit *= 2)
{
// check if we can switch to target CPU
if(!(process_affinity & cpu_bit))
continue;
// .. and do so.
if(!SetProcessAffinityMask(hProcess, process_affinity))
{
WARN_ERR(ERR_CPU_RESTRICTED_AFFINITY);
continue;
}
// reschedule, to make sure we switch CPUs
Sleep(1);
cb();
}
// restore to original value
SetProcessAffinityMask(hProcess, process_affinity);
return ERR_OK;
}