Commit 67152390 authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Introduce keycode converter methods between XKB/EVDEV and DomCode.

Then, migrate EvdevCodeToNativeCode and NativeCodeToEvdevCode
into the new functions.

Bug: 1135034
Test: Built locally.
Change-Id: Ibfa0ff101649e1087f547147ca53201067b6ae2f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2461166Reviewed-by: default avatarWez <wez@chromium.org>
Reviewed-by: default avatarMichael Spang <spang@chromium.org>
Reviewed-by: default avatarRobert Kroeger <rjkroege@chromium.org>
Reviewed-by: default avatarKevin Schoedel <kpschoedel@chromium.org>
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819871}
parent 71e0315e
......@@ -11,6 +11,10 @@
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/dom_key.h"
#if defined(OS_LINUX) || defined(OS_CHROMEOS)
#include <linux/input.h>
#endif
namespace ui {
namespace {
......@@ -63,6 +67,46 @@ struct DomKeyMapEntry {
#undef DOM_KEY_MAP
#undef DOM_KEY_UNI
#if defined(OS_LINUX) || defined(OS_CHROMEOS)
// The offset between XKB Keycode and evdev code.
constexpr int kXkbKeycodeOffset = 8;
// TODO(crbug.com/1135034): After migrating native code for
// these platforms from XKB to evdev, use XKB_INVALID_KEYCODE
// (=0xFFFFFFFF) to represent invalid XKB keycode.
// Currently, 0 is returned for backward compatibility.
// Converts XKB keycode to evdev code, based on the mapping
// usually available at /usr/share/X11/xkb/keycodes/evdev.
// See also
// https://xkbcommon.org/doc/current/xkbcommon_8h.html#ac29aee92124c08d1953910ab28ee1997
// for the reference of the history of key mapping.
// Returns KEY_RESERVED for unknown XKB keycode mapping.
int XkbKeycodeToEvdevCode(uint32_t xkb_keycode) {
// There's no mapping from XKB keycode in range [0-7] (inclusive)
// to evdev. Return KEY_RESERVED as an error.
if (xkb_keycode < kXkbKeycodeOffset)
return KEY_RESERVED;
return static_cast<int>(xkb_keycode - kXkbKeycodeOffset);
}
// Converts evdev code into XKB keycode.
// Returns KeycodeConverter::InvalidNativeKeycode() if the given code is in
// the invalid range or KEY_RESERVED.
uint32_t EvdevCodeToXkbKeycode(int evdev_code) {
if (evdev_code < 0 || evdev_code > KEY_MAX || evdev_code == KEY_RESERVED)
return KeycodeConverter::InvalidNativeKeycode();
// TODO(crbug.com/1135034): Move this to EvdevCodeToDomCode on
// migration.
if (evdev_code == KEY_PLAYCD)
evdev_code = KEY_PLAY;
return static_cast<uint32_t>(evdev_code + kXkbKeycodeOffset);
}
#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
} // namespace
// static
......@@ -104,6 +148,36 @@ int KeycodeConverter::DomCodeToNativeKeycode(DomCode code) {
return UsbKeycodeToNativeKeycode(static_cast<uint32_t>(code));
}
#if defined(OS_LINUX) || defined(OS_CHROMEOS)
// static
DomCode KeycodeConverter::XkbKeycodeToDomCode(uint32_t xkb_keycode) {
// Currently XKB keycode is the native keycode.
// TODO(crbug.com/1135034): Replace with evdev.
return NativeKeycodeToDomCode(static_cast<int>(xkb_keycode));
}
// static
uint32_t KeycodeConverter::DomCodeToXkbKeycode(DomCode code) {
// Currently XKB keycode is the native keycode.
// TODO(crbug.com/1135034): Replace with evdev.
return static_cast<uint32_t>(DomCodeToNativeKeycode(code));
}
// static
DomCode KeycodeConverter::EvdevCodeToDomCode(int evdev_code) {
// Currently XKB keycode is the native keycode.
// TODO(crbug.com/1135034): Replace with evdev.
return XkbKeycodeToDomCode(EvdevCodeToXkbKeycode(evdev_code));
}
// static
int KeycodeConverter::DomCodeToEvdevCode(DomCode code) {
// Currently XKB keycode is the native keycode.
// TODO(crbug.com/1135034): Replace with evdev.
return XkbKeycodeToEvdevCode(DomCodeToXkbKeycode(code));
}
#endif
// static
DomCode KeycodeConverter::CodeStringToDomCode(const std::string& code) {
if (code.empty())
......
......@@ -9,7 +9,7 @@
#include <stdint.h>
#include <string>
#include "base/macros.h"
#include "build/build_config.h"
#include "ui/events/keycodes/dom/dom_key.h"
// For reference, the W3C UI Event spec is located at:
......@@ -46,6 +46,10 @@ typedef struct {
// spec (http://www.w3.org/TR/uievents/).
class KeycodeConverter {
public:
KeycodeConverter() = delete;
KeycodeConverter(const KeycodeConverter&) = delete;
KeycodeConverter& operator=(const KeycodeConverter&) = delete;
// Return the value that identifies an invalid native keycode.
static int InvalidNativeKeycode();
......@@ -55,6 +59,20 @@ class KeycodeConverter {
// Convert a DomCode into a native keycode.
static int DomCodeToNativeKeycode(DomCode code);
#if defined(OS_LINUX) || defined(OS_CHROMEOS)
// Convert a XKB keycode into a DomCode.
static DomCode XkbKeycodeToDomCode(uint32_t xkb_keycode);
// Convert a DomCode into a XKB keycode.
static uint32_t DomCodeToXkbKeycode(DomCode code);
// Convert an evdev code into DomCode.
static DomCode EvdevCodeToDomCode(int evdev_code);
// Convert a DomCode into an evdev code.
static int DomCodeToEvdevCode(DomCode code);
#endif
// Convert a UI Events |code| string value into a DomCode.
static DomCode CodeStringToDomCode(const std::string& code);
......@@ -110,9 +128,6 @@ class KeycodeConverter {
static size_t NumKeycodeMapEntriesForTest();
static const KeycodeMapEntry* GetKeycodeMapForTest();
static const char* DomKeyStringForTest(size_t index);
private:
DISALLOW_COPY_AND_ASSIGN(KeycodeConverter);
};
} // namespace ui
......
......@@ -63,8 +63,6 @@ component("evdev") {
"input_injector_evdev.h",
"keyboard_evdev.cc",
"keyboard_evdev.h",
"keyboard_util_evdev.cc",
"keyboard_util_evdev.h",
"mouse_button_map_evdev.cc",
"mouse_button_map_evdev.h",
"stylus_button_event_converter_evdev.cc",
......@@ -152,8 +150,8 @@ component("evdev") {
}
visibility += [
"//ui/ozone/*",
"//ui/chromeos/*",
"//ui/ozone/*",
]
}
......
......@@ -15,7 +15,6 @@
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/events/ozone/evdev/device_event_dispatcher_evdev.h"
#include "ui/events/ozone/evdev/keyboard_util_evdev.h"
namespace ui {
......@@ -99,10 +98,8 @@ void EventConverterEvdevImpl::SetKeyFilter(bool enable_filter,
}
blocked_keys_.set();
for (const DomCode& it : allowed_keys) {
int evdev_code =
NativeCodeToEvdevCode(KeycodeConverter::DomCodeToNativeKeycode(it));
blocked_keys_.reset(evdev_code);
for (const DomCode& code : allowed_keys) {
blocked_keys_.reset(KeycodeConverter::DomCodeToEvdevCode(code));
}
// Release any pressed blocked keys.
......
......@@ -11,10 +11,10 @@
#include "ui/events/event_modifiers.h"
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/events/ozone/evdev/cursor_delegate_evdev.h"
#include "ui/events/ozone/evdev/device_event_dispatcher_evdev.h"
#include "ui/events/ozone/evdev/keyboard_evdev.h"
#include "ui/events/ozone/evdev/keyboard_util_evdev.h"
namespace ui {
......@@ -78,9 +78,7 @@ void InputInjectorEvdev::InjectKeyEvent(DomCode physical_key,
if (physical_key == DomCode::NONE)
return;
int native_keycode = KeycodeConverter::DomCodeToNativeKeycode(physical_key);
int evdev_code = NativeCodeToEvdevCode(native_keycode);
int evdev_code = KeycodeConverter::DomCodeToEvdevCode(physical_key);
dispatcher_->DispatchKeyEvent(KeyEventParams(
kDeviceIdForInjection, ui::EF_NONE, evdev_code, 0 /*scan_code*/, down,
suppress_auto_repeat, EventTimeForNow()));
......
......@@ -13,7 +13,6 @@
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/events/keycodes/keyboard_code_conversion.h"
#include "ui/events/ozone/evdev/keyboard_util_evdev.h"
#include "ui/events/ozone/layout/keyboard_layout_engine.h"
#include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
#include "ui/events/types/event_type.h"
......@@ -119,8 +118,7 @@ void KeyboardEvdev::RefreshModifiers() {
for (int key = 0; key < KEY_CNT; ++key) {
if (!key_state_.test(key))
continue;
DomCode dom_code =
KeycodeConverter::NativeKeycodeToDomCode(EvdevCodeToNativeCode(key));
DomCode dom_code = KeycodeConverter::EvdevCodeToDomCode(key);
if (dom_code == DomCode::NONE)
continue;
DomKey dom_key;
......@@ -141,8 +139,7 @@ void KeyboardEvdev::DispatchKey(unsigned int key,
base::TimeTicks timestamp,
int device_id,
int flags) {
DomCode dom_code =
KeycodeConverter::NativeKeycodeToDomCode(EvdevCodeToNativeCode(key));
DomCode dom_code = KeycodeConverter::EvdevCodeToDomCode(key);
if (dom_code == DomCode::NONE)
return;
int modifier_flags = modifiers_->GetModifierFlags();
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/events/ozone/evdev/keyboard_util_evdev.h"
#include <linux/input.h>
#include "ui/events/keycodes/dom/keycode_converter.h"
namespace ui {
namespace {
const int kXkbKeycodeOffset = 8;
} // namespace
int NativeCodeToEvdevCode(int native_code) {
if (native_code == KeycodeConverter::InvalidNativeKeycode())
return KEY_RESERVED;
return native_code - kXkbKeycodeOffset;
}
int EvdevCodeToNativeCode(int evdev_code) {
if (evdev_code == KEY_RESERVED)
return KeycodeConverter::InvalidNativeKeycode();
if (evdev_code == KEY_PLAYCD)
evdev_code = KEY_PLAY;
return evdev_code + kXkbKeycodeOffset;
}
} // namespace ui
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_EVENTS_OZONE_EVDEV_KEYBOARD_UTIL_EVDEV_H_
#define UI_EVENTS_OZONE_EVDEV_KEYBOARD_UTIL_EVDEV_H_
#include "base/component_export.h"
namespace ui {
int COMPONENT_EXPORT(EVDEV) NativeCodeToEvdevCode(int native_code);
int COMPONENT_EXPORT(EVDEV) EvdevCodeToNativeCode(int evdev_code);
} // namespace ui
#endif // UI_EVENTS_OZONE_EVDEV_KEYBOARD_UTIL_EVDEV_H_
......@@ -20,7 +20,6 @@
#include "ui/events/ozone/evdev/device_event_dispatcher_evdev.h"
#include "ui/events/ozone/evdev/event_device_info.h"
#include "ui/events/ozone/evdev/event_device_util.h"
#include "ui/events/ozone/evdev/keyboard_util_evdev.h"
#include "ui/events/ozone/evdev/libgestures_glue/gesture_property_provider.h"
#include "ui/events/ozone/evdev/libgestures_glue/gesture_timer_provider.h"
#include "ui/gfx/geometry/point_f.h"
......
......@@ -15,8 +15,6 @@
#include "ui/events/base_event_utils.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/events/ozone/evdev/keyboard_util_evdev.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/range/range.h"
#include "ui/ozone/platform/wayland/host/wayland_connection.h"
......
......@@ -16,7 +16,6 @@
#include "ui/events/keycodes/dom/dom_key.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/events/ozone/evdev/keyboard_util_evdev.h"
#include "ui/events/ozone/layout/keyboard_layout_engine.h"
#include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
#include "ui/events/types/event_type.h"
......@@ -189,8 +188,7 @@ void WaylandKeyboard::DispatchKey(uint32_t key,
base::TimeTicks timestamp,
int device_id,
int flags) {
DomCode dom_code =
KeycodeConverter::NativeKeycodeToDomCode(EvdevCodeToNativeCode(key));
DomCode dom_code = KeycodeConverter::EvdevCodeToDomCode(key);
if (dom_code == ui::DomCode::NONE)
return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment