ManaPlus
sdlinput.h
Go to the documentation of this file.
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 [email protected]\
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 
105 {
106  public:
110  SDLInput();
111 
113 
114 
120  void pushInput(const SDL_Event &event);
121 
123 
125  { return mKeyInputQueue.empty(); }
126 
128  { return mMouseInputQueue.empty(); }
129 
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:
148  static MouseButtonT convertMouseButton(const int button) A_WARN_UNUSED;
149 
157  constexpr2 static int convertKeyCharacter(const SDL_Event &event)
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;
383 
386 };
387 
388 extern SDLInput *guiInput;
390 #endif // GUI_SDLINPUT_H
#define CAST_U32
Definition: cast.h:31
static MouseButtonT convertMouseButton(const int button)
Definition: sdlinput.cpp:337
bool mMouseDown
Definition: sdlinput.h:384
static int convertKeyCharacter(const SDL_Event &event)
Definition: sdlinput.h:157
static void convertKeyEventToKey(const SDL_Event &event, KeyInput &keyInput)
Definition: sdlinput.cpp:329
void simulateKey(const int guiKey, const InputActionT actionId)
Definition: sdlinput.cpp:403
time_t mMouseMoveTime
Definition: sdlinput.h:382
void simulateMouseClick(const int x, const int y, const MouseButtonT button)
Definition: sdlinput.cpp:360
std::queue< KeyInput > mKeyInputQueue
Definition: sdlinput.h:380
MouseInput dequeueMouseInput()
Definition: sdlinput.cpp:123
std::queue< MouseInput > mMouseInputQueue
Definition: sdlinput.h:381
bool mMouseInWindow
Definition: sdlinput.h:385
bool isMouseQueueEmpty() const
Definition: sdlinput.h:127
void simulateMouseMove()
Definition: sdlinput.cpp:380
KeyInput dequeueKeyInput()
Definition: sdlinput.cpp:112
void pushInput(const SDL_Event &event)
Definition: sdlinput.cpp:136
bool isKeyQueueEmpty() const
Definition: sdlinput.h:124
PRAGMA45(GCC diagnostic push) PRAGMA45(GCC diagnostic ignored "-Wredundant-decls") PRAGMA45(GCC diagnostic pop) class TestMain
InputAction ::T InputActionT
Definition: inputaction.h:717
#define constexpr2
Definition: localconsts.h:49
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define noexcept2
Definition: localconsts.h:50
#define final
Definition: localconsts.h:46
#define PRAGMA48(str)
Definition: localconsts.h:199
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
MouseButton ::T MouseButtonT
Definition: mousebutton.h:78
@ ENTER
Definition: keyvalue.h:83
@ HOME
Definition: keyvalue.h:96
@ PAGE_DOWN
Definition: keyvalue.h:100
@ BACKSPACE
Definition: keyvalue.h:103
@ PRINT_SCREEN
Definition: keyvalue.h:119
@ SPACE
Definition: keyvalue.h:81
@ RIGHT_META
Definition: keyvalue.h:92
@ LEFT_SUPER
Definition: keyvalue.h:93
@ RIGHT_SHIFT
Definition: keyvalue.h:88
@ SCROLL_LOCK
Definition: keyvalue.h:120
@ LEFT_META
Definition: keyvalue.h:91
@ ALT_GR
Definition: keyvalue.h:123
@ PAGE_UP
Definition: keyvalue.h:97
@ NUM_LOCK
Definition: keyvalue.h:122
@ LEFT_CONTROL
Definition: keyvalue.h:89
@ INSERT
Definition: keyvalue.h:95
@ LEFT_SHIFT
Definition: keyvalue.h:87
@ DELETE_
Definition: keyvalue.h:98
@ ESCAPE
Definition: keyvalue.h:101
@ RIGHT_CONTROL
Definition: keyvalue.h:90
@ RIGHT_ALT
Definition: keyvalue.h:86
@ LEFT_ALT
Definition: keyvalue.h:85
@ CAPS_LOCK
Definition: keyvalue.h:102
@ RIGHT_SUPER
Definition: keyvalue.h:94
SDLInput * guiInput
Definition: sdlinput.cpp:101