mirror of
https://github.com/Rockbox/rockbox.git
synced 2025-10-14 10:37:38 -04:00
filetypes.c small cleanup
no functional changes Change-Id: I9c27689920ba89abf5ba16d84ed0dad8747be74a
This commit is contained in:
parent
253eb79db3
commit
2591f6ad02
1 changed files with 86 additions and 77 deletions
149
apps/filetypes.c
149
apps/filetypes.c
|
@ -46,9 +46,13 @@
|
||||||
/* max viewer plugins */
|
/* max viewer plugins */
|
||||||
#define MAX_VIEWERS 56
|
#define MAX_VIEWERS 56
|
||||||
|
|
||||||
|
static void fill_from_builtin(const char*,int) INIT_ATTR;
|
||||||
static void read_builtin_types_init(void) INIT_ATTR;
|
static void read_builtin_types_init(void) INIT_ATTR;
|
||||||
static void read_viewers_config_init(void) INIT_ATTR;
|
static void read_viewers_config_init(void) INIT_ATTR;
|
||||||
static void read_config_init(int fd) INIT_ATTR;
|
static void read_config_init(int fd) INIT_ATTR;
|
||||||
|
#ifdef HAVE_LCD_COLOR
|
||||||
|
void read_color_theme_file(void) INIT_ATTR;
|
||||||
|
#endif
|
||||||
|
|
||||||
/* string array for known audio file types (tree_attr == FILE_ATTR_AUDIO) */
|
/* string array for known audio file types (tree_attr == FILE_ATTR_AUDIO) */
|
||||||
static const char* inbuilt_audio_filetypes[] = {
|
static const char* inbuilt_audio_filetypes[] = {
|
||||||
|
@ -142,22 +146,34 @@ static const struct fileattr_icon_voice inbuilt_attr_icons_voices[] = {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static int filetype_inbuilt_index(int tree_attr)
|
||||||
|
{
|
||||||
|
size_t count = ARRAY_SIZE(inbuilt_attr_icons_voices);
|
||||||
|
/* try to find a inbuilt index for the extension, if known */
|
||||||
|
tree_attr &= FILE_ATTR_MASK; /* file type */
|
||||||
|
|
||||||
|
for (size_t i = count - 1; i < count; i--)
|
||||||
|
{
|
||||||
|
if (tree_attr == inbuilt_attr_icons_voices[i].tree_attr)
|
||||||
|
{
|
||||||
|
logf("%s found attr %d id", __func__, tree_attr);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logf("%s not found attr %d", __func__, tree_attr);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
long tree_get_filetype_voiceclip(int attr)
|
long tree_get_filetype_voiceclip(int attr)
|
||||||
{
|
{
|
||||||
if (global_settings.talk_filetype)
|
if (global_settings.talk_filetype)
|
||||||
{
|
{
|
||||||
size_t count = ARRAY_SIZE(inbuilt_attr_icons_voices);
|
int index = filetype_inbuilt_index(attr);
|
||||||
/* try to find a voice ID for the extension, if known */
|
if (index >= 0)
|
||||||
attr &= FILE_ATTR_MASK; /* file type */
|
|
||||||
|
|
||||||
for (size_t i = count - 1; i < count; i--)
|
|
||||||
{
|
|
||||||
if (attr == inbuilt_attr_icons_voices[i].tree_attr)
|
|
||||||
{
|
{
|
||||||
logf("%s found attr %d id %d", __func__, attr,
|
logf("%s found attr %d id %d", __func__, attr,
|
||||||
inbuilt_attr_icons_voices[i].voiceclip);
|
inbuilt_attr_icons_voices[index].voiceclip);
|
||||||
return inbuilt_attr_icons_voices[i].voiceclip;
|
return inbuilt_attr_icons_voices[index].voiceclip;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logf("%s not found attr %d", __func__, attr);
|
logf("%s not found attr %d", __func__, attr);
|
||||||
|
@ -176,23 +192,22 @@ struct file_type {
|
||||||
static struct file_type filetypes[MAX_FILETYPES];
|
static struct file_type filetypes[MAX_FILETYPES];
|
||||||
|
|
||||||
static enum themable_icons custom_filetype_icons[MAX_FILETYPES];
|
static enum themable_icons custom_filetype_icons[MAX_FILETYPES];
|
||||||
|
|
||||||
static bool custom_icons_loaded = false;
|
static bool custom_icons_loaded = false;
|
||||||
|
|
||||||
#ifdef HAVE_LCD_COLOR
|
#ifdef HAVE_LCD_COLOR
|
||||||
static int custom_colors[MAX_FILETYPES];
|
static int custom_colors[MAX_FILETYPES];
|
||||||
#endif
|
|
||||||
struct filetype_unknown {
|
struct filetype_unknown {
|
||||||
enum themable_icons icon;
|
enum themable_icons icon;
|
||||||
#ifdef HAVE_LCD_COLOR
|
|
||||||
int color;
|
int color;
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
static struct filetype_unknown unknown_file = {
|
static struct filetype_unknown unknown_file = {
|
||||||
.icon = Icon_NOICON,
|
.icon = Icon_NOICON,
|
||||||
#ifdef HAVE_LCD_COLOR
|
|
||||||
.color = -1,
|
.color = -1,
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
#else
|
||||||
|
struct filetype_unknown { enum themable_icons icon; };
|
||||||
|
static struct filetype_unknown unknown_file = { .icon = Icon_NOICON };
|
||||||
|
#endif
|
||||||
|
|
||||||
/* index array to filetypes used in open with list. */
|
/* index array to filetypes used in open with list. */
|
||||||
static int viewers[MAX_VIEWERS];
|
static int viewers[MAX_VIEWERS];
|
||||||
|
@ -222,14 +237,14 @@ static struct buflib_callbacks ops = {
|
||||||
.shrink_callback = NULL,
|
.shrink_callback = NULL,
|
||||||
};
|
};
|
||||||
|
|
||||||
static const char *filetypes_strdup(char* string)
|
static const char *filetypes_strdup(const char* string)
|
||||||
{
|
{
|
||||||
char *buffer = core_get_data(strdup_handle) + strdup_cur_idx;
|
char *buffer = core_get_data(strdup_handle) + strdup_cur_idx;
|
||||||
strdup_cur_idx += strlcpy(buffer, string, strdup_bufsize-strdup_cur_idx)+1;
|
strdup_cur_idx += strlcpy(buffer, string, strdup_bufsize-strdup_cur_idx)+1;
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *filetypes_store_plugin(char *plugin, int n)
|
static const char *filetypes_store_plugin(const char *plugin, int n)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
/* if the plugin is in the list already, use it. */
|
/* if the plugin is in the list already, use it. */
|
||||||
|
@ -246,15 +261,15 @@ static const char *filetypes_store_plugin(char *plugin, int n)
|
||||||
|
|
||||||
static int find_extension(const char* extension)
|
static int find_extension(const char* extension)
|
||||||
{
|
{
|
||||||
int i;
|
if (extension)
|
||||||
if (!extension)
|
{
|
||||||
return -1;
|
for (int i=1; i<filetype_count; i++)
|
||||||
for (i=1; i<filetype_count; i++)
|
|
||||||
{
|
{
|
||||||
if (filetypes[i].extension &&
|
if (filetypes[i].extension &&
|
||||||
!strcasecmp(extension, filetypes[i].extension))
|
!strcasecmp(extension, filetypes[i].extension))
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -288,7 +303,7 @@ void read_color_theme_file(void) {
|
||||||
hex_to_rgb(color, &custom_colors[0]);
|
hex_to_rgb(color, &custom_colors[0]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!strcasecmp(ext, "???"))
|
if (!strcmp(ext, "???"))
|
||||||
{
|
{
|
||||||
hex_to_rgb(color, &unknown_file.color);
|
hex_to_rgb(color, &unknown_file.color);
|
||||||
continue;
|
continue;
|
||||||
|
@ -300,6 +315,27 @@ void read_color_theme_file(void) {
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static int parse_icon(const char *line, enum themable_icons *icon)
|
||||||
|
{
|
||||||
|
int num = -1;
|
||||||
|
if (*line == '*')
|
||||||
|
{
|
||||||
|
num = atoi(line+1);
|
||||||
|
*icon = num;
|
||||||
|
}
|
||||||
|
else if (*line == '-')
|
||||||
|
{
|
||||||
|
*icon = Icon_NOICON;
|
||||||
|
}
|
||||||
|
else if (*line >= '0' && *line <= '9')
|
||||||
|
{
|
||||||
|
num = atoi(line);
|
||||||
|
*icon = Icon_Last_Themeable + num;
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
void read_viewer_theme_file(void)
|
void read_viewer_theme_file(void)
|
||||||
{
|
{
|
||||||
char buffer[MAX_PATH];
|
char buffer[MAX_PATH];
|
||||||
|
@ -309,8 +345,8 @@ void read_viewer_theme_file(void)
|
||||||
enum themable_icons *icon_dest;
|
enum themable_icons *icon_dest;
|
||||||
global_status.viewer_icon_count = 0;
|
global_status.viewer_icon_count = 0;
|
||||||
custom_icons_loaded = false;
|
custom_icons_loaded = false;
|
||||||
custom_filetype_icons[0] = Icon_Folder;
|
/*custom_filetype_icons[0] = Icon_Folder; filetypes[0] is folder icon.. */
|
||||||
for (i=1; i<filetype_count; i++)
|
for (i=0; i<filetype_count; i++)
|
||||||
{
|
{
|
||||||
custom_filetype_icons[i] = filetypes[i].icon;
|
custom_filetype_icons[i] = filetypes[i].icon;
|
||||||
}
|
}
|
||||||
|
@ -334,17 +370,8 @@ void read_viewer_theme_file(void)
|
||||||
|
|
||||||
if (icon_dest)
|
if (icon_dest)
|
||||||
{
|
{
|
||||||
if (*icon == '*')
|
if (parse_icon(icon, icon_dest) > global_status.viewer_icon_count)
|
||||||
*icon_dest = atoi(icon+1);
|
|
||||||
else if (*icon == '-')
|
|
||||||
*icon_dest = Icon_NOICON;
|
|
||||||
else if (*icon >= '0' && *icon <= '9')
|
|
||||||
{
|
|
||||||
int number = atoi(icon);
|
|
||||||
if (number > global_status.viewer_icon_count)
|
|
||||||
global_status.viewer_icon_count++;
|
global_status.viewer_icon_count++;
|
||||||
*icon_dest = Icon_Last_Themeable + number;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
|
@ -411,7 +438,6 @@ static void rm_whitespaces(char* str)
|
||||||
|
|
||||||
static void fill_from_builtin(const char *ext, int tree_attr)
|
static void fill_from_builtin(const char *ext, int tree_attr)
|
||||||
{
|
{
|
||||||
size_t icon_count = ARRAY_SIZE(inbuilt_attr_icons_voices);
|
|
||||||
if (filetype_count >= MAX_FILETYPES)
|
if (filetype_count >= MAX_FILETYPES)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -424,14 +450,12 @@ static void fill_from_builtin(const char *ext, int tree_attr)
|
||||||
if (filetype->attr > highest_attr)
|
if (filetype->attr > highest_attr)
|
||||||
highest_attr = filetype->attr;
|
highest_attr = filetype->attr;
|
||||||
|
|
||||||
for (size_t j = icon_count - 1; j < icon_count; j--)
|
int index = filetype_inbuilt_index(tree_attr);
|
||||||
|
if (index >= 0)
|
||||||
{
|
{
|
||||||
if (tree_attr == inbuilt_attr_icons_voices[j].tree_attr)
|
filetype->icon = inbuilt_attr_icons_voices[index].icon;
|
||||||
{
|
|
||||||
filetype->icon = inbuilt_attr_icons_voices[j].icon;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
filetype_count++;
|
filetype_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -452,7 +476,7 @@ static void read_builtin_types_init(void)
|
||||||
static void read_config_init(int fd)
|
static void read_config_init(int fd)
|
||||||
{
|
{
|
||||||
char line[64], *s, *e;
|
char line[64], *s, *e;
|
||||||
char *extension, *plugin;
|
const char *extension, *plugin;
|
||||||
/* config file is in the format
|
/* config file is in the format
|
||||||
<extension>,<plugin>,<icon code>
|
<extension>,<plugin>,<icon code>
|
||||||
ignore line if either of the first two are missing */
|
ignore line if either of the first two are missing */
|
||||||
|
@ -484,12 +508,7 @@ static void read_config_init(int fd)
|
||||||
{
|
{
|
||||||
/* get the icon */
|
/* get the icon */
|
||||||
s = e+1;
|
s = e+1;
|
||||||
if (*s == '*')
|
parse_icon(s, &unknown_file.icon);
|
||||||
unknown_file.icon = atoi(s+1);
|
|
||||||
else if (*s == '-')
|
|
||||||
unknown_file.icon = Icon_NOICON;
|
|
||||||
else if (*s >= '0' && *s <= '9')
|
|
||||||
unknown_file.icon = Icon_Last_Themeable + atoi(s);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -502,25 +521,22 @@ static void read_config_init(int fd)
|
||||||
highest_attr++;
|
highest_attr++;
|
||||||
/* get the icon */
|
/* get the icon */
|
||||||
s = e+1;
|
s = e+1;
|
||||||
if (*s == '*')
|
parse_icon(s, &file_type->icon);
|
||||||
file_type->icon = atoi(s+1);
|
|
||||||
else if (*s == '-')
|
|
||||||
file_type->icon = Icon_NOICON;
|
|
||||||
else if (*s >= '0' && *s <= '9')
|
|
||||||
file_type->icon = Icon_Last_Themeable + atoi(s);
|
|
||||||
filetype_count++;
|
filetype_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int filetype_get_attr(const char* file)
|
static int file_find_extension(const char* file)
|
||||||
{
|
{
|
||||||
char *extension = strrchr(file, '.');
|
char *extension = strrchr(file, '.');
|
||||||
int i;
|
if (extension)
|
||||||
if (!extension)
|
|
||||||
return 0;
|
|
||||||
extension++;
|
extension++;
|
||||||
|
return find_extension(extension);
|
||||||
|
}
|
||||||
|
|
||||||
i = find_extension(extension);
|
int filetype_get_attr(const char* file)
|
||||||
|
{
|
||||||
|
int i = file_find_extension(file);
|
||||||
if (i >= 0)
|
if (i >= 0)
|
||||||
return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
|
return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -543,19 +559,12 @@ static int find_attr(int attr)
|
||||||
#ifdef HAVE_LCD_COLOR
|
#ifdef HAVE_LCD_COLOR
|
||||||
int filetype_get_color(const char * name, int attr)
|
int filetype_get_color(const char * name, int attr)
|
||||||
{
|
{
|
||||||
char *extension;
|
|
||||||
int i;
|
|
||||||
if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
|
if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
|
||||||
return custom_colors[0];
|
return custom_colors[0];
|
||||||
extension = strrchr(name, '.');
|
int i = file_find_extension(name);
|
||||||
if (!extension)
|
if (i <= 0)
|
||||||
return unknown_file.color;
|
return unknown_file.color;
|
||||||
extension++;
|
|
||||||
|
|
||||||
i = find_extension(extension);
|
|
||||||
if (i >= 0)
|
|
||||||
return custom_colors[i];
|
return custom_colors[i];
|
||||||
return unknown_file.color;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -700,7 +709,7 @@ int filetype_load_plugin(const char* plugin, const char* file)
|
||||||
char plugin_name[MAX_PATH];
|
char plugin_name[MAX_PATH];
|
||||||
char *s;
|
char *s;
|
||||||
|
|
||||||
for (i=0;i<filetype_count;i++)
|
for (i=1;i<filetype_count;i++)
|
||||||
{
|
{
|
||||||
if (filetypes[i].plugin)
|
if (filetypes[i].plugin)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue