1 /*
2  *  The ManaPlus Client
3  *  Copyright (C) 2007  Joshua Langley <joshlangley@optusnet.com.au>
4  *  Copyright (C) 2009-2010  The Mana Developers
5  *  Copyright (C) 2011-2019  The ManaPlus Developers
6  *  Copyright (C) 2019-2021  Andrei Karas
7  *
8  *  This file is part of The ManaPlus Client.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef INPUT_KEYBOARDCONFIG_H
25 #define INPUT_KEYBOARDCONFIG_H
26 
27 #include "localconsts.h"
28 
29 #ifndef USE_SDL2
30 #include "utils/cast.h"
31 #endif  // USE_SDL2
32 
33 PRAGMA48(GCC diagnostic push)
34 PRAGMA48(GCC diagnostic ignored "-Wshadow")
35 #include <SDL_events.h>
PRAGMA48(GCC diagnostic pop)36 PRAGMA48(GCC diagnostic pop)
37 
38 #include "sdlshared.h"
39 
40 #include "events/inputevent.h"
41 
42 class KeyboardConfig final
43 {
44     public:
45         KeyboardConfig();
46 
47         A_DELETE_COPY(KeyboardConfig)
48 
49         /**
50          * Initializes the keyboard config explicitly.
51          */
52         void init();
53 
54         void deinit();
55 
56         /**
57          * Get the enable flag, which will stop the user from doing actions.
58          */
59         bool isEnabled() const noexcept2 A_WARN_UNUSED
60         { return mEnabled; }
61 
62         /**
63          * Get the key function index by providing the keys value.
64          */
65         static InputActionT getKeyIndex(const SDL_Event &event,
66                                         const int grp) A_WARN_UNUSED;
67 
68         /**
69          * Set the enable flag, which will stop the user from doing actions.
70          */
71         void setEnabled(const bool flag)
72         { mEnabled = flag; }
73 
74         /**
75          * Takes a snapshot of all the active keys.
76          */
77         void refreshActiveKeys();
78 
79         static std::string getKeyShortString(const std::string &key)
80                                              A_WARN_UNUSED;
81 
82         constexpr static SDLKey getKeyFromEvent(const SDL_Event &event)
83                                                 A_WARN_UNUSED
84         {
85 #ifdef USE_SDL2
86             return event.key.keysym.scancode;
87 #else  // USE_SDL2
88 
89             return event.key.keysym.sym;
90 #endif  // USE_SDL2
91         }
92 
93         constexpr2 static int getKeyValueFromEvent(const SDL_Event &event)
94                                                    A_WARN_UNUSED
95         {
96 #ifdef USE_SDL2
97             return event.key.keysym.scancode;
98 #else  // USE_SDL2
99 
100             if (event.key.keysym.sym != 0U)
101                 return CAST_S32(event.key.keysym.sym);
102             else if (event.key.keysym.scancode > 1)
103                 return -event.key.keysym.scancode;
104             return 0;
105 #endif  // USE_SDL2
106         }
107 
108 
109         KeysVector *getActionVector(const SDL_Event &event) A_WARN_UNUSED;
110 
111         KeysVector *getActionVectorByKey(const int i) A_WARN_UNUSED;
112 
113         static std::string getKeyName(const int key) A_WARN_UNUSED;
114 
115         bool isActionActive(const InputActionT index) const A_WARN_UNUSED;
116 
117         void update();
118 
119         void handleActivateKey(const SDL_Event &event);
120 
121         void handleActivateKey(const int key);
122 
123         void handleDeActicateKey(const SDL_Event &event);
124 
125         void handleDeActicateKey(const int key);
126 
127         InputActionT getActionId(const SDL_Event &event) A_WARN_UNUSED;
128 
129         void handleRepeat(const int time);
130 
131         void resetRepeat(const int key);
132 
133 #ifdef USE_SDL2
134         bool ignoreKey(const SDL_Event &restrict event) A_WARN_UNUSED;
135 #endif  // USE_SDL2
136 
137     private:
138         bool mEnabled;                   /**< Flag to respond to key input */
139 
140         const uint8_t *mActiveKeys;      /**< Stores a list of all the keys */
141 
142         uint8_t *mActiveKeys2;           /**< Stores a list of all the keys */
143 
144         unsigned int mRepeatTime;
145 
146         KeyToActionMap mKeyToAction;
147 
148         KeyToIdMap mKeyToId;
149 
150         KeyTimeMap mKeyTimeMap;
151 
152         bool mBlockAltTab;
153 };
154 
155 extern KeyboardConfig keyboard;
156 
157 #endif  // INPUT_KEYBOARDCONFIG_H
158