1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_ROSEN_FOLD_SCREEN_STATE_INTERNEL_H 17 #define OHOS_ROSEN_FOLD_SCREEN_STATE_INTERNEL_H 18 19 #include <sstream> 20 #include <parameters.h> 21 #include <regex> 22 23 namespace OHOS { 24 namespace Rosen { 25 namespace { 26 static const std::string INVALID_DEVICE = "-1"; 27 static const std::string g_foldScreenType = system::GetParameter("const.window.foldscreen.type", "0,0,0,0"); 28 static const std::string PHY_ROTATION_OFFSET = system::GetParameter("const.window.phyrotation.offset", "0"); 29 static const std::string SINGLE_DISPLAY = "1"; 30 static const std::string DUAL_DISPLAY = "2"; 31 static const std::string SINGLE_POCKET_DISPLAY = "4"; 32 static const std::string SUPER_FOLD_DISPLAY = "5"; 33 static const std::string SECONDARY_FOLD_DISPLAY = "6"; 34 static const std::string DEFAULT_OFFSET = "0"; 35 static const size_t THIRD_ANGLE = 2; 36 } 37 class FoldScreenStateInternel { 38 public: GetFoldType()39 static std::string GetFoldType() 40 { 41 if (!IsValidFoldType(g_foldScreenType)) { 42 return INVALID_DEVICE; 43 } 44 std::vector<std::string> foldTypes = StringSplit(g_foldScreenType, ','); 45 if (foldTypes.empty()) { 46 return INVALID_DEVICE; 47 } 48 return foldTypes[0]; 49 } 50 IsFoldScreenDevice()51 static bool IsFoldScreenDevice() 52 { 53 return g_foldScreenType != ""; 54 } 55 IsDualDisplayFoldDevice()56 static bool IsDualDisplayFoldDevice() 57 { 58 if (!IsValidFoldType(g_foldScreenType)) { 59 return false; 60 } 61 std::vector<std::string> foldTypes = StringSplit(g_foldScreenType, ','); 62 if (foldTypes.empty()) { 63 return false; 64 } 65 return foldTypes[0] == DUAL_DISPLAY; 66 } 67 IsSingleDisplayFoldDevice()68 static bool IsSingleDisplayFoldDevice() 69 { 70 // ALTB ccm property conflict with the chip, waiting for chip conflict resolution 71 return !IsDualDisplayFoldDevice() && !IsSingleDisplayPocketFoldDevice() && 72 !IsSuperFoldDisplayDevice() && !IsSecondaryDisplayFoldDevice(); 73 } 74 IsSingleDisplayPocketFoldDevice()75 static bool IsSingleDisplayPocketFoldDevice() 76 { 77 if (!IsValidFoldType(g_foldScreenType)) { 78 return false; 79 } 80 std::vector<std::string> foldTypes = StringSplit(g_foldScreenType, ','); 81 if (foldTypes.empty()) { 82 return false; 83 } 84 return foldTypes[0] == SINGLE_POCKET_DISPLAY; 85 } 86 IsSuperFoldDisplayDevice()87 static bool IsSuperFoldDisplayDevice() 88 { 89 if (!IsValidFoldType(g_foldScreenType)) { 90 return false; 91 } 92 std::vector<std::string> foldTypes = StringSplit(g_foldScreenType, ','); 93 if (foldTypes.empty()) { 94 return false; 95 } 96 return foldTypes[0] == SUPER_FOLD_DISPLAY; 97 } 98 IsSecondaryDisplayFoldDevice()99 static bool IsSecondaryDisplayFoldDevice() 100 { 101 return GetFoldType() == SECONDARY_FOLD_DISPLAY; 102 } 103 IsOuterScreen(FoldDisplayMode foldDisplayMode)104 static bool IsOuterScreen(FoldDisplayMode foldDisplayMode) 105 { 106 if (IsDualDisplayFoldDevice()) { 107 return foldDisplayMode == FoldDisplayMode::SUB; 108 } 109 if (IsSingleDisplayFoldDevice() || IsSingleDisplayPocketFoldDevice()) { 110 return foldDisplayMode == FoldDisplayMode::MAIN; 111 } 112 return false; 113 } 114 StringSplit(const std::string & str,char delim)115 static std::vector<std::string> StringSplit(const std::string& str, char delim) 116 { 117 std::size_t previous = 0; 118 std::size_t current = str.find(delim); 119 std::vector<std::string> elems; 120 while (current != std::string::npos) { 121 if (current > previous) { 122 elems.push_back(str.substr(previous, current - previous)); 123 } 124 previous = current + 1; 125 current = str.find(delim, previous); 126 } 127 if (previous != str.size()) { 128 elems.push_back(str.substr(previous)); 129 } 130 return elems; 131 } 132 GetPhyRotationOffset()133 static std::vector<std::string> GetPhyRotationOffset() 134 { 135 static std::vector<std::string> phyOffsets; 136 if (phyOffsets.empty()) { 137 std::vector<std::string> elems = StringSplit(PHY_ROTATION_OFFSET, ';'); 138 for (auto& num : elems) { 139 if (IsNumber(num)) { 140 phyOffsets.push_back(num); 141 } else { 142 phyOffsets.push_back(DEFAULT_OFFSET); 143 } 144 } 145 } 146 return phyOffsets; 147 } 148 IsNumber(const std::string & str)149 static bool IsNumber(const std::string& str) 150 { 151 int32_t length = static_cast<int32_t>(str.size()); 152 if (length == 0) { 153 return false; 154 } 155 for (int32_t i = 0; i < length; i++) { 156 if (str.at(i) < '0' || str.at(i) > '9') { 157 return false; 158 } 159 } 160 return true; 161 } 162 IsValidFoldType(const std::string & foldTypeStr)163 static bool IsValidFoldType(const std::string& foldTypeStr) 164 { 165 std::regex reg("^([0-9],){3}[0-9]{1}$"); 166 return std::regex_match(foldTypeStr, reg); 167 } 168 StringFoldRectSplitToInt(const std::string & str,const std::string & delims)169 static std::vector<int32_t> StringFoldRectSplitToInt(const std::string& str, const std::string& delims) 170 { 171 if (str.empty()) { 172 return {}; 173 } 174 std::vector<int32_t> numbers; 175 std::vector<std::string> tokens; 176 if (delims.empty()) { 177 tokens.push_back(str); 178 } else { 179 std::size_t previous = 0; 180 std::size_t current = str.find_first_of(delims); 181 while (current != std::string::npos) { 182 if (current > previous) { 183 tokens.push_back(str.substr(previous, current - previous)); 184 } 185 previous = current + 1; 186 current = str.find_first_of(delims, previous); 187 } 188 if (previous < str.size()) { 189 tokens.push_back(str.substr(previous)); 190 } 191 } 192 if (!IsParamsDigitsOnly(tokens)) { 193 return {}; 194 } 195 for (const auto& token : tokens) { 196 numbers.push_back(std::stoi(token)); 197 } 198 return numbers; 199 } 200 IsParamsDigitsOnly(const std::vector<std::string> & params)201 static bool IsParamsDigitsOnly(const std::vector<std::string>& params) 202 { 203 for (const auto& param : params) { 204 if (param.size() == 0) { 205 return false; 206 } 207 for (size_t i = 0; i < param.size(); ++i) { 208 if (param.at(i) < '0' || param.at(i) > '9') { 209 return false; 210 } 211 } 212 } 213 return true; 214 } 215 216 template<typename T> TransVec2Str(const std::vector<T> & vec,const std::string & name)217 static std::string TransVec2Str(const std::vector<T> &vec, const std::string &name) 218 { 219 std::stringstream strs; 220 for (uint32_t i = 0; i < vec.size(); i++) { 221 auto str = vec[i]; 222 strs << name; 223 if (i == 0) { 224 strs << "_bc"; 225 } else if (i == 1) { 226 strs << "_ab"; 227 } else if (i == THIRD_ANGLE) { 228 strs << "_ab_anti"; 229 } 230 strs << ": "; 231 strs << std::to_string(str) << " "; 232 } 233 return strs.str(); 234 } 235 236 inline static bool FloatEqualAbs(float a, float b, float epsilon = 1e-6f) 237 { 238 if (epsilon < 0) { 239 return false; 240 } 241 242 return std::fabs(a-b) < epsilon; 243 } 244 }; 245 } // Rosen 246 } // OHOS 247 #endif // OHOS_ROSEN_FOLD_SCREEN_STATE_INTERNEL_H