Fix a dircache NULL-pointer dereference.
[kugel-rb.git] / apps / filetypes.c
blob49ae4afde29246be7b52d2a32b07164659854572
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * $Id$
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 ****************************************************************************/
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include "string.h"
27 #include <ctype.h>
29 #include "sprintf.h"
30 #include "settings.h"
31 #include "debug.h"
32 #include "lang.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 128
47 #else
48 #define MAX_FILETYPES 64
49 #endif
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 },
89 { "cmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
90 { "cm3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
91 { "cmr", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
92 { "cms", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
93 { "dmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
94 { "dlt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
95 { "mpt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
96 { "mpd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
97 { "rmt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
98 { "tmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
99 { "tm8", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
100 { "tm2", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
101 { "oma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
102 { "aa3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
103 { "at3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
104 #endif
105 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
106 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
107 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
108 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
109 #ifdef HAVE_REMOTE_LCD
110 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
111 #endif
112 #if CONFIG_TUNER
113 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
114 #endif
115 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
116 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
117 { "lua", FILE_ATTR_LUA, Icon_Plugin, VOICE_EXT_ROCK },
118 #ifdef HAVE_LCD_BITMAP
119 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
120 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
121 #endif
122 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
123 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
124 #ifdef HAVE_LCD_BITMAP
125 { "sbs", FILE_ATTR_SBS, Icon_Wps, VOICE_EXT_SBS },
126 #endif
127 #ifdef HAVE_REMOTE_LCD
128 { "rsbs", FILE_ATTR_RSBS, Icon_Wps, VOICE_EXT_RSBS },
129 #endif
130 #ifdef BOOTFILE_EXT
131 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
132 #endif
133 #ifdef BOOTFILE_EXT2
134 { BOOTFILE_EXT2, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
135 #endif
138 void tree_get_filetypes(const struct filetype** types, int* count)
140 *types = inbuilt_filetypes;
141 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
144 /* mask for dynamic filetype info in attribute */
145 #define ROCK_EXTENSION "rock"
147 struct file_type {
148 int icon; /* the icon which shall be used for it, NOICON if unknown */
149 unsigned char attr; /* FILE_ATTR_MASK >> 8 */
150 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
151 char* extension; /* NULL for none */
153 static struct file_type filetypes[MAX_FILETYPES];
154 static int custom_filetype_icons[MAX_FILETYPES];
155 static bool custom_icons_loaded = false;
156 #ifdef HAVE_LCD_COLOR
157 static int custom_colors[MAX_FILETYPES+1];
158 #endif
159 static int filetype_count = 0;
160 static unsigned char highest_attr = 0;
162 static char *filetypes_strdup(char* string)
164 char *buffer = (char*)buffer_alloc(strlen(string)+1);
165 strcpy(buffer, string);
166 return buffer;
168 static void read_builtin_types(void);
169 static void read_config(char* config_file);
170 #ifdef HAVE_LCD_COLOR
171 /* Colors file format is similar to icons:
172 * ext:hex_color
173 * load a colors file from a theme with:
174 * filetype colours: filename.colours */
175 void read_color_theme_file(void) {
176 char buffer[MAX_PATH];
177 int fd;
178 char *ext, *color;
179 int i;
180 for (i = 0; i < MAX_FILETYPES+1; i++) {
181 custom_colors[i] = -1;
183 snprintf(buffer, MAX_PATH, "%s/%s.colours", THEME_DIR,
184 global_settings.colors_file);
185 fd = open(buffer, O_RDONLY);
186 if (fd < 0)
187 return;
188 while (read_line(fd, buffer, MAX_PATH) > 0)
190 if (!settings_parseline(buffer, &ext, &color))
191 continue;
192 if (!strcasecmp(ext, "folder"))
194 hex_to_rgb(color, &custom_colors[0]);
195 continue;
197 if (!strcasecmp(ext, "???"))
199 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
200 continue;
202 for (i=1; i<filetype_count; i++)
204 if (filetypes[i].extension &&
205 !strcasecmp(ext, filetypes[i].extension))
207 hex_to_rgb(color, &custom_colors[i]);
208 break;
212 close(fd);
214 #endif
215 #ifdef HAVE_LCD_BITMAP
216 void read_viewer_theme_file(void)
218 char buffer[MAX_PATH];
219 int fd;
220 char *ext, *icon;
221 int i;
222 global_status.viewer_icon_count = 0;
223 custom_icons_loaded = false;
224 custom_filetype_icons[0] = Icon_Folder;
225 for (i=1; i<filetype_count; i++)
227 custom_filetype_icons[i] = filetypes[i].icon;
230 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
231 global_settings.viewers_icon_file);
232 fd = open(buffer, O_RDONLY);
233 if (fd < 0)
234 return;
235 while (read_line(fd, buffer, MAX_PATH) > 0)
237 if (!settings_parseline(buffer, &ext, &icon))
238 continue;
239 for (i=0; i<filetype_count; i++)
241 if (filetypes[i].extension &&
242 !strcasecmp(ext, filetypes[i].extension))
244 if (*icon == '*')
245 custom_filetype_icons[i] = atoi(icon+1);
246 else if (*icon == '-')
247 custom_filetype_icons[i] = Icon_NOICON;
248 else if (*icon >= '0' && *icon <= '9')
250 int number = atoi(icon);
251 if (number > global_status.viewer_icon_count)
252 global_status.viewer_icon_count++;
253 custom_filetype_icons[i] = Icon_Last_Themeable + number;
255 break;
259 close(fd);
260 custom_icons_loaded = true;
262 #endif
264 void filetype_init(void)
266 /* set the directory item first */
267 filetypes[0].extension = NULL;
268 filetypes[0].plugin = NULL;
269 filetypes[0].attr = 0;
270 filetypes[0].icon = Icon_Folder;
272 filetype_count = 1;
273 read_builtin_types();
274 read_config(VIEWERS_CONFIG);
275 #ifdef HAVE_LCD_BITMAP
276 read_viewer_theme_file();
277 #endif
278 #ifdef HAVE_LCD_COLOR
279 read_color_theme_file();
280 #endif
283 /* remove all white spaces from string */
284 static void rm_whitespaces(char* str)
286 char *s = str;
287 while (*str)
289 if (!isspace(*str))
291 *s = *str;
292 s++;
294 str++;
296 *s = '\0';
299 static void read_builtin_types(void)
301 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
302 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
304 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
305 filetypes[filetype_count].plugin = NULL;
306 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
307 if (filetypes[filetype_count].attr > highest_attr)
308 highest_attr = filetypes[filetype_count].attr;
309 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
310 filetype_count++;
314 static void read_config(char* config_file)
316 char line[64], *s, *e;
317 char extension[8], plugin[32];
318 int fd = open(config_file, O_RDONLY);
319 if (fd < 0)
320 return;
321 /* config file is in the for
322 <extension>,<plugin>,<icon code>
323 ignore line if either of the first two are missing */
324 while (read_line(fd, line, 64) > 0)
326 if (filetype_count >= MAX_FILETYPES)
328 splash(HZ, ID2P(LANG_FILETYPES_FULL));
329 break;
331 rm_whitespaces(line);
332 /* get the extention */
333 s = line;
334 e = strchr(s, ',');
335 if (!e)
336 continue;
337 *e = '\0';
338 strcpy(extension, s);
340 /* get the plugin */
341 s = e+1;
342 e = strchr(s, ',');
343 if (!e)
344 continue;
345 *e = '\0';
347 strcpy(plugin, s);
348 /* ok, store this plugin/extension, check icon after */
349 filetypes[filetype_count].extension = filetypes_strdup(extension);
350 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
351 filetypes[filetype_count].attr = highest_attr +1;
352 filetypes[filetype_count].icon = Icon_Questionmark;
353 highest_attr++;
354 /* get the icon */
355 s = e+1;
356 if (*s == '*')
357 filetypes[filetype_count].icon = atoi(s+1);
358 else if (*s == '-')
359 filetypes[filetype_count].icon = Icon_NOICON;
360 else if (*s >= '0' && *s <= '9')
361 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
362 filetype_count++;
366 int filetype_get_attr(const char* file)
368 char *extension = strrchr(file, '.');
369 int i;
370 if (!extension)
371 return 0;
372 extension++;
373 for (i=0; i<filetype_count; i++)
375 if (filetypes[i].extension &&
376 !strcasecmp(extension, filetypes[i].extension))
377 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
379 return 0;
382 static int find_attr(int attr)
384 int i;
385 /* skip the directory item */
386 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
387 return 0;
388 for (i=1; i<filetype_count; i++)
390 if ((attr>>8) == filetypes[i].attr)
391 return i;
393 return -1;
396 #ifdef HAVE_LCD_COLOR
397 int filetype_get_color(const char * name, int attr)
399 char *extension;
400 int i;
401 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
402 return custom_colors[0];
403 extension = strrchr(name, '.');
404 if (!extension)
405 return custom_colors[MAX_FILETYPES];
406 extension++;
408 for (i=1; i<filetype_count; i++)
410 if (filetypes[i].extension &&
411 !strcasecmp(extension, filetypes[i].extension))
412 return custom_colors[i];
414 return custom_colors[MAX_FILETYPES];
416 #endif
418 int filetype_get_icon(int attr)
420 int index = find_attr(attr);
421 if (index < 0)
422 return Icon_NOICON;
423 if (custom_icons_loaded)
424 return custom_filetype_icons[index];
425 return filetypes[index].icon;
428 char* filetype_get_plugin(const struct entry* file)
430 static char plugin_name[MAX_PATH];
431 int index = find_attr(file->attr);
432 if (index < 0)
433 return NULL;
434 if (filetypes[index].plugin == NULL)
435 return NULL;
436 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
437 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
438 return plugin_name;
441 bool filetype_supported(int attr)
443 return find_attr(attr) >= 0;
446 /**** Open With Screen ****/
447 struct cb_data {
448 int *items;
449 const char *current_file;
452 static enum themable_icons openwith_get_icon(int selected_item, void * data)
454 struct cb_data *info = (struct cb_data *)data;
455 int *items = info->items;
456 return filetypes[items[selected_item]].icon;
459 static const char* openwith_get_name(int selected_item, void * data,
460 char * buffer, size_t buffer_len)
462 (void)buffer; (void)buffer_len;
463 struct cb_data *info = (struct cb_data *)data;
464 int *items = info->items;
465 const char *s = strrchr(filetypes[items[selected_item]].plugin, '/');
466 if (s)
467 return s+1;
468 else return filetypes[items[selected_item]].plugin;
471 static int openwith_action_callback(int action, struct gui_synclist *lists)
473 struct cb_data *info = (struct cb_data *)lists->data;
474 int *items = info->items;
475 int i;
476 if (action == ACTION_STD_OK)
478 char plugin[MAX_PATH];
479 i = items[gui_synclist_get_sel_pos(lists)];
480 snprintf(plugin, MAX_PATH, "%s/%s.%s",
481 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
482 plugin_load(plugin, info->current_file);
483 return ACTION_STD_CANCEL;
485 return action;
488 int filetype_list_viewers(const char* current_file)
490 int i, count = 0;
491 int items[MAX_FILETYPES];
492 struct simplelist_info info;
493 struct cb_data data = { items, current_file };
494 for (i=0; i<filetype_count && count < MAX_FILETYPES; i++)
496 if (filetypes[i].plugin)
498 int j;
499 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
501 if (!strcmp(filetypes[i].plugin,filetypes[items[j]].plugin))
502 break;
504 if (j<count)
505 continue; /* it is so grab the next plugin */
506 items[count++] = i;
509 #ifndef HAVE_LCD_BITMAP
510 if (count == 0)
512 /* FIX: translation! */
513 splash(HZ*2, "No viewers found");
514 return PLUGIN_OK;
516 #endif
517 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), count, &data);
518 info.action_callback = openwith_action_callback;
519 info.get_name = openwith_get_name;
520 info.get_icon = global_settings.show_icons?openwith_get_icon:NULL;
521 return simplelist_show_list(&info);
524 int filetype_load_plugin(const char* plugin, char* file)
526 int i;
527 char plugin_name[MAX_PATH];
528 char *s;
530 for (i=0;i<filetype_count;i++)
532 if (filetypes[i].plugin)
534 s = strrchr(filetypes[i].plugin, '/');
535 if (s)
537 if (!strcmp(s+1, plugin))
538 break;
540 else if (!strcmp(filetypes[i].plugin, plugin))
541 break;
544 if (i >= filetype_count)
545 return PLUGIN_ERROR;
546 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
547 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
548 return plugin_load(plugin_name, file);