(svn r2728) -Fix/Feature: Change the driver probing algorithm
authortron
Wed, 27 Jul 2005 19:57:12 +0000
changeset 2210 58a293892a66
parent 2209 53b1df15a9fe
child 2211 07a209c31e4f
(svn r2728) -Fix/Feature: Change the driver probing algorithm
Instead of trying to start a single driver and bailing out if that fails, try to initialise one by one and use the first one which succeeds.
This should fix problems on machines with no sound card, where -s null had to be specified manually.
debug.c
debug.h
driver.c
functions.h
hal.h
os2.c
unix.c
video/dedicated_v.c
video/sdl_v.c
win32.c
--- a/debug.c	Wed Jul 27 19:26:53 2005 +0000
+++ b/debug.c	Wed Jul 27 19:57:12 2005 +0000
@@ -10,6 +10,7 @@
 #include "string.h"
 
 int _debug_ai_level;
+int _debug_driver_level;
 int _debug_grf_level;
 int _debug_map_level;
 int _debug_misc_level;
@@ -45,6 +46,7 @@
 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
 	static const DebugLevel debug_level[] = {
 	DEBUG_LEVEL(ai),
+	DEBUG_LEVEL(driver),
 	DEBUG_LEVEL(grf),
 	DEBUG_LEVEL(map),
 	DEBUG_LEVEL(misc),
--- a/debug.h	Wed Jul 27 19:26:53 2005 +0000
+++ b/debug.h	Wed Jul 27 19:57:12 2005 +0000
@@ -9,6 +9,7 @@
 	#define DEBUG(name, level) if (level == 0 || _debug_ ## name ## _level >= level) debug
 
 	extern int _debug_ai_level;
+	extern int _debug_driver_level;
 	extern int _debug_grf_level;
 	extern int _debug_map_level;
 	extern int _debug_misc_level;
--- a/driver.c	Wed Jul 27 19:26:53 2005 +0000
+++ b/driver.c	Wed Jul 27 19:57:12 2005 +0000
@@ -2,11 +2,28 @@
 
 #include "stdafx.h"
 #include "openttd.h"
+#include "debug.h"
 #include "driver.h"
 #include "functions.h"
 #include "hal.h"
 #include "string.h"
 
+#include "music/bemidi.h"
+#include "music/dmusic.h"
+#include "music/extmidi.h"
+#include "music/null_m.h"
+#include "music/os2_m.h"
+#include "music/win32_m.h"
+
+#include "sound/null_s.h"
+#include "sound/sdl_s.h"
+#include "sound/win32_s.h"
+
+#include "video/dedicated_v.h"
+#include "video/null_v.h"
+#include "video/sdl_v.h"
+#include "video/win32_v.h"
+
 typedef struct {
 	const DriverDesc *descs;
 	const char *name;
@@ -31,21 +48,6 @@
 	return NULL;
 }
 
-static const DriverDesc* ChooseDefaultDriver(const DriverDesc* dd)
-{
-	byte os_version = GetOSVersion();
-	const DriverDesc *best = NULL;
-	int best_pri = -1;
-
-	for (; dd->name != NULL; dd++) {
-		if ((int)(dd->flags & DF_PRIORITY_MASK) > best_pri && os_version >= (byte)dd->flags) {
-			best_pri = dd->flags & DF_PRIORITY_MASK;
-			best = dd;
-		}
-	}
-	return best;
-}
-
 void LoadDriver(int driver, const char *name)
 {
 	const DriverClass *dc = &_driver_classes[driver];
@@ -59,8 +61,23 @@
 
 	parms[0] = NULL;
 
-	if (!*name) {
-		dd = ChooseDefaultDriver(dc->descs);
+	if (*name == '\0') {
+		for (dd = dc->descs; dd->name != NULL; dd++) {
+			err = ((const HalCommonDriver*)dd->drv)->start(parms);
+			if (err == NULL) break;
+			DEBUG(driver, 1) ("Probing %s driver \"%s\" failed with error: %s",
+				dc->name, dd->name, err
+			);
+		}
+		if (dd->name == NULL) {
+			error("Couldn't find any suitable %s driver", dc->name);
+		}
+
+		DEBUG(driver, 1)
+			("Successfully probed %s driver \"%s\"", dc->name, dd->name);
+
+		var = dc->var;
+		*var = dd->drv;
 	} else {
 		// Extract the driver name and put parameter list in parm
 		ttd_strlcpy(buffer, name, sizeof(buffer));
@@ -80,16 +97,20 @@
 		dd = GetDriverByName(dc->descs, buffer);
 		if (dd == NULL)
 			error("No such %s driver: %s\n", dc->name, buffer);
+
+		var = dc->var;
+		if (*var != NULL) ((const HalCommonDriver*)*var)->stop();
+		*var = NULL;
+		drv = dd->drv;
+
+		err = ((const HalCommonDriver*)drv)->start(parms);
+		if (err != NULL) {
+			error("Unable to load driver %s(%s). The error was: %s\n",
+				dd->name, dd->longname, err
+			);
+		}
+		*var = drv;
 	}
-	var = dc->var;
-	if (*var != NULL) ((const HalCommonDriver*)*var)->stop();
-	*var = NULL;
-	drv = dd->drv;
-
-	err = ((const HalCommonDriver*)drv)->start(parms);
-	if (err != NULL)
-		error("Unable to load driver %s(%s). The error was: %s\n", dd->name, dd->longname, err);
-	*var = drv;
 }
 
 
@@ -133,3 +154,51 @@
 		}
 	}
 }
+
+
+const DriverDesc _music_driver_descs[] = {
+#ifdef __BEOS__
+	{ "bemidi",  "BeOS MIDI Driver",        &_bemidi_music_driver },
+#endif
+#ifdef __OS2__
+	{ "os2",     "OS/2 Music Driver",       &_os2_music_driver},
+#endif
+#ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT
+	{ "dmusic",  "DirectMusic MIDI Driver", &_dmusic_midi_driver },
+#endif
+#ifdef WIN32
+	{ "win32",   "Win32 MIDI Driver",       &_win32_music_driver },
+#endif
+#ifdef UNIX
+#if !defined(__BEOS__) && !defined(__MORPHOS__) && !defined(__AMIGA__)
+	{ "extmidi", "External MIDI Driver",    &_extmidi_music_driver },
+#endif
+#endif
+	{ "null",    "Null Music Driver",       &_null_music_driver },
+	{ NULL, NULL, NULL}
+};
+
+const DriverDesc _sound_driver_descs[] = {
+#ifdef WIN32
+	{ "win32", "Win32 WaveOut Driver", &_win32_sound_driver },
+#endif
+#ifdef WITH_SDL
+	{	"sdl",   "SDL Sound Driver",     &_sdl_sound_driver },
+#endif
+	{	"null",  "Null Sound Driver",    &_null_sound_driver },
+	{	NULL, NULL, NULL}
+};
+
+const DriverDesc _video_driver_descs[] = {
+#ifdef WIN32
+	{ "win32",      "Win32 GDI Video Driver", &_win32_video_driver },
+#endif
+#ifdef WITH_SDL
+	{ "sdl",        "SDL Video Driver",       &_sdl_video_driver },
+#endif
+	{ "null",       "Null Video Driver",      &_null_video_driver},
+#ifdef ENABLE_NETWORK
+	{ "dedicated",	"Dedicated Video Driver",	&_dedicated_video_driver},
+#endif
+	{ NULL, NULL, NULL}
+};
--- a/functions.h	Wed Jul 27 19:26:53 2005 +0000
+++ b/functions.h	Wed Jul 27 19:57:12 2005 +0000
@@ -273,7 +273,6 @@
 void SaveToConfig(void);
 void CheckConfig(void);
 int ttd_main(int argc, char* argv[]);
-byte GetOSVersion(void);
 
 void DeterminePaths(void);
 char * CDECL str_fmt(const char *str, ...);
--- a/hal.h	Wed Jul 27 19:26:53 2005 +0000
+++ b/hal.h	Wed Jul 27 19:57:12 2005 +0000
@@ -41,7 +41,6 @@
 	const char *name;
 	const char *longname;
 	const void *drv;
-	uint32 flags;
 } DriverDesc;
 
 enum {
--- a/os2.c	Wed Jul 27 19:26:53 2005 +0000
+++ b/os2.c	Wed Jul 27 19:57:12 2005 +0000
@@ -2,6 +2,7 @@
 
 #include "stdafx.h"
 #include "openttd.h"
+#include "hal.h"
 #include "variables.h"
 #include "string.h"
 #include "table/strings.h"
@@ -28,16 +29,6 @@
 
 #include <i86.h>
 
-#include "sound/null_s.h"
-#include "sound/sdl_s.h"
-
-#include "video/dedicated_v.h"
-#include "video/null_v.h"
-#include "video/sdl_v.h"
-
-#include "music/null_m.h"
-#include "music/os2_m.h"
-
 static inline int strcasecmp(const char* s1, const char* s2)
 {
 	return stricmp(s1, s2);
@@ -432,38 +423,6 @@
 	unlink(path);
 }
 
-const DriverDesc _video_driver_descs[] = {
-	{	"null",			"Null Video Driver",		&_null_video_driver,		0},
-#if defined(WITH_SDL)
-	{	"sdl",			"SDL Video Driver",			&_sdl_video_driver,			1},
-#endif
-#ifdef ENABLE_NETWORK
-	{	"dedicated",	"Dedicated Video Driver",	&_dedicated_video_driver,	0},
-#endif
-	{	NULL,			NULL,						NULL,						0}
-};
-
-const DriverDesc _sound_driver_descs[] = {
-	{	"null",	"Null Sound Driver",	&_null_sound_driver,		0},
-#if defined(WITH_SDL)
-	{	"sdl",	"SDL Sound Driver",		&_sdl_sound_driver,			1},
-#endif
-	{	NULL,	NULL,					NULL,						0}
-};
-
-const DriverDesc _music_driver_descs[] = {
-	{	"os2",		"OS/2 Music Driver",		&_os2_music_driver,			0},
-	{   "null",     "Null Music Driver",	    &_null_music_driver,	    1},
-	{	NULL,		NULL,						NULL,						0}
-};
-
-/* GetOSVersion returns the minimal required version of OS to be able to use that driver.
-	 Not needed for OS/2. */
-byte GetOSVersion(void)
-{
-	return 2;  // any arbitrary number bigger then 0
-}
-
 bool FileExists(const char *filename)
 {
 	return access(filename, 0) == 0;
--- a/unix.c	Wed Jul 27 19:26:53 2005 +0000
+++ b/unix.c	Wed Jul 27 19:57:12 2005 +0000
@@ -9,17 +9,6 @@
 #include "hal.h"
 #include "variables.h"
 
-#include "music/bemidi.h"
-#include "music/extmidi.h"
-#include "music/null_m.h"
-
-#include "sound/null_s.h"
-#include "sound/sdl_s.h"
-
-#include "video/dedicated_v.h"
-#include "video/null_v.h"
-#include "video/sdl_v.h"
-
 #include <dirent.h>
 #include <unistd.h>
 #include <sys/stat.h>
@@ -369,53 +358,6 @@
 	unlink(path);
 }
 
-const DriverDesc _video_driver_descs[] = {
-	{"null",	"Null Video Driver",	&_null_video_driver,	0},
-#if defined(WITH_SDL)
-	{ "sdl",	"SDL Video Driver",		&_sdl_video_driver,		1},
-#endif
-#ifdef ENABLE_NETWORK
-	{ "dedicated", "Dedicated Video Driver", &_dedicated_video_driver, 0},
-#endif
-	{ NULL,		NULL,									NULL,									0}
-};
-
-const DriverDesc _sound_driver_descs[] = {
-	{"null",	"Null Sound Driver",	&_null_sound_driver,		0},
-#if defined(WITH_SDL)
-	{ "sdl",	"SDL Sound Driver",		&_sdl_sound_driver,			1},
-#endif
-	{ NULL,		NULL,									NULL,										0}
-};
-
-#if defined(__APPLE__)
-#define EXTMIDI_PRI 2
-#else
-#define EXTMIDI_PRI 0
-#endif
-
-const DriverDesc _music_driver_descs[] = {
-#ifndef __BEOS__
-#if !defined(__MORPHOS__) && !defined(__AMIGA__)
-// MorphOS and AmigaOS have no music support
-	{"extmidi",	"External MIDI Driver",	&_extmidi_music_driver,	EXTMIDI_PRI},
-#endif
-#endif
-#ifdef __BEOS__
-	{ "bemidi",	"BeOS MIDI Driver",			&_bemidi_music_driver,	1},
-#endif
-	{   "null",	"Null Music Driver",		&_null_music_driver,		1},
-	{     NULL,	NULL,										NULL,										0}
-};
-
-/* GetOSVersion returns the minimal required version of OS to be able to use that driver.
-	 Not needed for *nix. */
-byte GetOSVersion(void)
-{
-	return 2;  // any arbitrary number bigger than 0
-				// numbers lower than 2 breaks default music selection on mac
-}
-
 bool FileExists(const char *filename)
 {
 	return access(filename, 0) == 0;
--- a/video/dedicated_v.c	Wed Jul 27 19:26:53 2005 +0000
+++ b/video/dedicated_v.c	Wed Jul 27 19:57:12 2005 +0000
@@ -86,14 +86,14 @@
 	if (hThread == NULL)
 		error("Cannot create console thread!");
 
-	DEBUG(misc, 0) ("Windows console thread started...");
+	DEBUG(driver, 1) ("Windows console thread started...");
 }
 
 static void CloseWindowsConsoleThread(void)
 {
 	CloseHandle(hThread);
 	CloseHandle(hEvent);
-	DEBUG(misc, 0) ("Windows console thread shut down...");
+	DEBUG(driver, 1) ("Windows console thread shut down...");
 }
 
 #endif
@@ -126,7 +126,7 @@
 	OS2_SwitchToConsoleMode();
 #endif
 
-	DEBUG(misc,0)("Loading dedicated server...");
+	DEBUG(driver, 1)("Loading dedicated server...");
 	return NULL;
 }
 
--- a/video/sdl_v.c	Wed Jul 27 19:26:53 2005 +0000
+++ b/video/sdl_v.c	Wed Jul 27 19:57:12 2005 +0000
@@ -169,7 +169,7 @@
 
 	GetAvailableVideoMode(&w, &h);
 
-	DEBUG(misc, 1) ("sdl: using mode %dx%d", w, h);
+	DEBUG(driver, 1) ("sdl: using mode %dx%d", w, h);
 
 	// DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK
 	newscreen = SDL_CALL SDL_SetVideoMode(w, h, 8, SDL_SWSURFACE | SDL_HWPALETTE | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
@@ -385,7 +385,7 @@
 	if (s != NULL) return s;
 
 	SDL_CALL SDL_VideoDriverName(buf, 30);
-	DEBUG(misc, 1) ("sdl: using driver '%s'", buf);
+	DEBUG(driver, 1) ("sdl: using driver '%s'", buf);
 
 	GetVideoModes();
 	CreateMainSurface(_cur_resolution[0], _cur_resolution[1]);
--- a/win32.c	Wed Jul 27 19:26:53 2005 +0000
+++ b/win32.c	Wed Jul 27 19:57:12 2005 +0000
@@ -17,20 +17,6 @@
 #include <fcntl.h>
 #include "variables.h"
 #include "win32.h"
-
-#include "driver.h"
-
-#include "music/dmusic.h"
-#include "music/null_m.h"
-#include "music/win32_m.h"
-
-#include "sound/null_s.h"
-#include "sound/sdl_s.h"
-#include "sound/win32_s.h"
-
-#include "video/dedicated_v.h"
-#include "video/null_v.h"
-#include "video/sdl_v.h"
 #include "video/win32_v.h"
 
 static bool _has_console;
@@ -933,73 +919,6 @@
 	DeleteFile(path);
 }
 
-#define Windows_2000		5
-#define Windows_NT3_51	4
-
-/* flags show the minimum required OS to use a given feature. Currently
-	 only dwMajorVersion and dwMinorVersion (WindowsME) are used
-														MajorVersion	MinorVersion
-		Windows Server 2003					5							 2				dmusic
-		Windows XP									5							 1				dmusic
-		Windows 2000								5							 0				dmusic
-		Windows NT 4.0							4							 0				win32
-		Windows Me									4							90				dmusic
-		Windows 98									4							10				win32
-		Windows 95									4							 0				win32
-		Windows NT 3.51							3							51				?????
-*/
-
-const DriverDesc _video_driver_descs[] = {
-	{"null", "Null Video Driver",				&_null_video_driver,		0},
-#if defined(WITH_SDL)
-	{"sdl", "SDL Video Driver",					&_sdl_video_driver,			1},
-#endif
-	{"win32", "Win32 GDI Video Driver",	&_win32_video_driver,		Windows_NT3_51},
-#ifdef ENABLE_NETWORK
-	{ "dedicated", "Dedicated Video Driver", &_dedicated_video_driver, 0},
-#endif
-	{ NULL, NULL, NULL, 0 }
-};
-
-const DriverDesc _sound_driver_descs[] = {
-	{"null", "Null Sound Driver",			&_null_sound_driver,	0},
-#if defined(WITH_SDL)
-	{"sdl", "SDL Sound Driver",				&_sdl_sound_driver,		1},
-#endif
-	{"win32", "Win32 WaveOut Driver",	&_win32_sound_driver,	Windows_NT3_51},
-	{ NULL, NULL, NULL, 0 }
-};
-
-const DriverDesc _music_driver_descs[] = {
-	{"null", "Null Music Driver",		&_null_music_driver,				0},
-#ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT
-	{"dmusic", "DirectMusic MIDI Driver",	&_dmusic_midi_driver,	Windows_2000},
-#endif
-	// Win32 MIDI driver has higher priority than DMusic, so this one is chosen
-	{"win32", "Win32 MIDI Driver",	&_win32_music_driver,				Windows_NT3_51},
-	{ NULL, NULL, NULL, 0 }
-};
-
-byte GetOSVersion(void)
-{
-	OSVERSIONINFO osvi;
-
-	ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
-	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
-
-	if (GetVersionEx(&osvi)) {
-		DEBUG(misc, 2) ("Windows Version is %d.%d", osvi.dwMajorVersion, osvi.dwMinorVersion);
-		// WinME needs directmusic too (dmusic, Windows_2000 mode), all others default to OK
-		if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) { return Windows_2000;} // WinME
-
-		return osvi.dwMajorVersion;
-	}
-
-	// GetVersionEx failed, but we can safely assume at least Win95/WinNT3.51 is used
-	DEBUG(misc, 0) ("Windows version retrieval failed, defaulting to level 4");
-	return Windows_NT3_51;
-}
-
 bool FileExists(const char *filename)
 {
 	HANDLE hand = CreateFile(filename, 0, 0, NULL, OPEN_EXISTING, 0, NULL);