GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/input/keyboardconfig.h Lines: 3 7 42.9 %
Date: 2017-11-29 Branches: 0 20 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2007  Joshua Langley <[email protected]>
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_KEYBOARDCONFIG_H
24
#define INPUT_KEYBOARDCONFIG_H
25
26
#include "localconsts.h"
27
28
#ifndef USE_SDL2
29
#include "utils/cast.h"
30
#endif  // USE_SDL2
31
32
PRAGMA48(GCC diagnostic push)
33
PRAGMA48(GCC diagnostic ignored "-Wshadow")
34
#include <SDL_events.h>
35
PRAGMA48(GCC diagnostic pop)
36
37
#include "sdlshared.h"
38
39
#include "events/inputevent.h"
40
41
8
class KeyboardConfig final
42
{
43
    public:
44
        KeyboardConfig();
45
46
        A_DELETE_COPY(KeyboardConfig)
47
48
        /**
49
         * Initializes the keyboard config explicitly.
50
         */
51
        void init();
52
53
        void deinit();
54
55
        /**
56
         * Get the enable flag, which will stop the user from doing actions.
57
         */
58
        bool isEnabled() const noexcept2 A_WARN_UNUSED
59
6
        { return mEnabled; }
60
61
        /**
62
         * Get the key function index by providing the keys value.
63
         */
64
        static InputActionT getKeyIndex(const SDL_Event &event,
65
                                        const int grp = 1) A_WARN_UNUSED;
66
67
        /**
68
         * Set the enable flag, which will stop the user from doing actions.
69
         */
70
        void setEnabled(const bool flag)
71
6
        { mEnabled = flag; }
72
73
        /**
74
         * Takes a snapshot of all the active keys.
75
         */
76
        void refreshActiveKeys();
77
78
        static std::string getKeyShortString(const std::string &key)
79
                                             A_WARN_UNUSED;
80
81
        constexpr static SDLKey getKeyFromEvent(const SDL_Event &event)
82
                                                A_WARN_UNUSED
83
        {
84
#ifdef USE_SDL2
85
            return event.key.keysym.scancode;
86
#else  // USE_SDL2
87
88
            return event.key.keysym.sym;
89
#endif  // USE_SDL2
90
        }
91
92
        constexpr2 static int getKeyValueFromEvent(const SDL_Event &event)
93
                                                   A_WARN_UNUSED
94
        {
95
#ifdef USE_SDL2
96
            return event.key.keysym.scancode;
97
#else  // USE_SDL2
98
99
            if (event.key.keysym.sym != 0u)
100
                return CAST_S32(event.key.keysym.sym);
101
            else if (event.key.keysym.scancode > 1)
102
                return -event.key.keysym.scancode;
103
            return 0;
104
#endif  // USE_SDL2
105
        }
106
107
108
        KeysVector *getActionVector(const SDL_Event &event) A_WARN_UNUSED;
109
110
        KeysVector *getActionVectorByKey(const int i) A_WARN_UNUSED;
111
112
        static std::string getKeyName(const int key) A_WARN_UNUSED;
113
114
        bool isActionActive(const InputActionT index) const A_WARN_UNUSED;
115
116
        void update();
117
118
        void handleActivateKey(const SDL_Event &event);
119
120
        void handleActivateKey(const int key);
121
122
        void handleDeActicateKey(const SDL_Event &event);
123
124
        void handleDeActicateKey(const int key);
125
126
        InputActionT getActionId(const SDL_Event &event) A_WARN_UNUSED;
127
128
        void handleRepeat(const int time);
129
130
        void resetRepeat(const int key);
131
132
#ifdef USE_SDL2
133
        bool ignoreKey(const SDL_Event &restrict event) A_WARN_UNUSED;
134
#endif  // USE_SDL2
135
136
    private:
137
        bool mEnabled;                   /**< Flag to respond to key input */
138
139
        const uint8_t *mActiveKeys;      /**< Stores a list of all the keys */
140
141
        uint8_t *mActiveKeys2;           /**< Stores a list of all the keys */
142
143
        unsigned int mRepeatTime;
144
145
        KeyToActionMap mKeyToAction;
146
147
        KeyToIdMap mKeyToId;
148
149
        KeyTimeMap mKeyTimeMap;
150
151
        bool mBlockAltTab;
152
};
153
154
extern KeyboardConfig keyboard;
155
156
#endif  // INPUT_KEYBOARDCONFIG_H