00001
00002
00034 #include "Route.h"
00035
00036 namespace compat30 {
00037
00041 #define GETLINE if ( !GetLine(config, i, s) ) return
00042
00043 static OutDevice * GetOut(int nr)
00044 {
00045 if ( nr < 0 )
00046 return 0;
00047 OutDevice *Out = OutDevice::GetDevice(nr);
00048 return Out;
00049 }
00050
00051
00053
00057 static RouteType Str2RT(mutChar *type)
00058 {
00059 int i;
00060
00061 for (i = 3; i > 0; i--)
00062 if
00063 #ifndef WX
00064 ( !strncmp(type, RTName[i], strlen(RTName[i])) )
00065 #else
00066 ( !wxStricmp(type, RTName[i]))
00067 #endif
00068 break;
00069
00070 return (RouteType) i;
00071 }
00072
00073
00074 static DevType Str2DT(const mutStringRef type)
00075 {
00076 wxString DTName[] = { _T(""), _T("MIDIPORT"), _T("MIDIFILE"), _T("GMN") };
00077 int i;
00078 DEBUGLOG2(other,_T("Comparing %s"),type.c_str());
00079
00080 for (i = 3; i > 0; i--) {
00081 DEBUGLOG2(other,_T("Testing %s"), DTName[i].c_str() );
00082
00083 if ( type.StartsWith(DTName[i]) )
00084 break;
00085 }
00086
00087 return (DevType)i;
00088 }
00089
00090
00091 static bool GetLine(const mutStringRef p, size_t& i,mutStringRef s)
00092 {
00093 size_t l = p.Length();
00094 start:
00095 if ( i >= l )
00096 return false;
00097 while ( i < l && wxString(_T(" \t\n\r")).Find(p.GetChar(i)) != -1 )
00098 i++;
00099 if ( i >= l )
00100 return false;
00101 size_t i1 = i;
00102 int i2 = p.find(_T("\n"), i1);
00103 if ( i2 == -1 ) {
00104 i = l;
00105 s = p.Mid(i1);
00106 } else {
00107 s = p.Mid(i1, i2-i1);
00108 i = (size_t) i2;
00109 }
00110
00111 s = s.Trim();
00112 if ( s.Length() == 0 || s.StartsWith(_T("#")) )
00113 goto start;
00114
00115 return true;
00116 }
00117
00118
00119 void LoadRoutes(const mutStringRef config)
00120 {
00121 bool error = false;
00122
00123 DEBUGLOG2(routing,_T(""));
00124 InDevice * InDevices = InDevice::GetDeviceList();
00125 OutDevice * OutDevices = OutDevice::GetDeviceList();
00126
00127
00128 if ( InDevices) {
00129 delete InDevices;
00130 InDevices = NULL;
00131 }
00132 if ( OutDevices ) {
00133 delete OutDevices;
00134 OutDevices = NULL;
00135 }
00136 while (Route * routes = Route::GetRouteList())
00137 delete routes;
00138
00139
00140 wxString s;
00141 size_t i = 0;
00142 GETLINE;
00143
00144 DEBUGLOG2(routing,_T("+%s"),s.c_str());
00145
00146 while ( !s.StartsWith(_T("OUTPUT")) ) {
00147 GETLINE;
00148 DEBUGLOG2(other,_T("+%s"),s.c_str());
00149 }
00150
00151 GETLINE;
00152 DEBUGLOG2(routing,_T("+%s"),s.c_str());
00153
00154 while ( !s.StartsWith(_T("INPUT")) ) {
00155 mutChar Type[80], Name[400];
00156 int DevId, BendingRange;
00157
00158 DEBUGLOG2(routing,_T("a%s"),s.c_str());
00159 #if (wxUSE_UNICODE || wxUSE_WCHAR_T)
00160 int test = SSCANF(s.c_str(), _T("%ls \"%l[^\"]\" %d %d"),
00161 Type, Name, &DevId, &BendingRange);
00162 if ( test < 2 )
00163 test = SSCANF(s.c_str(), _T("%ls %ls %d %d"),
00164 Type, Name, &DevId, &BendingRange);
00165
00166 if ( test < 3 ) {
00167 error = true;
00168 }
00169 #else
00170 int test = SSCANF(s.c_str(), _T("%s \"%[^\"]\" %d %d"),
00171 Type, Name, &DevId, &BendingRange);
00172 if ( test < 2 )
00173 test = SSCANF(s.c_str(), _T("%s %s %d %d"),
00174 Type, Name, &DevId, &BendingRange);
00175 if ( test < 3 ) {
00176 error = true;
00177 }
00178 #endif
00179 DEBUGLOG2(routing,_T("%d parameters read: Type = '%s', Name = '%s', devid = %d, bendingrange = %d"),test,Type,Name,DevId,BendingRange);
00180 DEBUGLOG2(routing,_T("Name = '%s'"),(wxString(Name).c_str()));
00181 OutDevice *Out =
00182 OutDevice::CreateDevice(Str2DT(muT(Type)),
00183 Name, DevId);
00184
00185 switch (Str2DT(muT(Type))) {
00186 case DTMidiPort:
00187 if (test < 4)
00188 error = true;
00189 else {
00190 OutMidiPort * dev =
00191 dynamic_cast<OutMidiPort *>(Out);
00192 if (!dev)
00193 UNREACHABLE;
00194 else
00195 dev -> SetBendingRange (BendingRange);
00196 }
00197 break;
00198 case DTMidiFile:
00199 if (test < 4)
00200 error = true;
00201 else {
00202 OutMidiFile * dev =
00203 dynamic_cast<OutMidiFile *>(Out);
00204 if (!dev)
00205 UNREACHABLE;
00206 else
00207 dev -> SetBendingRange (BendingRange);
00208 }
00209 break;
00210 case DTUnknown:
00211 case DTGis:
00212 default:
00213 if (test >= 4) error = true;
00214 }
00215
00216 GETLINE;
00217 DEBUGLOG2(other,_T("+%s"),s.c_str());
00218 }
00219
00220 OutDevice::InitializeIds();
00221
00222 GETLINE;
00223 DEBUGLOG2(routing,_T("+%s"),s.c_str());
00224
00225
00226 while ( 1 ) {
00227
00228 mutChar Type[40], Name[400];
00229
00230 int DevId = -1;
00231 #if (wxUSE_UNICODE || wxUSE_WCHAR_T)
00232 int test = SSCANF(s, _T("%ls \"%l[^\"]\" %d"),
00233 Type, Name, &DevId);
00234 if ( test < 2 )
00235 test = SSCANF(s, _T("%ls %ls %d"),
00236 Type, Name, &DevId);
00237 if ( test < 3 ) {
00238 error = 1;
00239 }
00240
00241 #else
00242 int test = SSCANF(s, _T("%s \"%[^\"]\" %d"),
00243 Type, Name, &DevId);
00244 if ( test < 2 )
00245 test = SSCANF(s, _T("%s %s %d"),
00246 Type, Name, &DevId);
00247
00248 if ( test < 3 ) {
00249 error = 1;
00250 }
00251
00252 #endif
00253 DEBUGLOG2(routing,_T("%d input parameters read: Type = '%s', Name = '%s', DevId = %d"), test, Type, Name, DevId);
00254 InDevice *In = InDevice::CreateDevice(Str2DT(muT(Type)),
00255 Name, DevId);
00256 GETLINE;
00257 DEBUGLOG2(routing,_T("+%s"),s.c_str());
00258
00259
00260 while ( Str2DT(s) == DTUnknown ) {
00261
00262 mutChar Type[40];
00263 int IFrom = 0, ITo = 0, Box = 0, BoxActive = 0,
00264 OutDev = -1, OFrom = -1, OTo = -1, ONoDrum = 1;
00265 #if (wxUSE_UNICODE || wxUSE_WCHAR_T)
00266 test = SSCANF(s.c_str(),
00267 _T("%ls %d %d %d %d %d %d %d %d"),
00268 Type, &IFrom, &ITo, &Box, &BoxActive,
00269 &OutDev, &OFrom, &OTo, &ONoDrum);
00270
00271 if ( test < 9 ) {
00272 error = true;
00273 }
00274
00275 #else
00276 test = SSCANF(s.c_str(),
00277 _T("%s %d %d %d %d %d %d %d %d"),
00278 Type, &IFrom, &ITo, &Box,
00279 &BoxActive, &OutDev, &OFrom, &OTo,
00280 &ONoDrum);
00281
00282 if ( test < 9 ) {
00283 error = true;
00284 }
00285
00286 #endif
00287 DEBUGLOG2(routing,_T("%d parameters read: Type = '%s', IFrom = %d, ITo = %d"),test, Type, IFrom, ITo);
00288 DEBUGLOG2(routing,_T(" Box = %d, BoxActive= %d, OutDev = %d, OFrom = %d, OTo = %d, ONoDrum = %d"), Box, BoxActive, OutDev, OFrom, OTo, ONoDrum);
00289
00290 In->AddRoute(new Route(In,GetOut(OutDev),
00291 Str2RT(Type),
00292 IFrom, ITo, Box,
00293 BoxActive,
00294 OFrom, OTo, ONoDrum));
00295 GETLINE;
00296 DEBUGLOG2(routing,_T("+%s"),s.c_str());
00297 }
00298 }
00299 }
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 void SaveRoutes(mutStringRef config)
00312 {
00314 STUB;
00315
00316 Route::InitializeIds();
00317
00318
00319 config = wxEmptyString;
00320
00321 OutDevice *Out;
00322 InDevice *In;
00323
00324
00325 config << _T("OUTPUT\n");
00326
00327 for ( Out = OutDevice::GetDeviceList(); Out;
00328 Out = Out->GetNext()) {
00329
00330 wxString sName = Out->GetName();
00331
00332 if ( sName.Find(_T(" ")) )
00333 sName.Prepend(_T("\"")) << _T("\"");
00334
00335 switch ( Out->GetType() ) {
00336
00337 case DTUnknown:
00338 config << wxString::Format(_T(" UNKNOWN %s\n"),
00339 sName.c_str());
00340
00341 break;
00342
00343 case DTMidiPort:
00344 {
00345 OutMidiPort * MidiOut =
00346 dynamic_cast <OutMidiPort *>(Out);
00347 if (MidiOut)
00348 config << wxString::Format(_T(" MIDIPORT %s %d %d\n"),
00349 sName.c_str(),
00350 Out->Device::GetId(),
00351 MidiOut->GetBendingRange());
00352 else
00353 UNREACHABLE;
00354 }
00355 break;
00356
00357 case DTMidiFile:
00358 {
00359 OutMidiFile * MidiFile =
00360 dynamic_cast <OutMidiFile *>(Out);
00361 if (MidiFile)
00362 config << wxString::Format(_T(" MIDIFILE %s %d %d\n"),
00363 sName.c_str(),
00364 0,
00365 MidiFile->GetBendingRange());
00366 else
00367 UNREACHABLE;
00368 }
00369 break;
00370
00371 case DTGis:
00372 config << wxString::Format(_T(" GMN %s\n"), sName.c_str());
00373
00374 break;
00375
00376 case DTNotSet:
00377 wxLogWarning(_("Device found, but device type not set."));
00378 }
00379 }
00380
00381 DEBUGLOG2(routing,_T("WriteConfig: %s"), (config.c_str()));
00382
00383
00384 config << _T("INPUT\n");
00385
00386 for ( In = InDevice::GetDeviceList(); In; In = In->GetNext()) {
00387 wxString sName = In->GetName();
00388
00389 if ( sName.Find(_T(" ")) )
00390 sName.Prepend(_T("\"")) << _T("\"");
00391
00392 switch ( In->GetType() ) {
00393
00394 case DTUnknown:
00395 config << wxString::Format(_T(" UNKNOWN %s\n"), sName.c_str());
00396
00397 break;
00398
00399 case DTGis:
00400 config << wxString::Format(_T(" GMN %s\n"), sName.c_str());
00401
00402 break;
00403
00404 case DTMidiPort:
00405 config << wxString::Format(_T(" MIDIPORT %s %d\n"), sName.c_str(), In->GetDevId());
00406
00407 break;
00408
00409 case DTMidiFile:
00410 config << wxString::Format(_T(" MIDIFILE %s\n"), sName.c_str());
00411
00412 break;
00413
00414 case DTNotSet:
00415 wxLogWarning(_("Device found but device type not set."));
00416
00417 break;
00418 }
00419
00420
00421 for (Route *R = In->GetRoutes(); R; R = R->GetNext()) {
00422 Device * dev = R->GetOutDevice();
00423 int OutNr;
00424 OutNr = (dev ? dev->GetId(): -1);
00425 config << wxT(" ") << RTName[R->Type] <<
00426 wxString::Format(_T(" %d %d %d %d %d %d %d %d\n"),
00427 R->IFrom,
00428 R->ITo,
00429 R->Box,
00430 R->Active,
00431 OutNr,
00432 R->OFrom,
00433 R->OTo,
00434 R->ONoDrum ? 1 : 0);
00435 }
00436 }
00437
00438 DEBUGLOG2(routing,_T("WriteRoutes: %s"), (config.c_str()));
00439 }
00440 }
00441