~aleteoryx/muditaos

ref: 39be35722da1bb602b537ae415af82fb795c5ea8 muditaos/module-gui/gui/input/InputEvent.hpp -rw-r--r-- 6.0 KiB
39be3572 — Mateusz Grzegorzek [EGD-6505] Implement “On when active” Keypad light functionality 5 years ago
                                                                                
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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <sstream>
#include <cstdint>
#include "bsp/keyboard/key_codes.hpp"
#include "common_data/RawKey.hpp"

namespace gui
{

    enum class KeyCode
    {
        KEY_UNDEFINED = 0,
        KEY_LEFT      = static_cast<int>(bsp::KeyCodes::JoystickLeft),
        KEY_UP        = static_cast<int>(bsp::KeyCodes::JoystickUp),
        KEY_RIGHT     = static_cast<int>(bsp::KeyCodes::JoystickRight),
        KEY_DOWN      = static_cast<int>(bsp::KeyCodes::JoystickDown),
        KEY_LF        = static_cast<int>(bsp::KeyCodes::FnLeft),
        KEY_RF        = static_cast<int>(bsp::KeyCodes::FnRight),
        KEY_ENTER     = static_cast<int>(bsp::KeyCodes::JoystickEnter),
        KEY_0         = static_cast<int>(bsp::KeyCodes::NumericKey0),
        KEY_1         = static_cast<int>(bsp::KeyCodes::NumericKey1),
        KEY_2         = static_cast<int>(bsp::KeyCodes::NumericKey2),
        KEY_3         = static_cast<int>(bsp::KeyCodes::NumericKey3),
        KEY_4         = static_cast<int>(bsp::KeyCodes::NumericKey4),
        KEY_5         = static_cast<int>(bsp::KeyCodes::NumericKey5),
        KEY_6         = static_cast<int>(bsp::KeyCodes::NumericKey6),
        KEY_7         = static_cast<int>(bsp::KeyCodes::NumericKey7),
        KEY_8         = static_cast<int>(bsp::KeyCodes::NumericKey8),
        KEY_9         = static_cast<int>(bsp::KeyCodes::NumericKey9),
        KEY_AST       = static_cast<int>(bsp::KeyCodes::NumericKeyAst),
        KEY_PND       = static_cast<int>(bsp::KeyCodes::NumericKeyPnd),
        //	KEY_GREEN,
        //	KEY_RED,
        KEY_VOLUP  = static_cast<int>(bsp::KeyCodes::VolUp),
        KEY_VOLDN  = static_cast<int>(bsp::KeyCodes::VolDown),
        KEY_TORCH  = static_cast<int>(bsp::KeyCodes::Torch),
        SWITCH_UP  = static_cast<int>(bsp::KeyCodes::SSwitchUp),
        SWITCH_MID = static_cast<int>(bsp::KeyCodes::SSwitchMid),
        SWITCH_DN  = static_cast<int>(bsp::KeyCodes::SSwitchDown),
    };

    static const int InvalidNumericKeyCode = -1;

    /// if numeric - returns numeric value, else return: InvalidNumericKeyCode ( -1 )
    [[nodiscard]] inline auto toNumeric(KeyCode key) -> int
    {
        switch (key) {
        case KeyCode::KEY_0:
            return 0;
        case KeyCode::KEY_1:
            return 1;
        case KeyCode::KEY_2:
            return 2;
        case KeyCode::KEY_3:
            return 3;
        case KeyCode::KEY_4:
            return 4;
        case KeyCode::KEY_5:
            return 5;
        case KeyCode::KEY_6:
            return 6;
        case KeyCode::KEY_7:
            return 7;
        case KeyCode::KEY_8:
            return 8;
        case KeyCode::KEY_9:
            return 9;
        default:
            return InvalidNumericKeyCode;
        }
    }

    class InputEvent
    {
      public:
        enum class State
        {
            Undefined        = 0x00,
            keyPressed       = 0x01,
            keyReleasedShort = 0x02,
            keyReleasedLong  = 0x04,
        };

        RawKey key      = {};                     /// RawKey data
        State state     = State::keyPressed;      /// initial translated key state
        KeyCode keyCode = KeyCode::KEY_UNDEFINED; /// initial translated key code

        InputEvent(RawKey key, State state = State::Undefined, KeyCode keyCode = KeyCode::KEY_UNDEFINED);
        [[nodiscard]] auto isShortPress() const -> bool
        {
            return state == State::keyReleasedShort;
        }
        [[nodiscard]] auto isKeyRelease() const -> bool
        {
            return state == State::keyReleasedShort || state == State::keyReleasedLong;
        }
        [[nodiscard]] auto isLongPress() const -> bool
        {
            return state == State::keyReleasedLong;
        }
        [[nodiscard]] auto is(KeyCode code) const -> bool
        {
            return keyCode == code;
        }
        [[nodiscard]] auto str() const -> std::string;
    };

} // namespace gui

[[nodiscard]] inline auto c_str(gui::InputEvent::State state) -> const char *
{
    switch (state) {
    case gui::InputEvent::State::Undefined:
        return "Undefined";
    case gui::InputEvent::State::keyPressed:
        return "keyPressed";
    case gui::InputEvent::State::keyReleasedShort:
        return "keyReleasedShort";
    case gui::InputEvent::State::keyReleasedLong:
        return "keyReleasedLong ";
    }
    return "";
}

[[nodiscard]] inline auto c_str(gui::KeyCode key) -> const char *
{
    switch (key) {
    case gui::KeyCode::KEY_UNDEFINED:
        return "KEY_UNDEFINED";
    case gui::KeyCode::KEY_LEFT:
        return "KEY_LEFT";
    case gui::KeyCode::KEY_UP:
        return "KEY_UP";
    case gui::KeyCode::KEY_RIGHT:
        return "KEY_RIGHT";
    case gui::KeyCode::KEY_DOWN:
        return "KEY_DOWN";
    case gui::KeyCode::KEY_LF:
        return "KEY_LF";
    case gui::KeyCode::KEY_RF:
        return "KEY_RF";
    case gui::KeyCode::KEY_ENTER:
        return "KEY_ENTER";
    case gui::KeyCode::KEY_0:
        return "KEY_0";
    case gui::KeyCode::KEY_1:
        return "KEY_1";
    case gui::KeyCode::KEY_2:
        return "KEY_2";
    case gui::KeyCode::KEY_3:
        return "KEY_3";
    case gui::KeyCode::KEY_4:
        return "KEY_4";
    case gui::KeyCode::KEY_5:
        return "KEY_5";
    case gui::KeyCode::KEY_6:
        return "KEY_6";
    case gui::KeyCode::KEY_7:
        return "KEY_7";
    case gui::KeyCode::KEY_8:
        return "KEY_8";
    case gui::KeyCode::KEY_9:
        return "KEY_9";
    case gui::KeyCode::KEY_AST:
        return "KEY_AST";
    case gui::KeyCode::KEY_PND:
        return "KEY_PND";
    case gui::KeyCode::KEY_VOLUP:
        return "KEY_VOLUP";
    case gui::KeyCode::KEY_VOLDN:
        return "KEY_VOLDN";
    case gui::KeyCode::KEY_TORCH:
        return "KEY_TORCH";
    case gui::KeyCode::SWITCH_UP:
        return "SWITCH_UP";
    case gui::KeyCode::SWITCH_MID:
        return "SWITCH_MID";
    case gui::KeyCode::SWITCH_DN:
        return "SWITCH_DN";
    }
    return "";
}