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/>.
26
#include "configuration.h"
27
#include "inputmanager.h"
29
#include "sdlshared.h"
33
int Joystick::joystickCount = 0;
34
bool Joystick::mEnabled = false;
36
Joystick::Joystick(const int no):
44
mNumber(no >= joystickCount ? joystickCount : no),
46
mButtonsNumber(MAX_BUTTONS),
53
for (int i = 0; i < MAX_BUTTONS; i++)
54
mActiveButtons[i] = false;
64
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
65
SDL_JoystickEventState(SDL_ENABLE);
66
joystickCount = SDL_NumJoysticks();
67
logger->log("%i joysticks/gamepads found", joystickCount);
68
for (int i = 0; i < joystickCount; i++)
69
logger->log("- %s", SDL_JoystickNameForIndex(i));
71
mEnabled = config.getBoolValue("joystickEnabled");
73
if (joystickCount > 0)
75
joystick = new Joystick(config.getIntValue("selectedJoystick"));
83
if (mNumber >= joystickCount)
84
mNumber = joystickCount - 1;
87
logger->log1("error: incorrect joystick selection");
90
logger->log("open joystick %d", mNumber);
92
mJoystick = SDL_JoystickOpen(mNumber);
96
logger->log("Couldn't open joystick: %s", SDL_GetError());
100
mButtonsNumber = SDL_JoystickNumButtons(mJoystick);
101
logger->log("Joystick: %i ", mNumber);
102
logger->log("Axes: %i ", SDL_JoystickNumAxes(mJoystick));
103
logger->log("Balls: %i", SDL_JoystickNumBalls(mJoystick));
104
logger->log("Hats: %i", SDL_JoystickNumHats(mJoystick));
105
logger->log("Buttons: %i", mButtonsNumber);
107
mHaveHats = (SDL_JoystickNumHats(mJoystick) > 0);
109
if (mButtonsNumber > MAX_BUTTONS)
110
mButtonsNumber = MAX_BUTTONS;
112
mCalibrated = config.getValueBool("joystick"
113
+ toString(mNumber) + "calibrated", false);
114
mUpTolerance = config.getIntValue("upTolerance" + toString(mNumber));
115
mDownTolerance = config.getIntValue("downTolerance" + toString(mNumber));
116
mLeftTolerance = config.getIntValue("leftTolerance" + toString(mNumber));
117
mRightTolerance = config.getIntValue("rightTolerance" + toString(mNumber));
118
mUseInactive = config.getBoolValue("useInactiveJoystick");
123
void Joystick::close()
125
logger->log("close joystick %d", mNumber);
128
SDL_JoystickClose(mJoystick);
133
void Joystick::reload()
135
joystickCount = SDL_NumJoysticks();
139
void Joystick::setNumber(const int n)
143
SDL_JoystickClose(mJoystick);
153
void Joystick::logic()
155
// When calibrating, don't bother the outside with our state
162
if (!mEnabled || !mCalibrated)
167
if (mUseInactive || client->getInputFocused())
170
int position = SDL_JoystickGetAxis(mJoystick, 0);
171
if (position >= mRightTolerance)
173
else if (position <= mLeftTolerance)
177
position = SDL_JoystickGetAxis(mJoystick, 1);
178
if (position <= mUpTolerance)
180
else if (position >= mDownTolerance)
183
#ifdef DEBUG_JOYSTICK
184
if (SDL_JoystickGetAxis(mJoystick, 2))
185
logger->log("axis 2 pos: %d", SDL_JoystickGetAxis(mJoystick, 2));
186
if (SDL_JoystickGetAxis(mJoystick, 3))
187
logger->log("axis 3 pos: %d", SDL_JoystickGetAxis(mJoystick, 3));
188
if (SDL_JoystickGetAxis(mJoystick, 4))
189
logger->log("axis 4 pos: %d", SDL_JoystickGetAxis(mJoystick, 4));
192
if (!mDirection && mHaveHats)
194
// reading only hat 0
195
const uint8_t hat = SDL_JoystickGetHat(mJoystick, 0);
196
if (hat & SDL_HAT_RIGHT)
198
else if (hat & SDL_HAT_LEFT)
200
if (hat & SDL_HAT_UP)
202
else if (hat & SDL_HAT_DOWN)
207
for (int i = 0; i < mButtonsNumber; i++)
209
const bool state = (SDL_JoystickGetButton(mJoystick, i) == 1);
210
mActiveButtons[i] = state;
213
#ifdef DEBUG_JOYSTICK
214
if (mActiveButtons[i])
215
logger->log("button: %d", i);
221
for (int i = 0; i < mButtonsNumber; i++)
222
mActiveButtons[i] = false;
226
void Joystick::startCalibration()
235
void Joystick::doCalibration()
238
int position = SDL_JoystickGetAxis(mJoystick, 0);
239
if (position > mRightTolerance)
240
mRightTolerance = position;
241
else if (position < mLeftTolerance)
242
mLeftTolerance = position;
245
position = SDL_JoystickGetAxis(mJoystick, 1);
246
if (position > mDownTolerance)
247
mDownTolerance = position;
248
else if (position < mUpTolerance)
249
mUpTolerance = position;
252
void Joystick::finishCalibration()
255
mCalibrating = false;
256
config.setValue("joystick" + toString(mNumber) + "calibrated", true);
257
config.setValue("leftTolerance" + toString(mNumber), mLeftTolerance);
258
config.setValue("rightTolerance" + toString(mNumber), mRightTolerance);
259
config.setValue("upTolerance" + toString(mNumber), mUpTolerance);
260
config.setValue("downTolerance" + toString(mNumber), mDownTolerance);
263
bool Joystick::buttonPressed(const unsigned char no) const
265
return (mEnabled && no < MAX_BUTTONS) ? mActiveButtons[no] : false;
268
void Joystick::getNames(std::vector <std::string> &names)
271
for (int i = 0; i < joystickCount; i++)
272
names.push_back(SDL_JoystickNameForIndex(i));
275
void Joystick::update()
277
inputManager.updateKeyActionMap(mKeyToAction, mKeyToId,
278
mKeyTimeMap, INPUT_JOYSTICK);
281
KeysVector *Joystick::getActionVector(const SDL_Event &event)
283
const int i = getButtonFromEvent(event);
285
if (i < 0 || i >= mButtonsNumber)
287
// logger->log("button triggerAction: %d", i);
288
if (mKeyToAction.find(i) != mKeyToAction.end())
289
return &mKeyToAction[i];
293
KeysVector *Joystick::getActionVectorByKey(const int i)
295
if (i < 0 || i >= mButtonsNumber)
297
// logger->log("button triggerAction: %d", i);
298
if (mKeyToAction.find(i) != mKeyToAction.end())
299
return &mKeyToAction[i];
303
int Joystick::getButtonFromEvent(const SDL_Event &event) const
305
if (event.jbutton.which != mNumber)
307
return event.jbutton.button;
310
bool Joystick::isActionActive(const int index) const
315
const KeyFunction &key = inputManager.getKey(index);
316
for (size_t i = 0; i < KeyFunctionSize; i ++)
318
const KeyItem &val = key.values[i];
319
if (val.type != INPUT_JOYSTICK)
321
const int value = val.value;
322
if (value >= 0 && value < mButtonsNumber)
324
if (mActiveButtons[value])
331
bool Joystick::validate() const
333
if (mCalibrating || !mEnabled || !mCalibrated)
336
return (mUseInactive || client->getInputFocused());
339
void Joystick::handleRepeat(const int time)
341
FOR_EACH (KeyTimeMapIter, it, mKeyTimeMap)
344
const int key = (*it).first;
345
int &keyTime = (*it).second;
346
if (key >= 0 && key < mButtonsNumber)
348
if (mActiveButtons[key])
353
if (time > keyTime && abs(time - keyTime)
354
> SDL_DEFAULT_REPEAT_DELAY * 10)
357
inputManager.triggerAction(getActionVectorByKey(key));
363
void Joystick::resetRepeat(const int key)
365
const KeyTimeMapIter it = mKeyTimeMap.find(key);
366
if (it != mKeyTimeMap.end())
367
(*it).second = tick_time;