FS#9281 Rename of splash functions.
[kugel-rb.git] / apps / filetypes.c
blobb50814f7f228c0af623143449e40bc6d8226f8db
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 { "wma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
61 { "wmv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
62 { "asf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
63 { "wav", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
64 { "flac",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
65 { "ac3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
66 { "a52", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
67 { "mpc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
68 { "wv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
69 { "m4a", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
70 { "m4b", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
71 { "mp4", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
72 { "mod", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
73 { "shn", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
74 { "aif", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
75 { "aiff",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
76 { "spx" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
77 { "sid", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
78 { "adx", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
79 { "nsf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
80 { "nsfe",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
81 { "spc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
82 { "ape", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
83 { "mac", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
84 { "sap" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
85 #endif
86 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
87 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
88 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
89 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
90 #ifdef HAVE_REMOTE_LCD
91 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
92 #endif
93 #if LCD_DEPTH > 1
94 { "bmp", FILE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
95 #endif
96 #if CONFIG_TUNER
97 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
98 #endif
99 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
100 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
101 #ifdef HAVE_LCD_BITMAP
102 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
103 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
104 #endif
105 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
106 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
107 #ifdef BOOTFILE_EXT
108 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
109 #endif
112 void tree_get_filetypes(const struct filetype** types, int* count)
114 *types = inbuilt_filetypes;
115 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
118 /* mask for dynamic filetype info in attribute */
119 #define FILETYPES_MASK 0xFF00
120 #define ROCK_EXTENSION "rock"
122 struct file_type {
123 int icon; /* the icon which shall be used for it, NOICON if unknown */
124 unsigned char attr; /* FILETYPES_MASK >> 8 */
125 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
126 char* extension; /* NULL for none */
128 static struct file_type filetypes[MAX_FILETYPES];
129 static int custom_filetype_icons[MAX_FILETYPES];
130 static bool custom_icons_loaded = false;
131 #ifdef HAVE_LCD_COLOR
132 static int custom_colors[MAX_FILETYPES+1];
133 #endif
134 static int filetype_count = 0;
135 static unsigned char highest_attr = 0;
137 static char *filetypes_strdup(char* string)
139 char *buffer = (char*)buffer_alloc(strlen(string)+1);
140 strcpy(buffer, string);
141 return buffer;
143 static void read_builtin_types(void);
144 static void read_config(char* config_file);
145 #ifdef HAVE_LCD_COLOR
146 /* Colors file format is similar to icons:
147 * ext:hex_color
148 * load a colors file from a theme with:
149 * filetype colours: filename.colours */
150 void read_color_theme_file(void) {
151 char buffer[MAX_PATH];
152 int fd;
153 char *ext, *color;
154 int i;
155 for (i = 0; i < MAX_FILETYPES+1; i++) {
156 custom_colors[i] = -1;
158 snprintf(buffer, MAX_PATH, "%s/%s.colours", THEME_DIR,
159 global_settings.colors_file);
160 fd = open(buffer, O_RDONLY);
161 if (fd < 0)
162 return;
163 while (read_line(fd, buffer, MAX_PATH) > 0)
165 if (!settings_parseline(buffer, &ext, &color))
166 continue;
167 if (!strcasecmp(ext, "folder"))
169 hex_to_rgb(color, &custom_colors[0]);
170 continue;
172 if (!strcasecmp(ext, "???"))
174 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
175 continue;
177 for (i=1; i<filetype_count; i++)
179 if (filetypes[i].extension &&
180 !strcasecmp(ext, filetypes[i].extension))
182 hex_to_rgb(color, &custom_colors[i]);
183 break;
187 close(fd);
189 #endif
190 #ifdef HAVE_LCD_BITMAP
191 void read_viewer_theme_file(void)
193 char buffer[MAX_PATH];
194 int fd;
195 char *ext, *icon;
196 int i;
197 global_status.viewer_icon_count = 0;
198 custom_icons_loaded = false;
199 custom_filetype_icons[0] = Icon_Folder;
200 for (i=1; i<filetype_count; i++)
202 custom_filetype_icons[i] = filetypes[i].icon;
205 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
206 global_settings.viewers_icon_file);
207 fd = open(buffer, O_RDONLY);
208 if (fd < 0)
209 return;
210 while (read_line(fd, buffer, MAX_PATH) > 0)
212 if (!settings_parseline(buffer, &ext, &icon))
213 continue;
214 for (i=0; i<filetype_count; i++)
216 if (filetypes[i].extension &&
217 !strcasecmp(ext, filetypes[i].extension))
219 if (*icon == '*')
220 custom_filetype_icons[i] = atoi(icon+1);
221 else if (*icon == '-')
222 custom_filetype_icons[i] = Icon_NOICON;
223 else if (*icon >= '0' && *icon <= '9')
225 int number = atoi(icon);
226 if (number > global_status.viewer_icon_count)
227 global_status.viewer_icon_count++;
228 custom_filetype_icons[i] = Icon_Last_Themeable + number;
230 break;
234 close(fd);
235 custom_icons_loaded = true;
237 #endif
239 void filetype_init(void)
241 /* set the directory item first */
242 filetypes[0].extension = NULL;
243 filetypes[0].plugin = NULL;
244 filetypes[0].attr = 0;
245 filetypes[0].icon = Icon_Folder;
247 filetype_count = 1;
248 read_builtin_types();
249 read_config(VIEWERS_CONFIG);
250 #ifdef HAVE_LCD_BITMAP
251 read_viewer_theme_file();
252 #endif
253 #ifdef HAVE_LCD_COLOR
254 read_color_theme_file();
255 #endif
258 /* remove all white spaces from string */
259 static void rm_whitespaces(char* str)
261 char *s = str;
262 while (*str)
264 if (!isspace(*str))
266 *s = *str;
267 s++;
269 str++;
271 *s = '\0';
274 static void read_builtin_types(void)
276 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
277 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
279 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
280 filetypes[filetype_count].plugin = NULL;
281 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
282 if (filetypes[filetype_count].attr > highest_attr)
283 highest_attr = filetypes[filetype_count].attr;
284 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
285 filetype_count++;
289 static void read_config(char* config_file)
291 char line[64], *s, *e;
292 char extension[8], plugin[32];
293 int fd = open(config_file, O_RDONLY);
294 if (fd < 0)
295 return;
296 /* config file is in the for
297 <extension>,<plugin>,<icon code>
298 ignore line if either of the first two are missing */
299 while (read_line(fd, line, 64) > 0)
301 if (filetype_count >= MAX_FILETYPES)
303 splash(HZ, ID2P(LANG_FILETYPES_FULL));
304 break;
306 rm_whitespaces(line);
307 /* get the extention */
308 s = line;
309 e = strchr(s, ',');
310 if (!e)
311 continue;
312 *e = '\0';
313 strcpy(extension, s);
315 /* get the plugin */
316 s = e+1;
317 e = strchr(s, ',');
318 if (!e)
319 continue;
320 *e = '\0';
322 strcpy(plugin, s);
323 /* ok, store this plugin/extension, check icon after */
324 filetypes[filetype_count].extension = filetypes_strdup(extension);
325 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
326 filetypes[filetype_count].attr = highest_attr +1;
327 filetypes[filetype_count].icon = Icon_Questionmark;
328 highest_attr++;
329 /* get the icon */
330 s = e+1;
331 if (*s == '*')
332 filetypes[filetype_count].icon = atoi(s+1);
333 else if (*s == '-')
334 filetypes[filetype_count].icon = Icon_NOICON;
335 else if (*s >= '0' && *s <= '9')
336 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
337 filetype_count++;
341 int filetype_get_attr(const char* file)
343 char *extension = strrchr(file, '.');
344 int i;
345 if (!extension)
346 return 0;
347 extension++;
348 for (i=0; i<filetype_count; i++)
350 if (filetypes[i].extension &&
351 !strcasecmp(extension, filetypes[i].extension))
352 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
354 return 0;
357 static int find_attr(int attr)
359 int i;
360 /* skip the directory item */
361 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
362 return 0;
363 for (i=1; i<filetype_count; i++)
365 if ((attr>>8) == filetypes[i].attr)
366 return i;
368 return -1;
371 #ifdef HAVE_LCD_COLOR
372 int filetype_get_color(const char * name, int attr)
374 char *extension;
375 int i;
376 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
377 return custom_colors[0];
378 extension = strrchr(name, '.');
379 if (!extension)
380 return custom_colors[MAX_FILETYPES];
381 extension++;
383 for (i=1; i<filetype_count; i++)
385 if (filetypes[i].extension &&
386 !strcasecmp(extension, filetypes[i].extension))
387 return custom_colors[i];
389 return custom_colors[MAX_FILETYPES];
391 #endif
393 int filetype_get_icon(int attr)
395 int index = find_attr(attr);
396 if (index < 0)
397 return Icon_NOICON;
398 if (custom_icons_loaded)
399 return custom_filetype_icons[index];
400 return filetypes[index].icon;
403 char* filetype_get_plugin(const struct entry* file)
405 static char plugin_name[MAX_PATH];
406 int index = find_attr(file->attr);
407 if (index < 0)
408 return NULL;
409 if (filetypes[index].plugin == NULL)
410 return NULL;
411 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
412 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
413 return plugin_name;
416 bool filetype_supported(int attr)
418 return find_attr(attr) >= 0;
421 /**** Open With Screen ****/
422 struct cb_data {
423 int *items;
424 const char *current_file;
427 static enum themable_icons openwith_get_icon(int selected_item, void * data)
429 struct cb_data *info = (struct cb_data *)data;
430 int *items = info->items;
431 return filetypes[items[selected_item]].icon;
434 static char * openwith_get_name(int selected_item, void * data,
435 char * buffer, size_t buffer_len)
437 (void)buffer; (void)buffer_len;
438 struct cb_data *info = (struct cb_data *)data;
439 int *items = info->items;
440 char *s = strrchr(filetypes[items[selected_item]].plugin, '/');
441 if (s)
442 return s+1;
443 else return filetypes[items[selected_item]].plugin;
446 static int openwith_action_callback(int action, struct gui_synclist *lists)
448 struct cb_data *info = (struct cb_data *)lists->data;
449 int *items = info->items;
450 int i;
451 if (action == ACTION_STD_OK)
453 char plugin[MAX_PATH];
454 i = items[gui_synclist_get_sel_pos(lists)];
455 snprintf(plugin, MAX_PATH, "%s/%s.%s",
456 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
457 plugin_load(plugin, info->current_file);
458 return ACTION_STD_CANCEL;
460 return action;
463 int filetype_list_viewers(const char* current_file)
465 int i, count = 0;
466 int items[MAX_FILETYPES];
467 struct simplelist_info info;
468 struct cb_data data = { items, current_file };
469 for (i=0; i<filetype_count && count < MAX_FILETYPES; i++)
471 if (filetypes[i].plugin)
473 int j;
474 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
476 if (!strcmp(filetypes[i].plugin,filetypes[items[j]].plugin))
477 break;
479 if (j<count)
480 continue; /* it is so grab the next plugin */
481 items[count++] = i;
484 #ifndef HAVE_LCD_BITMAP
485 if (count == 0)
487 /* FIX: translation! */
488 splash(HZ*2, "No viewers found");
489 return PLUGIN_OK;
491 #endif
492 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), count, &data);
493 info.action_callback = openwith_action_callback;
494 info.get_name = openwith_get_name;
495 info.get_icon = openwith_get_icon;
496 return simplelist_show_list(&info);
499 int filetype_load_plugin(const char* plugin, char* file)
501 int i;
502 char plugin_name[MAX_PATH];
503 char *s;
505 for (i=0;i<filetype_count;i++)
507 if (filetypes[i].plugin)
509 s = strrchr(filetypes[i].plugin, '/');
510 if (s)
512 if (!strcmp(s+1, plugin))
513 break;
515 else if (!strcmp(filetypes[i].plugin, plugin))
516 break;
519 if (i >= filetype_count)
520 return PLUGIN_ERROR;
521 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
522 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
523 return plugin_load(plugin_name, file);