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

Side by Side Diff: views/events/event_x.cc

Issue 7942004: Consolidate/cleanup event cracking code; single out GdkEvents. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: KeyboardCodeFromNative, Wayland, cleanup and consolidate. Created 9 years, 3 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "views/events/event.h" 5 #include "views/events/event.h"
6 6
7 #include <gdk/gdk.h> 7 #include <gdk/gdk.h>
8 #include <gdk/gdkx.h> 8 #include <gdk/gdkx.h>
9 #include <X11/extensions/XInput2.h> 9 #include <X11/extensions/XInput2.h>
10 #include <X11/Xlib.h> 10 #include <X11/Xlib.h>
11 11
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "ui/base/keycodes/keyboard_code_conversion_x.h" 14 #include "ui/base/keycodes/keyboard_code_conversion_x.h"
15 #include "views/touchui/touch_factory.h" 15 #include "ui/base/touchui/touch_factory.h"
16 #include "views/widget/root_view.h" 16 #include "views/widget/root_view.h"
17 17
18 namespace views { 18 namespace views {
19 19
20 namespace { 20 namespace {
21 21
22 // Scroll amount for each wheelscroll event. 53 is also the value used for GTK+.
23 static int kWheelScrollAmount = 53;
24
25 int GetEventFlagsFromXState(unsigned int state) {
26 int flags = 0;
27 if (state & ControlMask)
28 flags |= ui::EF_CONTROL_DOWN;
29 if (state & ShiftMask)
30 flags |= ui::EF_SHIFT_DOWN;
31 if (state & Mod1Mask)
32 flags |= ui::EF_ALT_DOWN;
33 if (state & LockMask)
34 flags |= ui::EF_CAPS_LOCK_DOWN;
35 if (state & Button1Mask)
36 flags |= ui::EF_LEFT_BUTTON_DOWN;
37 if (state & Button2Mask)
38 flags |= ui::EF_MIDDLE_BUTTON_DOWN;
39 if (state & Button3Mask)
40 flags |= ui::EF_RIGHT_BUTTON_DOWN;
41
42 return flags;
43 }
44
45 // Get the event flag for the button in XButtonEvent. During a ButtonPress
46 // event, |state| in XButtonEvent does not include the button that has just been
47 // pressed. Instead |state| contains flags for the buttons (if any) that had
48 // already been pressed before the current button, and |button| stores the most
49 // current pressed button. So, if you press down left mouse button, and while
50 // pressing it down, press down the right mouse button, then for the latter
51 // event, |state| would have Button1Mask set but not Button3Mask, and |button|
52 // would be 3.
53 int GetEventFlagsForButton(int button) {
54 switch (button) {
55 case 1:
56 return ui::EF_LEFT_BUTTON_DOWN;
57 case 2:
58 return ui::EF_MIDDLE_BUTTON_DOWN;
59 case 3:
60 return ui::EF_RIGHT_BUTTON_DOWN;
61 }
62
63 DLOG(WARNING) << "Unexpected button (" << button << ") received.";
64 return 0;
65 }
66
67 int GetButtonMaskForX2Event(XIDeviceEvent* xievent) {
68 int buttonflags = 0;
69
70 for (int i = 0; i < 8 * xievent->buttons.mask_len; i++) {
71 if (XIMaskIsSet(xievent->buttons.mask, i)) {
72 buttonflags |= GetEventFlagsForButton(i);
73 }
74 }
75
76 return buttonflags;
77 }
78
79 ui::EventType GetTouchEventType(XEvent* xev) {
80 XGenericEventCookie* cookie = &xev->xcookie;
81 DCHECK_EQ(cookie->evtype, XI_Motion);
82
83 // Note: We will not generate a _STATIONARY event here. It will be created,
84 // when necessary, by a RWHVV.
85 // TODO(sad): When should _CANCELLED be generated?
86
87 TouchFactory* factory = TouchFactory::GetInstance();
88 float slot;
89 if (!factory->ExtractTouchParam(*xev, TouchFactory::TP_SLOT_ID, &slot))
90 return ui::ET_UNKNOWN;
91
92 if (!factory->IsSlotUsed(slot)) {
93 // This is a new touch point.
94 return ui::ET_TOUCH_PRESSED;
95 }
96
97 float tracking;
98 if (!factory->ExtractTouchParam(*xev, TouchFactory::TP_TRACKING_ID,
99 &tracking))
100 return ui::ET_UNKNOWN;
101
102 if (tracking == 0l) {
103 // The touch point has been released.
104 return ui::ET_TOUCH_RELEASED;
105 }
106
107 return ui::ET_TOUCH_MOVED;
108 }
109
110 int GetTouchIDFromXEvent(XEvent* xev) { 22 int GetTouchIDFromXEvent(XEvent* xev) {
111 float slot = 0; 23 float slot = 0;
112 if (!TouchFactory::GetInstance()->ExtractTouchParam( 24 if (!ui::TouchFactory::GetInstance()->ExtractTouchParam(
113 *xev, TouchFactory::TP_SLOT_ID, &slot)) 25 *xev, ui::TouchFactory::TP_SLOT_ID, &slot))
114 LOG(ERROR) << "Could not get the slot ID for the event. Using 0."; 26 LOG(ERROR) << "Could not get the slot ID for the event. Using 0.";
115 return slot; 27 return slot;
116 } 28 }
117 29
118 ui::EventType EventTypeFromNative(NativeEvent2 native_event) {
119 switch (native_event->type) {
120 case KeyPress:
121 return ui::ET_KEY_PRESSED;
122 case KeyRelease:
123 return ui::ET_KEY_RELEASED;
124 case ButtonPress:
125 if (native_event->xbutton.button == 4 ||
126 native_event->xbutton.button == 5)
127 return ui::ET_MOUSEWHEEL;
128 return ui::ET_MOUSE_PRESSED;
129 case ButtonRelease:
130 if (native_event->xbutton.button == 4 ||
131 native_event->xbutton.button == 5)
132 return ui::ET_MOUSEWHEEL;
133 return ui::ET_MOUSE_RELEASED;
134 case MotionNotify:
135 if (native_event->xmotion.state &
136 (Button1Mask | Button2Mask | Button3Mask))
137 return ui::ET_MOUSE_DRAGGED;
138 return ui::ET_MOUSE_MOVED;
139 case GenericEvent: {
140 XIDeviceEvent* xievent =
141 static_cast<XIDeviceEvent*>(native_event->xcookie.data);
142 if (TouchFactory::GetInstance()->IsTouchDevice(xievent->sourceid))
143 return GetTouchEventType(native_event);
144 switch (xievent->evtype) {
145 case XI_ButtonPress:
146 return (xievent->detail == 4 || xievent->detail == 5) ?
147 ui::ET_MOUSEWHEEL : ui::ET_MOUSE_PRESSED;
148 case XI_ButtonRelease:
149 return (xievent->detail == 4 || xievent->detail == 5) ?
150 ui::ET_MOUSEWHEEL : ui::ET_MOUSE_RELEASED;
151 case XI_Motion:
152 return GetButtonMaskForX2Event(xievent) ? ui::ET_MOUSE_DRAGGED :
153 ui::ET_MOUSE_MOVED;
154 }
155 }
156 default:
157 NOTREACHED();
158 break;
159 }
160 return ui::ET_UNKNOWN;
161 }
162
163 int GetMouseWheelOffset(XEvent* xev) {
164 if (xev->type == GenericEvent) {
165 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev->xcookie.data);
166 return xiev->detail == 4 ? kWheelScrollAmount : -kWheelScrollAmount;
167 }
168 return xev->xbutton.button == 4 ? kWheelScrollAmount : -kWheelScrollAmount;
169 }
170
171 gfx::Point GetEventLocation(XEvent* xev) {
172 switch (xev->type) {
173 case ButtonPress:
174 case ButtonRelease:
175 return gfx::Point(xev->xbutton.x, xev->xbutton.y);
176
177 case MotionNotify:
178 return gfx::Point(xev->xmotion.x, xev->xmotion.y);
179
180 case GenericEvent: {
181 XIDeviceEvent* xievent =
182 static_cast<XIDeviceEvent*>(xev->xcookie.data);
183 return gfx::Point(static_cast<int>(xievent->event_x),
184 static_cast<int>(xievent->event_y));
185 }
186 }
187
188 return gfx::Point();
189 }
190
191 int GetLocatedEventFlags(XEvent* xev) {
192 switch (xev->type) {
193 case ButtonPress:
194 case ButtonRelease:
195 return GetEventFlagsFromXState(xev->xbutton.state) |
196 GetEventFlagsForButton(xev->xbutton.button);
197
198 case MotionNotify:
199 return GetEventFlagsFromXState(xev->xmotion.state);
200
201 case GenericEvent: {
202 XIDeviceEvent* xievent = static_cast<XIDeviceEvent*>(xev->xcookie.data);
203 bool touch =
204 TouchFactory::GetInstance()->IsTouchDevice(xievent->sourceid);
205 switch (xievent->evtype) {
206 case XI_ButtonPress:
207 case XI_ButtonRelease:
208 return GetButtonMaskForX2Event(xievent) |
209 GetEventFlagsFromXState(xievent->mods.effective) |
210 (touch ? 0 : GetEventFlagsForButton(xievent->detail));
211
212 case XI_Motion:
213 return GetButtonMaskForX2Event(xievent) |
214 GetEventFlagsFromXState(xievent->mods.effective);
215 }
216 }
217 }
218
219 return 0;
220 }
221
222 uint16 GetCharacterFromXKeyEvent(XKeyEvent* key) { 30 uint16 GetCharacterFromXKeyEvent(XKeyEvent* key) {
223 char buf[6]; 31 char buf[6];
224 int bytes_written = XLookupString(key, buf, 6, NULL, NULL); 32 int bytes_written = XLookupString(key, buf, 6, NULL, NULL);
225 DCHECK_LE(bytes_written, 6); 33 DCHECK_LE(bytes_written, 6);
226 34
227 string16 result; 35 string16 result;
228 return (bytes_written > 0 && UTF8ToUTF16(buf, bytes_written, &result) && 36 return (bytes_written > 0 && UTF8ToUTF16(buf, bytes_written, &result) &&
229 result.length() == 1) ? result[0] : 0; 37 result.length() == 1) ? result[0] : 0;
230 } 38 }
231 39
232 float GetTouchParamFromXEvent(XEvent* xev, 40 float GetTouchParamFromXEvent(XEvent* xev,
233 TouchFactory::TouchParam tp, 41 ui::TouchFactory::TouchParam tp,
234 float default_value) { 42 float default_value) {
235 TouchFactory::GetInstance()->ExtractTouchParam(*xev, tp, &default_value); 43 ui::TouchFactory::GetInstance()->ExtractTouchParam(*xev, tp, &default_value);
236 return default_value; 44 return default_value;
237 } 45 }
238 46
239 float GetTouchForceFromXEvent(XEvent* xev) { 47 float GetTouchForceFromXEvent(XEvent* xev) {
240 float force = 0.0; 48 float force = 0.0;
241 force = GetTouchParamFromXEvent(xev, TouchFactory::TP_PRESSURE, 0.0); 49 force = GetTouchParamFromXEvent(xev, ui::TouchFactory::TP_PRESSURE, 0.0);
242 unsigned int deviceid = 50 unsigned int deviceid =
243 static_cast<XIDeviceEvent*>(xev->xcookie.data)->sourceid; 51 static_cast<XIDeviceEvent*>(xev->xcookie.data)->sourceid;
244 // Force is normalized to fall into [0, 1] 52 // Force is normalized to fall into [0, 1]
245 if (!TouchFactory::GetInstance()->NormalizeTouchParam( 53 if (!ui::TouchFactory::GetInstance()->NormalizeTouchParam(
246 deviceid, TouchFactory::TP_PRESSURE, &force)) 54 deviceid, ui::TouchFactory::TP_PRESSURE, &force))
247 force = 0.0; 55 force = 0.0;
248 return force; 56 return force;
249 } 57 }
250 58
251 // The following two functions are copied from event_gtk.cc. These will be 59 // The following two functions are copied from event_gtk.cc. These will be
252 // removed when GTK dependency is removed. 60 // removed when GTK dependency is removed.
253 uint16 GetCharacterFromGdkKeyval(guint keyval) { 61 uint16 GetCharacterFromGdkKeyval(guint keyval) {
254 guint32 ch = gdk_keyval_to_unicode(keyval); 62 guint32 ch = gdk_keyval_to_unicode(keyval);
255 63
256 // We only support BMP characters. 64 // We only support BMP characters.
257 return ch < 0xFFFE ? static_cast<uint16>(ch) : 0; 65 return ch < 0xFFFE ? static_cast<uint16>(ch) : 0;
258 } 66 }
259 67
260 GdkEventKey* GetGdkEventKeyFromNative(NativeEvent native_event) { 68 GdkEventKey* GetGdkEventKeyFromNative(const ui::NativeEvent& native_event) {
261 DCHECK(native_event->type == GDK_KEY_PRESS || 69 DCHECK(native_event->type == GDK_KEY_PRESS ||
262 native_event->type == GDK_KEY_RELEASE); 70 native_event->type == GDK_KEY_RELEASE);
263 return &native_event->key; 71 return &native_event->key;
264 } 72 }
265 73
266 } // namespace 74 } // namespace
267 75
268 //////////////////////////////////////////////////////////////////////////////// 76 ////////////////////////////////////////////////////////////////////////////////
269 // Event, private: 77 // Event, private:
270 78
271 void Event::InitWithNativeEvent2(NativeEvent2 native_event_2, 79 void Event::InitWithNativeEvent2(const ui::NativeEvent2& native_event_2,
272 FromNativeEvent2) { 80 FromNativeEvent2) {
273 native_event_ = NULL; 81 native_event_ = NULL;
274 // TODO(beng): remove once we rid views of Gtk/Gdk. 82 // TODO(beng): remove once we rid views of Gtk/Gdk.
275 native_event_2_ = native_event_2; 83 native_event_2_ = native_event_2;
276 } 84 }
277 85
278 //////////////////////////////////////////////////////////////////////////////// 86 ////////////////////////////////////////////////////////////////////////////////
279 // LocatedEvent, protected: 87 // LocatedEvent, protected:
280 88
281 LocatedEvent::LocatedEvent(NativeEvent2 native_event_2, 89 LocatedEvent::LocatedEvent(const ui::NativeEvent2& native_event_2,
282 FromNativeEvent2 from_native) 90 FromNativeEvent2 from_native)
283 : Event(native_event_2, 91 : Event(native_event_2,
284 EventTypeFromNative(native_event_2), 92 ui::EventTypeFromNative(native_event_2),
285 GetLocatedEventFlags(native_event_2), 93 ui::EventFlagsFromNative(native_event_2),
286 from_native), 94 from_native),
287 location_(GetEventLocation(native_event_2)) { 95 location_(ui::EventLocationFromNative(native_event_2)) {
288 } 96 }
289 97
290 //////////////////////////////////////////////////////////////////////////////// 98 ////////////////////////////////////////////////////////////////////////////////
291 // KeyEvent, public: 99 // KeyEvent, public:
292 100
293 KeyEvent::KeyEvent(NativeEvent2 native_event_2, FromNativeEvent2 from_native) 101 KeyEvent::KeyEvent(const ui::NativeEvent2& native_event_2,
102 FromNativeEvent2 from_native)
294 : Event(native_event_2, 103 : Event(native_event_2,
295 EventTypeFromNative(native_event_2), 104 ui::EventTypeFromNative(native_event_2),
296 GetEventFlagsFromXState(native_event_2->xkey.state), 105 ui::EventFlagsFromNative(native_event_2),
297 from_native), 106 from_native),
298 key_code_(ui::KeyboardCodeFromXKeyEvent(native_event_2)), 107 key_code_(ui::KeyboardCodeFromNative(native_event_2)),
299 character_(0), 108 character_(0),
300 unmodified_character_(0) { 109 unmodified_character_(0) {
301 } 110 }
302 111
303 uint16 KeyEvent::GetCharacter() const { 112 uint16 KeyEvent::GetCharacter() const {
304 if (character_) 113 if (character_)
305 return character_; 114 return character_;
306 115
307 if (!native_event_2()) { 116 if (!native_event_2()) {
308 // This event may have been created from a Gdk event. 117 // This event may have been created from a Gdk event.
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 // used by X11 internally. 174 // used by X11 internally.
366 key.state &= ~kIgnoredModifiers; 175 key.state &= ~kIgnoredModifiers;
367 uint16 ch = GetCharacterFromXKeyEvent(&key); 176 uint16 ch = GetCharacterFromXKeyEvent(&key);
368 return ch ? ch : 177 return ch ? ch :
369 GetCharacterFromKeyCode(key_code_, flags() & ui::EF_SHIFT_DOWN); 178 GetCharacterFromKeyCode(key_code_, flags() & ui::EF_SHIFT_DOWN);
370 } 179 }
371 180
372 //////////////////////////////////////////////////////////////////////////////// 181 ////////////////////////////////////////////////////////////////////////////////
373 // MouseEvent, public: 182 // MouseEvent, public:
374 183
375 MouseEvent::MouseEvent(NativeEvent2 native_event_2, 184 MouseEvent::MouseEvent(const ui::NativeEvent2& native_event_2,
376 FromNativeEvent2 from_native) 185 FromNativeEvent2 from_native)
377 : LocatedEvent(native_event_2, from_native) { 186 : LocatedEvent(native_event_2, from_native) {
378 } 187 }
379 188
380 //////////////////////////////////////////////////////////////////////////////// 189 ////////////////////////////////////////////////////////////////////////////////
381 // MouseWheelEvent, public: 190 // MouseWheelEvent, public:
382 191
383 MouseWheelEvent::MouseWheelEvent(NativeEvent2 native_event_2, 192 MouseWheelEvent::MouseWheelEvent(const ui::NativeEvent2& native_event_2,
384 FromNativeEvent2 from_native) 193 FromNativeEvent2 from_native)
385 : MouseEvent(native_event_2, from_native), 194 : MouseEvent(native_event_2, from_native),
386 offset_(GetMouseWheelOffset(native_event_2)) { 195 offset_(ui::GetMouseWheelOffset(native_event_2)) {
387 } 196 }
388 197
389 //////////////////////////////////////////////////////////////////////////////// 198 ////////////////////////////////////////////////////////////////////////////////
390 // TouchEvent, public: 199 // TouchEvent, public:
391 200
392 TouchEvent::TouchEvent(NativeEvent2 native_event_2, 201 TouchEvent::TouchEvent(const ui::NativeEvent2& native_event_2,
393 FromNativeEvent2 from_native) 202 FromNativeEvent2 from_native)
394 : LocatedEvent(native_event_2, from_native), 203 : LocatedEvent(native_event_2, from_native),
395 touch_id_(GetTouchIDFromXEvent(native_event_2)), 204 touch_id_(GetTouchIDFromXEvent(native_event_2)),
396 radius_x_(GetTouchParamFromXEvent(native_event_2, 205 radius_x_(GetTouchParamFromXEvent(native_event_2,
397 TouchFactory::TP_TOUCH_MAJOR, 206 ui::TouchFactory::TP_TOUCH_MAJOR,
398 2.0) / 2.0), 207 2.0) / 2.0),
399 radius_y_(GetTouchParamFromXEvent(native_event_2, 208 radius_y_(GetTouchParamFromXEvent(native_event_2,
400 TouchFactory::TP_TOUCH_MINOR, 209 ui::TouchFactory::TP_TOUCH_MINOR,
401 2.0) / 2.0), 210 2.0) / 2.0),
402 rotation_angle_(GetTouchParamFromXEvent(native_event_2, 211 rotation_angle_(GetTouchParamFromXEvent(native_event_2,
403 TouchFactory::TP_ORIENTATION, 212 ui::TouchFactory::TP_ORIENTATION,
404 0.0)), 213 0.0)),
405 force_(GetTouchForceFromXEvent(native_event_2)) { 214 force_(GetTouchForceFromXEvent(native_event_2)) {
406 if (type() == ui::ET_TOUCH_PRESSED || type() == ui::ET_TOUCH_RELEASED) { 215 if (type() == ui::ET_TOUCH_PRESSED || type() == ui::ET_TOUCH_RELEASED) {
407 TouchFactory* factory = TouchFactory::GetInstance(); 216 ui::TouchFactory* factory = ui::TouchFactory::GetInstance();
408 float slot; 217 float slot;
409 if (factory->ExtractTouchParam(*native_event_2, 218 if (factory->ExtractTouchParam(*native_event_2,
410 TouchFactory::TP_SLOT_ID, &slot)) { 219 ui::TouchFactory::TP_SLOT_ID, &slot)) {
411 factory->SetSlotUsed(slot, type() == ui::ET_TOUCH_PRESSED); 220 factory->SetSlotUsed(slot, type() == ui::ET_TOUCH_PRESSED);
412 } 221 }
413 } 222 }
414 } 223 }
415 224
416 } // namespace views 225 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698