rbutil: Change layout to RTL if Hebrew language is used
[kugel-rb.git] / apps / filetypes.c
blob2c737ce883ddbae16ca92a25c590c8875d2c998a
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 HAVE_LCD_BITMAP
112 { "sbs", FILE_ATTR_SBS, Icon_Wps, VOICE_EXT_SBS },
113 #endif
114 #ifdef HAVE_REMOTE_LCD
115 { "rsbs", FILE_ATTR_RSBS, Icon_Wps, VOICE_EXT_RSBS },
116 #endif
117 #ifdef BOOTFILE_EXT
118 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
119 #endif
120 #ifdef BOOTFILE_EXT2
121 { BOOTFILE_EXT2, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
122 #endif
125 void tree_get_filetypes(const struct filetype** types, int* count)
127 *types = inbuilt_filetypes;
128 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
131 /* mask for dynamic filetype info in attribute */
132 #define FILETYPES_MASK 0xFF00
133 #define ROCK_EXTENSION "rock"
135 struct file_type {
136 int icon; /* the icon which shall be used for it, NOICON if unknown */
137 unsigned char attr; /* FILETYPES_MASK >> 8 */
138 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
139 char* extension; /* NULL for none */
141 static struct file_type filetypes[MAX_FILETYPES];
142 static int custom_filetype_icons[MAX_FILETYPES];
143 static bool custom_icons_loaded = false;
144 #ifdef HAVE_LCD_COLOR
145 static int custom_colors[MAX_FILETYPES+1];
146 #endif
147 static int filetype_count = 0;
148 static unsigned char highest_attr = 0;
150 static char *filetypes_strdup(char* string)
152 char *buffer = (char*)buffer_alloc(strlen(string)+1);
153 strcpy(buffer, string);
154 return buffer;
156 static void read_builtin_types(void);
157 static void read_config(char* config_file);
158 #ifdef HAVE_LCD_COLOR
159 /* Colors file format is similar to icons:
160 * ext:hex_color
161 * load a colors file from a theme with:
162 * filetype colours: filename.colours */
163 void read_color_theme_file(void) {
164 char buffer[MAX_PATH];
165 int fd;
166 char *ext, *color;
167 int i;
168 for (i = 0; i < MAX_FILETYPES+1; i++) {
169 custom_colors[i] = -1;
171 snprintf(buffer, MAX_PATH, "%s/%s.colours", THEME_DIR,
172 global_settings.colors_file);
173 fd = open(buffer, O_RDONLY);
174 if (fd < 0)
175 return;
176 while (read_line(fd, buffer, MAX_PATH) > 0)
178 if (!settings_parseline(buffer, &ext, &color))
179 continue;
180 if (!strcasecmp(ext, "folder"))
182 hex_to_rgb(color, &custom_colors[0]);
183 continue;
185 if (!strcasecmp(ext, "???"))
187 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
188 continue;
190 for (i=1; i<filetype_count; i++)
192 if (filetypes[i].extension &&
193 !strcasecmp(ext, filetypes[i].extension))
195 hex_to_rgb(color, &custom_colors[i]);
196 break;
200 close(fd);
202 #endif
203 #ifdef HAVE_LCD_BITMAP
204 void read_viewer_theme_file(void)
206 char buffer[MAX_PATH];
207 int fd;
208 char *ext, *icon;
209 int i;
210 global_status.viewer_icon_count = 0;
211 custom_icons_loaded = false;
212 custom_filetype_icons[0] = Icon_Folder;
213 for (i=1; i<filetype_count; i++)
215 custom_filetype_icons[i] = filetypes[i].icon;
218 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
219 global_settings.viewers_icon_file);
220 fd = open(buffer, O_RDONLY);
221 if (fd < 0)
222 return;
223 while (read_line(fd, buffer, MAX_PATH) > 0)
225 if (!settings_parseline(buffer, &ext, &icon))
226 continue;
227 for (i=0; i<filetype_count; i++)
229 if (filetypes[i].extension &&
230 !strcasecmp(ext, filetypes[i].extension))
232 if (*icon == '*')
233 custom_filetype_icons[i] = atoi(icon+1);
234 else if (*icon == '-')
235 custom_filetype_icons[i] = Icon_NOICON;
236 else if (*icon >= '0' && *icon <= '9')
238 int number = atoi(icon);
239 if (number > global_status.viewer_icon_count)
240 global_status.viewer_icon_count++;
241 custom_filetype_icons[i] = Icon_Last_Themeable + number;
243 break;
247 close(fd);
248 custom_icons_loaded = true;
250 #endif
252 void filetype_init(void)
254 /* set the directory item first */
255 filetypes[0].extension = NULL;
256 filetypes[0].plugin = NULL;
257 filetypes[0].attr = 0;
258 filetypes[0].icon = Icon_Folder;
260 filetype_count = 1;
261 read_builtin_types();
262 read_config(VIEWERS_CONFIG);
263 #ifdef HAVE_LCD_BITMAP
264 read_viewer_theme_file();
265 #endif
266 #ifdef HAVE_LCD_COLOR
267 read_color_theme_file();
268 #endif
271 /* remove all white spaces from string */
272 static void rm_whitespaces(char* str)
274 char *s = str;
275 while (*str)
277 if (!isspace(*str))
279 *s = *str;
280 s++;
282 str++;
284 *s = '\0';
287 static void read_builtin_types(void)
289 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
290 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
292 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
293 filetypes[filetype_count].plugin = NULL;
294 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
295 if (filetypes[filetype_count].attr > highest_attr)
296 highest_attr = filetypes[filetype_count].attr;
297 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
298 filetype_count++;
302 static void read_config(char* config_file)
304 char line[64], *s, *e;
305 char extension[8], plugin[32];
306 int fd = open(config_file, O_RDONLY);
307 if (fd < 0)
308 return;
309 /* config file is in the for
310 <extension>,<plugin>,<icon code>
311 ignore line if either of the first two are missing */
312 while (read_line(fd, line, 64) > 0)
314 if (filetype_count >= MAX_FILETYPES)
316 splash(HZ, ID2P(LANG_FILETYPES_FULL));
317 break;
319 rm_whitespaces(line);
320 /* get the extention */
321 s = line;
322 e = strchr(s, ',');
323 if (!e)
324 continue;
325 *e = '\0';
326 strcpy(extension, s);
328 /* get the plugin */
329 s = e+1;
330 e = strchr(s, ',');
331 if (!e)
332 continue;
333 *e = '\0';
335 strcpy(plugin, s);
336 /* ok, store this plugin/extension, check icon after */
337 filetypes[filetype_count].extension = filetypes_strdup(extension);
338 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
339 filetypes[filetype_count].attr = highest_attr +1;
340 filetypes[filetype_count].icon = Icon_Questionmark;
341 highest_attr++;
342 /* get the icon */
343 s = e+1;
344 if (*s == '*')
345 filetypes[filetype_count].icon = atoi(s+1);
346 else if (*s == '-')
347 filetypes[filetype_count].icon = Icon_NOICON;
348 else if (*s >= '0' && *s <= '9')
349 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
350 filetype_count++;
354 int filetype_get_attr(const char* file)
356 char *extension = strrchr(file, '.');
357 int i;
358 if (!extension)
359 return 0;
360 extension++;
361 for (i=0; i<filetype_count; i++)
363 if (filetypes[i].extension &&
364 !strcasecmp(extension, filetypes[i].extension))
365 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
367 return 0;
370 static int find_attr(int attr)
372 int i;
373 /* skip the directory item */
374 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
375 return 0;
376 for (i=1; i<filetype_count; i++)
378 if ((attr>>8) == filetypes[i].attr)
379 return i;
381 return -1;
384 #ifdef HAVE_LCD_COLOR
385 int filetype_get_color(const char * name, int attr)
387 char *extension;
388 int i;
389 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
390 return custom_colors[0];
391 extension = strrchr(name, '.');
392 if (!extension)
393 return custom_colors[MAX_FILETYPES];
394 extension++;
396 for (i=1; i<filetype_count; i++)
398 if (filetypes[i].extension &&
399 !strcasecmp(extension, filetypes[i].extension))
400 return custom_colors[i];
402 return custom_colors[MAX_FILETYPES];
404 #endif
406 int filetype_get_icon(int attr)
408 int index = find_attr(attr);
409 if (index < 0)
410 return Icon_NOICON;
411 if (custom_icons_loaded)
412 return custom_filetype_icons[index];
413 return filetypes[index].icon;
416 char* filetype_get_plugin(const struct entry* file)
418 static char plugin_name[MAX_PATH];
419 int index = find_attr(file->attr);
420 if (index < 0)
421 return NULL;
422 if (filetypes[index].plugin == NULL)
423 return NULL;
424 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
425 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
426 return plugin_name;
429 bool filetype_supported(int attr)
431 return find_attr(attr) >= 0;
434 /**** Open With Screen ****/
435 struct cb_data {
436 int *items;
437 const char *current_file;
440 static enum themable_icons openwith_get_icon(int selected_item, void * data)
442 struct cb_data *info = (struct cb_data *)data;
443 int *items = info->items;
444 return filetypes[items[selected_item]].icon;
447 static const char* openwith_get_name(int selected_item, void * data,
448 char * buffer, size_t buffer_len)
450 (void)buffer; (void)buffer_len;
451 struct cb_data *info = (struct cb_data *)data;
452 int *items = info->items;
453 const char *s = strrchr(filetypes[items[selected_item]].plugin, '/');
454 if (s)
455 return s+1;
456 else return filetypes[items[selected_item]].plugin;
459 static int openwith_action_callback(int action, struct gui_synclist *lists)
461 struct cb_data *info = (struct cb_data *)lists->data;
462 int *items = info->items;
463 int i;
464 if (action == ACTION_STD_OK)
466 char plugin[MAX_PATH];
467 i = items[gui_synclist_get_sel_pos(lists)];
468 snprintf(plugin, MAX_PATH, "%s/%s.%s",
469 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
470 plugin_load(plugin, info->current_file);
471 return ACTION_STD_CANCEL;
473 return action;
476 int filetype_list_viewers(const char* current_file)
478 int i, count = 0;
479 int items[MAX_FILETYPES];
480 struct simplelist_info info;
481 struct cb_data data = { items, current_file };
482 for (i=0; i<filetype_count && count < MAX_FILETYPES; i++)
484 if (filetypes[i].plugin)
486 int j;
487 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
489 if (!strcmp(filetypes[i].plugin,filetypes[items[j]].plugin))
490 break;
492 if (j<count)
493 continue; /* it is so grab the next plugin */
494 items[count++] = i;
497 #ifndef HAVE_LCD_BITMAP
498 if (count == 0)
500 /* FIX: translation! */
501 splash(HZ*2, "No viewers found");
502 return PLUGIN_OK;
504 #endif
505 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), count, &data);
506 info.action_callback = openwith_action_callback;
507 info.get_name = openwith_get_name;
508 info.get_icon = openwith_get_icon;
509 return simplelist_show_list(&info);
512 int filetype_load_plugin(const char* plugin, char* file)
514 int i;
515 char plugin_name[MAX_PATH];
516 char *s;
518 for (i=0;i<filetype_count;i++)
520 if (filetypes[i].plugin)
522 s = strrchr(filetypes[i].plugin, '/');
523 if (s)
525 if (!strcmp(s+1, plugin))
526 break;
528 else if (!strcmp(filetypes[i].plugin, plugin))
529 break;
532 if (i >= filetype_count)
533 return PLUGIN_ERROR;
534 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
535 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
536 return plugin_load(plugin_name, file);