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/commands/assemble_cvd/display_flags.h"
18
19 #include <unordered_map>
20 #include <vector>
21
22 #include <android-base/logging.h>
23 #include <android-base/parseint.h>
24 #include <android-base/strings.h>
25
26 #include "common/libs/utils/contains.h"
27 #include "host/commands/assemble_cvd/flags_defaults.h"
28
29 namespace cuttlefish {
30
ParseDisplayConfig(const std::string & flag)31 Result<std::optional<CuttlefishConfig::DisplayConfig>> ParseDisplayConfig(
32 const std::string& flag) {
33 if (flag.empty()) {
34 return std::nullopt;
35 }
36
37 std::unordered_map<std::string, std::string> props;
38
39 const std::vector<std::string> pairs = android::base::Split(flag, ",");
40 for (const std::string& pair : pairs) {
41 const std::vector<std::string> keyvalue = android::base::Split(pair, "=");
42 CF_EXPECT_EQ(keyvalue.size(), 2,
43 "Invalid display flag key-value: \"" << flag << "\"");
44 const std::string& prop_key = keyvalue[0];
45 const std::string& prop_val = keyvalue[1];
46 props[prop_key] = prop_val;
47 }
48
49 CF_EXPECT(Contains(props, "width"),
50 "Display configuration missing 'width' in \"" << flag << "\"");
51 CF_EXPECT(Contains(props, "height"),
52 "Display configuration missing 'height' in \"" << flag << "\"");
53
54 int display_width;
55 CF_EXPECT(android::base::ParseInt(props["width"], &display_width),
56 "Display configuration invalid 'width' in \"" << flag << "\"");
57
58 int display_height;
59 CF_EXPECT(android::base::ParseInt(props["height"], &display_height),
60 "Display configuration invalid 'height' in \"" << flag << "\"");
61
62 int display_dpi = CF_DEFAULTS_DISPLAY_DPI;
63 auto display_dpi_it = props.find("dpi");
64 if (display_dpi_it != props.end()) {
65 CF_EXPECT(android::base::ParseInt(display_dpi_it->second, &display_dpi),
66 "Display configuration invalid 'dpi' in \"" << flag << "\"");
67 }
68
69 int display_refresh_rate_hz = CF_DEFAULTS_DISPLAY_REFRESH_RATE;
70 auto display_refresh_rate_hz_it = props.find("refresh_rate_hz");
71 if (display_refresh_rate_hz_it != props.end()) {
72 CF_EXPECT(android::base::ParseInt(display_refresh_rate_hz_it->second,
73 &display_refresh_rate_hz),
74 "Display configuration invalid 'refresh_rate_hz' in \"" << flag
75 << "\"");
76 }
77
78 return CuttlefishConfig::DisplayConfig{
79 .width = display_width,
80 .height = display_height,
81 .dpi = display_dpi,
82 .refresh_rate_hz = display_refresh_rate_hz,
83 };
84 }
85
86 } // namespace cuttlefish