Packard Bell Vibe 500: add RTC alarm.
[kugel-rb.git] / apps / filetypes.c
blobe74edff5c26a036f150736b2428ea5218ddaefc7
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 LCD_DEPTH > 1
113 { "bmp", FILE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
114 #endif
115 #if CONFIG_TUNER
116 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
117 #endif
118 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
119 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
120 { "lua", FILE_ATTR_LUA, Icon_Plugin, VOICE_EXT_ROCK },
121 #ifdef HAVE_LCD_BITMAP
122 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
123 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
124 #endif
125 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
126 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
127 #ifdef HAVE_LCD_BITMAP
128 { "sbs", FILE_ATTR_SBS, Icon_Wps, VOICE_EXT_SBS },
129 #endif
130 #ifdef HAVE_REMOTE_LCD
131 { "rsbs", FILE_ATTR_RSBS, Icon_Wps, VOICE_EXT_RSBS },
132 #endif
133 #ifdef BOOTFILE_EXT
134 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
135 #endif
136 #ifdef BOOTFILE_EXT2
137 { BOOTFILE_EXT2, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
138 #endif
141 void tree_get_filetypes(const struct filetype** types, int* count)
143 *types = inbuilt_filetypes;
144 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
147 /* mask for dynamic filetype info in attribute */
148 #define ROCK_EXTENSION "rock"
150 struct file_type {
151 int icon; /* the icon which shall be used for it, NOICON if unknown */
152 unsigned char attr; /* FILE_ATTR_MASK >> 8 */
153 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
154 char* extension; /* NULL for none */
156 static struct file_type filetypes[MAX_FILETYPES];
157 static int custom_filetype_icons[MAX_FILETYPES];
158 static bool custom_icons_loaded = false;
159 #ifdef HAVE_LCD_COLOR
160 static int custom_colors[MAX_FILETYPES+1];
161 #endif
162 static int filetype_count = 0;
163 static unsigned char highest_attr = 0;
165 static char *filetypes_strdup(char* string)
167 char *buffer = (char*)buffer_alloc(strlen(string)+1);
168 strcpy(buffer, string);
169 return buffer;
171 static void read_builtin_types(void);
172 static void read_config(char* config_file);
173 #ifdef HAVE_LCD_COLOR
174 /* Colors file format is similar to icons:
175 * ext:hex_color
176 * load a colors file from a theme with:
177 * filetype colours: filename.colours */
178 void read_color_theme_file(void) {
179 char buffer[MAX_PATH];
180 int fd;
181 char *ext, *color;
182 int i;
183 for (i = 0; i < MAX_FILETYPES+1; i++) {
184 custom_colors[i] = -1;
186 snprintf(buffer, MAX_PATH, "%s/%s.colours", THEME_DIR,
187 global_settings.colors_file);
188 fd = open(buffer, O_RDONLY);
189 if (fd < 0)
190 return;
191 while (read_line(fd, buffer, MAX_PATH) > 0)
193 if (!settings_parseline(buffer, &ext, &color))
194 continue;
195 if (!strcasecmp(ext, "folder"))
197 hex_to_rgb(color, &custom_colors[0]);
198 continue;
200 if (!strcasecmp(ext, "???"))
202 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
203 continue;
205 for (i=1; i<filetype_count; i++)
207 if (filetypes[i].extension &&
208 !strcasecmp(ext, filetypes[i].extension))
210 hex_to_rgb(color, &custom_colors[i]);
211 break;
215 close(fd);
217 #endif
218 #ifdef HAVE_LCD_BITMAP
219 void read_viewer_theme_file(void)
221 char buffer[MAX_PATH];
222 int fd;
223 char *ext, *icon;
224 int i;
225 global_status.viewer_icon_count = 0;
226 custom_icons_loaded = false;
227 custom_filetype_icons[0] = Icon_Folder;
228 for (i=1; i<filetype_count; i++)
230 custom_filetype_icons[i] = filetypes[i].icon;
233 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
234 global_settings.viewers_icon_file);
235 fd = open(buffer, O_RDONLY);
236 if (fd < 0)
237 return;
238 while (read_line(fd, buffer, MAX_PATH) > 0)
240 if (!settings_parseline(buffer, &ext, &icon))
241 continue;
242 for (i=0; i<filetype_count; i++)
244 if (filetypes[i].extension &&
245 !strcasecmp(ext, filetypes[i].extension))
247 if (*icon == '*')
248 custom_filetype_icons[i] = atoi(icon+1);
249 else if (*icon == '-')
250 custom_filetype_icons[i] = Icon_NOICON;
251 else if (*icon >= '0' && *icon <= '9')
253 int number = atoi(icon);
254 if (number > global_status.viewer_icon_count)
255 global_status.viewer_icon_count++;
256 custom_filetype_icons[i] = Icon_Last_Themeable + number;
258 break;
262 close(fd);
263 custom_icons_loaded = true;
265 #endif
267 void filetype_init(void)
269 /* set the directory item first */
270 filetypes[0].extension = NULL;
271 filetypes[0].plugin = NULL;
272 filetypes[0].attr = 0;
273 filetypes[0].icon = Icon_Folder;
275 filetype_count = 1;
276 read_builtin_types();
277 read_config(VIEWERS_CONFIG);
278 #ifdef HAVE_LCD_BITMAP
279 read_viewer_theme_file();
280 #endif
281 #ifdef HAVE_LCD_COLOR
282 read_color_theme_file();
283 #endif
286 /* remove all white spaces from string */
287 static void rm_whitespaces(char* str)
289 char *s = str;
290 while (*str)
292 if (!isspace(*str))
294 *s = *str;
295 s++;
297 str++;
299 *s = '\0';
302 static void read_builtin_types(void)
304 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
305 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
307 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
308 filetypes[filetype_count].plugin = NULL;
309 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
310 if (filetypes[filetype_count].attr > highest_attr)
311 highest_attr = filetypes[filetype_count].attr;
312 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
313 filetype_count++;
317 static void read_config(char* config_file)
319 char line[64], *s, *e;
320 char extension[8], plugin[32];
321 int fd = open(config_file, O_RDONLY);
322 if (fd < 0)
323 return;
324 /* config file is in the for
325 <extension>,<plugin>,<icon code>
326 ignore line if either of the first two are missing */
327 while (read_line(fd, line, 64) > 0)
329 if (filetype_count >= MAX_FILETYPES)
331 splash(HZ, ID2P(LANG_FILETYPES_FULL));
332 break;
334 rm_whitespaces(line);
335 /* get the extention */
336 s = line;
337 e = strchr(s, ',');
338 if (!e)
339 continue;
340 *e = '\0';
341 strcpy(extension, s);
343 /* get the plugin */
344 s = e+1;
345 e = strchr(s, ',');
346 if (!e)
347 continue;
348 *e = '\0';
350 strcpy(plugin, s);
351 /* ok, store this plugin/extension, check icon after */
352 filetypes[filetype_count].extension = filetypes_strdup(extension);
353 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
354 filetypes[filetype_count].attr = highest_attr +1;
355 filetypes[filetype_count].icon = Icon_Questionmark;
356 highest_attr++;
357 /* get the icon */
358 s = e+1;
359 if (*s == '*')
360 filetypes[filetype_count].icon = atoi(s+1);
361 else if (*s == '-')
362 filetypes[filetype_count].icon = Icon_NOICON;
363 else if (*s >= '0' && *s <= '9')
364 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
365 filetype_count++;
369 int filetype_get_attr(const char* file)
371 char *extension = strrchr(file, '.');
372 int i;
373 if (!extension)
374 return 0;
375 extension++;
376 for (i=0; i<filetype_count; i++)
378 if (filetypes[i].extension &&
379 !strcasecmp(extension, filetypes[i].extension))
380 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
382 return 0;
385 static int find_attr(int attr)
387 int i;
388 /* skip the directory item */
389 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
390 return 0;
391 for (i=1; i<filetype_count; i++)
393 if ((attr>>8) == filetypes[i].attr)
394 return i;
396 return -1;
399 #ifdef HAVE_LCD_COLOR
400 int filetype_get_color(const char * name, int attr)
402 char *extension;
403 int i;
404 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
405 return custom_colors[0];
406 extension = strrchr(name, '.');
407 if (!extension)
408 return custom_colors[MAX_FILETYPES];
409 extension++;
411 for (i=1; i<filetype_count; i++)
413 if (filetypes[i].extension &&
414 !strcasecmp(extension, filetypes[i].extension))
415 return custom_colors[i];
417 return custom_colors[MAX_FILETYPES];
419 #endif
421 int filetype_get_icon(int attr)
423 int index = find_attr(attr);
424 if (index < 0)
425 return Icon_NOICON;
426 if (custom_icons_loaded)
427 return custom_filetype_icons[index];
428 return filetypes[index].icon;
431 char* filetype_get_plugin(const struct entry* file)
433 static char plugin_name[MAX_PATH];
434 int index = find_attr(file->attr);
435 if (index < 0)
436 return NULL;
437 if (filetypes[index].plugin == NULL)
438 return NULL;
439 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
440 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
441 return plugin_name;
444 bool filetype_supported(int attr)
446 return find_attr(attr) >= 0;
449 /**** Open With Screen ****/
450 struct cb_data {
451 int *items;
452 const char *current_file;
455 static enum themable_icons openwith_get_icon(int selected_item, void * data)
457 struct cb_data *info = (struct cb_data *)data;
458 int *items = info->items;
459 return filetypes[items[selected_item]].icon;
462 static const char* openwith_get_name(int selected_item, void * data,
463 char * buffer, size_t buffer_len)
465 (void)buffer; (void)buffer_len;
466 struct cb_data *info = (struct cb_data *)data;
467 int *items = info->items;
468 const char *s = strrchr(filetypes[items[selected_item]].plugin, '/');
469 if (s)
470 return s+1;
471 else return filetypes[items[selected_item]].plugin;
474 static int openwith_action_callback(int action, struct gui_synclist *lists)
476 struct cb_data *info = (struct cb_data *)lists->data;
477 int *items = info->items;
478 int i;
479 if (action == ACTION_STD_OK)
481 char plugin[MAX_PATH];
482 i = items[gui_synclist_get_sel_pos(lists)];
483 snprintf(plugin, MAX_PATH, "%s/%s.%s",
484 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
485 plugin_load(plugin, info->current_file);
486 return ACTION_STD_CANCEL;
488 return action;
491 int filetype_list_viewers(const char* current_file)
493 int i, count = 0;
494 int items[MAX_FILETYPES];
495 struct simplelist_info info;
496 struct cb_data data = { items, current_file };
497 for (i=0; i<filetype_count && count < MAX_FILETYPES; i++)
499 if (filetypes[i].plugin)
501 int j;
502 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
504 if (!strcmp(filetypes[i].plugin,filetypes[items[j]].plugin))
505 break;
507 if (j<count)
508 continue; /* it is so grab the next plugin */
509 items[count++] = i;
512 #ifndef HAVE_LCD_BITMAP
513 if (count == 0)
515 /* FIX: translation! */
516 splash(HZ*2, "No viewers found");
517 return PLUGIN_OK;
519 #endif
520 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), count, &data);
521 info.action_callback = openwith_action_callback;
522 info.get_name = openwith_get_name;
523 info.get_icon = global_settings.show_icons?openwith_get_icon:NULL;
524 return simplelist_show_list(&info);
527 int filetype_load_plugin(const char* plugin, char* file)
529 int i;
530 char plugin_name[MAX_PATH];
531 char *s;
533 for (i=0;i<filetype_count;i++)
535 if (filetypes[i].plugin)
537 s = strrchr(filetypes[i].plugin, '/');
538 if (s)
540 if (!strcmp(s+1, plugin))
541 break;
543 else if (!strcmp(filetypes[i].plugin, plugin))
544 break;
547 if (i >= filetype_count)
548 return PLUGIN_ERROR;
549 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
550 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
551 return plugin_load(plugin_name, file);