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 *****************************************************************************/
34 #include <vlc_config.h>
37 #include <vlc_image.h>
39 #include <vlc_charset.h>
43 int osd_parser_simpleOpen( vlc_object_t *p_this );
45 /*****************************************************************************
46 * Simple parser open function
47 *****************************************************************************/
48 int osd_parser_simpleOpen( vlc_object_t *p_this )
50 osd_menu_t *p_menu = (osd_menu_t *) p_this;
51 osd_button_t *p_current = NULL; /* button currently processed */
52 osd_button_t *p_prev = NULL; /* previous processed button */
54 #define MAX_FILE_PATH 256
58 if( !p_menu ) return VLC_ENOOBJ;
60 msg_Dbg( p_this, "opening osdmenu definition file %s", p_menu->psz_file );
61 fd = utf8_fopen( p_menu->psz_file, "r" );
64 msg_Err( p_this, "failed to open osdmenu definition file %s",
74 char path[MAX_FILE_PATH] = "";
75 char *psz_path = NULL;
79 result = fscanf(fd, "%24s %255s", &action[0], &path[0] );
81 /* override images path ? */
82 psz_path = config_GetPsz( p_this, "osdmenu-file-path" );
85 /* psz_path is not null and therefor &path[0] cannot be NULL
86 * it might be null terminated.
88 strncpy( &path[0], psz_path, MAX_FILE_PATH );
92 /* NULL terminate before asking the length of path[] */
93 path[MAX_FILE_PATH-1] = '\0';
94 i_len = strlen(&path[0]);
95 if( i_len == MAX_FILE_PATH )
96 i_len--; /* truncate to prevent buffer overflow */
97 #if defined(WIN32) || defined(UNDER_CE)
98 if( (i_len > 0) && path[i_len] != '\\' )
101 if( (i_len > 0) && path[i_len] != '/' )
104 path[i_len+1] = '\0';
105 if( result == 0 || result == EOF )
107 msg_Dbg( p_this, "osdmenu dir %s", &path[0] );
110 p_menu = osd_MenuNew( p_menu, NULL, 0, 0 );
112 p_menu = osd_MenuNew( p_menu, &path[0], 0, 0 );
114 /* Peek for 'style' argument */
119 result = fscanf(fd, "%24s %24s", &cmd[0], &action[0] );
120 if( result == 0 || result == EOF )
123 msg_Dbg( p_this, "osdmenu %s %s", &cmd[0], &action[0] );
124 if( strncmp( &cmd[0], "style", 5 ) == 0 )
126 if( strncmp( &action[0], "default", 7) == 0 )
128 p_menu->i_style = OSD_MENU_STYLE_SIMPLE;
130 else if( strncmp( &action[0], "concat", 6) == 0 )
132 p_menu->i_style = OSD_MENU_STYLE_CONCAT;
137 result = fseek( fd, pos, SEEK_SET );
146 /* read successive lines */
149 osd_state_t *p_state_current = NULL; /* button state currently processed */
150 osd_state_t *p_state_prev = NULL; /* previous state processed button */
153 char action[25] = "";
160 result = fscanf( fd, "%24s %24s (%d,%d)", &cmd[0], &action[0], &i_x, &i_y );
163 if( strncmp( &cmd[0], "action", 6 ) != 0 )
165 msg_Dbg( p_this, " + %s hotkey=%s (%d,%d)", &cmd[0], &action[0], i_x, i_y );
168 p_current = osd_ButtonNew( &action[0], i_x, i_y );
173 p_prev->p_next = p_current;
175 p_menu->p_button = p_current;
176 p_current->p_prev = p_prev;
178 /* parse all states */
183 result = fscanf( fd, "\t%24s", &state[0] );
187 /* FIXME: We only parse one level deep now */
188 if( strncmp( &state[0], "action", 6 ) == 0 )
190 osd_button_t *p_up = NULL;
192 result = fscanf( fd, "%24s (%d,%d)", &action[0], &i_x, &i_y );
195 /* create new button */
196 p_up = osd_ButtonNew( &action[0], i_x, i_y );
200 p_up->p_down = p_current;
201 p_current->p_up = p_up;
202 msg_Dbg( p_this, " + (menu up) hotkey=%s (%d,%d)", &action[0], i_x, i_y );
203 /* Parse type state */
204 result = fscanf( fd, "\t%24s %24s", &cmd[0], &type[0] );
207 if( strncmp( &cmd[0], "type", 4 ) == 0 )
209 if( strncmp( &type[0], "volume", 6 ) == 0 )
211 p_menu->p_state->p_volume = p_up;
212 msg_Dbg( p_this, " + type=%s", &type[0] );
215 /* Parse range state */
216 result = fscanf( fd, "\t%24s", &state[0] );
219 /* Parse the range state */
220 if( strncmp( &state[0], "range", 5 ) == 0 )
222 osd_state_t *p_range_current = NULL; /* range state currently processed */
223 osd_state_t *p_range_prev = NULL; /* previous state processed range */
226 p_up->b_range = VLC_TRUE;
228 result = fscanf( fd, "\t%24s", &action[0] );
232 result = fscanf( fd, "\t%d", &i_index );
236 msg_Dbg( p_this, " + (menu up) hotkey down %s, file=%s%s",
237 &action[0], p_menu->psz_path, &file[0] );
239 free( p_up->psz_action_down );
240 p_up->psz_action_down = strdup( &action[0] );
242 /* Parse range contstruction :
251 result = fscanf( fd, "\t%255s", &file[0] );
254 if( strncmp( &file[0], "end", 3 ) == 0 )
257 p_range_prev = p_range_current;
259 if( p_menu->psz_path )
261 size_t i_path_size = strlen( p_menu->psz_path );
262 size_t i_file_size = strlen( &file[0] );
264 strncpy( &path[0], p_menu->psz_path, i_path_size );
265 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
266 path[ i_path_size + i_file_size ] = '\0';
268 p_range_current = osd_StateNew( p_menu, &path[0], "pressed" );
270 else /* absolute paths are used. */
271 p_range_current = osd_StateNew( p_menu, &file[0], "pressed" );
273 if( !p_range_current || !p_range_current->p_pic )
276 p_range_current->i_x = i_x;
277 p_range_current->i_y = i_y;
279 /* increment the number of ranges for this button */
283 p_range_prev->p_next = p_range_current;
285 p_up->p_states = p_range_current;
286 p_range_current->p_prev = p_range_prev;
288 msg_Dbg( p_this, " |- range=%d, file=%s%s",
290 p_menu->psz_path, &file[0] );
294 osd_state_t *p_range = NULL;
296 /* Find the default index for state range */
297 p_range = p_up->p_states;
298 while( (--i_index > 0) && p_range->p_next )
300 osd_state_t *p_temp = NULL;
301 p_temp = p_range->p_next;
304 p_up->p_current_state = p_range;
306 else p_up->p_current_state = p_up->p_states;
309 result = fscanf( fd, "\t%24s", &state[0] );
312 if( strncmp( &state[0], "end", 3 ) != 0 )
315 /* Continue at the beginning of the while() */
319 /* Parse the range state */
320 if( strncmp( &state[0], "range", 5 ) == 0 )
322 osd_state_t *p_range_current = NULL; /* range state currently processed */
323 osd_state_t *p_range_prev = NULL; /* previous state processed range */
326 p_current->b_range = VLC_TRUE;
328 result = fscanf( fd, "\t%24s", &action[0] );
332 result = fscanf( fd, "\t%d", &i_index );
336 msg_Dbg( p_this, " + hotkey down %s, file=%s%s",
337 &action[0], p_menu->psz_path, &file[0] );
338 free( p_current->psz_action_down );
339 p_current->psz_action_down = strdup( &action[0] );
341 /* Parse range contstruction :
350 result = fscanf( fd, "\t%255s", &file[0] );
353 if( strncmp( &file[0], "end", 3 ) == 0 )
356 p_range_prev = p_range_current;
358 if( p_menu->psz_path )
360 size_t i_path_size = strlen( p_menu->psz_path );
361 size_t i_file_size = strlen( &file[0] );
363 strncpy( &path[0], p_menu->psz_path, i_path_size );
364 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
365 path[ i_path_size + i_file_size ] = '\0';
367 p_range_current = osd_StateNew( p_menu, &path[0], "pressed" );
369 else /* absolute paths are used. */
370 p_range_current = osd_StateNew( p_menu, &file[0], "pressed" );
372 if( !p_range_current || !p_range_current->p_pic )
375 p_range_current->i_x = i_x;
376 p_range_current->i_y = i_y;
378 /* increment the number of ranges for this button */
379 p_current->i_ranges++;
382 p_range_prev->p_next = p_range_current;
384 p_current->p_states = p_range_current;
385 p_range_current->p_prev = p_range_prev;
387 msg_Dbg( p_this, " |- range=%d, file=%s%s",
389 p_menu->psz_path, &file[0] );
393 osd_state_t *p_range = NULL;
395 /* Find the default index for state range */
396 p_range = p_current->p_states;
397 while( (--i_index > 0) && p_range->p_next )
399 osd_state_t *p_temp = NULL;
400 p_temp = p_range->p_next;
403 p_current->p_current_state = p_range;
405 else p_current->p_current_state = p_current->p_states;
406 /* Continue at the beginning of the while() */
409 if( strncmp( &state[0], "end", 3 ) == 0 )
412 result = fscanf( fd, "\t%255s", &file[0] );
416 p_state_prev = p_state_current;
418 if( ( strncmp( ppsz_button_states[0], &state[0], strlen(ppsz_button_states[0]) ) != 0 ) &&
419 ( strncmp( ppsz_button_states[1], &state[0], strlen(ppsz_button_states[1]) ) != 0 ) &&
420 ( strncmp( ppsz_button_states[2], &state[0], strlen(ppsz_button_states[2]) ) != 0 ) )
422 msg_Err( p_this, "invalid button state %s for button %s "
423 "expected %u: unselect, select or pressed)",
424 &state[0], &action[0], (unsigned)strlen(&state[0]));
428 if( p_menu->psz_path )
430 size_t i_path_size = strlen( p_menu->psz_path );
431 size_t i_file_size = strlen( &file[0] );
433 strncpy( &path[0], p_menu->psz_path, i_path_size );
434 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
435 path[ i_path_size + i_file_size ] = '\0';
437 p_state_current = osd_StateNew( p_menu, &path[0], &state[0] );
439 else /* absolute paths are used. */
440 p_state_current = osd_StateNew( p_menu, &file[0], &state[0] );
442 if( !p_state_current || !p_state_current->p_pic )
445 p_state_current->i_x = i_x;
446 p_state_current->i_y = i_y;
449 p_state_prev->p_next = p_state_current;
451 p_current->p_states = p_state_current;
452 p_state_current->p_prev = p_state_prev;
454 msg_Dbg( p_this, " |- state=%s, file=%s%s", &state[0],
455 p_menu->psz_path, &file[0] );
457 p_current->p_current_state = p_current->p_states;
460 /* Find the last button and store its pointer.
461 * The OSD menu behaves like a roundrobin list.
463 p_current = p_menu->p_button;
464 while( p_current && p_current->p_next )
466 osd_button_t *p_temp = NULL;
467 p_temp = p_current->p_next;
470 p_menu->p_last_button = p_current;
476 msg_Err( p_menu, "parsing file failed (returned %d)", result );
477 osd_MenuFree( p_menu );