GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/input/joystick.cpp Lines: 4 172 2.3 %
Date: 2020-06-04 Branches: 1 188 0.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
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
12
 *  any later version.
13
 *
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.
18
 *
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/>.
21
 */
22
23
#include "input/joystick.h"
24
25
#include "configuration.h"
26
#include "logger.h"
27
#include "settings.h"
28
#include "sdlshared.h"
29
30
#include "input/inputmanager.h"
31
32
#include "utils/foreach.h"
33
#include "utils/timer.h"
34
35
PRAGMA48(GCC diagnostic push)
36
PRAGMA48(GCC diagnostic ignored "-Wshadow")
37
#include <SDL.h>
38
PRAGMA48(GCC diagnostic pop)
39
40
#include "debug.h"
41
42
Joystick *joystick = nullptr;
43
int Joystick::joystickCount = 0;
44
bool Joystick::mEnabled = false;
45
46
Joystick::Joystick(const int no) :
47
    mDirection(0),
48
    mJoystick(nullptr),
49
    mUpTolerance(0),
50
    mDownTolerance(0),
51
    mLeftTolerance(0),
52
    mRightTolerance(0),
53
    mCalibrating(false),
54
    mNumber(no >= joystickCount ? joystickCount : no),
55
    mCalibrated(false),
56
    mButtonsNumber(MAX_BUTTONS),
57
    mUseInactive(false),
58
    mHaveHats(false),
59
    mKeyToAction(),
60
    mKeyToId(),
61
    mKeyTimeMap()
62
{
63
    for (int i = 0; i < MAX_BUTTONS; i++)
64
        mActiveButtons[i] = false;
65
}
66
67
Joystick::~Joystick()
68
{
69
    close();
70
}
71
72
void Joystick::init()
73
{
74
    SDL_InitSubSystem(SDL_INIT_JOYSTICK);
75
    // +++ possible to use SDL_EventState with different joystick features.
76
    SDL_JoystickEventState(SDL_ENABLE);
77
    joystickCount = SDL_NumJoysticks();
78
    logger->log("%i joysticks/gamepads found", joystickCount);
79
    for (int i = 0; i < joystickCount; i++)
80
        logger->log("- %s", SDL_JoystickNameForIndex(i));
81
82
    mEnabled = config.getBoolValue("joystickEnabled");
83
84
    if (joystickCount > 0)
85
    {
86
        joystick = new Joystick(config.getIntValue("selectedJoystick"));
87
        if (mEnabled)
88
            joystick->open();
89
    }
90
}
91
92
bool Joystick::open()
93
{
94
    if (mNumber >= joystickCount)
95
        mNumber = joystickCount - 1;
96
    if (mNumber < 0)
97
    {
98
        logger->log1("error: incorrect joystick selection");
99
        return false;
100
    }
101
    logger->log("open joystick %d", mNumber);
102
103
    mJoystick = SDL_JoystickOpen(mNumber);
104
105
    if (mJoystick == nullptr)
106
    {
107
        logger->log("Couldn't open joystick: %s", SDL_GetError());
108
        return false;
109
    }
110
111
    mButtonsNumber = SDL_JoystickNumButtons(mJoystick);
112
    logger->log("Joystick: %i ", mNumber);
113
#ifdef USE_SDL2
114
    logger->log("Name: %s", SDL_JoystickName(mJoystick));
115
    SDL_JoystickGUID guid = SDL_JoystickGetGUID(mJoystick);
116
    std::string guidStr;
117
    for (int f = 0; f < 16; f ++)
118
        guidStr.append(strprintf("%02x", CAST_U32(guid.data[f])));
119
    logger->log("Guid: %s", guidStr.c_str());
120
#if SDL_VERSION_ATLEAST(2, 0, 6)
121
    logger->log("Device id: %u:%u.%u",
122
        CAST_U32(SDL_JoystickGetVendor(mJoystick)),
123
        CAST_U32(SDL_JoystickGetProduct(mJoystick)),
124
        CAST_U32(SDL_JoystickGetProductVersion(mJoystick)));
125
126
    SDL_JoystickType type = SDL_JoystickGetType(mJoystick);
127
    std::string typeStr;
128
    switch (type)
129
    {
130
        default:
131
        case SDL_JOYSTICK_TYPE_UNKNOWN:
132
            typeStr = "unknown";
133
            break;
134
        case SDL_JOYSTICK_TYPE_GAMECONTROLLER:
135
            typeStr = "game controller";
136
            break;
137
        case SDL_JOYSTICK_TYPE_WHEEL:
138
            typeStr = "wheel";
139
            break;
140
        case SDL_JOYSTICK_TYPE_ARCADE_STICK:
141
            typeStr = "arcade stick";
142
            break;
143
        case SDL_JOYSTICK_TYPE_FLIGHT_STICK:
144
            typeStr = "flight stick";
145
            break;
146
        case SDL_JOYSTICK_TYPE_DANCE_PAD:
147
            typeStr = "dance pad";
148
            break;
149
        case SDL_JOYSTICK_TYPE_GUITAR:
150
            typeStr = "guitar";
151
            break;
152
        case SDL_JOYSTICK_TYPE_DRUM_KIT:
153
            typeStr = "drum kit";
154
            break;
155
        case SDL_JOYSTICK_TYPE_ARCADE_PAD:
156
            typeStr = "arcade pad";
157
            break;
158
        case SDL_JOYSTICK_TYPE_THROTTLE:
159
            typeStr = "throttle";
160
            break;
161
    }
162
    logger->log("Type: " + typeStr);
163
#endif  // SDL_VERSION_ATLEAST(2, 0, 6)
164
    // probably need aslo dump SDL_JoystickCurrentPowerLevel
165
#else  // USE_SDL2
166
167
    logger->log("Name: %s", SDL_JoystickName(mNumber));
168
#endif  // USE_SDL2
169
170
    logger->log("Axes: %i ", SDL_JoystickNumAxes(mJoystick));
171
    logger->log("Balls: %i", SDL_JoystickNumBalls(mJoystick));
172
    logger->log("Hats: %i", SDL_JoystickNumHats(mJoystick));
173
    logger->log("Buttons: %i", mButtonsNumber);
174
175
    mHaveHats = (SDL_JoystickNumHats(mJoystick) > 0);
176
177
    if (mButtonsNumber > MAX_BUTTONS)
178
        mButtonsNumber = MAX_BUTTONS;
179
180
#ifdef __SWITCH__
181
    config.setValue("joystick" + toString(mNumber) + "calibrated", true);
182
    config.setValue("leftTolerance" + toString(mNumber), -10000);
183
    config.setValue("rightTolerance" + toString(mNumber), 10000);
184
    config.setValue("upTolerance" + toString(mNumber), -10000);
185
    config.setValue("downTolerance" + toString(mNumber), 10000);
186
#endif
187
    mCalibrated = config.getValueBool("joystick"
188
        + toString(mNumber) + "calibrated", false);
189
190
    mUpTolerance = config.getIntValue("upTolerance" + toString(mNumber));
191
    mDownTolerance = config.getIntValue("downTolerance" + toString(mNumber));
192
    mLeftTolerance = config.getIntValue("leftTolerance" + toString(mNumber));
193
    mRightTolerance = config.getIntValue("rightTolerance" + toString(mNumber));
194
    mUseInactive = config.getBoolValue("useInactiveJoystick");
195
196
    return true;
197
}
198
199
void Joystick::close()
200
{
201
    logger->log("close joystick %d", mNumber);
202
    if (mJoystick != nullptr)
203
    {
204
        SDL_JoystickClose(mJoystick);
205
        mJoystick = nullptr;
206
    }
207
}
208
209
void Joystick::reload()
210
{
211
    joystickCount = SDL_NumJoysticks();
212
    setNumber(mNumber);
213
}
214
215
void Joystick::setNumber(const int n)
216
{
217
    if (mJoystick != nullptr)
218
    {
219
        SDL_JoystickClose(mJoystick);
220
        mNumber = n;
221
        open();
222
    }
223
    else
224
    {
225
        mNumber = n;
226
    }
227
}
228
229
void Joystick::logic()
230
{
231
    BLOCK_START("Joystick::logic")
232
    // When calibrating, don't bother the outside with our state
233
    if (mCalibrating)
234
    {
235
        doCalibration();
236
        BLOCK_END("Joystick::logic")
237
        return;
238
    }
239
240
    if (!mEnabled || !mCalibrated)
241
    {
242
        BLOCK_END("Joystick::logic")
243
        return;
244
    }
245
246
    mDirection = 0;
247
248
    if (mUseInactive ||
249
        settings.inputFocused != KeyboardFocus::Unfocused)
250
    {
251
        // X-Axis
252
        int position = SDL_JoystickGetAxis(mJoystick, 0);
253
        if (position >= mRightTolerance)
254
            mDirection |= RIGHT;
255
        else if (position <= mLeftTolerance)
256
            mDirection |= LEFT;
257
258
        // Y-Axis
259
        position = SDL_JoystickGetAxis(mJoystick, 1);
260
        if (position <= mUpTolerance)
261
            mDirection |= UP;
262
        else if (position >= mDownTolerance)
263
            mDirection |= DOWN;
264
265
#ifdef DEBUG_JOYSTICK
266
        if (SDL_JoystickGetAxis(mJoystick, 2))
267
            logger->log("axis 2 pos: %d", SDL_JoystickGetAxis(mJoystick, 2));
268
        if (SDL_JoystickGetAxis(mJoystick, 3))
269
            logger->log("axis 3 pos: %d", SDL_JoystickGetAxis(mJoystick, 3));
270
        if (SDL_JoystickGetAxis(mJoystick, 4))
271
            logger->log("axis 4 pos: %d", SDL_JoystickGetAxis(mJoystick, 4));
272
#endif  // DEBUG_JOYSTICK
273
274
        if ((mDirection == 0U) && mHaveHats)
275
        {
276
            // reading only hat 0
277
            const uint8_t hat = SDL_JoystickGetHat(mJoystick, 0);
278
            if ((hat & SDL_HAT_RIGHT) != 0)
279
                mDirection |= RIGHT;
280
            else if ((hat & SDL_HAT_LEFT) != 0)
281
                mDirection |= LEFT;
282
            if ((hat & SDL_HAT_UP) != 0)
283
                mDirection |= UP;
284
            else if ((hat & SDL_HAT_DOWN) != 0)
285
                mDirection |= DOWN;
286
        }
287
288
        // Buttons
289
        for (int i = 0; i < mButtonsNumber; i++)
290
        {
291
            const bool state = (SDL_JoystickGetButton(mJoystick, i) == 1);
292
            mActiveButtons[i] = state;
293
            if (!state)
294
                resetRepeat(i);
295
#ifdef DEBUG_JOYSTICK
296
            if (mActiveButtons[i])
297
                logger->log("button: %d", i);
298
#endif  // DEBUG_JOYSTICK
299
        }
300
    }
301
    else
302
    {
303
        for (int i = 0; i < mButtonsNumber; i++)
304
            mActiveButtons[i] = false;
305
    }
306
    BLOCK_END("Joystick::logic")
307
}
308
309
void Joystick::startCalibration()
310
{
311
    mUpTolerance = 0;
312
    mDownTolerance = 0;
313
    mLeftTolerance = 0;
314
    mRightTolerance = 0;
315
    mCalibrating = true;
316
}
317
318
void Joystick::doCalibration()
319
{
320
    // X-Axis
321
    int position = SDL_JoystickGetAxis(mJoystick, 0);
322
    if (position > mRightTolerance)
323
        mRightTolerance = position;
324
    else if (position < mLeftTolerance)
325
        mLeftTolerance = position;
326
327
    // Y-Axis
328
    position = SDL_JoystickGetAxis(mJoystick, 1);
329
    if (position > mDownTolerance)
330
        mDownTolerance = position;
331
    else if (position < mUpTolerance)
332
        mUpTolerance = position;
333
}
334
335
void Joystick::finishCalibration()
336
{
337
    mCalibrated = true;
338
    mCalibrating = false;
339
    config.setValue("joystick" + toString(mNumber) + "calibrated", true);
340
    config.setValue("leftTolerance" + toString(mNumber), mLeftTolerance);
341
    config.setValue("rightTolerance" + toString(mNumber), mRightTolerance);
342
    config.setValue("upTolerance" + toString(mNumber), mUpTolerance);
343
    config.setValue("downTolerance" + toString(mNumber), mDownTolerance);
344
}
345
346
bool Joystick::buttonPressed(const unsigned char no) const
347
{
348
    return (mEnabled && no < MAX_BUTTONS) ? mActiveButtons[no] : false;
349
}
350
351
2
void Joystick::getNames(STD_VECTOR <std::string> &names)
352
{
353
2
    names.clear();
354
2
    for (int i = 0; i < joystickCount; i++)
355
        names.push_back(SDL_JoystickNameForIndex(i));
356
2
}
357
358
void Joystick::update()
359
{
360
    inputManager.updateKeyActionMap(mKeyToAction, mKeyToId,
361
        mKeyTimeMap, InputType::JOYSTICK);
362
}
363
364
KeysVector *Joystick::getActionVector(const SDL_Event &event)
365
{
366
    const int i = getButtonFromEvent(event);
367
368
    if (i < 0 || i >= mButtonsNumber)
369
        return nullptr;
370
//    logger->log("button triggerAction: %d", i);
371
    if (mKeyToAction.find(i) != mKeyToAction.end())
372
        return &mKeyToAction[i];
373
    return nullptr;
374
}
375
376
KeysVector *Joystick::getActionVectorByKey(const int i)
377
{
378
    if (i < 0 || i >= mButtonsNumber)
379
        return nullptr;
380
//    logger->log("button triggerAction: %d", i);
381
    if (mKeyToAction.find(i) != mKeyToAction.end())
382
        return &mKeyToAction[i];
383
    return nullptr;
384
}
385
386
int Joystick::getButtonFromEvent(const SDL_Event &event) const
387
{
388
    if (event.jbutton.which != mNumber)
389
        return -1;
390
    return event.jbutton.button;
391
}
392
393
bool Joystick::isActionActive(const InputActionT index) const
394
{
395
    if (!validate())
396
        return false;
397
398
    const InputFunction &key = inputManager.getKey(index);
399
    for (size_t i = 0; i < inputFunctionSize; i ++)
400
    {
401
        const InputItem &val = key.values[i];
402
        if (val.type != InputType::JOYSTICK)
403
            continue;
404
        const int value = val.value;
405
        if (value >= 0 && value < mButtonsNumber)
406
        {
407
            if (mActiveButtons[value])
408
                return true;
409
        }
410
    }
411
    return false;
412
}
413
414
bool Joystick::validate() const
415
{
416
    if (mCalibrating || !mEnabled || !mCalibrated)
417
        return false;
418
419
    return mUseInactive ||
420
        settings.inputFocused != KeyboardFocus::Unfocused;
421
}
422
423
void Joystick::handleRepeat(const int time)
424
{
425
    BLOCK_START("Joystick::handleRepeat")
426
    FOR_EACH (KeyTimeMapIter, it, mKeyTimeMap)
427
    {
428
        bool repeat(false);
429
        const int key = (*it).first;
430
        if (key >= 0 && key < mButtonsNumber)
431
        {
432
            if (mActiveButtons[key])
433
                repeat = true;
434
        }
435
        if (repeat)
436
        {
437
            int &keyTime = (*it).second;
438
            if (time > keyTime && abs(time - keyTime)
439
                > SDL_DEFAULT_REPEAT_DELAY * 10)
440
            {
441
                keyTime = time;
442
                inputManager.triggerAction(getActionVectorByKey(key));
443
            }
444
        }
445
    }
446
    BLOCK_END("Joystick::handleRepeat")
447
}
448
449
void Joystick::resetRepeat(const int key)
450
{
451
    const KeyTimeMapIter it = mKeyTimeMap.find(key);
452
    if (it != mKeyTimeMap.end())
453
        (*it).second = tick_time;
454
}