| 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> | 7 #include <android/input.h> |
| 8 | 8 |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 | 10 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 } | 118 } |
| 119 | 119 |
| 120 } // namespace | 120 } // namespace |
| 121 | 121 |
| 122 MotionEventAndroid::Pointer::Pointer(jint id, | 122 MotionEventAndroid::Pointer::Pointer(jint id, |
| 123 jfloat pos_x_pixels, | 123 jfloat pos_x_pixels, |
| 124 jfloat pos_y_pixels, | 124 jfloat pos_y_pixels, |
| 125 jfloat touch_major_pixels, | 125 jfloat touch_major_pixels, |
| 126 jfloat touch_minor_pixels, | 126 jfloat touch_minor_pixels, |
| 127 jfloat orientation_rad, | 127 jfloat orientation_rad, |
| 128 jfloat tilt_rad, |
| 128 jint tool_type) | 129 jint tool_type) |
| 129 : id(id), | 130 : id(id), |
| 130 pos_x_pixels(pos_x_pixels), | 131 pos_x_pixels(pos_x_pixels), |
| 131 pos_y_pixels(pos_y_pixels), | 132 pos_y_pixels(pos_y_pixels), |
| 132 touch_major_pixels(touch_major_pixels), | 133 touch_major_pixels(touch_major_pixels), |
| 133 touch_minor_pixels(touch_minor_pixels), | 134 touch_minor_pixels(touch_minor_pixels), |
| 134 orientation_rad(orientation_rad), | 135 orientation_rad(orientation_rad), |
| 136 tilt_rad(tilt_rad), |
| 135 tool_type(tool_type) { | 137 tool_type(tool_type) { |
| 136 } | 138 } |
| 137 | 139 |
| 138 MotionEventAndroid::CachedPointer::CachedPointer() | 140 MotionEventAndroid::CachedPointer::CachedPointer() |
| 139 : id(0), | 141 : id(0), |
| 140 touch_major(0), | 142 touch_major(0), |
| 141 touch_minor(0), | 143 touch_minor(0), |
| 142 orientation(0), | 144 orientation(0), |
| 145 tilt(0), |
| 143 tool_type(TOOL_TYPE_UNKNOWN) { | 146 tool_type(TOOL_TYPE_UNKNOWN) { |
| 144 } | 147 } |
| 145 | 148 |
| 146 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, | 149 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, |
| 147 JNIEnv* env, | 150 JNIEnv* env, |
| 148 jobject event, | 151 jobject event, |
| 149 jlong time_ms, | 152 jlong time_ms, |
| 150 jint android_action, | 153 jint android_action, |
| 151 jint pointer_count, | 154 jint pointer_count, |
| 152 jint history_size, | 155 jint history_size, |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 DCHECK_LT(pointer_index, cached_pointer_count_); | 265 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 263 // Note that this early return is a special case exercised only in testing, as | 266 // Note that this early return is a special case exercised only in testing, as |
| 264 // caching the pressure values is not a worthwhile optimization (they're | 267 // caching the pressure values is not a worthwhile optimization (they're |
| 265 // accessed at most once per event instance). | 268 // accessed at most once per event instance). |
| 266 if (!event_.obj()) | 269 if (!event_.obj()) |
| 267 return 0.f; | 270 return 0.f; |
| 268 return Java_MotionEvent_getPressureF_I( | 271 return Java_MotionEvent_getPressureF_I( |
| 269 AttachCurrentThread(), event_.obj(), pointer_index); | 272 AttachCurrentThread(), event_.obj(), pointer_index); |
| 270 } | 273 } |
| 271 | 274 |
| 275 float MotionEventAndroid::GetTilt(size_t pointer_index) const { |
| 276 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 277 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 278 return cached_pointers_[pointer_index].tilt; |
| 279 if (!event_.obj()) |
| 280 return 0.f; |
| 281 return ToValidFloat(Java_MotionEvent_getAxisValueF_I_I( |
| 282 AttachCurrentThread(), event_.obj(), AXIS_TILT, pointer_index)); |
| 283 } |
| 284 |
| 285 // TODO(e_hakkinen): Remove either this or GetTiltX and GetTiltY. |
| 286 float MotionEventAndroid::GetTiltOrientation(size_t pointer_index) const { |
| 287 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 288 if (GetToolType(pointer_index) != MotionEvent::TOOL_TYPE_STYLUS) |
| 289 return 0.f; |
| 290 float orientation; |
| 291 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 292 orientation = cached_pointers_[pointer_index].orientation; |
| 293 else if (!event_.obj()) |
| 294 return 0.f; |
| 295 else |
| 296 orientation = ToValidFloat(Java_MotionEvent_getOrientationF_I( |
| 297 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 298 return orientation < 0 ? orientation + M_PI : orientation - M_PI; |
| 299 } |
| 300 |
| 301 // TODO(e_hakkinen): Remove either this and GetTiltY or GetTiltOrientation. |
| 302 float MotionEventAndroid::GetTiltX(size_t pointer_index) const { |
| 303 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 304 if (GetToolType(pointer_index) != MotionEvent::TOOL_TYPE_STYLUS) |
| 305 return 0.f; |
| 306 float orientation = GetOrientation(pointer_index); |
| 307 float tilt = GetTilt(pointer_index); |
| 308 // A stylus points to a direction specified by orientation and tilts to |
| 309 // the opposite direction. Coordinate system is left-handed. |
| 310 return atan2(sin(-orientation) * sin(tilt), cos(tilt)); |
| 311 } |
| 312 |
| 313 // TODO(e_hakkinen): Remove either this and GetTiltX or GetTiltOrientation. |
| 314 float MotionEventAndroid::GetTiltY(size_t pointer_index) const { |
| 315 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 316 if (GetToolType(pointer_index) != MotionEvent::TOOL_TYPE_STYLUS) |
| 317 return 0.f; |
| 318 float orientation = GetOrientation(pointer_index); |
| 319 float tilt = GetTilt(pointer_index); |
| 320 // A stylus points to a direction specified by orientation and tilts to |
| 321 // the opposite direction. Coordinate system is left-handed. |
| 322 return atan2(cos(-orientation) * sin(tilt), cos(tilt)); |
| 323 } |
| 324 |
| 272 base::TimeTicks MotionEventAndroid::GetEventTime() const { | 325 base::TimeTicks MotionEventAndroid::GetEventTime() const { |
| 273 return cached_time_; | 326 return cached_time_; |
| 274 } | 327 } |
| 275 | 328 |
| 276 size_t MotionEventAndroid::GetHistorySize() const { | 329 size_t MotionEventAndroid::GetHistorySize() const { |
| 277 return cached_history_size_; | 330 return cached_history_size_; |
| 278 } | 331 } |
| 279 | 332 |
| 280 base::TimeTicks MotionEventAndroid::GetHistoricalEventTime( | 333 base::TimeTicks MotionEventAndroid::GetHistoricalEventTime( |
| 281 size_t historical_index) const { | 334 size_t historical_index) const { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 | 378 |
| 326 MotionEventAndroid::CachedPointer MotionEventAndroid::FromAndroidPointer( | 379 MotionEventAndroid::CachedPointer MotionEventAndroid::FromAndroidPointer( |
| 327 const Pointer& pointer) const { | 380 const Pointer& pointer) const { |
| 328 CachedPointer result; | 381 CachedPointer result; |
| 329 result.id = pointer.id; | 382 result.id = pointer.id; |
| 330 result.position = | 383 result.position = |
| 331 gfx::PointF(ToDips(pointer.pos_x_pixels), ToDips(pointer.pos_y_pixels)); | 384 gfx::PointF(ToDips(pointer.pos_x_pixels), ToDips(pointer.pos_y_pixels)); |
| 332 result.touch_major = ToDips(pointer.touch_major_pixels); | 385 result.touch_major = ToDips(pointer.touch_major_pixels); |
| 333 result.touch_minor = ToDips(pointer.touch_minor_pixels); | 386 result.touch_minor = ToDips(pointer.touch_minor_pixels); |
| 334 result.orientation = ToValidFloat(pointer.orientation_rad); | 387 result.orientation = ToValidFloat(pointer.orientation_rad); |
| 388 result.tilt = ToValidFloat(pointer.tilt_rad); |
| 335 result.tool_type = FromAndroidToolType(pointer.tool_type); | 389 result.tool_type = FromAndroidToolType(pointer.tool_type); |
| 336 return result; | 390 return result; |
| 337 } | 391 } |
| 338 | 392 |
| 339 // static | 393 // static |
| 340 bool MotionEventAndroid::RegisterMotionEventAndroid(JNIEnv* env) { | 394 bool MotionEventAndroid::RegisterMotionEventAndroid(JNIEnv* env) { |
| 341 return JNI_MotionEvent::RegisterNativesImpl(env); | 395 return JNI_MotionEvent::RegisterNativesImpl(env); |
| 342 } | 396 } |
| 343 | 397 |
| 344 } // namespace content | 398 } // namespace content |
| OLD | NEW |