![]() |
Mixxx
|
00001 00008 /*************************************************************************** 00009 * * 00010 * This program is free software; you can redistribute it and/or modify * 00011 * it under the terms of the GNU General Public License as published by * 00012 * the Free Software Foundation; either version 2 of the License, or * 00013 * (at your option) any later version. * 00014 * * 00015 ***************************************************************************/ 00016 00017 #include <QtCore> 00018 #include "mididevice.h" 00019 #include "dlgprefmidibindings.h" 00020 #include "mididevicemanager.h" 00021 #include "midiledhandler.h" 00022 #include "../mixxxcontrol.h" 00023 #include "midimapping.h" 00024 00025 #define DEVICE_CONFIG_PATH BINDINGS_PATH.append("MixxxMIDIDevices") 00026 00027 MidiDeviceManager::MidiDeviceManager(ConfigObject<ConfigValue> * pConfig) : QObject() 00028 { 00029 m_pConfig = pConfig; 00030 00031 m_pPMEnumerator = new PortMidiEnumerator(); 00032 #ifdef __HSS1394__ 00033 m_pHSSEnumerator = new Hss1394Enumerator(); 00034 #endif 00035 } 00036 00037 MidiDeviceManager::~MidiDeviceManager() 00038 { 00039 //Delete enumerators and they'll delete their Devices 00040 delete m_pPMEnumerator; 00041 #ifdef __HSS1394__ 00042 delete m_pHSSEnumerator; 00043 #endif 00044 } 00045 00046 void MidiDeviceManager::saveMappings(bool onlyActive) { 00047 // Write out MIDI mappings for currently connected devices 00048 QList<MidiDevice*> deviceList = getDeviceList(false, true); 00049 QListIterator<MidiDevice*> it(deviceList); 00050 00051 QList<QString> filenames; 00052 00053 while (it.hasNext()) 00054 { 00055 MidiDevice *cur= it.next(); 00056 if (onlyActive && !cur->isOpen()) continue; 00057 MidiMapping *mapping = cur->getMidiMapping(); 00058 QString name = cur->getName(); 00059 00060 QString ofilename = name.right(name.size()-name.indexOf(" ")-1).replace(" ", "_"); 00061 00062 QString filename = ofilename; 00063 00064 int i=1; 00065 while (filenames.contains(filename)) { 00066 i++; 00067 filename = QString("%1--%2").arg(ofilename).arg(i); 00068 } 00069 00070 filenames.append(filename); 00071 mapping->savePreset(BINDINGS_PATH.append(filename + MIDI_MAPPING_EXTENSION)); 00072 } 00073 } 00074 00075 QList<MidiDevice*> MidiDeviceManager::getDeviceList(bool bOutputDevices, bool bInputDevices) 00076 { 00077 qDebug() << "MidiDeviceManager::getDeviceList"; 00078 bool bMatchedCriteria = false; //Whether or not the current device matched the filtering criteria 00079 00080 if (m_devices.empty()) { 00081 m_devices = m_pPMEnumerator->queryDevices(); 00082 #ifdef __HSS1394__ 00083 m_devices.append(m_pHSSEnumerator->queryDevices()); 00084 #endif 00085 } 00086 00087 //Create a list of MIDI devices filtered to match the given input/output options. 00088 QList<MidiDevice*> filteredDeviceList; 00089 QListIterator<MidiDevice*> dev_it(m_devices); 00090 while (dev_it.hasNext()) 00091 { 00092 bMatchedCriteria = false; //Reset this for the next device. 00093 MidiDevice *device = dev_it.next(); 00094 00095 if ((bOutputDevices == device->isOutputDevice()) || 00096 (bInputDevices == device->isInputDevice())) { 00097 bMatchedCriteria = true; 00098 } 00099 00100 if (bMatchedCriteria) 00101 filteredDeviceList.push_back(device); 00102 } 00103 return filteredDeviceList; 00104 } 00105 00106 //void MidiDeviceManager::closeDevices() 00107 //{ 00108 // QListIterator<MidiDevice*> dev_it(m_devices); 00109 // while (dev_it.hasNext()) 00110 // { 00111 // qDebug() << "Closing MIDI device" << dev_it.peekNext()->getName(); 00112 // dev_it.next()->close(); 00113 // } 00114 //} 00115 00117 int MidiDeviceManager::setupDevices() 00118 { 00119 QList<MidiDevice*> deviceList = getDeviceList(false, true); 00120 QListIterator<MidiDevice*> it(deviceList); 00121 00122 qDebug() << "MidiDeviceManager: Setting up devices"; 00123 00124 QList<QString> filenames; 00125 00126 while (it.hasNext()) 00127 { 00128 MidiDevice *cur= it.next(); 00129 MidiMapping *mapping = cur->getMidiMapping(); 00130 QString name = cur->getName(); 00131 mapping->setName(name); 00132 00133 cur->close(); 00134 00135 QString ofilename = name.right(name.size()-name.indexOf(" ")-1).replace(" ", "_"); 00136 00137 QString filename = ofilename; 00138 00139 int i=1; 00140 while (filenames.contains(filename)) { 00141 i++; 00142 filename = QString("%1--%2").arg(ofilename).arg(i); 00143 } 00144 00145 filenames.append(filename); 00146 mapping->loadPreset(BINDINGS_PATH.append(filename + MIDI_MAPPING_EXTENSION),true); 00147 00148 if ( m_pConfig->getValueString(ConfigKey("[Midi]", name.replace(" ", "_"))) != "1" ) 00149 continue; 00150 00151 qDebug() << "Opening Device:" << name; 00152 00153 cur->open(); 00154 // Prevents a deadlock if the device is sending data when it's initialized 00155 cur->setReceiveInhibit(true); 00156 mapping->applyPreset(); 00157 cur->setReceiveInhibit(false); 00158 } 00159 00160 return 0; 00161 } 00162 00163 00165 QStringList MidiDeviceManager::getConfigList(QString path) 00166 { 00167 // Make sure list is empty 00168 QStringList configs; 00169 configs.clear(); 00170 00171 // Get list of available midi configurations 00172 QDir dir(path); 00173 dir.setFilter(QDir::Files); 00174 dir.setNameFilters(QStringList() << "*.midi.xml" << "*.MIDI.XML"); 00175 00176 //const QFileInfoList *list = dir.entryInfoList(); 00177 //if (dir.entryInfoList().empty()) 00178 { 00179 QListIterator<QFileInfo> it(dir.entryInfoList()); 00180 QFileInfo fi; // pointer for traversing 00181 while (it.hasNext()) 00182 { 00183 fi = it.next(); 00184 configs.append(fi.fileName()); 00185 } 00186 } 00187 00188 return configs; 00189 } 00190 00191 00192 void MidiDeviceManager::associateInputAndOutputDevices(MidiDevice* inputDevice, QString outputDeviceName) 00193 { 00194 //TODO: This function needs to be updated to work with our "aggregate" input/ouput MidiDevice class 00195 // or just simply removed all together. I just sent out a mixxx-devel email with more history 00196 // on this, check the archive if you need more info. -- Albert Nov 9/09 (1.8 CRUNCH TIME!) 00197 // 00198 00199 /* 00200 //Find the output MidiDevice object that corresponds to outputDeviceName. 00201 QListIterator<MidiDevice*> dev_it(m_devices); 00202 MidiDevice* outputDevice = NULL; 00203 while (dev_it.hasNext()) { 00204 outputDevice = dev_it.next(); 00205 if (outputDevice->getName() == outputDeviceName) { 00206 qDebug() << "associating input dev" << inputDevice->getName() << "with" << outputDeviceName; 00207 break; 00208 } 00209 } 00210 00211 if (outputDevice == NULL) //No output device matched outputDeviceName... 00212 return; 00213 00214 //Tell the input device that it's corresponding output device is... outputDevice. 00215 inputDevice->setOutputDevice(outputDevice); 00216 */ 00217 }