GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/sdlinput.h Lines: 1 118 0.8 %
Date: 2021-03-17 Branches: 0 58 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
/*      _______   __   __   __   ______   __   __   _______   __   __
23
 *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
24
 *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
25
 *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
26
 *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
27
 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
28
 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
29
 *
30
 * Copyright (c) 2004, 2005, 2006, 2007 Olof Naessén and Per Larsson
31
 * Copyright (C) 2007-2010  The Mana World Development Team
32
 *
33
 *                                                         Js_./
34
 * Per Larsson a.k.a finalman                          _RqZ{a<^_aa
35
 * Olof Naessén a.k.a jansem/yakslem                _asww7!uY`>  )\a//
36
 *                                                 _Qhm`] _f "'c  1!5m
37
 * Visit: http://guichan.darkbits.org             )Qk<P ` _: :+' .'  "{[
38
 *                                               .)j(] .d_/ '-(  P .   S
39
 * License: (BSD)                                <Td/Z <fP"5(\"??"\a.  .L
40
 * Redistribution and use in source and          _dV>ws?a-?'      ._/L  #'
41
 * binary forms, with or without                 )4d[#7r, .   '     )d`)[
42
 * modification, are permitted provided         _Q-5'5W..j/?'   -?!\)cam'
43
 * that the following conditions are met:       j<<WP+k/);.        _W=j f
44
 * 1. Redistributions of source code must       .$%w\/]Q  . ."'  .  mj$
45
 *    retain the above copyright notice,        ]E.pYY(Q]>.   a     J@\
46
 *    this list of conditions and the           j(]1u<sE"L,. .   ./^ ]{a
47
 *    following disclaimer.                     4'_uomm\.  )L);-4     (3=
48
 * 2. Redistributions in binary form must        )_]X{Z('a_"a7'<a"a,  ]"[
49
 *    reproduce the above copyright notice,       #}<]m7`Za??4,P-"'7. ).m
50
 *    this list of conditions and the            ]d2e)Q(<Q(  ?94   b-  LQ/
51
 *    following disclaimer in the                <B!</]C)d_, '(<' .f. =C+m
52
 *    documentation and/or other materials      .Z!=J ]e []('-4f _ ) -.)m]'
53
 *    provided with the distribution.          .w[5]' _[ /.)_-"+?   _/ <W"
54
 * 3. Neither the name of Guichan nor the      :$we` _! + _/ .        j?
55
 *    names of its contributors may be used     =3)= _f  (_yQmWW$#(    "
56
 *    to endorse or promote products derived     -   W,  sQQQQmZQ#Wwa]..
57
 *    from this software without specific        (js, \[QQW$QWW#?!V"".
58
 *    prior written permission.                    ]y:.<\..          .
59
 *                                                 -]n w/ '         [.
60
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT       )/ )/           !
61
 * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY         <  (; sac    ,    '
62
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,               ]^ .-  %
63
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF            c <   r
64
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR            aga<  <La
65
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE          5%  )P'-3L
66
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR        _bQf` y`..)a
67
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,          ,J?4P'.P"_(\?d'.,
68
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES               _Pa,)!f/<[]/  ?"
69
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT      _2-..:. .r+_,.. .
70
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     ?a.<%"'  " -'.a_ _,
71
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION)                     ^
72
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
73
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
74
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
75
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
76
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
77
 */
78
79
#ifndef GUI_SDLINPUT_H
80
#define GUI_SDLINPUT_H
81
82
#include "enums/input/keyvalue.h"
83
84
#include "input/keyinput.h"
85
86
#include "utils/cast.h"
87
88
PRAGMA48(GCC diagnostic push)
89
PRAGMA48(GCC diagnostic ignored "-Wshadow")
90
#include <SDL_events.h>
91
PRAGMA48(GCC diagnostic pop)
92
93
#ifdef USE_SDL2
94
#include "sdlshared.h"
95
#endif  // USE_SDL2
96
97
#include "input/mouseinput.h"
98
99
#include <queue>
100
101
/**
102
 * SDL implementation of SDLInput.
103
 */
104
381
class SDLInput final
105
{
106
    public:
107
        /**
108
         * Constructor.
109
         */
110
        SDLInput();
111
112
        A_DELETE_COPY(SDLInput)
113
114
        /**
115
         * Pushes an SDL event. It should be called at least once per frame to
116
         * update input with user input.
117
         *
118
         * @param event an event from SDL.
119
         */
120
        void pushInput(const SDL_Event &event);
121
122
        KeyInput dequeueKeyInput() A_WARN_UNUSED;
123
124
        bool isKeyQueueEmpty() const noexcept2 A_WARN_UNUSED
125
        { return mKeyInputQueue.empty(); }
126
127
        bool isMouseQueueEmpty() const noexcept2 A_WARN_UNUSED
128
        { return mMouseInputQueue.empty(); }
129
130
        MouseInput dequeueMouseInput() A_WARN_UNUSED;
131
132
        void simulateMouseClick(const int x, const int y,
133
                                const MouseButtonT button);
134
135
        void simulateMouseMove();
136
137
        void simulateKey(const int guiKey,
138
                         const InputActionT actionId);
139
140
    protected:
141
        /**
142
         * Converts a mouse button from SDL to a Guichan mouse button
143
         * representation.
144
         *
145
         * @param button an SDL mouse button.
146
         * @return a Guichan mouse button.
147
         */
148
        static MouseButtonT convertMouseButton(const int button) A_WARN_UNUSED;
149
150
        /**
151
         * Converts an SDL event key to a key value.
152
         *
153
         * @param event an SDL event with a key to convert.
154
         * @return a key value.
155
         * @see Key
156
         */
157
        constexpr2 static int convertKeyCharacter(const SDL_Event &event)
158
                                                 A_WARN_UNUSED
159
        {
160
            const SDL_keysym keysym = event.key.keysym;
161
#ifdef USE_SDL2
162
            int value = keysym.scancode;
163
#else  // USE_SDL2
164
165
            int value = keysym.unicode;
166
#endif  // USE_SDL2
167
168
            PRAGMA45(GCC diagnostic push)
169
            PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
170
            switch (keysym.sym)
171
            {
172
              case SDLK_TAB:
173
                  value = KeyValue::TAB;
174
                  break;
175
              case SDLK_LALT:
176
                  value = KeyValue::LEFT_ALT;
177
                  break;
178
              case SDLK_RALT:
179
                  value = KeyValue::RIGHT_ALT;
180
                  break;
181
              case SDLK_LSHIFT:
182
                  value = KeyValue::LEFT_SHIFT;
183
                  break;
184
              case SDLK_RSHIFT:
185
                  value = KeyValue::RIGHT_SHIFT;
186
                  break;
187
              case SDLK_LCTRL:
188
                  value = KeyValue::LEFT_CONTROL;
189
                  break;
190
              case SDLK_RCTRL:
191
                  value = KeyValue::RIGHT_CONTROL;
192
                  break;
193
              case SDLK_BACKSPACE:
194
                  value = KeyValue::BACKSPACE;
195
                  break;
196
              case SDLK_PAUSE:
197
                  value = KeyValue::PAUSE;
198
                  break;
199
              case SDLK_SPACE:
200
                  // Special characters like ~ (tilde) ends up
201
                  // with the keysym.sym SDLK_SPACE which
202
                  // without this check would be lost. The check
203
                  // is only valid on key down events in SDL.
204
#ifndef USE_SDL2
205
                  if (event.type == SDL_KEYUP ||
206
                      keysym.unicode == ' ')
207
#endif  // USE_SDL2
208
                  {
209
                      value = KeyValue::SPACE;
210
                  }
211
                  break;
212
              case SDLK_ESCAPE:
213
#ifdef USE_SDL2
214
              case SDLK_AC_BACK:
215
#endif  // USE_SDL2
216
                  value = KeyValue::ESCAPE;
217
                  break;
218
              case SDLK_DELETE:
219
                  value = KeyValue::DELETE_;
220
                  break;
221
              case SDLK_INSERT:
222
                  value = KeyValue::INSERT;
223
                  break;
224
              case SDLK_HOME:
225
                  value = KeyValue::HOME;
226
                  break;
227
              case SDLK_END:
228
                  value = KeyValue::END;
229
                  break;
230
              case SDLK_PAGEUP:
231
                  value = KeyValue::PAGE_UP;
232
                  break;
233
              case SDLK_PRINT:
234
                  value = KeyValue::PRINT_SCREEN;
235
                  break;
236
              case SDLK_PAGEDOWN:
237
                  value = KeyValue::PAGE_DOWN;
238
                  break;
239
              case SDLK_F1:
240
                  value = KeyValue::F1;
241
                  break;
242
              case SDLK_F2:
243
                  value = KeyValue::F2;
244
                  break;
245
              case SDLK_F3:
246
                  value = KeyValue::F3;
247
                  break;
248
              case SDLK_F4:
249
                  value = KeyValue::F4;
250
                  break;
251
              case SDLK_F5:
252
                  value = KeyValue::F5;
253
                  break;
254
              case SDLK_F6:
255
                  value = KeyValue::F6;
256
                  break;
257
              case SDLK_F7:
258
                  value = KeyValue::F7;
259
                  break;
260
              case SDLK_F8:
261
                  value = KeyValue::F8;
262
                  break;
263
              case SDLK_F9:
264
                  value = KeyValue::F9;
265
                  break;
266
              case SDLK_F10:
267
                  value = KeyValue::F10;
268
                  break;
269
              case SDLK_F11:
270
                  value = KeyValue::F11;
271
                  break;
272
              case SDLK_F12:
273
                  value = KeyValue::F12;
274
                  break;
275
              case SDLK_F13:
276
                  value = KeyValue::F13;
277
                  break;
278
              case SDLK_F14:
279
                  value = KeyValue::F14;
280
                  break;
281
              case SDLK_F15:
282
                  value = KeyValue::F15;
283
                  break;
284
              case SDLK_NUMLOCK:
285
                  value = KeyValue::NUM_LOCK;
286
                  break;
287
              case SDLK_CAPSLOCK:
288
                  value = KeyValue::CAPS_LOCK;
289
                  break;
290
              case SDLK_SCROLLOCK:
291
                  value = KeyValue::SCROLL_LOCK;
292
                  break;
293
              case SDLK_RMETA:
294
                  value = KeyValue::RIGHT_META;
295
                  break;
296
              case SDLK_LMETA:
297
                  value = KeyValue::LEFT_META;
298
                  break;
299
#ifndef USE_SDL2
300
              case SDLK_LSUPER:
301
                  value = KeyValue::LEFT_SUPER;
302
                  break;
303
              case SDLK_RSUPER:
304
                  value = KeyValue::RIGHT_SUPER;
305
                  break;
306
#endif  // USE_SDL2
307
308
              case SDLK_MODE:
309
                  value = KeyValue::ALT_GR;
310
                  break;
311
              case SDLK_UP:
312
                  value = KeyValue::UP;
313
                  break;
314
              case SDLK_DOWN:
315
                  value = KeyValue::DOWN;
316
                  break;
317
              case SDLK_LEFT:
318
                  value = KeyValue::LEFT;
319
                  break;
320
              case SDLK_RIGHT:
321
                  value = KeyValue::RIGHT;
322
                  break;
323
              case SDLK_RETURN:
324
              case SDLK_KP_ENTER:
325
                  value = KeyValue::ENTER;
326
                  break;
327
328
              default:
329
                  break;
330
            }
331
            PRAGMA45(GCC diagnostic pop)
332
333
            if ((CAST_U32(keysym.mod) & KMOD_NUM) == 0U)
334
            {
335
                PRAGMA45(GCC diagnostic push)
336
                PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
337
                switch (keysym.sym)
338
                {
339
                  case SDLK_KP0:
340
                      value = KeyValue::INSERT;
341
                      break;
342
                  case SDLK_KP1:
343
                      value = KeyValue::END;
344
                      break;
345
                  case SDLK_KP2:
346
                      value = KeyValue::DOWN;
347
                      break;
348
                  case SDLK_KP3:
349
                      value = KeyValue::PAGE_DOWN;
350
                      break;
351
                  case SDLK_KP4:
352
                      value = KeyValue::LEFT;
353
                      break;
354
                  case SDLK_KP5:
355
                      value = 0;
356
                      break;
357
                  case SDLK_KP6:
358
                      value = KeyValue::RIGHT;
359
                      break;
360
                  case SDLK_KP7:
361
                      value = KeyValue::HOME;
362
                      break;
363
                  case SDLK_KP8:
364
                      value = KeyValue::UP;
365
                      break;
366
                  case SDLK_KP9:
367
                      value = KeyValue::PAGE_UP;
368
                      break;
369
                  default:
370
                      break;
371
                }
372
                PRAGMA45(GCC diagnostic pop)
373
            }
374
            return value;
375
        }
376
377
        static void convertKeyEventToKey(const SDL_Event &event,
378
                                         KeyInput &keyInput);
379
380
        std::queue<KeyInput> mKeyInputQueue;
381
        std::queue<MouseInput> mMouseInputQueue;
382
        time_t mMouseMoveTime;
383
384
        bool mMouseDown;
385
        bool mMouseInWindow;
386
};
387
388
extern SDLInput *guiInput;                  /**< GUI input */
389
390
#endif  // GUI_SDLINPUT_H