Fuze: Change the button to mark possibilities from a select+up combo to just select...
[kugel-rb.git] / apps / filetypes.c
blobed3d938fe9694b470af05a74c095b560fe975324
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
114 #ifdef BOOTFILE_EXT2
115 { BOOTFILE_EXT2, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
116 #endif
119 void tree_get_filetypes(const struct filetype** types, int* count)
121 *types = inbuilt_filetypes;
122 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
125 /* mask for dynamic filetype info in attribute */
126 #define FILETYPES_MASK 0xFF00
127 #define ROCK_EXTENSION "rock"
129 struct file_type {
130 int icon; /* the icon which shall be used for it, NOICON if unknown */
131 unsigned char attr; /* FILETYPES_MASK >> 8 */
132 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
133 char* extension; /* NULL for none */
135 static struct file_type filetypes[MAX_FILETYPES];
136 static int custom_filetype_icons[MAX_FILETYPES];
137 static bool custom_icons_loaded = false;
138 #ifdef HAVE_LCD_COLOR
139 static int custom_colors[MAX_FILETYPES+1];
140 #endif
141 static int filetype_count = 0;
142 static unsigned char highest_attr = 0;
144 static char *filetypes_strdup(char* string)
146 char *buffer = (char*)buffer_alloc(strlen(string)+1);
147 strcpy(buffer, string);
148 return buffer;
150 static void read_builtin_types(void);
151 static void read_config(char* config_file);
152 #ifdef HAVE_LCD_COLOR
153 /* Colors file format is similar to icons:
154 * ext:hex_color
155 * load a colors file from a theme with:
156 * filetype colours: filename.colours */
157 void read_color_theme_file(void) {
158 char buffer[MAX_PATH];
159 int fd;
160 char *ext, *color;
161 int i;
162 for (i = 0; i < MAX_FILETYPES+1; i++) {
163 custom_colors[i] = -1;
165 snprintf(buffer, MAX_PATH, "%s/%s.colours", THEME_DIR,
166 global_settings.colors_file);
167 fd = open(buffer, O_RDONLY);
168 if (fd < 0)
169 return;
170 while (read_line(fd, buffer, MAX_PATH) > 0)
172 if (!settings_parseline(buffer, &ext, &color))
173 continue;
174 if (!strcasecmp(ext, "folder"))
176 hex_to_rgb(color, &custom_colors[0]);
177 continue;
179 if (!strcasecmp(ext, "???"))
181 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
182 continue;
184 for (i=1; i<filetype_count; i++)
186 if (filetypes[i].extension &&
187 !strcasecmp(ext, filetypes[i].extension))
189 hex_to_rgb(color, &custom_colors[i]);
190 break;
194 close(fd);
196 #endif
197 #ifdef HAVE_LCD_BITMAP
198 void read_viewer_theme_file(void)
200 char buffer[MAX_PATH];
201 int fd;
202 char *ext, *icon;
203 int i;
204 global_status.viewer_icon_count = 0;
205 custom_icons_loaded = false;
206 custom_filetype_icons[0] = Icon_Folder;
207 for (i=1; i<filetype_count; i++)
209 custom_filetype_icons[i] = filetypes[i].icon;
212 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
213 global_settings.viewers_icon_file);
214 fd = open(buffer, O_RDONLY);
215 if (fd < 0)
216 return;
217 while (read_line(fd, buffer, MAX_PATH) > 0)
219 if (!settings_parseline(buffer, &ext, &icon))
220 continue;
221 for (i=0; i<filetype_count; i++)
223 if (filetypes[i].extension &&
224 !strcasecmp(ext, filetypes[i].extension))
226 if (*icon == '*')
227 custom_filetype_icons[i] = atoi(icon+1);
228 else if (*icon == '-')
229 custom_filetype_icons[i] = Icon_NOICON;
230 else if (*icon >= '0' && *icon <= '9')
232 int number = atoi(icon);
233 if (number > global_status.viewer_icon_count)
234 global_status.viewer_icon_count++;
235 custom_filetype_icons[i] = Icon_Last_Themeable + number;
237 break;
241 close(fd);
242 custom_icons_loaded = true;
244 #endif
246 void filetype_init(void)
248 /* set the directory item first */
249 filetypes[0].extension = NULL;
250 filetypes[0].plugin = NULL;
251 filetypes[0].attr = 0;
252 filetypes[0].icon = Icon_Folder;
254 filetype_count = 1;
255 read_builtin_types();
256 read_config(VIEWERS_CONFIG);
257 #ifdef HAVE_LCD_BITMAP
258 read_viewer_theme_file();
259 #endif
260 #ifdef HAVE_LCD_COLOR
261 read_color_theme_file();
262 #endif
265 /* remove all white spaces from string */
266 static void rm_whitespaces(char* str)
268 char *s = str;
269 while (*str)
271 if (!isspace(*str))
273 *s = *str;
274 s++;
276 str++;
278 *s = '\0';
281 static void read_builtin_types(void)
283 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
284 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
286 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
287 filetypes[filetype_count].plugin = NULL;
288 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
289 if (filetypes[filetype_count].attr > highest_attr)
290 highest_attr = filetypes[filetype_count].attr;
291 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
292 filetype_count++;
296 static void read_config(char* config_file)
298 char line[64], *s, *e;
299 char extension[8], plugin[32];
300 int fd = open(config_file, O_RDONLY);
301 if (fd < 0)
302 return;
303 /* config file is in the for
304 <extension>,<plugin>,<icon code>
305 ignore line if either of the first two are missing */
306 while (read_line(fd, line, 64) > 0)
308 if (filetype_count >= MAX_FILETYPES)
310 splash(HZ, ID2P(LANG_FILETYPES_FULL));
311 break;
313 rm_whitespaces(line);
314 /* get the extention */
315 s = line;
316 e = strchr(s, ',');
317 if (!e)
318 continue;
319 *e = '\0';
320 strcpy(extension, s);
322 /* get the plugin */
323 s = e+1;
324 e = strchr(s, ',');
325 if (!e)
326 continue;
327 *e = '\0';
329 strcpy(plugin, s);
330 /* ok, store this plugin/extension, check icon after */
331 filetypes[filetype_count].extension = filetypes_strdup(extension);
332 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
333 filetypes[filetype_count].attr = highest_attr +1;
334 filetypes[filetype_count].icon = Icon_Questionmark;
335 highest_attr++;
336 /* get the icon */
337 s = e+1;
338 if (*s == '*')
339 filetypes[filetype_count].icon = atoi(s+1);
340 else if (*s == '-')
341 filetypes[filetype_count].icon = Icon_NOICON;
342 else if (*s >= '0' && *s <= '9')
343 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
344 filetype_count++;
348 int filetype_get_attr(const char* file)
350 char *extension = strrchr(file, '.');
351 int i;
352 if (!extension)
353 return 0;
354 extension++;
355 for (i=0; i<filetype_count; i++)
357 if (filetypes[i].extension &&
358 !strcasecmp(extension, filetypes[i].extension))
359 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
361 return 0;
364 static int find_attr(int attr)
366 int i;
367 /* skip the directory item */
368 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
369 return 0;
370 for (i=1; i<filetype_count; i++)
372 if ((attr>>8) == filetypes[i].attr)
373 return i;
375 return -1;
378 #ifdef HAVE_LCD_COLOR
379 int filetype_get_color(const char * name, int attr)
381 char *extension;
382 int i;
383 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
384 return custom_colors[0];
385 extension = strrchr(name, '.');
386 if (!extension)
387 return custom_colors[MAX_FILETYPES];
388 extension++;
390 for (i=1; i<filetype_count; i++)
392 if (filetypes[i].extension &&
393 !strcasecmp(extension, filetypes[i].extension))
394 return custom_colors[i];
396 return custom_colors[MAX_FILETYPES];
398 #endif
400 int filetype_get_icon(int attr)
402 int index = find_attr(attr);
403 if (index < 0)
404 return Icon_NOICON;
405 if (custom_icons_loaded)
406 return custom_filetype_icons[index];
407 return filetypes[index].icon;
410 char* filetype_get_plugin(const struct entry* file)
412 static char plugin_name[MAX_PATH];
413 int index = find_attr(file->attr);
414 if (index < 0)
415 return NULL;
416 if (filetypes[index].plugin == NULL)
417 return NULL;
418 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
419 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
420 return plugin_name;
423 bool filetype_supported(int attr)
425 return find_attr(attr) >= 0;
428 /**** Open With Screen ****/
429 struct cb_data {
430 int *items;
431 const char *current_file;
434 static enum themable_icons openwith_get_icon(int selected_item, void * data)
436 struct cb_data *info = (struct cb_data *)data;
437 int *items = info->items;
438 return filetypes[items[selected_item]].icon;
441 static const char* openwith_get_name(int selected_item, void * data,
442 char * buffer, size_t buffer_len)
444 (void)buffer; (void)buffer_len;
445 struct cb_data *info = (struct cb_data *)data;
446 int *items = info->items;
447 const char *s = strrchr(filetypes[items[selected_item]].plugin, '/');
448 if (s)
449 return s+1;
450 else return filetypes[items[selected_item]].plugin;
453 static int openwith_action_callback(int action, struct gui_synclist *lists)
455 struct cb_data *info = (struct cb_data *)lists->data;
456 int *items = info->items;
457 int i;
458 if (action == ACTION_STD_OK)
460 char plugin[MAX_PATH];
461 i = items[gui_synclist_get_sel_pos(lists)];
462 snprintf(plugin, MAX_PATH, "%s/%s.%s",
463 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
464 plugin_load(plugin, info->current_file);
465 return ACTION_STD_CANCEL;
467 return action;
470 int filetype_list_viewers(const char* current_file)
472 int i, count = 0;
473 int items[MAX_FILETYPES];
474 struct simplelist_info info;
475 struct cb_data data = { items, current_file };
476 for (i=0; i<filetype_count && count < MAX_FILETYPES; i++)
478 if (filetypes[i].plugin)
480 int j;
481 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
483 if (!strcmp(filetypes[i].plugin,filetypes[items[j]].plugin))
484 break;
486 if (j<count)
487 continue; /* it is so grab the next plugin */
488 items[count++] = i;
491 #ifndef HAVE_LCD_BITMAP
492 if (count == 0)
494 /* FIX: translation! */
495 splash(HZ*2, "No viewers found");
496 return PLUGIN_OK;
498 #endif
499 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), count, &data);
500 info.action_callback = openwith_action_callback;
501 info.get_name = openwith_get_name;
502 info.get_icon = openwith_get_icon;
503 return simplelist_show_list(&info);
506 int filetype_load_plugin(const char* plugin, char* file)
508 int i;
509 char plugin_name[MAX_PATH];
510 char *s;
512 for (i=0;i<filetype_count;i++)
514 if (filetypes[i].plugin)
516 s = strrchr(filetypes[i].plugin, '/');
517 if (s)
519 if (!strcmp(s+1, plugin))
520 break;
522 else if (!strcmp(filetypes[i].plugin, plugin))
523 break;
526 if (i >= filetype_count)
527 return PLUGIN_ERROR;
528 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
529 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
530 return plugin_load(plugin_name, file);