driver.c
author bjarni
Sat, 29 Apr 2006 14:38:21 +0000
branch0.4
changeset 9973 72d7f7f30746
parent 9959 984493ab6fff
permissions -rw-r--r--
(svn r4624) -Backported r4149 from trunk
main reason is that it fixes the load/save issue for OSX 10.3.9, but the other stuff in this commit can't be taken as it's a result of the fix

full commit log entry:
-Codechange: [OSX] rewrite of how universal binaries are compiled

Now OSX stores object files in .OSX and instead of making FAT object files, there are one for each architecture
Each architecture got their own targets to make a non-FAT binary and in the end, lipo will merge them into one binary

It's now possible to select which architectures you want to support by defining OTTD_PPC, OTTD_PPC970 (G5) and/or OTTD_i386
All combos are supported. UNIVERSAL_BINARY and TRIPLE_BINARY can still be used even though it's possible to gain the same result by using the new flags
Making a universal build when you already got part of it compiled (say the PPC part), it will reuse it and only compile the i386 part to save time
Note: in some cases when you switch flags, you risk that openttd is not updated. Delete it and try again. The Makefile can't solve this except if it forces linking each time

This fixes: FS#87 universal binary building borked in 0.4.7
Now universal binaries work on OSX 10.3.9 again

Building universal binaries no longer needs to store flags in Makefile.config as the new design makes it possible to figure everything out automatically
/* $Id$ */

#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 "music/qtmidi.h"

#include "sound/null_s.h"
#include "sound/sdl_s.h"
#include "sound/cocoa_s.h"
#include "sound/win32_s.h"

#include "video/dedicated_v.h"
#include "video/null_v.h"
#include "video/sdl_v.h"
#include "video/cocoa_v.h"
#include "video/win32_v.h"

typedef struct DriverDesc {
	const char* name;
	const char* longname;
	const HalCommonDriver* drv;
} DriverDesc;

typedef struct DriverClass {
	const DriverDesc *descs;
	const char *name;
	const HalCommonDriver** drv;
} DriverClass;


#define M(x, y, z) { x, y, (const HalCommonDriver *)(void *)z }
static const DriverDesc _music_driver_descs[] = {
#ifdef __BEOS__
	M("bemidi",  "BeOS MIDI Driver",        &_bemidi_music_driver),
#endif
#ifdef __OS2__
	M("os2",     "OS/2 Music Driver",       &_os2_music_driver),
#endif
#ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT
	M("dmusic",  "DirectMusic MIDI Driver", &_dmusic_midi_driver),
#endif
#ifdef WIN32
	M("win32",   "Win32 MIDI Driver",       &_win32_music_driver),
#endif
#if defined(__APPLE__) && !defined(DEDICATED)
	M("qt",      "QuickTime MIDI Driver",   &_qtime_music_driver),
#endif
#ifdef UNIX
#if !defined(__MORPHOS__) && !defined(__AMIGA__)
	M("extmidi", "External MIDI Driver",    &_extmidi_music_driver),
#endif
#endif
	M("null",    "Null Music Driver",       &_null_music_driver),
	M(NULL, NULL, NULL)
};

static const DriverDesc _sound_driver_descs[] = {
#ifdef WIN32
	M("win32", "Win32 WaveOut Driver", &_win32_sound_driver),
#endif
#ifdef WITH_SDL
	M("sdl",   "SDL Sound Driver",     &_sdl_sound_driver),
#endif
#ifdef WITH_COCOA
	M("cocoa", "Cocoa Sound Driver",   &_cocoa_sound_driver),
#endif
	M("null",  "Null Sound Driver",    &_null_sound_driver),
	M(NULL, NULL, NULL)
};

static const DriverDesc _video_driver_descs[] = {
#ifdef WIN32
	M("win32",      "Win32 GDI Video Driver", &_win32_video_driver),
#endif
#ifdef WITH_SDL
	M("sdl",        "SDL Video Driver",       &_sdl_video_driver),
#endif
#ifdef WITH_COCOA
	M("cocoa",      "Cocoa Video Driver",       &_cocoa_video_driver),
#endif
	M("null",       "Null Video Driver",      &_null_video_driver),
#ifdef ENABLE_NETWORK
	M("dedicated",  "Dedicated Video Driver", &_dedicated_video_driver),
#endif
	M(NULL, NULL, NULL)
};
#undef M


#define M(x, y, z) { x, y, (const HalCommonDriver **)(void *)z }
static const DriverClass _driver_classes[] = {
	M(_video_driver_descs, "video", &_video_driver),
	M(_sound_driver_descs, "sound", &_sound_driver),
	M(_music_driver_descs, "music", &_music_driver)
};
#undef M

static const DriverDesc* GetDriverByName(const DriverDesc* dd, const char* name)
{
	for (; dd->name != NULL; dd++) {
		if (strcmp(dd->name, name) == 0) return dd;
	}
	return NULL;
}

void LoadDriver(int driver, const char *name)
{
	const DriverClass *dc = &_driver_classes[driver];
	const DriverDesc *dd;
	const char *err;

	if (*name == '\0') {
		for (dd = dc->descs; dd->name != NULL; dd++) {
			err = dd->drv->start(NULL);
			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);

		*dc->drv = dd->drv;
	} else {
		char* parm;
		char buffer[256];
		const char* parms[32];

		// Extract the driver name and put parameter list in parm
		ttd_strlcpy(buffer, name, sizeof(buffer));
		parm = strchr(buffer, ':');
		parms[0] = NULL;
		if (parm != NULL) {
			uint np = 0;
			// Tokenize the parm.
			do {
				*parm++ = '\0';
				if (np < lengthof(parms) - 1)
					parms[np++] = parm;
				while (*parm != '\0' && *parm != ',')
					parm++;
			} while (*parm == ',');
			parms[np] = NULL;
		}
		dd = GetDriverByName(dc->descs, buffer);
		if (dd == NULL)
			error("No such %s driver: %s\n", dc->name, buffer);

		if (*dc->drv != NULL) (*dc->drv)->stop();
		*dc->drv = NULL;

		err = dd->drv->start(parms);
		if (err != NULL) {
			error("Unable to load driver %s(%s). The error was: %s\n",
				dd->name, dd->longname, err
			);
		}
		*dc->drv = dd->drv;
	}
}


static const char* GetDriverParam(const char* const* parm, const char* name)
{
	size_t len;

	if (parm == NULL) return NULL;

	len = strlen(name);
	for (; *parm != NULL; parm++) {
		const char* p = *parm;

		if (strncmp(p, name, len) == 0) {
			if (p[len] == '=')  return p + len + 1;
			if (p[len] == '\0') return p + len;
		}
	}
	return NULL;
}

bool GetDriverParamBool(const char* const* parm, const char* name)
{
	return GetDriverParam(parm, name) != NULL;
}

int GetDriverParamInt(const char* const* parm, const char* name, int def)
{
	const char* p = GetDriverParam(parm, name);
	return p != NULL ? atoi(p) : def;
}


char *GetDriverList(char* p)
{
	const DriverClass* dc;

	for (dc = _driver_classes; dc != endof(_driver_classes); dc++) {
		const DriverDesc* dd;

		p += sprintf(p, "List of %s drivers:\n", dc->name);
		for (dd = dc->descs; dd->name != NULL; dd++) {
			p += sprintf(p, "%10s: %s\n", dd->name, dd->longname);
		}
		p += sprintf(p, "\n");
	}

	return p;
}