summaryrefslogtreecommitdiff
path: root/src/input/inputmanager.cpp
blob: 4ba87b92e7f1e262a801edf5bca397dd31673b35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
/*
 *  The ManaPlus Client
 *  Copyright (C) 2012-2015  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "input/inputmanager.h"

#include "configuration.h"
#include "game.h"
#include "settings.h"

#include "being/localplayer.h"
#include "being/playerinfo.h"

#include "input/inputactionmap.h"
#include "input/inputactionsortfunctor.h"
#include "input/joystick.h"
#include "input/keyboardconfig.h"
#ifdef USE_SDL2
#include "input/touch/multitouchmanager.h"
#endif

#include "input/touch/touchmanager.h"

#include "gui/gui.h"
#include "gui/sdlinput.h"

#include "gui/widgets/selldialog.h"
#include "gui/widgets/textfield.h"

#include "gui/widgets/tabs/setup_input.h"

#include "gui/windows/buydialog.h"
#include "gui/windows/chatwindow.h"
#include "gui/windows/inventorywindow.h"
#include "gui/windows/npcdialog.h"
#include "gui/windows/setupwindow.h"
#include "gui/windows/textdialog.h"
#include "gui/windows/tradewindow.h"

#include "utils/gettext.h"
#include "utils/timer.h"

#include "gui/focushandler.h"

#include <algorithm>

#include "debug.h"

InputManager inputManager;

class QuitDialog;

extern QuitDialog *quitDialog;

namespace
{
    InputActionSortFunctor inputActionDataSorter;
}  // namespace

InputManager::InputManager() :
    mSetupInput(nullptr),
    mNewKeyIndex(InputAction::NO_VALUE),
    mMask(1),
    mNameMap(),
    mChatMap()
{
}

void InputManager::init() restrict2
{
    for (unsigned int i = 0;
         i < static_cast<unsigned int>(InputAction::TOTAL);
         i ++)
    {
        InputFunction &kf = mKey[i];
        for (unsigned int f = 0; f < inputFunctionSize; f ++)
        {
            InputItem &ki = kf.values[f];
            ki.type = InputType::UNKNOWN;
            ki.value = -1;
        }
    }

    mNewKeyIndex = InputAction::NO_VALUE;

    resetKeys();
    retrieve();
    update();
}

void InputManager::update()
{
    keyboard.update();
    if (joystick)
        joystick->update();
}

void InputManager::retrieve() restrict2
{
    for (int i = 0; i < static_cast<int>(InputAction::TOTAL); i ++)
    {
        const std::string &restrict cmd = inputActionData[i].chatCommand;
        if (!cmd.empty())
        {
            StringVect tokens;
            splitToStringVector(tokens, cmd, '|');
            FOR_EACH (StringVectCIter, it, tokens)
                mChatMap[*it] = i;
        }
#ifdef USE_SDL2
        const std::string cf = std::string("sdl2")
            + inputActionData[i].configField;
#else
        const std::string cf = inputActionData[i].configField;
#endif
        if (!cf.empty())
        {
            mNameMap[cf] = static_cast<InputActionT>(i);
            InputFunction &restrict kf = mKey[i];
            const std::string keyStr = config.getValue(cf, "");
            const size_t keyStrSize = keyStr.size();
            if (keyStr.empty())
                continue;

            StringVect keys;
            splitToStringVector(keys, keyStr, ',');
            unsigned int i2 = 0;
            for (StringVectCIter it = keys.begin(), it_end = keys.end();
                 it != it_end && i2 < inputFunctionSize; ++ it)
            {
                std::string keyStr2 = *it;
                if (keyStrSize < 2)
                    continue;
                InputTypeT type = InputType::KEYBOARD;
                if ((keyStr2[0] < '0' || keyStr2[0] > '9')
                    && keyStr2[0] != '-')
                {
                    switch (keyStr2[0])
                    {
                        case 'm':
                            type = InputType::MOUSE;
                            break;
                        case 'j':
                            type = InputType::JOYSTICK;
                            break;
                        default:
                            break;
                    }
                    keyStr2 = keyStr2.substr(1);
                }
                const int key = atoi(keyStr2.c_str());
                if (key >= -255 && key < SDLK_LAST)
                {
                    kf.values[i2] = InputItem(type, key);
                    i2 ++;
                }
            }
        }
    }
}

void InputManager::store() const restrict2
{
    for (int i = 0; i < static_cast<int>(InputAction::TOTAL); i ++)
    {
#ifdef USE_SDL2
        const std::string cf = std::string("sdl2")
            + inputActionData[i].configField;
#else
        const std::string cf = inputActionData[i].configField;
#endif
        if (!cf.empty())
        {
            std::string keyStr;
            const InputFunction &restrict kf = mKey[i];

            for (size_t i2 = 0; i2 < inputFunctionSize; i2 ++)
            {
                const InputItem &restrict key = kf.values[i2];
                if (key.type != InputType::UNKNOWN)
                {
                    std::string tmp("k");
                    switch (key.type)
                    {
                        case InputType::MOUSE:
                            tmp = "m";
                            break;
                        case InputType::JOYSTICK:
                            tmp = "j";
                            break;
                        case InputType::KEYBOARD:
                        case InputType::UNKNOWN:
                        default:
                            break;
                    }
                    if (key.value != -1)
                    {
                        if (keyStr.empty())
                        {
                            keyStr.append(tmp).append(toString(key.value));
                        }
                        else
                        {
                            keyStr.append(strprintf(",%s%d",
                                tmp.c_str(), key.value));
                        }
                    }
                }
            }
            if (keyStr.empty())
                keyStr = "-1";

            config.setValue(cf, keyStr);
        }
    }
}

void InputManager::resetKey(const InputActionT i) restrict2
{
    InputFunction &restrict key = mKey[static_cast<size_t>(i)];
    for (size_t i2 = 1; i2 < inputFunctionSize; i2 ++)
    {
        InputItem &restrict ki2 = key.values[i2];
        ki2.type = InputType::UNKNOWN;
        ki2.value = -1;
    }
    const InputActionData &restrict kd =
        inputActionData[static_cast<size_t>(i)];
    InputItem &restrict val0 = key.values[0];
    val0.type = kd.defaultType1;
    InputItem &restrict val1 = key.values[1];
    val1.type = kd.defaultType2;
#ifdef USE_SDL2
    if (kd.defaultType1 == InputType::KEYBOARD)
        val0.value = SDL_GetScancodeFromKey(kd.defaultValue1);
    else
        val0.value = kd.defaultValue1;
    if (kd.defaultType2 == InputType::KEYBOARD)
        val1.value = SDL_GetScancodeFromKey(kd.defaultValue2);
    else
        val1.value = kd.defaultValue2;
#else
    val0.value = kd.defaultValue1;
    val1.value = kd.defaultValue2;
#endif
}

void InputManager::resetKeys() restrict2
{
    for (int i = 0; i < static_cast<int>(InputAction::TOTAL); i++)
        resetKey(static_cast<InputActionT>(i));
}

void InputManager::makeDefault(const InputActionT i) restrict2
{
    if (i > InputAction::NO_VALUE && i < InputAction::TOTAL)
    {
        resetKey(i);
        update();
    }
}

bool InputManager::hasConflicts(InputActionT &restrict key1,
                                InputActionT &restrict key2) const restrict2
{
    /**
     * No need to parse the square matrix: only check one triangle
     * that's enough to detect conflicts
     */
    for (int i = 0; i < static_cast<int>(InputAction::TOTAL); i++)
    {
        const InputActionData &restrict kdi = inputActionData[i];
        if (!*kdi.configField)
            continue;

        const InputFunction &restrict ki = mKey[i];
        for (size_t i2 = 0; i2 < inputFunctionSize; i2 ++)
        {
            const InputItem &restrict vali2 = ki.values[i2];
            if (vali2.value == -1)
                continue;

            size_t j;
            for (j = i, j++; j < static_cast<int>(InputAction::TOTAL); j++)
            {
                if ((kdi.grp & inputActionData[j].grp) == 0
                    || !*kdi.configField)
                {
                    continue;
                }

                for (size_t j2 = 0; j2 < inputFunctionSize; j2 ++)
                {
                    const InputItem &restrict valj2 = mKey[j].values[j2];
                    // Allow for item shortcut and emote keys to overlap
                    // as well as emote and ignore keys, but no other keys
                    if (valj2.type != InputType::UNKNOWN
                        && vali2.value == valj2.value
                        && vali2.type == valj2.type)
                    {
                        key1 = static_cast<InputActionT>(i);
                        key2 = static_cast<InputActionT>(j);
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

void InputManager::callbackNewKey() restrict2
{
#ifndef DYECMD
    mSetupInput->newKeyCallback(mNewKeyIndex);
#endif
}

bool InputManager::isActionActive(const InputActionT index) const restrict2
{
    if (!isActionActive0(index))
        return false;

    const InputActionData &restrict key =
        inputActionData[static_cast<size_t>(index)];
//    logger->log("isActionActive mask=%d, condition=%d, index=%d",
//        mMask, key.condition, index);
    if ((key.condition & mMask) != key.condition)
        return false;
    return true;
}

bool InputManager::isActionActive0(const InputActionT index)
{
    if (keyboard.isActionActive(index))
        return true;
    if (joystick && joystick->isActionActive(index))
        return true;
    return touchManager.isActionActive(index);
}

InputFunction &InputManager::getKey(InputActionT index) restrict2
{
    if (static_cast<int>(index) < 0 || index >= InputAction::TOTAL)
        index = InputAction::MOVE_UP;
    return mKey[static_cast<size_t>(index)];
}

std::string InputManager::getKeyStringLong(const InputActionT index) const
                                           restrict2
{
    std::string keyStr;
    const InputFunction &restrict ki = mKey[static_cast<size_t>(index)];

    for (size_t i = 0; i < inputFunctionSize; i ++)
    {
        const InputItem &restrict key = ki.values[i];
        std::string str;
        if (key.type == InputType::KEYBOARD)
        {
            if (key.value >= 0)
            {
                str = keyboard.getKeyName(key.value);
            }
            else if (key.value < -1)
            {
                // TRANSLATORS: long key name. must be short.
                str = strprintf(_("key_%d"), -key.value);
            }
        }
        else if (key.type == InputType::JOYSTICK)
        {
            // TRANSLATORS: long joystick button name. must be short.
            str = strprintf(_("JButton%d"), key.value + 1);
        }
        if (!str.empty())
        {
            if (keyStr.empty())
                keyStr = str;
            else
                keyStr.append(", ").append(str);
        }
    }

    if (keyStr.empty())
    {
        // TRANSLATORS: unknown long key type
        return _("unknown key");
    }
    return keyStr;
}

std::string InputManager::getKeyValueString(const InputActionT index) const
                                            restrict2
{
    std::string keyStr;
    const InputFunction &restrict ki = mKey[static_cast<size_t>(index)];

    for (size_t i = 0; i < inputFunctionSize; i ++)
    {
        const InputItem &restrict key = ki.values[i];
        std::string str;
        if (key.type == InputType::KEYBOARD)
        {
            if (key.value >= 0)
            {
                str = keyboard.getKeyShortString(
                    keyboard.getKeyName(key.value));
            }
            else if (key.value < -1)
            {
                // TRANSLATORS: short key name. must be very short.
                str = strprintf(_("key_%d"), -key.value);
            }
        }
        else if (key.type == InputType::JOYSTICK)
        {
            // TRANSLATORS: short joystick button name. muse be very short
            str = strprintf(_("JB%d"), key.value + 1);
        }
        if (!str.empty())
        {
            if (keyStr.empty())
                keyStr = str;
            else
                keyStr.append(", ").append(str);
        }
    }

    if (keyStr.empty())
    {
        // TRANSLATORS: unknown short key type. must be short
        return _("u key");
    }
    return keyStr;
}

std::string InputManager::getKeyValueByName(const std::string &restrict
                                            keyName) restrict2
{
    const StringInpActionMapCIter it = mNameMap.find(keyName);

    if (it == mNameMap.end())
        return std::string();
    return getKeyValueString((*it).second);
}

std::string InputManager::getKeyValueByNameLong(const std::string &restrict
                                                keyName) restrict2
{
    const StringInpActionMapCIter it = mNameMap.find(keyName);

    if (it == mNameMap.end())
        return std::string();
    return getKeyStringLong((*it).second);
}

void InputManager::addActionKey(const InputActionT action,
                                const InputTypeT type,
                                const int val) restrict2
{
    if (static_cast<int>(action) < 0 || action >= InputAction::TOTAL)
        return;

    int idx = -1;
    InputFunction &restrict key = mKey[static_cast<size_t>(action)];
    for (size_t i = 0; i < inputFunctionSize; i ++)
    {
        const InputItem &restrict val2 = key.values[i];
        if (val2.type == InputType::UNKNOWN ||
            (val2.type == type && val2.value == val))
        {
            idx = static_cast<int>(i);
            break;
        }
    }
    if (idx == -1)
    {
        for (size_t i = 1; i < inputFunctionSize; i ++)
        {
            InputItem &restrict val1 = key.values[i - 1];
            InputItem &restrict val2 = key.values[i];
            val1.type = val2.type;
            val1.value = val2.value;
        }
        idx = inputFunctionSize - 1;
    }

    key.values[idx] = InputItem(type, val);
}

void InputManager::setNewKey(const SDL_Event &event,
                             const InputTypeT type) restrict2
{
    int val = -1;
    if (type == InputType::KEYBOARD)
        val = KeyboardConfig::getKeyValueFromEvent(event);
    else if (type == InputType::JOYSTICK && joystick)
        val = joystick->getButtonFromEvent(event);

    if (val != -1)
    {
        addActionKey(mNewKeyIndex, type, val);
        update();
    }
}

void InputManager::unassignKey() restrict2
{
    InputFunction &restrict key = mKey[static_cast<size_t>(mNewKeyIndex)];
    for (size_t i = 0; i < inputFunctionSize; i ++)
    {
        InputItem &restrict val = key.values[i];
        val.type = InputType::UNKNOWN;
        val.value = -1;
    }
    update();
}

#ifndef DYECMD
bool InputManager::handleAssignKey(const SDL_Event &restrict event,
                                   const InputTypeT type) restrict2
{
    if (setupWindow && setupWindow->isWindowVisible() &&
        getNewKeyIndex() > InputAction::NO_VALUE)
    {
        setNewKey(event, type);
        callbackNewKey();
        setNewKeyIndex(InputAction::NO_VALUE);
        return true;
    }
    return false;
}
#else
bool InputManager::handleAssignKey(const SDL_Event &restrict event A_UNUSED,
                                   const InputTypeT type A_UNUSED) restrict2
{
    return false;
}
#endif

bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2
{
    BLOCK_START("InputManager::handleEvent")
    switch (event.type)
    {
        case SDL_KEYDOWN:
        {
            keyboard.refreshActiveKeys();
            updateConditionMask();
            if (handleAssignKey(event, InputType::KEYBOARD))
            {
                BLOCK_END("InputManager::handleEvent")
                return true;
            }

            keyboard.handleActicateKey(event);
            // send straight to gui for certain windows
#ifndef DYECMD
            if (quitDialog || TextDialog::isActive())
            {
                if (guiInput)
                    guiInput->pushInput(event);
                if (gui)
                    gui->handleInput();
                BLOCK_END("InputManager::handleEvent")
                return true;
            }
#endif
            break;
        }
        case SDL_KEYUP:
        {
            keyboard.refreshActiveKeys();
            updateConditionMask();
            keyboard.handleDeActicateKey(event);
            break;
        }
        case SDL_JOYBUTTONDOWN:
        {
            updateConditionMask();
//            joystick.handleActicateButton(event);
            if (handleAssignKey(event, InputType::JOYSTICK))
            {
                BLOCK_END("InputManager::handleEvent")
                return true;
            }
            break;
        }
        case SDL_JOYBUTTONUP:
        {
            updateConditionMask();
//            joystick.handleDeActicateButton(event);
            break;
        }
#ifdef USE_SDL2
        case SDL_FINGERDOWN:
            multiTouchManager.handleFingerDown(event);
            break;
        case SDL_FINGERUP:
            multiTouchManager.handleFingerUp(event);
            break;
#else
#ifdef ANDROID
        case SDL_ACCELEROMETER:
        {
            break;
        }
#endif
#endif
        default:
            break;
    }

    if (guiInput)
        guiInput->pushInput(event);
    if (gui)
    {
        const bool res = gui->handleInput();
        if (res && event.type == SDL_KEYDOWN)
        {
            BLOCK_END("InputManager::handleEvent")
            return true;
        }
    }

    switch (event.type)
    {
        case SDL_KEYDOWN:
            if (triggerAction(keyboard.getActionVector(event)))
            {
                BLOCK_END("InputManager::handleEvent")
                return true;
            }
            break;

        case SDL_JOYBUTTONDOWN:
            if (joystick && joystick->validate())
            {
                if (triggerAction(joystick->getActionVector(event)))
                {
                    BLOCK_END("InputManager::handleEvent")
                    return true;
                }
            }
            break;
#ifdef ANDROID
#ifndef USE_SDL2
        case SDL_ACCELEROMETER:
        {
            break;
        }
#endif
#endif
        default:
            break;
    }

    BLOCK_END("InputManager::handleEvent")
    return false;
}

void InputManager::handleRepeat()
{
    const int time = tick_time;
    keyboard.handleRepeat(time);
    if (joystick)
        joystick->handleRepeat(time);
}

void InputManager::updateConditionMask() restrict2
{
    mMask = 1;
    if (keyboard.isEnabled())
        mMask |= InputCondition::ENABLED;
#ifndef DYECMD
    if ((!chatWindow || !chatWindow->isInputFocused()) &&
        !NpcDialog::isAnyInputFocused() &&
        !InventoryWindow::isAnyInputFocused() &&
        (!tradeWindow || !tradeWindow->isInpupFocused()))
    {
        if (gui)
        {
            FocusHandler *restrict const focus = gui->getFocusHandler();
            if (focus)
            {
                if (!dynamic_cast<TextField*>(focus->getFocused()))
                    mMask |= InputCondition::NOINPUT;
            }
            else
            {
                mMask |= InputCondition::NOINPUT;
            }
        }
        else
        {
            mMask |= InputCondition::NOINPUT;
        }
    }

    if (!BuyDialog::isActive() && !SellDialog::isActive())
        mMask |= InputCondition::NOBUYSELL;

    if (!PlayerInfo::isVending())
        mMask |= InputCondition::NOVENDING;
#ifdef EATHENA_SUPPORT
    if (!PlayerInfo::isInRoom())
#endif
        mMask |= InputCondition::NOROOM;

    const NpcDialog *restrict const dialog = NpcDialog::getActive();
    if (!dialog || !dialog->isTextInputFocused())
        mMask |= InputCondition::NONPCINPUT;
    if (!dialog || dialog->isCloseState())
    {
        mMask |= InputCondition::NONPCDIALOG;
        if (!InventoryWindow::isStorageActive())
            mMask |= InputCondition::NOTALKING;
    }
    if (!setupWindow || !setupWindow->isWindowVisible())
        mMask |= InputCondition::NOSETUP;

    if (Game::instance() && Game::instance()->getValidSpeed())
        mMask |= InputCondition::VALIDSPEED;

    if (Game::instance())
        mMask |= InputCondition::INGAME;

    if (localPlayer)
    {
        if (localPlayer->getFollow().empty())
            mMask |= InputCondition::NOFOLLOW;

        if (!localPlayer->isTrickDead())
            mMask |= InputCondition::NOBLOCK;

        if (localPlayer->isAlive())
            mMask |= InputCondition::ALIVE;
    }
    else
    {
        mMask |= InputCondition::NOFOLLOW;
        mMask |= InputCondition::NOBLOCK;
    }
#endif

    if (!settings.awayMode)
        mMask |= InputCondition::NOAWAY;

    if (gui && !gui->getFocusHandler()->getModalFocused())
        mMask |= InputCondition::NOMODAL;

    if (!settings.disableGameModifiers)
        mMask |= InputCondition::EMODS;

    if (!isActionActive0(InputAction::STOP_ATTACK)
        && !isActionActive0(InputAction::UNTARGET))
    {
        mMask |= InputCondition::NOTARGET;
    }
}

bool InputManager::checkKey(const InputActionData *restrict const key) const
                            restrict2
{
    // logger->log("checkKey mask=%d, condition=%d", mMask, key->condition);
    if (!key || (key->condition & mMask) != key->condition)
        return false;

    return (key->modKeyIndex == InputAction::NO_VALUE
        || isActionActive0(key->modKeyIndex));
}

bool InputManager::invokeKey(const InputActionData *restrict const key,
                             const InputActionT keyNum) restrict2
{
    // no validation to keyNum because it validated in caller

    if (checkKey(key))
    {
        InputEvent evt(keyNum, mMask);
        ActionFuncPtr func = *(inputActionData[
            static_cast<size_t>(keyNum)].action);
        if (func && func(evt))
            return true;
    }
    return false;
}

void InputManager::executeAction(const InputActionT keyNum) restrict2
{
    if (keyNum < InputAction::MOVE_UP || keyNum >= InputAction::TOTAL)
        return;

    InputEvent evt(keyNum, mMask);
    ActionFuncPtr func = *(inputActionData[static_cast<size_t>(
        keyNum)].action);
    if (func)
        func(evt);
}

bool InputManager::executeChatCommand(const std::string &restrict cmd,
                                      const std::string &restrict args,
                                      ChatTab *restrict const tab) restrict2
{
    const StringIntMapCIter it = mChatMap.find(cmd);
    if (it != mChatMap.end())
    {
        ActionFuncPtr func = *(inputActionData[(*it).second].action);
        if (func)
        {
            InputEvent evt(args, tab, mMask);
            func(evt);
            return true;
        }
    }
    return false;
}

bool InputManager::executeRemoteChatCommand(const std::string &restrict cmd,
                                            const std::string &restrict args,
                                            ChatTab *restrict const tab)
                                            restrict2
{
    const StringIntMapCIter it = mChatMap.find(cmd);
    if (it != mChatMap.end())
    {
        const InputActionData &restrict data = inputActionData[(*it).second];
        if (data.isProtected == Protected_true)
            return false;
        ActionFuncPtr func = *(data.action);
        if (func)
        {
            InputEvent evt(args, tab, mMask);
            func(evt);
            return true;
        }
    }
    return false;
}

bool InputManager::executeChatCommand(const InputActionT keyNum,
                                      const std::string &restrict args,
                                      ChatTab *restrict const tab) restrict2
{
    if (static_cast<int>(keyNum) < 0 || keyNum >= InputAction::TOTAL)
        return false;
    ActionFuncPtr func = *(inputActionData[static_cast<size_t>(
        keyNum)].action);
    if (func)
    {
        InputEvent evt(args, tab, mMask);
        func(evt);
        return true;
    }
    return false;
}

void InputManager::updateKeyActionMap(KeyToActionMap &restrict actionMap,
                                      KeyToIdMap &restrict idMap,
                                      KeyTimeMap &restrict keyTimeMap,
                                      const InputTypeT type) const restrict2
{
    actionMap.clear();
    keyTimeMap.clear();

    for (size_t i = 0; i < static_cast<size_t>(InputAction::TOTAL); i ++)
    {
        const InputFunction &restrict key = mKey[i];
        const InputActionData &restrict kd = inputActionData[i];
        if (kd.action)
        {
            for (size_t i2 = 0; i2 < inputFunctionSize; i2 ++)
            {
                const InputItem &restrict ki = key.values[i2];
                if (ki.type == type && ki.value != -1)
                {
                    actionMap[ki.value].push_back(
                        static_cast<InputActionT>(i));
                }
            }
        }
        if (kd.configField && (kd.grp & Input::GRP_GUICHAN))
        {
            for (size_t i2 = 0; i2 < inputFunctionSize; i2 ++)
            {
                const InputItem &restrict ki = key.values[i2];
                if (ki.type == type && ki.value != -1)
                    idMap[ki.value] = static_cast<InputActionT>(i);
            }
        }
        if (kd.configField && (kd.grp & Input::GRP_REPEAT))
        {
            for (size_t i2 = 0; i2 < inputFunctionSize; i2 ++)
            {
                const InputItem &restrict ki = key.values[i2];
                if (ki.type == type && ki.value != -1)
                    keyTimeMap[ki.value] = 0;
            }
        }
    }

    inputActionDataSorter.keys = &inputActionData[0];
    FOR_EACH (KeyToActionMapIter, it, actionMap)
    {
        KeysVector *const keys = &it->second;
        if (keys && keys->size() > 1)
            std::sort(keys->begin(), keys->end(), inputActionDataSorter);
    }
}

bool InputManager::triggerAction(const KeysVector *restrict const ptrs)
                                 restrict2
{
    if (!ptrs)
        return false;

//    logger->log("ptrs: %d", (int)ptrs.size());

    FOR_EACHP (KeysVectorCIter, it, ptrs)
    {
        const InputActionT keyNum = *it;
        if (static_cast<int>(keyNum) < 0 || keyNum >= InputAction::TOTAL)
            continue;

        if (invokeKey(&inputActionData[static_cast<size_t>(keyNum)], keyNum))
            return true;
    }
    return false;
}

InputActionT InputManager::getKeyIndex(const int value,
                                       const int grp,
                                       const InputTypeT type) const restrict2
{
    for (size_t i = 0; i < static_cast<size_t>(InputAction::TOTAL); i++)
    {
        const InputFunction &restrict key = mKey[i];
        const InputActionData &restrict kd = inputActionData[i];
        for (size_t i2 = 0; i2 < inputFunctionSize; i2 ++)
        {
            const InputItem &restrict vali2 = key.values[i2];
            if (value == vali2.value &&
                (grp & kd.grp) != 0 && vali2.type == type)
            {
                return static_cast<InputActionT>(i);
            }
        }
    }
    return InputAction::NO_VALUE;
}

InputActionT InputManager::getActionByKey(const SDL_Event &restrict event)
                                          const restrict2
{
    // for now support only keyboard events
    if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP)
    {
        const InputActionT idx = keyboard.getActionId(event);
        if (static_cast<int>(idx) >= 0 &&
            checkKey(&inputActionData[static_cast<size_t>(idx)]))
        {
            return idx;
        }
    }
    return InputAction::NO_VALUE;
}

void InputManager::addChatCommands(std::list<std::string> &restrict arr)
                                   restrict
{
    const int sz = static_cast<int>(InputAction::TOTAL);
    for (int i = 0; i < sz; i++)
    {
        const InputActionData &restrict ad = inputActionData[i];
        std::string cmd = ad.chatCommand;
        if (!cmd.empty())
        {
            StringVect tokens;
            splitToStringVector(tokens, cmd, '|');
            FOR_EACH (StringVectCIter, it, tokens)
            {
                cmd = std::string("/").append(*it);
                if (ad.useArgs == UseArgs_true)
                    cmd.append(" ");
                arr.push_back(cmd);
            }
        }
    }
}