ozone: evdev: Populate keyboard list on DeviceDataManager
[chromium-blink-merge.git] / ui / events / ozone / evdev / libgestures_glue / event_reader_libevdev_cros.cc
blobaeed7914f908c1c5e03a4f27141560977cc0adb5
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"
7 #include <errno.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"
15 namespace ui {
17 namespace {
19 std::string FormatLog(const char* fmt, va_list args) {
20 std::string msg = base::StringPrintV(fmt, args);
21 if (!msg.empty() && msg[msg.size() - 1] == '\n')
22 msg.erase(msg.end() - 1, msg.end());
23 return msg;
26 } // namespace
28 EventReaderLibevdevCros::EventReaderLibevdevCros(int fd,
29 const base::FilePath& path,
30 int id,
31 InputDeviceType type,
32 const EventDeviceInfo& devinfo,
33 scoped_ptr<Delegate> delegate)
34 : EventConverterEvdev(fd, path, id, type),
35 has_keyboard_(devinfo.HasKeyboard()),
36 delegate_(delegate.Pass()) {
37 memset(&evdev_, 0, sizeof(evdev_));
38 evdev_.log = OnLogMessage;
39 evdev_.log_udata = this;
40 evdev_.syn_report = OnSynReport;
41 evdev_.syn_report_udata = this;
42 evdev_.fd = fd;
44 memset(&evstate_, 0, sizeof(evstate_));
45 evdev_.evstate = &evstate_;
46 Event_Init(&evdev_);
48 Event_Open(&evdev_);
50 delegate_->OnLibEvdevCrosOpen(&evdev_, &evstate_);
53 EventReaderLibevdevCros::~EventReaderLibevdevCros() {
54 Stop();
55 EvdevClose(&evdev_);
58 EventReaderLibevdevCros::Delegate::~Delegate() {}
60 void EventReaderLibevdevCros::OnFileCanReadWithoutBlocking(int fd) {
61 if (EvdevRead(&evdev_)) {
62 if (errno == EINTR || errno == EAGAIN)
63 return;
64 if (errno != ENODEV)
65 PLOG(ERROR) << "error reading device " << path_.value();
66 Stop();
67 return;
71 bool EventReaderLibevdevCros::HasKeyboard() const {
72 return has_keyboard_;
75 // static
76 void EventReaderLibevdevCros::OnSynReport(void* data,
77 EventStateRec* evstate,
78 struct timeval* tv) {
79 EventReaderLibevdevCros* reader = static_cast<EventReaderLibevdevCros*>(data);
80 reader->delegate_->OnLibEvdevCrosEvent(&reader->evdev_, evstate, *tv);
83 // static
84 void EventReaderLibevdevCros::OnLogMessage(void* data,
85 int level,
86 const char* fmt,
87 ...) {
88 va_list args;
89 va_start(args, fmt);
90 if (level >= LOGLEVEL_ERROR)
91 LOG(ERROR) << "libevdev: " << FormatLog(fmt, args);
92 else if (level >= LOGLEVEL_WARNING)
93 LOG(WARNING) << "libevdev: " << FormatLog(fmt, args);
94 else
95 VLOG(3) << "libevdev: " << FormatLog(fmt, args);
96 va_end(args);
99 } // namespace ui