1 // Copyright 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.
5 #include "ui/events/ozone/evdev/event_factory.h"
9 #include <linux/input.h>
13 #include "base/strings/stringprintf.h"
14 #include "ui/events/ozone/evdev/key_event_converter.h"
15 #include "ui/events/ozone/evdev/touch_event_converter.h"
16 #include "ui/events/ozone/event_factory_ozone.h"
20 EventFactoryEvdev::EventFactoryEvdev() {}
22 EventFactoryEvdev::~EventFactoryEvdev() {}
24 void EventFactoryEvdev::StartProcessingEvents() {
25 // The number of devices in the directory is unknown without reading
26 // the contents of the directory. Further, with hot-plugging, the entries
27 // might decrease during the execution of this loop. So exciting from the
28 // loop on the first failure of open below is both cheaper and more
30 for (int id = 0; true; id++) {
31 std::string path = base::StringPrintf("/dev/input/event%d", id);
32 int fd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
34 DLOG(ERROR) << "Cannot open '" << path << "': " << strerror(errno);
38 COMPILE_ASSERT(sizeof(evtype) * 8 >= EV_MAX, evtype_wide_enough);
39 if (ioctl(fd, EVIOCGBIT(0, sizeof(evtype)), &evtype) == -1) {
40 DLOG(ERROR) << "failed ioctl EVIOCGBIT 0" << path;
45 scoped_ptr<EventConverterOzone> converter;
46 // TODO(rjkroege) Add more device types. Support hot-plugging.
47 if (evtype & (1 << EV_ABS))
48 converter.reset(new TouchEventConverterEvdev(fd, id));
49 else if (evtype & (1 << EV_KEY))
50 converter.reset(new KeyEventConverterEvdev(&modifiers_));
53 AddEventConverter(fd, converter.Pass());