-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathkeyboard.h
117 lines (81 loc) · 2.89 KB
/
keyboard.h
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
#pragma once
#include <UI.h>
#include "keymap.h"
struct KeyInfo;
struct EvKeyInfo;
struct terminal_t;
struct Layout;
class KeyboardRenderObject;
using KeyboardCallback = std::function<void(int)>;
struct KeyboardParams {
const Layout& layout;
const KeyMap& keymap;
std::chrono::milliseconds repeatDelay = std::chrono::seconds(1);
std::chrono::milliseconds repeatTime = std::chrono::milliseconds(100);
};
/// Keyboard widget, displays a virtual keyboard of the given layout.
/// Also interprets physical key presses.
class Keyboard : public rmlib::Widget<KeyboardRenderObject> {
public:
// default size:
constexpr static int key_height = 64;
constexpr static int key_width = 128;
Keyboard(struct terminal_t* term, KeyboardParams params, KeyboardCallback cb)
: term(term), params(params), callback(std::move(cb)) {}
std::unique_ptr<rmlib::RenderObject> createRenderObject() const;
private:
friend class KeyboardRenderObject;
struct terminal_t* term;
KeyboardParams params;
KeyboardCallback callback;
};
/// Keyboard render object
///
///
class KeyboardRenderObject : public rmlib::LeafRenderObject<Keyboard> {
using TimeSource = std::chrono::steady_clock;
public:
KeyboardRenderObject(const Keyboard& keyboard);
void update(const Keyboard& keyboard);
protected:
void doRebuild(rmlib::AppContext& ctx,
const rmlib::BuildContext& /*buildContext*/) final;
rmlib::Size doLayout(const rmlib::Constraints& constraints) final;
rmlib::UpdateRegion doDraw(rmlib::Rect rect, rmlib::Canvas& canvas) final;
void handleInput(const rmlib::input::Event& ev) final;
private:
void updateRepeat();
void updateLayout();
void sendKeyDown(const KeyInfo& key, bool repeat = false);
void sendKeyDown(const EvKeyInfo& key, bool repeat = false);
void sendKeyDown(int scancode, bool shift, bool alt, bool ctrl);
const KeyInfo* getKey(const rmlib::Point& point);
void clearSticky();
template<typename Ev>
void handleTouchEvent(const Ev& ev);
void handleKeyEvent(const rmlib::input::KeyEvent& ev);
rmlib::UpdateRegion drawKey(rmlib::Point pos,
const KeyInfo& key,
rmlib::Canvas& canvas);
// fields
float keyWidth{};
float keyHeight{};
struct KeyState {
int slot = -1;
bool stuck = false; // Used for mod keys, get stuck after tap.
bool held = false; // Used for mod keys, held down after long press.
bool dirty = false;
bool isDown() const { return slot != -1 || stuck || held; }
TimeSource::time_point nextRepeat;
};
const KeyInfo* shiftKey = nullptr;
const KeyInfo* ctrlKey = nullptr;
const KeyInfo* altKey = nullptr;
std::unordered_map<const KeyInfo*, KeyState> keyState;
struct PhysKeyState {
bool down = false;
TimeSource::time_point nextRepeat;
};
std::unordered_map<const EvKeyInfo*, PhysKeyState> physKeyState;
rmlib::TimerHandle repeatTimer;
};