Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: ui/ozone/platform/wayland/wayland_keyboard.cc

Issue 2639053002: [ozone/wayland] Implement basic keyboard handling support (Closed)
Patch Set: Implement basic keyboard handling support Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/ozone/platform/wayland/wayland_keyboard.h"
6
7 #include <sys/mman.h>
8 #include <wayland-client.h>
9
10 #include "ui/base/ui_features.h"
11 #include "ui/events/event.h"
12 #include "ui/events/keycodes/dom/dom_code.h"
13 #include "ui/events/keycodes/dom/keycode_converter.h"
14 #include "ui/events/ozone/evdev/keyboard_util_evdev.h"
15 #include "ui/events/ozone/layout/keyboard_layout_engine.h"
16 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
17 #include "ui/ozone/platform/wayland/wayland_window.h"
18
19 #if BUILDFLAG(USE_XKBCOMMON)
20 #include "ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.h"
21 #endif
22
23 namespace ui {
24
25 WaylandKeyboard::WaylandKeyboard(wl_keyboard* keyboard,
26 const EventDispatchCallback& callback)
27 : obj_(keyboard), callback_(callback) {
28 static const wl_keyboard_listener listener = {
29 &WaylandKeyboard::Keymap, &WaylandKeyboard::Enter,
30 &WaylandKeyboard::Leave, &WaylandKeyboard::Key,
31 &WaylandKeyboard::Modifiers, &WaylandKeyboard::RepeatInfo,
32 };
33
34 wl_keyboard_add_listener(obj_.get(), &listener, this);
35
36 // TODO(tonikitoo): Default auto-repeat to ON here?
37 }
38
39 WaylandKeyboard::~WaylandKeyboard() {}
40
41 void WaylandKeyboard::Keymap(void* data,
42 wl_keyboard* obj,
43 uint32_t format,
44 int32_t fd,
45 uint32_t size) {
spang 2017/01/24 20:41:50 ScopedFD keymap_fd(fd); and remove all the manual
tonikitoo 2017/01/24 21:51:11 Done.
46 if (!data || format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
47 close(fd);
48 return;
49 }
50
51 char* keymap_str = reinterpret_cast<char*>(
52 mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0));
53 if (keymap_str == MAP_FAILED)
54 return;
55
56 bool success =
57 KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()
58 ->SetCurrentLayoutFromBuffer(keymap_str, strlen(keymap_str));
spang 2017/01/24 20:41:50 Please don't strlen a mmap'd file - this is unsafe
tonikitoo 2017/01/24 21:51:11 Ok, I was willing to use 'size' to begin with BUT
Michael Forney 2017/01/24 22:11:47 Hmm, so I looked into this a bit further. All way
spang 2017/01/24 22:24:07 SGTM.
59 DCHECK(success) << "Failed to set the XKB keyboard mapping.";
60 munmap(keymap_str, size);
61 close(fd);
62 }
63
64 void WaylandKeyboard::Enter(void* data,
65 wl_keyboard* obj,
66 uint32_t serial,
67 wl_surface* surface,
68 wl_array* keys) {
69 WaylandWindow::FromSurface(surface)->set_keyboard_focus(true);
70 // TODO(forney): The KeyboardEvdev object should update its state using the
71 // passed key array.
72 }
73
74 void WaylandKeyboard::Leave(void* data,
75 wl_keyboard* obj,
76 uint32_t serial,
77 wl_surface* surface) {
78 WaylandWindow::FromSurface(surface)->set_keyboard_focus(false);
79 }
80
81 void WaylandKeyboard::Key(void* data,
82 wl_keyboard* obj,
83 uint32_t serial,
84 uint32_t time,
85 uint32_t key,
86 uint32_t state) {
87 WaylandKeyboard* keyboard = static_cast<WaylandKeyboard*>(data);
88 DomCode dom_code =
89 KeycodeConverter::NativeKeycodeToDomCode(EvdevCodeToNativeCode(key));
90 if (dom_code == ui::DomCode::NONE)
91 return;
92
93 uint8_t flags = keyboard->modifiers_;
94 DomKey dom_key;
95 KeyboardCode key_code;
96 if (!KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()->Lookup(
97 dom_code, flags, &dom_key, &key_code))
98 return;
99
100 // TODO(tonikitoo): handle repeat here.
spang 2017/01/24 20:41:50 What happens when you get a repeat?
Michael Forney 2017/01/24 20:47:53 The wayland server doesn't send repeats, and expec
tonikitoo 2017/01/24 21:51:11 Exactly.
101 bool down = state == WL_KEYBOARD_KEY_STATE_PRESSED;
102 ui::KeyEvent event(
103 down ? ET_KEY_PRESSED : ET_KEY_RELEASED, key_code, dom_code,
104 keyboard->modifiers_, dom_key,
105 base::TimeTicks() + base::TimeDelta::FromMilliseconds(time));
106 event.set_source_device_id(keyboard->obj_.id());
107 keyboard->callback_.Run(&event);
108 }
109
110 void WaylandKeyboard::Modifiers(void* data,
111 wl_keyboard* obj,
112 uint32_t serial,
113 uint32_t mods_depressed,
114 uint32_t mods_latched,
115 uint32_t mods_locked,
116 uint32_t group) {
117 #if BUILDFLAG(USE_XKBCOMMON)
118 WaylandKeyboard* keyboard = static_cast<WaylandKeyboard*>(data);
119 auto* engine = static_cast<XkbKeyboardLayoutEngine*>(
120 KeyboardLayoutEngineManager::GetKeyboardLayoutEngine());
121 keyboard->modifiers_ =
122 engine->UpdateModifiers(mods_depressed, mods_latched, mods_locked, group);
123 #endif
124 }
125
126 void WaylandKeyboard::RepeatInfo(void* data,
127 wl_keyboard* obj,
128 int32_t rate,
129 int32_t delay) {
130 // TODO(tonikitoo): Implement proper repeat handling.
131 NOTIMPLEMENTED();
132 }
133
134 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698