OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/converters/input_events/input_events_type_converters.h" | 5 #include "mojo/converters/input_events/input_events_type_converters.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 |
11 | 11 |
12 #include "mojo/converters/geometry/geometry_type_converters.h" | 12 #include "mojo/converters/geometry/geometry_type_converters.h" |
13 #include "mojo/converters/input_events/mojo_extended_key_event_data.h" | 13 #include "mojo/converters/input_events/mojo_extended_key_event_data.h" |
14 #include "third_party/mojo_services/src/input_events/public/interfaces/input_eve
nts.mojom.h" | 14 #include "third_party/mojo_services/src/input_events/public/interfaces/input_eve
nts.mojom.h" |
15 #include "ui/events/event_utils.h" | 15 #include "ui/events/event_utils.h" |
16 #include "ui/events/keycodes/keyboard_codes.h" | 16 #include "ui/events/keycodes/keyboard_codes.h" |
17 | 17 |
18 namespace mojo { | 18 namespace mojo { |
| 19 namespace { |
| 20 |
| 21 ui::EventType MojoMouseEventTypeToUIEvent(const EventPtr& event) { |
| 22 DCHECK(!event->pointer_data.is_null()); |
| 23 DCHECK_EQ(POINTER_KIND_MOUSE, event->pointer_data->kind); |
| 24 switch (event->action) { |
| 25 case EVENT_TYPE_POINTER_DOWN: |
| 26 return ui::ET_MOUSE_PRESSED; |
| 27 |
| 28 case EVENT_TYPE_POINTER_UP: |
| 29 return ui::ET_MOUSE_RELEASED; |
| 30 |
| 31 case EVENT_TYPE_POINTER_MOVE: |
| 32 DCHECK(event->pointer_data); |
| 33 if (event->pointer_data->horizontal_wheel != 0 || |
| 34 event->pointer_data->vertical_wheel != 0) { |
| 35 return ui::ET_MOUSEWHEEL; |
| 36 } |
| 37 if (event->flags & |
| 38 (EVENT_FLAGS_LEFT_MOUSE_BUTTON | EVENT_FLAGS_MIDDLE_MOUSE_BUTTON | |
| 39 EVENT_FLAGS_RIGHT_MOUSE_BUTTON)) { |
| 40 return ui::ET_MOUSE_DRAGGED; |
| 41 } |
| 42 return ui::ET_MOUSE_MOVED; |
| 43 |
| 44 default: |
| 45 NOTREACHED(); |
| 46 } |
| 47 |
| 48 return ui::ET_MOUSE_RELEASED; |
| 49 } |
| 50 |
| 51 ui::EventType MojoTouchEventTypeToUIEvent(const EventPtr& event) { |
| 52 DCHECK(!event->pointer_data.is_null()); |
| 53 DCHECK_EQ(POINTER_KIND_TOUCH, event->pointer_data->kind); |
| 54 switch (event->action) { |
| 55 case EVENT_TYPE_POINTER_DOWN: |
| 56 return ui::ET_TOUCH_PRESSED; |
| 57 |
| 58 case EVENT_TYPE_POINTER_UP: |
| 59 return ui::ET_TOUCH_RELEASED; |
| 60 |
| 61 case EVENT_TYPE_POINTER_MOVE: |
| 62 return ui::ET_TOUCH_MOVED; |
| 63 |
| 64 case EVENT_TYPE_POINTER_CANCEL: |
| 65 return ui::ET_TOUCH_CANCELLED; |
| 66 |
| 67 default: |
| 68 NOTREACHED(); |
| 69 } |
| 70 |
| 71 return ui::ET_TOUCH_CANCELLED; |
| 72 } |
| 73 |
| 74 void SetPointerDataLocationFromEvent(const ui::LocatedEvent& located_event, |
| 75 PointerData* pointer_data) { |
| 76 pointer_data->x = located_event.location_f().x(); |
| 77 pointer_data->y = located_event.location_f().y(); |
| 78 pointer_data->screen_x = located_event.root_location_f().x(); |
| 79 pointer_data->screen_y = located_event.root_location_f().y(); |
| 80 } |
| 81 |
| 82 } // namespace |
19 | 83 |
20 COMPILE_ASSERT(static_cast<int32>(EVENT_FLAGS_NONE) == | 84 COMPILE_ASSERT(static_cast<int32>(EVENT_FLAGS_NONE) == |
21 static_cast<int32>(ui::EF_NONE), | 85 static_cast<int32>(ui::EF_NONE), |
22 event_flags_should_match); | 86 event_flags_should_match); |
23 COMPILE_ASSERT(static_cast<int32>(EVENT_FLAGS_CAPS_LOCK_DOWN) == | 87 COMPILE_ASSERT(static_cast<int32>(EVENT_FLAGS_CAPS_LOCK_DOWN) == |
24 static_cast<int32>(ui::EF_CAPS_LOCK_DOWN), | 88 static_cast<int32>(ui::EF_CAPS_LOCK_DOWN), |
25 event_flags_should_match); | 89 event_flags_should_match); |
26 COMPILE_ASSERT(static_cast<int32>(EVENT_FLAGS_SHIFT_DOWN) == | 90 COMPILE_ASSERT(static_cast<int32>(EVENT_FLAGS_SHIFT_DOWN) == |
27 static_cast<int32>(ui::EF_SHIFT_DOWN), | 91 static_cast<int32>(ui::EF_SHIFT_DOWN), |
28 event_flags_should_match); | 92 event_flags_should_match); |
(...skipping 24 matching lines...) Expand all Loading... |
53 COMPILE_ASSERT(static_cast<int32>(EVENT_FLAGS_ALTGR_DOWN) == | 117 COMPILE_ASSERT(static_cast<int32>(EVENT_FLAGS_ALTGR_DOWN) == |
54 static_cast<int32>(ui::EF_ALTGR_DOWN), | 118 static_cast<int32>(ui::EF_ALTGR_DOWN), |
55 event_flags_should_match); | 119 event_flags_should_match); |
56 COMPILE_ASSERT(static_cast<int32>(EVENT_FLAGS_MOD3_DOWN) == | 120 COMPILE_ASSERT(static_cast<int32>(EVENT_FLAGS_MOD3_DOWN) == |
57 static_cast<int32>(ui::EF_MOD3_DOWN), | 121 static_cast<int32>(ui::EF_MOD3_DOWN), |
58 event_flags_should_match); | 122 event_flags_should_match); |
59 | 123 |
60 | 124 |
61 // static | 125 // static |
62 EventType TypeConverter<EventType, ui::EventType>::Convert(ui::EventType type) { | 126 EventType TypeConverter<EventType, ui::EventType>::Convert(ui::EventType type) { |
63 #define MOJO_INPUT_EVENT_NAME(name) case ui::ET_##name: return EVENT_TYPE_##name | 127 switch (type) { |
| 128 case ui::ET_MOUSE_PRESSED: |
| 129 case ui::ET_TOUCH_PRESSED: |
| 130 return EVENT_TYPE_POINTER_DOWN; |
64 | 131 |
65 switch (type) { | 132 case ui::ET_MOUSE_DRAGGED: |
66 #include "mojo/converters/input_events/input_event_names.h" | 133 case ui::ET_MOUSE_MOVED: |
67 case ui::ET_LAST: | 134 case ui::ET_MOUSE_ENTERED: |
68 NOTREACHED(); | 135 case ui::ET_MOUSE_EXITED: |
| 136 case ui::ET_TOUCH_MOVED: |
| 137 case ui::ET_MOUSEWHEEL: |
| 138 return EVENT_TYPE_POINTER_MOVE; |
| 139 |
| 140 case ui::ET_MOUSE_RELEASED: |
| 141 case ui::ET_TOUCH_RELEASED: |
| 142 return EVENT_TYPE_POINTER_UP; |
| 143 |
| 144 case ui::ET_TOUCH_CANCELLED: |
| 145 return EVENT_TYPE_POINTER_CANCEL; |
| 146 |
| 147 case ui::ET_KEY_PRESSED: |
| 148 return EVENT_TYPE_KEY_PRESSED; |
| 149 |
| 150 case ui::ET_KEY_RELEASED: |
| 151 return EVENT_TYPE_KEY_RELEASED; |
| 152 |
| 153 default: |
69 break; | 154 break; |
70 } | 155 } |
71 | |
72 #undef MOJO_INPUT_EVENT_NAME | |
73 | |
74 NOTREACHED(); | |
75 return EVENT_TYPE_UNKNOWN; | 156 return EVENT_TYPE_UNKNOWN; |
76 } | 157 } |
77 | 158 |
78 // static | 159 EventPtr TypeConverter<EventPtr, ui::Event>::Convert(const ui::Event& input) { |
79 ui::EventType TypeConverter<ui::EventType, EventType>::Convert(EventType type) { | 160 const EventType type = ConvertTo<EventType>(input.type()); |
80 #define MOJO_INPUT_EVENT_NAME(name) case EVENT_TYPE_##name: return ui::ET_##name | 161 if (type == EVENT_TYPE_UNKNOWN) |
| 162 return nullptr; |
81 | 163 |
82 switch (type) { | 164 EventPtr event = Event::New(); |
83 #include "mojo/converters/input_events/input_event_names.h" | 165 event->action = type; |
84 } | |
85 | |
86 #undef MOJO_INPUT_EVENT_NAME | |
87 | |
88 NOTREACHED(); | |
89 return ui::ET_UNKNOWN; | |
90 } | |
91 | |
92 // static | |
93 EventPtr TypeConverter<EventPtr, ui::Event>::Convert(const ui::Event& input) { | |
94 EventPtr event(Event::New()); | |
95 event->action = ConvertTo<EventType>(input.type()); | |
96 event->flags = EventFlags(input.flags()); | 166 event->flags = EventFlags(input.flags()); |
97 event->time_stamp = input.time_stamp().ToInternalValue(); | 167 event->time_stamp = input.time_stamp().ToInternalValue(); |
98 | 168 |
99 if (input.IsMouseEvent() || input.IsTouchEvent()) { | 169 PointerData pointer_data; |
| 170 if (input.IsMouseEvent()) { |
100 const ui::LocatedEvent* located_event = | 171 const ui::LocatedEvent* located_event = |
101 static_cast<const ui::LocatedEvent*>(&input); | 172 static_cast<const ui::LocatedEvent*>(&input); |
102 | 173 PointerDataPtr pointer_data(PointerData::New()); |
103 LocationDataPtr location_data(LocationData::New()); | 174 // TODO(sky): come up with a better way to handle this. |
104 location_data->in_view_location = Point::From(located_event->location()); | 175 pointer_data->pointer_id = std::numeric_limits<int32>::max(); |
105 if (input.HasNativeEvent()) { | 176 pointer_data->kind = POINTER_KIND_MOUSE; |
106 location_data->screen_location = | 177 SetPointerDataLocationFromEvent(*located_event, pointer_data.get()); |
107 Point::From(ui::EventSystemLocationFromNative(input.native_event())); | 178 if (input.IsMouseWheelEvent()) { |
| 179 const ui::MouseWheelEvent* wheel_event = |
| 180 static_cast<const ui::MouseWheelEvent*>(&input); |
| 181 // This conversion assumes we're using the mojo meaning of these values: |
| 182 // [-1 1]. |
| 183 pointer_data->horizontal_wheel = |
| 184 static_cast<float>(wheel_event->x_offset()) / 100.0f; |
| 185 pointer_data->vertical_wheel = |
| 186 static_cast<float>(wheel_event->y_offset()) / 100.0f; |
108 } | 187 } |
109 | 188 event->pointer_data = pointer_data.Pass(); |
110 event->location_data = location_data.Pass(); | 189 } else if (input.IsTouchEvent()) { |
111 } | |
112 | |
113 if (input.IsTouchEvent()) { | |
114 const ui::TouchEvent* touch_event = | 190 const ui::TouchEvent* touch_event = |
115 static_cast<const ui::TouchEvent*>(&input); | 191 static_cast<const ui::TouchEvent*>(&input); |
116 TouchDataPtr touch_data(TouchData::New()); | 192 PointerDataPtr pointer_data(PointerData::New()); |
117 touch_data->pointer_id = touch_event->touch_id(); | 193 pointer_data->pointer_id = touch_event->touch_id(); |
118 event->touch_data = touch_data.Pass(); | 194 pointer_data->kind = POINTER_KIND_TOUCH; |
| 195 SetPointerDataLocationFromEvent(*touch_event, pointer_data.get()); |
| 196 pointer_data->radius_major = touch_event->radius_x(); |
| 197 pointer_data->radius_minor = touch_event->radius_y(); |
| 198 pointer_data->pressure = touch_event->force(); |
| 199 pointer_data->orientation = touch_event->rotation_angle(); |
| 200 event->pointer_data = pointer_data.Pass(); |
119 } else if (input.IsKeyEvent()) { | 201 } else if (input.IsKeyEvent()) { |
120 const ui::KeyEvent* key_event = static_cast<const ui::KeyEvent*>(&input); | 202 const ui::KeyEvent* key_event = static_cast<const ui::KeyEvent*>(&input); |
121 KeyDataPtr key_data(KeyData::New()); | 203 KeyDataPtr key_data(KeyData::New()); |
122 key_data->key_code = key_event->GetConflatedWindowsKeyCode(); | 204 key_data->key_code = key_event->GetConflatedWindowsKeyCode(); |
123 key_data->native_key_code = key_event->platform_keycode(); | 205 key_data->native_key_code = key_event->platform_keycode(); |
124 key_data->is_char = key_event->is_char(); | 206 key_data->is_char = key_event->is_char(); |
125 key_data->character = key_event->GetCharacter(); | 207 key_data->character = key_event->GetCharacter(); |
126 | 208 |
127 if (key_event->extended_key_event_data()) { | 209 if (key_event->extended_key_event_data()) { |
128 const MojoExtendedKeyEventData* data = | 210 const MojoExtendedKeyEventData* data = |
129 static_cast<const MojoExtendedKeyEventData*>( | 211 static_cast<const MojoExtendedKeyEventData*>( |
130 key_event->extended_key_event_data()); | 212 key_event->extended_key_event_data()); |
131 key_data->windows_key_code = static_cast<mojo::KeyboardCode>( | 213 key_data->windows_key_code = static_cast<mojo::KeyboardCode>( |
132 data->windows_key_code()); | 214 data->windows_key_code()); |
133 key_data->text = data->text(); | 215 key_data->text = data->text(); |
134 key_data->unmodified_text = data->unmodified_text(); | 216 key_data->unmodified_text = data->unmodified_text(); |
135 } else { | 217 } else { |
136 key_data->windows_key_code = static_cast<mojo::KeyboardCode>( | 218 key_data->windows_key_code = static_cast<mojo::KeyboardCode>( |
137 key_event->GetLocatedWindowsKeyboardCode()); | 219 key_event->GetLocatedWindowsKeyboardCode()); |
138 key_data->text = key_event->GetText(); | 220 key_data->text = key_event->GetText(); |
139 key_data->unmodified_text = key_event->GetUnmodifiedText(); | 221 key_data->unmodified_text = key_event->GetUnmodifiedText(); |
140 } | 222 } |
141 | |
142 event->key_data = key_data.Pass(); | 223 event->key_data = key_data.Pass(); |
143 } else if (input.IsMouseWheelEvent()) { | |
144 const ui::MouseWheelEvent* wheel_event = | |
145 static_cast<const ui::MouseWheelEvent*>(&input); | |
146 MouseWheelDataPtr wheel_data(MouseWheelData::New()); | |
147 wheel_data->x_offset = wheel_event->x_offset(); | |
148 wheel_data->y_offset = wheel_event->y_offset(); | |
149 event->wheel_data = wheel_data.Pass(); | |
150 } | 224 } |
151 return event.Pass(); | 225 return event.Pass(); |
152 } | 226 } |
153 | 227 |
154 // static | 228 // static |
155 EventPtr TypeConverter<EventPtr, ui::KeyEvent>::Convert( | 229 EventPtr TypeConverter<EventPtr, ui::KeyEvent>::Convert( |
156 const ui::KeyEvent& input) { | 230 const ui::KeyEvent& input) { |
157 return Event::From(static_cast<const ui::Event&>(input)); | 231 return Event::From(static_cast<const ui::Event&>(input)); |
158 } | 232 } |
159 | 233 |
160 // static | 234 // static |
161 scoped_ptr<ui::Event> TypeConverter<scoped_ptr<ui::Event>, EventPtr>::Convert( | 235 scoped_ptr<ui::Event> TypeConverter<scoped_ptr<ui::Event>, EventPtr>::Convert( |
162 const EventPtr& input) { | 236 const EventPtr& input) { |
163 scoped_ptr<ui::Event> ui_event; | 237 gfx::PointF location; |
164 ui::EventType ui_event_type = ConvertTo<ui::EventType>(input->action); | 238 gfx::PointF screen_location; |
165 | 239 if (!input->pointer_data.is_null()) { |
166 gfx::Point location; | 240 location.SetPoint(input->pointer_data->x, input->pointer_data->y); |
167 if (!input->location_data.is_null() && | 241 screen_location.SetPoint(input->pointer_data->screen_x, |
168 !input->location_data->in_view_location.is_null()) { | 242 input->pointer_data->screen_y); |
169 location = input->location_data->in_view_location.To<gfx::Point>(); | |
170 } | 243 } |
171 | 244 |
172 switch (input->action) { | 245 switch (input->action) { |
173 case ui::ET_KEY_PRESSED: | 246 case EVENT_TYPE_KEY_PRESSED: |
174 case ui::ET_KEY_RELEASED: { | 247 case EVENT_TYPE_KEY_RELEASED: { |
175 scoped_ptr<ui::KeyEvent> key_event; | 248 scoped_ptr<ui::KeyEvent> key_event; |
176 if (input->key_data->is_char) { | 249 if (input->key_data->is_char) { |
177 key_event.reset(new ui::KeyEvent( | 250 key_event.reset(new ui::KeyEvent( |
178 static_cast<base::char16>(input->key_data->character), | 251 static_cast<base::char16>(input->key_data->character), |
179 static_cast<ui::KeyboardCode>( | 252 static_cast<ui::KeyboardCode>( |
180 input->key_data->key_code), | 253 input->key_data->key_code), |
181 input->flags)); | 254 input->flags)); |
182 } else { | 255 } else { |
183 key_event.reset(new ui::KeyEvent( | 256 key_event.reset(new ui::KeyEvent( |
184 ui_event_type, | 257 input->action == EVENT_TYPE_KEY_PRESSED ? ui::ET_KEY_PRESSED |
185 static_cast<ui::KeyboardCode>( | 258 : ui::ET_KEY_RELEASED, |
186 input->key_data->key_code), | 259 |
| 260 static_cast<ui::KeyboardCode>(input->key_data->key_code), |
187 input->flags)); | 261 input->flags)); |
188 } | 262 } |
189 key_event->SetExtendedKeyEventData(scoped_ptr<ui::ExtendedKeyEventData>( | 263 key_event->SetExtendedKeyEventData(scoped_ptr<ui::ExtendedKeyEventData>( |
190 new MojoExtendedKeyEventData( | 264 new MojoExtendedKeyEventData( |
191 static_cast<int32_t>(input->key_data->windows_key_code), | 265 static_cast<int32_t>(input->key_data->windows_key_code), |
192 input->key_data->text, | 266 input->key_data->text, |
193 input->key_data->unmodified_text))); | 267 input->key_data->unmodified_text))); |
194 key_event->set_platform_keycode(input->key_data->native_key_code); | 268 key_event->set_platform_keycode(input->key_data->native_key_code); |
195 ui_event = key_event.Pass(); | 269 return key_event.Pass(); |
196 break; | |
197 } | 270 } |
198 case EVENT_TYPE_MOUSE_PRESSED: | 271 case EVENT_TYPE_POINTER_DOWN: |
199 case EVENT_TYPE_MOUSE_DRAGGED: | 272 case EVENT_TYPE_POINTER_UP: |
200 case EVENT_TYPE_MOUSE_RELEASED: | 273 case EVENT_TYPE_POINTER_MOVE: |
201 case EVENT_TYPE_MOUSE_MOVED: | 274 case EVENT_TYPE_POINTER_CANCEL: { |
202 case EVENT_TYPE_MOUSE_ENTERED: | 275 if (input->pointer_data->kind == POINTER_KIND_MOUSE) { |
203 case EVENT_TYPE_MOUSE_EXITED: { | 276 // TODO: last flags isn't right. Need to send changed_flags. |
204 // TODO: last flags isn't right. Need to send changed_flags. | 277 scoped_ptr<ui::MouseEvent> event(new ui::MouseEvent( |
205 ui_event.reset(new ui::MouseEvent( | 278 MojoMouseEventTypeToUIEvent(input), location, screen_location, |
206 ui_event_type, | 279 ui::EventTimeForNow(), ui::EventFlags(input->flags), |
207 location, | 280 ui::EventFlags(input->flags))); |
208 location, | 281 if (event->IsMouseWheelEvent()) { |
209 ui::EventFlags(input->flags), | 282 // This conversion assumes we're using the mojo meaning of these |
210 ui::EventFlags(input->flags))); | 283 // values: [-1 1]. |
211 break; | 284 scoped_ptr<ui::MouseEvent> wheel_event(new ui::MouseWheelEvent( |
212 } | 285 *event, |
213 case EVENT_TYPE_MOUSEWHEEL: { | 286 static_cast<int>(input->pointer_data->horizontal_wheel * 100), |
214 const gfx::Vector2d offset(input->wheel_data->x_offset, | 287 static_cast<int>(input->pointer_data->vertical_wheel * 100))); |
215 input->wheel_data->y_offset); | 288 event = wheel_event.Pass(); |
216 ui_event.reset(new ui::MouseWheelEvent(offset, | 289 } |
217 location, | 290 return event.Pass(); |
218 location, | 291 } |
219 ui::EventFlags(input->flags), | 292 scoped_ptr<ui::TouchEvent> touch_event(new ui::TouchEvent( |
220 ui::EventFlags(input->flags))); | 293 MojoTouchEventTypeToUIEvent(input), location, |
221 break; | 294 ui::EventFlags(input->flags), input->pointer_data->pointer_id, |
222 } | 295 base::TimeDelta::FromInternalValue(input->time_stamp), |
223 case EVENT_TYPE_TOUCH_MOVED: | 296 input->pointer_data->radius_major, input->pointer_data->radius_minor, |
224 case EVENT_TYPE_TOUCH_PRESSED: | 297 input->pointer_data->orientation, input->pointer_data->pressure)); |
225 case EVENT_TYPE_TOUCH_CANCELLED: | 298 touch_event->set_root_location(screen_location); |
226 case EVENT_TYPE_TOUCH_RELEASED: { | 299 return touch_event.Pass(); |
227 ui_event.reset(new ui::TouchEvent( | |
228 ui_event_type, | |
229 location, | |
230 ui::EventFlags(input->flags), | |
231 input->touch_data->pointer_id, | |
232 base::TimeDelta::FromInternalValue(input->time_stamp), | |
233 0.f, 0.f, 0.f, 0.f)); | |
234 break; | |
235 } | 300 } |
236 default: | 301 default: |
237 // TODO: support other types. | 302 NOTIMPLEMENTED(); |
238 // NOTIMPLEMENTED(); | |
239 ; | |
240 } | 303 } |
241 // TODO: need to support time_stamp. | 304 // TODO: need to support time_stamp. |
242 return ui_event.Pass(); | 305 return nullptr; |
243 } | 306 } |
244 | 307 |
245 } // namespace mojo | 308 } // namespace mojo |
OLD | NEW |