OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/renderer_host/render_widget_host_view_views.h" | |
6 | |
7 #include <gdk/gdkx.h> | |
8 #include <gtk/gtk.h> | |
9 | |
10 #include "base/logging.h" | |
11 #include "content/browser/renderer_host/gtk_window_utils.h" | |
12 #include "content/browser/renderer_host/render_widget_host.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/gtk/WebInputEventFact
ory.h" | |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h" | |
15 #include "views/widget/widget.h" | |
16 | |
17 static const char kRenderWidgetHostViewKey[] = "__RENDER_WIDGET_HOST_VIEW__"; | |
18 | |
19 using WebKit::WebInputEventFactory; | |
20 using WebKit::WebTouchEvent; | |
21 | |
22 namespace { | |
23 | |
24 WebKit::WebTouchPoint::State TouchPointStateFromEvent( | |
25 const views::TouchEvent* event) { | |
26 switch (event->type()) { | |
27 case ui::ET_TOUCH_PRESSED: | |
28 return WebKit::WebTouchPoint::StatePressed; | |
29 case ui::ET_TOUCH_RELEASED: | |
30 return WebKit::WebTouchPoint::StateReleased; | |
31 case ui::ET_TOUCH_MOVED: | |
32 return WebKit::WebTouchPoint::StateMoved; | |
33 case ui::ET_TOUCH_CANCELLED: | |
34 return WebKit::WebTouchPoint::StateCancelled; | |
35 default: | |
36 return WebKit::WebTouchPoint::StateUndefined; | |
37 } | |
38 } | |
39 | |
40 WebKit::WebInputEvent::Type TouchEventTypeFromEvent( | |
41 const views::TouchEvent* event) { | |
42 switch (event->type()) { | |
43 case ui::ET_TOUCH_PRESSED: | |
44 return WebKit::WebInputEvent::TouchStart; | |
45 case ui::ET_TOUCH_RELEASED: | |
46 return WebKit::WebInputEvent::TouchEnd; | |
47 case ui::ET_TOUCH_MOVED: | |
48 return WebKit::WebInputEvent::TouchMove; | |
49 case ui::ET_TOUCH_CANCELLED: | |
50 return WebKit::WebInputEvent::TouchCancel; | |
51 default: | |
52 return WebKit::WebInputEvent::Undefined; | |
53 } | |
54 } | |
55 | |
56 inline void UpdateTouchParams(const views::TouchEvent& event, | |
57 WebKit::WebTouchPoint* tpoint) { | |
58 tpoint->radiusX = event.radius_x(); | |
59 tpoint->radiusY = event.radius_y(); | |
60 tpoint->rotationAngle = event.rotation_angle(); | |
61 tpoint->force = event.force(); | |
62 } | |
63 | |
64 void UpdateTouchPointPosition(const views::TouchEvent* event, | |
65 const gfx::Point& origin, | |
66 WebKit::WebTouchPoint* tpoint) { | |
67 tpoint->position.x = event->x(); | |
68 tpoint->position.y = event->y(); | |
69 | |
70 tpoint->screenPosition.x = tpoint->position.x + origin.x(); | |
71 tpoint->screenPosition.y = tpoint->position.y + origin.y(); | |
72 } | |
73 | |
74 } // namespace | |
75 | |
76 ui::TouchStatus RenderWidgetHostViewViews::OnTouchEvent( | |
77 const views::TouchEvent& event) { | |
78 if (!host_) | |
79 return ui::TOUCH_STATUS_UNKNOWN; | |
80 | |
81 // Update the list of touch points first. | |
82 WebKit::WebTouchPoint* point = NULL; | |
83 ui::TouchStatus status = ui::TOUCH_STATUS_UNKNOWN; | |
84 | |
85 switch (event.type()) { | |
86 case ui::ET_TOUCH_PRESSED: | |
87 // Add a new touch point. | |
88 if (touch_event_.touchesLength < | |
89 WebTouchEvent::touchesLengthCap) { | |
90 point = &touch_event_.touches[touch_event_.touchesLength++]; | |
91 point->id = event.identity(); | |
92 | |
93 if (touch_event_.touchesLength == 1) { | |
94 // A new touch sequence has started. | |
95 status = ui::TOUCH_STATUS_START; | |
96 | |
97 // We also want the focus. | |
98 RequestFocus(); | |
99 | |
100 // Confirm existing composition text on touch press events, to make | |
101 // sure the input caret won't be moved with an ongoing composition | |
102 // text. | |
103 FinishImeCompositionSession(); | |
104 } | |
105 } | |
106 break; | |
107 case ui::ET_TOUCH_RELEASED: | |
108 case ui::ET_TOUCH_CANCELLED: | |
109 case ui::ET_TOUCH_MOVED: { | |
110 // The touch point should have been added to the event from an earlier | |
111 // _PRESSED event. So find that. | |
112 // At the moment, only a maximum of 4 touch-points are allowed. So a | |
113 // simple loop should be sufficient. | |
114 for (unsigned i = 0; i < touch_event_.touchesLength; ++i) { | |
115 point = touch_event_.touches + i; | |
116 if (point->id == event.identity()) { | |
117 break; | |
118 } | |
119 point = NULL; | |
120 } | |
121 break; | |
122 } | |
123 default: | |
124 DLOG(WARNING) << "Unknown touch event " << event.type(); | |
125 break; | |
126 } | |
127 | |
128 if (!point) | |
129 return ui::TOUCH_STATUS_UNKNOWN; | |
130 | |
131 if (status != ui::TOUCH_STATUS_START) | |
132 status = ui::TOUCH_STATUS_CONTINUE; | |
133 | |
134 UpdateTouchParams(event, point); | |
135 | |
136 // Update the location and state of the point. | |
137 point->state = TouchPointStateFromEvent(&event); | |
138 if (point->state == WebKit::WebTouchPoint::StateMoved) { | |
139 // It is possible for badly written touch drivers to emit Move events even | |
140 // when the touch location hasn't changed. In such cases, consume the event | |
141 // and pretend nothing happened. | |
142 if (point->position.x == event.x() && point->position.y == event.y()) { | |
143 return status; | |
144 } | |
145 } | |
146 UpdateTouchPointPosition(&event, GetMirroredPosition(), point); | |
147 | |
148 // Mark the rest of the points as stationary. | |
149 for (unsigned i = 0; i < touch_event_.touchesLength; ++i) { | |
150 WebKit::WebTouchPoint* iter = touch_event_.touches + i; | |
151 if (iter != point) { | |
152 iter->state = WebKit::WebTouchPoint::StateStationary; | |
153 } | |
154 } | |
155 | |
156 // Update the type of the touch event. | |
157 touch_event_.type = TouchEventTypeFromEvent(&event); | |
158 touch_event_.timeStampSeconds = base::Time::Now().ToDoubleT(); | |
159 | |
160 // The event and all the touches have been updated. Dispatch. | |
161 host_->ForwardTouchEvent(touch_event_); | |
162 | |
163 // If the touch was released, then remove it from the list of touch points. | |
164 if (event.type() == ui::ET_TOUCH_RELEASED) { | |
165 --touch_event_.touchesLength; | |
166 for (unsigned i = point - touch_event_.touches; | |
167 i < touch_event_.touchesLength; | |
168 ++i) { | |
169 touch_event_.touches[i] = touch_event_.touches[i + 1]; | |
170 } | |
171 if (touch_event_.touchesLength == 0) | |
172 status = ui::TOUCH_STATUS_END; | |
173 } else if (event.type() == ui::ET_TOUCH_CANCELLED) { | |
174 status = ui::TOUCH_STATUS_CANCEL; | |
175 } | |
176 | |
177 return status; | |
178 } | |
OLD | NEW |