OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/events/ozone/event_factory_ozone.h" | 5 #include "ui/events/ozone/event_factory_ozone.h" |
6 | 6 |
7 #include <errno.h> | 7 #include "base/command_line.h" |
8 #include <fcntl.h> | |
9 #include <linux/input.h> | |
10 #include <poll.h> | |
11 #include <unistd.h> | |
12 | |
13 #include "base/message_loop/message_pump_ozone.h" | 8 #include "base/message_loop/message_pump_ozone.h" |
14 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
15 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
16 #include "ui/events/ozone/evdev/key_event_converter_ozone.h" | 11 #include "ui/events/event_switches.h" |
17 #include "ui/events/ozone/evdev/touch_event_converter_ozone.h" | |
18 #include "ui/events/ozone/event_factory_delegate_ozone.h" | 12 #include "ui/events/ozone/event_factory_delegate_ozone.h" |
19 | 13 |
| 14 #if defined(USE_OZONE_EVDEV) |
| 15 #include "ui/events/ozone/evdev/event_factory_delegate.h" |
| 16 #endif |
| 17 |
20 namespace ui { | 18 namespace ui { |
21 | 19 |
| 20 namespace { |
| 21 |
| 22 #if defined(USE_OZONE_EVDEV) |
| 23 static const char kEventFactoryEvdev[] = "evdev"; |
| 24 #endif |
| 25 |
| 26 EventFactoryDelegateOzone* CreateDelegate(const std::string& event_delegate) { |
| 27 #if defined(USE_OZONE_EVDEV) |
| 28 if (event_delegate == "evdev" || event_delegate == "default") |
| 29 return new EventFactoryDelegateEvdev; |
| 30 #endif |
| 31 |
| 32 if (event_delegate == "none" || event_delegate == "default") { |
| 33 LOG(WARNING) << "No ozone events implementation - limited input support"; |
| 34 return NULL; |
| 35 } |
| 36 |
| 37 LOG(FATAL) << "Invalid ozone events implementation: " << event_delegate; |
| 38 return NULL; // not reached |
| 39 } |
| 40 |
| 41 std::string GetRequestedDelegate() { |
| 42 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kOzoneEvents)) |
| 43 return "default"; |
| 44 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 45 switches::kOzoneEvents); |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
22 // static | 50 // static |
23 EventFactoryDelegateOzone* EventFactoryOzone::delegate_ = NULL; | 51 EventFactoryDelegateOzone* EventFactoryOzone::delegate_ = NULL; |
24 | 52 |
25 EventFactoryOzone::EventFactoryOzone() {} | 53 EventFactoryOzone::EventFactoryOzone() {} |
26 | 54 |
27 EventFactoryOzone::~EventFactoryOzone() { | 55 EventFactoryOzone::~EventFactoryOzone() { |
28 // Always delete watchers before converters to prevent a possible race. | 56 // Always delete watchers before converters to prevent a possible race. |
29 STLDeleteValues<std::map<int, FDWatcher> >(&watchers_); | 57 STLDeleteValues<std::map<int, FDWatcher> >(&watchers_); |
30 STLDeleteValues<std::map<int, Converter> >(&converters_); | 58 STLDeleteValues<std::map<int, Converter> >(&converters_); |
31 } | 59 } |
32 | 60 |
33 void EventFactoryOzone::CreateStartupEventConverters() { | 61 void EventFactoryOzone::CreateStartupEventConverters() { |
34 if (delegate_) { | 62 if (!delegate_) { |
| 63 std::string requested_delegate = GetRequestedDelegate(); |
| 64 SetEventFactoryDelegateOzone(CreateDelegate(requested_delegate)); |
| 65 } |
| 66 if (delegate_) |
35 delegate_->CreateStartupEventConverters(this); | 67 delegate_->CreateStartupEventConverters(this); |
36 return; | |
37 } | |
38 | |
39 // If there is no |delegate_| set, read events from /dev/input/* | |
40 | |
41 // The number of devices in the directory is unknown without reading | |
42 // the contents of the directory. Further, with hot-plugging, the entries | |
43 // might decrease during the execution of this loop. So exciting from the | |
44 // loop on the first failure of open below is both cheaper and more | |
45 // reliable. | |
46 for (int id = 0; true; id++) { | |
47 std::string path = base::StringPrintf("/dev/input/event%d", id); | |
48 int fd = open(path.c_str(), O_RDONLY | O_NONBLOCK); | |
49 if (fd < 0) { | |
50 DLOG(ERROR) << "Cannot open '" << path << "': " << strerror(errno); | |
51 break; | |
52 } | |
53 size_t evtype = 0; | |
54 COMPILE_ASSERT(sizeof(evtype) * 8 >= EV_MAX, evtype_wide_enough); | |
55 if (ioctl(fd, EVIOCGBIT(0, sizeof(evtype)), &evtype) == -1) { | |
56 DLOG(ERROR) << "failed ioctl EVIOCGBIT 0" << path; | |
57 close(fd); | |
58 continue; | |
59 } | |
60 | |
61 scoped_ptr<EventConverterOzone> converter; | |
62 // TODO(rjkroege) Add more device types. Support hot-plugging. | |
63 if (evtype & (1 << EV_ABS)) | |
64 converter.reset(new TouchEventConverterOzone(fd, id)); | |
65 else if (evtype & (1 << EV_KEY)) | |
66 converter.reset(new KeyEventConverterOzone()); | |
67 | |
68 if (converter) { | |
69 AddEventConverter(fd, converter.Pass()); | |
70 } else { | |
71 close(fd); | |
72 } | |
73 } | |
74 } | 68 } |
75 | 69 |
| 70 // static |
76 void EventFactoryOzone::SetEventFactoryDelegateOzone( | 71 void EventFactoryOzone::SetEventFactoryDelegateOzone( |
77 EventFactoryDelegateOzone* delegate) { | 72 EventFactoryDelegateOzone* delegate) { |
78 // It should be unnecessary to call this more than once. | 73 // It should be unnecessary to call this more than once. |
79 DCHECK(!delegate_); | 74 DCHECK(!delegate_); |
80 delegate_ = delegate; | 75 delegate_ = delegate; |
81 } | 76 } |
82 | 77 |
83 void EventFactoryOzone::AddEventConverter( | 78 void EventFactoryOzone::AddEventConverter( |
84 int fd, | 79 int fd, |
85 scoped_ptr<EventConverterOzone> converter) { | 80 scoped_ptr<EventConverterOzone> converter) { |
(...skipping 14 matching lines...) Expand all Loading... |
100 | 95 |
101 void EventFactoryOzone::RemoveEventConverter(int fd) { | 96 void EventFactoryOzone::RemoveEventConverter(int fd) { |
102 // Always delete watchers before converters to prevent a possible race. | 97 // Always delete watchers before converters to prevent a possible race. |
103 delete watchers_[fd]; | 98 delete watchers_[fd]; |
104 delete converters_[fd]; | 99 delete converters_[fd]; |
105 watchers_.erase(fd); | 100 watchers_.erase(fd); |
106 converters_.erase(fd); | 101 converters_.erase(fd); |
107 } | 102 } |
108 | 103 |
109 } // namespace ui | 104 } // namespace ui |
OLD | NEW |