Oops...that wasn't suppose to get left there. :P
[Rockbox.git] / apps / filetypes.c
blob314d9a0aeed134ef36c9da2e11e36a2b7642d5c6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * $Id$
10 * Copyright (C) 2007 Jonathan Gordon
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include "string.h"
25 #include "atoi.h"
26 #include <ctype.h>
28 #include "sprintf.h"
29 #include "settings.h"
30 #include "debug.h"
31 #include "lang.h"
32 #include "language.h"
33 #include "kernel.h"
34 #include "plugin.h"
35 #include "filetypes.h"
36 #include "screens.h"
37 #include "dir.h"
38 #include "file.h"
39 #include "splash.h"
40 #include "buffer.h"
41 #include "icons.h"
42 #include "logf.h"
44 /* max filetypes (plugins & icons stored here) */
45 #if CONFIG_CODEC == SWCODEC
46 #define MAX_FILETYPES 73
47 #else
48 #define MAX_FILETYPES 48
49 #endif
51 /* a table for the know file types */
52 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 { "wma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
61 { "wmv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
62 { "asf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
63 { "wav", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
64 { "flac",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
65 { "ac3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
66 { "a52", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
67 { "mpc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
68 { "wv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
69 { "m4a", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
70 { "m4b", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
71 { "mp4", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
72 { "shn", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
73 { "aif", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
74 { "aiff",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
75 { "spx" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
76 { "sid", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
77 { "adx", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
78 { "nsf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
79 { "nsfe",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
80 { "spc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
81 { "ape", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
82 { "mac", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
83 #endif
84 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
85 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
86 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
87 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
88 #ifdef HAVE_REMOTE_LCD
89 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
90 #endif
91 #if LCD_DEPTH > 1
92 { "bmp", FILE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
93 #endif
94 #if CONFIG_TUNER
95 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
96 #endif
97 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
98 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
99 #ifdef HAVE_LCD_BITMAP
100 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
101 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
102 #endif
103 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
104 { "cue", FILE_ATTR_CUE, Icon_Bookmark, LANG_CUESHEET },
105 #ifdef BOOTFILE_EXT
106 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
107 #endif /* #ifndef SIMULATOR */
110 void tree_get_filetypes(const struct filetype** types, int* count)
112 *types = inbuilt_filetypes;
113 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
116 /* mask for dynamic filetype info in attribute */
117 #define FILETYPES_MASK 0xFF00
118 #define ROCK_EXTENSION "rock"
120 struct file_type {
121 int icon; /* the icon which shall be used for it, NOICON if unknown */
122 bool viewer; /* true if the rock is in viewers, false if in rocks */
123 unsigned char attr; /* FILETYPES_MASK >> 8 */
124 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
125 char* extension; /* NULL for none */
127 static struct file_type filetypes[MAX_FILETYPES];
128 static int custom_filetype_icons[MAX_FILETYPES];
129 static bool custom_icons_loaded = false;
130 #ifdef HAVE_LCD_COLOR
131 static int custom_colors[MAX_FILETYPES+1];
132 #endif
133 static int filetype_count = 0;
134 static unsigned char heighest_attr = 0;
136 static char *filetypes_strdup(char* string)
138 char *buffer = (char*)buffer_alloc(strlen(string)+1);
139 strcpy(buffer, string);
140 return buffer;
142 static void read_builtin_types(void);
143 static void read_config(char* config_file);
144 #ifdef HAVE_LCD_COLOR
145 /* Colors file format is similar to icons:
146 * ext:hex_color
147 * load a colors file from a theme with:
148 * filetype colours: filename.colours */
149 void read_color_theme_file(void) {
150 char buffer[MAX_PATH];
151 int fd;
152 char *ext, *color;
153 int i;
154 for (i = 0; i < MAX_FILETYPES+1; i++) {
155 custom_colors[i] = -1;
157 snprintf(buffer, MAX_PATH, "%s/%s.colours", THEME_DIR,
158 global_settings.colors_file);
159 fd = open(buffer, O_RDONLY);
160 if (fd < 0)
161 return;
162 while (read_line(fd, buffer, MAX_PATH) > 0)
164 if (!settings_parseline(buffer, &ext, &color))
165 continue;
166 if (!strcasecmp(ext, "folder"))
168 custom_colors[0] = hex_to_rgb(color);
169 continue;
171 if (!strcasecmp(ext, "???"))
173 custom_colors[MAX_FILETYPES] = hex_to_rgb(color);
174 continue;
176 for (i=1; i<filetype_count; i++)
178 if (filetypes[i].extension &&
179 !strcasecmp(ext, filetypes[i].extension))
181 custom_colors[i] = hex_to_rgb(color);
182 break;
186 close(fd);
188 #endif
189 #ifdef HAVE_LCD_BITMAP
190 void read_viewer_theme_file(void)
192 char buffer[MAX_PATH];
193 int fd;
194 char *ext, *icon;
195 int i;
196 global_status.viewer_icon_count = 0;
197 custom_icons_loaded = false;
198 custom_filetype_icons[0] = Icon_Folder;
199 for (i=1; i<filetype_count; i++)
201 custom_filetype_icons[i] = filetypes[i].icon;
204 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
205 global_settings.viewers_icon_file);
206 fd = open(buffer, O_RDONLY);
207 if (fd < 0)
208 return;
209 while (read_line(fd, buffer, MAX_PATH) > 0)
211 if (!settings_parseline(buffer, &ext, &icon))
212 continue;
213 for (i=0; i<filetype_count; i++)
215 if (filetypes[i].extension &&
216 !strcasecmp(ext, filetypes[i].extension))
218 if (*icon == '*')
219 custom_filetype_icons[i] = atoi(icon+1);
220 else if (*icon == '-')
221 custom_filetype_icons[i] = Icon_NOICON;
222 else if (*icon >= '0' && *icon <= '9')
224 int number = atoi(icon);
225 if (number > global_status.viewer_icon_count)
226 global_status.viewer_icon_count++;
227 custom_filetype_icons[i] = Icon_Last_Themeable + number;
229 break;
233 close(fd);
234 custom_icons_loaded = true;
236 #endif
238 void filetype_init(void)
240 /* set the directory item first */
241 filetypes[0].extension = NULL;
242 filetypes[0].plugin = NULL;
243 filetypes[0].attr = 0;
244 filetypes[0].icon = Icon_Folder;
246 filetype_count = 1;
247 read_builtin_types();
248 read_config(VIEWERS_CONFIG);
249 #ifdef HAVE_LCD_BITMAP
250 read_viewer_theme_file();
251 #endif
252 #ifdef HAVE_LCD_COLOR
253 read_color_theme_file();
254 #endif
257 /* remove all white spaces from string */
258 static void rm_whitespaces(char* str)
260 char *s = str;
261 while (*str)
263 if (!isspace(*str))
265 *s = *str;
266 s++;
268 str++;
270 *s = '\0';
273 static void read_builtin_types(void)
275 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
276 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
278 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
279 filetypes[filetype_count].plugin = NULL;
280 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
281 if (filetypes[filetype_count].attr > heighest_attr)
282 heighest_attr = filetypes[filetype_count].attr;
283 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
284 filetype_count++;
288 static void read_config(char* config_file)
290 char line[64], *s, *e;
291 char extension[8], plugin[32];
292 bool viewer;
293 int fd = open(config_file, O_RDONLY);
294 if (fd < 0)
295 return;
296 /* config file is in the for
297 <extension>,<plugin>,<icon code>
298 ignore line if either of the first two are missing */
299 while (read_line(fd, line, 64) > 0)
301 if (filetype_count >= MAX_FILETYPES)
303 gui_syncsplash(HZ, str(LANG_FILETYPES_FULL));
304 break;
306 rm_whitespaces(line);
307 /* get the extention */
308 s = line;
309 e = strchr(s, ',');
310 if (!e)
311 continue;
312 *e = '\0';
313 strcpy(extension, s);
315 /* get the plugin */
316 s = e+1;
317 e = strchr(s, '/');
318 if (!e)
319 continue;
320 *e = '\0';
321 if (!strcasecmp("viewers", s))
322 viewer = true;
323 else
324 viewer = false;
325 s = e+1;
326 e = strchr(s, ',');
327 if (!e)
328 continue;
329 *e = '\0';
330 strcpy(plugin, s);
331 /* ok, store this plugin/extension, check icon after */
332 filetypes[filetype_count].extension = filetypes_strdup(extension);
333 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
334 filetypes[filetype_count].viewer = viewer;
335 filetypes[filetype_count].attr = heighest_attr +1;
336 filetypes[filetype_count].icon = Icon_Questionmark;
337 heighest_attr++;
338 /* get the icon */
339 #ifdef HAVE_LCD_BITMAP
340 s = e+1;
341 if (*s == '*')
342 filetypes[filetype_count].icon = atoi(s+1);
343 else if (*s == '-')
344 filetypes[filetype_count].icon = Icon_NOICON;
345 else if (*s >= '0' && *s <= '9')
346 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
347 #else
348 filetypes[filetype_count].icon = Icon_NOICON;
349 #endif
350 filetype_count++;
354 int filetype_get_attr(const char* file)
356 char *extension = strrchr(file, '.');
357 int i;
358 if (!extension)
359 return 0;
360 extension++;
361 for (i=0; i<filetype_count; i++)
363 if (filetypes[i].extension &&
364 !strcasecmp(extension, filetypes[i].extension))
365 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
367 return 0;
370 static int find_attr(int attr)
372 int i;
373 /* skip the directory item */
374 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
375 return 0;
376 for (i=1; i<filetype_count; i++)
378 if ((attr>>8) == filetypes[i].attr)
379 return i;
381 return -1;
384 #ifdef HAVE_LCD_COLOR
385 int filetype_get_color(const char * name, int attr)
387 char *extension;
388 int i;
389 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
390 return custom_colors[0];
391 extension = strrchr(name, '.');
392 if (!extension)
393 return custom_colors[MAX_FILETYPES];
394 extension++;
395 logf("%s %s",name,extension);
396 for (i=1; i<filetype_count; i++)
398 if (filetypes[i].extension &&
399 !strcasecmp(extension, filetypes[i].extension))
400 return custom_colors[i];
402 return custom_colors[MAX_FILETYPES];
404 #endif
406 int filetype_get_icon(int attr)
408 int index = find_attr(attr);
409 if (index < 0)
410 return Icon_NOICON;
411 if (custom_icons_loaded)
412 return custom_filetype_icons[index];
413 return filetypes[index].icon;
416 char* filetype_get_plugin(const struct entry* file)
418 static char plugin_name[MAX_PATH];
419 int index = find_attr(file->attr);
420 if (index < 0)
421 return NULL;
422 if (filetypes[index].plugin == NULL)
423 return NULL;
424 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
425 filetypes[index].viewer? VIEWERS_DIR: PLUGIN_DIR,
426 filetypes[index].plugin, ROCK_EXTENSION);
427 return plugin_name;
430 bool filetype_supported(int attr)
432 return find_attr(attr) >= 0;
435 int filetype_list_viewers(const char* current_file)
437 int i, count = 0;
438 char *strings[MAX_FILETYPES/2];
439 struct menu_callback_with_desc cb_and_desc =
440 { NULL, ID2P(LANG_ONPLAY_OPEN_WITH), Icon_Plugin };
441 struct menu_item_ex menu;
443 for (i=0; i<filetype_count && count < (MAX_FILETYPES/2); i++)
445 if (filetypes[i].plugin)
447 int j;
448 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
450 if (!strcmp(strings[j], filetypes[i].plugin))
451 break;
453 if (j<count)
454 continue; /* it is so grab the next plugin */
455 strings[count] = filetypes[i].plugin;
456 count++;
459 #ifndef HAVE_LCD_BITMAP
460 if (count == 0)
462 /* FIX: translation! */
463 gui_syncsplash(HZ*2, (unsigned char *)"No viewers found");
464 return PLUGIN_OK;
466 #endif
467 menu.flags = MT_RETURN_ID|MENU_HAS_DESC|MENU_ITEM_COUNT(count);
468 menu.strings = (const char**)strings;
469 menu.callback_and_desc = &cb_and_desc;
470 i = do_menu(&menu, NULL);
471 if (i >= 0)
472 return filetype_load_plugin(strings[i], (void*)current_file);
473 return i;
476 int filetype_load_plugin(const char* plugin, char* file)
478 int fd;
479 char plugin_name[MAX_PATH];
480 snprintf(plugin_name, sizeof(plugin_name), "%s/%s.%s",
481 VIEWERS_DIR, plugin, ROCK_EXTENSION);
482 if ((fd = open(plugin_name,O_RDONLY))>=0)
484 close(fd);
485 return plugin_load(plugin_name,file);
487 else
489 snprintf(plugin_name, sizeof(plugin_name), "%s/%s.%s",
490 PLUGIN_DIR, plugin, ROCK_EXTENSION);
491 if ((fd = open(plugin_name,O_RDONLY))>=0)
493 close(fd);
494 return plugin_load(plugin_name,file);
497 return PLUGIN_ERROR;