1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
#include "Device.h"
#include <string>
#include <sstream>
namespace ciface
{
namespace Core
{
//
// Device :: ~Device
//
// Destructor, delete all inputs/outputs on device destruction
//
Device::~Device()
{
{
// delete inputs
std::vector<Device::Input*>::iterator
i = m_inputs.begin(),
e = m_inputs.end();
for ( ;i!=e; ++i)
delete *i;
}
{
// delete outputs
std::vector<Device::Output*>::iterator
o = m_outputs.begin(),
e = m_outputs.end();
for ( ;o!=e; ++o)
delete *o;
}
}
void Device::AddInput(Device::Input* const i)
{
m_inputs.push_back(i);
}
void Device::AddOutput(Device::Output* const o)
{
m_outputs.push_back(o);
}
Device::Input* Device::FindInput(const std::string &name) const
{
std::vector<Input*>::const_iterator
it = m_inputs.begin(),
itend = m_inputs.end();
for (; it != itend; ++it)
if ((*it)->GetName() == name)
return *it;
return NULL;
}
Device::Output* Device::FindOutput(const std::string &name) const
{
std::vector<Output*>::const_iterator
it = m_outputs.begin(),
itend = m_outputs.end();
for (; it != itend; ++it)
if ((*it)->GetName() == name)
return *it;
return NULL;
}
//
// Device :: ClearInputState
//
// Device classes should override this function
// ControllerInterface will call this when the device returns failure during UpdateInput
// used to try to set all buttons and axes to their default state when user unplugs a gamepad during play
// buttons/axes that were held down at the time of unplugging should be seen as not pressed after unplugging
//
void Device::ClearInputState()
{
// this is going to be called for every UpdateInput call that fails
// kinda slow but, w/e, should only happen when user unplugs a device while playing
}
//
// DeviceQualifier :: ToString
//
// get string from a device qualifier / serialize
//
std::string DeviceQualifier::ToString() const
{
if (source.empty() && (cid < 0) && name.empty())
return "";
std::ostringstream ss;
ss << source << '/';
if ( cid > -1 )
ss << cid;
ss << '/' << name;
return ss.str();
}
//
// DeviceQualifier :: FromString
//
// set a device qualifier from a string / unserialize
//
void DeviceQualifier::FromString(const std::string& str)
{
std::istringstream ss(str);
std::getline(ss, source = "", '/');
// silly
std::getline(ss, name, '/');
std::istringstream(name) >> (cid = -1);
std::getline(ss, name = "");
}
//
// DeviceQualifier :: FromDevice
//
// set a device qualifier from a device
//
void DeviceQualifier::FromDevice(const Device* const dev)
{
name = dev->GetName();
cid = dev->GetId();
source= dev->GetSource();
}
bool DeviceQualifier::operator==(const Device* const dev) const
{
if (dev->GetId() == cid)
if (dev->GetName() == name)
if (dev->GetSource() == source)
return true;
return false;
}
bool DeviceQualifier::operator==(const DeviceQualifier& devq) const
{
if (cid == devq.cid)
if (name == devq.name)
if (source == devq.source)
return true;
return false;
}
Device* DeviceContainer::FindDevice(const DeviceQualifier& devq) const
{
std::vector<Device*>::const_iterator
di = m_devices.begin(),
de = m_devices.end();
for (; di!=de; ++di)
if (devq == *di)
return *di;
return NULL;
}
Device::Input* DeviceContainer::FindInput(const std::string& name, const Device* def_dev) const
{
if (def_dev)
{
Device::Input* const inp = def_dev->FindInput(name);
if (inp)
return inp;
}
std::vector<Device*>::const_iterator
di = m_devices.begin(),
de = m_devices.end();
for (; di != de; ++di)
{
Device::Input* const i = (*di)->FindInput(name);
if (i)
return i;
}
return NULL;
}
Device::Output* DeviceContainer::FindOutput(const std::string& name, const Device* def_dev) const
{
return def_dev->FindOutput(name);
}
}
}
|