fix red.
[kugel-rb/myfork.git] / apps / filetypes.c
blobffa7161693cdaad7f7c5ec95ffb718a4aa7fa0d6
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 48
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 #endif
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 },
96 #endif
97 #if LCD_DEPTH > 1
98 { "bmp", FILE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
99 #endif
100 #if CONFIG_TUNER
101 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
102 #endif
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 },
108 #endif
109 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
110 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
111 #ifdef BOOTFILE_EXT
112 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
113 #endif
116 void tree_get_filetypes(const struct filetype** types, int* count)
118 *types = inbuilt_filetypes;
119 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
122 /* mask for dynamic filetype info in attribute */
123 #define FILETYPES_MASK 0xFF00
124 #define ROCK_EXTENSION "rock"
126 struct file_type {
127 int icon; /* the icon which shall be used for it, NOICON if unknown */
128 unsigned char attr; /* FILETYPES_MASK >> 8 */
129 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
130 char* extension; /* NULL for none */
132 static struct file_type filetypes[MAX_FILETYPES];
133 static int custom_filetype_icons[MAX_FILETYPES];
134 static bool custom_icons_loaded = false;
135 #ifdef HAVE_LCD_COLOR
136 static int custom_colors[MAX_FILETYPES+1];
137 #endif
138 static int filetype_count = 0;
139 static unsigned char highest_attr = 0;
141 static char *filetypes_strdup(char* string)
143 char *buffer = (char*)buffer_alloc(strlen(string)+1);
144 strcpy(buffer, string);
145 return buffer;
147 static void read_builtin_types(void);
148 static void read_config(char* config_file);
149 #ifdef HAVE_LCD_COLOR
150 /* Colors file format is similar to icons:
151 * ext:hex_color
152 * load a colors file from a theme with:
153 * filetype colours: filename.colours */
154 void read_color_theme_file(void) {
155 char buffer[MAX_PATH];
156 int fd;
157 char *ext, *color;
158 int i;
159 for (i = 0; i < MAX_FILETYPES+1; i++) {
160 custom_colors[i] = -1;
162 snprintf(buffer, MAX_PATH, "%s/%s.colours", THEME_DIR,
163 global_settings.colors_file);
164 fd = open(buffer, O_RDONLY);
165 if (fd < 0)
166 return;
167 while (read_line(fd, buffer, MAX_PATH) > 0)
169 if (!settings_parseline(buffer, &ext, &color))
170 continue;
171 if (!strcasecmp(ext, "folder"))
173 hex_to_rgb(color, &custom_colors[0]);
174 continue;
176 if (!strcasecmp(ext, "???"))
178 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
179 continue;
181 for (i=1; i<filetype_count; i++)
183 if (filetypes[i].extension &&
184 !strcasecmp(ext, filetypes[i].extension))
186 hex_to_rgb(color, &custom_colors[i]);
187 break;
191 close(fd);
193 #endif
194 #ifdef HAVE_LCD_BITMAP
195 void read_viewer_theme_file(void)
197 char buffer[MAX_PATH];
198 int fd;
199 char *ext, *icon;
200 int i;
201 global_status.viewer_icon_count = 0;
202 custom_icons_loaded = false;
203 custom_filetype_icons[0] = Icon_Folder;
204 for (i=1; i<filetype_count; i++)
206 custom_filetype_icons[i] = filetypes[i].icon;
209 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
210 global_settings.viewers_icon_file);
211 fd = open(buffer, O_RDONLY);
212 if (fd < 0)
213 return;
214 while (read_line(fd, buffer, MAX_PATH) > 0)
216 if (!settings_parseline(buffer, &ext, &icon))
217 continue;
218 for (i=0; i<filetype_count; i++)
220 if (filetypes[i].extension &&
221 !strcasecmp(ext, filetypes[i].extension))
223 if (*icon == '*')
224 custom_filetype_icons[i] = atoi(icon+1);
225 else if (*icon == '-')
226 custom_filetype_icons[i] = Icon_NOICON;
227 else if (*icon >= '0' && *icon <= '9')
229 int number = atoi(icon);
230 if (number > global_status.viewer_icon_count)
231 global_status.viewer_icon_count++;
232 custom_filetype_icons[i] = Icon_Last_Themeable + number;
234 break;
238 close(fd);
239 custom_icons_loaded = true;
241 #endif
243 void filetype_init(void)
245 /* set the directory item first */
246 filetypes[0].extension = NULL;
247 filetypes[0].plugin = NULL;
248 filetypes[0].attr = 0;
249 filetypes[0].icon = Icon_Folder;
251 filetype_count = 1;
252 read_builtin_types();
253 read_config(VIEWERS_CONFIG);
254 #ifdef HAVE_LCD_BITMAP
255 read_viewer_theme_file();
256 #endif
257 #ifdef HAVE_LCD_COLOR
258 read_color_theme_file();
259 #endif
262 /* remove all white spaces from string */
263 static void rm_whitespaces(char* str)
265 char *s = str;
266 while (*str)
268 if (!isspace(*str))
270 *s = *str;
271 s++;
273 str++;
275 *s = '\0';
278 static void read_builtin_types(void)
280 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
281 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
283 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
284 filetypes[filetype_count].plugin = NULL;
285 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
286 if (filetypes[filetype_count].attr > highest_attr)
287 highest_attr = filetypes[filetype_count].attr;
288 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
289 filetype_count++;
293 static void read_config(char* config_file)
295 char line[64], *s, *e;
296 char extension[8], plugin[32];
297 int fd = open(config_file, O_RDONLY);
298 if (fd < 0)
299 return;
300 /* config file is in the for
301 <extension>,<plugin>,<icon code>
302 ignore line if either of the first two are missing */
303 while (read_line(fd, line, 64) > 0)
305 if (filetype_count >= MAX_FILETYPES)
307 splash(HZ, ID2P(LANG_FILETYPES_FULL));
308 break;
310 rm_whitespaces(line);
311 /* get the extention */
312 s = line;
313 e = strchr(s, ',');
314 if (!e)
315 continue;
316 *e = '\0';
317 strcpy(extension, s);
319 /* get the plugin */
320 s = e+1;
321 e = strchr(s, ',');
322 if (!e)
323 continue;
324 *e = '\0';
326 strcpy(plugin, s);
327 /* ok, store this plugin/extension, check icon after */
328 filetypes[filetype_count].extension = filetypes_strdup(extension);
329 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
330 filetypes[filetype_count].attr = highest_attr +1;
331 filetypes[filetype_count].icon = Icon_Questionmark;
332 highest_attr++;
333 /* get the icon */
334 s = e+1;
335 if (*s == '*')
336 filetypes[filetype_count].icon = atoi(s+1);
337 else if (*s == '-')
338 filetypes[filetype_count].icon = Icon_NOICON;
339 else if (*s >= '0' && *s <= '9')
340 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
341 filetype_count++;
345 int filetype_get_attr(const char* file)
347 char *extension = strrchr(file, '.');
348 int i;
349 if (!extension)
350 return 0;
351 extension++;
352 for (i=0; i<filetype_count; i++)
354 if (filetypes[i].extension &&
355 !strcasecmp(extension, filetypes[i].extension))
356 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
358 return 0;
361 static int find_attr(int attr)
363 int i;
364 /* skip the directory item */
365 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
366 return 0;
367 for (i=1; i<filetype_count; i++)
369 if ((attr>>8) == filetypes[i].attr)
370 return i;
372 return -1;
375 #ifdef HAVE_LCD_COLOR
376 int filetype_get_color(const char * name, int attr)
378 char *extension;
379 int i;
380 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
381 return custom_colors[0];
382 extension = strrchr(name, '.');
383 if (!extension)
384 return custom_colors[MAX_FILETYPES];
385 extension++;
387 for (i=1; i<filetype_count; i++)
389 if (filetypes[i].extension &&
390 !strcasecmp(extension, filetypes[i].extension))
391 return custom_colors[i];
393 return custom_colors[MAX_FILETYPES];
395 #endif
397 int filetype_get_icon(int attr)
399 int index = find_attr(attr);
400 if (index < 0)
401 return Icon_NOICON;
402 if (custom_icons_loaded)
403 return custom_filetype_icons[index];
404 return filetypes[index].icon;
407 char* filetype_get_plugin(const struct entry* file)
409 static char plugin_name[MAX_PATH];
410 int index = find_attr(file->attr);
411 if (index < 0)
412 return NULL;
413 if (filetypes[index].plugin == NULL)
414 return NULL;
415 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
416 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
417 return plugin_name;
420 bool filetype_supported(int attr)
422 return find_attr(attr) >= 0;
425 /**** Open With Screen ****/
426 struct cb_data {
427 int *items;
428 const char *current_file;
431 static enum themable_icons openwith_get_icon(int selected_item, void * data)
433 struct cb_data *info = (struct cb_data *)data;
434 int *items = info->items;
435 return filetypes[items[selected_item]].icon;
438 static const char* openwith_get_name(int selected_item, void * data,
439 char * buffer, size_t buffer_len)
441 (void)buffer; (void)buffer_len;
442 struct cb_data *info = (struct cb_data *)data;
443 int *items = info->items;
444 const char *s = strrchr(filetypes[items[selected_item]].plugin, '/');
445 if (s)
446 return s+1;
447 else return filetypes[items[selected_item]].plugin;
450 static int openwith_action_callback(int action, struct gui_synclist *lists)
452 struct cb_data *info = (struct cb_data *)lists->data;
453 int *items = info->items;
454 int i;
455 if (action == ACTION_STD_OK)
457 char plugin[MAX_PATH];
458 i = items[gui_synclist_get_sel_pos(lists)];
459 snprintf(plugin, MAX_PATH, "%s/%s.%s",
460 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
461 plugin_load(plugin, info->current_file);
462 return ACTION_STD_CANCEL;
464 return action;
467 int filetype_list_viewers(const char* current_file)
469 int i, count = 0;
470 int items[MAX_FILETYPES];
471 struct simplelist_info info;
472 struct cb_data data = { items, current_file };
473 for (i=0; i<filetype_count && count < MAX_FILETYPES; i++)
475 if (filetypes[i].plugin)
477 int j;
478 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
480 if (!strcmp(filetypes[i].plugin,filetypes[items[j]].plugin))
481 break;
483 if (j<count)
484 continue; /* it is so grab the next plugin */
485 items[count++] = i;
488 #ifndef HAVE_LCD_BITMAP
489 if (count == 0)
491 /* FIX: translation! */
492 splash(HZ*2, "No viewers found");
493 return PLUGIN_OK;
495 #endif
496 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), count, &data);
497 info.action_callback = openwith_action_callback;
498 info.get_name = openwith_get_name;
499 info.get_icon = openwith_get_icon;
500 return simplelist_show_list(&info);
503 int filetype_load_plugin(const char* plugin, char* file)
505 int i;
506 char plugin_name[MAX_PATH];
507 char *s;
509 for (i=0;i<filetype_count;i++)
511 if (filetypes[i].plugin)
513 s = strrchr(filetypes[i].plugin, '/');
514 if (s)
516 if (!strcmp(s+1, plugin))
517 break;
519 else if (!strcmp(filetypes[i].plugin, plugin))
520 break;
523 if (i >= filetype_count)
524 return PLUGIN_ERROR;
525 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
526 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
527 return plugin_load(plugin_name, file);