Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | #ifndef _UI_INPUTREADER_LIGHT_INPUT_MAPPER_H |
| 18 | #define _UI_INPUTREADER_LIGHT_INPUT_MAPPER_H |
| 19 | |
| 20 | #include "InputMapper.h" |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | class LightInputMapper : public InputMapper { |
| 25 | // Refer to https://developer.android.com/reference/kotlin/android/graphics/Color |
| 26 | /* Number of colors : {red, green, blue} */ |
| 27 | static constexpr size_t COLOR_NUM = 3; |
| 28 | static constexpr int32_t MAX_BRIGHTNESS = 0xff; |
| 29 | |
| 30 | public: |
| 31 | explicit LightInputMapper(InputDeviceContext& deviceContext); |
| 32 | ~LightInputMapper() override; |
| 33 | |
| 34 | uint32_t getSources() override; |
| 35 | void populateDeviceInfo(InputDeviceInfo* deviceInfo) override; |
| 36 | void dump(std::string& dump) override; |
| 37 | void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) override; |
| 38 | void reset(nsecs_t when) override; |
| 39 | void process(const RawEvent* rawEvent) override; |
| 40 | bool setLightColor(int32_t lightId, int32_t color) override; |
| 41 | bool setLightPlayerId(int32_t lightId, int32_t playerId) override; |
| 42 | std::optional<int32_t> getLightColor(int32_t lightId) override; |
| 43 | std::optional<int32_t> getLightPlayerId(int32_t lightId) override; |
| 44 | |
| 45 | private: |
| 46 | struct Light { |
| 47 | explicit Light(InputDeviceContext& context, std::string name, int32_t id, |
| 48 | InputDeviceLightType type) |
| 49 | : context(context), name(name), id(id), type(type) {} |
| 50 | virtual ~Light() {} |
| 51 | InputDeviceContext& context; |
| 52 | std::string name; |
| 53 | int32_t id; |
| 54 | InputDeviceLightType type; |
| 55 | |
| 56 | virtual bool setLightColor(int32_t color) { return false; } |
| 57 | virtual std::optional<int32_t> getLightColor() { return std::nullopt; } |
| 58 | virtual bool setLightPlayerId(int32_t playerId) { return false; } |
| 59 | virtual std::optional<int32_t> getLightPlayerId() { return std::nullopt; } |
| 60 | |
| 61 | virtual void dump(std::string& dump) {} |
| 62 | |
| 63 | std::optional<std::int32_t> getRawLightBrightness(int32_t rawLightId); |
| 64 | void setRawLightBrightness(int32_t rawLightId, int32_t brightness); |
| 65 | }; |
| 66 | |
| 67 | struct SingleLight : public Light { |
| 68 | explicit SingleLight(InputDeviceContext& context, std::string name, int32_t id, |
| 69 | int32_t rawId) |
| 70 | : Light(context, name, id, InputDeviceLightType::SINGLE), rawId(rawId) {} |
| 71 | int32_t rawId; |
| 72 | |
| 73 | bool setLightColor(int32_t color) override; |
| 74 | std::optional<int32_t> getLightColor() override; |
| 75 | void dump(std::string& dump) override; |
| 76 | }; |
| 77 | |
| 78 | struct RgbLight : public Light { |
| 79 | explicit RgbLight(InputDeviceContext& context, int32_t id, |
| 80 | std::unordered_map<LightColor, int32_t> rawRgbIds, |
| 81 | std::optional<int32_t> rawGlobalId) |
| 82 | : Light(context, "RGB", id, InputDeviceLightType::RGB), |
| 83 | rawRgbIds(rawRgbIds), |
| 84 | rawGlobalId(rawGlobalId) { |
| 85 | brightness = rawGlobalId.has_value() |
| 86 | ? getRawLightBrightness(rawGlobalId.value()).value_or(MAX_BRIGHTNESS) |
| 87 | : MAX_BRIGHTNESS; |
| 88 | } |
| 89 | // Map from color to raw light id. |
| 90 | std::unordered_map<LightColor, int32_t /* rawLightId */> rawRgbIds; |
| 91 | // Optional global control raw light id. |
| 92 | std::optional<int32_t> rawGlobalId; |
| 93 | int32_t brightness; |
| 94 | |
| 95 | bool setLightColor(int32_t color) override; |
| 96 | std::optional<int32_t> getLightColor() override; |
| 97 | void dump(std::string& dump) override; |
| 98 | }; |
| 99 | |
| 100 | struct MultiColorLight : public Light { |
| 101 | explicit MultiColorLight(InputDeviceContext& context, std::string name, int32_t id, |
| 102 | int32_t rawId) |
| 103 | : Light(context, name, id, InputDeviceLightType::MULTI_COLOR), rawId(rawId) {} |
| 104 | int32_t rawId; |
| 105 | |
| 106 | bool setLightColor(int32_t color) override; |
| 107 | std::optional<int32_t> getLightColor() override; |
| 108 | void dump(std::string& dump) override; |
| 109 | }; |
| 110 | |
| 111 | struct PlayerIdLight : public Light { |
| 112 | explicit PlayerIdLight(InputDeviceContext& context, std::string name, int32_t id, |
| 113 | std::unordered_map<int32_t, int32_t> rawLightIds) |
| 114 | : Light(context, name, id, InputDeviceLightType::PLAYER_ID), |
| 115 | rawLightIds(rawLightIds) {} |
| 116 | // Map from player Id to raw light Id |
| 117 | std::unordered_map<int32_t, int32_t> rawLightIds; |
| 118 | |
| 119 | bool setLightPlayerId(int32_t palyerId) override; |
| 120 | std::optional<int32_t> getLightPlayerId() override; |
| 121 | void dump(std::string& dump) override; |
| 122 | }; |
| 123 | |
| 124 | int32_t mNextId = 0; |
| 125 | |
| 126 | // Light color map from light color to the color index. |
| 127 | static const std::unordered_map<std::string, size_t> LIGHT_COLORS; |
| 128 | |
| 129 | // Light map from light ID to Light |
| 130 | std::unordered_map<int32_t, std::unique_ptr<Light>> mLights; |
| 131 | }; |
| 132 | |
| 133 | } // namespace android |
| 134 | |
| 135 | #endif // _UI_INPUTREADER_LIGHT_INPUT_MAPPER_H |