GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/sdlinput.h Lines: 1 181 0.6 %
Date: 2017-11-29 Branches: 0 65 0.0 %

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