- add sources.
[platform/framework/web/crosswalk.git] / src / ui / events / ozone / evdev / event_factory.cc
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.
4
5 #include "ui/events/ozone/evdev/event_factory.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.h"
15 #include "ui/events/ozone/evdev/touch_event_converter.h"
16 #include "ui/events/ozone/event_factory_ozone.h"
17
18 namespace ui {
19
20 EventFactoryEvdev::EventFactoryEvdev() {}
21
22 EventFactoryEvdev::~EventFactoryEvdev() {}
23
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
29   // reliable.
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);
33     if (fd < 0) {
34       DLOG(ERROR) << "Cannot open '" << path << "': " << strerror(errno);
35       break;
36     }
37     size_t evtype = 0;
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;
41       close(fd);
42       continue;
43     }
44
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_));
51
52     if (converter) {
53       AddEventConverter(fd, converter.Pass());
54     } else {
55       close(fd);
56     }
57   }
58 }
59
60 }  // namespace ui