GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/input/joystick.h Lines: 0 9 0.0 %
Date: 2017-11-29 Branches: 0 16 0.0 %

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-2017  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
#ifndef INPUT_JOYSTICK_H
24
#define INPUT_JOYSTICK_H
25
26
#include "events/inputevent.h"
27
28
PRAGMA48(GCC diagnostic push)
29
PRAGMA48(GCC diagnostic ignored "-Wshadow")
30
#include <SDL_events.h>
31
PRAGMA48(GCC diagnostic pop)
32
33
class Joystick final
34
{
35
    public:
36
        /**
37
         * Number of buttons we can handle.
38
         */
39
        enum
40
        {
41
            MAX_BUTTONS = 64
42
        };
43
44
        /**
45
         * Directions, to be used as bitmask values.
46
         */
47
        enum
48
        {
49
            UP    = 1,
50
            DOWN  = 2,
51
            LEFT  = 4,
52
            RIGHT = 8
53
        };
54
55
        /**
56
         * Initializes the joystick subsystem.
57
         */
58
        static void init();
59
60
        /**
61
         * Returns the number of available joysticks.
62
         */
63
        static int getNumberOfJoysticks() A_WARN_UNUSED
64
        { return joystickCount; }
65
66
        /**
67
         * Constructor, pass the number of the joystick the new object
68
         * should access.
69
         */
70
        explicit Joystick(const int no);
71
72
        A_DELETE_COPY(Joystick)
73
74
        ~Joystick();
75
76
        bool open();
77
78
        void close();
79
80
        static bool isEnabled() A_WARN_UNUSED
81
        { return mEnabled; }
82
83
        void setNumber(const int n);
84
85
        constexpr2 static void setEnabled(const bool enabled) noexcept2
86
        { mEnabled = enabled; }
87
88
        static void getNames(STD_VECTOR <std::string> &names);
89
90
        /**
91
         * Updates the direction and button information.
92
         */
93
        void logic();
94
95
        void startCalibration();
96
97
        void finishCalibration();
98
99
        bool isCalibrating() const noexcept2 A_WARN_UNUSED
100
        { return mCalibrating; }
101
102
        bool buttonPressed(const unsigned char no) const A_WARN_UNUSED;
103
104
        bool isUp() const noexcept2 A_WARN_UNUSED
105
        { return mEnabled && ((mDirection & UP) != 0); }
106
107
        bool isDown() const noexcept2 A_WARN_UNUSED
108
        { return mEnabled && ((mDirection & DOWN) != 0); }
109
110
        bool isLeft() const noexcept2 A_WARN_UNUSED
111
        { return mEnabled && ((mDirection & LEFT) != 0); }
112
113
        bool isRight() const noexcept2 A_WARN_UNUSED
114
        { return mEnabled && ((mDirection & RIGHT) != 0); }
115
116
        int getNumber() const noexcept2 A_WARN_UNUSED
117
        { return mNumber; }
118
119
        void setUseInactive(const bool b)
120
        { mUseInactive = b; }
121
122
        void update();
123
124
        KeysVector *getActionVector(const SDL_Event &event) A_WARN_UNUSED;
125
126
        KeysVector *getActionVectorByKey(const int i) A_WARN_UNUSED;
127
128
        int getButtonFromEvent(const SDL_Event &event) const A_WARN_UNUSED;
129
130
        bool isActionActive(const InputActionT index) const A_WARN_UNUSED;
131
132
        bool validate() const A_WARN_UNUSED;
133
134
        void handleRepeat(const int time);
135
136
        void resetRepeat(const int key);
137
138
        void reload();
139
140
    protected:
141
        unsigned char mDirection;
142
143
        bool mActiveButtons[MAX_BUTTONS];
144
145
        SDL_Joystick *mJoystick;
146
147
        int mUpTolerance;
148
        int mDownTolerance;
149
        int mLeftTolerance;
150
        int mRightTolerance;
151
        bool mCalibrating;
152
        int mNumber;
153
        bool mCalibrated;
154
        int mButtonsNumber;
155
        bool mUseInactive;
156
        bool mHaveHats;
157
158
        KeyToActionMap mKeyToAction;
159
160
        KeyToIdMap mKeyToId;
161
162
        KeyTimeMap mKeyTimeMap;
163
164
        /**
165
         * Is joystick support enabled.
166
         */
167
        static bool mEnabled;
168
        static int joystickCount;
169
170
        void doCalibration();
171
};
172
173
extern Joystick *joystick;
174
175
#endif  // INPUT_JOYSTICK_H