1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/events/ozone/evdev/event_factory_evdev.h"
8 #include "base/task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "base/threading/worker_pool.h"
11 #include "base/time/time.h"
12 #include "base/trace_event/trace_event.h"
13 #include "ui/events/devices/device_data_manager.h"
14 #include "ui/events/devices/input_device.h"
15 #include "ui/events/event_utils.h"
16 #include "ui/events/ozone/device/device_event.h"
17 #include "ui/events/ozone/device/device_manager.h"
18 #include "ui/events/ozone/evdev/cursor_delegate_evdev.h"
19 #include "ui/events/ozone/evdev/input_controller_evdev.h"
20 #include "ui/events/ozone/evdev/input_device_factory_evdev.h"
21 #include "ui/events/ozone/evdev/input_device_factory_evdev_proxy.h"
22 #include "ui/events/ozone/evdev/input_injector_evdev.h"
28 // Thread safe dispatcher proxy for EventFactoryEvdev.
30 // This is used on the device I/O thread for dispatching to UI.
31 class ProxyDeviceEventDispatcher
: public DeviceEventDispatcherEvdev
{
33 ProxyDeviceEventDispatcher(
34 scoped_refptr
<base::SingleThreadTaskRunner
> ui_thread_runner
,
35 base::WeakPtr
<EventFactoryEvdev
> event_factory_evdev
)
36 : ui_thread_runner_(ui_thread_runner
),
37 event_factory_evdev_(event_factory_evdev
) {}
38 ~ProxyDeviceEventDispatcher() override
{}
40 // DeviceEventDispatcher:
41 void DispatchKeyEvent(const KeyEventParams
& params
) override
{
42 ui_thread_runner_
->PostTask(FROM_HERE
,
43 base::Bind(&EventFactoryEvdev::DispatchKeyEvent
,
44 event_factory_evdev_
, params
));
47 void DispatchMouseMoveEvent(const MouseMoveEventParams
& params
) override
{
48 ui_thread_runner_
->PostTask(
49 FROM_HERE
, base::Bind(&EventFactoryEvdev::DispatchMouseMoveEvent
,
50 event_factory_evdev_
, params
));
53 void DispatchMouseButtonEvent(const MouseButtonEventParams
& params
) override
{
54 ui_thread_runner_
->PostTask(
55 FROM_HERE
, base::Bind(&EventFactoryEvdev::DispatchMouseButtonEvent
,
56 event_factory_evdev_
, params
));
59 void DispatchMouseWheelEvent(const MouseWheelEventParams
& params
) override
{
60 ui_thread_runner_
->PostTask(
61 FROM_HERE
, base::Bind(&EventFactoryEvdev::DispatchMouseWheelEvent
,
62 event_factory_evdev_
, params
));
65 void DispatchScrollEvent(const ScrollEventParams
& params
) override
{
66 ui_thread_runner_
->PostTask(
67 FROM_HERE
, base::Bind(&EventFactoryEvdev::DispatchScrollEvent
,
68 event_factory_evdev_
, params
));
71 void DispatchTouchEvent(const TouchEventParams
& params
) override
{
72 ui_thread_runner_
->PostTask(
73 FROM_HERE
, base::Bind(&EventFactoryEvdev::DispatchTouchEvent
,
74 event_factory_evdev_
, params
));
77 void DispatchKeyboardDevicesUpdated(
78 const std::vector
<KeyboardDevice
>& devices
) override
{
79 ui_thread_runner_
->PostTask(
81 base::Bind(&EventFactoryEvdev::DispatchKeyboardDevicesUpdated
,
82 event_factory_evdev_
, devices
));
84 void DispatchTouchscreenDevicesUpdated(
85 const std::vector
<TouchscreenDevice
>& devices
) override
{
86 ui_thread_runner_
->PostTask(
88 base::Bind(&EventFactoryEvdev::DispatchTouchscreenDevicesUpdated
,
89 event_factory_evdev_
, devices
));
91 void DispatchMouseDevicesUpdated(
92 const std::vector
<InputDevice
>& devices
) override
{
93 ui_thread_runner_
->PostTask(
94 FROM_HERE
, base::Bind(&EventFactoryEvdev::DispatchMouseDevicesUpdated
,
95 event_factory_evdev_
, devices
));
97 void DispatchTouchpadDevicesUpdated(
98 const std::vector
<InputDevice
>& devices
) override
{
99 ui_thread_runner_
->PostTask(
101 base::Bind(&EventFactoryEvdev::DispatchTouchpadDevicesUpdated
,
102 event_factory_evdev_
, devices
));
106 scoped_refptr
<base::SingleThreadTaskRunner
> ui_thread_runner_
;
107 base::WeakPtr
<EventFactoryEvdev
> event_factory_evdev_
;
112 EventFactoryEvdev::EventFactoryEvdev(CursorDelegateEvdev
* cursor
,
113 DeviceManager
* device_manager
,
114 KeyboardLayoutEngine
* keyboard_layout
)
115 : last_device_id_(0),
116 device_manager_(device_manager
),
117 keyboard_(&modifiers_
,
119 base::Bind(&EventFactoryEvdev::DispatchUiEvent
,
120 base::Unretained(this))),
122 input_controller_(&keyboard_
, &button_map_
),
124 weak_ptr_factory_(this) {
125 DCHECK(device_manager_
);
128 EventFactoryEvdev::~EventFactoryEvdev() {
131 void EventFactoryEvdev::Init() {
132 DCHECK(!initialized_
);
139 scoped_ptr
<SystemInputInjector
> EventFactoryEvdev::CreateSystemInputInjector() {
140 // Use forwarding dispatcher for the injector rather than dispatching
141 // directly. We cannot assume it is safe to (re-)enter ui::Event dispatch
142 // synchronously from the injection point.
143 scoped_ptr
<DeviceEventDispatcherEvdev
> proxy_dispatcher(
144 new ProxyDeviceEventDispatcher(base::ThreadTaskRunnerHandle::Get(),
145 weak_ptr_factory_
.GetWeakPtr()));
146 return make_scoped_ptr(
147 new InputInjectorEvdev(proxy_dispatcher
.Pass(), cursor_
));
150 void EventFactoryEvdev::DispatchKeyEvent(const KeyEventParams
& params
) {
151 keyboard_
.OnKeyChange(params
.code
, params
.down
, params
.timestamp
);
154 void EventFactoryEvdev::DispatchMouseMoveEvent(
155 const MouseMoveEventParams
& params
) {
156 MouseEvent
event(ui::ET_MOUSE_MOVED
, params
.location
, params
.location
,
157 params
.timestamp
, modifiers_
.GetModifierFlags(),
158 /* changed_button_flags */ 0);
159 event
.set_source_device_id(params
.device_id
);
160 DispatchUiEvent(&event
);
163 void EventFactoryEvdev::DispatchMouseButtonEvent(
164 const MouseButtonEventParams
& params
) {
165 // Mouse buttons can be remapped, touchpad taps & clicks cannot.
166 unsigned int button
= params
.button
;
167 if (params
.allow_remap
)
168 button
= button_map_
.GetMappedButton(button
);
170 int modifier
= EVDEV_MODIFIER_NONE
;
173 modifier
= EVDEV_MODIFIER_LEFT_MOUSE_BUTTON
;
176 modifier
= EVDEV_MODIFIER_RIGHT_MOUSE_BUTTON
;
179 modifier
= EVDEV_MODIFIER_MIDDLE_MOUSE_BUTTON
;
185 int flag
= modifiers_
.GetEventFlagFromModifier(modifier
);
186 modifiers_
.UpdateModifier(modifier
, params
.down
);
188 MouseEvent
event(params
.down
? ui::ET_MOUSE_PRESSED
: ui::ET_MOUSE_RELEASED
,
189 params
.location
, params
.location
, params
.timestamp
,
190 modifiers_
.GetModifierFlags() | flag
,
191 /* changed_button_flags */ flag
);
192 event
.set_source_device_id(params
.device_id
);
193 DispatchUiEvent(&event
);
196 void EventFactoryEvdev::DispatchMouseWheelEvent(
197 const MouseWheelEventParams
& params
) {
198 MouseWheelEvent
event(params
.delta
, params
.location
, params
.location
,
199 params
.timestamp
, modifiers_
.GetModifierFlags(),
200 0 /* changed_button_flags */);
201 event
.set_source_device_id(params
.device_id
);
202 DispatchUiEvent(&event
);
205 void EventFactoryEvdev::DispatchScrollEvent(const ScrollEventParams
& params
) {
206 ScrollEvent
event(params
.type
, params
.location
, params
.timestamp
,
207 modifiers_
.GetModifierFlags(), params
.delta
.x(),
208 params
.delta
.y(), params
.ordinal_delta
.x(),
209 params
.ordinal_delta
.y(), params
.finger_count
);
210 event
.set_source_device_id(params
.device_id
);
211 DispatchUiEvent(&event
);
214 void EventFactoryEvdev::DispatchTouchEvent(const TouchEventParams
& params
) {
215 float x
= params
.location
.x();
216 float y
= params
.location
.y();
217 double radius_x
= params
.radii
.x();
218 double radius_y
= params
.radii
.y();
220 // Transform the event to align touches to the image based on display mode.
221 DeviceDataManager::GetInstance()->ApplyTouchTransformer(params
.device_id
, &x
,
223 DeviceDataManager::GetInstance()->ApplyTouchRadiusScale(params
.device_id
,
225 DeviceDataManager::GetInstance()->ApplyTouchRadiusScale(params
.device_id
,
228 TouchEvent
touch_event(params
.type
, gfx::PointF(x
, y
),
229 modifiers_
.GetModifierFlags(), params
.touch_id
,
230 params
.timestamp
, radius_x
, radius_y
,
231 /* angle */ 0.f
, params
.pressure
);
232 touch_event
.set_source_device_id(params
.device_id
);
233 DispatchUiEvent(&touch_event
);
236 void EventFactoryEvdev::DispatchUiEvent(Event
* event
) {
237 // DispatchEvent takes PlatformEvent which is void*. This function
238 // wraps it with the real type.
239 DispatchEvent(event
);
242 void EventFactoryEvdev::DispatchKeyboardDevicesUpdated(
243 const std::vector
<KeyboardDevice
>& devices
) {
244 DeviceHotplugEventObserver
* observer
= DeviceDataManager::GetInstance();
245 observer
->OnKeyboardDevicesUpdated(devices
);
248 void EventFactoryEvdev::DispatchTouchscreenDevicesUpdated(
249 const std::vector
<TouchscreenDevice
>& devices
) {
250 DeviceHotplugEventObserver
* observer
= DeviceDataManager::GetInstance();
251 observer
->OnTouchscreenDevicesUpdated(devices
);
254 void EventFactoryEvdev::DispatchMouseDevicesUpdated(
255 const std::vector
<InputDevice
>& devices
) {
256 // There's no list of mice in DeviceDataManager.
257 input_controller_
.set_has_mouse(devices
.size() != 0);
258 DeviceHotplugEventObserver
* observer
= DeviceDataManager::GetInstance();
259 observer
->OnMouseDevicesUpdated(devices
);
262 void EventFactoryEvdev::DispatchTouchpadDevicesUpdated(
263 const std::vector
<InputDevice
>& devices
) {
264 // There's no list of touchpads in DeviceDataManager.
265 input_controller_
.set_has_touchpad(devices
.size() != 0);
266 DeviceHotplugEventObserver
* observer
= DeviceDataManager::GetInstance();
267 observer
->OnTouchpadDevicesUpdated(devices
);
271 void EventFactoryEvdev::OnDeviceEvent(const DeviceEvent
& event
) {
272 if (event
.device_type() != DeviceEvent::INPUT
)
275 switch (event
.action_type()) {
276 case DeviceEvent::ADD
:
277 case DeviceEvent::CHANGE
: {
278 TRACE_EVENT1("ozone", "OnDeviceAdded", "path", event
.path().value());
279 input_device_factory_proxy_
->AddInputDevice(NextDeviceId(), event
.path());
282 case DeviceEvent::REMOVE
: {
283 TRACE_EVENT1("ozone", "OnDeviceRemoved", "path", event
.path().value());
284 input_device_factory_proxy_
->RemoveInputDevice(event
.path());
290 void EventFactoryEvdev::OnDispatcherListChanged() {
295 void EventFactoryEvdev::WarpCursorTo(gfx::AcceleratedWidget widget
,
296 const gfx::PointF
& location
) {
300 cursor_
->MoveCursorTo(widget
, location
);
302 base::ThreadTaskRunnerHandle::Get()->PostTask(
303 FROM_HERE
, base::Bind(&EventFactoryEvdev::DispatchMouseMoveEvent
,
304 weak_ptr_factory_
.GetWeakPtr(),
305 MouseMoveEventParams(-1 /* device_id */,
306 cursor_
->GetLocation(),
307 EventTimeForNow())));
310 int EventFactoryEvdev::NextDeviceId() {
311 return ++last_device_id_
;
314 void EventFactoryEvdev::StartThread() {
315 // Set up device factory.
316 scoped_ptr
<DeviceEventDispatcherEvdev
> proxy_dispatcher(
317 new ProxyDeviceEventDispatcher(base::ThreadTaskRunnerHandle::Get(),
318 weak_ptr_factory_
.GetWeakPtr()));
319 thread_
.Start(proxy_dispatcher
.Pass(), cursor_
,
320 base::Bind(&EventFactoryEvdev::OnThreadStarted
,
321 weak_ptr_factory_
.GetWeakPtr()));
324 void EventFactoryEvdev::OnThreadStarted(
325 scoped_ptr
<InputDeviceFactoryEvdevProxy
> input_device_factory
) {
326 input_device_factory_proxy_
= input_device_factory
.Pass();
328 // Hook up device configuration.
329 input_controller_
.SetInputDeviceFactory(input_device_factory_proxy_
.get());
331 // Scan & monitor devices.
332 device_manager_
->AddObserver(this);
333 device_manager_
->ScanDevices(this);