Chromium Code Reviews| Index: media/base/user_input_monitor_linux.cc |
| diff --git a/remoting/host/local_input_monitor_linux.cc b/media/base/user_input_monitor_linux.cc |
| similarity index 59% |
| copy from remoting/host/local_input_monitor_linux.cc |
| copy to media/base/user_input_monitor_linux.cc |
| index 3d85bf8a2943873718f56befd4c7594316e936d7..a80617fb8463c7e54341acadd3d0601108cf174f 100644 |
| --- a/remoting/host/local_input_monitor_linux.cc |
| +++ b/media/base/user_input_monitor_linux.cc |
| @@ -1,8 +1,8 @@ |
| -// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Copyright (c) 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 "remoting/host/local_input_monitor.h" |
| +#include "media/base/user_input_monitor.h" |
| #include <sys/select.h> |
| #include <unistd.h> |
| @@ -20,8 +20,8 @@ |
| #include "base/posix/eintr_wrapper.h" |
| #include "base/single_thread_task_runner.h" |
| #include "base/threading/non_thread_safe.h" |
| -#include "remoting/host/client_session_control.h" |
| #include "third_party/skia/include/core/SkPoint.h" |
| +#include "ui/base/keycodes/keyboard_code_conversion_x.h" |
| // These includes need to be later than dictated by the style guide due to |
| // Xlib header pollution, specifically the min, max, and Status macros. |
| @@ -29,28 +29,29 @@ |
| #include <X11/Xlibint.h> |
| #include <X11/extensions/record.h> |
| -namespace remoting { |
| +namespace media { |
| namespace { |
| -class LocalInputMonitorLinux : public base::NonThreadSafe, |
| - public LocalInputMonitor { |
| +class UserInputMonitorLinux : public base::NonThreadSafe, |
|
DaleCurtis
2013/08/07 17:50:39
Instead of copying this code from LocalInputMonito
|
| + public UserInputMonitor { |
| public: |
| - LocalInputMonitorLinux( |
| - scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| + UserInputMonitorLinux( |
| scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| - base::WeakPtr<ClientSessionControl> client_session_control); |
| - virtual ~LocalInputMonitorLinux(); |
| + scoped_refptr<base::SingleThreadTaskRunner> observer_task_runner, |
| + uint8 events_of_interest, |
| + base::WeakPtr<UserInputObserver> observer); |
| + virtual ~UserInputMonitorLinux(); |
| private: |
| - // The actual implementation resides in LocalInputMonitorLinux::Core class. |
| - class Core |
| - : public base::RefCountedThreadSafe<Core>, |
| - public base::MessagePumpLibevent::Watcher { |
| + // The actual implementation resides in UserInputMonitorLinux::Core class. |
| + class Core : public base::RefCountedThreadSafe<Core>, |
| + public base::MessagePumpLibevent::Watcher { |
| public: |
| - Core(scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| - scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| - base::WeakPtr<ClientSessionControl> client_session_control); |
| + Core(scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| + scoped_refptr<base::SingleThreadTaskRunner> observer_task_runner, |
| + uint8 events_of_interest, |
| + base::WeakPtr<UserInputObserver> observer); |
| void Start(); |
| void Stop(); |
| @@ -71,86 +72,75 @@ class LocalInputMonitorLinux : public base::NonThreadSafe, |
| static void ProcessReply(XPointer self, XRecordInterceptData* data); |
| - // Task runner on which public methods of this class must be called. |
| - scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; |
| - |
| // Task runner on which X Window events are received. |
| scoped_refptr<base::SingleThreadTaskRunner> input_task_runner_; |
| - // Points to the object receiving mouse event notifications and session |
| - // disconnect requests. |
| - base::WeakPtr<ClientSessionControl> client_session_control_; |
| + // Task runner on which public methods of this class must be called. |
| + scoped_refptr<base::SingleThreadTaskRunner> observer_task_runner_; |
| + |
| + // Points to the object receiving notifications. |
| + base::WeakPtr<UserInputObserver> observer_; |
| // Used to receive base::MessagePumpLibevent::Watcher events. |
| base::MessagePumpLibevent::FileDescriptorWatcher controller_; |
| - // True when Alt is pressed. |
| - bool alt_pressed_; |
| - |
| - // True when Ctrl is pressed. |
| - bool ctrl_pressed_; |
| - |
| Display* display_; |
| Display* x_record_display_; |
| XRecordRange* x_record_range_[2]; |
| XRecordContext x_record_context_; |
| + uint8 events_of_interest_; |
| DISALLOW_COPY_AND_ASSIGN(Core); |
| }; |
| scoped_refptr<Core> core_; |
| - DISALLOW_COPY_AND_ASSIGN(LocalInputMonitorLinux); |
| + DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux); |
| }; |
| -LocalInputMonitorLinux::LocalInputMonitorLinux( |
| - scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| +UserInputMonitorLinux::UserInputMonitorLinux( |
| scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| - base::WeakPtr<ClientSessionControl> client_session_control) |
| - : core_(new Core(caller_task_runner, |
| - input_task_runner, |
| - client_session_control)) { |
| + scoped_refptr<base::SingleThreadTaskRunner> observer_task_runner, |
| + uint8 events_of_interest, |
| + base::WeakPtr<UserInputObserver> observer) |
| + : core_(new Core(input_task_runner, |
| + observer_task_runner, |
| + events_of_interest, |
| + observer)) { |
| core_->Start(); |
| } |
| -LocalInputMonitorLinux::~LocalInputMonitorLinux() { |
| - core_->Stop(); |
| -} |
| +UserInputMonitorLinux::~UserInputMonitorLinux() { core_->Stop(); } |
| -LocalInputMonitorLinux::Core::Core( |
| - scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| +UserInputMonitorLinux::Core::Core( |
| scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| - base::WeakPtr<ClientSessionControl> client_session_control) |
| - : caller_task_runner_(caller_task_runner), |
| - input_task_runner_(input_task_runner), |
| - client_session_control_(client_session_control), |
| - alt_pressed_(false), |
| - ctrl_pressed_(false), |
| + scoped_refptr<base::SingleThreadTaskRunner> observer_task_runner, |
| + uint8 events_of_interest, |
| + base::WeakPtr<UserInputObserver> observer) |
| + : input_task_runner_(input_task_runner), |
| + observer_task_runner_(observer_task_runner), |
| + observer_(observer), |
| display_(NULL), |
| x_record_display_(NULL), |
| - x_record_context_(0) { |
| - DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| - DCHECK(client_session_control_.get()); |
| + x_record_context_(0), |
| + events_of_interest_(events_of_interest) { |
| + DCHECK(observer_.get()); |
| x_record_range_[0] = NULL; |
| x_record_range_[1] = NULL; |
| } |
| -void LocalInputMonitorLinux::Core::Start() { |
| - DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| - |
| +void UserInputMonitorLinux::Core::Start() { |
| input_task_runner_->PostTask(FROM_HERE, |
| base::Bind(&Core::StartOnInputThread, this)); |
| } |
| -void LocalInputMonitorLinux::Core::Stop() { |
| - DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| - |
| +void UserInputMonitorLinux::Core::Stop() { |
| input_task_runner_->PostTask(FROM_HERE, |
| base::Bind(&Core::StopOnInputThread, this)); |
| } |
| -LocalInputMonitorLinux::Core::~Core() { |
| +UserInputMonitorLinux::Core::~Core() { |
| DCHECK(!display_); |
| DCHECK(!x_record_display_); |
| DCHECK(!x_record_range_[0]); |
| @@ -158,7 +148,7 @@ LocalInputMonitorLinux::Core::~Core() { |
| DCHECK(!x_record_context_); |
| } |
| -void LocalInputMonitorLinux::Core::StartOnInputThread() { |
| +void UserInputMonitorLinux::Core::StartOnInputThread() { |
| DCHECK(input_task_runner_->BelongsToCurrentThread()); |
| DCHECK(!display_); |
| DCHECK(!x_record_display_); |
| @@ -190,21 +180,29 @@ void LocalInputMonitorLinux::Core::StartOnInputThread() { |
| LOG(ERROR) << "XRecordAllocRange failed."; |
| return; |
| } |
| - x_record_range_[0]->device_events.first = MotionNotify; |
| - x_record_range_[0]->device_events.last = MotionNotify; |
| - x_record_range_[1]->device_events.first = KeyPress; |
| - x_record_range_[1]->device_events.last = KeyRelease; |
| - XRecordClientSpec client_spec = XRecordAllClients; |
| + if (events_of_interest_ & MOUSE_MOVEMENT) { |
| + x_record_range_[0]->device_events.first = MotionNotify; |
| + x_record_range_[0]->device_events.last = MotionNotify; |
| + } |
| + if (events_of_interest_ & KEYBOARD_EVENT) { |
| + x_record_range_[1]->device_events.first = KeyPress; |
| + x_record_range_[1]->device_events.last = KeyRelease; |
| + } |
| - x_record_context_ = XRecordCreateContext( |
| - x_record_display_, 0, &client_spec, 1, x_record_range_, |
| - arraysize(x_record_range_)); |
| + XRecordClientSpec client_spec = XRecordAllClients; |
| + x_record_context_ = XRecordCreateContext(x_record_display_, |
| + 0, |
| + &client_spec, |
| + 1, |
| + x_record_range_, |
| + arraysize(x_record_range_)); |
| if (!x_record_context_) { |
| LOG(ERROR) << "XRecordCreateContext failed."; |
| return; |
| } |
| - if (!XRecordEnableContextAsync(x_record_display_, x_record_context_, |
| + if (!XRecordEnableContextAsync(x_record_display_, |
| + x_record_context_, |
| &Core::ProcessReply, |
| reinterpret_cast<XPointer>(this))) { |
| LOG(ERROR) << "XRecordEnableContextAsync failed."; |
| @@ -232,7 +230,7 @@ void LocalInputMonitorLinux::Core::StartOnInputThread() { |
| } |
| } |
| -void LocalInputMonitorLinux::Core::StopOnInputThread() { |
| +void UserInputMonitorLinux::Core::StopOnInputThread() { |
| DCHECK(input_task_runner_->BelongsToCurrentThread()); |
| // Context must be disabled via the control channel because we can't send |
| @@ -266,7 +264,7 @@ void LocalInputMonitorLinux::Core::StopOnInputThread() { |
| } |
| } |
| -void LocalInputMonitorLinux::Core::OnFileCanReadWithoutBlocking(int fd) { |
| +void UserInputMonitorLinux::Core::OnFileCanReadWithoutBlocking(int fd) { |
| DCHECK(input_task_runner_->BelongsToCurrentThread()); |
| // Fetch pending events if any. |
| @@ -276,39 +274,39 @@ void LocalInputMonitorLinux::Core::OnFileCanReadWithoutBlocking(int fd) { |
| } |
| } |
| -void LocalInputMonitorLinux::Core::OnFileCanWriteWithoutBlocking(int fd) { |
| +void UserInputMonitorLinux::Core::OnFileCanWriteWithoutBlocking(int fd) { |
| NOTREACHED(); |
| } |
| -void LocalInputMonitorLinux::Core::ProcessXEvent(xEvent* event) { |
| +void UserInputMonitorLinux::Core::ProcessXEvent(xEvent* event) { |
| DCHECK(input_task_runner_->BelongsToCurrentThread()); |
| if (event->u.u.type == MotionNotify) { |
| SkIPoint position(SkIPoint::Make(event->u.keyButtonPointer.rootX, |
| event->u.keyButtonPointer.rootY)); |
| - caller_task_runner_->PostTask( |
| - FROM_HERE, base::Bind(&ClientSessionControl::OnLocalMouseMoved, |
| - client_session_control_, |
| - position)); |
| + observer_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&UserInputObserver::OnMouseMoved, observer_, position)); |
| } else { |
| - int key_code = event->u.u.detail; |
| - bool down = event->u.u.type == KeyPress; |
| - KeySym key_sym = XkbKeycodeToKeysym(display_, key_code, 0, 0); |
| - if (key_sym == XK_Control_L || key_sym == XK_Control_R) { |
| - ctrl_pressed_ = down; |
| - } else if (key_sym == XK_Alt_L || key_sym == XK_Alt_R) { |
| - alt_pressed_ = down; |
| - } else if (key_sym == XK_Escape && down && alt_pressed_ && ctrl_pressed_) { |
| - caller_task_runner_->PostTask( |
| - FROM_HERE, base::Bind(&ClientSessionControl::DisconnectSession, |
| - client_session_control_)); |
| - } |
| + ui::EventType type; |
| + if (event->u.u.type == KeyPress) |
| + type = ui::ET_KEY_PRESSED; |
| + else if (event->u.u.type == KeyRelease) |
| + type = ui::ET_KEY_RELEASED; |
| + else |
| + return; |
| + KeySym key_sym = XkbKeycodeToKeysym(display_, event->u.u.detail, 0, 0); |
| + ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym); |
| + observer_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind( |
| + &UserInputObserver::OnKeyboardEvent, observer_, type, key_code)); |
| } |
| } |
| // static |
| -void LocalInputMonitorLinux::Core::ProcessReply(XPointer self, |
| - XRecordInterceptData* data) { |
| +void UserInputMonitorLinux::Core::ProcessReply(XPointer self, |
| + XRecordInterceptData* data) { |
| if (data->category == XRecordFromServer) { |
| xEvent* event = reinterpret_cast<xEvent*>(data->data); |
| reinterpret_cast<Core*>(self)->ProcessXEvent(event); |
| @@ -318,15 +316,14 @@ void LocalInputMonitorLinux::Core::ProcessReply(XPointer self, |
| } // namespace |
| -scoped_ptr<LocalInputMonitor> LocalInputMonitor::Create( |
| - scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| +scoped_ptr<UserInputMonitor> UserInputMonitor::Create( |
| scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| - base::WeakPtr<ClientSessionControl> client_session_control) { |
| - return scoped_ptr<LocalInputMonitor>( |
| - new LocalInputMonitorLinux(caller_task_runner, |
| - input_task_runner, |
| - client_session_control)); |
| + scoped_refptr<base::SingleThreadTaskRunner> observer_task_runner, |
| + uint8 events_of_interest, |
| + base::WeakPtr<UserInputObserver> observer) { |
| + return scoped_ptr<UserInputMonitor>(new UserInputMonitorLinux( |
| + input_task_runner, observer_task_runner, events_of_interest, observer)); |
| } |
| -} // namespace remoting |
| +} // namespace media |