1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4   Rosegarden
5   A sequencer and musical notation editor.
6   Copyright 2000-2021 the Rosegarden development team.
7 
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU General Public License as
10   published by the Free Software Foundation; either version 2 of the
11   License, or (at your option) any later version.  See the file
12   COPYING included with this distribution for more information.
13 */
14 
15 #include "MappedDevice.h"
16 #include "MappedInstrument.h"
17 #include <misc/Strings.h>
18 
19 namespace Rosegarden
20 {
21 
MappedDevice()22 MappedDevice::MappedDevice():
23     std::vector<MappedInstrument*>(),
24     m_id(Device::NO_DEVICE),
25     m_type(Device::Midi),
26     m_name("Unconfigured device"),
27     m_connection(""),
28     m_direction(MidiDevice::Play),
29     m_recording(false)
30 {}
31 
MappedDevice(DeviceId id,Device::DeviceType type,std::string name,std::string connection)32 MappedDevice::MappedDevice(DeviceId id,
33                            Device::DeviceType type,
34                            std::string name,
35                            std::string connection):
36     std::vector<MappedInstrument*>(),
37     m_id(id),
38     m_type(type),
39     m_name(name),
40     m_connection(connection),
41     m_direction(MidiDevice::Play),
42     m_recording(false)
43 {}
44 
~MappedDevice()45 MappedDevice::~MappedDevice()
46 {}
47 
MappedDevice(const MappedDevice & mD)48 MappedDevice::MappedDevice(const MappedDevice &mD):
49         std::vector<MappedInstrument*>()
50 {
51     clear();
52 
53     for (MappedDeviceConstIterator it = mD.begin(); it != mD.end(); ++it)
54         this->push_back(new MappedInstrument(**it));
55 
56     m_id = mD.getId();
57     m_type = mD.getType();
58     m_name = mD.getName();
59     m_connection = mD.getConnection();
60     m_direction = mD.getDirection();
61 }
62 
63 void
clear()64 MappedDevice::clear()
65 {
66     MappedDeviceIterator it;
67 
68     for (it = this->begin(); it != this->end(); ++it)
69         delete (*it);
70 
71     this->erase(this->begin(), this->end());
72 }
73 
74 MappedDevice&
operator +(const MappedDevice & mD)75 MappedDevice::operator+(const MappedDevice &mD)
76 {
77     for (MappedDeviceConstIterator it = mD.begin(); it != mD.end(); ++it)
78         this->push_back(new MappedInstrument(**it));
79 
80     return *this;
81 }
82 
83 MappedDevice&
operator =(const MappedDevice & mD)84 MappedDevice::operator=(const MappedDevice &mD)
85 {
86     if (&mD == this)
87         return * this;
88 
89     clear();
90 
91     for (MappedDeviceConstIterator it = mD.begin(); it != mD.end(); ++it)
92         this->push_back(new MappedInstrument(**it));
93 
94     m_id = mD.getId();
95     m_type = mD.getType();
96     m_name = mD.getName();
97     m_connection = mD.getConnection();
98     m_direction = mD.getDirection();
99 
100     return *this;
101 }
102 
103 
104 QDataStream&
operator >>(QDataStream & dS,MappedDevice * mD)105 operator>>(QDataStream &dS, MappedDevice *mD)
106 {
107     int instruments = 0;
108     dS >> instruments;
109 
110     MappedInstrument mI;
111     while (!dS.atEnd() && instruments) {
112         dS >> mI;
113         mD->push_back(new MappedInstrument(mI));
114         instruments--;
115     }
116 
117     QString name;
118     unsigned int id, dType;
119     QString connection;
120     unsigned int direction;
121     unsigned int recording;
122 
123     dS >> id;
124     dS >> dType;
125     dS >> name;
126     dS >> connection;
127     dS >> direction;
128     dS >> recording;
129     mD->setId(id);
130     mD->setType(Device::DeviceType(dType));
131 	mD->setName( qStrToStrLocal8(name) );
132 	mD->setConnection( qStrToStrLocal8(connection) );
133     mD->setDirection(MidiDevice::DeviceDirection(direction));
134 
135 #ifdef DEBUG_MAPPEDDEVICE
136 
137     if (instruments) {
138         std::cerr << "MappedDevice::operator>> - "
139         << "wrong number of events received" << std::endl;
140     }
141 #endif
142 
143     return dS;
144 }
145 
146 
147 QDataStream&
operator >>(QDataStream & dS,MappedDevice & mD)148 operator>>(QDataStream &dS, MappedDevice &mD)
149 {
150     int instruments;
151     dS >> instruments;
152 
153     MappedInstrument mI;
154 
155     while (!dS.atEnd() && instruments) {
156         dS >> mI;
157         mD.push_back(new MappedInstrument(mI));
158         instruments--;
159     }
160 
161     unsigned int id, dType;
162     QString name;
163     QString connection;
164     unsigned int direction;
165     unsigned int recording;
166 
167     dS >> id;
168     dS >> dType;
169     dS >> name;
170     dS >> connection;
171     dS >> direction;
172     dS >> recording;
173     mD.setId(id);
174     mD.setType(Device::DeviceType(dType));
175 	mD.setName( qStrToStrLocal8(name) );
176 	mD.setConnection( qStrToStrLocal8(connection) );
177     mD.setDirection(MidiDevice::DeviceDirection(direction));
178 
179 #ifdef DEBUG_MAPPEDDEVICE
180 
181     if (instruments) {
182         std::cerr << "MappedDevice::operator>> - "
183         << "wrong number of events received" << std::endl;
184     }
185 #endif
186 
187     return dS;
188 }
189 
190 QDataStream&
operator <<(QDataStream & dS,MappedDevice * mD)191 operator<<(QDataStream &dS, MappedDevice *mD)
192 {
193     dS << (int)mD->size();
194 
195     for (MappedDeviceIterator it = mD->begin(); it != mD->end(); ++it)
196         dS << (*it);
197 
198     dS << (unsigned int)(mD->getId());
199     dS << (int)(mD->getType());
200     dS << QString(mD->getName().c_str());
201     dS << QString(mD->getConnection().c_str());
202     dS << mD->getDirection();
203     dS << (unsigned int)(mD->isRecording());
204 
205 #ifdef DEBUG_MAPPEDDEVICE
206 
207     std::cerr << "MappedDevice::operator>> - wrote \"" << mD->getConnection() << "\""
208     << std::endl;
209 #endif
210 
211     return dS;
212 }
213 
214 QDataStream&
operator <<(QDataStream & dS,const MappedDevice & mD)215 operator<<(QDataStream &dS, const MappedDevice &mD)
216 {
217     dS << (int)mD.size();
218 
219     for (MappedDeviceConstIterator it = mD.begin(); it != mD.end(); ++it)
220         dS << (*it);
221 
222     dS << (unsigned int)(mD.getId());
223     dS << (int)(mD.getType());
224     dS << QString(mD.getName().c_str());
225     dS << QString(mD.getConnection().c_str());
226     dS << mD.getDirection();
227     dS << (unsigned int)(mD.isRecording());
228 
229 #ifdef DEBUG_MAPPEDDEVICE
230 
231     std::cerr << "MappedDevice::operator>> - wrote \"" << mD.getConnection() << "\""
232     << std::endl;
233 #endif
234 
235     return dS;
236 }
237 
238 }
239 
240