-
Notifications
You must be signed in to change notification settings - Fork 170
/
keyboard.cpp
377 lines (312 loc) · 10.8 KB
/
keyboard.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
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
#include <cstring>
#include <wayfire/bindings-repository.hpp>
#include <linux/input-event-codes.h>
#include <wayland-server-protocol.h>
#include <xkbcommon/xkbcommon.h>
#include <wayfire/util/log.hpp>
#include "pointer.hpp"
#include "keyboard.hpp"
#include "../core-impl.hpp"
#include "touch.hpp"
#include "input-manager.hpp"
#include "input-method-relay.hpp"
#include "wayfire/signal-definitions.hpp"
void wf::keyboard_t::setup_listeners()
{
on_config_reload = [=] (auto)
{
reload_input_options();
};
wf::get_core().connect(&on_config_reload);
on_key.set_callback([&] (void *data)
{
auto ev = static_cast<wlr_keyboard_key_event*>(data);
auto mode = emit_device_event_signal(ev, &handle->base);
auto& seat = wf::get_core_impl().seat;
wf::get_core().seat->notify_activity();
if (mode == input_event_processing_mode_t::IGNORE)
{
emit_device_post_event_signal(ev, &handle->base);
return;
}
auto is_im_sent = wf::get_core_impl().im_relay->is_im_sent(handle);
if (!is_im_sent)
{
seat->priv->set_keyboard(this);
}
LOGC(IM, "Received key=", ev->keycode, " state=", ev->state, " im=", is_im_sent);
if ((is_im_sent || !handle_keyboard_key(ev->keycode, ev->state)) &&
(mode == input_event_processing_mode_t::FULL))
{
if (!is_im_sent &&
wf::get_core_impl().im_relay->handle_key(handle, ev->time_msec, ev->keycode, ev->state))
{
LOGC(IM, "key=", ev->keycode, " state=", ev->state, " swallowed by IM.");
return emit_device_post_event_signal(ev, &handle->base);
}
if (ev->state == WL_KEYBOARD_KEY_STATE_PRESSED)
{
seat->priv->pressed_keys.insert(ev->keycode);
}
if (ev->state == WL_KEYBOARD_KEY_STATE_RELEASED)
{
if (seat->priv->pressed_keys.count(ev->keycode))
{
seat->priv->pressed_keys.erase(seat->priv->pressed_keys.find(ev->keycode));
} else
{
LOGC(IM, "Key ", ev->keycode, " ignored.");
return emit_device_post_event_signal(ev, &handle->base);
}
}
pre_client_input_event_signal<wlr_keyboard_key_event> pre_ev;
pre_ev.event = ev;
pre_ev.device = &handle->base;
pre_ev.focus_node = seat->priv->keyboard_focus;
wf::get_core().emit(&pre_ev);
if (pre_ev.carried_out)
{
LOGC(IM, "key=", ev->keycode, " state=", ev->state, " swallowed by a plugin in pre-client.");
} else if (seat->priv->keyboard_focus)
{
LOGC(IM, "key=", ev->keycode, " state=", ev->state, " sent to node.");
seat->priv->keyboard_focus->keyboard_interaction()
.handle_keyboard_key(wf::get_core().seat.get(), *ev);
}
} else
{
LOGC(IM, "key=", ev->keycode, " state=", ev->state, " swallowed by a plugin.");
}
emit_device_post_event_signal(ev, &handle->base);
});
on_modifier.set_callback([&] (void *data)
{
auto kbd = static_cast<wlr_keyboard*>(data);
auto seat = wf::get_core().get_current_seat();
mwlr_keyboard_modifiers_event event;
event.time_msec = wf::get_current_time();
emit_device_event_signal(&event, &handle->base);
if (!wf::get_core_impl().im_relay->handle_modifier(kbd))
{
if (!wf::get_core_impl().im_relay->is_im_sent(handle))
{
wlr_seat_set_keyboard(seat, kbd);
}
wlr_seat_keyboard_send_modifiers(seat, &kbd->modifiers);
}
wf::get_core().seat->notify_activity();
emit_device_post_event_signal(&event, &handle->base);
});
on_key.connect(&handle->events.key);
on_modifier.connect(&handle->events.modifiers);
}
wf::keyboard_t::keyboard_t(wlr_input_device *dev) :
handle(wlr_keyboard_from_input_device(dev)), device(dev)
{
model.load_option("input/xkb_model");
variant.load_option("input/xkb_variant");
layout.load_option("input/xkb_layout");
options.load_option("input/xkb_options");
rules.load_option("input/xkb_rules");
repeat_rate.load_option("input/kb_repeat_rate");
repeat_delay.load_option("input/kb_repeat_delay");
// When the configuration options change, mark them as dirty.
// They are applied at the config-reloaded signal.
model.set_callback([=] () { this->dirty_options = true; });
variant.set_callback([=] () { this->dirty_options = true; });
layout.set_callback([=] () { this->dirty_options = true; });
options.set_callback([=] () { this->dirty_options = true; });
rules.set_callback([=] () { this->dirty_options = true; });
repeat_rate.set_callback([=] () { this->dirty_options = true; });
repeat_delay.set_callback([=] () { this->dirty_options = true; });
setup_listeners();
reload_input_options();
wlr_seat_set_keyboard(
wf::get_core().get_current_seat(), wlr_keyboard_from_input_device(dev));
}
uint32_t wf::keyboard_t::get_modifiers()
{
return wlr_keyboard_get_modifiers(handle);
}
static void set_locked_mod(xkb_mod_mask_t *mods, xkb_keymap *keymap, const char *mod)
{
xkb_mod_index_t mod_index = xkb_map_mod_get_index(keymap, mod);
if (mod_index != XKB_MOD_INVALID)
{
*mods |= (uint32_t)1 << mod_index;
}
}
void wf::keyboard_t::reload_input_options()
{
if (!this->dirty_options)
{
return;
}
this->dirty_options = false;
auto ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
/* Copy memory to stack, so that .c_str() is valid */
std::string rules = this->rules;
std::string model = this->model;
std::string layout = this->layout;
std::string variant = this->variant;
std::string options = this->options;
xkb_rule_names names;
names.rules = rules.c_str();
names.model = model.c_str();
names.layout = layout.c_str();
names.variant = variant.c_str();
names.options = options.c_str();
auto keymap = xkb_map_new_from_names(ctx, &names,
XKB_KEYMAP_COMPILE_NO_FLAGS);
if (!keymap)
{
LOGE("Could not create keymap with given configuration:",
" rules=\"", rules, "\" model=\"", model, "\" layout=\"", layout,
"\" variant=\"", variant, "\" options=\"", options, "\"");
// reset to NULL
std::memset(&names, 0, sizeof(names));
keymap = xkb_map_new_from_names(ctx, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);
}
xkb_mod_mask_t locked_mods = 0;
if (wf::get_core_impl().input->locked_mods & KB_MOD_NUM_LOCK)
{
set_locked_mod(&locked_mods, keymap, XKB_MOD_NAME_NUM);
}
if (wf::get_core_impl().input->locked_mods & KB_MOD_CAPS_LOCK)
{
set_locked_mod(&locked_mods, keymap, XKB_MOD_NAME_CAPS);
}
wlr_keyboard_set_keymap(handle, keymap);
xkb_keymap_unref(keymap);
xkb_context_unref(ctx);
wlr_keyboard_set_repeat_info(handle, repeat_rate, repeat_delay);
wlr_keyboard_notify_modifiers(handle, 0, 0, locked_mods, 0);
}
wf::keyboard_t::~keyboard_t()
{}
static bool check_vt_switch(wlr_session *session, uint32_t key, uint32_t mods)
{
if (!session)
{
return false;
}
if (mods ^ (WLR_MODIFIER_ALT | WLR_MODIFIER_CTRL))
{
return false;
}
if ((key < KEY_F1) || (key > KEY_F10))
{
return false;
}
int target_vt = key - KEY_F1 + 1;
wlr_session_change_vt(session, target_vt);
return true;
}
uint32_t wf::keyboard_t::mod_from_key(uint32_t key)
{
xkb_keycode_t keycode = key + 8;
const xkb_keysym_t *keysyms;
auto keysyms_len = xkb_state_key_get_syms(handle->xkb_state, keycode, &keysyms);
for (int i = 0; i < keysyms_len; i++)
{
auto key = keysyms[i];
if ((key == XKB_KEY_Alt_L) || (key == XKB_KEY_Alt_R))
{
return WLR_MODIFIER_ALT;
}
if ((key == XKB_KEY_Control_L) || (key == XKB_KEY_Control_R))
{
return WLR_MODIFIER_CTRL;
}
if ((key == XKB_KEY_Shift_L) || (key == XKB_KEY_Shift_R))
{
return WLR_MODIFIER_SHIFT;
}
if ((key == XKB_KEY_Super_L) || (key == XKB_KEY_Super_R))
{
return WLR_MODIFIER_LOGO;
}
}
return 0;
}
uint32_t wf::keyboard_t::get_locked_mods()
{
uint32_t leds = 0;
for (uint32_t i = 0; i < WLR_LED_COUNT; i++)
{
bool led_active = xkb_state_led_index_is_active(
handle->xkb_state, handle->led_indexes[i]);
if (led_active)
{
leds |= (1 << i);
}
}
uint32_t mods = 0;
if (leds & WLR_LED_NUM_LOCK)
{
mods |= KB_MOD_NUM_LOCK;
}
if (leds & WLR_LED_CAPS_LOCK)
{
mods |= KB_MOD_CAPS_LOCK;
}
return mods;
}
bool wf::keyboard_t::has_only_modifiers()
{
for (size_t i = 0; i < handle->num_keycodes; i++)
{
if (!this->mod_from_key(handle->keycodes[i]))
{
return false;
}
}
return true;
}
bool wf::keyboard_t::handle_keyboard_key(uint32_t key, uint32_t state)
{
using namespace std::chrono;
auto& input = wf::get_core_impl().input;
auto& seat = wf::get_core_impl().seat;
bool handled_in_plugin = false;
auto mod = mod_from_key(key);
input->locked_mods = this->get_locked_mods();
if (state == WLR_KEY_PRESSED)
{
if (check_vt_switch(wf::get_core().session, key, get_modifiers()))
{
return true;
}
bool modifiers_only = !seat->priv->lpointer->has_pressed_buttons() &&
(seat->priv->touch->get_state().fingers.empty()) &&
this->has_only_modifiers();
/* as long as we have pressed only modifiers, we should check for modifier
* bindings on release */
if (mod && modifiers_only)
{
mod_binding_start = steady_clock::now();
mod_binding_key = key;
} else
{
mod_binding_key = 0;
}
handled_in_plugin |= wf::get_core().bindings->handle_key(
wf::keybinding_t{get_modifiers(), key}, mod_binding_key);
} else
{
if (mod_binding_key != 0)
{
int timeout = wf::option_wrapper_t<int>(
"input/modifier_binding_timeout");
auto time_elapsed = duration_cast<milliseconds>(
steady_clock::now() - mod_binding_start);
if ((timeout <= 0) || (time_elapsed < milliseconds(timeout)))
{
wf::get_core().bindings->handle_key(
wf::keybinding_t{get_modifiers() | mod, 0}, mod_binding_key);
}
}
mod_binding_key = 0;
}
return handled_in_plugin;
}