GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/sdlinput.cpp Lines: 3 122 2.5 %
Date: 2020-06-04 Branches: 0 60 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2019  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
#include "sdlshared.h"
79
80
#include "input/inputmanager.h"
81
82
#include "gui/gui.h"
83
#include "gui/sdlinput.h"
84
85
#include "render/graphics.h"
86
87
PRAGMA48(GCC diagnostic push)
88
PRAGMA48(GCC diagnostic ignored "-Wshadow")
89
#include <SDL_timer.h>
90
PRAGMA48(GCC diagnostic pop)
91
92
#include "debug.h"
93
94
#ifdef __SWITCH__
95
bool keyboardClosed = false;
96
#endif
97
98
extern volatile time_t cur_time;
99
100
SDLInput *guiInput = nullptr;
101
102
127
SDLInput::SDLInput() :
103
    mKeyInputQueue(),
104
    mMouseInputQueue(),
105
    mMouseMoveTime(0),
106
    mMouseDown(false),
107
762
    mMouseInWindow(true)
108
{
109
127
}
110
111
KeyInput SDLInput::dequeueKeyInput()
112
{
113
    if (mKeyInputQueue.empty())
114
        return KeyInput();
115
116
    KeyInput keyInput = mKeyInputQueue.front();
117
    mKeyInputQueue.pop();
118
119
    return keyInput;
120
}
121
122
MouseInput SDLInput::dequeueMouseInput()
123
{
124
    MouseInput mouseInput;
125
126
    if (mMouseInputQueue.empty())
127
        return MouseInput();
128
129
    mouseInput = mMouseInputQueue.front();
130
    mMouseInputQueue.pop();
131
132
    return mouseInput;
133
}
134
135
void SDLInput::pushInput(const SDL_Event &event)
136
{
137
    BLOCK_START("SDLInput::pushInput")
138
    KeyInput keyInput;
139
    MouseInput mouseInput;
140
141
#ifdef __SWITCH__
142
    // send an enter/select key on keyboard dismiss event
143
    bool visible = SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE;
144
    if (visible)
145
    {
146
        keyboardClosed = false;
147
    }
148
    else if (!keyboardClosed)
149
    {
150
        simulateKey(KeyValue::ENTER, InputAction::GUI_SELECT2);
151
        keyboardClosed = true;
152
    }
153
#endif
154
155
    switch (event.type)
156
    {
157
        case SDL_KEYDOWN:
158
        {
159
            keyInput.setType(KeyEventType::PRESSED);
160
            convertKeyEventToKey(event, keyInput);
161
            mKeyInputQueue.push(keyInput);
162
            break;
163
        }
164
165
        case SDL_KEYUP:
166
        {
167
            keyInput.setType(KeyEventType::RELEASED);
168
            convertKeyEventToKey(event, keyInput);
169
            mKeyInputQueue.push(keyInput);
170
            break;
171
        }
172
173
#ifdef USE_SDL2
174
        case SDL_TEXTINPUT:
175
            keyInput.setType(KeyEventType::PRESSED);
176
            keyInput.setKey(Key(KeyValue::TEXTINPUT));
177
            keyInput.setText(event.text.text);
178
            mKeyInputQueue.push(keyInput);
179
            break;
180
181
        case SDL_MOUSEWHEEL:
182
        {
183
            const int y = event.wheel.y;
184
            if (y)
185
            {
186
                mouseInput.setX(gui->getLastMouseX());
187
                mouseInput.setY(gui->getLastMouseY());
188
#ifdef ANDROID
189
                mouseInput.setReal(0, 0);
190
#endif  // ANDROID
191
192
                mouseInput.setButton(MouseButton::WHEEL);
193
                if (y > 0)
194
                    mouseInput.setType(MouseEventType::WHEEL_MOVED_UP);
195
                else
196
                    mouseInput.setType(MouseEventType::WHEEL_MOVED_DOWN);
197
                mouseInput.setTimeStamp(SDL_GetTicks());
198
                mMouseInputQueue.push(mouseInput);
199
            }
200
201
            break;
202
        }
203
#endif  // USE_SDL2
204
205
#ifdef ANDROID
206
#ifndef USE_SDL2
207
        case SDL_ACCELEROMETER:
208
            break;
209
#endif  // USE_SDL2
210
#endif  // ANDROID
211
212
        case SDL_MOUSEBUTTONDOWN:
213
        {
214
            mMouseDown = true;
215
            const int scale = mainGraphics->getScale();
216
            const int x = event.button.x / scale;
217
            const int y = event.button.y / scale;
218
            mouseInput.setX(x);
219
            mouseInput.setY(y);
220
#ifdef ANDROID
221
#ifdef USE_SDL2
222
            mouseInput.setReal(x, y);
223
#else  // USE_SDL2
224
225
            mouseInput.setReal(event.button.realx / scale,
226
                event.button.realy / scale);
227
#endif  // USE_SDL2
228
#endif  // ANDROID
229
230
            mouseInput.setButton(convertMouseButton(event.button.button));
231
232
#ifndef USE_SDL2
233
            if (event.button.button == SDL_BUTTON_WHEELDOWN)
234
                mouseInput.setType(MouseEventType::WHEEL_MOVED_DOWN);
235
            else if (event.button.button == SDL_BUTTON_WHEELUP)
236
                mouseInput.setType(MouseEventType::WHEEL_MOVED_UP);
237
            else
238
#endif  // USE_SDL2
239
                mouseInput.setType(MouseEventType::PRESSED);
240
            mouseInput.setTimeStamp(SDL_GetTicks());
241
            mMouseInputQueue.push(mouseInput);
242
            break;
243
        }
244
        case SDL_MOUSEBUTTONUP:
245
        {
246
            mMouseDown = false;
247
            const int scale = mainGraphics->getScale();
248
            const int x = event.button.x / scale;
249
            const int y = event.button.y / scale;
250
            mouseInput.setX(x);
251
            mouseInput.setY(y);
252
#ifdef ANDROID
253
#ifdef USE_SDL2
254
            mouseInput.setReal(x, y);
255
#else  // USE_SDL2
256
257
            mouseInput.setReal(event.button.realx / scale,
258
                event.button.realy / scale);
259
#endif  // USE_SDL2
260
#endif  // ANDROID
261
262
            mouseInput.setButton(convertMouseButton(event.button.button));
263
            mouseInput.setType(MouseEventType::RELEASED);
264
            mouseInput.setTimeStamp(SDL_GetTicks());
265
            mMouseInputQueue.push(mouseInput);
266
            break;
267
        }
268
        case SDL_MOUSEMOTION:
269
        {
270
            const int scale = mainGraphics->getScale();
271
            const int x = event.motion.x / scale;
272
            const int y = event.motion.y / scale;
273
            mouseInput.setX(x);
274
            mouseInput.setY(y);
275
#ifdef ANDROID
276
#ifdef USE_SDL2
277
            mouseInput.setReal(x, y);
278
#else  // USE_SDL2
279
280
            mouseInput.setReal(event.motion.realx / scale,
281
                event.motion.realy / scale);
282
#endif  // USE_SDL2
283
#endif  // ANDROID
284
285
            mouseInput.setButton(MouseButton::EMPTY);
286
            mouseInput.setType(MouseEventType::MOVED);
287
            mouseInput.setTimeStamp(SDL_GetTicks());
288
            mMouseInputQueue.push(mouseInput);
289
            mMouseMoveTime = cur_time;
290
            break;
291
        }
292
#ifndef USE_SDL2
293
        case SDL_ACTIVEEVENT:
294
            /*
295
             * This occurs when the mouse leaves the window and the Gui-chan
296
             * application loses its mousefocus.
297
             */
298
            if ((event.active.state & SDL_APPMOUSEFOCUS) != 0 &&
299
                event.active.gain == 0U)
300
            {
301
                mMouseInWindow = false;
302
303
                if (!mMouseDown)
304
                {
305
                    mouseInput.setX(-1);
306
                    mouseInput.setY(-1);
307
                    mouseInput.setButton(MouseButton::EMPTY);
308
                    mouseInput.setType(MouseEventType::MOVED);
309
                    mMouseInputQueue.push(mouseInput);
310
                    mMouseMoveTime = cur_time;
311
                }
312
            }
313
314
            if ((event.active.state & SDL_APPMOUSEFOCUS) != 0 &&
315
                event.active.gain != 0U)
316
            {
317
                mMouseInWindow = true;
318
            }
319
            break;
320
#endif  // USE_SDL2
321
322
        default:
323
            break;
324
    }  // end switch
325
    BLOCK_END("SDLInput::pushInput")
326
}
327
328
void SDLInput::convertKeyEventToKey(const SDL_Event &event, KeyInput &keyInput)
329
{
330
    keyInput.setKey(Key(convertKeyCharacter(event)));
331
    const InputActionT actionId = inputManager.getActionByKey(event);
332
    if (actionId > InputAction::NO_VALUE)
333
        keyInput.setActionId(actionId);
334
}
335
336
MouseButtonT SDLInput::convertMouseButton(const int button)
337
{
338
    switch (button)
339
    {
340
        case SDL_BUTTON_LEFT:
341
            return MouseButton::LEFT;
342
        case SDL_BUTTON_RIGHT:
343
            return MouseButton::RIGHT;
344
        case SDL_BUTTON_MIDDLE:
345
            return MouseButton::MIDDLE;
346
#ifndef USE_SDL2
347
        case SDL_BUTTON_WHEELUP:
348
        case SDL_BUTTON_WHEELDOWN:
349
            return MouseButton::EMPTY;
350
#endif  // USE_SDL2
351
352
        default:
353
            // We have an unknown mouse type which is ignored.
354
            logger->log("unknown button type: %d", button);
355
            return MouseButton::EMPTY;
356
    }
357
}
358
359
void SDLInput::simulateMouseClick(const int x, const int y,
360
                                  const MouseButtonT button)
361
{
362
    MouseInput mouseInput;
363
    mouseInput.setX(x);
364
    mouseInput.setY(y);
365
    mouseInput.setReal(x, y);
366
    mouseInput.setButton(MouseButton::EMPTY);
367
    mouseInput.setType(MouseEventType::MOVED);
368
    mouseInput.setTimeStamp(SDL_GetTicks());
369
    mMouseInputQueue.push(mouseInput);
370
    mouseInput.setButton(button);
371
    mouseInput.setType(MouseEventType::PRESSED);
372
    mouseInput.setTimeStamp(SDL_GetTicks());
373
    mMouseInputQueue.push(mouseInput);
374
    mouseInput.setType(MouseEventType::RELEASED);
375
    mouseInput.setTimeStamp(SDL_GetTicks());
376
    mMouseInputQueue.push(mouseInput);
377
}
378
379
void SDLInput::simulateMouseMove()
380
{
381
    if (gui == nullptr)
382
        return;
383
384
    if (mMouseMoveTime == cur_time)
385
        return;
386
387
    mMouseMoveTime = cur_time;
388
    int x;
389
    int y;
390
    Gui::getMouseState(x, y);
391
392
    MouseInput mouseInput;
393
    mouseInput.setX(x);
394
    mouseInput.setY(y);
395
    mouseInput.setReal(x, y);
396
    mouseInput.setButton(MouseButton::EMPTY);
397
    mouseInput.setType(MouseEventType::MOVED);
398
    mouseInput.setTimeStamp(SDL_GetTicks());
399
    mMouseInputQueue.push(mouseInput);
400
}
401
402
void SDLInput::simulateKey(const int guiKey,
403
                           const InputActionT actionId)
404
{
405
    KeyInput keyInput;
406
    keyInput.setType(KeyEventType::PRESSED);
407
#ifdef USE_SDL2
408
    char str[2];
409
    str[0] = CAST_S8(guiKey);
410
    str[1] = 0;
411
412
    keyInput.setKey(Key(KeyValue::TEXTINPUT));
413
    keyInput.setText(str);
414
    if (guiKey >= 32)
415
        mKeyInputQueue.push(keyInput);
416
#endif  // USE_SDL2
417
418
    keyInput.setKey(Key(guiKey));
419
    if (actionId > InputAction::NO_VALUE)
420
        keyInput.setActionId(actionId);
421
    mKeyInputQueue.push(keyInput);
422
    keyInput.setType(KeyEventType::RELEASED);
423
    mKeyInputQueue.push(keyInput);
424
}