xref: /aosp_15_r20/external/drm_hwcomposer/utils/properties.h (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1*0a9764feSAndroid Build Coastguard Worker /*
2*0a9764feSAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*0a9764feSAndroid Build Coastguard Worker  *
4*0a9764feSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*0a9764feSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*0a9764feSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*0a9764feSAndroid Build Coastguard Worker  *
8*0a9764feSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*0a9764feSAndroid Build Coastguard Worker  *
10*0a9764feSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*0a9764feSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*0a9764feSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*0a9764feSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*0a9764feSAndroid Build Coastguard Worker  * limitations under the License.
15*0a9764feSAndroid Build Coastguard Worker  */
16*0a9764feSAndroid Build Coastguard Worker 
17*0a9764feSAndroid Build Coastguard Worker #pragma once
18*0a9764feSAndroid Build Coastguard Worker 
19*0a9764feSAndroid Build Coastguard Worker #ifdef ANDROID
20*0a9764feSAndroid Build Coastguard Worker 
21*0a9764feSAndroid Build Coastguard Worker #include <cutils/properties.h>
22*0a9764feSAndroid Build Coastguard Worker 
23*0a9764feSAndroid Build Coastguard Worker #else
24*0a9764feSAndroid Build Coastguard Worker 
25*0a9764feSAndroid Build Coastguard Worker #include <cstdio>
26*0a9764feSAndroid Build Coastguard Worker #include <cstdlib>
27*0a9764feSAndroid Build Coastguard Worker #include <cstring>
28*0a9764feSAndroid Build Coastguard Worker 
29*0a9764feSAndroid Build Coastguard Worker // NOLINTNEXTLINE(readability-identifier-naming)
30*0a9764feSAndroid Build Coastguard Worker constexpr int PROPERTY_VALUE_MAX = 92;
31*0a9764feSAndroid Build Coastguard Worker 
32*0a9764feSAndroid Build Coastguard Worker // NOLINTNEXTLINE(readability-identifier-naming)
33*0a9764feSAndroid Build Coastguard Worker auto inline property_get(const char *name, char *value,
34*0a9764feSAndroid Build Coastguard Worker                          const char *default_value) -> int {
35*0a9764feSAndroid Build Coastguard Worker   // NOLINTNEXTLINE (concurrency-mt-unsafe)
36*0a9764feSAndroid Build Coastguard Worker   char *prop = std::getenv(name);
37*0a9764feSAndroid Build Coastguard Worker   snprintf(value, PROPERTY_VALUE_MAX, "%s",
38*0a9764feSAndroid Build Coastguard Worker            (prop == nullptr) ? default_value : prop);
39*0a9764feSAndroid Build Coastguard Worker   return static_cast<int>(strlen(value));
40*0a9764feSAndroid Build Coastguard Worker }
41*0a9764feSAndroid Build Coastguard Worker 
42*0a9764feSAndroid Build Coastguard Worker /**
43*0a9764feSAndroid Build Coastguard Worker  * Bluntly copied from system/core/libcutils/properties.cpp,
44*0a9764feSAndroid Build Coastguard Worker  * which is part of the Android Project and licensed under Apache 2.
45*0a9764feSAndroid Build Coastguard Worker  * Source:
46*0a9764feSAndroid Build Coastguard Worker  * https://cs.android.com/android/platform/superproject/main/+/main:system/core/libcutils/properties.cpp;l=27
47*0a9764feSAndroid Build Coastguard Worker  */
48*0a9764feSAndroid Build Coastguard Worker auto inline property_get_bool(const char *key, int8_t default_value) -> int8_t {
49*0a9764feSAndroid Build Coastguard Worker   if (!key)
50*0a9764feSAndroid Build Coastguard Worker     return default_value;
51*0a9764feSAndroid Build Coastguard Worker 
52*0a9764feSAndroid Build Coastguard Worker   int8_t result = default_value;
53*0a9764feSAndroid Build Coastguard Worker   char buf[PROPERTY_VALUE_MAX] = {};
54*0a9764feSAndroid Build Coastguard Worker 
55*0a9764feSAndroid Build Coastguard Worker   int len = property_get(key, buf, "");
56*0a9764feSAndroid Build Coastguard Worker   if (len == 1) {
57*0a9764feSAndroid Build Coastguard Worker     char ch = buf[0];
58*0a9764feSAndroid Build Coastguard Worker     if (ch == '0' || ch == 'n') {
59*0a9764feSAndroid Build Coastguard Worker       result = false;
60*0a9764feSAndroid Build Coastguard Worker     } else if (ch == '1' || ch == 'y') {
61*0a9764feSAndroid Build Coastguard Worker       result = true;
62*0a9764feSAndroid Build Coastguard Worker     }
63*0a9764feSAndroid Build Coastguard Worker   } else if (len > 1) {
64*0a9764feSAndroid Build Coastguard Worker     if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) {
65*0a9764feSAndroid Build Coastguard Worker       result = false;
66*0a9764feSAndroid Build Coastguard Worker     } else if (!strcmp(buf, "yes") || !strcmp(buf, "true") ||
67*0a9764feSAndroid Build Coastguard Worker                !strcmp(buf, "on")) {
68*0a9764feSAndroid Build Coastguard Worker       result = true;
69*0a9764feSAndroid Build Coastguard Worker     }
70*0a9764feSAndroid Build Coastguard Worker   }
71*0a9764feSAndroid Build Coastguard Worker 
72*0a9764feSAndroid Build Coastguard Worker   return result;
73*0a9764feSAndroid Build Coastguard Worker }
74*0a9764feSAndroid Build Coastguard Worker 
75*0a9764feSAndroid Build Coastguard Worker #endif
76*0a9764feSAndroid Build Coastguard Worker 
77*0a9764feSAndroid Build Coastguard Worker class Properties {
78*0a9764feSAndroid Build Coastguard Worker  public:
79*0a9764feSAndroid Build Coastguard Worker   static auto IsPresentFenceNotReliable() -> bool;
80*0a9764feSAndroid Build Coastguard Worker   static auto UseConfigGroups() -> bool;
81*0a9764feSAndroid Build Coastguard Worker };
82