fix a warning.
[Rockbox.git] / apps / filetypes.c
blob9cf23c2b00c47f9e8d18bede97298f0cfd58b13c
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 72
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 { "wav", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
62 { "flac",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
63 { "ac3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
64 { "a52", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
65 { "mpc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
66 { "wv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
67 { "m4a", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
68 { "m4b", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
69 { "mp4", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
70 { "shn", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
71 { "aif", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
72 { "aiff",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
73 { "spx" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
74 { "sid", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
75 { "adx", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
76 { "nsf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
77 { "nsfe",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
78 { "spc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
79 { "ape", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
80 { "mac", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
81 #endif
82 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
83 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
84 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
85 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
86 #ifdef HAVE_REMOTE_LCD
87 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
88 #endif
89 #if LCD_DEPTH > 1
90 { "bmp", FILE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
91 #endif
92 #if CONFIG_TUNER
93 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
94 #endif
95 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
96 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
97 #ifdef HAVE_LCD_BITMAP
98 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
99 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
100 #endif
101 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
102 { "cue", FILE_ATTR_CUE, Icon_Bookmark, LANG_CUESHEET },
103 #ifdef BOOTFILE_EXT
104 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
105 #endif /* #ifndef SIMULATOR */
108 void tree_get_filetypes(const struct filetype** types, int* count)
110 *types = inbuilt_filetypes;
111 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
114 /* mask for dynamic filetype info in attribute */
115 #define FILETYPES_MASK 0xFF00
116 #define ROCK_EXTENSION "rock"
118 struct file_type {
119 int icon; /* the icon which shall be used for it, NOICON if unknown */
120 bool viewer; /* true if the rock is in viewers, false if in rocks */
121 unsigned char attr; /* FILETYPES_MASK >> 8 */
122 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
123 char* extension; /* NULL for none */
125 static struct file_type filetypes[MAX_FILETYPES];
126 static int custom_filetype_icons[MAX_FILETYPES];
127 static bool custom_icons_loaded = false;
128 #ifdef HAVE_LCD_COLOR
129 static int custom_colors[MAX_FILETYPES+1];
130 #endif
131 static int filetype_count = 0;
132 static unsigned char heighest_attr = 0;
134 static char *filetypes_strdup(char* string)
136 char *buffer = (char*)buffer_alloc(strlen(string)+1);
137 strcpy(buffer, string);
138 return buffer;
140 static void read_builtin_types(void);
141 static void read_config(char* config_file);
142 #ifdef HAVE_LCD_COLOR
143 /* Colors file format is similar to icons:
144 * ext:hex_color
145 * load a colors file from a theme with:
146 * filetype colours: filename.colours */
147 void read_color_theme_file(void) {
148 char buffer[MAX_PATH];
149 int fd;
150 char *ext, *color;
151 int i;
152 for (i = 0; i < MAX_FILETYPES+1; i++) {
153 custom_colors[i] = -1;
155 snprintf(buffer, MAX_PATH, "%s/%s.colours", THEME_DIR,
156 global_settings.colors_file);
157 fd = open(buffer, O_RDONLY);
158 if (fd < 0)
159 return;
160 while (read_line(fd, buffer, MAX_PATH) > 0)
162 if (!settings_parseline(buffer, &ext, &color))
163 continue;
164 if (!strcasecmp(ext, "folder"))
166 custom_colors[0] = hex_to_rgb(color);
167 continue;
169 if (!strcasecmp(ext, "???"))
171 custom_colors[MAX_FILETYPES] = hex_to_rgb(color);
172 continue;
174 for (i=1; i<filetype_count; i++)
176 if (filetypes[i].extension &&
177 !strcasecmp(ext, filetypes[i].extension))
179 custom_colors[i] = hex_to_rgb(color);
180 break;
184 close(fd);
186 #endif
187 #ifdef HAVE_LCD_BITMAP
188 void read_viewer_theme_file(void)
190 char buffer[MAX_PATH];
191 int fd;
192 char *ext, *icon;
193 int i;
194 global_status.viewer_icon_count = 0;
195 custom_icons_loaded = false;
196 custom_filetype_icons[0] = Icon_Folder;
197 for (i=1; i<filetype_count; i++)
199 custom_filetype_icons[i] = filetypes[i].icon;
202 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
203 global_settings.viewers_icon_file);
204 fd = open(buffer, O_RDONLY);
205 if (fd < 0)
206 return;
207 while (read_line(fd, buffer, MAX_PATH) > 0)
209 if (!settings_parseline(buffer, &ext, &icon))
210 continue;
211 for (i=0; i<filetype_count; i++)
213 if (filetypes[i].extension &&
214 !strcasecmp(ext, filetypes[i].extension))
216 if (*icon == '*')
217 custom_filetype_icons[i] = atoi(icon+1);
218 else if (*icon == '-')
219 custom_filetype_icons[i] = Icon_NOICON;
220 else if (*icon >= '0' && *icon <= '9')
222 int number = atoi(icon);
223 if (number > global_status.viewer_icon_count)
224 global_status.viewer_icon_count++;
225 custom_filetype_icons[i] = Icon_Last_Themeable + number;
227 break;
231 close(fd);
232 custom_icons_loaded = true;
234 #endif
236 void filetype_init(void)
238 /* set the directory item first */
239 filetypes[0].extension = NULL;
240 filetypes[0].plugin = NULL;
241 filetypes[0].attr = 0;
242 filetypes[0].icon = Icon_Folder;
244 filetype_count = 1;
245 read_builtin_types();
246 read_config(VIEWERS_CONFIG);
247 #ifdef HAVE_LCD_BITMAP
248 read_viewer_theme_file();
249 #endif
250 #ifdef HAVE_LCD_COLOR
251 read_color_theme_file();
252 #endif
255 /* remove all white spaces from string */
256 static void rm_whitespaces(char* str)
258 char *s = str;
259 while (*str)
261 if (!isspace(*str))
263 *s = *str;
264 s++;
266 str++;
268 *s = '\0';
271 static void read_builtin_types(void)
273 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
274 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
276 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
277 filetypes[filetype_count].plugin = NULL;
278 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
279 if (filetypes[filetype_count].attr > heighest_attr)
280 heighest_attr = filetypes[filetype_count].attr;
281 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
282 filetype_count++;
286 static void read_config(char* config_file)
288 char line[64], *s, *e;
289 char extension[8], plugin[32];
290 bool viewer;
291 int fd = open(config_file, O_RDONLY);
292 if (fd < 0)
293 return;
294 /* config file is in the for
295 <extension>,<plugin>,<icon code>
296 ignore line if either of the first two are missing */
297 while (read_line(fd, line, 64) > 0)
299 if (filetype_count >= MAX_FILETYPES)
301 gui_syncsplash(HZ, str(LANG_FILETYPES_FULL));
302 break;
304 rm_whitespaces(line);
305 /* get the extention */
306 s = line;
307 e = strchr(s, ',');
308 if (!e)
309 continue;
310 *e = '\0';
311 strcpy(extension, s);
313 /* get the plugin */
314 s = e+1;
315 e = strchr(s, '/');
316 if (!e)
317 continue;
318 *e = '\0';
319 if (!strcasecmp("viewers", s))
320 viewer = true;
321 else
322 viewer = false;
323 s = e+1;
324 e = strchr(s, ',');
325 if (!e)
326 continue;
327 *e = '\0';
328 strcpy(plugin, s);
329 /* ok, store this plugin/extension, check icon after */
330 filetypes[filetype_count].extension = filetypes_strdup(extension);
331 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
332 filetypes[filetype_count].viewer = viewer;
333 filetypes[filetype_count].attr = heighest_attr +1;
334 filetypes[filetype_count].icon = Icon_Questionmark;
335 heighest_attr++;
336 /* get the icon */
337 #ifdef HAVE_LCD_BITMAP
338 s = e+1;
339 if (*s == '*')
340 filetypes[filetype_count].icon = atoi(s+1);
341 else if (*s == '-')
342 filetypes[filetype_count].icon = Icon_NOICON;
343 else if (*s >= '0' && *s <= '9')
344 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
345 #else
346 filetypes[filetype_count].icon = Icon_NOICON;
347 #endif
348 filetype_count++;
352 int filetype_get_attr(const char* file)
354 char *extension = strrchr(file, '.');
355 int i;
356 if (!extension)
357 return 0;
358 extension++;
359 for (i=0; i<filetype_count; i++)
361 if (filetypes[i].extension &&
362 !strcasecmp(extension, filetypes[i].extension))
363 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
365 return 0;
368 static int find_attr(int attr)
370 int i;
371 /* skip the directory item */
372 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
373 return 0;
374 for (i=1; i<filetype_count; i++)
376 if ((attr>>8) == filetypes[i].attr)
377 return i;
379 return -1;
382 #ifdef HAVE_LCD_COLOR
383 int filetype_get_color(const char * name, int attr)
385 char *extension;
386 int i;
387 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
388 return custom_colors[0];
389 extension = strrchr(name, '.');
390 if (!extension)
391 return custom_colors[MAX_FILETYPES];
392 extension++;
393 logf("%s %s",name,extension);
394 for (i=1; i<filetype_count; i++)
396 if (filetypes[i].extension &&
397 !strcasecmp(extension, filetypes[i].extension))
398 return custom_colors[i];
400 return custom_colors[MAX_FILETYPES];
402 #endif
404 int filetype_get_icon(int attr)
406 int index = find_attr(attr);
407 if (index < 0)
408 return Icon_NOICON;
409 if (custom_icons_loaded)
410 return custom_filetype_icons[index];
411 return filetypes[index].icon;
414 char* filetype_get_plugin(const struct entry* file)
416 static char plugin_name[MAX_PATH];
417 int index = find_attr(file->attr);
418 if (index < 0)
419 return NULL;
420 if (filetypes[index].plugin == NULL)
421 return NULL;
422 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
423 filetypes[index].viewer? VIEWERS_DIR: PLUGIN_DIR,
424 filetypes[index].plugin, ROCK_EXTENSION);
425 return plugin_name;
428 bool filetype_supported(int attr)
430 return find_attr(attr) >= 0;
433 int filetype_list_viewers(const char* current_file)
435 int i, count = 0;
436 char *strings[MAX_FILETYPES/2];
437 struct menu_callback_with_desc cb_and_desc =
438 { NULL, ID2P(LANG_ONPLAY_OPEN_WITH), Icon_Plugin };
439 struct menu_item_ex menu;
441 for (i=0; i<filetype_count && count < (MAX_FILETYPES/2); i++)
443 if (filetypes[i].plugin)
445 int j;
446 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
448 if (!strcmp(strings[j], filetypes[i].plugin))
449 break;
451 if (j<count)
452 continue; /* it is so grab the next plugin */
453 strings[count] = filetypes[i].plugin;
454 count++;
457 #ifndef HAVE_LCD_BITMAP
458 if (count == 0)
460 /* FIX: translation! */
461 gui_syncsplash(HZ*2, (unsigned char *)"No viewers found");
462 return PLUGIN_OK;
464 #endif
465 menu.flags = MT_RETURN_ID|MENU_HAS_DESC|MENU_ITEM_COUNT(count);
466 menu.strings = (const char**)strings;
467 menu.callback_and_desc = &cb_and_desc;
468 i = do_menu(&menu, NULL);
469 if (i >= 0)
470 return filetype_load_plugin(strings[i], (void*)current_file);
471 return i;
474 int filetype_load_plugin(const char* plugin, char* file)
476 int fd;
477 char plugin_name[MAX_PATH];
478 snprintf(plugin_name, sizeof(plugin_name), "%s/%s.%s",
479 VIEWERS_DIR, plugin, ROCK_EXTENSION);
480 if ((fd = open(plugin_name,O_RDONLY))>=0)
482 close(fd);
483 return plugin_load(plugin_name,file);
485 else
487 snprintf(plugin_name, sizeof(plugin_name), "%s/%s.%s",
488 PLUGIN_DIR, plugin, ROCK_EXTENSION);
489 if ((fd = open(plugin_name,O_RDONLY))>=0)
491 close(fd);
492 return plugin_load(plugin_name,file);
495 return PLUGIN_ERROR;