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/libgestures_glue/event_reader_libevdev_cros.h"
8 #include <libevdev/libevdev.h>
9 #include <linux/input.h>
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/trace_event/trace_event.h"
20 std::string
FormatLog(const char* fmt
, va_list args
) {
21 std::string msg
= base::StringPrintV(fmt
, args
);
22 if (!msg
.empty() && msg
[msg
.size() - 1] == '\n')
23 msg
.erase(msg
.end() - 1, msg
.end());
29 EventReaderLibevdevCros::EventReaderLibevdevCros(int fd
,
30 const base::FilePath
& path
,
32 const EventDeviceInfo
& devinfo
,
33 scoped_ptr
<Delegate
> delegate
)
34 : EventConverterEvdev(fd
,
37 devinfo
.device_type(),
40 devinfo
.product_id()),
41 has_keyboard_(devinfo
.HasKeyboard()),
42 has_mouse_(devinfo
.HasMouse()),
43 has_touchpad_(devinfo
.HasTouchpad()),
44 has_caps_lock_led_(devinfo
.HasLedEvent(LED_CAPSL
)),
45 delegate_(delegate
.Pass()) {
46 // This class assumes it does not deal with internal keyboards.
47 CHECK(!has_keyboard_
|| type() != INPUT_DEVICE_INTERNAL
);
49 memset(&evdev_
, 0, sizeof(evdev_
));
50 evdev_
.log
= OnLogMessage
;
51 evdev_
.log_udata
= this;
52 evdev_
.syn_report
= OnSynReport
;
53 evdev_
.syn_report_udata
= this;
56 memset(&evstate_
, 0, sizeof(evstate_
));
57 evdev_
.evstate
= &evstate_
;
62 delegate_
->OnLibEvdevCrosOpen(&evdev_
, &evstate_
);
65 EventReaderLibevdevCros::~EventReaderLibevdevCros() {
71 EventReaderLibevdevCros::Delegate::~Delegate() {}
73 void EventReaderLibevdevCros::OnFileCanReadWithoutBlocking(int fd
) {
74 TRACE_EVENT1("evdev", "EventReaderLibevdevCros::OnFileCanReadWithoutBlocking",
77 if (EvdevRead(&evdev_
)) {
78 if (errno
== EINTR
|| errno
== EAGAIN
)
81 PLOG(ERROR
) << "error reading device " << path_
.value();
87 bool EventReaderLibevdevCros::HasKeyboard() const {
91 bool EventReaderLibevdevCros::HasMouse() const {
95 bool EventReaderLibevdevCros::HasTouchpad() const {
99 bool EventReaderLibevdevCros::HasCapsLockLed() const {
100 return has_caps_lock_led_
;
103 void EventReaderLibevdevCros::OnDisabled() {
104 delegate_
->OnLibEvdevCrosStopped(&evdev_
, &evstate_
);
108 void EventReaderLibevdevCros::OnSynReport(void* data
,
109 EventStateRec
* evstate
,
110 struct timeval
* tv
) {
111 EventReaderLibevdevCros
* reader
= static_cast<EventReaderLibevdevCros
*>(data
);
112 if (!reader
->enabled_
)
115 reader
->delegate_
->OnLibEvdevCrosEvent(&reader
->evdev_
, evstate
, *tv
);
119 void EventReaderLibevdevCros::OnLogMessage(void* data
,
125 if (level
>= LOGLEVEL_ERROR
)
126 LOG(ERROR
) << "libevdev: " << FormatLog(fmt
, args
);
127 else if (level
>= LOGLEVEL_WARNING
)
128 LOG(WARNING
) << "libevdev: " << FormatLog(fmt
, args
);
130 VLOG(3) << "libevdev: " << FormatLog(fmt
, args
);