Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1108)

Side by Side Diff: ui/events/ozone/event_factory_ozone.cc

Issue 28513004: Move evdev events support into EventFactoryDelegateEvdev (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_evdev.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 } // namespace
42
22 // static 43 // static
23 EventFactoryDelegateOzone* EventFactoryOzone::delegate_ = NULL; 44 EventFactoryDelegateOzone* EventFactoryOzone::delegate_ = NULL;
24 45
25 EventFactoryOzone::EventFactoryOzone() {} 46 EventFactoryOzone::EventFactoryOzone() {}
26 47
27 EventFactoryOzone::~EventFactoryOzone() { 48 EventFactoryOzone::~EventFactoryOzone() {
28 // Always delete watchers before converters to prevent a possible race. 49 // Always delete watchers before converters to prevent a possible race.
29 STLDeleteValues<std::map<int, FDWatcher> >(&watchers_); 50 STLDeleteValues<std::map<int, FDWatcher> >(&watchers_);
30 STLDeleteValues<std::map<int, Converter> >(&converters_); 51 STLDeleteValues<std::map<int, Converter> >(&converters_);
31 } 52 }
32 53
33 void EventFactoryOzone::CreateStartupEventConverters() { 54 void EventFactoryOzone::CreateStartupEventConverters() {
34 if (delegate_) { 55 if (!delegate_) {
56 std::string requested_delegate = GetRequestedDelegate();
57 SetEventFactoryDelegateOzone(CreateDelegate(requested_delegate));
58 }
59 if (delegate_)
35 delegate_->CreateStartupEventConverters(this); 60 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 } 61 }
75 62
63 // static
76 void EventFactoryOzone::SetEventFactoryDelegateOzone( 64 void EventFactoryOzone::SetEventFactoryDelegateOzone(
77 EventFactoryDelegateOzone* delegate) { 65 EventFactoryDelegateOzone* delegate) {
78 // It should be unnecessary to call this more than once. 66 // It should be unnecessary to call this more than once.
79 DCHECK(!delegate_); 67 DCHECK(!delegate_);
80 delegate_ = delegate; 68 delegate_ = delegate;
81 } 69 }
82 70
71 // static
72 std::string EventFactoryOzone::GetRequestedDelegate() {
73 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kOzoneEvents))
74 return "default";
75 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
76 switches::kOzoneEvents);
77 }
78
83 void EventFactoryOzone::AddEventConverter( 79 void EventFactoryOzone::AddEventConverter(
84 int fd, 80 int fd,
85 scoped_ptr<EventConverterOzone> converter) { 81 scoped_ptr<EventConverterOzone> converter) {
86 CHECK(watchers_.count(fd) == 0 && converters_.count(fd) == 0); 82 CHECK(watchers_.count(fd) == 0 && converters_.count(fd) == 0);
87 83
88 FDWatcher watcher = new base::MessagePumpLibevent::FileDescriptorWatcher(); 84 FDWatcher watcher = new base::MessagePumpLibevent::FileDescriptorWatcher();
89 85
90 base::MessagePumpOzone::Current()->WatchFileDescriptor( 86 base::MessagePumpOzone::Current()->WatchFileDescriptor(
91 fd, 87 fd,
92 true, 88 true,
93 base::MessagePumpLibevent::WATCH_READ, 89 base::MessagePumpLibevent::WATCH_READ,
94 watcher, 90 watcher,
95 converter.get()); 91 converter.get());
96 92
97 converters_[fd] = converter.release(); 93 converters_[fd] = converter.release();
98 watchers_[fd] = watcher; 94 watchers_[fd] = watcher;
99 } 95 }
100 96
101 void EventFactoryOzone::RemoveEventConverter(int fd) { 97 void EventFactoryOzone::RemoveEventConverter(int fd) {
102 // Always delete watchers before converters to prevent a possible race. 98 // Always delete watchers before converters to prevent a possible race.
103 delete watchers_[fd]; 99 delete watchers_[fd];
104 delete converters_[fd]; 100 delete converters_[fd];
105 watchers_.erase(fd); 101 watchers_.erase(fd);
106 converters_.erase(fd); 102 converters_.erase(fd);
107 } 103 }
108 104
109 } // namespace ui 105 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698