OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/event_factory_delegate_evdev.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <fcntl.h> |
| 9 #include <linux/input.h> |
| 10 #include <poll.h> |
| 11 #include <unistd.h> |
| 12 |
| 13 #include "base/strings/stringprintf.h" |
| 14 #include "ui/events/ozone/evdev/key_event_converter_evdev.h" |
| 15 #include "ui/events/ozone/evdev/touch_event_converter_evdev.h" |
| 16 #include "ui/events/ozone/event_factory_delegate_ozone.h" |
| 17 #include "ui/events/ozone/event_factory_ozone.h" |
| 18 |
| 19 namespace ui { |
| 20 |
| 21 EventFactoryDelegateEvdev::EventFactoryDelegateEvdev() {} |
| 22 |
| 23 EventFactoryDelegateEvdev::~EventFactoryDelegateEvdev() {} |
| 24 |
| 25 void EventFactoryDelegateEvdev::CreateStartupEventConverters( |
| 26 EventFactoryOzone* factory) { |
| 27 // The number of devices in the directory is unknown without reading |
| 28 // the contents of the directory. Further, with hot-plugging, the entries |
| 29 // might decrease during the execution of this loop. So exciting from the |
| 30 // loop on the first failure of open below is both cheaper and more |
| 31 // reliable. |
| 32 for (int id = 0; true; id++) { |
| 33 std::string path = base::StringPrintf("/dev/input/event%d", id); |
| 34 int fd = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
| 35 if (fd < 0) { |
| 36 DLOG(ERROR) << "Cannot open '" << path << "': " << strerror(errno); |
| 37 break; |
| 38 } |
| 39 size_t evtype = 0; |
| 40 COMPILE_ASSERT(sizeof(evtype) * 8 >= EV_MAX, evtype_wide_enough); |
| 41 if (ioctl(fd, EVIOCGBIT(0, sizeof(evtype)), &evtype) == -1) { |
| 42 DLOG(ERROR) << "failed ioctl EVIOCGBIT 0" << path; |
| 43 close(fd); |
| 44 continue; |
| 45 } |
| 46 |
| 47 scoped_ptr<EventConverterOzone> converter; |
| 48 // TODO(rjkroege) Add more device types. Support hot-plugging. |
| 49 if (evtype & (1 << EV_ABS)) |
| 50 converter.reset(new TouchEventConverterEvdev(fd, id)); |
| 51 else if (evtype & (1 << EV_KEY)) |
| 52 converter.reset(new KeyEventConverterEvdev()); |
| 53 |
| 54 if (converter) { |
| 55 factory->AddEventConverter(fd, converter.Pass()); |
| 56 } else { |
| 57 close(fd); |
| 58 } |
| 59 } |
| 60 } |
| 61 |
| 62 } // namespace ui |
OLD | NEW |