3
* Copyright (C) 2004-2009 The Mana World Development Team
4
* Copyright (C) 2009-2010 The Mana Developers
5
* Copyright (C) 2011-2013 The ManaPlus Developers
7
* This file is part of The ManaPlus Client.
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include "input/joystick.h"
26
#include "configuration.h"
28
#include "sdlshared.h"
30
#include "input/inputmanager.h"
34
int Joystick::joystickCount = 0;
35
bool Joystick::mEnabled = false;
37
Joystick::Joystick(const int no):
45
mNumber(no >= joystickCount ? joystickCount : no),
47
mButtonsNumber(MAX_BUTTONS),
54
for (int i = 0; i < MAX_BUTTONS; i++)
55
mActiveButtons[i] = false;
65
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
66
SDL_JoystickEventState(SDL_ENABLE);
67
joystickCount = SDL_NumJoysticks();
68
logger->log("%i joysticks/gamepads found", joystickCount);
69
for (int i = 0; i < joystickCount; i++)
70
logger->log("- %s", SDL_JoystickNameForIndex(i));
72
mEnabled = config.getBoolValue("joystickEnabled");
74
if (joystickCount > 0)
76
joystick = new Joystick(config.getIntValue("selectedJoystick"));
84
if (mNumber >= joystickCount)
85
mNumber = joystickCount - 1;
88
logger->log1("error: incorrect joystick selection");
91
logger->log("open joystick %d", mNumber);
93
mJoystick = SDL_JoystickOpen(mNumber);
97
logger->log("Couldn't open joystick: %s", SDL_GetError());
101
mButtonsNumber = SDL_JoystickNumButtons(mJoystick);
102
logger->log("Joystick: %i ", mNumber);
103
logger->log("Axes: %i ", SDL_JoystickNumAxes(mJoystick));
104
logger->log("Balls: %i", SDL_JoystickNumBalls(mJoystick));
105
logger->log("Hats: %i", SDL_JoystickNumHats(mJoystick));
106
logger->log("Buttons: %i", mButtonsNumber);
108
mHaveHats = (SDL_JoystickNumHats(mJoystick) > 0);
110
if (mButtonsNumber > MAX_BUTTONS)
111
mButtonsNumber = MAX_BUTTONS;
113
mCalibrated = config.getValueBool("joystick"
114
+ toString(mNumber) + "calibrated", false);
115
mUpTolerance = config.getIntValue("upTolerance" + toString(mNumber));
116
mDownTolerance = config.getIntValue("downTolerance" + toString(mNumber));
117
mLeftTolerance = config.getIntValue("leftTolerance" + toString(mNumber));
118
mRightTolerance = config.getIntValue("rightTolerance" + toString(mNumber));
119
mUseInactive = config.getBoolValue("useInactiveJoystick");
124
void Joystick::close()
126
logger->log("close joystick %d", mNumber);
129
SDL_JoystickClose(mJoystick);
134
void Joystick::reload()
136
joystickCount = SDL_NumJoysticks();
140
void Joystick::setNumber(const int n)
144
SDL_JoystickClose(mJoystick);
154
void Joystick::logic()
156
// When calibrating, don't bother the outside with our state
163
if (!mEnabled || !mCalibrated)
168
if (mUseInactive || client->getInputFocused())
171
int position = SDL_JoystickGetAxis(mJoystick, 0);
172
if (position >= mRightTolerance)
174
else if (position <= mLeftTolerance)
178
position = SDL_JoystickGetAxis(mJoystick, 1);
179
if (position <= mUpTolerance)
181
else if (position >= mDownTolerance)
184
#ifdef DEBUG_JOYSTICK
185
if (SDL_JoystickGetAxis(mJoystick, 2))
186
logger->log("axis 2 pos: %d", SDL_JoystickGetAxis(mJoystick, 2));
187
if (SDL_JoystickGetAxis(mJoystick, 3))
188
logger->log("axis 3 pos: %d", SDL_JoystickGetAxis(mJoystick, 3));
189
if (SDL_JoystickGetAxis(mJoystick, 4))
190
logger->log("axis 4 pos: %d", SDL_JoystickGetAxis(mJoystick, 4));
193
if (!mDirection && mHaveHats)
195
// reading only hat 0
196
const uint8_t hat = SDL_JoystickGetHat(mJoystick, 0);
197
if (hat & SDL_HAT_RIGHT)
199
else if (hat & SDL_HAT_LEFT)
201
if (hat & SDL_HAT_UP)
203
else if (hat & SDL_HAT_DOWN)
208
for (int i = 0; i < mButtonsNumber; i++)
210
const bool state = (SDL_JoystickGetButton(mJoystick, i) == 1);
211
mActiveButtons[i] = state;
214
#ifdef DEBUG_JOYSTICK
215
if (mActiveButtons[i])
216
logger->log("button: %d", i);
222
for (int i = 0; i < mButtonsNumber; i++)
223
mActiveButtons[i] = false;
227
void Joystick::startCalibration()
236
void Joystick::doCalibration()
239
int position = SDL_JoystickGetAxis(mJoystick, 0);
240
if (position > mRightTolerance)
241
mRightTolerance = position;
242
else if (position < mLeftTolerance)
243
mLeftTolerance = position;
246
position = SDL_JoystickGetAxis(mJoystick, 1);
247
if (position > mDownTolerance)
248
mDownTolerance = position;
249
else if (position < mUpTolerance)
250
mUpTolerance = position;
253
void Joystick::finishCalibration()
256
mCalibrating = false;
257
config.setValue("joystick" + toString(mNumber) + "calibrated", true);
258
config.setValue("leftTolerance" + toString(mNumber), mLeftTolerance);
259
config.setValue("rightTolerance" + toString(mNumber), mRightTolerance);
260
config.setValue("upTolerance" + toString(mNumber), mUpTolerance);
261
config.setValue("downTolerance" + toString(mNumber), mDownTolerance);
264
bool Joystick::buttonPressed(const unsigned char no) const
266
return (mEnabled && no < MAX_BUTTONS) ? mActiveButtons[no] : false;
269
void Joystick::getNames(std::vector <std::string> &names)
272
for (int i = 0; i < joystickCount; i++)
273
names.push_back(SDL_JoystickNameForIndex(i));
276
void Joystick::update()
278
inputManager.updateKeyActionMap(mKeyToAction, mKeyToId,
279
mKeyTimeMap, INPUT_JOYSTICK);
282
KeysVector *Joystick::getActionVector(const SDL_Event &event)
284
const int i = getButtonFromEvent(event);
286
if (i < 0 || i >= mButtonsNumber)
288
// logger->log("button triggerAction: %d", i);
289
if (mKeyToAction.find(i) != mKeyToAction.end())
290
return &mKeyToAction[i];
294
KeysVector *Joystick::getActionVectorByKey(const int i)
296
if (i < 0 || i >= mButtonsNumber)
298
// logger->log("button triggerAction: %d", i);
299
if (mKeyToAction.find(i) != mKeyToAction.end())
300
return &mKeyToAction[i];
304
int Joystick::getButtonFromEvent(const SDL_Event &event) const
306
if (event.jbutton.which != mNumber)
308
return event.jbutton.button;
311
bool Joystick::isActionActive(const int index) const
316
const KeyFunction &key = inputManager.getKey(index);
317
for (size_t i = 0; i < KeyFunctionSize; i ++)
319
const KeyItem &val = key.values[i];
320
if (val.type != INPUT_JOYSTICK)
322
const int value = val.value;
323
if (value >= 0 && value < mButtonsNumber)
325
if (mActiveButtons[value])
332
bool Joystick::validate() const
334
if (mCalibrating || !mEnabled || !mCalibrated)
337
return (mUseInactive || client->getInputFocused());
340
void Joystick::handleRepeat(const int time)
342
FOR_EACH (KeyTimeMapIter, it, mKeyTimeMap)
345
const int key = (*it).first;
346
int &keyTime = (*it).second;
347
if (key >= 0 && key < mButtonsNumber)
349
if (mActiveButtons[key])
354
if (time > keyTime && abs(time - keyTime)
355
> SDL_DEFAULT_REPEAT_DELAY * 10)
358
inputManager.triggerAction(getActionVectorByKey(key));
364
void Joystick::resetRepeat(const int key)
366
const KeyTimeMapIter it = mKeyTimeMap.find(key);
367
if (it != mKeyTimeMap.end())
368
(*it).second = tick_time;