Rockbox open source high quality audio player as a Music Player Daemon
mpris rockbox mpd libadwaita audio rust zig deno
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

filetypes.c small cleanup

no functional changes

Change-Id: I9c27689920ba89abf5ba16d84ed0dad8747be74a

authored by

William Wilgus and committed by
William Wilgus
2591f6ad 253eb79d

+86 -77
+86 -77
apps/filetypes.c
··· 46 46 /* max viewer plugins */ 47 47 #define MAX_VIEWERS 56 48 48 49 + static void fill_from_builtin(const char*,int) INIT_ATTR; 49 50 static void read_builtin_types_init(void) INIT_ATTR; 50 51 static void read_viewers_config_init(void) INIT_ATTR; 51 52 static void read_config_init(int fd) INIT_ATTR; 53 + #ifdef HAVE_LCD_COLOR 54 + void read_color_theme_file(void) INIT_ATTR; 55 + #endif 52 56 53 57 /* string array for known audio file types (tree_attr == FILE_ATTR_AUDIO) */ 54 58 static const char* inbuilt_audio_filetypes[] = { ··· 142 146 #endif 143 147 }; 144 148 149 + static int filetype_inbuilt_index(int tree_attr) 150 + { 151 + size_t count = ARRAY_SIZE(inbuilt_attr_icons_voices); 152 + /* try to find a inbuilt index for the extension, if known */ 153 + tree_attr &= FILE_ATTR_MASK; /* file type */ 154 + 155 + for (size_t i = count - 1; i < count; i--) 156 + { 157 + if (tree_attr == inbuilt_attr_icons_voices[i].tree_attr) 158 + { 159 + logf("%s found attr %d id", __func__, tree_attr); 160 + return i; 161 + } 162 + } 163 + logf("%s not found attr %d", __func__, tree_attr); 164 + return -1; 165 + } 166 + 145 167 long tree_get_filetype_voiceclip(int attr) 146 168 { 147 169 if (global_settings.talk_filetype) 148 170 { 149 - size_t count = ARRAY_SIZE(inbuilt_attr_icons_voices); 150 - /* try to find a voice ID for the extension, if known */ 151 - attr &= FILE_ATTR_MASK; /* file type */ 152 - 153 - for (size_t i = count - 1; i < count; i--) 171 + int index = filetype_inbuilt_index(attr); 172 + if (index >= 0) 154 173 { 155 - if (attr == inbuilt_attr_icons_voices[i].tree_attr) 156 - { 157 - logf("%s found attr %d id %d", __func__, attr, 158 - inbuilt_attr_icons_voices[i].voiceclip); 159 - return inbuilt_attr_icons_voices[i].voiceclip; 160 - } 174 + logf("%s found attr %d id %d", __func__, attr, 175 + inbuilt_attr_icons_voices[index].voiceclip); 176 + return inbuilt_attr_icons_voices[index].voiceclip; 161 177 } 162 178 } 163 179 logf("%s not found attr %d", __func__, attr); ··· 176 192 static struct file_type filetypes[MAX_FILETYPES]; 177 193 178 194 static enum themable_icons custom_filetype_icons[MAX_FILETYPES]; 179 - 180 195 static bool custom_icons_loaded = false; 196 + 181 197 #ifdef HAVE_LCD_COLOR 182 198 static int custom_colors[MAX_FILETYPES]; 183 - #endif 184 199 struct filetype_unknown { 185 200 enum themable_icons icon; 186 - #ifdef HAVE_LCD_COLOR 187 201 int color; 188 - #endif 189 202 }; 190 203 static struct filetype_unknown unknown_file = { 191 204 .icon = Icon_NOICON, 192 - #ifdef HAVE_LCD_COLOR 193 205 .color = -1, 194 - #endif 195 206 }; 207 + #else 208 + struct filetype_unknown { enum themable_icons icon; }; 209 + static struct filetype_unknown unknown_file = { .icon = Icon_NOICON }; 210 + #endif 196 211 197 212 /* index array to filetypes used in open with list. */ 198 213 static int viewers[MAX_VIEWERS]; ··· 222 237 .shrink_callback = NULL, 223 238 }; 224 239 225 - static const char *filetypes_strdup(char* string) 240 + static const char *filetypes_strdup(const char* string) 226 241 { 227 242 char *buffer = core_get_data(strdup_handle) + strdup_cur_idx; 228 243 strdup_cur_idx += strlcpy(buffer, string, strdup_bufsize-strdup_cur_idx)+1; 229 244 return buffer; 230 245 } 231 246 232 - static const char *filetypes_store_plugin(char *plugin, int n) 247 + static const char *filetypes_store_plugin(const char *plugin, int n) 233 248 { 234 249 int i; 235 250 /* if the plugin is in the list already, use it. */ ··· 246 261 247 262 static int find_extension(const char* extension) 248 263 { 249 - int i; 250 - if (!extension) 251 - return -1; 252 - for (i=1; i<filetype_count; i++) 264 + if (extension) 253 265 { 254 - if (filetypes[i].extension && 255 - !strcasecmp(extension, filetypes[i].extension)) 256 - return i; 266 + for (int i=1; i<filetype_count; i++) 267 + { 268 + if (filetypes[i].extension && 269 + !strcasecmp(extension, filetypes[i].extension)) 270 + return i; 271 + } 257 272 } 258 273 return -1; 259 274 } ··· 288 303 hex_to_rgb(color, &custom_colors[0]); 289 304 continue; 290 305 } 291 - if (!strcasecmp(ext, "???")) 306 + if (!strcmp(ext, "???")) 292 307 { 293 308 hex_to_rgb(color, &unknown_file.color); 294 309 continue; ··· 300 315 close(fd); 301 316 } 302 317 #endif 318 + 319 + static int parse_icon(const char *line, enum themable_icons *icon) 320 + { 321 + int num = -1; 322 + if (*line == '*') 323 + { 324 + num = atoi(line+1); 325 + *icon = num; 326 + } 327 + else if (*line == '-') 328 + { 329 + *icon = Icon_NOICON; 330 + } 331 + else if (*line >= '0' && *line <= '9') 332 + { 333 + num = atoi(line); 334 + *icon = Icon_Last_Themeable + num; 335 + } 336 + return num; 337 + } 338 + 303 339 void read_viewer_theme_file(void) 304 340 { 305 341 char buffer[MAX_PATH]; ··· 309 345 enum themable_icons *icon_dest; 310 346 global_status.viewer_icon_count = 0; 311 347 custom_icons_loaded = false; 312 - custom_filetype_icons[0] = Icon_Folder; 313 - for (i=1; i<filetype_count; i++) 348 + /*custom_filetype_icons[0] = Icon_Folder; filetypes[0] is folder icon.. */ 349 + for (i=0; i<filetype_count; i++) 314 350 { 315 351 custom_filetype_icons[i] = filetypes[i].icon; 316 352 } ··· 334 370 335 371 if (icon_dest) 336 372 { 337 - if (*icon == '*') 338 - *icon_dest = atoi(icon+1); 339 - else if (*icon == '-') 340 - *icon_dest = Icon_NOICON; 341 - else if (*icon >= '0' && *icon <= '9') 342 - { 343 - int number = atoi(icon); 344 - if (number > global_status.viewer_icon_count) 345 - global_status.viewer_icon_count++; 346 - *icon_dest = Icon_Last_Themeable + number; 347 - } 373 + if (parse_icon(icon, icon_dest) > global_status.viewer_icon_count) 374 + global_status.viewer_icon_count++; 348 375 } 349 376 } 350 377 close(fd); ··· 411 438 412 439 static void fill_from_builtin(const char *ext, int tree_attr) 413 440 { 414 - size_t icon_count = ARRAY_SIZE(inbuilt_attr_icons_voices); 415 441 if (filetype_count >= MAX_FILETYPES) 416 442 return; 417 443 ··· 424 450 if (filetype->attr > highest_attr) 425 451 highest_attr = filetype->attr; 426 452 427 - for (size_t j = icon_count - 1; j < icon_count; j--) 453 + int index = filetype_inbuilt_index(tree_attr); 454 + if (index >= 0) 428 455 { 429 - if (tree_attr == inbuilt_attr_icons_voices[j].tree_attr) 430 - { 431 - filetype->icon = inbuilt_attr_icons_voices[j].icon; 432 - break; 433 - } 456 + filetype->icon = inbuilt_attr_icons_voices[index].icon; 434 457 } 458 + 435 459 filetype_count++; 436 460 } 437 461 ··· 452 476 static void read_config_init(int fd) 453 477 { 454 478 char line[64], *s, *e; 455 - char *extension, *plugin; 479 + const char *extension, *plugin; 456 480 /* config file is in the format 457 481 <extension>,<plugin>,<icon code> 458 482 ignore line if either of the first two are missing */ ··· 484 508 { 485 509 /* get the icon */ 486 510 s = e+1; 487 - if (*s == '*') 488 - unknown_file.icon = atoi(s+1); 489 - else if (*s == '-') 490 - unknown_file.icon = Icon_NOICON; 491 - else if (*s >= '0' && *s <= '9') 492 - unknown_file.icon = Icon_Last_Themeable + atoi(s); 511 + parse_icon(s, &unknown_file.icon); 493 512 continue; 494 513 } 495 514 ··· 502 521 highest_attr++; 503 522 /* get the icon */ 504 523 s = e+1; 505 - if (*s == '*') 506 - file_type->icon = atoi(s+1); 507 - else if (*s == '-') 508 - file_type->icon = Icon_NOICON; 509 - else if (*s >= '0' && *s <= '9') 510 - file_type->icon = Icon_Last_Themeable + atoi(s); 524 + parse_icon(s, &file_type->icon); 511 525 filetype_count++; 512 526 } 513 527 } 514 528 515 - int filetype_get_attr(const char* file) 529 + static int file_find_extension(const char* file) 516 530 { 517 531 char *extension = strrchr(file, '.'); 518 - int i; 519 - if (!extension) 520 - return 0; 521 - extension++; 532 + if (extension) 533 + extension++; 534 + return find_extension(extension); 535 + } 522 536 523 - i = find_extension(extension); 537 + int filetype_get_attr(const char* file) 538 + { 539 + int i = file_find_extension(file); 524 540 if (i >= 0) 525 541 return (filetypes[i].attr<<8)&FILE_ATTR_MASK; 526 542 return 0; ··· 543 559 #ifdef HAVE_LCD_COLOR 544 560 int filetype_get_color(const char * name, int attr) 545 561 { 546 - char *extension; 547 - int i; 548 562 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY) 549 563 return custom_colors[0]; 550 - extension = strrchr(name, '.'); 551 - if (!extension) 564 + int i = file_find_extension(name); 565 + if (i <= 0) 552 566 return unknown_file.color; 553 - extension++; 554 - 555 - i = find_extension(extension); 556 - if (i >= 0) 557 - return custom_colors[i]; 558 - return unknown_file.color; 567 + return custom_colors[i]; 559 568 } 560 569 #endif 561 570 ··· 700 709 char plugin_name[MAX_PATH]; 701 710 char *s; 702 711 703 - for (i=0;i<filetype_count;i++) 712 + for (i=1;i<filetype_count;i++) 704 713 { 705 714 if (filetypes[i].plugin) 706 715 {