summaryrefslogtreecommitdiff
path: root/services/inputflinger/reader/controller/PeripheralController.cpp
blob: a6934960c9664f33879cc7d2ee45faf8dc296706 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <locale>
#include <regex>

#include <ftl/enum.h>

#include "../Macros.h"
#include "PeripheralController.h"

// Log detailed debug messages about input device lights.
static constexpr bool DEBUG_LIGHT_DETAILS = false;

namespace android {

static inline int32_t getAlpha(int32_t color) {
    return (color >> 24) & 0xff;
}

static inline int32_t getRed(int32_t color) {
    return (color >> 16) & 0xff;
}

static inline int32_t getGreen(int32_t color) {
    return (color >> 8) & 0xff;
}

static inline int32_t getBlue(int32_t color) {
    return color & 0xff;
}

static inline int32_t toArgb(int32_t brightness, int32_t red, int32_t green, int32_t blue) {
    return (brightness & 0xff) << 24 | (red & 0xff) << 16 | (green & 0xff) << 8 | (blue & 0xff);
}

/**
 * Input controller owned by InputReader device, implements the native API for querying input
 * lights, getting and setting the lights brightness and color, by interacting with EventHub
 * devices.
 */
PeripheralController::PeripheralController(InputDeviceContext& deviceContext)
      : mDeviceContext(deviceContext) {
    configureBattries();
    configureLights();
}

PeripheralController::~PeripheralController() {}

std::optional<std::int32_t> PeripheralController::Light::getRawLightBrightness(int32_t rawLightId) {
    std::optional<RawLightInfo> rawInfoOpt = context.getRawLightInfo(rawLightId);
    if (!rawInfoOpt.has_value()) {
        return std::nullopt;
    }
    std::optional<int32_t> brightnessOpt = context.getLightBrightness(rawLightId);
    if (!brightnessOpt.has_value()) {
        return std::nullopt;
    }
    int brightness = brightnessOpt.value();

    // If the light node doesn't have max brightness, use the default max brightness.
    int rawMaxBrightness = rawInfoOpt->maxBrightness.value_or(MAX_BRIGHTNESS);
    float ratio = MAX_BRIGHTNESS / rawMaxBrightness;
    // Scale the returned brightness in [0, rawMaxBrightness] to [0, 255]
    if (rawMaxBrightness != MAX_BRIGHTNESS) {
        brightness = brightness * ratio;
    }
    if (DEBUG_LIGHT_DETAILS) {
        ALOGD("getRawLightBrightness rawLightId %d brightness 0x%x ratio %.2f", rawLightId,
              brightness, ratio);
    }
    return brightness;
}

void PeripheralController::Light::setRawLightBrightness(int32_t rawLightId, int32_t brightness) {
    std::optional<RawLightInfo> rawInfo = context.getRawLightInfo(rawLightId);
    if (!rawInfo.has_value()) {
        return;
    }
    // If the light node doesn't have max brightness, use the default max brightness.
    int rawMaxBrightness = rawInfo->maxBrightness.value_or(MAX_BRIGHTNESS);
    float ratio = MAX_BRIGHTNESS / rawMaxBrightness;
    // Scale the requested brightness in [0, 255] to [0, rawMaxBrightness]
    if (rawMaxBrightness != MAX_BRIGHTNESS) {
        brightness = ceil(brightness / ratio);
    }
    if (DEBUG_LIGHT_DETAILS) {
        ALOGD("setRawLightBrightness rawLightId %d brightness 0x%x ratio %.2f", rawLightId,
              brightness, ratio);
    }
    context.setLightBrightness(rawLightId, brightness);
}

bool PeripheralController::MonoLight::setLightColor(int32_t color) {
    int32_t brightness = getAlpha(color);
    setRawLightBrightness(rawId, brightness);

    return true;
}

bool PeripheralController::RgbLight::setLightColor(int32_t color) {
    // Compose color value as per:
    // https://developer.android.com/reference/android/graphics/Color?hl=en
    // int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
    // The alpha component is used to scale the R,G,B leds brightness, with the ratio to
    // MAX_BRIGHTNESS.
    brightness = getAlpha(color);
    int32_t red = 0;
    int32_t green = 0;
    int32_t blue = 0;
    if (brightness > 0) {
        float ratio = MAX_BRIGHTNESS / brightness;
        red = ceil(getRed(color) / ratio);
        green = ceil(getGreen(color) / ratio);
        blue = ceil(getBlue(color) / ratio);
    }
    setRawLightBrightness(rawRgbIds.at(LightColor::RED), red);
    setRawLightBrightness(rawRgbIds.at(LightColor::GREEN), green);
    setRawLightBrightness(rawRgbIds.at(LightColor::BLUE), blue);
    if (rawGlobalId.has_value()) {
        setRawLightBrightness(rawGlobalId.value(), brightness);
    }

    return true;
}

bool PeripheralController::MultiColorLight::setLightColor(int32_t color) {
    std::unordered_map<LightColor, int32_t> intensities;
    intensities.emplace(LightColor::RED, getRed(color));
    intensities.emplace(LightColor::GREEN, getGreen(color));
    intensities.emplace(LightColor::BLUE, getBlue(color));

    context.setLightIntensities(rawId, intensities);
    setRawLightBrightness(rawId, getAlpha(color));
    return true;
}

std::optional<int32_t> PeripheralController::MonoLight::getLightColor() {
    std::optional<int32_t> brightness = getRawLightBrightness(rawId);
    if (!brightness.has_value()) {
        return std::nullopt;
    }

    return toArgb(brightness.value(), 0 /* red */, 0 /* green */, 0 /* blue */);
}

std::optional<int32_t> PeripheralController::RgbLight::getLightColor() {
    // If the Alpha component is zero, then return color 0.
    if (brightness == 0) {
        return 0;
    }
    // Compose color value as per:
    // https://developer.android.com/reference/android/graphics/Color?hl=en
    // int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
    std::optional<int32_t> redOr = getRawLightBrightness(rawRgbIds.at(LightColor::RED));
    std::optional<int32_t> greenOr = getRawLightBrightness(rawRgbIds.at(LightColor::GREEN));
    std::optional<int32_t> blueOr = getRawLightBrightness(rawRgbIds.at(LightColor::BLUE));
    // If we can't get brightness for any of the RGB light
    if (!redOr.has_value() || !greenOr.has_value() || !blueOr.has_value()) {
        return std::nullopt;
    }

    // Compose the ARGB format color. As the R,G,B color led brightness is scaled by Alpha
    // value, scale it back to return the nominal color value.
    float ratio = MAX_BRIGHTNESS / brightness;
    int32_t red = round(redOr.value() * ratio);
    int32_t green = round(greenOr.value() * ratio);
    int32_t blue = round(blueOr.value() * ratio);

    if (red > MAX_BRIGHTNESS || green > MAX_BRIGHTNESS || blue > MAX_BRIGHTNESS) {
        // Previously stored brightness isn't valid for current LED values, so just reset to max
        // brightness since an app couldn't have provided these values in the first place.
        red = redOr.value();
        green = greenOr.value();
        blue = blueOr.value();
        brightness = MAX_BRIGHTNESS;
    }

    return toArgb(brightness, red, green, blue);
}

std::optional<int32_t> PeripheralController::MultiColorLight::getLightColor() {
    auto ret = context.getLightIntensities(rawId);
    if (!ret.has_value()) {
        return std::nullopt;
    }
    std::unordered_map<LightColor, int32_t> intensities = ret.value();
    // Get red, green, blue colors
    int32_t color = toArgb(0 /* brightness */, intensities.at(LightColor::RED) /* red */,
                           intensities.at(LightColor::GREEN) /* green */,
                           intensities.at(LightColor::BLUE) /* blue */);
    // Get brightness
    std::optional<int32_t> brightness = getRawLightBrightness(rawId);
    if (brightness.has_value()) {
        return toArgb(brightness.value() /* A */, 0, 0, 0) | color;
    }
    return std::nullopt;
}

bool PeripheralController::PlayerIdLight::setLightPlayerId(int32_t playerId) {
    if (rawLightIds.find(playerId) == rawLightIds.end()) {
        return false;
    }
    for (const auto& [id, rawId] : rawLightIds) {
        if (playerId == id) {
            setRawLightBrightness(rawId, MAX_BRIGHTNESS);
        } else {
            setRawLightBrightness(rawId, 0);
        }
    }
    return true;
}

std::optional<int32_t> PeripheralController::PlayerIdLight::getLightPlayerId() {
    for (const auto& [id, rawId] : rawLightIds) {
        std::optional<int32_t> brightness = getRawLightBrightness(rawId);
        if (brightness.has_value() && brightness.value() > 0) {
            return id;
        }
    }
    return std::nullopt;
}

void PeripheralController::MonoLight::dump(std::string& dump) {
    dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0));
}

void PeripheralController::PlayerIdLight::dump(std::string& dump) {
    dump += StringPrintf(INDENT4 "PlayerId: %d\n", getLightPlayerId().value_or(-1));
    dump += StringPrintf(INDENT4 "Raw Player ID LEDs:");
    for (const auto& [id, rawId] : rawLightIds) {
        dump += StringPrintf("id %d -> %d ", id, rawId);
    }
    dump += "\n";
}

void PeripheralController::RgbLight::dump(std::string& dump) {
    dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0));
    dump += StringPrintf(INDENT4 "Raw RGB LEDs: [%d, %d, %d] ", rawRgbIds.at(LightColor::RED),
                         rawRgbIds.at(LightColor::GREEN), rawRgbIds.at(LightColor::BLUE));
    if (rawGlobalId.has_value()) {
        dump += StringPrintf(INDENT4 "Raw Global LED: [%d] ", rawGlobalId.value());
    }
    dump += "\n";
}

void PeripheralController::MultiColorLight::dump(std::string& dump) {
    dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0));
}

void PeripheralController::populateDeviceInfo(InputDeviceInfo* deviceInfo) {
    // TODO: b/180733860 Remove this after enabling multi-battery
    if (!mBatteries.empty()) {
        deviceInfo->setHasBattery(true);
    }

    for (const auto& [batteryId, battery] : mBatteries) {
        InputDeviceBatteryInfo batteryInfo(battery->name, battery->id);
        deviceInfo->addBatteryInfo(batteryInfo);
    }

    for (const auto& [lightId, light] : mLights) {
        // Input device light doesn't support ordinal, always pass 1.
        InputDeviceLightInfo lightInfo(light->name, light->id, light->type, 1 /* ordinal */);
        deviceInfo->addLightInfo(lightInfo);
    }
}

void PeripheralController::dump(std::string& dump) {
    dump += INDENT2 "Input Controller:\n";
    if (!mLights.empty()) {
        dump += INDENT3 "Lights:\n";
        for (const auto& [lightId, light] : mLights) {
            dump += StringPrintf(INDENT4 "Id: %d", lightId);
            dump += StringPrintf(INDENT4 "Name: %s", light->name.c_str());
            dump += StringPrintf(INDENT4 "Type: %s", ftl::enum_string(light->type).c_str());
            light->dump(dump);
        }
    }
    // Dump raw lights
    dump += INDENT3 "RawLights:\n";
    dump += INDENT4 "Id:\t Name:\t Flags:\t Max brightness:\t Brightness\n";
    const std::vector<int32_t> rawLightIds = getDeviceContext().getRawLightIds();
    // Map from raw light id to raw light info
    std::unordered_map<int32_t, RawLightInfo> rawInfos;
    for (const auto& rawId : rawLightIds) {
        std::optional<RawLightInfo> rawInfo = getDeviceContext().getRawLightInfo(rawId);
        if (!rawInfo.has_value()) {
            continue;
        }
        dump += StringPrintf(INDENT4 "%d", rawId);
        dump += StringPrintf(INDENT4 "%s", rawInfo->name.c_str());
        dump += StringPrintf(INDENT4 "%s", rawInfo->flags.string().c_str());
        dump += StringPrintf(INDENT4 "%d", rawInfo->maxBrightness.value_or(MAX_BRIGHTNESS));
        dump += StringPrintf(INDENT4 "%d\n",
                             getDeviceContext().getLightBrightness(rawId).value_or(-1));
    }

    if (!mBatteries.empty()) {
        dump += INDENT3 "Batteries:\n";
        for (const auto& [batteryId, battery] : mBatteries) {
            dump += StringPrintf(INDENT4 "Id: %d", batteryId);
            dump += StringPrintf(INDENT4 "Name: %s", battery->name.c_str());
            dump += getBatteryCapacity(batteryId).has_value()
                    ? StringPrintf(INDENT3 "Capacity: %d\n", getBatteryCapacity(batteryId).value())
                    : StringPrintf(INDENT3 "Capacity: Unknown");

            std::string status;
            switch (getBatteryStatus(batteryId).value_or(BATTERY_STATUS_UNKNOWN)) {
                case BATTERY_STATUS_CHARGING:
                    status = "Charging";
                    break;
                case BATTERY_STATUS_DISCHARGING:
                    status = "Discharging";
                    break;
                case BATTERY_STATUS_NOT_CHARGING:
                    status = "Not charging";
                    break;
                case BATTERY_STATUS_FULL:
                    status = "Full";
                    break;
                default:
                    status = "Unknown";
            }
            dump += StringPrintf(INDENT3 "Status: %s\n", status.c_str());
        }
    }
}

void PeripheralController::configureBattries() {
    // Check raw batteries
    const std::vector<int32_t> rawBatteryIds = getDeviceContext().getRawBatteryIds();

    for (const auto& rawId : rawBatteryIds) {
        std::optional<RawBatteryInfo> rawInfo = getDeviceContext().getRawBatteryInfo(rawId);
        if (!rawInfo.has_value()) {
            continue;
        }
        std::unique_ptr<Battery> battery =
                std::make_unique<Battery>(getDeviceContext(), rawInfo->name, rawInfo->id);
        mBatteries.insert_or_assign(rawId, std::move(battery));
    }
}

void PeripheralController::configureLights() {
    bool hasRedLed = false;
    bool hasGreenLed = false;
    bool hasBlueLed = false;
    std::optional<int32_t> rawGlobalId = std::nullopt;
    // Player ID light common name string
    std::string playerIdName;
    // Raw RGB color to raw light ID
    std::unordered_map<LightColor, int32_t /* rawLightId */> rawRgbIds;
    // Map from player Id to raw light Id
    std::unordered_map<int32_t, int32_t> playerIdLightIds;

    // Check raw lights
    const std::vector<int32_t> rawLightIds = getDeviceContext().getRawLightIds();
    // Map from raw light id to raw light info
    std::unordered_map<int32_t, RawLightInfo> rawInfos;
    for (const auto& rawId : rawLightIds) {
        std::optional<RawLightInfo> rawInfo = getDeviceContext().getRawLightInfo(rawId);
        if (!rawInfo.has_value()) {
            continue;
        }
        rawInfos.insert_or_assign(rawId, rawInfo.value());
        // Check if this is a group LEDs for player ID
        std::regex lightPattern("([a-z]+)([0-9]+)");
        std::smatch results;
        if (std::regex_match(rawInfo->name, results, lightPattern)) {
            std::string commonName = results[1].str();
            int32_t playerId = std::stoi(results[2]);
            if (playerIdLightIds.empty()) {
                playerIdName = commonName;
                playerIdLightIds.insert_or_assign(playerId, rawId);
            } else {
                // Make sure the player ID leds have common string name
                if (playerIdName.compare(commonName) == 0 &&
                    playerIdLightIds.find(playerId) == playerIdLightIds.end()) {
                    playerIdLightIds.insert_or_assign(playerId, rawId);
                }
            }
        }
        // Check if this is an LED of RGB light
        if (rawInfo->flags.test(InputLightClass::RED)) {
            hasRedLed = true;
            rawRgbIds.emplace(LightColor::RED, rawId);
        }
        if (rawInfo->flags.test(InputLightClass::GREEN)) {
            hasGreenLed = true;
            rawRgbIds.emplace(LightColor::GREEN, rawId);
        }
        if (rawInfo->flags.test(InputLightClass::BLUE)) {
            hasBlueLed = true;
            rawRgbIds.emplace(LightColor::BLUE, rawId);
        }
        if (rawInfo->flags.test(InputLightClass::GLOBAL)) {
            rawGlobalId = rawId;
        }
        if (DEBUG_LIGHT_DETAILS) {
            ALOGD("Light rawId %d name %s max %d flags %s \n", rawInfo->id, rawInfo->name.c_str(),
                  rawInfo->maxBrightness.value_or(MAX_BRIGHTNESS), rawInfo->flags.string().c_str());
        }
    }

    // Construct a player ID light
    if (playerIdLightIds.size() > 1) {
        std::unique_ptr<Light> light =
                std::make_unique<PlayerIdLight>(getDeviceContext(), playerIdName, ++mNextId,
                                                playerIdLightIds);
        mLights.insert_or_assign(light->id, std::move(light));
        // Remove these raw lights from raw light info as they've been used to compose a
        // Player ID light, so we do not expose these raw lights as mono lights.
        for (const auto& [playerId, rawId] : playerIdLightIds) {
            rawInfos.erase(rawId);
        }
    }
    // Construct a RGB light for composed RGB light
    if (hasRedLed && hasGreenLed && hasBlueLed) {
        if (DEBUG_LIGHT_DETAILS) {
            ALOGD("Rgb light ids [%d, %d, %d] \n", rawRgbIds.at(LightColor::RED),
                  rawRgbIds.at(LightColor::GREEN), rawRgbIds.at(LightColor::BLUE));
        }
        std::unique_ptr<Light> light =
                std::make_unique<RgbLight>(getDeviceContext(), ++mNextId, rawRgbIds, rawGlobalId);
        mLights.insert_or_assign(light->id, std::move(light));
        // Remove from raw light info as they've been composed a RBG light.
        rawInfos.erase(rawRgbIds.at(LightColor::RED));
        rawInfos.erase(rawRgbIds.at(LightColor::GREEN));
        rawInfos.erase(rawRgbIds.at(LightColor::BLUE));
        if (rawGlobalId.has_value()) {
            rawInfos.erase(rawGlobalId.value());
        }
    }

    // Check the rest of raw light infos
    for (const auto& [rawId, rawInfo] : rawInfos) {
        // If the node is multi-color led, construct a MULTI_COLOR light
        if (rawInfo.flags.test(InputLightClass::MULTI_INDEX) &&
            rawInfo.flags.test(InputLightClass::MULTI_INTENSITY)) {
            if (DEBUG_LIGHT_DETAILS) {
                ALOGD("Multicolor light Id %d name %s \n", rawInfo.id, rawInfo.name.c_str());
            }
            std::unique_ptr<Light> light =
                    std::make_unique<MultiColorLight>(getDeviceContext(), rawInfo.name, ++mNextId,
                                                      rawInfo.id);
            mLights.insert_or_assign(light->id, std::move(light));
            continue;
        }
        // Construct a Mono LED light
        if (DEBUG_LIGHT_DETAILS) {
            ALOGD("Mono light Id %d name %s \n", rawInfo.id, rawInfo.name.c_str());
        }
        std::unique_ptr<Light> light = std::make_unique<MonoLight>(getDeviceContext(), rawInfo.name,
                                                                   ++mNextId, rawInfo.id);

        mLights.insert_or_assign(light->id, std::move(light));
    }
}

std::optional<int32_t> PeripheralController::getBatteryCapacity(int batteryId) {
    return getDeviceContext().getBatteryCapacity(batteryId);
}

std::optional<int32_t> PeripheralController::getBatteryStatus(int batteryId) {
    return getDeviceContext().getBatteryStatus(batteryId);
}

bool PeripheralController::setLightColor(int32_t lightId, int32_t color) {
    auto it = mLights.find(lightId);
    if (it == mLights.end()) {
        return false;
    }
    auto& light = it->second;
    if (DEBUG_LIGHT_DETAILS) {
        ALOGD("setLightColor lightId %d type %s color 0x%x", lightId,
              ftl::enum_string(light->type).c_str(), color);
    }
    return light->setLightColor(color);
}

std::optional<int32_t> PeripheralController::getLightColor(int32_t lightId) {
    auto it = mLights.find(lightId);
    if (it == mLights.end()) {
        return std::nullopt;
    }
    auto& light = it->second;
    std::optional<int32_t> color = light->getLightColor();
    if (DEBUG_LIGHT_DETAILS) {
        ALOGD("getLightColor lightId %d type %s color 0x%x", lightId,
              ftl::enum_string(light->type).c_str(), color.value_or(0));
    }
    return color;
}

bool PeripheralController::setLightPlayerId(int32_t lightId, int32_t playerId) {
    auto it = mLights.find(lightId);
    if (it == mLights.end()) {
        return false;
    }
    auto& light = it->second;
    return light->setLightPlayerId(playerId);
}

std::optional<int32_t> PeripheralController::getLightPlayerId(int32_t lightId) {
    auto it = mLights.find(lightId);
    if (it == mLights.end()) {
        return std::nullopt;
    }
    auto& light = it->second;
    return light->getLightPlayerId();
}

} // namespace android