VelocityControl.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2012 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef _LIBINPUT_VELOCITY_CONTROL_H
  17. #define _LIBINPUT_VELOCITY_CONTROL_H
  18. #include <input/Input.h>
  19. #include <input/VelocityTracker.h>
  20. #include <utils/Timers.h>
  21. namespace android {
  22. /*
  23. * Specifies parameters that govern pointer or wheel acceleration.
  24. */
  25. struct VelocityControlParameters {
  26. // A scale factor that is multiplied with the raw velocity deltas
  27. // prior to applying any other velocity control factors. The scale
  28. // factor should be used to adapt the input device resolution
  29. // (eg. counts per inch) to the output device resolution (eg. pixels per inch).
  30. //
  31. // Must be a positive value.
  32. // Default is 1.0 (no scaling).
  33. float scale;
  34. // The scaled speed at which acceleration begins to be applied.
  35. // This value establishes the upper bound of a low speed regime for
  36. // small precise motions that are performed without any acceleration.
  37. //
  38. // Must be a non-negative value.
  39. // Default is 0.0 (no low threshold).
  40. float lowThreshold;
  41. // The scaled speed at which maximum acceleration is applied.
  42. // The difference between highThreshold and lowThreshold controls
  43. // the range of speeds over which the acceleration factor is interpolated.
  44. // The wider the range, the smoother the acceleration.
  45. //
  46. // Must be a non-negative value greater than or equal to lowThreshold.
  47. // Default is 0.0 (no high threshold).
  48. float highThreshold;
  49. // The acceleration factor.
  50. // When the speed is above the low speed threshold, the velocity will scaled
  51. // by an interpolated value between 1.0 and this amount.
  52. //
  53. // Must be a positive greater than or equal to 1.0.
  54. // Default is 1.0 (no acceleration).
  55. float acceleration;
  56. VelocityControlParameters() :
  57. scale(1.0f), lowThreshold(0.0f), highThreshold(0.0f), acceleration(1.0f) {
  58. }
  59. VelocityControlParameters(float scale, float lowThreshold,
  60. float highThreshold, float acceleration) :
  61. scale(scale), lowThreshold(lowThreshold),
  62. highThreshold(highThreshold), acceleration(acceleration) {
  63. }
  64. };
  65. /*
  66. * Implements mouse pointer and wheel speed control and acceleration.
  67. */
  68. class VelocityControl {
  69. public:
  70. VelocityControl();
  71. /* Sets the various parameters. */
  72. void setParameters(const VelocityControlParameters& parameters);
  73. /* Resets the current movement counters to zero.
  74. * This has the effect of nullifying any acceleration. */
  75. void reset();
  76. /* Translates a raw movement delta into an appropriately
  77. * scaled / accelerated delta based on the current velocity. */
  78. void move(nsecs_t eventTime, float* deltaX, float* deltaY);
  79. private:
  80. // If no movements are received within this amount of time,
  81. // we assume the movement has stopped and reset the movement counters.
  82. static const nsecs_t STOP_TIME = 500 * 1000000; // 500 ms
  83. VelocityControlParameters mParameters;
  84. nsecs_t mLastMovementTime;
  85. VelocityTracker::Position mRawPosition;
  86. VelocityTracker mVelocityTracker;
  87. };
  88. } // namespace android
  89. #endif // _LIBINPUT_VELOCITY_CONTROL_H