1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_OZONE_PLATFORM_WAYLAND_HOST_WAYLAND_KEYBOARD_H_
#define UI_OZONE_PLATFORM_WAYLAND_HOST_WAYLAND_KEYBOARD_H_
#include <cstdint>
#include <optional>
#include "base/containers/flat_set.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "ui/base/buildflags.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/events/ozone/keyboard/event_auto_repeat_handler.h"
#include "ui/events/types/event_type.h"
#include "ui/ozone/common/base_keyboard_hook.h"
#include "ui/ozone/platform/wayland/common/wayland_object.h"
struct zwp_keyboard_shortcuts_inhibitor_v1;
namespace ui {
class KeyboardLayoutEngine;
class KeyEvent;
class WaylandConnection;
class WaylandWindow;
#if BUILDFLAG(USE_XKBCOMMON)
class XkbKeyboardLayoutEngine;
#endif
class WaylandKeyboard : public EventAutoRepeatHandler::Delegate {
public:
class Delegate;
class ZCRExtendedKeyboard;
enum class KeyEventKind {
kPeekKey, // Originated by extended_keyboard::peek_key.
kKey, // Originated by wl_keyboard::key.
};
// Property key to annotate wayland serial to a KeyEvent.
static constexpr char kPropertyWaylandSerial[] = "_keyevent_wayland_serial_";
WaylandKeyboard(wl_keyboard* keyboard,
zcr_keyboard_extension_v1* keyboard_extension_v1,
WaylandConnection* connection,
KeyboardLayoutEngine* keyboard_layout_engine,
Delegate* delegate);
virtual ~WaylandKeyboard();
uint32_t id() const { return obj_.id(); }
int device_id() const { return obj_.id(); }
// Called when it turns out that KeyEvent is not handled.
void OnUnhandledKeyEvent(const KeyEvent& key_event);
// Creates a new PlatformKeyboardHook/shortcuts inhibitor for |window|. See
// comments in this function's definition for more context.
std::unique_ptr<PlatformKeyboardHook> CreateKeyboardHook(
WaylandWindow* window,
std::optional<base::flat_set<DomCode>> dom_codes,
PlatformKeyboardHook::KeyEventCallback callback);
wl::Object<zwp_keyboard_shortcuts_inhibitor_v1> CreateShortcutsInhibitor(
WaylandWindow* window);
private:
using LayoutEngine =
#if BUILDFLAG(USE_XKBCOMMON)
XkbKeyboardLayoutEngine
#else
KeyboardLayoutEngine
#endif
;
// wl_keyboard_listener callbacks:
static void OnKeymap(void* data,
wl_keyboard* keyboard,
uint32_t format,
int32_t fd,
uint32_t size);
static void OnEnter(void* data,
wl_keyboard* keyboard,
uint32_t serial,
wl_surface* surface,
wl_array* keys);
static void OnLeave(void* data,
wl_keyboard* keyboard,
uint32_t serial,
wl_surface* surface);
static void OnKey(void* data,
wl_keyboard* keyboard,
uint32_t serial,
uint32_t time,
uint32_t key,
uint32_t state);
static void OnModifiers(void* data,
wl_keyboard* keyboard,
uint32_t serial,
uint32_t mods_depressed,
uint32_t mods_latched,
uint32_t mods_locked,
uint32_t group);
static void OnRepeatInfo(void* data,
wl_keyboard* keyboard,
int32_t rate,
int32_t delay);
// wl_callback_listener callbacks:
static void OnSyncDone(void* data,
struct wl_callback* callback,
uint32_t time);
// Callback for wl_keyboard::key and extended_keyboard::peek_key.
void ProcessKey(uint32_t serial,
uint32_t time,
uint32_t key,
uint32_t state,
KeyEventKind kind);
// Dispatches the key event.
void DispatchKey(unsigned int key,
unsigned int scan_code,
bool down,
bool repeat,
std::optional<uint32_t> serial,
base::TimeTicks timestamp,
int device_id,
int flags,
KeyEventKind kind);
// EventAutoRepeatHandler::Delegate
void FlushInput(base::OnceClosure closure) override;
void DispatchKey(unsigned int key,
unsigned int scan_code,
bool down,
bool repeat,
base::TimeTicks timestamp,
int device_id,
int flags) override;
wl::Object<wl_keyboard> obj_;
std::unique_ptr<ZCRExtendedKeyboard> extended_keyboard_;
const raw_ptr<WaylandConnection> connection_;
const raw_ptr<Delegate> delegate_;
// Key repeat handler.
EventAutoRepeatHandler auto_repeat_handler_;
base::OnceClosure auto_repeat_closure_;
wl::Object<wl_callback> sync_callback_;
raw_ptr<LayoutEngine> layout_engine_;
};
class WaylandKeyboard::Delegate {
public:
virtual void OnKeyboardFocusChanged(WaylandWindow* window, bool focused) = 0;
virtual void OnKeyboardModifiersChanged(int modifiers) = 0;
// Returns a mask of ui::PostDispatchAction indicating how the event was
// dispatched.
virtual uint32_t OnKeyboardKeyEvent(EventType type,
DomCode dom_code,
bool repeat,
std::optional<uint32_t> serial,
base::TimeTicks timestamp,
int device_id,
WaylandKeyboard::KeyEventKind kind) = 0;
// Dispatches a synthesized key event for `dom_code` and `timestamp`. Keyboard
// focus is temporarily transferred to `window` during this function.
virtual void OnSynthesizedKeyPressEvent(WaylandWindow* window,
DomCode dom_code,
base::TimeTicks timestamp) = 0;
protected:
// Prevent deletion through a WaylandKeyboard::Delegate pointer.
virtual ~Delegate() = default;
};
} // namespace ui
#endif // UI_OZONE_PLATFORM_WAYLAND_HOST_WAYLAND_KEYBOARD_H_
|