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" |
| 7 | "os" |
| 8 | "path" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | ) |
| 12 | |
| 13 | func printThemeWarning(theme string) { |
| 14 | if theme == "" { |
| 15 | theme = "not set" |
| 16 | } |
| 17 | themeWarning := "***************************************************************************\n" |
| 18 | themeWarning += "Could not find ui.xml for TW_THEME: " |
| 19 | themeWarning += theme |
| 20 | themeWarning += "\nSet TARGET_SCREEN_WIDTH and TARGET_SCREEN_HEIGHT to automatically select\n" |
| 21 | themeWarning += "an appropriate theme, or set TW_THEME to one of the following:\n" |
| 22 | themeWarning += "landscape_hdpi landscape_mdpi portrait_hdpi portrait_mdpi watch_mdpi\n" |
| 23 | themeWarning += "****************************************************************************\n" |
| 24 | themeWarning += "(theme selection failed; exiting)\n" |
| 25 | |
| 26 | fmt.Printf(themeWarning) |
| 27 | } |
| 28 | |
| 29 | func printCustomThemeWarning(theme string, location string) { |
| 30 | customThemeWarning := "****************************************************************************\n" |
| 31 | customThemeWarning += "Could not find ui.xml for TW_CUSTOM_THEME: " |
| 32 | customThemeWarning += theme + "\n" |
| 33 | customThemeWarning += "Expected to find custom theme's ui.xml at: " |
| 34 | customThemeWarning += location |
| 35 | customThemeWarning += "Please fix this or set TW_THEME to one of the following:\n" |
| 36 | customThemeWarning += "landscape_hdpi landscape_mdpi portrait_hdpi portrait_mdpi watch_mdpi\n" |
| 37 | customThemeWarning += "****************************************************************************\n" |
| 38 | customThemeWarning += "(theme selection failed; exiting)\n" |
| 39 | fmt.Printf(customThemeWarning) |
| 40 | } |
| 41 | |
| 42 | func copyThemeResources(ctx android.BaseContext, dirs []string, files []string) { |
| 43 | outDir := ctx.Config().Getenv("OUT") |
| 44 | twRes := outDir + "/recovery/root/twres/" |
Captain Throwback | b7a3c6f | 2022-02-18 12:55:42 -0500 | [diff] [blame] | 45 | os.MkdirAll(twRes, os.ModePerm) |
bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 46 | recoveryDir := getRecoveryAbsDir(ctx) |
| 47 | theme := determineTheme(ctx) |
| 48 | for idx, dir := range dirs { |
| 49 | _ = idx |
| 50 | dirToCopy := "" |
| 51 | destDir := twRes + path.Base(dir) |
| 52 | baseDir := path.Base(dir) |
| 53 | if baseDir == theme { |
| 54 | destDir = twRes |
| 55 | dirToCopy = recoveryDir + dir |
| 56 | } else { |
| 57 | dirToCopy = recoveryDir + dir |
| 58 | } |
| 59 | copyDir(dirToCopy, destDir) |
| 60 | } |
| 61 | for idx, file := range files { |
| 62 | _ = idx |
| 63 | fileToCopy := recoveryDir + file |
| 64 | fileDest := twRes + path.Base(file) |
| 65 | copyFile(fileToCopy, fileDest) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func copyCustomTheme(ctx android.BaseContext, customTheme string) { |
| 70 | outDir := ctx.Config().Getenv("OUT") |
| 71 | twRes := outDir + "/recovery/root/twres/" |
Captain Throwback | b7a3c6f | 2022-02-18 12:55:42 -0500 | [diff] [blame] | 72 | os.MkdirAll(twRes, os.ModePerm) |
bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 73 | fileDest := twRes + path.Base(customTheme) |
| 74 | fileToCopy := fmt.Sprintf("%s%s", getBuildAbsDir(ctx), customTheme) |
| 75 | copyFile(fileToCopy, fileDest) |
| 76 | } |
| 77 | |
| 78 | func determineTheme(ctx android.BaseContext) string { |
| 79 | guiWidth := 0 |
| 80 | guiHeight := 0 |
| 81 | if getMakeVars(ctx, "TW_CUSTOM_THEME") == "" { |
| 82 | if getMakeVars(ctx, "TW_THEME") == "" { |
| 83 | if getMakeVars(ctx, "DEVICE_RESOLUTION") == "" { |
| 84 | width, err := strconv.Atoi(getMakeVars(ctx, "TARGET_SCREEN_WIDTH")) |
| 85 | if err == nil { |
| 86 | guiWidth = width |
| 87 | } |
| 88 | height, err := strconv.Atoi(getMakeVars(ctx, "TARGET_SCREEN_HEIGHT")) |
| 89 | if err == nil { |
| 90 | guiHeight = height |
| 91 | } |
| 92 | } else { |
| 93 | deviceRes := getMakeVars(ctx, "DEVICE_RESOLUTION") |
| 94 | width, err := strconv.Atoi(strings.Split(deviceRes, "x")[0]) |
| 95 | if err == nil { |
| 96 | guiWidth = width |
| 97 | } |
| 98 | height, err := strconv.Atoi(strings.Split(deviceRes, "x")[1]) |
| 99 | if err == nil { |
| 100 | guiHeight = height |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | if guiWidth > 100 { |
| 105 | if guiHeight > 100 { |
| 106 | if guiWidth > guiHeight { |
| 107 | if guiWidth > 1280 { |
| 108 | return "landscape_hdpi" |
| 109 | } else { |
| 110 | return "landscape_mdpi" |
| 111 | } |
| 112 | } else if guiWidth < guiHeight { |
| 113 | if guiWidth > 720 { |
| 114 | return "portrait_hdpi" |
| 115 | } else { |
| 116 | return "portrait_mdpi" |
| 117 | } |
| 118 | } else if guiWidth == guiHeight { |
| 119 | return "watch_mdpi" |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return getMakeVars(ctx, "TW_THEME") |
| 126 | } |
| 127 | |
| 128 | func copyTheme(ctx android.BaseContext) bool { |
| 129 | var directories []string |
| 130 | var files []string |
| 131 | var customThemeLoc string |
| 132 | localPath := ctx.ModuleDir() |
| 133 | directories = append(directories, "gui/theme/common/fonts/") |
| 134 | directories = append(directories, "gui/theme/common/languages/") |
| 135 | if getMakeVars(ctx, "TW_EXTRA_LANGUAGES") == "true" { |
| 136 | directories = append(directories, "gui/theme/extra-languages/fonts/") |
| 137 | directories = append(directories, "gui/theme/extra-languages/languages/") |
| 138 | } |
| 139 | var theme = determineTheme(ctx) |
| 140 | directories = append(directories, "gui/theme/"+theme) |
| 141 | themeXML := fmt.Sprintf("gui/theme/common/%s.xml", strings.Split(theme, "_")[0]) |
| 142 | files = append(files, themeXML) |
| 143 | if getMakeVars(ctx, "TW_CUSTOM_THEME") == "" { |
| 144 | defaultTheme := fmt.Sprintf("%s/theme/%s/ui.xml", localPath, theme) |
| 145 | if android.ExistentPathForSource(ctx, defaultTheme).Valid() { |
| 146 | fullDefaultThemePath := fmt.Sprintf("gui/theme/%s/ui.xml", theme) |
| 147 | files = append(files, fullDefaultThemePath) |
| 148 | } else { |
| 149 | printThemeWarning(theme) |
| 150 | return false |
| 151 | } |
| 152 | } else { |
| 153 | customThemeLoc = getMakeVars(ctx, "TW_CUSTOM_THEME") |
| 154 | if android.ExistentPathForSource(ctx, customThemeLoc).Valid() { |
| 155 | } else { |
| 156 | printCustomThemeWarning(customThemeLoc, getMakeVars(ctx, "TW_CUSTOM_THEME")) |
| 157 | return false |
| 158 | } |
| 159 | } |
| 160 | copyThemeResources(ctx, directories, files) |
| 161 | if customThemeLoc != "" { |
| 162 | copyCustomTheme(ctx, customThemeLoc) |
| 163 | } |
| 164 | return true |
| 165 | } |
| 166 | |
| 167 | func globalFlags(ctx android.BaseContext) []string { |
| 168 | var cflags []string |
| 169 | |
| 170 | if getMakeVars(ctx, "TW_DELAY_TOUCH_INIT_MS") != "" { |
| 171 | cflags = append(cflags, "-DTW_DELAY_TOUCH_INIT_MS="+getMakeVars(ctx, "TW_DELAY_TOUCH_INIT_MS")) |
| 172 | } |
| 173 | |
| 174 | if getMakeVars(ctx, "TW_EVENT_LOGGING") == "true" { |
| 175 | cflags = append(cflags, "-D_EVENT_LOGGING") |
| 176 | } |
| 177 | |
| 178 | if getMakeVars(ctx, "TW_USE_KEY_CODE_TOUCH_SYNC") != "" { |
| 179 | cflags = append(cflags, "DTW_USE_KEY_CODE_TOUCH_SYNC="+getMakeVars(ctx, "TW_USE_KEY_CODE_TOUCH_SYNC")) |
| 180 | } |
| 181 | |
HemanthJabalpuri | c651244 | 2021-07-05 11:26:44 +0000 | [diff] [blame] | 182 | if getMakeVars(ctx, "TW_SCREEN_BLANK_ON_BOOT") != "" { |
bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 183 | cflags = append(cflags, "-DTW_SCREEN_BLANK_ON_BOOT") |
| 184 | } |
| 185 | |
| 186 | if getMakeVars(ctx, "TW_OZIP_DECRYPT_KEY") != "" { |
| 187 | cflags = append(cflags, "-DTW_OZIP_DECRYPT_KEY=\""+getMakeVars(ctx, "TW_OZIP_DECRYPT_KEY")+"\"") |
| 188 | } else { |
| 189 | cflags = append(cflags, "-DTW_OZIP_DECRYPT_KEY=0") |
| 190 | } |
| 191 | |
| 192 | if getMakeVars(ctx, "TW_NO_SCREEN_BLANK") != "" { |
| 193 | cflags = append(cflags, "-DTW_NO_SCREEN_BLANK") |
| 194 | } |
| 195 | |
| 196 | if getMakeVars(ctx, "TW_NO_SCREEN_TIMEOUT") != "" { |
| 197 | cflags = append(cflags, "-DTW_NO_SCREEN_TIMEOUT") |
| 198 | } |
| 199 | |
| 200 | if getMakeVars(ctx, "TW_OEM_BUILD") != "" { |
| 201 | cflags = append(cflags, "-DTW_OEM_BUILD") |
| 202 | } |
| 203 | |
| 204 | if getMakeVars(ctx, "TW_X_OFFSET") != "" { |
| 205 | cflags = append(cflags, "-DTW_X_OFFSET="+getMakeVars(ctx, "TW_X_OFFSET")) |
| 206 | } |
| 207 | |
| 208 | if getMakeVars(ctx, "TW_Y_OFFSET") != "" { |
| 209 | cflags = append(cflags, "-DTW_Y_OFFSET="+getMakeVars(ctx, "TW_Y_OFFSET")) |
| 210 | } |
| 211 | |
| 212 | if getMakeVars(ctx, "TW_W_OFFSET") != "" { |
| 213 | cflags = append(cflags, "-DTW_W_OFFSET="+getMakeVars(ctx, "TW_W_OFFSET")) |
| 214 | } |
| 215 | |
| 216 | if getMakeVars(ctx, "TW_H_OFFSET") != "" { |
| 217 | cflags = append(cflags, "-DTW_H_OFFSET="+getMakeVars(ctx, "TW_H_OFFSET")) |
| 218 | } |
| 219 | |
| 220 | if getMakeVars(ctx, "TW_ROUND_SCREEN") == "true" { |
| 221 | cflags = append(cflags, "-DTW_ROUND_SCREEN") |
| 222 | } |
| 223 | |
| 224 | if getMakeVars(ctx, "TW_EXCLUDE_NANO") == "true" { |
| 225 | cflags = append(cflags, "-DTW_EXCLUDE_NANO") |
| 226 | } |
| 227 | |
| 228 | if getMakeVars(ctx, "AB_OTA_UPDATER") == "true" { |
| 229 | cflags = append(cflags, "-DAB_OTA_UPDATER=1") |
| 230 | } |
| 231 | |
bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 232 | return cflags |
| 233 | } |
| 234 | |
| 235 | func globalSrcs(ctx android.BaseContext) []string { |
| 236 | var srcs []string |
| 237 | |
| 238 | if getMakeVars(ctx, "TWRP_CUSTOM_KEYBOARD") != "" { |
| 239 | srcs = append(srcs, getMakeVars(ctx, "TWRP_CUSTOM_KEYBOARD")) |
| 240 | } else { |
| 241 | srcs = append(srcs, "hardwarekeyboard.cpp") |
| 242 | } |
| 243 | return srcs |
| 244 | } |
| 245 | |
| 246 | func globalIncludes(ctx android.BaseContext) []string { |
| 247 | var includes []string |
| 248 | |
| 249 | if getMakeVars(ctx, "TW_INCLUDE_CRYPTO") != "" { |
| 250 | includes = append(includes, "bootable/recovery/crypto/fscrypt") |
| 251 | } |
| 252 | return includes |
| 253 | } |
| 254 | |
| 255 | func libGuiDefaults(ctx android.LoadHookContext) { |
| 256 | type props struct { |
| 257 | Target struct { |
| 258 | Android struct { |
| 259 | Cflags []string |
| 260 | Enabled *bool |
| 261 | } |
| 262 | } |
| 263 | Cflags []string |
| 264 | Srcs []string |
| 265 | Include_dirs []string |
| 266 | } |
| 267 | |
| 268 | p := &props{} |
| 269 | p.Cflags = globalFlags(ctx) |
| 270 | s := globalSrcs(ctx) |
| 271 | p.Srcs = s |
| 272 | i := globalIncludes(ctx) |
| 273 | p.Include_dirs = i |
| 274 | ctx.AppendProperties(p) |
| 275 | if copyTheme(ctx) == false { |
| 276 | os.Exit(-1) |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | func init() { |
| 281 | android.RegisterModuleType("libguitwrp_defaults", libGuiDefaultsFactory) |
| 282 | } |
| 283 | |
| 284 | func libGuiDefaultsFactory() android.Module { |
| 285 | module := cc.DefaultsFactory() |
| 286 | android.AddLoadHook(module, libGuiDefaults) |
| 287 | |
| 288 | return module |
| 289 | } |