Commit c8ef1bcf authored by Maksim Sisov's avatar Maksim Sisov Committed by Commit Bot

X11 and Ozone: Switch between X11 and Ozone KbdHook.

Add a factory method for linux that chooses between
the ozone and non-ozone/X11 keyboard hook based on
the IsUsingOzonePlatform condition.

PS: Ozone misses platform kbd hook implementation. This
will be addressed in https://crbug.com/1099225

Bug: 1085700
Change-Id: I0c4845d24c99eb5d4c414acec61515fecac9871d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2266474
Commit-Queue: Nick Yamane <nickdiego@igalia.com>
Reviewed-by: default avatarRobert Kroeger <rjkroege@chromium.org>
Reviewed-by: default avatarThomas Anderson <thomasanderson@chromium.org>
Reviewed-by: default avatarSadrul Chowdhury <sadrul@chromium.org>
Cr-Commit-Position: refs/heads/master@{#784442}
parent 797ea626
...@@ -274,6 +274,7 @@ jumbo_component("events") { ...@@ -274,6 +274,7 @@ jumbo_component("events") {
":gesture_detection", ":gesture_detection",
"//base/third_party/dynamic_annotations", "//base/third_party/dynamic_annotations",
"//skia", "//skia",
"//ui/base:features",
"//ui/gfx", "//ui/gfx",
"//ui/gfx/geometry", "//ui/gfx/geometry",
] ]
...@@ -285,7 +286,10 @@ jumbo_component("events") { ...@@ -285,7 +286,10 @@ jumbo_component("events") {
friend = [ ":*" ] friend = [ ":*" ]
if (use_x11) { if (use_x11) {
sources += [ "x/keyboard_hook_x11.cc" ] sources += [
"x/keyboard_hook_x11.cc",
"x/keyboard_hook_x11.h",
]
} }
if (use_x11 || ozone_platform_x11) { if (use_x11 || ozone_platform_x11) {
...@@ -306,6 +310,12 @@ jumbo_component("events") { ...@@ -306,6 +310,12 @@ jumbo_component("events") {
if (use_ozone || use_x11) { if (use_ozone || use_x11) {
sources += [ "events_default.cc" ] sources += [ "events_default.cc" ]
if (is_linux) {
# TODO(https://crbug.com/1099225): refactor X11 kbd hook and implement
# that for Ozone.
sources += [ "keyboard_hook_linux.cc" ]
}
} }
if (is_win && use_ozone) { if (is_win && use_ozone) {
...@@ -313,7 +323,10 @@ jumbo_component("events") { ...@@ -313,7 +323,10 @@ jumbo_component("events") {
} }
if (use_ozone) { if (use_ozone) {
public += [ "ozone/events_ozone.h" ] public += [
"ozone/events_ozone.h",
"ozone/keyboard_hook_ozone.h",
]
sources += [ sources += [
"ozone/events_ozone.cc", "ozone/events_ozone.cc",
"ozone/keyboard_hook_ozone.cc", "ozone/keyboard_hook_ozone.cc",
......
include_rules = [ include_rules = [
"+ui/base/ui_base_features.h",
"+ui/display", "+ui/display",
"+ui/gfx", "+ui/gfx",
"+ui/latency", "+ui/latency",
......
...@@ -27,6 +27,11 @@ bool KeyboardHookBase::IsKeyLocked(DomCode dom_code) const { ...@@ -27,6 +27,11 @@ bool KeyboardHookBase::IsKeyLocked(DomCode dom_code) const {
return ShouldCaptureKeyEvent(dom_code); return ShouldCaptureKeyEvent(dom_code);
} }
bool KeyboardHookBase::RegisterHook() {
NOTIMPLEMENTED();
return false;
}
bool KeyboardHookBase::ShouldCaptureKeyEvent(DomCode dom_code) const { bool KeyboardHookBase::ShouldCaptureKeyEvent(DomCode dom_code) const {
if (dom_code == DomCode::NONE) if (dom_code == DomCode::NONE)
return false; return false;
......
...@@ -24,6 +24,8 @@ class KeyboardHookBase : public KeyboardHook { ...@@ -24,6 +24,8 @@ class KeyboardHookBase : public KeyboardHook {
// KeyboardHook implementation. // KeyboardHook implementation.
bool IsKeyLocked(DomCode dom_code) const override; bool IsKeyLocked(DomCode dom_code) const override;
virtual bool RegisterHook();
protected: protected:
// Indicates whether |dom_code| should be intercepted by the keyboard hook. // Indicates whether |dom_code| should be intercepted by the keyboard hook.
bool ShouldCaptureKeyEvent(DomCode dom_code) const; bool ShouldCaptureKeyEvent(DomCode dom_code) const;
......
// Copyright 2020 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/keyboard_hook_base.h"
#include <memory>
#include "base/callback.h"
#include "base/optional.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/gfx/native_widget_types.h"
#if defined(USE_X11)
#include "ui/events/x/keyboard_hook_x11.h"
#endif
#if defined(USE_OZONE)
#include "ui/events/ozone/keyboard_hook_ozone.h"
#endif
namespace ui {
// static
std::unique_ptr<KeyboardHook> KeyboardHook::CreateModifierKeyboardHook(
base::Optional<base::flat_set<DomCode>> dom_codes,
gfx::AcceleratedWidget accelerated_widget,
KeyEventCallback callback) {
std::unique_ptr<KeyboardHookBase> keyboard_hook;
#if defined(USE_OZONE)
if (features::IsUsingOzonePlatform()) {
keyboard_hook = std::make_unique<KeyboardHookOzone>(std::move(dom_codes),
std::move(callback));
}
#endif
#if defined(USE_X11)
if (!keyboard_hook) {
keyboard_hook = std::make_unique<KeyboardHookX11>(
std::move(dom_codes), accelerated_widget, std::move(callback));
}
#endif
DCHECK(keyboard_hook);
if (!keyboard_hook->RegisterHook())
return nullptr;
return keyboard_hook;
}
// static
std::unique_ptr<KeyboardHook> KeyboardHook::CreateMediaKeyboardHook(
KeyEventCallback callback) {
return nullptr;
}
} // namespace ui
...@@ -2,34 +2,20 @@ ...@@ -2,34 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "ui/events/keyboard_hook_base.h" #include "ui/events/ozone/keyboard_hook_ozone.h"
#include <utility> #include <utility>
#include "base/callback.h" #include "base/callback.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/optional.h" #include "base/optional.h"
#include "build/build_config.h"
#include "ui/events/event.h" #include "ui/events/event.h"
#include "ui/events/keycodes/dom/dom_code.h" #include "ui/events/keycodes/dom/dom_code.h"
#include "ui/gfx/native_widget_types.h" #include "ui/gfx/native_widget_types.h"
namespace ui { namespace ui {
namespace {
// A default implementation for Ozone platform.
class KeyboardHookOzone : public KeyboardHookBase {
public:
KeyboardHookOzone(base::Optional<base::flat_set<DomCode>> dom_codes,
KeyEventCallback callback);
~KeyboardHookOzone() override;
bool Register();
private:
DISALLOW_COPY_AND_ASSIGN(KeyboardHookOzone);
};
KeyboardHookOzone::KeyboardHookOzone( KeyboardHookOzone::KeyboardHookOzone(
base::Optional<base::flat_set<DomCode>> dom_codes, base::Optional<base::flat_set<DomCode>> dom_codes,
KeyEventCallback callback) KeyEventCallback callback)
...@@ -37,14 +23,13 @@ KeyboardHookOzone::KeyboardHookOzone( ...@@ -37,14 +23,13 @@ KeyboardHookOzone::KeyboardHookOzone(
KeyboardHookOzone::~KeyboardHookOzone() = default; KeyboardHookOzone::~KeyboardHookOzone() = default;
bool KeyboardHookOzone::Register() { bool KeyboardHookOzone::RegisterHook() {
// TODO(680809): Implement system-level keyboard lock feature for ozone. // TODO(680809): Implement system-level keyboard lock feature for ozone.
// Return true to enable browser-level keyboard lock for ozone platform. // Return true to enable browser-level keyboard lock for ozone platform.
return true; return true;
} }
} // namespace #if !defined(OS_LINUX)
// static // static
std::unique_ptr<KeyboardHook> KeyboardHook::CreateModifierKeyboardHook( std::unique_ptr<KeyboardHook> KeyboardHook::CreateModifierKeyboardHook(
base::Optional<base::flat_set<DomCode>> dom_codes, base::Optional<base::flat_set<DomCode>> dom_codes,
...@@ -54,7 +39,7 @@ std::unique_ptr<KeyboardHook> KeyboardHook::CreateModifierKeyboardHook( ...@@ -54,7 +39,7 @@ std::unique_ptr<KeyboardHook> KeyboardHook::CreateModifierKeyboardHook(
std::make_unique<KeyboardHookOzone>(std::move(dom_codes), std::make_unique<KeyboardHookOzone>(std::move(dom_codes),
std::move(callback)); std::move(callback));
if (!keyboard_hook->Register()) if (!keyboard_hook->RegisterHook())
return nullptr; return nullptr;
return keyboard_hook; return keyboard_hook;
...@@ -65,5 +50,6 @@ std::unique_ptr<KeyboardHook> KeyboardHook::CreateMediaKeyboardHook( ...@@ -65,5 +50,6 @@ std::unique_ptr<KeyboardHook> KeyboardHook::CreateMediaKeyboardHook(
KeyEventCallback callback) { KeyEventCallback callback) {
return nullptr; return nullptr;
} }
#endif
} // namespace ui } // namespace ui
// 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 UI_EVENTS_OZONE_KEYBOARD_HOOK_OZONE_H_
#define UI_EVENTS_OZONE_KEYBOARD_HOOK_OZONE_H_
#include "base/callback.h"
#include "base/optional.h"
#include "ui/events/keyboard_hook_base.h"
namespace ui {
// A default implementation for Ozone platform.
class KeyboardHookOzone : public KeyboardHookBase {
public:
KeyboardHookOzone(base::Optional<base::flat_set<DomCode>> dom_codes,
KeyEventCallback callback);
KeyboardHookOzone(const KeyboardHookOzone&) = delete;
KeyboardHookOzone& operator=(const KeyboardHookOzone&) = delete;
~KeyboardHookOzone() override;
// KeyboardHookBase:
bool RegisterHook() override;
};
} // namespace ui
#endif // UI_EVENTS_OZONE_KEYBOARD_HOOK_OZONE_H_
...@@ -2,29 +2,25 @@ ...@@ -2,29 +2,25 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "ui/events/x/keyboard_hook_x11.h"
#include <memory> #include <memory>
#include <utility> #include <utility>
#include <vector>
#include "base/callback.h" #include "base/callback.h"
#include "base/check_op.h" #include "base/check_op.h"
#include "base/containers/flat_set.h" #include "base/containers/flat_set.h"
#include "base/macros.h"
#include "base/optional.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/threading/thread_checker.h"
#include "ui/events/event.h" #include "ui/events/event.h"
#include "ui/events/keyboard_hook_base.h"
#include "ui/events/keycodes/dom/dom_code.h" #include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/keycode_converter.h" #include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/x/x11.h"
#include "ui/gfx/x/x11_types.h"
namespace ui { namespace ui {
namespace { namespace {
static KeyboardHookX11* g_instance = nullptr;
// XGrabKey essentially requires the modifier mask to explicitly be specified. // XGrabKey essentially requires the modifier mask to explicitly be specified.
// You can specify 'AnyModifier' however doing so means the call to XGrabKey // You can specify 'AnyModifier' however doing so means the call to XGrabKey
// will fail if that key has been grabbed with any combination of modifiers. // will fail if that key has been grabbed with any combination of modifiers.
...@@ -47,38 +43,7 @@ const DomCode kDomCodesForLockAllKeys[] = { ...@@ -47,38 +43,7 @@ const DomCode kDomCodesForLockAllKeys[] = {
DomCode::CONTROL_RIGHT, DomCode::SHIFT_RIGHT, DomCode::ALT_RIGHT, DomCode::CONTROL_RIGHT, DomCode::SHIFT_RIGHT, DomCode::ALT_RIGHT,
DomCode::META_RIGHT}; DomCode::META_RIGHT};
// A default implementation for the X11 platform. } // namespace
class KeyboardHookX11 : public KeyboardHookBase {
public:
KeyboardHookX11(base::Optional<base::flat_set<DomCode>> dom_codes,
gfx::AcceleratedWidget accelerated_widget,
KeyEventCallback callback);
~KeyboardHookX11() override;
void Register();
private:
static KeyboardHookX11* instance_;
// Helper methods for setting up key event capture.
void CaptureAllKeys();
void CaptureSpecificKeys();
void CaptureKeyForDomCode(DomCode dom_code);
THREAD_CHECKER(thread_checker_);
// The x11 default display and the owner's native window.
XDisplay* const x_display_ = nullptr;
const gfx::AcceleratedWidget x_window_ = gfx::kNullAcceleratedWidget;
// Tracks the keys that were grabbed.
std::vector<int> grabbed_keys_;
DISALLOW_COPY_AND_ASSIGN(KeyboardHookX11);
};
// static
KeyboardHookX11* KeyboardHookX11::instance_ = nullptr;
KeyboardHookX11::KeyboardHookX11( KeyboardHookX11::KeyboardHookX11(
base::Optional<base::flat_set<DomCode>> dom_codes, base::Optional<base::flat_set<DomCode>> dom_codes,
...@@ -91,8 +56,8 @@ KeyboardHookX11::KeyboardHookX11( ...@@ -91,8 +56,8 @@ KeyboardHookX11::KeyboardHookX11(
KeyboardHookX11::~KeyboardHookX11() { KeyboardHookX11::~KeyboardHookX11() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK_EQ(instance_, this); DCHECK_EQ(g_instance, this);
instance_ = nullptr; g_instance = nullptr;
// Use XUngrabKeys for each key that has been grabbed. XUngrabKeyboard // Use XUngrabKeys for each key that has been grabbed. XUngrabKeyboard
// purportedly releases all keys when called and would not require the nested // purportedly releases all keys when called and would not require the nested
...@@ -105,17 +70,19 @@ KeyboardHookX11::~KeyboardHookX11() { ...@@ -105,17 +70,19 @@ KeyboardHookX11::~KeyboardHookX11() {
} }
} }
void KeyboardHookX11::Register() { bool KeyboardHookX11::RegisterHook() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
// Only one instance of this class can be registered at a time. // Only one instance of this class can be registered at a time.
DCHECK(!instance_); DCHECK(!g_instance);
instance_ = this; g_instance = this;
if (dom_codes().has_value()) if (dom_codes().has_value())
CaptureSpecificKeys(); CaptureSpecificKeys();
else else
CaptureAllKeys(); CaptureAllKeys();
return true;
} }
void KeyboardHookX11::CaptureAllKeys() { void KeyboardHookX11::CaptureAllKeys() {
...@@ -155,26 +122,4 @@ void KeyboardHookX11::CaptureKeyForDomCode(DomCode dom_code) { ...@@ -155,26 +122,4 @@ void KeyboardHookX11::CaptureKeyForDomCode(DomCode dom_code) {
grabbed_keys_.push_back(native_key_code); grabbed_keys_.push_back(native_key_code);
} }
} // namespace
// static
std::unique_ptr<KeyboardHook> KeyboardHook::CreateModifierKeyboardHook(
base::Optional<base::flat_set<DomCode>> dom_codes,
gfx::AcceleratedWidget accelerated_widget,
KeyboardHook::KeyEventCallback callback) {
std::unique_ptr<KeyboardHookX11> keyboard_hook =
std::make_unique<KeyboardHookX11>(
std::move(dom_codes), accelerated_widget, std::move(callback));
keyboard_hook->Register();
return keyboard_hook;
}
// static
std::unique_ptr<KeyboardHook> KeyboardHook::CreateMediaKeyboardHook(
KeyEventCallback callback) {
return nullptr;
}
} // namespace ui } // namespace ui
// Copyright 2020 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_X_KEYBOARD_HOOK_X11_H_
#define UI_EVENTS_X_KEYBOARD_HOOK_X11_H_
#include <vector>
#include "base/optional.h"
#include "base/threading/thread_checker.h"
#include "ui/events/keyboard_hook_base.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/x/x11.h"
#include "ui/gfx/x/x11_types.h"
namespace ui {
// A default implementation for the X11 platform.
class KeyboardHookX11 : public KeyboardHookBase {
public:
KeyboardHookX11(base::Optional<base::flat_set<DomCode>> dom_codes,
gfx::AcceleratedWidget accelerated_widget,
KeyEventCallback callback);
KeyboardHookX11(const KeyboardHookX11&) = delete;
KeyboardHookX11& operator=(const KeyboardHookX11&) = delete;
~KeyboardHookX11() override;
// KeyboardHookBase:
bool RegisterHook() override;
private:
// Helper methods for setting up key event capture.
void CaptureAllKeys();
void CaptureSpecificKeys();
void CaptureKeyForDomCode(DomCode dom_code);
THREAD_CHECKER(thread_checker_);
// The x11 default display and the owner's native window.
XDisplay* const x_display_ = nullptr;
const gfx::AcceleratedWidget x_window_ = gfx::kNullAcceleratedWidget;
// Tracks the keys that were grabbed.
std::vector<int> grabbed_keys_;
};
} // namespace ui
#endif // UI_EVENTS_X_KEYBOARD_HOOK_X11_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