1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 Jonathan Gordon
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
35 #include "filetypes.h"
44 /* max filetypes (plugins & icons stored here) */
45 #if CONFIG_CODEC == SWCODEC
46 #define MAX_FILETYPES 128
48 #define MAX_FILETYPES 48
51 /* a table for the know file types */
52 static const struct filetype inbuilt_filetypes
[] = {
53 { "mp3", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
54 { "mp2", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
55 { "mpa", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
56 #if CONFIG_CODEC == SWCODEC
57 /* Temporary hack to allow playlist creation */
58 { "mp1", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
59 { "ogg", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
60 { "oga", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
61 { "wma", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
62 { "wmv", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
63 { "asf", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
64 { "wav", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
65 { "flac",FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
66 { "ac3", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
67 { "a52", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
68 { "mpc", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
69 { "wv", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
70 { "m4a", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
71 { "m4b", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
72 { "mp4", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
73 { "mod", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
74 { "shn", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
75 { "aif", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
76 { "aiff",FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
77 { "spx" ,FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
78 { "sid", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
79 { "adx", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
80 { "nsf", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
81 { "nsfe",FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
82 { "spc", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
83 { "ape", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
84 { "mac", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
85 { "sap" ,FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
86 { "rm", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
87 { "ra", FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
88 { "rmvb",FILE_ATTR_AUDIO
, Icon_Audio
, VOICE_EXT_MPA
},
90 { "m3u", FILE_ATTR_M3U
, Icon_Playlist
, LANG_PLAYLIST
},
91 { "m3u8",FILE_ATTR_M3U
, Icon_Playlist
, LANG_PLAYLIST
},
92 { "cfg", FILE_ATTR_CFG
, Icon_Config
, VOICE_EXT_CFG
},
93 { "wps", FILE_ATTR_WPS
, Icon_Wps
, VOICE_EXT_WPS
},
94 #ifdef HAVE_REMOTE_LCD
95 { "rwps",FILE_ATTR_RWPS
, Icon_Wps
, VOICE_EXT_RWPS
},
98 { "bmp", FILE_ATTR_BMP
, Icon_Wps
, VOICE_EXT_WPS
},
101 { "fmr", FILE_ATTR_FMR
, Icon_Preset
, LANG_FMR
},
103 { "lng", FILE_ATTR_LNG
, Icon_Language
, LANG_LANGUAGE
},
104 { "rock",FILE_ATTR_ROCK
,Icon_Plugin
, VOICE_EXT_ROCK
},
105 #ifdef HAVE_LCD_BITMAP
106 { "fnt", FILE_ATTR_FONT
,Icon_Font
, VOICE_EXT_FONT
},
107 { "kbd", FILE_ATTR_KBD
, Icon_Keyboard
, VOICE_EXT_KBD
},
109 { "bmark",FILE_ATTR_BMARK
, Icon_Bookmark
, VOICE_EXT_BMARK
},
110 { "cue", FILE_ATTR_CUE
, Icon_Bookmark
, VOICE_EXT_CUESHEET
},
111 #ifdef HAVE_LCD_BITMAP
112 { "sb", FILE_ATTR_SB
, Icon_Wps
, VOICE_EXT_WPS
},
114 #ifdef HAVE_REMOTE_LCD
115 { "rsb", FILE_ATTR_RSB
, Icon_Wps
, VOICE_EXT_WPS
},
118 { BOOTFILE_EXT
, FILE_ATTR_MOD
, Icon_Firmware
, VOICE_EXT_AJZ
},
122 void tree_get_filetypes(const struct filetype
** types
, int* count
)
124 *types
= inbuilt_filetypes
;
125 *count
= sizeof(inbuilt_filetypes
) / sizeof(*inbuilt_filetypes
);
128 /* mask for dynamic filetype info in attribute */
129 #define FILETYPES_MASK 0xFF00
130 #define ROCK_EXTENSION "rock"
133 int icon
; /* the icon which shall be used for it, NOICON if unknown */
134 unsigned char attr
; /* FILETYPES_MASK >> 8 */
135 char* plugin
; /* Which plugin to use, NULL if unknown, or builtin */
136 char* extension
; /* NULL for none */
138 static struct file_type filetypes
[MAX_FILETYPES
];
139 static int custom_filetype_icons
[MAX_FILETYPES
];
140 static bool custom_icons_loaded
= false;
141 #ifdef HAVE_LCD_COLOR
142 static int custom_colors
[MAX_FILETYPES
+1];
144 static int filetype_count
= 0;
145 static unsigned char highest_attr
= 0;
147 static char *filetypes_strdup(char* string
)
149 char *buffer
= (char*)buffer_alloc(strlen(string
)+1);
150 strcpy(buffer
, string
);
153 static void read_builtin_types(void);
154 static void read_config(char* config_file
);
155 #ifdef HAVE_LCD_COLOR
156 /* Colors file format is similar to icons:
158 * load a colors file from a theme with:
159 * filetype colours: filename.colours */
160 void read_color_theme_file(void) {
161 char buffer
[MAX_PATH
];
165 for (i
= 0; i
< MAX_FILETYPES
+1; i
++) {
166 custom_colors
[i
] = -1;
168 snprintf(buffer
, MAX_PATH
, "%s/%s.colours", THEME_DIR
,
169 global_settings
.colors_file
);
170 fd
= open(buffer
, O_RDONLY
);
173 while (read_line(fd
, buffer
, MAX_PATH
) > 0)
175 if (!settings_parseline(buffer
, &ext
, &color
))
177 if (!strcasecmp(ext
, "folder"))
179 hex_to_rgb(color
, &custom_colors
[0]);
182 if (!strcasecmp(ext
, "???"))
184 hex_to_rgb(color
, &custom_colors
[MAX_FILETYPES
]);
187 for (i
=1; i
<filetype_count
; i
++)
189 if (filetypes
[i
].extension
&&
190 !strcasecmp(ext
, filetypes
[i
].extension
))
192 hex_to_rgb(color
, &custom_colors
[i
]);
200 #ifdef HAVE_LCD_BITMAP
201 void read_viewer_theme_file(void)
203 char buffer
[MAX_PATH
];
207 global_status
.viewer_icon_count
= 0;
208 custom_icons_loaded
= false;
209 custom_filetype_icons
[0] = Icon_Folder
;
210 for (i
=1; i
<filetype_count
; i
++)
212 custom_filetype_icons
[i
] = filetypes
[i
].icon
;
215 snprintf(buffer
, MAX_PATH
, "%s/%s.icons", ICON_DIR
,
216 global_settings
.viewers_icon_file
);
217 fd
= open(buffer
, O_RDONLY
);
220 while (read_line(fd
, buffer
, MAX_PATH
) > 0)
222 if (!settings_parseline(buffer
, &ext
, &icon
))
224 for (i
=0; i
<filetype_count
; i
++)
226 if (filetypes
[i
].extension
&&
227 !strcasecmp(ext
, filetypes
[i
].extension
))
230 custom_filetype_icons
[i
] = atoi(icon
+1);
231 else if (*icon
== '-')
232 custom_filetype_icons
[i
] = Icon_NOICON
;
233 else if (*icon
>= '0' && *icon
<= '9')
235 int number
= atoi(icon
);
236 if (number
> global_status
.viewer_icon_count
)
237 global_status
.viewer_icon_count
++;
238 custom_filetype_icons
[i
] = Icon_Last_Themeable
+ number
;
245 custom_icons_loaded
= true;
249 void filetype_init(void)
251 /* set the directory item first */
252 filetypes
[0].extension
= NULL
;
253 filetypes
[0].plugin
= NULL
;
254 filetypes
[0].attr
= 0;
255 filetypes
[0].icon
= Icon_Folder
;
258 read_builtin_types();
259 read_config(VIEWERS_CONFIG
);
260 #ifdef HAVE_LCD_BITMAP
261 read_viewer_theme_file();
263 #ifdef HAVE_LCD_COLOR
264 read_color_theme_file();
268 /* remove all white spaces from string */
269 static void rm_whitespaces(char* str
)
284 static void read_builtin_types(void)
286 int count
= sizeof(inbuilt_filetypes
)/sizeof(*inbuilt_filetypes
), i
;
287 for(i
=0; i
<count
&& (filetype_count
< MAX_FILETYPES
); i
++)
289 filetypes
[filetype_count
].extension
= inbuilt_filetypes
[i
].extension
;
290 filetypes
[filetype_count
].plugin
= NULL
;
291 filetypes
[filetype_count
].attr
= inbuilt_filetypes
[i
].tree_attr
>>8;
292 if (filetypes
[filetype_count
].attr
> highest_attr
)
293 highest_attr
= filetypes
[filetype_count
].attr
;
294 filetypes
[filetype_count
].icon
= inbuilt_filetypes
[i
].icon
;
299 static void read_config(char* config_file
)
301 char line
[64], *s
, *e
;
302 char extension
[8], plugin
[32];
303 int fd
= open(config_file
, O_RDONLY
);
306 /* config file is in the for
307 <extension>,<plugin>,<icon code>
308 ignore line if either of the first two are missing */
309 while (read_line(fd
, line
, 64) > 0)
311 if (filetype_count
>= MAX_FILETYPES
)
313 splash(HZ
, ID2P(LANG_FILETYPES_FULL
));
316 rm_whitespaces(line
);
317 /* get the extention */
323 strcpy(extension
, s
);
333 /* ok, store this plugin/extension, check icon after */
334 filetypes
[filetype_count
].extension
= filetypes_strdup(extension
);
335 filetypes
[filetype_count
].plugin
= filetypes_strdup(plugin
);
336 filetypes
[filetype_count
].attr
= highest_attr
+1;
337 filetypes
[filetype_count
].icon
= Icon_Questionmark
;
342 filetypes
[filetype_count
].icon
= atoi(s
+1);
344 filetypes
[filetype_count
].icon
= Icon_NOICON
;
345 else if (*s
>= '0' && *s
<= '9')
346 filetypes
[filetype_count
].icon
= Icon_Last_Themeable
+ atoi(s
);
351 int filetype_get_attr(const char* file
)
353 char *extension
= strrchr(file
, '.');
358 for (i
=0; i
<filetype_count
; i
++)
360 if (filetypes
[i
].extension
&&
361 !strcasecmp(extension
, filetypes
[i
].extension
))
362 return (filetypes
[i
].attr
<<8)&FILE_ATTR_MASK
;
367 static int find_attr(int attr
)
370 /* skip the directory item */
371 if ((attr
& ATTR_DIRECTORY
)==ATTR_DIRECTORY
)
373 for (i
=1; i
<filetype_count
; i
++)
375 if ((attr
>>8) == filetypes
[i
].attr
)
381 #ifdef HAVE_LCD_COLOR
382 int filetype_get_color(const char * name
, int attr
)
386 if ((attr
& ATTR_DIRECTORY
)==ATTR_DIRECTORY
)
387 return custom_colors
[0];
388 extension
= strrchr(name
, '.');
390 return custom_colors
[MAX_FILETYPES
];
393 for (i
=1; i
<filetype_count
; i
++)
395 if (filetypes
[i
].extension
&&
396 !strcasecmp(extension
, filetypes
[i
].extension
))
397 return custom_colors
[i
];
399 return custom_colors
[MAX_FILETYPES
];
403 int filetype_get_icon(int attr
)
405 int index
= find_attr(attr
);
408 if (custom_icons_loaded
)
409 return custom_filetype_icons
[index
];
410 return filetypes
[index
].icon
;
413 char* filetype_get_plugin(const struct entry
* file
)
415 static char plugin_name
[MAX_PATH
];
416 int index
= find_attr(file
->attr
);
419 if (filetypes
[index
].plugin
== NULL
)
421 snprintf(plugin_name
, MAX_PATH
, "%s/%s.%s",
422 PLUGIN_DIR
, filetypes
[index
].plugin
, ROCK_EXTENSION
);
426 bool filetype_supported(int attr
)
428 return find_attr(attr
) >= 0;
431 /**** Open With Screen ****/
434 const char *current_file
;
437 static enum themable_icons
openwith_get_icon(int selected_item
, void * data
)
439 struct cb_data
*info
= (struct cb_data
*)data
;
440 int *items
= info
->items
;
441 return filetypes
[items
[selected_item
]].icon
;
444 static const char* openwith_get_name(int selected_item
, void * data
,
445 char * buffer
, size_t buffer_len
)
447 (void)buffer
; (void)buffer_len
;
448 struct cb_data
*info
= (struct cb_data
*)data
;
449 int *items
= info
->items
;
450 const char *s
= strrchr(filetypes
[items
[selected_item
]].plugin
, '/');
453 else return filetypes
[items
[selected_item
]].plugin
;
456 static int openwith_action_callback(int action
, struct gui_synclist
*lists
)
458 struct cb_data
*info
= (struct cb_data
*)lists
->data
;
459 int *items
= info
->items
;
461 if (action
== ACTION_STD_OK
)
463 char plugin
[MAX_PATH
];
464 i
= items
[gui_synclist_get_sel_pos(lists
)];
465 snprintf(plugin
, MAX_PATH
, "%s/%s.%s",
466 PLUGIN_DIR
, filetypes
[i
].plugin
, ROCK_EXTENSION
);
467 plugin_load(plugin
, info
->current_file
);
468 return ACTION_STD_CANCEL
;
473 int filetype_list_viewers(const char* current_file
)
476 int items
[MAX_FILETYPES
];
477 struct simplelist_info info
;
478 struct cb_data data
= { items
, current_file
};
479 for (i
=0; i
<filetype_count
&& count
< MAX_FILETYPES
; i
++)
481 if (filetypes
[i
].plugin
)
484 for (j
=0;j
<count
;j
++) /* check if the plugin is in the list yet */
486 if (!strcmp(filetypes
[i
].plugin
,filetypes
[items
[j
]].plugin
))
490 continue; /* it is so grab the next plugin */
494 #ifndef HAVE_LCD_BITMAP
497 /* FIX: translation! */
498 splash(HZ
*2, "No viewers found");
502 simplelist_info_init(&info
, str(LANG_ONPLAY_OPEN_WITH
), count
, &data
);
503 info
.action_callback
= openwith_action_callback
;
504 info
.get_name
= openwith_get_name
;
505 info
.get_icon
= openwith_get_icon
;
506 return simplelist_show_list(&info
);
509 int filetype_load_plugin(const char* plugin
, char* file
)
512 char plugin_name
[MAX_PATH
];
515 for (i
=0;i
<filetype_count
;i
++)
517 if (filetypes
[i
].plugin
)
519 s
= strrchr(filetypes
[i
].plugin
, '/');
522 if (!strcmp(s
+1, plugin
))
525 else if (!strcmp(filetypes
[i
].plugin
, plugin
))
529 if (i
>= filetype_count
)
531 snprintf(plugin_name
, MAX_PATH
, "%s/%s.%s",
532 PLUGIN_DIR
, filetypes
[i
].plugin
, ROCK_EXTENSION
);
533 return plugin_load(plugin_name
, file
);