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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#include <algorithm>
#include <map>
#include <sstream>
#include "Common/StringUtil.h"
#include "InputCommon/ControllerInterface/SDL/SDL.h"
#ifdef _WIN32
#pragma comment(lib, "SDL2.lib")
#endif
namespace ciface
{
namespace SDL
{
static std::string GetJoystickName(int index)
{
#if SDL_VERSION_ATLEAST(2, 0, 0)
return SDL_JoystickNameForIndex(index);
#else
return SDL_JoystickName(index);
#endif
}
void Init( std::vector<Core::Device*>& devices )
{
// this is used to number the joysticks
// multiple joysticks with the same name shall get unique ids starting at 0
std::map<std::string, int> name_counts;
if (SDL_Init( SDL_INIT_FLAGS ) >= 0)
{
// joysticks
for (int i = 0; i < SDL_NumJoysticks(); ++i)
{
SDL_Joystick* dev = SDL_JoystickOpen(i);
if (dev)
{
Joystick* js = new Joystick(dev, i, name_counts[GetJoystickName(i)]++);
// only add if it has some inputs/outputs
if (js->Inputs().size() || js->Outputs().size())
devices.push_back( js );
else
delete js;
}
}
}
}
Joystick::Joystick(SDL_Joystick* const joystick, const int sdl_index, const unsigned int index)
: m_joystick(joystick)
, m_sdl_index(sdl_index)
, m_index(index)
{
// really bad HACKS:
// to not use SDL for an XInput device
// too many people on the forums pick the SDL device and ask:
// "why don't my 360 gamepad triggers/rumble work correctly"
#ifdef _WIN32
// checking the name is probably good (and hacky) enough
// but I'll double check with the num of buttons/axes
std::string lcasename = GetName();
std::transform(lcasename.begin(), lcasename.end(), lcasename.begin(), tolower);
if ((std::string::npos != lcasename.find("xbox 360")) &&
(10 == SDL_JoystickNumButtons(joystick)) &&
(5 == SDL_JoystickNumAxes(joystick)) &&
(1 == SDL_JoystickNumHats(joystick)) &&
(0 == SDL_JoystickNumBalls(joystick)))
{
// this device won't be used
return;
}
#endif
if (SDL_JoystickNumButtons(joystick) > 255 ||
SDL_JoystickNumAxes(joystick) > 255 ||
SDL_JoystickNumHats(joystick) > 255 ||
SDL_JoystickNumBalls(joystick) > 255)
{
// This device is invalid, don't use it
// Some crazy devices(HP webcam 2100) end up as HID devices
// SDL tries parsing these as joysticks
return;
}
// get buttons
for (u8 i = 0; i != SDL_JoystickNumButtons(m_joystick); ++i)
AddInput(new Button(i, m_joystick));
// get hats
for (u8 i = 0; i != SDL_JoystickNumHats(m_joystick); ++i)
{
// each hat gets 4 input instances associated with it, (up down left right)
for (u8 d = 0; d != 4; ++d)
AddInput(new Hat(i, m_joystick, d));
}
// get axes
for (u8 i = 0; i != SDL_JoystickNumAxes(m_joystick); ++i)
{
// each axis gets a negative and a positive input instance associated with it
AddAnalogInputs(new Axis(i, m_joystick, -32768),
new Axis(i, m_joystick, 32767));
}
#ifdef USE_SDL_HAPTIC
// try to get supported ff effects
m_haptic = SDL_HapticOpenFromJoystick( m_joystick );
if (m_haptic)
{
//SDL_HapticSetGain( m_haptic, 1000 );
//SDL_HapticSetAutocenter( m_haptic, 0 );
const unsigned int supported_effects = SDL_HapticQuery( m_haptic );
// constant effect
if (supported_effects & SDL_HAPTIC_CONSTANT)
{
m_state_out.push_back(EffectIDState());
AddOutput(new ConstantEffect(m_state_out.back()));
}
// ramp effect
if (supported_effects & SDL_HAPTIC_RAMP)
{
m_state_out.push_back(EffectIDState());
AddOutput(new RampEffect(m_state_out.back()));
}
// sine effect
if (supported_effects & SDL_HAPTIC_SINE)
{
m_state_out.push_back(EffectIDState());
AddOutput(new SineEffect(m_state_out.back()));
}
#ifdef SDL_HAPTIC_SQUARE
// square effect
if (supported_effects & SDL_HAPTIC_SQUARE)
{
m_state_out.push_back(EffectIDState());
AddOutput(new SquareEffect(m_state_out.back()));
}
#endif // defined(SDL_HAPTIC_SQUARE)
// triangle effect
if (supported_effects & SDL_HAPTIC_TRIANGLE)
{
m_state_out.push_back(EffectIDState());
AddOutput(new TriangleEffect(m_state_out.back()));
}
}
#endif
}
Joystick::~Joystick()
{
#ifdef USE_SDL_HAPTIC
if (m_haptic)
{
// stop/destroy all effects
SDL_HapticStopAll(m_haptic);
for (auto &i : m_state_out)
{
if (i.id != -1)
{
SDL_HapticDestroyEffect(m_haptic, i.id);
}
}
// close haptic first
SDL_HapticClose(m_haptic);
}
#endif
// close joystick
SDL_JoystickClose(m_joystick);
}
#ifdef USE_SDL_HAPTIC
std::string Joystick::ConstantEffect::GetName() const
{
return "Constant";
}
std::string Joystick::RampEffect::GetName() const
{
return "Ramp";
}
std::string Joystick::SineEffect::GetName() const
{
return "Sine";
}
#ifdef SDL_HAPTIC_SQUARE
std::string Joystick::SquareEffect::GetName() const
{
return "Square";
}
#endif // defined(SDL_HAPTIC_SQUARE)
std::string Joystick::TriangleEffect::GetName() const
{
return "Triangle";
}
void Joystick::ConstantEffect::SetState(ControlState state)
{
if (state)
{
m_effect.effect.type = SDL_HAPTIC_CONSTANT;
m_effect.effect.constant.length = SDL_HAPTIC_INFINITY;
}
else
{
m_effect.effect.type = 0;
}
const Sint16 old = m_effect.effect.constant.level;
m_effect.effect.constant.level = (Sint16)(state * 0x7FFF);
if (old != m_effect.effect.constant.level)
m_effect.changed = true;
}
void Joystick::RampEffect::SetState(ControlState state)
{
if (state)
{
m_effect.effect.type = SDL_HAPTIC_RAMP;
m_effect.effect.ramp.length = SDL_HAPTIC_INFINITY;
}
else
{
m_effect.effect.type = 0;
}
const Sint16 old = m_effect.effect.ramp.start;
m_effect.effect.ramp.start = (Sint16)(state * 0x7FFF);
if (old != m_effect.effect.ramp.start)
m_effect.changed = true;
}
void Joystick::SineEffect::SetState(ControlState state)
{
if (state)
{
m_effect.effect.type = SDL_HAPTIC_SINE;
m_effect.effect.periodic.length = 250;
}
else
{
m_effect.effect.type = 0;
}
const Sint16 old = m_effect.effect.periodic.magnitude;
m_effect.effect.periodic.period = 5;
m_effect.effect.periodic.magnitude = (Sint16)(state * 0x5000);
m_effect.effect.periodic.attack_length = 0;
m_effect.effect.periodic.fade_length = 500;
if (old != m_effect.effect.periodic.magnitude)
m_effect.changed = true;
}
#ifdef SDL_HAPTIC_SQUARE
void Joystick::SquareEffect::SetState(ControlState state)
{
if (state)
{
m_effect.effect.type = SDL_HAPTIC_SQUARE;
m_effect.effect.periodic.length = 250;
}
else
{
m_effect.effect.type = 0;
}
const Sint16 old = m_effect.effect.periodic.magnitude;
m_effect.effect.periodic.period = 5;
m_effect.effect.periodic.magnitude = state * 0x5000;
m_effect.effect.periodic.attack_length = 0;
m_effect.effect.periodic.fade_length = 100;
if (old != m_effect.effect.periodic.magnitude)
m_effect.changed = true;
}
#endif // defined(SDL_HAPTIC_SQUARE)
void Joystick::TriangleEffect::SetState(ControlState state)
{
if (state)
{
m_effect.effect.type = SDL_HAPTIC_TRIANGLE;
m_effect.effect.periodic.length = 250;
}
else
{
m_effect.effect.type = 0;
}
const Sint16 old = m_effect.effect.periodic.magnitude;
m_effect.effect.periodic.period = 5;
m_effect.effect.periodic.magnitude = (Sint16)(state * 0x5000);
m_effect.effect.periodic.attack_length = 0;
m_effect.effect.periodic.fade_length = 100;
if (old != m_effect.effect.periodic.magnitude)
m_effect.changed = true;
}
#endif
bool Joystick::UpdateInput()
{
// each joystick is doin this, o well
SDL_JoystickUpdate();
return true;
}
bool Joystick::UpdateOutput()
{
#ifdef USE_SDL_HAPTIC
for (auto &i : m_state_out)
{
if (i.changed) // if SetState was called on this output
{
if (-1 == i.id) // effect isn't currently uploaded
{
if (i.effect.type) // if outputstate is >0 this would be true
{
if ((i.id = SDL_HapticNewEffect(m_haptic, &i.effect)) > -1) // upload the effect
{
SDL_HapticRunEffect(m_haptic, i.id, 1); // run the effect
}
}
}
else // effect is already uploaded
{
if (i.effect.type) // if ouputstate >0
{
SDL_HapticUpdateEffect(m_haptic, i.id, &i.effect); // update the effect
}
else
{
SDL_HapticStopEffect(m_haptic, i.id); // else, stop and remove the effect
SDL_HapticDestroyEffect(m_haptic, i.id);
i.id = -1; // mark it as not uploaded
}
}
i.changed = false;
}
}
#endif
return true;
}
std::string Joystick::GetName() const
{
return StripSpaces(GetJoystickName(m_sdl_index));
}
std::string Joystick::GetSource() const
{
return "SDL";
}
int Joystick::GetId() const
{
return m_index;
}
std::string Joystick::Button::GetName() const
{
std::ostringstream ss;
ss << "Button " << (int)m_index;
return ss.str();
}
std::string Joystick::Axis::GetName() const
{
std::ostringstream ss;
ss << "Axis " << (int)m_index << (m_range<0 ? '-' : '+');
return ss.str();
}
std::string Joystick::Hat::GetName() const
{
static char tmpstr[] = "Hat . .";
// I don't think more than 10 hats are supported
tmpstr[4] = (char)('0' + m_index);
tmpstr[6] = "NESW"[m_direction];
return tmpstr;
}
ControlState Joystick::Button::GetState() const
{
return SDL_JoystickGetButton(m_js, m_index);
}
ControlState Joystick::Axis::GetState() const
{
return std::max(0.0f, ControlState(SDL_JoystickGetAxis(m_js, m_index)) / m_range);
}
ControlState Joystick::Hat::GetState() const
{
return (SDL_JoystickGetHat(m_js, m_index) & (1 << m_direction)) > 0;
}
}
}
|