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 = |
| 57 KeyboardLayoutEngineManager::GetKeyboardLayoutEngine() |
| 58 ->SetCurrentLayoutFromBuffer(keymap_str, strnlen(keymap_str, size)); |
| 59 DCHECK(success) << "Failed to set the XKB keyboard mapping."; |
| 60 munmap(keymap_str, size); |
| 61 } |
| 62 |
| 63 void WaylandKeyboard::Enter(void* data, |
| 64 wl_keyboard* obj, |
| 65 uint32_t serial, |
| 66 wl_surface* surface, |
| 67 wl_array* keys) { |
| 68 WaylandWindow::FromSurface(surface)->set_keyboard_focus(true); |
| 69 } |
| 70 |
| 71 void WaylandKeyboard::Leave(void* data, |
| 72 wl_keyboard* obj, |
| 73 uint32_t serial, |
| 74 wl_surface* surface) { |
| 75 WaylandWindow::FromSurface(surface)->set_keyboard_focus(false); |
| 76 } |
| 77 |
| 78 void WaylandKeyboard::Key(void* data, |
| 79 wl_keyboard* obj, |
| 80 uint32_t serial, |
| 81 uint32_t time, |
| 82 uint32_t key, |
| 83 uint32_t state) { |
| 84 WaylandKeyboard* keyboard = static_cast<WaylandKeyboard*>(data); |
| 85 DomCode dom_code = |
| 86 KeycodeConverter::NativeKeycodeToDomCode(EvdevCodeToNativeCode(key)); |
| 87 if (dom_code == ui::DomCode::NONE) |
| 88 return; |
| 89 |
| 90 uint8_t flags = keyboard->modifiers_; |
| 91 DomKey dom_key; |
| 92 KeyboardCode key_code; |
| 93 if (!KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()->Lookup( |
| 94 dom_code, flags, &dom_key, &key_code)) |
| 95 return; |
| 96 |
| 97 // TODO(tonikitoo): handle repeat here. |
| 98 bool down = state == WL_KEYBOARD_KEY_STATE_PRESSED; |
| 99 ui::KeyEvent event( |
| 100 down ? ET_KEY_PRESSED : ET_KEY_RELEASED, key_code, dom_code, |
| 101 keyboard->modifiers_, dom_key, |
| 102 base::TimeTicks() + base::TimeDelta::FromMilliseconds(time)); |
| 103 event.set_source_device_id(keyboard->obj_.id()); |
| 104 keyboard->callback_.Run(&event); |
| 105 } |
| 106 |
| 107 void WaylandKeyboard::Modifiers(void* data, |
| 108 wl_keyboard* obj, |
| 109 uint32_t serial, |
| 110 uint32_t mods_depressed, |
| 111 uint32_t mods_latched, |
| 112 uint32_t mods_locked, |
| 113 uint32_t group) { |
| 114 #if BUILDFLAG(USE_XKBCOMMON) |
| 115 WaylandKeyboard* keyboard = static_cast<WaylandKeyboard*>(data); |
| 116 auto* engine = static_cast<XkbKeyboardLayoutEngine*>( |
| 117 KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()); |
| 118 keyboard->modifiers_ = |
| 119 engine->UpdateModifiers(mods_depressed, mods_latched, mods_locked, group); |
| 120 #endif |
| 121 } |
| 122 |
| 123 void WaylandKeyboard::RepeatInfo(void* data, |
| 124 wl_keyboard* obj, |
| 125 int32_t rate, |
| 126 int32_t delay) { |
| 127 // TODO(tonikitoo): Implement proper repeat handling. |
| 128 NOTIMPLEMENTED(); |
| 129 } |
| 130 |
| 131 } // namespace ui |
OLD | NEW |