1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 Jonathan Gordon
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
34 #include "filetypes.h"
43 /* max filetypes (plugins & icons stored here) */
44 #if CONFIG_CODEC == SWCODEC
45 #define MAX_FILETYPES 72
47 #define MAX_FILETYPES 48
50 /* number of bytes for the binary icon */
53 /* mask for dynamic filetype info in attribute */
54 #define FILETYPES_MASK 0xFF00
55 #define ROCK_EXTENSION "rock"
58 ICON_NO_CONST icon
; /* the icon which shall be used for it, NOICON if unknown */
59 bool viewer
; /* true if the rock is in viewers, false if in rocks */
60 unsigned char attr
; /* FILETYPES_MASK >> 8 */
61 char* plugin
; /* Which plugin to use, NULL if unknown, or builtin */
62 char* extension
; /* NULL for none */
64 static struct file_type filetypes
[MAX_FILETYPES
];
65 static int filetype_count
= 0;
66 static unsigned char heighest_attr
= 0;
68 static char *filetypes_strdup(char* string
)
70 char *buffer
= (char*)buffer_alloc(strlen(string
)+1);
71 strcpy(buffer
, string
);
74 static void read_builtin_types(void);
75 static void read_config(char* config_file
);
77 void filetype_init(void)
79 /* set the directory item first */
80 filetypes
[0].extension
= NULL
;
81 filetypes
[0].plugin
= NULL
;
82 filetypes
[0].attr
= 0;
84 #ifdef HAVE_LCD_BITMAP
85 (ICON_NO_CONST
)&bitmap_icons_6x8
[Icon_Folder
];
87 (ICON_NO_CONST
)Icon_Folder
;
91 read_config(VIEWERS_CONFIG
);
94 /* remove all white spaces from string */
95 static void rm_whitespaces(char* str
)
110 static void read_builtin_types(void)
112 const struct filetype
*types
;
114 tree_get_filetypes(&types
, &count
);
115 for(i
=0; i
<count
&& (filetype_count
< MAX_FILETYPES
); i
++)
117 filetypes
[filetype_count
].extension
= types
[i
].extension
;
118 filetypes
[filetype_count
].plugin
= NULL
;
119 filetypes
[filetype_count
].attr
= types
[i
].tree_attr
>>8;
120 if (filetypes
[filetype_count
].attr
> heighest_attr
)
121 heighest_attr
= filetypes
[filetype_count
].attr
;
122 filetypes
[filetype_count
].icon
=
123 #ifdef HAVE_LCD_BITMAP
124 (ICON_NO_CONST
)&bitmap_icons_6x8
[types
[i
].icon
];
126 (ICON_NO_CONST
)types
[i
].icon
;
132 static void read_config(char* config_file
)
134 char line
[64], *s
, *e
;
135 char extension
[8], plugin
[32];
136 #ifdef HAVE_LCD_BITMAP
137 char icon
[ICON_LENGTH
];
141 int fd
= open(config_file
, O_RDONLY
);
144 /* config file is in the for
145 <extension>,<plugin>,<icon code>
146 ignore line if either of the first two are missing */
147 while (read_line(fd
, line
, 64) > 0)
149 if (filetype_count
>= MAX_FILETYPES
)
151 gui_syncsplash(HZ
, str(LANG_FILETYPES_FULL
));
154 rm_whitespaces(line
);
155 /* get the extention */
161 strcpy(extension
, s
);
169 if (!strcasecmp("viewers", s
))
179 /* ok, store this plugin/extension, check icon after */
180 filetypes
[filetype_count
].extension
= filetypes_strdup(extension
);
181 filetypes
[filetype_count
].plugin
= filetypes_strdup(plugin
);
182 filetypes
[filetype_count
].viewer
= viewer
;
183 filetypes
[filetype_count
].attr
= heighest_attr
+1;
186 #ifdef HAVE_LCD_BITMAP
193 for (i
= 0; good_icon
&& i
< ICON_LENGTH
; i
++)
195 for (j
=0; good_icon
&& j
<2; j
++)
197 val
[j
] = tolower(s
[i
*2+j
]);
198 if (val
[j
] >= 'a' && val
[j
] <= 'f')
200 val
[j
] = val
[j
] - 'a' + 10;
202 else if (val
[j
] >= '0' && val
[j
] <= '9')
204 val
[j
] = val
[j
] - '0';
209 icon
[i
]=((val
[0]<<4) | val
[1]);
214 filetypes
[filetype_count
].icon
=
215 (ICON_NO_CONST
)buffer_alloc(ICON_LENGTH
);
216 memcpy(filetypes
[filetype_count
].icon
, icon
, ICON_LENGTH
);
219 filetypes
[filetype_count
].icon
= NOICON
;
221 filetypes
[filetype_count
].icon
= Icon_Unknown
;
227 int filetype_get_attr(const char* file
)
229 char *extension
= strrchr(file
, '.');
234 for (i
=0; i
<filetype_count
; i
++)
236 if (filetypes
[i
].extension
&&
237 !strcasecmp(extension
, filetypes
[i
].extension
))
238 return (filetypes
[i
].attr
<<8)&TREE_ATTR_MASK
;
243 static int find_attr(int attr
)
246 /* skip the directory item */
247 if ((attr
& ATTR_DIRECTORY
)==ATTR_DIRECTORY
)
249 for (i
=1; i
<filetype_count
; i
++)
251 if ((attr
>>8) == filetypes
[i
].attr
)
257 ICON
filetype_get_icon(int attr
)
259 int index
= find_attr(attr
);
262 return (ICON
)filetypes
[index
].icon
;
265 char* filetype_get_plugin(const struct entry
* file
)
267 static char plugin_name
[MAX_PATH
];
268 int index
= find_attr(file
->attr
);
271 snprintf(plugin_name
, MAX_PATH
, "%s/%s.%s",
272 filetypes
[index
].viewer
? VIEWERS_DIR
: PLUGIN_DIR
,
273 filetypes
[index
].plugin
, ROCK_EXTENSION
);
277 bool filetype_supported(int attr
)
279 return find_attr(attr
) >= 0;
282 int filetype_list_viewers(const char* current_file
)
285 char *strings
[MAX_FILETYPES
/2];
286 struct menu_callback_with_desc cb_and_desc
=
287 { NULL
, ID2P(LANG_ONPLAY_OPEN_WITH
), Icon_Plugin
};
288 struct menu_item_ex menu
;
290 for (i
=0; i
<filetype_count
&& count
< (MAX_FILETYPES
/2); i
++)
292 if (filetypes
[i
].plugin
)
295 for (j
=0;j
<count
;j
++) /* check if the plugin is in the list yet */
297 if (!strcmp(strings
[j
], filetypes
[i
].plugin
))
301 continue; /* it is so grab the next plugin */
302 strings
[count
] = filetypes
[i
].plugin
;
306 #ifndef HAVE_LCD_BITMAP
309 /* FIX: translation! */
310 gui_syncsplash(HZ
*2, (unsigned char *)"No viewers found");
314 menu
.flags
= MT_RETURN_ID
|MENU_HAS_DESC
|MENU_ITEM_COUNT(count
);
315 menu
.strings
= (const char**)strings
;
316 menu
.callback_and_desc
= &cb_and_desc
;
317 i
= do_menu(&menu
, NULL
);
319 return filetype_load_plugin(strings
[i
], (void*)current_file
);
323 int filetype_load_plugin(const char* plugin
, char* file
)
326 char plugin_name
[MAX_PATH
];
327 snprintf(plugin_name
, sizeof(plugin_name
), "%s/%s.%s",
328 VIEWERS_DIR
, plugin
, ROCK_EXTENSION
);
329 if ((fd
= open(plugin_name
,O_RDONLY
))>=0)
332 return plugin_load(plugin_name
,file
);
336 snprintf(plugin_name
, sizeof(plugin_name
), "%s/%s.%s",
337 PLUGIN_DIR
, plugin
, ROCK_EXTENSION
);
338 if ((fd
= open(plugin_name
,O_RDONLY
))>=0)
341 return plugin_load(plugin_name
,file
);