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
|
/*
* Copyright (C) 2007-2010 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "system.h"
#include "../Key.h"
#include "SDLJoystick.h"
#include "ButtonTranslator.h"
#include "AdvancedSettings.h"
#include "utils/log.h"
#include <math.h>
#ifdef HAS_SDL_JOYSTICK
using namespace std;
CJoystick g_Joystick; // global
CJoystick::CJoystick()
{
Reset();
m_NumAxes = 0;
m_AxisId = 0;
m_JoyId = 0;
m_ButtonId = 0;
m_HatId = 0;
m_HatState = SDL_HAT_CENTERED;
m_ActiveFlags = JACTIVE_NONE;
for (int i = 0 ; i<MAX_AXES ; i++)
m_Amount[i] = 0;
SetDeadzone(0);
}
void CJoystick::Initialize()
{
// clear old joystick names
m_JoystickNames.clear();
// any open ones? if so, close them.
if (m_Joysticks.size()>0)
{
for(size_t idJoy = 0; idJoy < m_Joysticks.size(); idJoy++)
{
// any joysticks unplugged?
if(SDL_JoystickOpened(idJoy))
SDL_JoystickClose(m_Joysticks[idJoy]);
}
m_Joysticks.clear();
m_JoyId = -1;
}
// Set deadzone range
SetDeadzone(g_advancedSettings.m_controllerDeadzone);
// any joysticks connected?
if (SDL_NumJoysticks()>0)
{
// load joystick names and open all connected joysticks
for (int i = 0 ; i<SDL_NumJoysticks() ; i++)
{
SDL_Joystick *joy = SDL_JoystickOpen(i);
#ifdef __APPLE__
// On OS X, the 360 controllers are handled externally, since the SDL code is
// really buggy and doesn't handle disconnects.
//
if (std::string(SDL_JoystickName(i)).find("360") != std::string::npos)
{
CLog::Log(LOGNOTICE, "Ignoring joystick: %s", SDL_JoystickName(i));
continue;
}
#endif
m_Joysticks.push_back(joy);
if (joy)
{
m_JoystickNames.push_back(string(SDL_JoystickName(i)));
CLog::Log(LOGNOTICE, "Enabled Joystick: %s", SDL_JoystickName(i));
CLog::Log(LOGNOTICE, "Details: Total Axis: %d Total Hats: %d Total Buttons: %d",
SDL_JoystickNumAxes(joy), SDL_JoystickNumHats(joy), SDL_JoystickNumButtons(joy));
}
else
{
m_JoystickNames.push_back(string(""));
}
}
}
// disable joystick events, since we'll be polling them
SDL_JoystickEventState(SDL_DISABLE);
}
void CJoystick::Reset(bool axis)
{
if (axis)
{
SetAxisActive(false);
for (int i = 0 ; i<MAX_AXES ; i++)
{
ResetAxis(i);
}
}
}
void CJoystick::Update()
{
int buttonId = -1;
int axisId = -1;
int hatId = -1;
int numj = m_Joysticks.size();
if (numj <= 0)
return;
// update the state of all opened joysticks
SDL_JoystickUpdate();
// go through all joysticks
for (int j = 0; j<numj; j++)
{
SDL_Joystick *joy = m_Joysticks[j];
int numb = SDL_JoystickNumButtons(joy);
int numhat = SDL_JoystickNumHats(joy);
int numax = SDL_JoystickNumAxes(joy);
numax = (numax>MAX_AXES)?MAX_AXES:numax;
int axisval;
uint8_t hatval;
// get button states first, they take priority over axis
for (int b = 0 ; b<numb ; b++)
{
if (SDL_JoystickGetButton(joy, b))
{
m_JoyId = j;
buttonId = b+1;
j = numj-1;
break;
}
}
for (int h = 0; h < numhat; h++)
{
hatval = SDL_JoystickGetHat(joy, h);
if (hatval != SDL_HAT_CENTERED)
{
m_JoyId = j;
hatId = h + 1;
m_HatState = hatval;
j = numj-1;
break;
}
}
// get axis states
m_NumAxes = numax;
for (int a = 0 ; a<numax ; a++)
{
axisval = SDL_JoystickGetAxis(joy, a);
axisId = a+1;
if (axisId<=0 || axisId>=MAX_AXES)
{
CLog::Log(LOGERROR, "Axis Id out of range. Maximum supported axis: %d", MAX_AXES);
}
else
{
m_Amount[axisId] = axisval; //[-32768 to 32767]
}
}
m_AxisId = GetAxisWithMaxAmount();
if (m_AxisId)
{
m_JoyId = j;
j = numj-1;
break;
}
}
if(hatId==-1)
{
if(m_HatId!=0)
CLog::Log(LOGDEBUG, "Joystick %d hat %u Centered", m_JoyId, hatId);
m_pressTicksHat = 0;
SetHatActive(false);
m_HatId = 0;
}
else
{
if(hatId!=m_HatId)
{
CLog::Log(LOGDEBUG, "Joystick %d hat %u Down", m_JoyId, hatId);
m_HatId = hatId;
m_pressTicksHat = SDL_GetTicks();
}
SetHatActive();
}
if (buttonId==-1)
{
if (m_ButtonId!=0)
{
CLog::Log(LOGDEBUG, "Joystick %d button %d Up", m_JoyId, m_ButtonId);
}
m_pressTicksButton = 0;
SetButtonActive(false);
m_ButtonId = 0;
}
else
{
if (buttonId!=m_ButtonId)
{
CLog::Log(LOGDEBUG, "Joystick %d button %d Down", m_JoyId, buttonId);
m_ButtonId = buttonId;
m_pressTicksButton = SDL_GetTicks();
}
SetButtonActive();
}
}
void CJoystick::Update(SDL_Event& joyEvent)
{
int buttonId = -1;
int axisId = -1;
int joyId = -1;
bool ignore = false; // not used for now
bool axis = false;
switch(joyEvent.type)
{
case SDL_JOYBUTTONDOWN:
m_JoyId = joyId = joyEvent.jbutton.which;
m_ButtonId = buttonId = joyEvent.jbutton.button + 1;
m_pressTicksButton = SDL_GetTicks();
SetButtonActive();
CLog::Log(LOGDEBUG, "Joystick %d button %d Down", joyId, buttonId);
break;
case SDL_JOYAXISMOTION:
joyId = joyEvent.jaxis.which;
axisId = joyEvent.jaxis.axis + 1;
m_NumAxes = SDL_JoystickNumAxes(m_Joysticks[joyId]);
if (axisId<=0 || axisId>=MAX_AXES)
{
CLog::Log(LOGERROR, "Axis Id out of range. Maximum supported axis: %d", MAX_AXES);
ignore = true;
break;
}
axis = true;
m_JoyId = joyId;
if (joyEvent.jaxis.value==0)
{
ignore = true;
m_Amount[axisId] = 0;
}
else
{
m_Amount[axisId] = joyEvent.jaxis.value; //[-32768 to 32767]
}
m_AxisId = GetAxisWithMaxAmount();
CLog::Log(LOGDEBUG, "Joystick %d Axis %d Amount %d", joyId, axisId, m_Amount[axisId]);
break;
case SDL_JOYHATMOTION:
m_JoyId = joyId = joyEvent.jbutton.which;
m_HatId = joyEvent.jhat.hat + 1;
m_pressTicksHat = SDL_GetTicks();
m_HatState = joyEvent.jhat.value;
SetHatActive(m_HatState != SDL_HAT_CENTERED);
CLog::Log(LOGDEBUG, "Joystick %d Hat %d Down with position %d", joyId, buttonId, m_HatState);
break;
case SDL_JOYBALLMOTION:
ignore = true;
break;
case SDL_JOYBUTTONUP:
m_pressTicksButton = 0;
SetButtonActive(false);
CLog::Log(LOGDEBUG, "Joystick %d button %d Up", joyEvent.jbutton.which, m_ButtonId);
default:
ignore = true;
break;
}
}
bool CJoystick::GetHat(int &id, int &position,bool consider_repeat)
{
if (!IsHatActive())
return false;
position = m_HatState;
id = m_HatId;
if (!consider_repeat)
return true;
static uint32_t lastPressTicks = 0;
static uint32_t lastTicks = 0;
static uint32_t nowTicks = 0;
if ((m_HatId>=0) && m_pressTicksHat)
{
// return the id if it's the first press
if (lastPressTicks!=m_pressTicksHat)
{
lastPressTicks = m_pressTicksHat;
return true;
}
nowTicks = SDL_GetTicks();
if ((nowTicks-m_pressTicksHat)<500) // 500ms delay before we repeat
return false;
if ((nowTicks-lastTicks)<100) // 100ms delay before successive repeats
return false;
lastTicks = nowTicks;
}
return true;
}
bool CJoystick::GetButton(int &id, bool consider_repeat)
{
if (!IsButtonActive())
return false;
if (!consider_repeat)
{
id = m_ButtonId;
return true;
}
static uint32_t lastPressTicks = 0;
static uint32_t lastTicks = 0;
static uint32_t nowTicks = 0;
if ((m_ButtonId>=0) && m_pressTicksButton)
{
// return the id if it's the first press
if (lastPressTicks!=m_pressTicksButton)
{
lastPressTicks = m_pressTicksButton;
id = m_ButtonId;
return true;
}
nowTicks = SDL_GetTicks();
if ((nowTicks-m_pressTicksButton)<500) // 500ms delay before we repeat
{
return false;
}
if ((nowTicks-lastTicks)<100) // 100ms delay before successive repeats
{
return false;
}
lastTicks = nowTicks;
}
id = m_ButtonId;
return true;
}
int CJoystick::GetAxisWithMaxAmount()
{
static int maxAmount;
static int axis;
axis = 0;
maxAmount = 0;
int tempf;
for (int i = 1 ; i<=m_NumAxes ; i++)
{
tempf = abs(m_Amount[i]);
if (tempf>m_DeadzoneRange && tempf>maxAmount)
{
maxAmount = tempf;
axis = i;
}
}
SetAxisActive(0 != maxAmount);
return axis;
}
float CJoystick::GetAmount(int axis)
{
if (m_Amount[axis] > m_DeadzoneRange)
return (float)(m_Amount[axis]-m_DeadzoneRange)/(float)(MAX_AXISAMOUNT-m_DeadzoneRange);
if (m_Amount[axis] < -m_DeadzoneRange)
return (float)(m_Amount[axis]+m_DeadzoneRange)/(float)(MAX_AXISAMOUNT-m_DeadzoneRange);
return 0;
}
float CJoystick::SetDeadzone(float val)
{
if (val<0) val=0;
if (val>1) val=1;
m_DeadzoneRange = (int)(val*MAX_AXISAMOUNT);
return val;
}
#endif
|