Device.cpp
gehe zur Dokumentation dieser Datei
00001 // -*- C++ -*-
00002 
00036 // ------------------------------------------------------------------
00037 // Mutabor 3, 1998, R.Krauße
00038 // Devices Basisklassen
00039 // ------------------------------------------------------------------
00040 
00041 #include "Device.h"
00042 #include "DevMidi.h"
00043 #include "DevMidF.h"
00044 #include "DevGIS.h"
00045 
00046 
00047 WATCHEDPTR(OutDevice,routing,OutDevice) OutDevice::deviceList(NULL,_T("OutDevice::deviceList"));
00048 
00049 
00050 char InDevChanged = 0;
00051 
00052 // CurrentTime ------------------------------------------------------
00053 
00054 //DWORD CurrentTime;
00055 CurrentTimer CurrentTime;
00056 //UINT CurrentTimeId;
00057 
00058 /* Obsoleted by CurrentTime Class
00059 // CallBack Funktion
00060 void CALLBACK _export CurrentTimeFunc(UINT wTimerID, UINT wMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
00061 {
00062   CurrentTime++;
00063 }
00064 */
00065 // set timer
00066 void StartCurrentTime()
00067 {
00068         CurrentTime.Start(0);
00069         //  CurrentTimeId = timeSetEvent(2, 1, CurrentTimeFunc, 0, TIME_PERIODIC);
00070 }
00071 
00072 // reset timer
00073 void StopCurrentTime()
00074 {
00075         // not needed any more
00076         //CurrentTime.Stop();
00077 }
00078 
00079 // OutDevice
00080 OutDevice * OutDevice::CreateDevice (DevType type)
00081 {
00082         switch (type) {
00083         case DTMidiPort:
00084                 return new OutMidiPort();
00085                 break;
00086         case DTMidiFile:
00087                 return new OutMidiFile();
00088                 break; 
00089         case DTGis:
00090                 return new OutGis();
00091                 break;
00092         case DTNotSet:
00093         case DTUnknown:
00094         default:
00095                 return NULL;
00096         }
00097 }
00098 
00099 OutDevice * OutDevice::CreateDevice(DevType type, const mutString & name, int id)
00100 {
00101         switch (type) {
00102                 case DTMidiPort:
00103                         return new OutMidiPort(name, id);
00104                         break;
00105                 case DTMidiFile:
00106                         return new OutMidiFile(name, id);
00107                         break; 
00108                 case DTGis:
00109                         return new OutGis(name, id);
00110                         break;
00111                 case DTNotSet:
00112                 case DTUnknown:
00113                 default:
00114                         return NULL;
00115         }
00116 }
00117 
00118 void OutDevice::InitializeIds()
00119 {
00120         OutDevice * out = GetDeviceList();
00121         int i = 0;
00122         while (out) {
00123                 out->Device::SetId(i++);
00124                 out = out->GetNext();
00125         }
00126 }
00127 
00128 void OutDevice::SaveDevices(tree_storage & config)
00129 {
00130         config.toLeaf(_T("OutDevices"));
00131         
00132         for (OutDevice * out = GetDeviceList(); out; out = out->GetNext()) {
00133                 config.toLeaf(_T("Device"),static_cast<Device *>(out)->GetId());
00134                 config.Write(_T("Type"),out->GetType());
00135                 config.Write(_T("Type Name"),out->GetTypeName());
00136                 out -> Save (config);
00137                 config.toParent();
00138         }
00139         
00140         config.toParent();
00141 }
00142 
00143 void OutDevice::LoadDevices(tree_storage & config)
00144 {
00145         config.toLeaf(_T("OutDevices"));
00146         
00147         int i = config.toFirstLeaf(_T("Device"));
00148         while (i != wxNOT_FOUND) {
00149                 DEBUGLOGTYPE(config,OutDevice,_T("Loading output device with id %d"),i);
00150                 DevType type = (DevType) config.Read(_T("Type"), DTMidiPort);
00151                 OutDevice * out = OutDevice::CreateDevice(type);
00152                 if (!out) continue;
00153                 out -> Device::SetId(i);
00154                 wxString name = config.Read(_T("Type Name"),_T("Midi output device"));
00155                 wxASSERT(name == out->GetTypeName());
00156                 out -> Load(config);
00157                 i = config.toNextLeaf(_T("Device"));
00158         }
00159         
00160         config.toParent(2);
00161 }
00162 
00163 OutDevice * OutDevice::GetDevice(int id)
00164 {
00165         OutDevice * out = GetDeviceList();
00166         while (out && out->Device::GetId() != id) 
00167                 out = out->Next;
00168         return out;
00169 }
00170 
00171 void OutDevice::AppendToDeviceList (OutDevice * dev) 
00172 {
00173         if (!deviceList) {
00174                 deviceList = dev; 
00175                 return;
00176         }
00177         
00178         OutDevice * d = GetDeviceList() ;
00179         OutDevice * d2;
00180         while ((d2 = d->GetNext())) d = d2;
00181         d->SetNext(dev);
00182 }
00183 
00184 void OutDevice::RemoveFromDeviceList (OutDevice * dev) 
00185 {
00186         if (!deviceList) return;
00187         
00188         if (deviceList == dev) {
00189                 deviceList = dev -> GetNext(); 
00190                 dev -> SetNext(NULL);
00191                 return;
00192         }
00193         
00194         OutDevice * d = deviceList ;
00195         while (d -> GetNext() != dev && d -> GetNext()) d = d -> GetNext();
00196         if (d -> GetNext()) {
00197                 d -> SetNext(dev -> GetNext());
00198                 dev -> SetNext(NULL);
00199         }
00200 }
00201 
00202 void OutDevice::TruncateDeviceList (OutDevice * dev) 
00203 {
00204         if (!deviceList) return;
00205         
00206         if (deviceList == dev) {
00207                 deviceList = NULL; 
00208                 return;
00209         }
00210         
00211         OutDevice * d = deviceList ;
00212         while (d->GetNext() && d->GetNext() != dev) {
00213                 d = d->GetNext();
00214         }
00215         if (d->GetNext()) {
00216                 d -> SetNext(NULL);
00217         }
00218 }
00219 
00220 OutDevice * OutDevice::GetInDeviceList(int devid) 
00221 {       
00222         OutDevice * dev = deviceList;
00223         while (dev && dev->GetDevId() != devid) dev = dev->GetNext();
00224         return dev;
00225 }
00226 
00227 
00228 // InDevice ---------------------------------------------------------
00229 
00230 WATCHEDPTR(InDevice,routing,InDevice) InDevice::deviceList(NULL, _T("InDevice::deviceList"),NULL);
00231 
00232 InDevice * InDevice::CreateDevice (DevType type)
00233 {
00234         switch (type) {
00235         case DTMidiPort:
00236                 return new InMidiPort();
00237                 break;
00238         case DTMidiFile:
00239                 return new InMidiFile();
00240                 break; 
00241         case DTGis:
00242                 return new InGis();
00243                 break;
00244         case DTNotSet:
00245         case DTUnknown:
00246         default:
00247                 return NULL;
00248         }
00249 }
00250 InDevice * InDevice::CreateDevice(DevType type, const mutString & name, int id)
00251 {
00252         DEBUGLOGTYPE(routing,InDevice,_T("Creating device `%s´ (Type = %d, id = %d)"),name.c_str(), type,id);
00253         switch (type) {
00254                 case DTMidiPort:
00255                         return new InMidiPort(name, id);
00256                         break;
00257                 case DTMidiFile:
00258                         return new InMidiFile(name, id);
00259                         break; 
00260                 case DTGis:
00261                         return new InGis(name, id);
00262                         break;
00263                 case DTNotSet:
00264                 case DTUnknown:
00265                 default:
00266                         return NULL;
00267         }
00268 }
00269 
00270 
00271 Route* InDevice::GetMoveRoutes()
00272 {
00273         Route *R = GetRoutes();
00274         SetRoute(NULL);
00275         return R;
00276 };
00277 
00278 Route *InDevice::GetRoute(int nr)   // Nummern beginnen mit 0
00279 {
00280 
00281         for (Route *R = Routes; R; R = R->GetNext())
00282         {
00283                 if ( !nr ) return R;
00284 
00285                 nr--;
00286         }
00287 
00288         return 0;
00289 }
00290 
00291 // Anzahl der Routen
00292 int InDevice::nRoutes()
00293 {
00294         int n = 0;
00295 
00296         for (Route *R = Routes; R; R = R->GetNext())
00297                 n++;
00298 
00299         return n;
00300 }
00301 
00302 // Neue Route am Ende anhängen (damit RTelse O.K. geht)
00303 void InDevice::AddRoute(Route *route)
00304 {
00305 
00306         if (!Routes) {
00307                 Routes = route;
00308                 route->SetInDevice(this);
00309                 return;
00310         }
00311         
00312         Route *R = Routes;
00313         wxASSERT(R->GetInDevice() == this);
00314         Route * next;
00315         while ( (next = R -> GetNext()) )
00316                 R = next;
00317 
00318         R -> SetNext(route);
00319 }
00320 
00321 void InDevice::Quite()
00322 {
00323         for (Route *R = Routes; R; R = R->GetNext())
00324                 if ( R->GetOutDevice() )
00325                         R->GetOutDevice()->Quite(R);
00326 }
00327 
00328 Route * InDevice::ReplaceDevice (InDevice * dev) 
00329 {
00330         // we don't replace the position in the device list since it is
00331         // already appended and we don't rely on the order in this list.
00332 #ifdef DEBUG
00333         if (debugFlags::flags[debugFlags::routing]) {
00334                 InDevice * test = GetDeviceList();
00335                 while (test && test != dev) test = test->GetNext();
00336                 wxASSERT(test);
00337         }
00338 #endif
00339         SetRoute(dev->GetRoutes());
00340         dev -> SetRoute(NULL);
00341         return GetRoutes();
00342 }
00343 
00344 void InDevice::InitializeIds()
00345 {
00346         InDevice * in = GetDeviceList();
00347         int i = 0;
00348         while (in) {
00349                 int id = i++;
00350                 in->Device::SetId(id);
00351                 for (Route * route = in->GetRoutes(); route; route = route->GetNext())
00352                         route -> SetInputId(id);
00353                 in = in->GetNext();
00354         }
00355 }
00356 
00357 void InDevice::SaveDevices(tree_storage & config)
00358 {
00359         config.toLeaf(_T("InDevices"));
00360         
00361         for (InDevice * in = GetDeviceList(); in; in = in->GetNext()) {
00362                 config.toLeaf(_T("Device"),in->Device::GetId());
00363                 config.Write(_T("Type"),in->GetType());
00364                 config.Write(_T("Type Name"),in->GetTypeName());
00365                 in -> Save (config);
00366                 config.toParent();
00367         }
00368         
00369         config.toParent();
00370 }
00371 
00372 void InDevice::LoadDevices(tree_storage & config)
00373 {
00374         config.toLeaf(_T("InDevices"));
00375         
00376         int i = config.toFirstLeaf(_T("Device"));
00377         while (i != wxNOT_FOUND) {
00378                 DevType type = (DevType) config.Read(_T("Type"), DTMidiPort);
00379                 InDevice * in = InDevice::CreateDevice(type);
00380                 in -> Device::SetId(i);
00381                 wxString name = config.Read(_T("Type Name"),_T("Midi input device"));
00382                 wxASSERT(name == in -> GetTypeName());
00383                 in -> Load(config);
00384                 i = config.toNextLeaf(_T("Device"));
00385         }
00386         
00387         config.toParent(2);
00388 }
00389 
00390 InDevice * InDevice::GetDevice(int id)
00391 {
00392         InDevice * in = GetDeviceList();
00393         while (in && in->Device::GetId() != id) 
00394                 in = in->Next;
00395         return in;
00396 }
00397 
00398 
00399 void InDevice::AppendToDeviceList (InDevice * dev) 
00400 {
00401         if (!GetDeviceList()) {
00402                 deviceList = dev; 
00403                 return;
00404         }
00405 
00406         InDevice * d = deviceList ;
00407         InDevice * next;
00408         while ( (next = d->GetNext()) ) d = next;
00409         d->SetNext(dev);
00410 }
00411 
00412 void InDevice::RemoveFromDeviceList (InDevice * dev) 
00413 {
00414         if (!deviceList) return;
00415         
00416         if (deviceList == dev) {
00417                 deviceList = dev -> GetNext(); 
00418                 dev -> SetNext(NULL);
00419                 return;
00420         }
00421 
00422         InDevice * d = deviceList, *next ;
00423         while ((next = d->GetNext()) != dev && next) d = next;
00424         if (next) {
00425                 d->SetNext(dev -> GetNext());
00426                 dev -> SetNext(NULL);
00427         }
00428 }
00429 
00430 void InDevice::TruncateDeviceList (InDevice * dev) 
00431 {
00432         if (!deviceList) return;
00433         
00434         if (deviceList == dev) {
00435                 deviceList = NULL; 
00436                 return;
00437         }
00438 
00439         InDevice * d = deviceList;
00440         InDevice * next = d->GetNext();
00441         while (next && next != dev) {
00442                 d = next;
00443                 next = d -> GetNext();
00444         }
00445         if (next) {
00446                 d -> SetNext(NULL);
00447         }
00448 }
00449 
00450 
00451 // functions --------------------------------------------------------
00452 
00453 void OutNotesCorrect(int box)
00454 {
00455         for (OutDevice *Out = OutDevice::GetDeviceList(); Out; Out = Out->GetNext())
00456                 Out->NotesCorrect(box);
00457 }
00458 
00459 bool OutOpen()
00460 {
00461         for (OutDevice *Out = OutDevice::GetDeviceList(); Out; Out = Out->GetNext())
00462                 if ( !Out->Open() ) {
00463                         for (OutDevice *Out1 = OutDevice::GetDeviceList(); Out1 && Out1 != Out; Out1 = Out1->GetNext())
00464                                 Out1->Close();
00465 
00466                         return false;
00467                 }
00468 
00469         return true;
00470 }
00471 
00472 void OutClose()
00473 {
00474         for (OutDevice *Out = OutDevice::GetDeviceList(); Out; Out = Out->GetNext())
00475                 Out->Close();
00476 }
00477 
00478 void OutAddTime(frac time)
00479 {
00480         for (OutDevice *Out = OutDevice::GetDeviceList(); Out; Out = Out->GetNext())
00481                 Out->AddTime(time);
00482 }
00483 
00484 bool InOpen()
00485 {
00486         DEBUGLOGBASE(other,"",_T(""));
00487 
00488         for (InDevice *In = InDevice::GetDeviceList(); In; In = In->GetNext())
00489                 if ( !In->Open() ) {
00490                         for (InDevice *In1 = InDevice::GetDeviceList(); In1 && In1 != In; 
00491                              In1 = In1->GetNext())
00492                                 In1->Close();
00493 
00494                         DEBUGLOGBASE(other,"",_T("Opening failed"));
00495 
00496                         return false;
00497                 }
00498 
00499         return true;
00500 }
00501 
00502 void InClose()
00503 {
00504         for (InDevice *In = InDevice::GetDeviceList(); In; In = In->GetNext())
00505                 In->Close();
00506 }
00507 
00508 bool NeedsRealTime()
00509 {
00510         for (OutDevice *Out = OutDevice::GetDeviceList(); Out; Out = Out->GetNext())
00511                 if ( Out->NeedsRealTime() )
00512                         return true;
00513 
00514         for (InDevice *In = InDevice::GetDeviceList(); In; In = In->GetNext())
00515                 if ( In->NeedsRealTime() )
00516                         return true;
00517 
00518         return false;
00519 }
00520 
00521 
00522 /*
00523 frac InReadOn(frac time)
00524 {
00525   frac z = frac(-1, 1);
00526   for (InDevice *In = InDevices; In; In = In->Next)
00527   {
00528           frac z1 = In->ReadOn(time);
00529           if ( z == frac(-1, 1) || z1 < z )
00530                   z = z1;
00531   }
00532   return z;
00533 }
00534 */
00535 
00536 void MidiOut(int box, DWORD data, char n)
00537 {
00538         for (InDevice *In = InDevice::GetDeviceList(); In; In = In->GetNext()) {
00539                 for (Route *R = In->GetRoutes(); R; R = R->GetNext()) {
00540                         OutDevice * out;
00541                         if ( R->Box == box && (out = R->GetOutDevice())) {
00542                                 out -> MidiOut(data, n);
00543                         }
00544                 }
00545         }
00546 }
00547 
00548 void NotesCorrect(int box)
00549 {
00550         for (OutDevice *Out = OutDevice::GetDeviceList(); Out; Out = Out->GetNext())
00551                 Out->NotesCorrect(box);
00552 }
00553 
00554 int GetChannel(int box, int taste)
00555 {
00556         for (InDevice *In = InDevice::GetDeviceList(); In; In = In->GetNext()) {
00557                 for (Route *R = In->GetRoutes(); R; R = R->GetNext()) {
00558                         OutDevice * out;
00559                         if ( R->Box == box && (out = R->GetOutDevice())) {
00560                                 int c = out->GetChannel(taste);
00561 
00562                                 if ( c != -1 )
00563                                         return c;
00564                         }
00565                 }
00566         }
00567         return -1;
00568 }
00569 
00571 

Erzeugt am Sun Aug 21 2011 10:51:52 für Mutabor von doxygen 1.7.4