stfm1000: comment static unused function
[maemo-rb.git] / apps / filetypes.c
blob8a4cedc0eadedc8987a0a1dc468ee7df9de87d38
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 "settings.h"
30 #include "debug.h"
31 #include "lang.h"
32 #include "kernel.h"
33 #include "plugin.h"
34 #include "filetypes.h"
35 #include "screens.h"
36 #include "dir.h"
37 #include "file.h"
38 #include "splash.h"
39 #include "core_alloc.h"
40 #include "icons.h"
41 #include "logf.h"
43 /* max filetypes (plugins & icons stored here) */
44 #if CONFIG_CODEC == SWCODEC
45 #define MAX_FILETYPES 192
46 #else
47 #define MAX_FILETYPES 128
48 #endif
49 /* max viewer plugins */
50 #ifdef HAVE_LCD_BITMAP
51 #define MAX_VIEWERS 56
52 #else
53 #define MAX_VIEWERS 24
54 #endif
56 /* a table for the known file types */
57 static const struct filetype inbuilt_filetypes[] = {
58 { "mp3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
59 { "mp2", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
60 { "mpa", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
61 #if CONFIG_CODEC == SWCODEC
62 /* Temporary hack to allow playlist creation */
63 { "mp1", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
64 { "ogg", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
65 { "oga", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
66 { "wma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
67 { "wmv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
68 { "asf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
69 { "wav", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
70 { "flac",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
71 { "ac3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
72 { "a52", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
73 { "mpc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
74 { "wv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
75 { "m4a", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
76 { "m4b", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
77 { "mp4", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
78 { "mod", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
79 { "shn", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
80 { "aif", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
81 { "aiff",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
82 { "spx" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
83 { "opus",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
84 { "sid", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
85 { "adx", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
86 { "nsf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
87 { "nsfe",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
88 { "spc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
89 { "ape", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
90 { "mac", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
91 { "sap" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
92 { "rm", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
93 { "ra", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
94 { "rmvb",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
95 { "cmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
96 { "cm3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
97 { "cmr", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
98 { "cms", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
99 { "dmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
100 { "dlt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
101 { "mpt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
102 { "mpd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
103 { "rmt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
104 { "tmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
105 { "tm8", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
106 { "tm2", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
107 { "oma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
108 { "aa3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
109 { "at3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
110 { "mmf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
111 { "au", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
112 { "snd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
113 { "vox", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
114 { "w64", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
115 { "tta", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
116 { "ay", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
117 { "gbs", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
118 { "hes", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
119 { "sgc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
120 { "vgm", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
121 { "vgz", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
122 { "kss", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
123 #endif
124 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
125 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
126 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
127 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
128 #ifdef HAVE_REMOTE_LCD
129 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
130 #endif
131 #if CONFIG_TUNER
132 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
133 { "fms", FILE_ATTR_FMS, Icon_Wps, VOICE_EXT_FMS },
134 #endif
135 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
136 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
137 { "lua", FILE_ATTR_LUA, Icon_Plugin, VOICE_EXT_ROCK },
138 #ifdef HAVE_LCD_BITMAP
139 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
140 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
141 #endif
142 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
143 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
144 #ifdef HAVE_LCD_BITMAP
145 { "sbs", FILE_ATTR_SBS, Icon_Wps, VOICE_EXT_SBS },
146 #endif
147 #ifdef HAVE_REMOTE_LCD
148 { "rsbs", FILE_ATTR_RSBS, Icon_Wps, VOICE_EXT_RSBS },
149 #if CONFIG_TUNER
150 { "rfms", FILE_ATTR_RFMS, Icon_Wps, VOICE_EXT_RFMS },
151 #endif
152 #endif
153 #ifdef BOOTFILE_EXT
154 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
155 #endif
156 #ifdef BOOTFILE_EXT2
157 { BOOTFILE_EXT2, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
158 #endif
161 void tree_get_filetypes(const struct filetype** types, int* count)
163 *types = inbuilt_filetypes;
164 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
167 #define ROCK_EXTENSION "rock"
169 struct file_type {
170 enum themable_icons icon; /* the icon which shall be used for it, NOICON if unknown */
171 unsigned char attr; /* FILE_ATTR_MASK >> 8 */
172 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
173 char* extension; /* NULL for none */
175 static struct file_type filetypes[MAX_FILETYPES];
176 static int custom_filetype_icons[MAX_FILETYPES];
177 static bool custom_icons_loaded = false;
178 #ifdef HAVE_LCD_COLOR
179 static int custom_colors[MAX_FILETYPES+1];
180 #endif
181 /* index array to filetypes used in open with list. */
182 static int viewers[MAX_VIEWERS];
183 static int filetype_count = 0;
184 static unsigned char highest_attr = 0;
185 static int viewer_count = 0;
187 static int strdup_handle, strdup_bufsize, strdup_cur_idx;
188 static int move_callback(int handle, void* current, void* new)
190 /*could compare to strdup_handle, but ops is only used once */
191 (void)handle;
192 size_t diff = new - current;
193 #define FIX_PTR(x) \
194 { if ((void*)x >= current && (void*)x < (current+strdup_bufsize)) x+= diff; }
195 for(int i = 0; i < filetype_count; i++)
197 FIX_PTR(filetypes[i].extension);
198 FIX_PTR(filetypes[i].plugin);
200 return BUFLIB_CB_OK;
203 static struct buflib_callbacks ops = {
204 .move_callback = move_callback,
205 .shrink_callback = NULL,
208 static char *filetypes_strdup(char* string)
210 char *buffer = core_get_data(strdup_handle) + strdup_cur_idx;
211 strdup_cur_idx += strlcpy(buffer, string, strdup_bufsize-strdup_cur_idx)+1;
212 return buffer;
215 static char *filetypes_store_plugin(char *plugin, int n)
217 int i;
218 /* if the plugin is in the list already, use it. */
219 for (i=0; i<viewer_count; i++)
221 if (!strcmp(filetypes[viewers[i]].plugin, plugin))
222 return filetypes[viewers[i]].plugin;
224 /* otherwise, allocate buffer */
225 if (viewer_count < MAX_VIEWERS)
226 viewers[viewer_count++] = n;
227 return filetypes_strdup(plugin);
230 static int find_extension(const char* extension)
232 int i;
233 if (!extension)
234 return -1;
235 for (i=1; i<filetype_count; i++)
237 if (filetypes[i].extension &&
238 !strcasecmp(extension, filetypes[i].extension))
239 return i;
241 return -1;
244 static void read_builtin_types(void);
245 static void read_config(int fd);
246 #ifdef HAVE_LCD_COLOR
247 /* Colors file format is similar to icons:
248 * ext:hex_color
249 * load a colors file from a theme with:
250 * filetype colours: filename.colours */
251 void read_color_theme_file(void) {
252 char buffer[MAX_PATH];
253 int fd;
254 char *ext, *color;
255 int i;
256 for (i = 0; i < MAX_FILETYPES+1; i++) {
257 custom_colors[i] = -1;
259 snprintf(buffer, MAX_PATH, THEME_DIR "/%s.colours",
260 global_settings.colors_file);
261 fd = open(buffer, O_RDONLY);
262 if (fd < 0)
263 return;
264 while (read_line(fd, buffer, MAX_PATH) > 0)
266 if (!settings_parseline(buffer, &ext, &color))
267 continue;
268 if (!strcasecmp(ext, "folder"))
270 hex_to_rgb(color, &custom_colors[0]);
271 continue;
273 if (!strcasecmp(ext, "???"))
275 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
276 continue;
278 i = find_extension(ext);
279 if (i >= 0)
280 hex_to_rgb(color, &custom_colors[i]);
282 close(fd);
284 #endif
285 #ifdef HAVE_LCD_BITMAP
286 void read_viewer_theme_file(void)
288 char buffer[MAX_PATH];
289 int fd;
290 char *ext, *icon;
291 int i;
292 global_status.viewer_icon_count = 0;
293 custom_icons_loaded = false;
294 custom_filetype_icons[0] = Icon_Folder;
295 for (i=1; i<filetype_count; i++)
297 custom_filetype_icons[i] = filetypes[i].icon;
300 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
301 global_settings.viewers_icon_file);
302 fd = open(buffer, O_RDONLY);
303 if (fd < 0)
304 return;
305 while (read_line(fd, buffer, MAX_PATH) > 0)
307 if (!settings_parseline(buffer, &ext, &icon))
308 continue;
309 i = find_extension(ext);
310 if (i >= 0)
312 if (*icon == '*')
313 custom_filetype_icons[i] = atoi(icon+1);
314 else if (*icon == '-')
315 custom_filetype_icons[i] = Icon_NOICON;
316 else if (*icon >= '0' && *icon <= '9')
318 int number = atoi(icon);
319 if (number > global_status.viewer_icon_count)
320 global_status.viewer_icon_count++;
321 custom_filetype_icons[i] = Icon_Last_Themeable + number;
325 close(fd);
326 custom_icons_loaded = true;
328 #endif
330 void filetype_init(void)
332 /* set the directory item first */
333 filetypes[0].extension = NULL;
334 filetypes[0].plugin = NULL;
335 filetypes[0].attr = 0;
336 filetypes[0].icon = Icon_Folder;
338 /* estimate bufsize with the filesize, will not be larger */
339 viewer_count = 0;
340 filetype_count = 1;
342 int fd = open(VIEWERS_CONFIG, O_RDONLY);
343 if (fd < 0)
344 return;
346 strdup_bufsize = filesize(fd);
347 strdup_handle = core_alloc_ex("filetypes", strdup_bufsize, &ops);
348 if (strdup_handle <= 0)
350 close(fd);
351 return;
353 read_builtin_types();
354 read_config(fd);
355 close(fd);
356 #ifdef HAVE_LCD_BITMAP
357 read_viewer_theme_file();
358 #endif
359 #ifdef HAVE_LCD_COLOR
360 read_color_theme_file();
361 #endif
362 core_shrink(strdup_handle, core_get_data(strdup_handle), strdup_cur_idx);
365 /* remove all white spaces from string */
366 static void rm_whitespaces(char* str)
368 char *s = str;
369 while (*str)
371 if (!isspace(*str))
373 *s = *str;
374 s++;
376 str++;
378 *s = '\0';
381 static void read_builtin_types(void)
383 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
384 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
386 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
387 filetypes[filetype_count].plugin = NULL;
388 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
389 if (filetypes[filetype_count].attr > highest_attr)
390 highest_attr = filetypes[filetype_count].attr;
391 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
392 filetype_count++;
396 static void read_config(int fd)
398 char line[64], *s, *e;
399 char *extension, *plugin;
400 /* config file is in the format
401 <extension>,<plugin>,<icon code>
402 ignore line if either of the first two are missing */
403 while (read_line(fd, line, sizeof line) > 0)
405 if (filetype_count >= MAX_FILETYPES)
407 splash(HZ, ID2P(LANG_FILETYPES_FULL));
408 break;
410 rm_whitespaces(line);
411 /* get the extension */
412 s = line;
413 e = strchr(s, ',');
414 if (!e)
415 continue;
416 *e = '\0';
417 extension = s;
419 /* get the plugin */
420 s = e+1;
421 e = strchr(s, ',');
422 if (!e)
423 continue;
424 *e = '\0';
425 plugin = s;
427 /* ok, store this plugin/extension, check icon after */
428 struct file_type *file_type = &filetypes[filetype_count];
429 file_type->extension = filetypes_strdup(extension);
430 file_type->plugin = filetypes_store_plugin(plugin, filetype_count);
431 file_type->attr = highest_attr +1;
432 file_type->icon = Icon_Questionmark;
433 highest_attr++;
434 /* get the icon */
435 s = e+1;
436 if (*s == '*')
437 file_type->icon = atoi(s+1);
438 else if (*s == '-')
439 file_type->icon = Icon_NOICON;
440 else if (*s >= '0' && *s <= '9')
441 file_type->icon = Icon_Last_Themeable + atoi(s);
442 filetype_count++;
446 int filetype_get_attr(const char* file)
448 char *extension = strrchr(file, '.');
449 int i;
450 if (!extension)
451 return 0;
452 extension++;
454 i = find_extension(extension);
455 if (i >= 0)
456 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
457 return 0;
460 static int find_attr(int attr)
462 int i;
463 /* skip the directory item */
464 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
465 return 0;
466 for (i=1; i<filetype_count; i++)
468 if ((attr>>8) == filetypes[i].attr)
469 return i;
471 return -1;
474 #ifdef HAVE_LCD_COLOR
475 int filetype_get_color(const char * name, int attr)
477 char *extension;
478 int i;
479 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
480 return custom_colors[0];
481 extension = strrchr(name, '.');
482 if (!extension)
483 return custom_colors[MAX_FILETYPES];
484 extension++;
486 i = find_extension(extension);
487 if (i >= 0)
488 return custom_colors[i];
489 return custom_colors[MAX_FILETYPES];
491 #endif
493 int filetype_get_icon(int attr)
495 int index = find_attr(attr);
496 if (index < 0)
497 return Icon_NOICON;
498 if (custom_icons_loaded)
499 return custom_filetype_icons[index];
500 return filetypes[index].icon;
503 char* filetype_get_plugin(const struct entry* file)
505 static char plugin_name[MAX_PATH];
506 int index = find_attr(file->attr);
507 if (index < 0)
508 return NULL;
509 if (filetypes[index].plugin == NULL)
510 return NULL;
511 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
512 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
513 return plugin_name;
516 bool filetype_supported(int attr)
518 return find_attr(attr) >= 0;
521 /**** Open With Screen ****/
522 struct cb_data {
523 const char *current_file;
526 static enum themable_icons openwith_get_icon(int selected_item, void * data)
528 (void)data;
529 return filetypes[viewers[selected_item]].icon;
532 static const char* openwith_get_name(int selected_item, void * data,
533 char * buffer, size_t buffer_len)
535 (void)data; (void)buffer; (void)buffer_len;
536 const char *s = strrchr(filetypes[viewers[selected_item]].plugin, '/');
537 if (s)
538 return s+1;
539 else return filetypes[viewers[selected_item]].plugin;
542 static int openwith_action_callback(int action, struct gui_synclist *lists)
544 struct cb_data *info = (struct cb_data *)lists->data;
545 int i;
546 if (action == ACTION_STD_OK)
548 char plugin[MAX_PATH];
549 i = viewers[gui_synclist_get_sel_pos(lists)];
550 snprintf(plugin, MAX_PATH, "%s/%s.%s",
551 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
552 plugin_load(plugin, info->current_file);
553 return ACTION_STD_CANCEL;
555 return action;
558 int filetype_list_viewers(const char* current_file)
560 struct simplelist_info info;
561 struct cb_data data = { current_file };
562 #ifndef HAVE_LCD_BITMAP
563 if (viewer_count == 0)
565 /* FIX: translation! */
566 splash(HZ*2, "No viewers found");
567 return PLUGIN_OK;
569 #endif
570 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), viewer_count, &data);
571 info.action_callback = openwith_action_callback;
572 info.get_name = openwith_get_name;
573 info.get_icon = global_settings.show_icons?openwith_get_icon:NULL;
574 return simplelist_show_list(&info);
577 int filetype_load_plugin(const char* plugin, const char* file)
579 int i;
580 char plugin_name[MAX_PATH];
581 char *s;
583 for (i=0;i<filetype_count;i++)
585 if (filetypes[i].plugin)
587 s = strrchr(filetypes[i].plugin, '/');
588 if (s)
590 if (!strcmp(s+1, plugin))
591 break;
593 else if (!strcmp(filetypes[i].plugin, plugin))
594 break;
597 if (i >= filetype_count)
598 return PLUGIN_ERROR;
599 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
600 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
601 return plugin_load(plugin_name, file);