Modify the ffmpeg source files in apps/codecs/libwmapro in order to compile the codec...
[kugel-rb.git] / apps / filetypes.c
blob4aefef1f0d6d361a1f984c406c80cc5dae44899c
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 { "mmf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
105 { "au", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
106 { "snd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
107 { "vox", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
108 { "w64", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
109 #endif
110 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
111 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
112 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
113 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
114 #ifdef HAVE_REMOTE_LCD
115 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
116 #endif
117 #if CONFIG_TUNER
118 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
119 #endif
120 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
121 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
122 { "lua", FILE_ATTR_LUA, Icon_Plugin, VOICE_EXT_ROCK },
123 #ifdef HAVE_LCD_BITMAP
124 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
125 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
126 #endif
127 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
128 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
129 #ifdef HAVE_LCD_BITMAP
130 { "sbs", FILE_ATTR_SBS, Icon_Wps, VOICE_EXT_SBS },
131 #endif
132 #ifdef HAVE_REMOTE_LCD
133 { "rsbs", FILE_ATTR_RSBS, Icon_Wps, VOICE_EXT_RSBS },
134 #endif
135 #ifdef BOOTFILE_EXT
136 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
137 #endif
138 #ifdef BOOTFILE_EXT2
139 { BOOTFILE_EXT2, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
140 #endif
143 void tree_get_filetypes(const struct filetype** types, int* count)
145 *types = inbuilt_filetypes;
146 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
149 /* mask for dynamic filetype info in attribute */
150 #define ROCK_EXTENSION "rock"
152 struct file_type {
153 int icon; /* the icon which shall be used for it, NOICON if unknown */
154 unsigned char attr; /* FILE_ATTR_MASK >> 8 */
155 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
156 char* extension; /* NULL for none */
158 static struct file_type filetypes[MAX_FILETYPES];
159 static int custom_filetype_icons[MAX_FILETYPES];
160 static bool custom_icons_loaded = false;
161 #ifdef HAVE_LCD_COLOR
162 static int custom_colors[MAX_FILETYPES+1];
163 #endif
164 static int filetype_count = 0;
165 static unsigned char highest_attr = 0;
167 static char *filetypes_strdup(char* string)
169 char *buffer = (char*)buffer_alloc(strlen(string)+1);
170 strcpy(buffer, string);
171 return buffer;
173 static void read_builtin_types(void);
174 static void read_config(char* config_file);
175 #ifdef HAVE_LCD_COLOR
176 /* Colors file format is similar to icons:
177 * ext:hex_color
178 * load a colors file from a theme with:
179 * filetype colours: filename.colours */
180 void read_color_theme_file(void) {
181 char buffer[MAX_PATH];
182 int fd;
183 char *ext, *color;
184 int i;
185 for (i = 0; i < MAX_FILETYPES+1; i++) {
186 custom_colors[i] = -1;
188 snprintf(buffer, MAX_PATH, "%s/%s.colours", THEME_DIR,
189 global_settings.colors_file);
190 fd = open(buffer, O_RDONLY);
191 if (fd < 0)
192 return;
193 while (read_line(fd, buffer, MAX_PATH) > 0)
195 if (!settings_parseline(buffer, &ext, &color))
196 continue;
197 if (!strcasecmp(ext, "folder"))
199 hex_to_rgb(color, &custom_colors[0]);
200 continue;
202 if (!strcasecmp(ext, "???"))
204 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
205 continue;
207 for (i=1; i<filetype_count; i++)
209 if (filetypes[i].extension &&
210 !strcasecmp(ext, filetypes[i].extension))
212 hex_to_rgb(color, &custom_colors[i]);
213 break;
217 close(fd);
219 #endif
220 #ifdef HAVE_LCD_BITMAP
221 void read_viewer_theme_file(void)
223 char buffer[MAX_PATH];
224 int fd;
225 char *ext, *icon;
226 int i;
227 global_status.viewer_icon_count = 0;
228 custom_icons_loaded = false;
229 custom_filetype_icons[0] = Icon_Folder;
230 for (i=1; i<filetype_count; i++)
232 custom_filetype_icons[i] = filetypes[i].icon;
235 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
236 global_settings.viewers_icon_file);
237 fd = open(buffer, O_RDONLY);
238 if (fd < 0)
239 return;
240 while (read_line(fd, buffer, MAX_PATH) > 0)
242 if (!settings_parseline(buffer, &ext, &icon))
243 continue;
244 for (i=0; i<filetype_count; i++)
246 if (filetypes[i].extension &&
247 !strcasecmp(ext, filetypes[i].extension))
249 if (*icon == '*')
250 custom_filetype_icons[i] = atoi(icon+1);
251 else if (*icon == '-')
252 custom_filetype_icons[i] = Icon_NOICON;
253 else if (*icon >= '0' && *icon <= '9')
255 int number = atoi(icon);
256 if (number > global_status.viewer_icon_count)
257 global_status.viewer_icon_count++;
258 custom_filetype_icons[i] = Icon_Last_Themeable + number;
260 break;
264 close(fd);
265 custom_icons_loaded = true;
267 #endif
269 void filetype_init(void)
271 /* set the directory item first */
272 filetypes[0].extension = NULL;
273 filetypes[0].plugin = NULL;
274 filetypes[0].attr = 0;
275 filetypes[0].icon = Icon_Folder;
277 filetype_count = 1;
278 read_builtin_types();
279 read_config(VIEWERS_CONFIG);
280 #ifdef HAVE_LCD_BITMAP
281 read_viewer_theme_file();
282 #endif
283 #ifdef HAVE_LCD_COLOR
284 read_color_theme_file();
285 #endif
288 /* remove all white spaces from string */
289 static void rm_whitespaces(char* str)
291 char *s = str;
292 while (*str)
294 if (!isspace(*str))
296 *s = *str;
297 s++;
299 str++;
301 *s = '\0';
304 static void read_builtin_types(void)
306 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
307 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
309 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
310 filetypes[filetype_count].plugin = NULL;
311 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
312 if (filetypes[filetype_count].attr > highest_attr)
313 highest_attr = filetypes[filetype_count].attr;
314 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
315 filetype_count++;
319 static void read_config(char* config_file)
321 char line[64], *s, *e;
322 char extension[8], plugin[32];
323 int fd = open(config_file, O_RDONLY);
324 if (fd < 0)
325 return;
326 /* config file is in the for
327 <extension>,<plugin>,<icon code>
328 ignore line if either of the first two are missing */
329 while (read_line(fd, line, 64) > 0)
331 if (filetype_count >= MAX_FILETYPES)
333 splash(HZ, ID2P(LANG_FILETYPES_FULL));
334 break;
336 rm_whitespaces(line);
337 /* get the extention */
338 s = line;
339 e = strchr(s, ',');
340 if (!e)
341 continue;
342 *e = '\0';
343 strcpy(extension, s);
345 /* get the plugin */
346 s = e+1;
347 e = strchr(s, ',');
348 if (!e)
349 continue;
350 *e = '\0';
352 strcpy(plugin, s);
353 /* ok, store this plugin/extension, check icon after */
354 filetypes[filetype_count].extension = filetypes_strdup(extension);
355 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
356 filetypes[filetype_count].attr = highest_attr +1;
357 filetypes[filetype_count].icon = Icon_Questionmark;
358 highest_attr++;
359 /* get the icon */
360 s = e+1;
361 if (*s == '*')
362 filetypes[filetype_count].icon = atoi(s+1);
363 else if (*s == '-')
364 filetypes[filetype_count].icon = Icon_NOICON;
365 else if (*s >= '0' && *s <= '9')
366 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
367 filetype_count++;
371 int filetype_get_attr(const char* file)
373 char *extension = strrchr(file, '.');
374 int i;
375 if (!extension)
376 return 0;
377 extension++;
378 for (i=0; i<filetype_count; i++)
380 if (filetypes[i].extension &&
381 !strcasecmp(extension, filetypes[i].extension))
382 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
384 return 0;
387 static int find_attr(int attr)
389 int i;
390 /* skip the directory item */
391 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
392 return 0;
393 for (i=1; i<filetype_count; i++)
395 if ((attr>>8) == filetypes[i].attr)
396 return i;
398 return -1;
401 #ifdef HAVE_LCD_COLOR
402 int filetype_get_color(const char * name, int attr)
404 char *extension;
405 int i;
406 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
407 return custom_colors[0];
408 extension = strrchr(name, '.');
409 if (!extension)
410 return custom_colors[MAX_FILETYPES];
411 extension++;
413 for (i=1; i<filetype_count; i++)
415 if (filetypes[i].extension &&
416 !strcasecmp(extension, filetypes[i].extension))
417 return custom_colors[i];
419 return custom_colors[MAX_FILETYPES];
421 #endif
423 int filetype_get_icon(int attr)
425 int index = find_attr(attr);
426 if (index < 0)
427 return Icon_NOICON;
428 if (custom_icons_loaded)
429 return custom_filetype_icons[index];
430 return filetypes[index].icon;
433 char* filetype_get_plugin(const struct entry* file)
435 static char plugin_name[MAX_PATH];
436 int index = find_attr(file->attr);
437 if (index < 0)
438 return NULL;
439 if (filetypes[index].plugin == NULL)
440 return NULL;
441 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
442 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
443 return plugin_name;
446 bool filetype_supported(int attr)
448 return find_attr(attr) >= 0;
451 /**** Open With Screen ****/
452 struct cb_data {
453 int *items;
454 const char *current_file;
457 static enum themable_icons openwith_get_icon(int selected_item, void * data)
459 struct cb_data *info = (struct cb_data *)data;
460 int *items = info->items;
461 return filetypes[items[selected_item]].icon;
464 static const char* openwith_get_name(int selected_item, void * data,
465 char * buffer, size_t buffer_len)
467 (void)buffer; (void)buffer_len;
468 struct cb_data *info = (struct cb_data *)data;
469 int *items = info->items;
470 const char *s = strrchr(filetypes[items[selected_item]].plugin, '/');
471 if (s)
472 return s+1;
473 else return filetypes[items[selected_item]].plugin;
476 static int openwith_action_callback(int action, struct gui_synclist *lists)
478 struct cb_data *info = (struct cb_data *)lists->data;
479 int *items = info->items;
480 int i;
481 if (action == ACTION_STD_OK)
483 char plugin[MAX_PATH];
484 i = items[gui_synclist_get_sel_pos(lists)];
485 snprintf(plugin, MAX_PATH, "%s/%s.%s",
486 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
487 plugin_load(plugin, info->current_file);
488 return ACTION_STD_CANCEL;
490 return action;
493 int filetype_list_viewers(const char* current_file)
495 int i, count = 0;
496 int items[MAX_FILETYPES];
497 struct simplelist_info info;
498 struct cb_data data = { items, current_file };
499 for (i=0; i<filetype_count && count < MAX_FILETYPES; i++)
501 if (filetypes[i].plugin)
503 int j;
504 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
506 if (!strcmp(filetypes[i].plugin,filetypes[items[j]].plugin))
507 break;
509 if (j<count)
510 continue; /* it is so grab the next plugin */
511 items[count++] = i;
514 #ifndef HAVE_LCD_BITMAP
515 if (count == 0)
517 /* FIX: translation! */
518 splash(HZ*2, "No viewers found");
519 return PLUGIN_OK;
521 #endif
522 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), count, &data);
523 info.action_callback = openwith_action_callback;
524 info.get_name = openwith_get_name;
525 info.get_icon = global_settings.show_icons?openwith_get_icon:NULL;
526 return simplelist_show_list(&info);
529 int filetype_load_plugin(const char* plugin, char* file)
531 int i;
532 char plugin_name[MAX_PATH];
533 char *s;
535 for (i=0;i<filetype_count;i++)
537 if (filetypes[i].plugin)
539 s = strrchr(filetypes[i].plugin, '/');
540 if (s)
542 if (!strcmp(s+1, plugin))
543 break;
545 else if (!strcmp(filetypes[i].plugin, plugin))
546 break;
549 if (i >= filetype_count)
550 return PLUGIN_ERROR;
551 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
552 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
553 return plugin_load(plugin_name, file);