OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/event.h" | 5 #include "ui/events/event.h" |
6 | 6 |
7 #if defined(USE_X11) | 7 #if defined(USE_X11) |
8 #include <X11/extensions/XInput2.h> | 8 #include <X11/extensions/XInput2.h> |
9 #include <X11/Xlib.h> | 9 #include <X11/Xlib.h> |
10 #endif | 10 #endif |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "ui/gfx/transform_util.h" | 22 #include "ui/gfx/transform_util.h" |
23 | 23 |
24 #if defined(USE_X11) | 24 #if defined(USE_X11) |
25 #include "ui/events/keycodes/keyboard_code_conversion_x.h" | 25 #include "ui/events/keycodes/keyboard_code_conversion_x.h" |
26 #elif defined(USE_OZONE) | 26 #elif defined(USE_OZONE) |
27 #include "ui/events/keycodes/keyboard_code_conversion.h" | 27 #include "ui/events/keycodes/keyboard_code_conversion.h" |
28 #endif | 28 #endif |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) { | |
33 #if defined(USE_X11) | |
34 if (!event || event->type == GenericEvent) | |
35 return NULL; | |
36 XEvent* copy = new XEvent; | |
37 *copy = *event; | |
38 return copy; | |
39 #elif defined(OS_WIN) | |
40 return event; | |
41 #elif defined(USE_OZONE) | |
42 return NULL; | |
43 #else | |
44 NOTREACHED() << | |
45 "Don't know how to copy base::NativeEvent for this platform"; | |
46 return NULL; | |
47 #endif | |
48 } | |
49 | |
50 std::string EventTypeName(ui::EventType type) { | 32 std::string EventTypeName(ui::EventType type) { |
51 #define RETURN_IF_TYPE(t) if (type == ui::t) return #t | 33 #define RETURN_IF_TYPE(t) if (type == ui::t) return #t |
52 #define CASE_TYPE(t) case ui::t: return #t | 34 #define CASE_TYPE(t) case ui::t: return #t |
53 switch (type) { | 35 switch (type) { |
54 CASE_TYPE(ET_UNKNOWN); | 36 CASE_TYPE(ET_UNKNOWN); |
55 CASE_TYPE(ET_MOUSE_PRESSED); | 37 CASE_TYPE(ET_MOUSE_PRESSED); |
56 CASE_TYPE(ET_MOUSE_DRAGGED); | 38 CASE_TYPE(ET_MOUSE_DRAGGED); |
57 CASE_TYPE(ET_MOUSE_RELEASED); | 39 CASE_TYPE(ET_MOUSE_RELEASED); |
58 CASE_TYPE(ET_MOUSE_MOVED); | 40 CASE_TYPE(ET_MOUSE_MOVED); |
59 CASE_TYPE(ET_MOUSE_ENTERED); | 41 CASE_TYPE(ET_MOUSE_ENTERED); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 } | 94 } |
113 | 95 |
114 } // namespace | 96 } // namespace |
115 | 97 |
116 namespace ui { | 98 namespace ui { |
117 | 99 |
118 //////////////////////////////////////////////////////////////////////////////// | 100 //////////////////////////////////////////////////////////////////////////////// |
119 // Event | 101 // Event |
120 | 102 |
121 Event::~Event() { | 103 Event::~Event() { |
122 #if defined(USE_X11) | |
123 if (delete_native_event_) | 104 if (delete_native_event_) |
124 delete native_event_; | 105 ReleaseCopiedNativeEvent(native_event_); |
125 #endif | |
126 } | 106 } |
127 | 107 |
128 bool Event::HasNativeEvent() const { | 108 bool Event::HasNativeEvent() const { |
129 base::NativeEvent null_event; | 109 base::NativeEvent null_event; |
130 std::memset(&null_event, 0, sizeof(null_event)); | 110 std::memset(&null_event, 0, sizeof(null_event)); |
131 return !!std::memcmp(&native_event_, &null_event, sizeof(null_event)); | 111 return !!std::memcmp(&native_event_, &null_event, sizeof(null_event)); |
132 } | 112 } |
133 | 113 |
134 void Event::StopPropagation() { | 114 void Event::StopPropagation() { |
135 // TODO(sad): Re-enable these checks once View uses dispatcher to dispatch | 115 // TODO(sad): Re-enable these checks once View uses dispatcher to dispatch |
136 // events. | 116 // events. |
137 // CHECK(phase_ != EP_PREDISPATCH && phase_ != EP_POSTDISPATCH); | 117 // CHECK(phase_ != EP_PREDISPATCH && phase_ != EP_POSTDISPATCH); |
138 CHECK(cancelable_); | 118 CHECK(cancelable_); |
139 result_ = static_cast<EventResult>(result_ | ER_CONSUMED); | 119 result_ = static_cast<EventResult>(result_ | ER_CONSUMED); |
140 } | 120 } |
141 | 121 |
142 void Event::SetHandled() { | 122 void Event::SetHandled() { |
143 // TODO(sad): Re-enable these checks once View uses dispatcher to dispatch | 123 // TODO(sad): Re-enable these checks once View uses dispatcher to dispatch |
144 // events. | 124 // events. |
145 // CHECK(phase_ != EP_PREDISPATCH && phase_ != EP_POSTDISPATCH); | 125 // CHECK(phase_ != EP_PREDISPATCH && phase_ != EP_POSTDISPATCH); |
146 CHECK(cancelable_); | 126 CHECK(cancelable_); |
147 result_ = static_cast<EventResult>(result_ | ER_HANDLED); | 127 result_ = static_cast<EventResult>(result_ | ER_HANDLED); |
148 } | 128 } |
149 | 129 |
150 Event::Event(EventType type, base::TimeDelta time_stamp, int flags) | 130 Event::Event(EventType type, base::TimeDelta time_stamp, int flags) |
151 : type_(type), | 131 : type_(type), |
152 time_stamp_(time_stamp), | 132 time_stamp_(time_stamp), |
153 flags_(flags), | 133 flags_(flags), |
154 #if defined(USE_X11) | 134 native_event_(base::NativeEvent()), |
155 native_event_(NULL), | |
156 #endif | |
157 delete_native_event_(false), | 135 delete_native_event_(false), |
158 cancelable_(true), | 136 cancelable_(true), |
159 target_(NULL), | 137 target_(NULL), |
160 phase_(EP_PREDISPATCH), | 138 phase_(EP_PREDISPATCH), |
161 result_(ER_UNHANDLED) { | 139 result_(ER_UNHANDLED) { |
162 if (type_ < ET_LAST) | 140 if (type_ < ET_LAST) |
163 name_ = EventTypeName(type_); | 141 name_ = EventTypeName(type_); |
164 Init(); | |
165 } | 142 } |
166 | 143 |
167 Event::Event(const base::NativeEvent& native_event, | 144 Event::Event(const base::NativeEvent& native_event, |
168 EventType type, | 145 EventType type, |
169 int flags) | 146 int flags) |
170 : type_(type), | 147 : type_(type), |
171 time_stamp_(EventTimeFromNative(native_event)), | 148 time_stamp_(EventTimeFromNative(native_event)), |
172 flags_(flags), | 149 flags_(flags), |
| 150 native_event_(native_event), |
173 delete_native_event_(false), | 151 delete_native_event_(false), |
174 cancelable_(true), | 152 cancelable_(true), |
175 target_(NULL), | 153 target_(NULL), |
176 phase_(EP_PREDISPATCH), | 154 phase_(EP_PREDISPATCH), |
177 result_(ER_UNHANDLED) { | 155 result_(ER_UNHANDLED) { |
178 base::TimeDelta delta = EventTimeForNow() - time_stamp_; | 156 base::TimeDelta delta = EventTimeForNow() - time_stamp_; |
179 if (type_ < ET_LAST) | 157 if (type_ < ET_LAST) |
180 name_ = EventTypeName(type_); | 158 name_ = EventTypeName(type_); |
181 UMA_HISTOGRAM_CUSTOM_COUNTS("Event.Latency.Browser", | 159 UMA_HISTOGRAM_CUSTOM_COUNTS("Event.Latency.Browser", |
182 delta.InMicroseconds(), 1, 1000000, 100); | 160 delta.InMicroseconds(), 1, 1000000, 100); |
183 std::string name_for_event = | 161 std::string name_for_event = |
184 base::StringPrintf("Event.Latency.Browser.%s", name_.c_str()); | 162 base::StringPrintf("Event.Latency.Browser.%s", name_.c_str()); |
185 base::HistogramBase* counter_for_type = | 163 base::HistogramBase* counter_for_type = |
186 base::Histogram::FactoryGet( | 164 base::Histogram::FactoryGet( |
187 name_for_event, | 165 name_for_event, |
188 1, | 166 1, |
189 1000000, | 167 1000000, |
190 100, | 168 100, |
191 base::HistogramBase::kUmaTargetedHistogramFlag); | 169 base::HistogramBase::kUmaTargetedHistogramFlag); |
192 counter_for_type->Add(delta.InMicroseconds()); | 170 counter_for_type->Add(delta.InMicroseconds()); |
193 InitWithNativeEvent(native_event); | |
194 } | 171 } |
195 | 172 |
196 Event::Event(const Event& copy) | 173 Event::Event(const Event& copy) |
197 : type_(copy.type_), | 174 : type_(copy.type_), |
198 time_stamp_(copy.time_stamp_), | 175 time_stamp_(copy.time_stamp_), |
199 latency_(copy.latency_), | 176 latency_(copy.latency_), |
200 flags_(copy.flags_), | 177 flags_(copy.flags_), |
201 native_event_(::CopyNativeEvent(copy.native_event_)), | 178 native_event_(CopyNativeEvent(copy.native_event_)), |
202 delete_native_event_(false), | 179 delete_native_event_(true), |
203 cancelable_(true), | 180 cancelable_(true), |
204 target_(NULL), | 181 target_(NULL), |
205 phase_(EP_PREDISPATCH), | 182 phase_(EP_PREDISPATCH), |
206 result_(ER_UNHANDLED) { | 183 result_(ER_UNHANDLED) { |
207 if (type_ < ET_LAST) | 184 if (type_ < ET_LAST) |
208 name_ = EventTypeName(type_); | 185 name_ = EventTypeName(type_); |
209 #if defined(USE_X11) | |
210 if (native_event_) | |
211 delete_native_event_ = true; | |
212 #endif | |
213 } | 186 } |
214 | 187 |
215 void Event::SetType(EventType type) { | 188 void Event::SetType(EventType type) { |
216 if (type_ < ET_LAST) | 189 if (type_ < ET_LAST) |
217 name_ = std::string(); | 190 name_ = std::string(); |
218 type_ = type; | 191 type_ = type; |
219 if (type_ < ET_LAST) | 192 if (type_ < ET_LAST) |
220 name_ = EventTypeName(type_); | 193 name_ = EventTypeName(type_); |
221 } | 194 } |
222 | 195 |
223 void Event::Init() { | |
224 std::memset(&native_event_, 0, sizeof(native_event_)); | |
225 } | |
226 | |
227 void Event::InitWithNativeEvent(const base::NativeEvent& native_event) { | |
228 native_event_ = native_event; | |
229 } | |
230 | |
231 //////////////////////////////////////////////////////////////////////////////// | 196 //////////////////////////////////////////////////////////////////////////////// |
232 // CancelModeEvent | 197 // CancelModeEvent |
233 | 198 |
234 CancelModeEvent::CancelModeEvent() | 199 CancelModeEvent::CancelModeEvent() |
235 : Event(ET_CANCEL_MODE, base::TimeDelta(), 0) { | 200 : Event(ET_CANCEL_MODE, base::TimeDelta(), 0) { |
236 set_cancelable(false); | 201 set_cancelable(false); |
237 } | 202 } |
238 | 203 |
239 CancelModeEvent::~CancelModeEvent() { | 204 CancelModeEvent::~CancelModeEvent() { |
240 } | 205 } |
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 int GestureEvent::GetLowestTouchId() const { | 696 int GestureEvent::GetLowestTouchId() const { |
732 if (touch_ids_bitfield_ == 0) | 697 if (touch_ids_bitfield_ == 0) |
733 return -1; | 698 return -1; |
734 int i = -1; | 699 int i = -1; |
735 // Find the index of the least significant 1 bit | 700 // Find the index of the least significant 1 bit |
736 while (!(1 << ++i & touch_ids_bitfield_)); | 701 while (!(1 << ++i & touch_ids_bitfield_)); |
737 return i; | 702 return i; |
738 } | 703 } |
739 | 704 |
740 } // namespace ui | 705 } // namespace ui |
OLD | NEW |