This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 311
/
KeyboardRepeat.cpp
219 lines (180 loc) · 6.36 KB
/
KeyboardRepeat.cpp
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
#include "KeyboardRepeat.hpp"
#include "CommonData.hpp"
#include "EventOutputQueue.hpp"
#include "IOLogWrapper.hpp"
#include "ListHookedKeyboard.hpp"
#include "RemapClass.hpp"
namespace org_pqrs_Karabiner {
List KeyboardRepeat::queue_;
TimerWrapper KeyboardRepeat::fire_timer_;
bool KeyboardRepeat::paused_ = false;
int KeyboardRepeat::id_ = 0;
int KeyboardRepeat::keyRepeat_ = 0;
AutogenId KeyboardRepeat::autogenId_(0);
void KeyboardRepeat::initialize(IOWorkLoop& workloop) {
fire_timer_.initialize(&workloop, nullptr, KeyboardRepeat::fire_timer_callback);
}
void KeyboardRepeat::terminate(void) {
fire_timer_.terminate();
queue_.clear();
}
void KeyboardRepeat::cancel(void) {
fire_timer_.cancelTimeout();
paused_ = false;
queue_.clear();
// Increase id_ at cancel for DependingPressingPeriodKeyToKey to detect keyboardRepeat canceling.
succID();
}
void KeyboardRepeat::primitive_add(EventType eventType,
Flags flags,
KeyCode key,
KeyboardType keyboardType) {
if (key == KeyCode::VK_NONE) return;
// ------------------------------------------------------------
Params_KeyboardEventCallBack params(eventType,
flags,
key,
keyboardType,
true);
queue_.push_back(new Item(params));
}
void KeyboardRepeat::primitive_add(EventType eventType,
Flags flags,
ConsumerKeyCode key) {
if (key == ConsumerKeyCode::VK_NONE) return;
// ------------------------------------------------------------
Params_KeyboardSpecialEventCallback params(eventType,
flags,
key,
true);
queue_.push_back(new Item(params));
}
void KeyboardRepeat::primitive_add(Buttons button) {
// ------------------------------------------------------------
Params_RelativePointerEventCallback params(button,
0,
0,
PointingButton::NONE,
false);
queue_.push_back(new Item(params));
}
int KeyboardRepeat::primitive_start(AutogenId autogenId, int delayUntilRepeat, int keyRepeat) {
keyRepeat_ = keyRepeat;
autogenId_ = autogenId;
fire_timer_.setTimeoutMS(delayUntilRepeat);
paused_ = false;
return succID();
}
void KeyboardRepeat::pause(void) {
if (!paused_) {
fire_timer_.cancelTimeout();
paused_ = true;
}
}
void KeyboardRepeat::resume(void) {
if (paused_) {
fire_timer_.setTimeoutMS(keyRepeat_);
paused_ = false;
}
}
void KeyboardRepeat::set(EventType eventType,
Flags flags,
KeyCode key,
KeyboardType keyboardType,
AutogenId autogenId,
int delayUntilRepeat,
int keyRepeat) {
if (key == KeyCode::VK_NONE) return;
if (eventType == EventType::MODIFY) {
goto cancel;
} else if (eventType == EventType::UP) {
// The repetition of multiple keys is controlled by manual operation.
// So, we ignore it.
if (queue_.size() != 1) return;
// We stop key repeat only when the repeating key is up.
KeyboardRepeat::Item* p = static_cast<KeyboardRepeat::Item*>(queue_.safe_front());
if (p) {
auto params = (p->getParamsBase()).get_Params_KeyboardEventCallBack();
if (params && key == params->key) {
goto cancel;
}
}
} else if (eventType == EventType::DOWN) {
cancel();
primitive_add(eventType, flags, key, keyboardType);
primitive_start(autogenId, delayUntilRepeat, keyRepeat);
IOLOG_DEVEL("KeyboardRepeat::set key:%d flags:0x%x\n", key.get(), flags.get());
} else {
goto cancel;
}
return;
cancel:
cancel();
}
void KeyboardRepeat::set(EventType eventType,
Flags flags,
ConsumerKeyCode key,
AutogenId autogenId,
int delayUntilRepeat,
int keyRepeat) {
if (key == ConsumerKeyCode::VK_NONE) return;
if (eventType == EventType::UP) {
goto cancel;
} else if (eventType == EventType::DOWN) {
if (!key.isRepeatable()) {
goto cancel;
}
cancel();
primitive_add(eventType, flags, key);
primitive_start(autogenId, delayUntilRepeat, keyRepeat);
IOLOG_DEVEL("KeyboardRepeat::set consumer key:%d flags:0x%x\n", key.get(), flags.get());
} else {
goto cancel;
}
return;
cancel:
cancel();
}
void KeyboardRepeat::fire_timer_callback(OSObject* owner, IOTimerEventSource* sender) {
IOLOG_DEVEL("KeyboardRepeat::fire queue_.size = %d\n", static_cast<int>(queue_.size()));
// ----------------------------------------
for (KeyboardRepeat::Item* p = static_cast<KeyboardRepeat::Item*>(queue_.safe_front()); p; p = static_cast<KeyboardRepeat::Item*>(p->getnext())) {
{
{
auto params = (p->getParamsBase()).get_Params_KeyboardEventCallBack();
if (params) {
EventOutputQueue::FireKey::fire(
Params_KeyboardEventCallBack(
params->eventType,
params->flags,
params->key,
params->keyboardType,
queue_.size() == 1 ? true : false),
autogenId_,
PhysicalEventType::DOWN);
}
}
{
auto params = (p->getParamsBase()).get_Params_KeyboardSpecialEventCallback();
if (params) {
EventOutputQueue::FireConsumer::fire(
Params_KeyboardSpecialEventCallback(
params->eventType,
params->flags,
params->key,
queue_.size() == 1 ? true : false),
autogenId_,
PhysicalEventType::DOWN);
}
}
{
auto params = (p->getParamsBase()).get_Params_RelativePointerEventCallback();
if (params) {
EventOutputQueue::FireRelativePointer::fire(autogenId_, PhysicalEventType::DOWN, params->buttons, params->dx, params->dy);
}
}
}
}
fire_timer_.setTimeoutMS(keyRepeat_);
}
}