OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ui/events/ozone/evdev/libgestures_glue/event_reader_libevdev_cros.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <libevdev/libevdev.h> |
| 9 #include <linux/input.h> |
| 10 |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" |
| 14 |
| 15 namespace ui { |
| 16 |
| 17 namespace { |
| 18 |
| 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; |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 EventReaderLibevdevCros::EventReaderLibevdevCros(int fd, |
| 29 const base::FilePath& path, |
| 30 scoped_ptr<Delegate> delegate) |
| 31 : path_(path), delegate_(delegate.Pass()) { |
| 32 memset(&evdev_, 0, sizeof(evdev_)); |
| 33 evdev_.log = OnLogMessage; |
| 34 evdev_.log_udata = this; |
| 35 evdev_.syn_report = OnSynReport; |
| 36 evdev_.syn_report_udata = this; |
| 37 evdev_.fd = fd; |
| 38 |
| 39 memset(&evstate_, 0, sizeof(evstate_)); |
| 40 evdev_.evstate = &evstate_; |
| 41 Event_Init(&evdev_); |
| 42 |
| 43 Event_Open(&evdev_); |
| 44 |
| 45 delegate_->OnLibEvdevCrosOpen(&evdev_, &evstate_); |
| 46 } |
| 47 |
| 48 EventReaderLibevdevCros::~EventReaderLibevdevCros() { |
| 49 Stop(); |
| 50 EvdevClose(&evdev_); |
| 51 } |
| 52 |
| 53 EventReaderLibevdevCros::Delegate::~Delegate() {} |
| 54 |
| 55 void EventReaderLibevdevCros::Start() { |
| 56 base::MessageLoopForUI::current()->WatchFileDescriptor( |
| 57 evdev_.fd, |
| 58 true, |
| 59 base::MessagePumpLibevent::WATCH_READ, |
| 60 &controller_, |
| 61 this); |
| 62 } |
| 63 |
| 64 void EventReaderLibevdevCros::Stop() { |
| 65 controller_.StopWatchingFileDescriptor(); |
| 66 } |
| 67 |
| 68 void EventReaderLibevdevCros::OnFileCanReadWithoutBlocking(int fd) { |
| 69 if (EvdevRead(&evdev_)) { |
| 70 if (errno == EINTR || errno == EAGAIN) |
| 71 return; |
| 72 if (errno != ENODEV) |
| 73 PLOG(ERROR) << "error reading device " << path_.value(); |
| 74 Stop(); |
| 75 return; |
| 76 } |
| 77 } |
| 78 |
| 79 void EventReaderLibevdevCros::OnFileCanWriteWithoutBlocking(int fd) { |
| 80 NOTREACHED(); |
| 81 } |
| 82 |
| 83 // static |
| 84 void EventReaderLibevdevCros::OnSynReport(void* data, |
| 85 EventStateRec* evstate, |
| 86 struct timeval* tv) { |
| 87 EventReaderLibevdevCros* reader = static_cast<EventReaderLibevdevCros*>(data); |
| 88 reader->delegate_->OnLibEvdevCrosEvent(&reader->evdev_, evstate, *tv); |
| 89 } |
| 90 |
| 91 // static |
| 92 void EventReaderLibevdevCros::OnLogMessage(void* data, |
| 93 int level, |
| 94 const char* fmt, |
| 95 ...) { |
| 96 va_list args; |
| 97 va_start(args, fmt); |
| 98 if (level >= LOGLEVEL_ERROR) |
| 99 LOG(ERROR) << "libevdev: " << FormatLog(fmt, args); |
| 100 else if (level >= LOGLEVEL_WARNING) |
| 101 LOG(WARNING) << "libevdev: " << FormatLog(fmt, args); |
| 102 else |
| 103 VLOG(3) << "libevdev: " << FormatLog(fmt, args); |
| 104 va_end(args); |
| 105 } |
| 106 |
| 107 } // namespace ui |
OLD | NEW |