| 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 "content/browser/renderer_host/input/motion_event_android.h" | 5 #include "content/browser/renderer_host/input/motion_event_android.h" |
| 6 | 6 |
| 7 #include <android/input.h> |
| 8 |
| 7 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 8 #include "base/float_util.h" | 10 #include "base/float_util.h" |
| 9 #include "jni/MotionEvent_jni.h" | 11 #include "jni/MotionEvent_jni.h" |
| 12 #include "ui/events/event_constants.h" |
| 10 | 13 |
| 11 using base::android::AttachCurrentThread; | 14 using base::android::AttachCurrentThread; |
| 12 using namespace JNI_MotionEvent; | 15 using namespace JNI_MotionEvent; |
| 13 | 16 |
| 14 namespace content { | 17 namespace content { |
| 15 namespace { | 18 namespace { |
| 16 | 19 |
| 17 int ToAndroidAction(MotionEventAndroid::Action action) { | 20 int ToAndroidAction(MotionEventAndroid::Action action) { |
| 18 switch (action) { | 21 switch (action) { |
| 19 case MotionEventAndroid::ACTION_DOWN: | 22 case MotionEventAndroid::ACTION_DOWN: |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 result |= MotionEventAndroid::BUTTON_FORWARD; | 85 result |= MotionEventAndroid::BUTTON_FORWARD; |
| 83 if ((button_state & BUTTON_PRIMARY) != 0) | 86 if ((button_state & BUTTON_PRIMARY) != 0) |
| 84 result |= MotionEventAndroid::BUTTON_PRIMARY; | 87 result |= MotionEventAndroid::BUTTON_PRIMARY; |
| 85 if ((button_state & BUTTON_SECONDARY) != 0) | 88 if ((button_state & BUTTON_SECONDARY) != 0) |
| 86 result |= MotionEventAndroid::BUTTON_SECONDARY; | 89 result |= MotionEventAndroid::BUTTON_SECONDARY; |
| 87 if ((button_state & BUTTON_TERTIARY) != 0) | 90 if ((button_state & BUTTON_TERTIARY) != 0) |
| 88 result |= MotionEventAndroid::BUTTON_TERTIARY; | 91 result |= MotionEventAndroid::BUTTON_TERTIARY; |
| 89 return result; | 92 return result; |
| 90 } | 93 } |
| 91 | 94 |
| 95 int FromAndroidMetaState(int meta_state) { |
| 96 int flags = ui::EF_NONE; |
| 97 if ((meta_state & AMETA_SHIFT_ON) != 0) |
| 98 flags |= ui::EF_SHIFT_DOWN; |
| 99 if ((meta_state & AMETA_CTRL_ON) != 0) |
| 100 flags |= ui::EF_CONTROL_DOWN; |
| 101 if ((meta_state & AMETA_ALT_ON) != 0) |
| 102 flags |= ui::EF_ALT_DOWN; |
| 103 if ((meta_state & AMETA_META_ON) != 0) |
| 104 flags |= ui::EF_COMMAND_DOWN; |
| 105 if ((meta_state & AMETA_CAPS_LOCK_ON) != 0) |
| 106 flags |= ui::EF_CAPS_LOCK_DOWN; |
| 107 return flags; |
| 108 } |
| 109 |
| 92 int64 ToAndroidTime(base::TimeTicks time) { | 110 int64 ToAndroidTime(base::TimeTicks time) { |
| 93 return (time - base::TimeTicks()).InMilliseconds(); | 111 return (time - base::TimeTicks()).InMilliseconds(); |
| 94 } | 112 } |
| 95 | 113 |
| 96 base::TimeTicks FromAndroidTime(int64 time_ms) { | 114 base::TimeTicks FromAndroidTime(int64 time_ms) { |
| 97 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms); | 115 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms); |
| 98 } | 116 } |
| 99 | 117 |
| 100 float ToValidFloat(float x) { | 118 float ToValidFloat(float x) { |
| 101 return base::IsNaN(x) ? 0.f : x; | 119 return base::IsNaN(x) ? 0.f : x; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 120 jfloat touch_major_0_pixels, | 138 jfloat touch_major_0_pixels, |
| 121 jfloat touch_major_1_pixels, | 139 jfloat touch_major_1_pixels, |
| 122 jfloat touch_minor_0_pixels, | 140 jfloat touch_minor_0_pixels, |
| 123 jfloat touch_minor_1_pixels, | 141 jfloat touch_minor_1_pixels, |
| 124 jfloat orientation_0_rad, | 142 jfloat orientation_0_rad, |
| 125 jfloat orientation_1_rad, | 143 jfloat orientation_1_rad, |
| 126 jfloat raw_pos_x_pixels, | 144 jfloat raw_pos_x_pixels, |
| 127 jfloat raw_pos_y_pixels, | 145 jfloat raw_pos_y_pixels, |
| 128 jint android_tool_type_0, | 146 jint android_tool_type_0, |
| 129 jint android_tool_type_1, | 147 jint android_tool_type_1, |
| 130 jint android_button_state) | 148 jint android_button_state, |
| 149 jint meta_state) |
| 131 : cached_time_(FromAndroidTime(time_ms)), | 150 : cached_time_(FromAndroidTime(time_ms)), |
| 132 cached_action_(FromAndroidAction(android_action)), | 151 cached_action_(FromAndroidAction(android_action)), |
| 133 cached_pointer_count_(pointer_count), | 152 cached_pointer_count_(pointer_count), |
| 134 cached_history_size_(history_size), | 153 cached_history_size_(history_size), |
| 135 cached_action_index_(action_index), | 154 cached_action_index_(action_index), |
| 136 cached_button_state_(FromAndroidButtonState(android_button_state)), | 155 cached_button_state_(FromAndroidButtonState(android_button_state)), |
| 156 cached_flags_(FromAndroidMetaState(meta_state)), |
| 137 pix_to_dip_(pix_to_dip), | 157 pix_to_dip_(pix_to_dip), |
| 138 should_recycle_(false) { | 158 should_recycle_(false) { |
| 139 DCHECK_GT(pointer_count, 0); | 159 DCHECK_GT(pointer_count, 0); |
| 140 DCHECK_GE(history_size, 0); | 160 DCHECK_GE(history_size, 0); |
| 141 | 161 |
| 142 event_.Reset(env, event); | 162 event_.Reset(env, event); |
| 143 DCHECK(event_.obj()); | 163 DCHECK(event_.obj()); |
| 144 | 164 |
| 145 cached_positions_[0] = ToDips(gfx::PointF(pos_x_0_pixels, pos_y_0_pixels)); | 165 cached_positions_[0] = ToDips(gfx::PointF(pos_x_0_pixels, pos_y_0_pixels)); |
| 146 cached_positions_[1] = ToDips(gfx::PointF(pos_x_1_pixels, pos_y_1_pixels)); | 166 cached_positions_[1] = ToDips(gfx::PointF(pos_x_1_pixels, pos_y_1_pixels)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 163 JNIEnv* env, | 183 JNIEnv* env, |
| 164 jobject event) | 184 jobject event) |
| 165 : cached_time_(FromAndroidTime(Java_MotionEvent_getEventTime(env, event))), | 185 : cached_time_(FromAndroidTime(Java_MotionEvent_getEventTime(env, event))), |
| 166 cached_action_( | 186 cached_action_( |
| 167 FromAndroidAction(Java_MotionEvent_getActionMasked(env, event))), | 187 FromAndroidAction(Java_MotionEvent_getActionMasked(env, event))), |
| 168 cached_pointer_count_(Java_MotionEvent_getPointerCount(env, event)), | 188 cached_pointer_count_(Java_MotionEvent_getPointerCount(env, event)), |
| 169 cached_history_size_(Java_MotionEvent_getHistorySize(env, event)), | 189 cached_history_size_(Java_MotionEvent_getHistorySize(env, event)), |
| 170 cached_action_index_(Java_MotionEvent_getActionIndex(env, event)), | 190 cached_action_index_(Java_MotionEvent_getActionIndex(env, event)), |
| 171 cached_button_state_( | 191 cached_button_state_( |
| 172 FromAndroidButtonState(Java_MotionEvent_getButtonState(env, event))), | 192 FromAndroidButtonState(Java_MotionEvent_getButtonState(env, event))), |
| 193 cached_flags_( |
| 194 FromAndroidMetaState(Java_MotionEvent_getMetaState(env, event))), |
| 173 pix_to_dip_(pix_to_dip), | 195 pix_to_dip_(pix_to_dip), |
| 174 should_recycle_(true) { | 196 should_recycle_(true) { |
| 175 event_.Reset(env, event); | 197 event_.Reset(env, event); |
| 176 DCHECK(event_.obj()); | 198 DCHECK(event_.obj()); |
| 177 | 199 |
| 178 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | 200 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
| 179 if (i < cached_pointer_count_) { | 201 if (i < cached_pointer_count_) { |
| 180 cached_positions_[i] = | 202 cached_positions_[i] = |
| 181 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), | 203 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), |
| 182 Java_MotionEvent_getYF_I(env, event, i))); | 204 Java_MotionEvent_getYF_I(env, event, i))); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 206 | 228 |
| 207 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) | 229 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) |
| 208 : event_(Obtain(other)), | 230 : event_(Obtain(other)), |
| 209 cached_time_(other.cached_time_), | 231 cached_time_(other.cached_time_), |
| 210 cached_action_(other.cached_action_), | 232 cached_action_(other.cached_action_), |
| 211 cached_pointer_count_(other.cached_pointer_count_), | 233 cached_pointer_count_(other.cached_pointer_count_), |
| 212 cached_history_size_(other.cached_history_size_), | 234 cached_history_size_(other.cached_history_size_), |
| 213 cached_action_index_(other.cached_action_index_), | 235 cached_action_index_(other.cached_action_index_), |
| 214 cached_raw_position_offset_(other.cached_raw_position_offset_), | 236 cached_raw_position_offset_(other.cached_raw_position_offset_), |
| 215 cached_button_state_(other.cached_button_state_), | 237 cached_button_state_(other.cached_button_state_), |
| 238 cached_flags_(other.cached_flags_), |
| 216 pix_to_dip_(other.pix_to_dip_), | 239 pix_to_dip_(other.pix_to_dip_), |
| 217 should_recycle_(true) { | 240 should_recycle_(true) { |
| 218 DCHECK(event_.obj()); | 241 DCHECK(event_.obj()); |
| 219 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | 242 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
| 220 cached_positions_[i] = other.cached_positions_[i]; | 243 cached_positions_[i] = other.cached_positions_[i]; |
| 221 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; | 244 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; |
| 222 cached_touch_majors_[i] = other.cached_touch_majors_[i]; | 245 cached_touch_majors_[i] = other.cached_touch_majors_[i]; |
| 223 cached_touch_minors_[i] = other.cached_touch_minors_[i]; | 246 cached_touch_minors_[i] = other.cached_touch_minors_[i]; |
| 224 cached_orientations_[i] = other.cached_orientations_[i]; | 247 cached_orientations_[i] = other.cached_orientations_[i]; |
| 225 cached_tool_types_[i] = other.cached_tool_types_[i]; | 248 cached_tool_types_[i] = other.cached_tool_types_[i]; |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 if (pointer_index < MAX_POINTERS_TO_CACHE) | 371 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 349 return cached_tool_types_[pointer_index]; | 372 return cached_tool_types_[pointer_index]; |
| 350 return FromAndroidToolType(Java_MotionEvent_getToolType( | 373 return FromAndroidToolType(Java_MotionEvent_getToolType( |
| 351 AttachCurrentThread(), event_.obj(), pointer_index)); | 374 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 352 } | 375 } |
| 353 | 376 |
| 354 int MotionEventAndroid::GetButtonState() const { | 377 int MotionEventAndroid::GetButtonState() const { |
| 355 return cached_button_state_; | 378 return cached_button_state_; |
| 356 } | 379 } |
| 357 | 380 |
| 381 int MotionEventAndroid::GetFlags() const { |
| 382 return cached_flags_; |
| 383 } |
| 384 |
| 358 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Clone() const { | 385 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Clone() const { |
| 359 return scoped_ptr<MotionEvent>(new MotionEventAndroid(*this)); | 386 return scoped_ptr<MotionEvent>(new MotionEventAndroid(*this)); |
| 360 } | 387 } |
| 361 | 388 |
| 362 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Cancel() const { | 389 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Cancel() const { |
| 363 // The input coordinates to |MotionEventAndroid| are always in device pixels, | 390 // The input coordinates to |MotionEventAndroid| are always in device pixels, |
| 364 // but the cached coordinates are in DIPs. | 391 // but the cached coordinates are in DIPs. |
| 365 const gfx::PointF position_pixels = | 392 const gfx::PointF position_pixels = |
| 366 gfx::ScalePoint(cached_positions_[0], 1.f / pix_to_dip_); | 393 gfx::ScalePoint(cached_positions_[0], 1.f / pix_to_dip_); |
| 367 return scoped_ptr<MotionEvent>( | 394 return scoped_ptr<MotionEvent>( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), | 436 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), |
| 410 ToAndroidTime(down_time), | 437 ToAndroidTime(down_time), |
| 411 ToAndroidTime(event_time), | 438 ToAndroidTime(event_time), |
| 412 ToAndroidAction(action), | 439 ToAndroidAction(action), |
| 413 x_pixels, | 440 x_pixels, |
| 414 y_pixels, | 441 y_pixels, |
| 415 0); | 442 0); |
| 416 } | 443 } |
| 417 | 444 |
| 418 } // namespace content | 445 } // namespace content |
| OLD | NEW |