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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
// Copyright 2013 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 "media/base/user_input_monitor.h"
#include <stddef.h>
#include <sys/select.h>
#include <unistd.h>
#include <memory>
#include "base/bind.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/files/file_descriptor_watcher_posix.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/lock.h"
#include "base/task/current_thread.h"
#include "media/base/keyboard_event_counter.h"
#include "third_party/skia/include/core/SkPoint.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/devices/x11/xinput_util.h"
#include "ui/events/keycodes/keyboard_code_conversion_x.h"
#include "ui/gfx/x/x11.h"
#include "ui/gfx/x/x11_types.h"
#include "ui/gfx/x/xinput.h"
namespace media {
namespace {
// This is the actual implementation of event monitoring. It's separated from
// UserInputMonitorLinux since it needs to be deleted on the IO thread.
class UserInputMonitorLinuxCore
: public base::SupportsWeakPtr<UserInputMonitorLinuxCore>,
public base::CurrentThread::DestructionObserver,
public x11::Connection::Delegate {
public:
explicit UserInputMonitorLinuxCore(
const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner);
~UserInputMonitorLinuxCore() override;
// base::CurrentThread::DestructionObserver:
void WillDestroyCurrentMessageLoop() override;
// x11::Connection::Delegate:
bool ShouldContinueStream() const override;
void DispatchXEvent(x11::Event* event) override;
uint32_t GetKeyPressCount() const;
void StartMonitor();
void StartMonitorWithMapping(base::WritableSharedMemoryMapping mapping);
void StopMonitor();
private:
void OnConnectionData();
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
// Used for sharing key press count value.
std::unique_ptr<base::WritableSharedMemoryMapping> key_press_count_mapping_;
//
// The following members should only be accessed on the IO thread.
//
std::unique_ptr<base::FileDescriptorWatcher::Controller> watch_controller_;
std::unique_ptr<x11::Connection> connection_;
KeyboardEventCounter counter_;
DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinuxCore);
};
class UserInputMonitorLinux : public UserInputMonitorBase {
public:
explicit UserInputMonitorLinux(
const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner);
~UserInputMonitorLinux() override;
// Public UserInputMonitor overrides.
uint32_t GetKeyPressCount() const override;
private:
// Private UserInputMonitor overrides.
void StartKeyboardMonitoring() override;
void StartKeyboardMonitoring(
base::WritableSharedMemoryMapping mapping) override;
void StopKeyboardMonitoring() override;
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
UserInputMonitorLinuxCore* core_;
DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux);
};
UserInputMonitorLinuxCore::UserInputMonitorLinuxCore(
const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
: io_task_runner_(io_task_runner) {}
UserInputMonitorLinuxCore::~UserInputMonitorLinuxCore() {
DCHECK(!connection_);
}
void UserInputMonitorLinuxCore::WillDestroyCurrentMessageLoop() {
DCHECK(io_task_runner_->BelongsToCurrentThread());
StopMonitor();
}
bool UserInputMonitorLinuxCore::ShouldContinueStream() const {
return true;
}
void UserInputMonitorLinuxCore::DispatchXEvent(x11::Event* event) {
DCHECK(io_task_runner_->BelongsToCurrentThread());
auto* raw = event->As<x11::Input::RawDeviceEvent>();
if (!raw || (raw->opcode != x11::Input::RawDeviceEvent::RawKeyPress &&
raw->opcode != x11::Input::RawDeviceEvent::RawKeyRelease)) {
return;
}
ui::EventType type = raw->opcode == x11::Input::RawDeviceEvent::RawKeyPress
? ui::ET_KEY_PRESSED
: ui::ET_KEY_RELEASED;
auto key_sym = connection_->KeycodeToKeysym(raw->detail, 0);
ui::KeyboardCode key_code =
ui::KeyboardCodeFromXKeysym(static_cast<uint32_t>(key_sym));
counter_.OnKeyboardEvent(type, key_code);
// Update count value in shared memory.
if (key_press_count_mapping_)
WriteKeyPressMonitorCount(*key_press_count_mapping_, GetKeyPressCount());
}
uint32_t UserInputMonitorLinuxCore::GetKeyPressCount() const {
return counter_.GetKeyPressCount();
}
void UserInputMonitorLinuxCore::StartMonitor() {
DCHECK(io_task_runner_->BelongsToCurrentThread());
// TODO(https://crbug.com/1116414): support UserInputMonitorLinux on
// Ozone/Linux.
if (features::IsUsingOzonePlatform()) {
NOTIMPLEMENTED_LOG_ONCE();
StopMonitor();
return;
}
if (!connection_) {
// TODO(jamiewalch): We should pass the connection in.
if (auto* connection = x11::Connection::Get()) {
connection_ = x11::Connection::Get()->Clone();
} else {
LOG(ERROR) << "Couldn't open X connection";
StopMonitor();
return;
}
}
if (!connection_->xinput().present()) {
LOG(ERROR) << "X Input extension not available.";
StopMonitor();
return;
}
// Let the server know the client XInput version.
connection_->xinput().XIQueryVersion(
{x11::Input::major_version, x11::Input::minor_version});
x11::Input::XIEventMask mask;
ui::SetXinputMask(&mask, x11::Input::RawDeviceEvent::RawKeyPress);
ui::SetXinputMask(&mask, x11::Input::RawDeviceEvent::RawKeyRelease);
connection_->xinput().XISelectEvents(
{connection_->default_root(),
{{x11::Input::DeviceId::AllMaster, {mask}}}});
connection_->Flush();
// Register OnConnectionData() to be called every time there is something to
// read from |connection_|.
watch_controller_ = base::FileDescriptorWatcher::WatchReadable(
connection_->GetFd(),
base::BindRepeating(&UserInputMonitorLinuxCore::OnConnectionData,
base::Unretained(this)));
// Start observing message loop destruction if we start monitoring the first
// event.
base::CurrentThread::Get()->AddDestructionObserver(this);
// Fetch pending events if any.
OnConnectionData();
}
void UserInputMonitorLinuxCore::StartMonitorWithMapping(
base::WritableSharedMemoryMapping mapping) {
StartMonitor();
key_press_count_mapping_ =
std::make_unique<base::WritableSharedMemoryMapping>(std::move(mapping));
}
void UserInputMonitorLinuxCore::StopMonitor() {
DCHECK(io_task_runner_->BelongsToCurrentThread());
watch_controller_.reset();
connection_.reset();
key_press_count_mapping_.reset();
// Stop observing message loop destruction if no event is being monitored.
base::CurrentThread::Get()->RemoveDestructionObserver(this);
}
void UserInputMonitorLinuxCore::OnConnectionData() {
DCHECK(io_task_runner_->BelongsToCurrentThread());
connection_->Dispatch(this);
}
//
// Implementation of UserInputMonitorLinux.
//
UserInputMonitorLinux::UserInputMonitorLinux(
const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
: io_task_runner_(io_task_runner),
core_(new UserInputMonitorLinuxCore(io_task_runner)) {}
UserInputMonitorLinux::~UserInputMonitorLinux() {
if (!io_task_runner_->DeleteSoon(FROM_HERE, core_))
delete core_;
}
uint32_t UserInputMonitorLinux::GetKeyPressCount() const {
return core_->GetKeyPressCount();
}
void UserInputMonitorLinux::StartKeyboardMonitoring() {
io_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&UserInputMonitorLinuxCore::StartMonitor,
core_->AsWeakPtr()));
}
void UserInputMonitorLinux::StartKeyboardMonitoring(
base::WritableSharedMemoryMapping mapping) {
io_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&UserInputMonitorLinuxCore::StartMonitorWithMapping,
core_->AsWeakPtr(), std::move(mapping)));
}
void UserInputMonitorLinux::StopKeyboardMonitoring() {
io_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&UserInputMonitorLinuxCore::StopMonitor,
core_->AsWeakPtr()));
}
} // namespace
std::unique_ptr<UserInputMonitor> UserInputMonitor::Create(
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
return std::make_unique<UserInputMonitorLinux>(io_task_runner);
}
} // namespace media
|