1 /*
2 * Copyright (C) 2019 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
17 #include "../Macros.h"
18
19 #include "VibratorInputMapper.h"
20
21 namespace android {
22
VibratorInputMapper(InputDeviceContext & deviceContext)23 VibratorInputMapper::VibratorInputMapper(InputDeviceContext& deviceContext)
24 : InputMapper(deviceContext), mVibrating(false), mSequence(0) {}
25
~VibratorInputMapper()26 VibratorInputMapper::~VibratorInputMapper() {}
27
getSources()28 uint32_t VibratorInputMapper::getSources() {
29 return 0;
30 }
31
populateDeviceInfo(InputDeviceInfo * info)32 void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
33 InputMapper::populateDeviceInfo(info);
34
35 info->setVibrator(true);
36 }
37
process(const RawEvent * rawEvent)38 void VibratorInputMapper::process(const RawEvent* rawEvent) {
39 // TODO: Handle FF_STATUS, although it does not seem to be widely supported.
40 }
41
vibrate(const VibrationSequence & sequence,ssize_t repeat,int32_t token)42 void VibratorInputMapper::vibrate(const VibrationSequence& sequence, ssize_t repeat,
43 int32_t token) {
44 #if DEBUG_VIBRATOR
45 ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%zd, token=%d", getDeviceId(),
46 sequence.toString().c_str(), repeat, token);
47 #endif
48
49 mVibrating = true;
50 mSequence = sequence;
51 mRepeat = repeat;
52 mToken = token;
53 mIndex = -1;
54
55 // Request InputReader to notify InputManagerService for vibration started.
56 NotifyVibratorStateArgs args(getContext()->getNextId(), systemTime(), getDeviceId(), true);
57 getListener()->notifyVibratorState(&args);
58 nextStep();
59 }
60
cancelVibrate(int32_t token)61 void VibratorInputMapper::cancelVibrate(int32_t token) {
62 #if DEBUG_VIBRATOR
63 ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token);
64 #endif
65
66 if (mVibrating && mToken == token) {
67 stopVibrating();
68 }
69 }
70
isVibrating()71 bool VibratorInputMapper::isVibrating() {
72 return mVibrating;
73 }
74
getVibratorIds()75 std::vector<int32_t> VibratorInputMapper::getVibratorIds() {
76 return getDeviceContext().getVibratorIds();
77 }
78
timeoutExpired(nsecs_t when)79 void VibratorInputMapper::timeoutExpired(nsecs_t when) {
80 if (mVibrating) {
81 if (when >= mNextStepTime) {
82 nextStep();
83 } else {
84 getContext()->requestTimeoutAtTime(mNextStepTime);
85 }
86 }
87 }
88
nextStep()89 void VibratorInputMapper::nextStep() {
90 #if DEBUG_VIBRATOR
91 ALOGD("nextStep: index=%d, vibrate deviceId=%d", (int)mIndex, getDeviceId());
92 #endif
93 mIndex += 1;
94 if (size_t(mIndex) >= mSequence.pattern.size()) {
95 if (mRepeat < 0) {
96 // We are done.
97 stopVibrating();
98 return;
99 }
100 mIndex = mRepeat;
101 }
102
103 const VibrationElement& element = mSequence.pattern[mIndex];
104 if (element.isOn()) {
105 #if DEBUG_VIBRATOR
106 std::string description = element.toString();
107 ALOGD("nextStep: sending vibrate deviceId=%d, element=%s", getDeviceId(),
108 description.c_str());
109 #endif
110 getDeviceContext().vibrate(element);
111 } else {
112 #if DEBUG_VIBRATOR
113 ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId());
114 #endif
115 getDeviceContext().cancelVibrate();
116 }
117 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
118 std::chrono::nanoseconds duration =
119 std::chrono::duration_cast<std::chrono::nanoseconds>(element.duration);
120 mNextStepTime = now + duration.count();
121 getContext()->requestTimeoutAtTime(mNextStepTime);
122 #if DEBUG_VIBRATOR
123 ALOGD("nextStep: scheduled timeout in %lldms", element.duration.count());
124 #endif
125 }
126
stopVibrating()127 void VibratorInputMapper::stopVibrating() {
128 mVibrating = false;
129 #if DEBUG_VIBRATOR
130 ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId());
131 #endif
132 getDeviceContext().cancelVibrate();
133
134 // Request InputReader to notify InputManagerService for vibration complete.
135 NotifyVibratorStateArgs args(getContext()->getNextId(), systemTime(), getDeviceId(), false);
136 getListener()->notifyVibratorState(&args);
137 }
138
dump(std::string & dump)139 void VibratorInputMapper::dump(std::string& dump) {
140 dump += INDENT2 "Vibrator Input Mapper:\n";
141 dump += StringPrintf(INDENT3 "Vibrating: %s\n", toString(mVibrating));
142 if (mVibrating) {
143 dump += INDENT3 "Pattern: ";
144 dump += mSequence.toString();
145 dump += "\n";
146 dump += StringPrintf(INDENT3 "Repeat Index: %zd\n", mRepeat);
147 }
148 }
149
150 } // namespace android
151