1 /*****************************************************************************
2 * simple.c - The OSD Menu simple parser code.
3 *****************************************************************************
4 * Copyright (C) 2005-2007 M2X
7 * Authors: Jean-Paul Saman
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc_config.h>
33 #include <vlc_image.h>
35 #include <vlc_charset.h>
39 int osd_parser_simpleOpen( vlc_object_t *p_this );
41 /*****************************************************************************
42 * Simple parser open function
43 *****************************************************************************/
44 int osd_parser_simpleOpen( vlc_object_t *p_this )
46 osd_menu_t *p_menu = (osd_menu_t *) p_this;
47 osd_button_t *p_current = NULL; /* button currently processed */
48 osd_button_t *p_prev = NULL; /* previous processed button */
50 #define MAX_FILE_PATH 256
54 if( !p_menu ) return VLC_ENOOBJ;
56 msg_Dbg( p_this, "opening osdmenu definition file %s", p_menu->psz_file );
57 fd = utf8_fopen( p_menu->psz_file, "r" );
60 msg_Err( p_this, "failed to open osdmenu definition file %s",
70 char path[MAX_FILE_PATH] = "";
71 char *psz_path = NULL;
75 result = fscanf(fd, "%24s %255s", &action[0], &path[0] );
77 /* override images path ? */
78 psz_path = config_GetPsz( p_this, "osdmenu-file-path" );
81 /* psz_path is not null and therefor &path[0] cannot be NULL
82 * it might be null terminated.
84 strncpy( &path[0], psz_path, MAX_FILE_PATH );
88 /* NULL terminate before asking the length of path[] */
89 path[MAX_FILE_PATH-1] = '\0';
90 i_len = strlen(&path[0]);
91 if( i_len == MAX_FILE_PATH )
92 i_len--; /* truncate to prevent buffer overflow */
93 #if defined(WIN32) || defined(UNDER_CE)
94 if( (i_len > 0) && path[i_len] != '\\' )
97 if( (i_len > 0) && path[i_len] != '/' )
100 path[i_len+1] = '\0';
101 if( result == 0 || result == EOF )
103 msg_Dbg( p_this, "osdmenu dir %s", &path[0] );
106 p_menu = osd_MenuNew( p_menu, NULL, 0, 0 );
108 p_menu = osd_MenuNew( p_menu, &path[0], 0, 0 );
110 /* Peek for 'style' argument */
115 result = fscanf(fd, "%24s %24s", &cmd[0], &action[0] );
116 if( result == 0 || result == EOF )
119 msg_Dbg( p_this, "osdmenu %s %s", &cmd[0], &action[0] );
120 if( strncmp( &cmd[0], "style", 5 ) == 0 )
122 if( strncmp( &action[0], "default", 7) == 0 )
124 p_menu->i_style = OSD_MENU_STYLE_SIMPLE;
126 else if( strncmp( &action[0], "concat", 6) == 0 )
128 p_menu->i_style = OSD_MENU_STYLE_CONCAT;
133 result = fseek( fd, pos, SEEK_SET );
142 /* read successive lines */
145 osd_state_t *p_state_current = NULL; /* button state currently processed */
146 osd_state_t *p_state_prev = NULL; /* previous state processed button */
149 char action[25] = "";
156 result = fscanf( fd, "%24s %24s (%d,%d)", &cmd[0], &action[0], &i_x, &i_y );
159 if( strncmp( &cmd[0], "action", 6 ) != 0 )
161 msg_Dbg( p_this, " + %s hotkey=%s (%d,%d)", &cmd[0], &action[0], i_x, i_y );
164 p_current = osd_ButtonNew( &action[0], i_x, i_y );
169 p_prev->p_next = p_current;
171 p_menu->p_button = p_current;
172 p_current->p_prev = p_prev;
174 /* parse all states */
179 result = fscanf( fd, "\t%24s", &state[0] );
183 /* FIXME: We only parse one level deep now */
184 if( strncmp( &state[0], "action", 6 ) == 0 )
186 osd_button_t *p_up = NULL;
188 result = fscanf( fd, "%24s (%d,%d)", &action[0], &i_x, &i_y );
191 /* create new button */
192 p_up = osd_ButtonNew( &action[0], i_x, i_y );
196 p_up->p_down = p_current;
197 p_current->p_up = p_up;
198 msg_Dbg( p_this, " + (menu up) hotkey=%s (%d,%d)", &action[0], i_x, i_y );
199 /* Parse type state */
200 result = fscanf( fd, "\t%24s %24s", &cmd[0], &type[0] );
203 if( strncmp( &cmd[0], "type", 4 ) == 0 )
205 if( strncmp( &type[0], "volume", 6 ) == 0 )
207 p_menu->p_state->p_volume = p_up;
208 msg_Dbg( p_this, " + type=%s", &type[0] );
211 /* Parse range state */
212 result = fscanf( fd, "\t%24s", &state[0] );
215 /* Parse the range state */
216 if( strncmp( &state[0], "range", 5 ) == 0 )
218 osd_state_t *p_range_current = NULL; /* range state currently processed */
219 osd_state_t *p_range_prev = NULL; /* previous state processed range */
222 p_up->b_range = VLC_TRUE;
224 result = fscanf( fd, "\t%24s", &action[0] );
228 result = fscanf( fd, "\t%d", &i_index );
232 msg_Dbg( p_this, " + (menu up) hotkey down %s, file=%s%s",
233 &action[0], p_menu->psz_path, &file[0] );
235 if( p_up->psz_action_down ) free( p_up->psz_action_down );
236 p_up->psz_action_down = strdup( &action[0] );
238 /* Parse range contstruction :
247 result = fscanf( fd, "\t%255s", &file[0] );
250 if( strncmp( &file[0], "end", 3 ) == 0 )
253 p_range_prev = p_range_current;
255 if( p_menu->psz_path )
257 size_t i_path_size = strlen( p_menu->psz_path );
258 size_t i_file_size = strlen( &file[0] );
260 strncpy( &path[0], p_menu->psz_path, i_path_size );
261 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
262 path[ i_path_size + i_file_size ] = '\0';
264 p_range_current = osd_StateNew( p_menu, &path[0], "pressed" );
266 else /* absolute paths are used. */
267 p_range_current = osd_StateNew( p_menu, &file[0], "pressed" );
269 if( !p_range_current || !p_range_current->p_pic )
272 /* increment the number of ranges for this button */
276 p_range_prev->p_next = p_range_current;
278 p_up->p_states = p_range_current;
279 p_range_current->p_prev = p_range_prev;
281 msg_Dbg( p_this, " |- range=%d, file=%s%s",
283 p_menu->psz_path, &file[0] );
287 osd_state_t *p_range = NULL;
289 /* Find the default index for state range */
290 p_range = p_up->p_states;
291 while( (--i_index > 0) && p_range->p_next )
293 osd_state_t *p_temp = NULL;
294 p_temp = p_range->p_next;
297 p_up->p_current_state = p_range;
299 else p_up->p_current_state = p_up->p_states;
302 result = fscanf( fd, "\t%24s", &state[0] );
305 if( strncmp( &state[0], "end", 3 ) != 0 )
308 /* Continue at the beginning of the while() */
312 /* Parse the range state */
313 if( strncmp( &state[0], "range", 5 ) == 0 )
315 osd_state_t *p_range_current = NULL; /* range state currently processed */
316 osd_state_t *p_range_prev = NULL; /* previous state processed range */
319 p_current->b_range = VLC_TRUE;
321 result = fscanf( fd, "\t%24s", &action[0] );
325 result = fscanf( fd, "\t%d", &i_index );
329 msg_Dbg( p_this, " + hotkey down %s, file=%s%s",
330 &action[0], p_menu->psz_path, &file[0] );
331 if( p_current->psz_action_down )
332 free( p_current->psz_action_down );
333 p_current->psz_action_down = strdup( &action[0] );
335 /* Parse range contstruction :
344 result = fscanf( fd, "\t%255s", &file[0] );
347 if( strncmp( &file[0], "end", 3 ) == 0 )
350 p_range_prev = p_range_current;
352 if( p_menu->psz_path )
354 size_t i_path_size = strlen( p_menu->psz_path );
355 size_t i_file_size = strlen( &file[0] );
357 strncpy( &path[0], p_menu->psz_path, i_path_size );
358 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
359 path[ i_path_size + i_file_size ] = '\0';
361 p_range_current = osd_StateNew( p_menu, &path[0], "pressed" );
363 else /* absolute paths are used. */
364 p_range_current = osd_StateNew( p_menu, &file[0], "pressed" );
366 if( !p_range_current || !p_range_current->p_pic )
369 /* increment the number of ranges for this button */
370 p_current->i_ranges++;
373 p_range_prev->p_next = p_range_current;
375 p_current->p_states = p_range_current;
376 p_range_current->p_prev = p_range_prev;
378 msg_Dbg( p_this, " |- range=%d, file=%s%s",
380 p_menu->psz_path, &file[0] );
384 osd_state_t *p_range = NULL;
386 /* Find the default index for state range */
387 p_range = p_current->p_states;
388 while( (--i_index > 0) && p_range->p_next )
390 osd_state_t *p_temp = NULL;
391 p_temp = p_range->p_next;
394 p_current->p_current_state = p_range;
396 else p_current->p_current_state = p_current->p_states;
397 /* Continue at the beginning of the while() */
400 if( strncmp( &state[0], "end", 3 ) == 0 )
403 result = fscanf( fd, "\t%255s", &file[0] );
407 p_state_prev = p_state_current;
409 if( ( strncmp( ppsz_button_states[0], &state[0], strlen(ppsz_button_states[0]) ) != 0 ) &&
410 ( strncmp( ppsz_button_states[1], &state[0], strlen(ppsz_button_states[1]) ) != 0 ) &&
411 ( strncmp( ppsz_button_states[2], &state[0], strlen(ppsz_button_states[2]) ) != 0 ) )
413 msg_Err( p_this, "invalid button state %s for button %s "
414 "expected %u: unselect, select or pressed)",
415 &state[0], &action[0], (unsigned)strlen(&state[0]));
419 if( p_menu->psz_path )
421 size_t i_path_size = strlen( p_menu->psz_path );
422 size_t i_file_size = strlen( &file[0] );
424 strncpy( &path[0], p_menu->psz_path, i_path_size );
425 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
426 path[ i_path_size + i_file_size ] = '\0';
428 p_state_current = osd_StateNew( p_menu, &path[0], &state[0] );
430 else /* absolute paths are used. */
431 p_state_current = osd_StateNew( p_menu, &file[0], &state[0] );
433 if( !p_state_current || !p_state_current->p_pic )
437 p_state_prev->p_next = p_state_current;
439 p_current->p_states = p_state_current;
440 p_state_current->p_prev = p_state_prev;
442 msg_Dbg( p_this, " |- state=%s, file=%s%s", &state[0],
443 p_menu->psz_path, &file[0] );
445 p_current->p_current_state = p_current->p_states;
448 /* Find the last button and store its pointer.
449 * The OSD menu behaves like a roundrobin list.
451 p_current = p_menu->p_button;
452 while( p_current && p_current->p_next )
454 osd_button_t *p_temp = NULL;
455 p_temp = p_current->p_next;
458 p_menu->p_last_button = p_current;
464 msg_Err( p_menu, "parsing file failed (returned %d)", result );
465 osd_MenuFree( p_menu );