5 #include "stdafx.h" |
5 #include "stdafx.h" |
6 #include "openttd.h" |
6 #include "openttd.h" |
7 #include "debug.h" |
7 #include "debug.h" |
8 #include "driver.h" |
8 #include "driver.h" |
9 #include "functions.h" |
9 #include "functions.h" |
10 #include "hal.h" |
|
11 #include "string.h" |
10 #include "string.h" |
12 |
11 |
13 #include "music/bemidi.h" |
12 #include "sound/sound_driver.hpp" |
14 #include "music/dmusic.h" |
13 #include "music/music_driver.hpp" |
15 #include "music/extmidi.h" |
14 #include "video/video_driver.hpp" |
16 #include "music/null_m.h" |
|
17 #include "music/os2_m.h" |
|
18 #include "music/win32_m.h" |
|
19 #include "music/qtmidi.h" |
|
20 #include "music/libtimidity.h" |
|
21 |
15 |
22 #include "sound/null_s.h" |
16 SoundDriver *_sound_driver; |
23 #include "sound/sdl_s.h" |
17 MusicDriver *_music_driver; |
24 #include "sound/cocoa_s.h" |
18 VideoDriver *_video_driver; |
25 #include "sound/win32_s.h" |
|
26 |
|
27 #include "video/dedicated_v.h" |
|
28 #include "video/null_v.h" |
|
29 #include "video/sdl_v.h" |
|
30 #include "video/cocoa_v.h" |
|
31 #include "video/win32_v.h" |
|
32 |
|
33 struct DriverDesc { |
|
34 const char* name; |
|
35 const char* longname; |
|
36 const HalCommonDriver* drv; |
|
37 }; |
|
38 |
|
39 struct DriverClass { |
|
40 const DriverDesc *descs; |
|
41 const char *name; |
|
42 const HalCommonDriver** drv; |
|
43 }; |
|
44 |
|
45 |
|
46 #define M(x, y, z) { x, y, (const HalCommonDriver *)(void *)z } |
|
47 static const DriverDesc _music_driver_descs[] = { |
|
48 #ifdef __BEOS__ |
|
49 M("bemidi", "BeOS MIDI Driver", &_bemidi_music_driver), |
|
50 #endif |
|
51 #if defined(__OS2__) && !defined(__INNOTEK_LIBC__) |
|
52 M("os2", "OS/2 Music Driver", &_os2_music_driver), |
|
53 #endif |
|
54 #ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT |
|
55 M("dmusic", "DirectMusic MIDI Driver", &_dmusic_midi_driver), |
|
56 #endif |
|
57 #if defined(WIN32) && !defined(WINCE) |
|
58 M("win32", "Win32 MIDI Driver", &_win32_music_driver), |
|
59 #endif |
|
60 #if defined(__APPLE__) && !defined(DEDICATED) |
|
61 M("qt", "QuickTime MIDI Driver", &_qtime_music_driver), |
|
62 #endif |
|
63 #ifdef UNIX |
|
64 #if defined(LIBTIMIDITY) |
|
65 M("libtimidity", "LibTimidity MIDI Driver", &_libtimidity_music_driver), |
|
66 #endif /* LIBTIMIDITY */ |
|
67 #if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(__OS2__) && !defined(PSP) |
|
68 M("extmidi", "External MIDI Driver", &_extmidi_music_driver), |
|
69 #endif |
|
70 #endif |
|
71 M("null", "Null Music Driver", &_null_music_driver), |
|
72 M(NULL, NULL, NULL) |
|
73 }; |
|
74 |
|
75 static const DriverDesc _sound_driver_descs[] = { |
|
76 #if defined(WIN32) && !defined(WINCE) |
|
77 M("win32", "Win32 WaveOut Driver", &_win32_sound_driver), |
|
78 #endif |
|
79 #ifdef WITH_SDL |
|
80 M("sdl", "SDL Sound Driver", &_sdl_sound_driver), |
|
81 #endif |
|
82 #ifdef WITH_COCOA |
|
83 M("cocoa", "Cocoa Sound Driver", &_cocoa_sound_driver), |
|
84 #endif |
|
85 M("null", "Null Sound Driver", &_null_sound_driver), |
|
86 M(NULL, NULL, NULL) |
|
87 }; |
|
88 |
|
89 static const DriverDesc _video_driver_descs[] = { |
|
90 #ifdef WIN32 |
|
91 M("win32", "Win32 GDI Video Driver", &_win32_video_driver), |
|
92 #endif |
|
93 #ifdef WITH_SDL |
|
94 M("sdl", "SDL Video Driver", &_sdl_video_driver), |
|
95 #endif |
|
96 #ifdef WITH_COCOA |
|
97 M("cocoa", "Cocoa Video Driver", &_cocoa_video_driver), |
|
98 #endif |
|
99 M("null", "Null Video Driver", &_null_video_driver), |
|
100 #ifdef ENABLE_NETWORK |
|
101 M("dedicated", "Dedicated Video Driver", &_dedicated_video_driver), |
|
102 #endif |
|
103 M(NULL, NULL, NULL) |
|
104 }; |
|
105 #undef M |
|
106 |
|
107 |
|
108 #define M(x, y, z) { x, y, (const HalCommonDriver **)(void *)z } |
|
109 static const DriverClass _driver_classes[] = { |
|
110 M(_video_driver_descs, "video", &_video_driver), |
|
111 M(_sound_driver_descs, "sound", &_sound_driver), |
|
112 M(_music_driver_descs, "music", &_music_driver) |
|
113 }; |
|
114 #undef M |
|
115 |
|
116 static const DriverDesc* GetDriverByName(const DriverDesc* dd, const char* name) |
|
117 { |
|
118 for (; dd->name != NULL; dd++) { |
|
119 if (strcmp(dd->name, name) == 0) return dd; |
|
120 } |
|
121 return NULL; |
|
122 } |
|
123 |
|
124 void LoadDriver(int driver, const char *name) |
|
125 { |
|
126 const DriverClass *dc = &_driver_classes[driver]; |
|
127 const DriverDesc *dd; |
|
128 const char *err; |
|
129 |
|
130 if (*name == '\0') { |
|
131 for (dd = dc->descs; dd->name != NULL; dd++) { |
|
132 err = dd->drv->start(NULL); |
|
133 if (err == NULL) break; |
|
134 DEBUG(driver, 1, "Probing %s driver '%s' failed with error: %s", |
|
135 dc->name, dd->name, err |
|
136 ); |
|
137 } |
|
138 if (dd->name == NULL) error("Couldn't find any suitable %s driver", dc->name); |
|
139 |
|
140 DEBUG(driver, 1, "Successfully probed %s driver '%s'", dc->name, dd->name); |
|
141 |
|
142 *dc->drv = dd->drv; |
|
143 } else { |
|
144 char* parm; |
|
145 char buffer[256]; |
|
146 const char* parms[32]; |
|
147 |
|
148 /* Extract the driver name and put parameter list in parm */ |
|
149 ttd_strlcpy(buffer, name, sizeof(buffer)); |
|
150 parm = strchr(buffer, ':'); |
|
151 parms[0] = NULL; |
|
152 if (parm != NULL) { |
|
153 uint np = 0; |
|
154 /* Tokenize the parm. */ |
|
155 do { |
|
156 *parm++ = '\0'; |
|
157 if (np < lengthof(parms) - 1) |
|
158 parms[np++] = parm; |
|
159 while (*parm != '\0' && *parm != ',') |
|
160 parm++; |
|
161 } while (*parm == ','); |
|
162 parms[np] = NULL; |
|
163 } |
|
164 dd = GetDriverByName(dc->descs, buffer); |
|
165 if (dd == NULL) |
|
166 error("No such %s driver: %s\n", dc->name, buffer); |
|
167 |
|
168 if (*dc->drv != NULL) (*dc->drv)->stop(); |
|
169 *dc->drv = NULL; |
|
170 |
|
171 err = dd->drv->start(parms); |
|
172 if (err != NULL) { |
|
173 error("Unable to load driver %s(%s). The error was: %s\n", |
|
174 dd->name, dd->longname, err |
|
175 ); |
|
176 } |
|
177 *dc->drv = dd->drv; |
|
178 } |
|
179 } |
|
180 |
|
181 |
19 |
182 static const char* GetDriverParam(const char* const* parm, const char* name) |
20 static const char* GetDriverParam(const char* const* parm, const char* name) |
183 { |
21 { |
184 size_t len; |
22 size_t len; |
185 |
23 |