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