1 /*
2 * Copyright (C) 2023 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 "host/libs/config/touchpad.h"
18
19 #include <algorithm>
20 #include <unordered_map>
21 #include <vector>
22
23 #include <android-base/logging.h>
24 #include <android-base/parseint.h>
25 #include <android-base/strings.h>
26
27 #include "common/libs/utils/contains.h"
28 #include "common/libs/utils/flag_parser.h"
29 #include "host/commands/assemble_cvd/flags_defaults.h"
30
31 namespace cuttlefish {
32
ParseTouchpadConfig(const std::string & flag)33 Result<CuttlefishConfig::TouchpadConfig> ParseTouchpadConfig(
34 const std::string& flag) {
35 CF_EXPECT(!flag.empty(), "Touchpad configuration empty");
36
37 std::unordered_map<std::string, std::string> props;
38 for (const auto& pair : android::base::Split(flag, ",")) {
39 const std::vector<std::string> keyvalue = android::base::Split(pair, "=");
40 CF_EXPECT_EQ(keyvalue.size(), 2,
41 "Invalid touchpad flag key-value: \"" << flag << "\"");
42 const std::string& prop_key = keyvalue[0];
43 const std::string& prop_val = keyvalue[1];
44 props[prop_key] = prop_val;
45 }
46
47 CF_EXPECT(Contains(props, "width"),
48 "Touchpad configuration missing 'width' in \"" << flag << "\"");
49 CF_EXPECT(Contains(props, "height"),
50 "Touchpad configuration missing 'height' in \"" << flag << "\"");
51 CF_EXPECT_EQ(
52 props.size(), 2,
53 "Touchpad configuration should only have width and height properties");
54
55 CuttlefishConfig::TouchpadConfig config = {};
56 CF_EXPECT(android::base::ParseInt(props["width"], &config.width),
57 "Touchpad configuration invalid 'width' in \"" << flag << "\"");
58 CF_EXPECT(android::base::ParseInt(props["height"], &config.height),
59 "Touchpad configuration invalid 'height' in \"" << flag << "\"");
60
61 return config;
62 }
63
64 Result<std::vector<CuttlefishConfig::TouchpadConfig>>
ParseTouchpadConfigsFromArgs(std::vector<std::string> & args)65 ParseTouchpadConfigsFromArgs(std::vector<std::string>& args) {
66 std::vector<std::string> repeated_touchpad_flag_values;
67
68 const std::vector<Flag> touchpad_flags = {
69 GflagsCompatFlag(kTouchpadFlag)
70 .Help(kTouchpadHelp)
71 .Setter([&](const FlagMatch& match) -> Result<void> {
72 repeated_touchpad_flag_values.push_back(match.value);
73 return {};
74 }),
75 };
76
77 CF_EXPECT(ConsumeFlags(touchpad_flags, args),
78 "Failed to parse touchpad flags.");
79
80 std::vector<CuttlefishConfig::TouchpadConfig> touchpad_configs;
81
82 for (const std::string& touchpad_params : repeated_touchpad_flag_values) {
83 auto touchpad = CF_EXPECT(ParseTouchpadConfig(touchpad_params));
84 touchpad_configs.push_back(touchpad);
85 }
86
87 return touchpad_configs;
88 }
89
90 } // namespace cuttlefish
91