OLD | NEW |
---|---|
(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 "base/files/scoped_file.h" | |
11 #include "ui/base/ui_features.h" | |
12 #include "ui/events/event.h" | |
13 #include "ui/events/keycodes/dom/dom_code.h" | |
14 #include "ui/events/keycodes/dom/keycode_converter.h" | |
15 #include "ui/events/ozone/evdev/keyboard_util_evdev.h" | |
16 #include "ui/events/ozone/layout/keyboard_layout_engine.h" | |
17 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h" | |
18 #include "ui/ozone/platform/wayland/wayland_window.h" | |
19 | |
20 #if BUILDFLAG(USE_XKBCOMMON) | |
21 #include "ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.h" | |
22 #endif | |
23 | |
24 namespace ui { | |
25 | |
26 WaylandKeyboard::WaylandKeyboard(wl_keyboard* keyboard, | |
27 const EventDispatchCallback& callback) | |
28 : obj_(keyboard), callback_(callback) { | |
29 static const wl_keyboard_listener listener = { | |
30 &WaylandKeyboard::Keymap, &WaylandKeyboard::Enter, | |
31 &WaylandKeyboard::Leave, &WaylandKeyboard::Key, | |
32 &WaylandKeyboard::Modifiers, &WaylandKeyboard::RepeatInfo, | |
33 }; | |
34 | |
35 wl_keyboard_add_listener(obj_.get(), &listener, this); | |
36 | |
37 // TODO(tonikitoo): Default auto-repeat to ON here? | |
38 } | |
39 | |
40 WaylandKeyboard::~WaylandKeyboard() {} | |
41 | |
42 void WaylandKeyboard::Keymap(void* data, | |
43 wl_keyboard* obj, | |
44 uint32_t format, | |
45 int32_t raw_fd, | |
46 uint32_t size) { | |
47 base::ScopedFD fd(raw_fd); | |
48 if (!data || format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) | |
49 return; | |
50 | |
51 char* keymap_str = reinterpret_cast<char*>( | |
52 mmap(nullptr, size, PROT_READ, MAP_SHARED, fd.get(), 0)); | |
53 if (keymap_str == MAP_FAILED) | |
54 return; | |
55 | |
56 bool success = KeyboardLayoutEngineManager::GetKeyboardLayoutEngine() | |
57 ->SetCurrentLayoutFromBuffer(keymap_str, size - 1); | |
58 DCHECK(success) << "Failed to set the XKB keyboard mapping."; | |
59 munmap(keymap_str, size); | |
60 } | |
61 | |
62 void WaylandKeyboard::Enter(void* data, | |
63 wl_keyboard* obj, | |
64 uint32_t serial, | |
65 wl_surface* surface, | |
66 wl_array* keys) { | |
67 WaylandWindow::FromSurface(surface)->set_keyboard_focus(true); | |
68 // TODO(forney): The KeyboardEvdev object should update its state using the | |
spang
2017/01/24 22:31:14
Remove this comment.
tonikitoo
2017/01/24 23:16:48
Done.
| |
69 // passed key array. | |
70 } | |
71 | |
72 void WaylandKeyboard::Leave(void* data, | |
73 wl_keyboard* obj, | |
74 uint32_t serial, | |
75 wl_surface* surface) { | |
76 WaylandWindow::FromSurface(surface)->set_keyboard_focus(false); | |
77 } | |
78 | |
79 void WaylandKeyboard::Key(void* data, | |
80 wl_keyboard* obj, | |
81 uint32_t serial, | |
82 uint32_t time, | |
83 uint32_t key, | |
84 uint32_t state) { | |
85 WaylandKeyboard* keyboard = static_cast<WaylandKeyboard*>(data); | |
86 DomCode dom_code = | |
87 KeycodeConverter::NativeKeycodeToDomCode(EvdevCodeToNativeCode(key)); | |
88 if (dom_code == ui::DomCode::NONE) | |
89 return; | |
90 | |
91 uint8_t flags = keyboard->modifiers_; | |
92 DomKey dom_key; | |
93 KeyboardCode key_code; | |
94 if (!KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()->Lookup( | |
95 dom_code, flags, &dom_key, &key_code)) | |
96 return; | |
97 | |
98 // TODO(tonikitoo): handle repeat here. | |
99 bool down = state == WL_KEYBOARD_KEY_STATE_PRESSED; | |
100 ui::KeyEvent event( | |
101 down ? ET_KEY_PRESSED : ET_KEY_RELEASED, key_code, dom_code, | |
102 keyboard->modifiers_, dom_key, | |
103 base::TimeTicks() + base::TimeDelta::FromMilliseconds(time)); | |
104 event.set_source_device_id(keyboard->obj_.id()); | |
105 keyboard->callback_.Run(&event); | |
106 } | |
107 | |
108 void WaylandKeyboard::Modifiers(void* data, | |
109 wl_keyboard* obj, | |
110 uint32_t serial, | |
111 uint32_t mods_depressed, | |
112 uint32_t mods_latched, | |
113 uint32_t mods_locked, | |
114 uint32_t group) { | |
115 #if BUILDFLAG(USE_XKBCOMMON) | |
116 WaylandKeyboard* keyboard = static_cast<WaylandKeyboard*>(data); | |
117 auto* engine = static_cast<XkbKeyboardLayoutEngine*>( | |
118 KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()); | |
119 keyboard->modifiers_ = | |
120 engine->UpdateModifiers(mods_depressed, mods_latched, mods_locked, group); | |
121 #endif | |
122 } | |
123 | |
124 void WaylandKeyboard::RepeatInfo(void* data, | |
125 wl_keyboard* obj, | |
126 int32_t rate, | |
127 int32_t delay) { | |
128 // TODO(tonikitoo): Implement proper repeat handling. | |
129 NOTIMPLEMENTED(); | |
130 } | |
131 | |
132 } // namespace ui | |
OLD | NEW |