Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / events / ozone / evdev / libgestures_glue / event_reader_libevdev_cros.cc
bloba5f3277ef887b1ea9b4cb73cc3ed5bc7c0642172
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"
14 #include "base/trace_event/trace_event.h"
16 namespace ui {
18 namespace {
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());
24 return msg;
27 } // namespace
29 EventReaderLibevdevCros::EventReaderLibevdevCros(int fd,
30 const base::FilePath& path,
31 int id,
32 const EventDeviceInfo& devinfo,
33 scoped_ptr<Delegate> delegate)
34 : EventConverterEvdev(fd,
35 path,
36 id,
37 devinfo.device_type(),
38 devinfo.name(),
39 devinfo.vendor_id(),
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;
54 evdev_.fd = fd;
56 memset(&evstate_, 0, sizeof(evstate_));
57 evdev_.evstate = &evstate_;
58 Event_Init(&evdev_);
60 Event_Open(&evdev_);
62 delegate_->OnLibEvdevCrosOpen(&evdev_, &evstate_);
65 EventReaderLibevdevCros::~EventReaderLibevdevCros() {
66 DCHECK(!watching_);
67 EvdevClose(&evdev_);
68 fd_ = -1;
71 EventReaderLibevdevCros::Delegate::~Delegate() {}
73 void EventReaderLibevdevCros::OnFileCanReadWithoutBlocking(int fd) {
74 TRACE_EVENT1("evdev", "EventReaderLibevdevCros::OnFileCanReadWithoutBlocking",
75 "fd", fd);
77 if (EvdevRead(&evdev_)) {
78 if (errno == EINTR || errno == EAGAIN)
79 return;
80 if (errno != ENODEV)
81 PLOG(ERROR) << "error reading device " << path_.value();
82 Stop();
83 return;
87 bool EventReaderLibevdevCros::HasKeyboard() const {
88 return has_keyboard_;
91 bool EventReaderLibevdevCros::HasMouse() const {
92 return has_mouse_;
95 bool EventReaderLibevdevCros::HasTouchpad() const {
96 return has_touchpad_;
99 bool EventReaderLibevdevCros::HasCapsLockLed() const {
100 return has_caps_lock_led_;
103 void EventReaderLibevdevCros::OnDisabled() {
104 delegate_->OnLibEvdevCrosStopped(&evdev_, &evstate_);
107 // static
108 void EventReaderLibevdevCros::OnSynReport(void* data,
109 EventStateRec* evstate,
110 struct timeval* tv) {
111 EventReaderLibevdevCros* reader = static_cast<EventReaderLibevdevCros*>(data);
112 if (!reader->enabled_)
113 return;
115 reader->delegate_->OnLibEvdevCrosEvent(&reader->evdev_, evstate, *tv);
118 // static
119 void EventReaderLibevdevCros::OnLogMessage(void* data,
120 int level,
121 const char* fmt,
122 ...) {
123 va_list args;
124 va_start(args, fmt);
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);
129 else
130 VLOG(3) << "libevdev: " << FormatLog(fmt, args);
131 va_end(args);
134 } // namespace ui