1 // Copyright 2015 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_thread_evdev.h"
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/threading/thread.h"
12 #include "base/trace_event/trace_event.h"
13 #include "ui/events/ozone/evdev/device_event_dispatcher_evdev.h"
14 #include "ui/events/ozone/evdev/input_device_factory_evdev.h"
15 #include "ui/events/ozone/evdev/input_device_factory_evdev_proxy.h"
21 // Internal base::Thread subclass for events thread.
22 class EvdevThread
: public base::Thread
{
24 EvdevThread(scoped_ptr
<DeviceEventDispatcherEvdev
> dispatcher
,
25 CursorDelegateEvdev
* cursor
,
26 const EventThreadStartCallback
& callback
)
27 : base::Thread("evdev"),
28 dispatcher_(dispatcher
.Pass()),
30 init_callback_(callback
),
31 init_runner_(base::ThreadTaskRunnerHandle::Get()),
32 input_device_factory_(nullptr) {}
33 ~EvdevThread() override
{ Stop(); }
35 void Init() override
{
36 TRACE_EVENT0("ozone", "EvdevThread::Init");
37 input_device_factory_
=
38 new InputDeviceFactoryEvdev(dispatcher_
.Pass(), cursor_
);
40 scoped_ptr
<InputDeviceFactoryEvdevProxy
> proxy(
41 new InputDeviceFactoryEvdevProxy(base::ThreadTaskRunnerHandle::Get(),
42 input_device_factory_
->GetWeakPtr()));
44 init_runner_
->PostTask(FROM_HERE
,
45 base::Bind(init_callback_
, base::Passed(&proxy
)));
48 void CleanUp() override
{
49 TRACE_EVENT0("ozone", "EvdevThread::CleanUp");
50 delete input_device_factory_
;
54 // Initialization bits passed from main thread.
55 scoped_ptr
<DeviceEventDispatcherEvdev
> dispatcher_
;
56 CursorDelegateEvdev
* cursor_
;
57 EventThreadStartCallback init_callback_
;
58 scoped_refptr
<base::SingleThreadTaskRunner
> init_runner_
;
60 // Thread-internal state.
61 InputDeviceFactoryEvdev
* input_device_factory_
;
66 EventThreadEvdev::EventThreadEvdev() {
69 EventThreadEvdev::~EventThreadEvdev() {
72 void EventThreadEvdev::Start(scoped_ptr
<DeviceEventDispatcherEvdev
> dispatcher
,
73 CursorDelegateEvdev
* cursor
,
74 const EventThreadStartCallback
& callback
) {
75 TRACE_EVENT0("ozone", "EventThreadEvdev::Start");
76 thread_
.reset(new EvdevThread(dispatcher
.Pass(), cursor
, callback
));
77 if (!thread_
->StartWithOptions(
78 base::Thread::Options(base::MessageLoop::TYPE_UI
, 0)))
79 LOG(FATAL
) << "Failed to create input thread";