bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 1 | package twrp |
| 2 | |
| 3 | import ( |
| 4 | "android/soong/android" |
| 5 | "android/soong/cc" |
| 6 | "fmt" |
Mohd Faraz | f202c03 | 2022-06-05 13:31:47 +0200 | [diff] [blame] | 7 | "io/ioutil" |
bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 8 | "os" |
| 9 | "path" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | ) |
| 13 | |
| 14 | func printThemeWarning(theme string) { |
| 15 | if theme == "" { |
| 16 | theme = "not set" |
| 17 | } |
| 18 | themeWarning := "***************************************************************************\n" |
| 19 | themeWarning += "Could not find ui.xml for TW_THEME: " |
| 20 | themeWarning += theme |
| 21 | themeWarning += "\nSet TARGET_SCREEN_WIDTH and TARGET_SCREEN_HEIGHT to automatically select\n" |
| 22 | themeWarning += "an appropriate theme, or set TW_THEME to one of the following:\n" |
| 23 | themeWarning += "landscape_hdpi landscape_mdpi portrait_hdpi portrait_mdpi watch_mdpi\n" |
| 24 | themeWarning += "****************************************************************************\n" |
| 25 | themeWarning += "(theme selection failed; exiting)\n" |
| 26 | |
| 27 | fmt.Printf(themeWarning) |
| 28 | } |
| 29 | |
| 30 | func printCustomThemeWarning(theme string, location string) { |
| 31 | customThemeWarning := "****************************************************************************\n" |
| 32 | customThemeWarning += "Could not find ui.xml for TW_CUSTOM_THEME: " |
| 33 | customThemeWarning += theme + "\n" |
| 34 | customThemeWarning += "Expected to find custom theme's ui.xml at: " |
| 35 | customThemeWarning += location |
| 36 | customThemeWarning += "Please fix this or set TW_THEME to one of the following:\n" |
| 37 | customThemeWarning += "landscape_hdpi landscape_mdpi portrait_hdpi portrait_mdpi watch_mdpi\n" |
| 38 | customThemeWarning += "****************************************************************************\n" |
| 39 | customThemeWarning += "(theme selection failed; exiting)\n" |
| 40 | fmt.Printf(customThemeWarning) |
| 41 | } |
| 42 | |
| 43 | func copyThemeResources(ctx android.BaseContext, dirs []string, files []string) { |
| 44 | outDir := ctx.Config().Getenv("OUT") |
| 45 | twRes := outDir + "/recovery/root/twres/" |
Captain Throwback | b7a3c6f | 2022-02-18 12:55:42 -0500 | [diff] [blame] | 46 | os.MkdirAll(twRes, os.ModePerm) |
bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 47 | recoveryDir := getRecoveryAbsDir(ctx) |
| 48 | theme := determineTheme(ctx) |
| 49 | for idx, dir := range dirs { |
| 50 | _ = idx |
| 51 | dirToCopy := "" |
| 52 | destDir := twRes + path.Base(dir) |
| 53 | baseDir := path.Base(dir) |
| 54 | if baseDir == theme { |
| 55 | destDir = twRes |
| 56 | dirToCopy = recoveryDir + dir |
| 57 | } else { |
| 58 | dirToCopy = recoveryDir + dir |
| 59 | } |
| 60 | copyDir(dirToCopy, destDir) |
| 61 | } |
| 62 | for idx, file := range files { |
| 63 | _ = idx |
| 64 | fileToCopy := recoveryDir + file |
| 65 | fileDest := twRes + path.Base(file) |
| 66 | copyFile(fileToCopy, fileDest) |
| 67 | } |
Mohd Faraz | f202c03 | 2022-06-05 13:31:47 +0200 | [diff] [blame] | 68 | data, err := ioutil.ReadFile(recoveryDir + "variables.h") |
| 69 | if err != nil { |
| 70 | fmt.Println(err) |
| 71 | return |
| 72 | } |
| 73 | version := "0" |
| 74 | for _, line := range strings.Split(string(data), "\n") { |
| 75 | if strings.Contains(line, "TW_THEME_VERSION") { |
| 76 | version = strings.Split(line, " ")[2] |
| 77 | } |
| 78 | } |
Yillié | cb87d9d | 2022-07-11 14:30:26 +0500 | [diff] [blame] | 79 | |
| 80 | _props := [3]string{"TW_CUSTOM_BATTERY_POS", "TW_CUSTOM_CPU_POS", "TW_CUSTOM_CLOCK_POS" } |
| 81 | props := [3]string{"0", "0", "0"} |
| 82 | for i, item := range _props { |
| 83 | if getMakeVars(ctx, item) != "" { |
| 84 | props[i] = strings.Trim(getMakeVars(ctx, item), "\"") |
| 85 | } |
| 86 | } |
| 87 | |
Mohd Faraz | f202c03 | 2022-06-05 13:31:47 +0200 | [diff] [blame] | 88 | _files := [2]string{"splash.xml", "ui.xml"} |
| 89 | for _, i := range _files { |
Yillié | cb87d9d | 2022-07-11 14:30:26 +0500 | [diff] [blame] | 90 | var fontsize int = 0 |
| 91 | var width int = 0 |
| 92 | |
Mohd Faraz | f202c03 | 2022-06-05 13:31:47 +0200 | [diff] [blame] | 93 | data, err = ioutil.ReadFile(twRes + i) |
| 94 | if err != nil { |
| 95 | fmt.Println(err) |
| 96 | return |
| 97 | } |
Yillié | cb87d9d | 2022-07-11 14:30:26 +0500 | [diff] [blame] | 98 | |
Mohd Faraz | f202c03 | 2022-06-05 13:31:47 +0200 | [diff] [blame] | 99 | newFile := strings.Replace(string(data), "{themeversion}", version, -1) |
Yillié | cb87d9d | 2022-07-11 14:30:26 +0500 | [diff] [blame] | 100 | |
| 101 | // Custom position for status bar items - start |
| 102 | if i == "ui.xml" { |
| 103 | |
| 104 | for _, line := range strings.Split(string(data), "\n") { |
| 105 | if strings.Contains(line, "name=\"font_m\"") { |
| 106 | fontsize, err = strconv.Atoi(strings.Split(line, "\"")[5]) |
| 107 | if err != nil { |
| 108 | fmt.Println(err) |
| 109 | return |
| 110 | } |
| 111 | } |
| 112 | if strings.Contains(line, "resolution") { |
| 113 | width, err = strconv.Atoi(strings.Split(line, "\"")[1]) |
| 114 | if err != nil { |
| 115 | fmt.Println(err) |
| 116 | return |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | var cpusize int = (fontsize * 5) + (width/100) |
| 122 | var clocksize int = (fontsize * 4) + (width/100) |
| 123 | var batterysize int = (fontsize * 6) - (width/100) |
| 124 | var pos_clock_24 string = props[2] |
| 125 | for j := 0; j < len(props); j++ { |
| 126 | if props[j] == "left" { |
| 127 | props[j] = strconv.Itoa(width/50) |
| 128 | if _props[j] == "TW_CUSTOM_CLOCK_POS" { |
| 129 | pos_clock_24 = props[j] |
| 130 | } |
| 131 | } else if props[j] == "center" { |
| 132 | if _props[j] == "TW_CUSTOM_BATTERY_POS" { |
| 133 | props[j] = strconv.Itoa( (width/2) - (batterysize*43/100) ) |
| 134 | } else if _props[j] == "TW_CUSTOM_CLOCK_POS" { |
| 135 | pos := (width/2) - (clocksize*45/100) |
| 136 | props[j] = strconv.Itoa(pos) |
| 137 | pos_clock_24 = strconv.Itoa( pos * 31/30 ) |
| 138 | } else if _props[j] == "TW_CUSTOM_CPU_POS" { |
| 139 | props[j] = strconv.Itoa( (width/2) - (cpusize*41/100) ) |
| 140 | } |
| 141 | } else if props[j] == "right" { |
| 142 | if _props[j] == "TW_CUSTOM_BATTERY_POS" { |
| 143 | props[j] = strconv.Itoa( width - batterysize ) |
| 144 | } else if _props[j] == "TW_CUSTOM_CLOCK_POS" { |
| 145 | props[j] = strconv.Itoa(width - clocksize) |
| 146 | pos_clock_24 = props[j] |
| 147 | } else if _props[j] == "TW_CUSTOM_CPU_POS" { |
| 148 | props[j] = strconv.Itoa( width - cpusize ) |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Yillié | c061378 | 2022-07-11 14:30:43 +0500 | [diff] [blame] | 153 | alignProp := "%status_topalign_header_y%" |
| 154 | if strings.Trim(getMakeVars(ctx, "TW_STATUS_ICONS_ALIGN"), "\"") == "center" || strings.Trim(getMakeVars(ctx, "TW_STATUS_ICONS_ALIGN"), "\"") == "2" { |
| 155 | alignProp = "%status_centeralign_header_y%" |
| 156 | } else if strings.Trim(getMakeVars(ctx, "TW_STATUS_ICONS_ALIGN"), "\"") == "bottom" || strings.Trim(getMakeVars(ctx, "TW_STATUS_ICONS_ALIGN"), "\"") == "3" { |
| 157 | alignProp = "%status_bottomalign_header_y%" |
| 158 | } |
Yillié | cb87d9d | 2022-07-11 14:30:26 +0500 | [diff] [blame] | 159 | newFile = strings.Replace(newFile, "{battery_pos}", props[0], -1) |
| 160 | newFile = strings.Replace(newFile, "{cpu_pos}", props[1], -1) |
| 161 | newFile = strings.Replace(newFile, "{clock_12_pos}", props[2], -1) |
| 162 | newFile = strings.Replace(newFile, "{clock_24_pos}", pos_clock_24, -1) |
Yillié | c061378 | 2022-07-11 14:30:43 +0500 | [diff] [blame] | 163 | newFile = strings.Replace(newFile, "{statusicons_align}", alignProp, -1) |
Yillié | cb87d9d | 2022-07-11 14:30:26 +0500 | [diff] [blame] | 164 | } |
| 165 | // Custom position for status bar items - end |
| 166 | |
Mohd Faraz | f202c03 | 2022-06-05 13:31:47 +0200 | [diff] [blame] | 167 | err = ioutil.WriteFile(twRes + i, []byte(newFile), 0) |
| 168 | if err != nil { |
| 169 | fmt.Println(err) |
| 170 | return |
| 171 | } |
| 172 | } |
bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | func copyCustomTheme(ctx android.BaseContext, customTheme string) { |
| 176 | outDir := ctx.Config().Getenv("OUT") |
| 177 | twRes := outDir + "/recovery/root/twres/" |
Captain Throwback | b7a3c6f | 2022-02-18 12:55:42 -0500 | [diff] [blame] | 178 | os.MkdirAll(twRes, os.ModePerm) |
bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 179 | fileDest := twRes + path.Base(customTheme) |
| 180 | fileToCopy := fmt.Sprintf("%s%s", getBuildAbsDir(ctx), customTheme) |
| 181 | copyFile(fileToCopy, fileDest) |
| 182 | } |
| 183 | |
| 184 | func determineTheme(ctx android.BaseContext) string { |
| 185 | guiWidth := 0 |
| 186 | guiHeight := 0 |
| 187 | if getMakeVars(ctx, "TW_CUSTOM_THEME") == "" { |
| 188 | if getMakeVars(ctx, "TW_THEME") == "" { |
| 189 | if getMakeVars(ctx, "DEVICE_RESOLUTION") == "" { |
| 190 | width, err := strconv.Atoi(getMakeVars(ctx, "TARGET_SCREEN_WIDTH")) |
| 191 | if err == nil { |
| 192 | guiWidth = width |
| 193 | } |
| 194 | height, err := strconv.Atoi(getMakeVars(ctx, "TARGET_SCREEN_HEIGHT")) |
| 195 | if err == nil { |
| 196 | guiHeight = height |
| 197 | } |
| 198 | } else { |
| 199 | deviceRes := getMakeVars(ctx, "DEVICE_RESOLUTION") |
| 200 | width, err := strconv.Atoi(strings.Split(deviceRes, "x")[0]) |
| 201 | if err == nil { |
| 202 | guiWidth = width |
| 203 | } |
| 204 | height, err := strconv.Atoi(strings.Split(deviceRes, "x")[1]) |
| 205 | if err == nil { |
| 206 | guiHeight = height |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | if guiWidth > 100 { |
| 211 | if guiHeight > 100 { |
| 212 | if guiWidth > guiHeight { |
| 213 | if guiWidth > 1280 { |
| 214 | return "landscape_hdpi" |
| 215 | } else { |
| 216 | return "landscape_mdpi" |
| 217 | } |
| 218 | } else if guiWidth < guiHeight { |
| 219 | if guiWidth > 720 { |
| 220 | return "portrait_hdpi" |
| 221 | } else { |
| 222 | return "portrait_mdpi" |
| 223 | } |
| 224 | } else if guiWidth == guiHeight { |
| 225 | return "watch_mdpi" |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return getMakeVars(ctx, "TW_THEME") |
| 232 | } |
| 233 | |
| 234 | func copyTheme(ctx android.BaseContext) bool { |
| 235 | var directories []string |
| 236 | var files []string |
| 237 | var customThemeLoc string |
| 238 | localPath := ctx.ModuleDir() |
| 239 | directories = append(directories, "gui/theme/common/fonts/") |
| 240 | directories = append(directories, "gui/theme/common/languages/") |
| 241 | if getMakeVars(ctx, "TW_EXTRA_LANGUAGES") == "true" { |
| 242 | directories = append(directories, "gui/theme/extra-languages/fonts/") |
| 243 | directories = append(directories, "gui/theme/extra-languages/languages/") |
| 244 | } |
| 245 | var theme = determineTheme(ctx) |
| 246 | directories = append(directories, "gui/theme/"+theme) |
| 247 | themeXML := fmt.Sprintf("gui/theme/common/%s.xml", strings.Split(theme, "_")[0]) |
| 248 | files = append(files, themeXML) |
| 249 | if getMakeVars(ctx, "TW_CUSTOM_THEME") == "" { |
| 250 | defaultTheme := fmt.Sprintf("%s/theme/%s/ui.xml", localPath, theme) |
| 251 | if android.ExistentPathForSource(ctx, defaultTheme).Valid() { |
| 252 | fullDefaultThemePath := fmt.Sprintf("gui/theme/%s/ui.xml", theme) |
| 253 | files = append(files, fullDefaultThemePath) |
| 254 | } else { |
| 255 | printThemeWarning(theme) |
| 256 | return false |
| 257 | } |
| 258 | } else { |
| 259 | customThemeLoc = getMakeVars(ctx, "TW_CUSTOM_THEME") |
| 260 | if android.ExistentPathForSource(ctx, customThemeLoc).Valid() { |
| 261 | } else { |
| 262 | printCustomThemeWarning(customThemeLoc, getMakeVars(ctx, "TW_CUSTOM_THEME")) |
| 263 | return false |
| 264 | } |
| 265 | } |
| 266 | copyThemeResources(ctx, directories, files) |
| 267 | if customThemeLoc != "" { |
| 268 | copyCustomTheme(ctx, customThemeLoc) |
| 269 | } |
| 270 | return true |
| 271 | } |
| 272 | |
| 273 | func globalFlags(ctx android.BaseContext) []string { |
| 274 | var cflags []string |
| 275 | |
| 276 | if getMakeVars(ctx, "TW_DELAY_TOUCH_INIT_MS") != "" { |
| 277 | cflags = append(cflags, "-DTW_DELAY_TOUCH_INIT_MS="+getMakeVars(ctx, "TW_DELAY_TOUCH_INIT_MS")) |
| 278 | } |
| 279 | |
| 280 | if getMakeVars(ctx, "TW_EVENT_LOGGING") == "true" { |
| 281 | cflags = append(cflags, "-D_EVENT_LOGGING") |
| 282 | } |
| 283 | |
| 284 | if getMakeVars(ctx, "TW_USE_KEY_CODE_TOUCH_SYNC") != "" { |
| 285 | cflags = append(cflags, "DTW_USE_KEY_CODE_TOUCH_SYNC="+getMakeVars(ctx, "TW_USE_KEY_CODE_TOUCH_SYNC")) |
| 286 | } |
| 287 | |
HemanthJabalpuri | c651244 | 2021-07-05 11:26:44 +0000 | [diff] [blame] | 288 | if getMakeVars(ctx, "TW_SCREEN_BLANK_ON_BOOT") != "" { |
bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 289 | cflags = append(cflags, "-DTW_SCREEN_BLANK_ON_BOOT") |
| 290 | } |
| 291 | |
| 292 | if getMakeVars(ctx, "TW_OZIP_DECRYPT_KEY") != "" { |
| 293 | cflags = append(cflags, "-DTW_OZIP_DECRYPT_KEY=\""+getMakeVars(ctx, "TW_OZIP_DECRYPT_KEY")+"\"") |
| 294 | } else { |
| 295 | cflags = append(cflags, "-DTW_OZIP_DECRYPT_KEY=0") |
| 296 | } |
| 297 | |
| 298 | if getMakeVars(ctx, "TW_NO_SCREEN_BLANK") != "" { |
| 299 | cflags = append(cflags, "-DTW_NO_SCREEN_BLANK") |
| 300 | } |
| 301 | |
| 302 | if getMakeVars(ctx, "TW_NO_SCREEN_TIMEOUT") != "" { |
| 303 | cflags = append(cflags, "-DTW_NO_SCREEN_TIMEOUT") |
| 304 | } |
| 305 | |
| 306 | if getMakeVars(ctx, "TW_OEM_BUILD") != "" { |
| 307 | cflags = append(cflags, "-DTW_OEM_BUILD") |
| 308 | } |
| 309 | |
| 310 | if getMakeVars(ctx, "TW_X_OFFSET") != "" { |
| 311 | cflags = append(cflags, "-DTW_X_OFFSET="+getMakeVars(ctx, "TW_X_OFFSET")) |
| 312 | } |
| 313 | |
| 314 | if getMakeVars(ctx, "TW_Y_OFFSET") != "" { |
| 315 | cflags = append(cflags, "-DTW_Y_OFFSET="+getMakeVars(ctx, "TW_Y_OFFSET")) |
| 316 | } |
| 317 | |
| 318 | if getMakeVars(ctx, "TW_W_OFFSET") != "" { |
| 319 | cflags = append(cflags, "-DTW_W_OFFSET="+getMakeVars(ctx, "TW_W_OFFSET")) |
| 320 | } |
| 321 | |
| 322 | if getMakeVars(ctx, "TW_H_OFFSET") != "" { |
| 323 | cflags = append(cflags, "-DTW_H_OFFSET="+getMakeVars(ctx, "TW_H_OFFSET")) |
| 324 | } |
| 325 | |
| 326 | if getMakeVars(ctx, "TW_ROUND_SCREEN") == "true" { |
| 327 | cflags = append(cflags, "-DTW_ROUND_SCREEN") |
| 328 | } |
| 329 | |
| 330 | if getMakeVars(ctx, "TW_EXCLUDE_NANO") == "true" { |
| 331 | cflags = append(cflags, "-DTW_EXCLUDE_NANO") |
| 332 | } |
| 333 | |
| 334 | if getMakeVars(ctx, "AB_OTA_UPDATER") == "true" { |
| 335 | cflags = append(cflags, "-DAB_OTA_UPDATER=1") |
| 336 | } |
| 337 | |
bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 338 | return cflags |
| 339 | } |
| 340 | |
| 341 | func globalSrcs(ctx android.BaseContext) []string { |
| 342 | var srcs []string |
| 343 | |
| 344 | if getMakeVars(ctx, "TWRP_CUSTOM_KEYBOARD") != "" { |
| 345 | srcs = append(srcs, getMakeVars(ctx, "TWRP_CUSTOM_KEYBOARD")) |
| 346 | } else { |
| 347 | srcs = append(srcs, "hardwarekeyboard.cpp") |
| 348 | } |
| 349 | return srcs |
| 350 | } |
| 351 | |
| 352 | func globalIncludes(ctx android.BaseContext) []string { |
| 353 | var includes []string |
| 354 | |
| 355 | if getMakeVars(ctx, "TW_INCLUDE_CRYPTO") != "" { |
| 356 | includes = append(includes, "bootable/recovery/crypto/fscrypt") |
| 357 | } |
| 358 | return includes |
| 359 | } |
| 360 | |
| 361 | func libGuiDefaults(ctx android.LoadHookContext) { |
| 362 | type props struct { |
| 363 | Target struct { |
| 364 | Android struct { |
| 365 | Cflags []string |
| 366 | Enabled *bool |
| 367 | } |
| 368 | } |
| 369 | Cflags []string |
| 370 | Srcs []string |
| 371 | Include_dirs []string |
| 372 | } |
| 373 | |
| 374 | p := &props{} |
| 375 | p.Cflags = globalFlags(ctx) |
| 376 | s := globalSrcs(ctx) |
| 377 | p.Srcs = s |
| 378 | i := globalIncludes(ctx) |
| 379 | p.Include_dirs = i |
| 380 | ctx.AppendProperties(p) |
| 381 | if copyTheme(ctx) == false { |
| 382 | os.Exit(-1) |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | func init() { |
| 387 | android.RegisterModuleType("libguitwrp_defaults", libGuiDefaultsFactory) |
| 388 | } |
| 389 | |
| 390 | func libGuiDefaultsFactory() android.Module { |
| 391 | module := cc.DefaultsFactory() |
| 392 | android.AddLoadHook(module, libGuiDefaults) |
| 393 | |
| 394 | return module |
| 395 | } |