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 p_range_current->i_x = i_x;
273 p_range_current->i_y = i_y;
275 /* increment the number of ranges for this button */
279 p_range_prev->p_next = p_range_current;
281 p_up->p_states = p_range_current;
282 p_range_current->p_prev = p_range_prev;
284 msg_Dbg( p_this, " |- range=%d, file=%s%s",
286 p_menu->psz_path, &file[0] );
290 osd_state_t *p_range = NULL;
292 /* Find the default index for state range */
293 p_range = p_up->p_states;
294 while( (--i_index > 0) && p_range->p_next )
296 osd_state_t *p_temp = NULL;
297 p_temp = p_range->p_next;
300 p_up->p_current_state = p_range;
302 else p_up->p_current_state = p_up->p_states;
305 result = fscanf( fd, "\t%24s", &state[0] );
308 if( strncmp( &state[0], "end", 3 ) != 0 )
311 /* Continue at the beginning of the while() */
315 /* Parse the range state */
316 if( strncmp( &state[0], "range", 5 ) == 0 )
318 osd_state_t *p_range_current = NULL; /* range state currently processed */
319 osd_state_t *p_range_prev = NULL; /* previous state processed range */
322 p_current->b_range = VLC_TRUE;
324 result = fscanf( fd, "\t%24s", &action[0] );
328 result = fscanf( fd, "\t%d", &i_index );
332 msg_Dbg( p_this, " + hotkey down %s, file=%s%s",
333 &action[0], p_menu->psz_path, &file[0] );
334 if( p_current->psz_action_down )
335 free( p_current->psz_action_down );
336 p_current->psz_action_down = strdup( &action[0] );
338 /* Parse range contstruction :
347 result = fscanf( fd, "\t%255s", &file[0] );
350 if( strncmp( &file[0], "end", 3 ) == 0 )
353 p_range_prev = p_range_current;
355 if( p_menu->psz_path )
357 size_t i_path_size = strlen( p_menu->psz_path );
358 size_t i_file_size = strlen( &file[0] );
360 strncpy( &path[0], p_menu->psz_path, i_path_size );
361 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
362 path[ i_path_size + i_file_size ] = '\0';
364 p_range_current = osd_StateNew( p_menu, &path[0], "pressed" );
366 else /* absolute paths are used. */
367 p_range_current = osd_StateNew( p_menu, &file[0], "pressed" );
369 if( !p_range_current || !p_range_current->p_pic )
372 p_range_current->i_x = i_x;
373 p_range_current->i_y = i_y;
375 /* increment the number of ranges for this button */
376 p_current->i_ranges++;
379 p_range_prev->p_next = p_range_current;
381 p_current->p_states = p_range_current;
382 p_range_current->p_prev = p_range_prev;
384 msg_Dbg( p_this, " |- range=%d, file=%s%s",
386 p_menu->psz_path, &file[0] );
390 osd_state_t *p_range = NULL;
392 /* Find the default index for state range */
393 p_range = p_current->p_states;
394 while( (--i_index > 0) && p_range->p_next )
396 osd_state_t *p_temp = NULL;
397 p_temp = p_range->p_next;
400 p_current->p_current_state = p_range;
402 else p_current->p_current_state = p_current->p_states;
403 /* Continue at the beginning of the while() */
406 if( strncmp( &state[0], "end", 3 ) == 0 )
409 result = fscanf( fd, "\t%255s", &file[0] );
413 p_state_prev = p_state_current;
415 if( ( strncmp( ppsz_button_states[0], &state[0], strlen(ppsz_button_states[0]) ) != 0 ) &&
416 ( strncmp( ppsz_button_states[1], &state[0], strlen(ppsz_button_states[1]) ) != 0 ) &&
417 ( strncmp( ppsz_button_states[2], &state[0], strlen(ppsz_button_states[2]) ) != 0 ) )
419 msg_Err( p_this, "invalid button state %s for button %s "
420 "expected %u: unselect, select or pressed)",
421 &state[0], &action[0], (unsigned)strlen(&state[0]));
425 if( p_menu->psz_path )
427 size_t i_path_size = strlen( p_menu->psz_path );
428 size_t i_file_size = strlen( &file[0] );
430 strncpy( &path[0], p_menu->psz_path, i_path_size );
431 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
432 path[ i_path_size + i_file_size ] = '\0';
434 p_state_current = osd_StateNew( p_menu, &path[0], &state[0] );
436 else /* absolute paths are used. */
437 p_state_current = osd_StateNew( p_menu, &file[0], &state[0] );
439 if( !p_state_current || !p_state_current->p_pic )
442 p_state_current->i_x = i_x;
443 p_state_current->i_y = i_y;
446 p_state_prev->p_next = p_state_current;
448 p_current->p_states = p_state_current;
449 p_state_current->p_prev = p_state_prev;
451 msg_Dbg( p_this, " |- state=%s, file=%s%s", &state[0],
452 p_menu->psz_path, &file[0] );
454 p_current->p_current_state = p_current->p_states;
457 /* Find the last button and store its pointer.
458 * The OSD menu behaves like a roundrobin list.
460 p_current = p_menu->p_button;
461 while( p_current && p_current->p_next )
463 osd_button_t *p_temp = NULL;
464 p_temp = p_current->p_next;
467 p_menu->p_last_button = p_current;
473 msg_Err( p_menu, "parsing file failed (returned %d)", result );
474 osd_MenuFree( p_menu );