Commit 1310d8a5 authored by Shawn Gallea's avatar Shawn Gallea Committed by Commit Bot

EXO: Refactor WaylandKeyboardDelegate from server.cc

Move WaylandKeyboardDelegate out of server.cc
This CL only moves code around.

Bug: 896710
Test: Built on ChromeOS
Change-Id: I09a062796c987ca23137acb081b05ea129badeba
Reviewed-on: https://chromium-review.googlesource.com/c/1318576
Commit-Queue: Shawn Gallea <sagallea@google.com>
Reviewed-by: default avatarDaniele Castagna <dcastagna@chromium.org>
Reviewed-by: default avatarDaniel Nicoara <dnicoara@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608159}
parent fc622277
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef COMPONENTS_EXO_KEYBOARD_DELEGATE_H_ #ifndef COMPONENTS_EXO_KEYBOARD_DELEGATE_H_
#define COMPONENTS_EXO_KEYBOARD_DELEGATE_H_ #define COMPONENTS_EXO_KEYBOARD_DELEGATE_H_
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h" #include "base/containers/flat_set.h"
#include "base/time/time.h" #include "base/time/time.h"
......
...@@ -37,6 +37,8 @@ source_set("wayland") { ...@@ -37,6 +37,8 @@ source_set("wayland") {
"server_util.h", "server_util.h",
"wayland_input_delegate.cc", "wayland_input_delegate.cc",
"wayland_input_delegate.h", "wayland_input_delegate.h",
"wayland_keyboard_delegate.cc",
"wayland_keyboard_delegate.h",
"wayland_pointer_delegate.cc", "wayland_pointer_delegate.cc",
"wayland_pointer_delegate.h", "wayland_pointer_delegate.h",
"zcr_notification_shell.cc", "zcr_notification_shell.cc",
......
...@@ -96,6 +96,7 @@ ...@@ -96,6 +96,7 @@
#include "components/exo/touch_stylus_delegate.h" #include "components/exo/touch_stylus_delegate.h"
#include "components/exo/wayland/server_util.h" #include "components/exo/wayland/server_util.h"
#include "components/exo/wayland/wayland_input_delegate.h" #include "components/exo/wayland/wayland_input_delegate.h"
#include "components/exo/wayland/wayland_keyboard_delegate.h"
#include "components/exo/wayland/wayland_pointer_delegate.h" #include "components/exo/wayland/wayland_pointer_delegate.h"
#include "components/exo/wayland/zcr_notification_shell.h" #include "components/exo/wayland/zcr_notification_shell.h"
#include "components/exo/wayland/zwp_text_input_manager.h" #include "components/exo/wayland/zwp_text_input_manager.h"
...@@ -3582,195 +3583,6 @@ const struct wl_pointer_interface pointer_implementation = {pointer_set_cursor, ...@@ -3582,195 +3583,6 @@ const struct wl_pointer_interface pointer_implementation = {pointer_set_cursor,
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// wl_keyboard_interface: // wl_keyboard_interface:
// Keyboard delegate class that accepts events for surfaces owned by the same
// client as a keyboard resource.
class WaylandKeyboardDelegate : public WaylandInputDelegate,
public KeyboardDelegate,
public KeyboardObserver
#if defined(OS_CHROMEOS)
,
public ash::ImeController::Observer
#endif
{
#if BUILDFLAG(USE_XKBCOMMON)
public:
explicit WaylandKeyboardDelegate(wl_resource* keyboard_resource)
: keyboard_resource_(keyboard_resource),
xkb_context_(xkb_context_new(XKB_CONTEXT_NO_FLAGS)) {
#if defined(OS_CHROMEOS)
ash::ImeController* ime_controller = ash::Shell::Get()->ime_controller();
ime_controller->AddObserver(this);
SendNamedLayout(ime_controller->keyboard_layout_name());
#else
SendLayout(nullptr);
#endif
}
#if defined(OS_CHROMEOS)
~WaylandKeyboardDelegate() override {
ash::Shell::Get()->ime_controller()->RemoveObserver(this);
}
#endif
// Overridden from KeyboardDelegate:
void OnKeyboardDestroying(Keyboard* keyboard) override { delete this; }
bool CanAcceptKeyboardEventsForSurface(Surface* surface) const override {
wl_resource* surface_resource = GetSurfaceResource(surface);
// We can accept events for this surface if the client is the same as the
// keyboard.
return surface_resource &&
wl_resource_get_client(surface_resource) == client();
}
void OnKeyboardEnter(
Surface* surface,
const base::flat_map<ui::DomCode, ui::DomCode>& pressed_keys) override {
wl_resource* surface_resource = GetSurfaceResource(surface);
DCHECK(surface_resource);
wl_array keys;
wl_array_init(&keys);
for (const auto& entry : pressed_keys) {
uint32_t* value =
static_cast<uint32_t*>(wl_array_add(&keys, sizeof(uint32_t)));
DCHECK(value);
*value = DomCodeToKey(entry.second);
}
wl_keyboard_send_enter(keyboard_resource_, next_serial(), surface_resource,
&keys);
wl_array_release(&keys);
wl_client_flush(client());
}
void OnKeyboardLeave(Surface* surface) override {
wl_resource* surface_resource = GetSurfaceResource(surface);
DCHECK(surface_resource);
wl_keyboard_send_leave(keyboard_resource_, next_serial(), surface_resource);
wl_client_flush(client());
}
uint32_t OnKeyboardKey(base::TimeTicks time_stamp,
ui::DomCode key,
bool pressed) override {
uint32_t serial = next_serial();
SendTimestamp(time_stamp);
wl_keyboard_send_key(keyboard_resource_, serial,
TimeTicksToMilliseconds(time_stamp), DomCodeToKey(key),
pressed ? WL_KEYBOARD_KEY_STATE_PRESSED
: WL_KEYBOARD_KEY_STATE_RELEASED);
wl_client_flush(client());
return serial;
}
void OnKeyboardModifiers(int modifier_flags) override {
xkb_state_update_mask(xkb_state_.get(),
ModifierFlagsToXkbModifiers(modifier_flags), 0, 0, 0,
0, 0);
wl_keyboard_send_modifiers(
keyboard_resource_, next_serial(),
xkb_state_serialize_mods(xkb_state_.get(), XKB_STATE_MODS_DEPRESSED),
xkb_state_serialize_mods(xkb_state_.get(), XKB_STATE_MODS_LOCKED),
xkb_state_serialize_mods(xkb_state_.get(), XKB_STATE_MODS_LATCHED),
xkb_state_serialize_layout(xkb_state_.get(),
XKB_STATE_LAYOUT_EFFECTIVE));
wl_client_flush(client());
}
#if defined(OS_CHROMEOS)
// Overridden from ImeController::Observer:
void OnCapsLockChanged(bool enabled) override {}
void OnKeyboardLayoutNameChanged(const std::string& layout_name) override {
SendNamedLayout(layout_name);
}
#endif
private:
// Returns the corresponding key given a dom code.
uint32_t DomCodeToKey(ui::DomCode code) const {
// This assumes KeycodeConverter has been built with evdev/xkb codes.
xkb_keycode_t xkb_keycode = static_cast<xkb_keycode_t>(
ui::KeycodeConverter::DomCodeToNativeKeycode(code));
// Keycodes are offset by 8 in Xkb.
DCHECK_GE(xkb_keycode, 8u);
return xkb_keycode - 8;
}
// Returns a set of Xkb modififers given a set of modifier flags.
uint32_t ModifierFlagsToXkbModifiers(int modifier_flags) {
struct {
ui::EventFlags flag;
const char* xkb_name;
} modifiers[] = {
{ui::EF_SHIFT_DOWN, XKB_MOD_NAME_SHIFT},
{ui::EF_CONTROL_DOWN, XKB_MOD_NAME_CTRL},
{ui::EF_ALT_DOWN, XKB_MOD_NAME_ALT},
{ui::EF_COMMAND_DOWN, XKB_MOD_NAME_LOGO},
{ui::EF_ALTGR_DOWN, "Mod5"},
{ui::EF_MOD3_DOWN, "Mod3"},
{ui::EF_NUM_LOCK_ON, XKB_MOD_NAME_NUM},
{ui::EF_CAPS_LOCK_ON, XKB_MOD_NAME_CAPS},
};
uint32_t xkb_modifiers = 0;
for (auto modifier : modifiers) {
if (modifier_flags & modifier.flag) {
xkb_modifiers |=
1 << xkb_keymap_mod_get_index(xkb_keymap_.get(), modifier.xkb_name);
}
}
return xkb_modifiers;
}
#if defined(OS_CHROMEOS)
// Send the named keyboard layout to the client.
void SendNamedLayout(const std::string& layout_name) {
std::string layout_id, layout_variant;
ui::XkbKeyboardLayoutEngine::ParseLayoutName(layout_name, &layout_id,
&layout_variant);
xkb_rule_names names = {.rules = nullptr,
.model = "pc101",
.layout = layout_id.c_str(),
.variant = layout_variant.c_str(),
.options = ""};
SendLayout(&names);
}
#endif
// Send the keyboard layout named by XKB rules to the client.
void SendLayout(const xkb_rule_names* names) {
xkb_keymap_.reset(xkb_keymap_new_from_names(xkb_context_.get(), names,
XKB_KEYMAP_COMPILE_NO_FLAGS));
xkb_state_.reset(xkb_state_new(xkb_keymap_.get()));
std::unique_ptr<char, base::FreeDeleter> keymap_string(
xkb_keymap_get_as_string(xkb_keymap_.get(), XKB_KEYMAP_FORMAT_TEXT_V1));
DCHECK(keymap_string.get());
size_t keymap_size = strlen(keymap_string.get()) + 1;
base::SharedMemory shared_keymap;
bool rv = shared_keymap.CreateAndMapAnonymous(keymap_size);
DCHECK(rv);
memcpy(shared_keymap.memory(), keymap_string.get(), keymap_size);
wl_keyboard_send_keymap(keyboard_resource_,
WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
shared_keymap.handle().GetHandle(), keymap_size);
wl_client_flush(client());
}
// The client who own this keyboard instance.
wl_client* client() const {
return wl_resource_get_client(keyboard_resource_);
}
// Returns the next serial to use for keyboard events.
uint32_t next_serial() const {
return wl_display_next_serial(wl_client_get_display(client()));
}
// The keyboard resource associated with the keyboard.
wl_resource* const keyboard_resource_;
// The Xkb state used for the keyboard.
std::unique_ptr<xkb_context, ui::XkbContextDeleter> xkb_context_;
std::unique_ptr<xkb_keymap, ui::XkbKeymapDeleter> xkb_keymap_;
std::unique_ptr<xkb_state, ui::XkbStateDeleter> xkb_state_;
DISALLOW_COPY_AND_ASSIGN(WaylandKeyboardDelegate);
#endif
};
#if BUILDFLAG(USE_XKBCOMMON) #if BUILDFLAG(USE_XKBCOMMON)
void keyboard_release(wl_client* client, wl_resource* resource) { void keyboard_release(wl_client* client, wl_resource* resource) {
......
// Copyright 2018 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 "components/exo/wayland/wayland_keyboard_delegate.h"
#include <wayland-server-core.h>
#include <wayland-server-protocol-core.h>
#include "base/containers/flat_map.h"
namespace exo {
namespace wayland {
#if BUILDFLAG(USE_XKBCOMMON)
WaylandKeyboardDelegate::WaylandKeyboardDelegate(wl_resource* keyboard_resource)
: keyboard_resource_(keyboard_resource),
xkb_context_(xkb_context_new(XKB_CONTEXT_NO_FLAGS)) {
#if defined(OS_CHROMEOS)
ash::ImeController* ime_controller = ash::Shell::Get()->ime_controller();
ime_controller->AddObserver(this);
SendNamedLayout(ime_controller->keyboard_layout_name());
#else
SendLayout(nullptr);
#endif
}
#if defined(OS_CHROMEOS)
WaylandKeyboardDelegate::~WaylandKeyboardDelegate() {
ash::Shell::Get()->ime_controller()->RemoveObserver(this);
}
#endif
void WaylandKeyboardDelegate::OnKeyboardDestroying(Keyboard* keyboard) {
delete this;
}
bool WaylandKeyboardDelegate::CanAcceptKeyboardEventsForSurface(
Surface* surface) const {
wl_resource* surface_resource = GetSurfaceResource(surface);
// We can accept events for this surface if the client is the same as the
// keyboard.
return surface_resource &&
wl_resource_get_client(surface_resource) == client();
}
void WaylandKeyboardDelegate::OnKeyboardEnter(
Surface* surface,
const base::flat_map<ui::DomCode, ui::DomCode>& pressed_keys) {
wl_resource* surface_resource = GetSurfaceResource(surface);
DCHECK(surface_resource);
wl_array keys;
wl_array_init(&keys);
for (const auto& entry : pressed_keys) {
uint32_t* value =
static_cast<uint32_t*>(wl_array_add(&keys, sizeof(uint32_t)));
DCHECK(value);
*value = DomCodeToKey(entry.second);
}
wl_keyboard_send_enter(keyboard_resource_, next_serial(), surface_resource,
&keys);
wl_array_release(&keys);
wl_client_flush(client());
}
void WaylandKeyboardDelegate::OnKeyboardLeave(Surface* surface) {
wl_resource* surface_resource = GetSurfaceResource(surface);
DCHECK(surface_resource);
wl_keyboard_send_leave(keyboard_resource_, next_serial(), surface_resource);
wl_client_flush(client());
}
uint32_t WaylandKeyboardDelegate::OnKeyboardKey(base::TimeTicks time_stamp,
ui::DomCode key,
bool pressed) {
uint32_t serial = next_serial();
SendTimestamp(time_stamp);
wl_keyboard_send_key(
keyboard_resource_, serial, TimeTicksToMilliseconds(time_stamp),
DomCodeToKey(key),
pressed ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED);
wl_client_flush(client());
return serial;
}
void WaylandKeyboardDelegate::OnKeyboardModifiers(int modifier_flags) {
xkb_state_update_mask(xkb_state_.get(),
ModifierFlagsToXkbModifiers(modifier_flags), 0, 0, 0, 0,
0);
wl_keyboard_send_modifiers(
keyboard_resource_, next_serial(),
xkb_state_serialize_mods(xkb_state_.get(), XKB_STATE_MODS_DEPRESSED),
xkb_state_serialize_mods(xkb_state_.get(), XKB_STATE_MODS_LOCKED),
xkb_state_serialize_mods(xkb_state_.get(), XKB_STATE_MODS_LATCHED),
xkb_state_serialize_layout(xkb_state_.get(), XKB_STATE_LAYOUT_EFFECTIVE));
wl_client_flush(client());
}
#if defined(OS_CHROMEOS)
void WaylandKeyboardDelegate::OnCapsLockChanged(bool enabled) {}
void WaylandKeyboardDelegate::OnKeyboardLayoutNameChanged(
const std::string& layout_name) {
SendNamedLayout(layout_name);
}
#endif
uint32_t WaylandKeyboardDelegate::DomCodeToKey(ui::DomCode code) const {
// This assumes KeycodeConverter has been built with evdev/xkb codes.
xkb_keycode_t xkb_keycode = static_cast<xkb_keycode_t>(
ui::KeycodeConverter::DomCodeToNativeKeycode(code));
// Keycodes are offset by 8 in Xkb.
DCHECK_GE(xkb_keycode, 8u);
return xkb_keycode - 8;
}
uint32_t WaylandKeyboardDelegate::ModifierFlagsToXkbModifiers(
int modifier_flags) {
struct {
ui::EventFlags flag;
const char* xkb_name;
} modifiers[] = {
{ui::EF_SHIFT_DOWN, XKB_MOD_NAME_SHIFT},
{ui::EF_CONTROL_DOWN, XKB_MOD_NAME_CTRL},
{ui::EF_ALT_DOWN, XKB_MOD_NAME_ALT},
{ui::EF_COMMAND_DOWN, XKB_MOD_NAME_LOGO},
{ui::EF_ALTGR_DOWN, "Mod5"},
{ui::EF_MOD3_DOWN, "Mod3"},
{ui::EF_NUM_LOCK_ON, XKB_MOD_NAME_NUM},
{ui::EF_CAPS_LOCK_ON, XKB_MOD_NAME_CAPS},
};
uint32_t xkb_modifiers = 0;
for (auto modifier : modifiers) {
if (modifier_flags & modifier.flag) {
xkb_modifiers |=
1 << xkb_keymap_mod_get_index(xkb_keymap_.get(), modifier.xkb_name);
}
}
return xkb_modifiers;
}
#if defined(OS_CHROMEOS)
void WaylandKeyboardDelegate::SendNamedLayout(const std::string& layout_name) {
std::string layout_id, layout_variant;
ui::XkbKeyboardLayoutEngine::ParseLayoutName(layout_name, &layout_id,
&layout_variant);
xkb_rule_names names = {.rules = nullptr,
.model = "pc101",
.layout = layout_id.c_str(),
.variant = layout_variant.c_str(),
.options = ""};
SendLayout(&names);
}
#endif
void WaylandKeyboardDelegate::SendLayout(const xkb_rule_names* names) {
xkb_keymap_.reset(xkb_keymap_new_from_names(xkb_context_.get(), names,
XKB_KEYMAP_COMPILE_NO_FLAGS));
xkb_state_.reset(xkb_state_new(xkb_keymap_.get()));
std::unique_ptr<char, base::FreeDeleter> keymap_string(
xkb_keymap_get_as_string(xkb_keymap_.get(), XKB_KEYMAP_FORMAT_TEXT_V1));
DCHECK(keymap_string.get());
size_t keymap_size = strlen(keymap_string.get()) + 1;
base::SharedMemory shared_keymap;
bool rv = shared_keymap.CreateAndMapAnonymous(keymap_size);
DCHECK(rv);
memcpy(shared_keymap.memory(), keymap_string.get(), keymap_size);
wl_keyboard_send_keymap(keyboard_resource_, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
shared_keymap.handle().GetHandle(), keymap_size);
wl_client_flush(client());
}
wl_client* WaylandKeyboardDelegate::client() const {
return wl_resource_get_client(keyboard_resource_);
}
uint32_t WaylandKeyboardDelegate::next_serial() const {
return wl_display_next_serial(wl_client_get_display(client()));
}
#endif
} // namespace wayland
} // namespace exo
// Copyright 2018 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 COMPONENTS_EXO_WAYLAND_WAYLAND_KEYBOARD_DELEGATE_H_
#define COMPONENTS_EXO_WAYLAND_WAYLAND_KEYBOARD_DELEGATE_H_
#include "base/containers/flat_map.h"
#include "base/macros.h"
#include "build/buildflag.h"
#include "components/exo/keyboard_delegate.h"
#include "components/exo/keyboard_observer.h"
#include "components/exo/wayland/server_util.h"
#include "components/exo/wayland/wayland_input_delegate.h"
#include "ui/base/ui_features.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#if defined(OS_CHROMEOS)
#include "ash/ime/ime_controller.h"
#include "ash/shell.h"
#include "ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.h"
#endif
#if BUILDFLAG(USE_XKBCOMMON)
#include <xkbcommon/xkbcommon.h>
#include "ui/events/keycodes/scoped_xkb.h" // nogncheck
#endif
struct wl_client;
struct wl_resource;
namespace exo {
namespace wayland {
// Keyboard delegate class that accepts events for surfaces owned by the same
// client as a keyboard resource.
class WaylandKeyboardDelegate : public WaylandInputDelegate,
public KeyboardDelegate,
public KeyboardObserver
#if defined(OS_CHROMEOS)
,
public ash::ImeController::Observer
#endif
{
#if BUILDFLAG(USE_XKBCOMMON)
public:
explicit WaylandKeyboardDelegate(wl_resource* keyboard_resource);
#if defined(OS_CHROMEOS)
~WaylandKeyboardDelegate() override;
#endif
// Overridden from KeyboardDelegate:
void OnKeyboardDestroying(Keyboard* keyboard) override;
bool CanAcceptKeyboardEventsForSurface(Surface* surface) const override;
void OnKeyboardEnter(
Surface* surface,
const base::flat_map<ui::DomCode, ui::DomCode>& pressed_keys) override;
void OnKeyboardLeave(Surface* surface) override;
uint32_t OnKeyboardKey(base::TimeTicks time_stamp,
ui::DomCode key,
bool pressed) override;
void OnKeyboardModifiers(int modifier_flags) override;
#if defined(OS_CHROMEOS)
// Overridden from ImeController::Observer:
void OnCapsLockChanged(bool enabled) override;
void OnKeyboardLayoutNameChanged(const std::string& layout_name) override;
#endif
private:
// Returns the corresponding key given a dom code.
uint32_t DomCodeToKey(ui::DomCode code) const;
// Returns a set of Xkb modififers given a set of modifier flags.
uint32_t ModifierFlagsToXkbModifiers(int modifier_flags);
#if defined(OS_CHROMEOS)
// Send the named keyboard layout to the client.
void SendNamedLayout(const std::string& layout_name);
#endif
// Send the keyboard layout named by XKB rules to the client.
void SendLayout(const xkb_rule_names* names);
// The client who own this keyboard instance.
wl_client* client() const;
// Returns the next serial to use for keyboard events.
uint32_t next_serial() const;
// The keyboard resource associated with the keyboard.
wl_resource* const keyboard_resource_;
// The Xkb state used for the keyboard.
std::unique_ptr<xkb_context, ui::XkbContextDeleter> xkb_context_;
std::unique_ptr<xkb_keymap, ui::XkbKeymapDeleter> xkb_keymap_;
std::unique_ptr<xkb_state, ui::XkbStateDeleter> xkb_state_;
DISALLOW_COPY_AND_ASSIGN(WaylandKeyboardDelegate);
#endif
};
} // namespace wayland
} // namespace exo
#endif // COMPONENTS_EXO_WAYLAND_WAYLAND_KEYBOARD_DELEGATE_H_
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