1 /*****************************************************************************
2 * simple.c - The OSD Menu simple parser code.
3 *****************************************************************************
4 * Copyright (C) 2005-2008 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 *****************************************************************************/
32 #include <vlc_common.h>
34 #include <vlc_config.h>
37 #include <vlc_image.h>
39 #include <vlc_charset.h>
45 int osd_parser_simpleOpen( vlc_object_t *p_this );
47 /*****************************************************************************
48 * Simple parser open function
49 *****************************************************************************/
50 int osd_parser_simpleOpen( vlc_object_t *p_this )
52 osd_menu_t *p_menu = (osd_menu_t *) p_this;
53 osd_button_t *p_current = NULL; /* button currently processed */
54 osd_button_t *p_prev = NULL; /* previous processed button */
59 if( !p_menu ) return VLC_ENOOBJ;
61 msg_Dbg( p_this, "opening osdmenu definition file %s", p_menu->psz_file );
62 fd = utf8_fopen( p_menu->psz_file, "r" );
65 msg_Err( p_this, "failed to open osdmenu definition file %s",
75 char path[PATH_MAX] = "";
76 char *psz_path = NULL;
80 result = fscanf(fd, "%24s %255s", &action[0], &path[0] );
82 /* override images path ? */
83 psz_path = config_GetPsz( p_this, "osdmenu-file-path" );
86 /* psz_path is not null and therefor &path[0] cannot be NULL
87 * it might be null terminated.
89 strncpy( &path[0], psz_path, PATH_MAX );
93 /* NULL terminate before asking the length of path[] */
94 path[PATH_MAX-1] = '\0';
95 i_len = strlen(&path[0]);
96 if( i_len == PATH_MAX )
97 i_len--; /* truncate to prevent buffer overflow */
98 #if defined(WIN32) || defined(UNDER_CE)
99 if( (i_len > 0) && path[i_len] != '\\' )
102 if( (i_len > 0) && path[i_len] != '/' )
105 path[i_len+1] = '\0';
106 if( result == 0 || result == EOF )
108 msg_Dbg( p_this, "osdmenu dir %s", &path[0] );
111 p_menu = osd_MenuNew( p_menu, NULL, 0, 0 );
113 p_menu = osd_MenuNew( p_menu, &path[0], 0, 0 );
115 /* Peek for 'style' argument */
120 result = fscanf(fd, "%24s %24s", &cmd[0], &action[0] );
121 if( result == 0 || result == EOF )
124 msg_Dbg( p_this, "osdmenu %s %s", &cmd[0], &action[0] );
125 if( strncmp( &cmd[0], "style", 5 ) == 0 )
127 if( strncmp( &action[0], "default", 7) == 0 )
129 p_menu->i_style = OSD_MENU_STYLE_SIMPLE;
131 else if( strncmp( &action[0], "concat", 6) == 0 )
133 p_menu->i_style = OSD_MENU_STYLE_CONCAT;
138 result = fseek( fd, pos, SEEK_SET );
147 /* read successive lines */
150 osd_state_t *p_state_current = NULL; /* button state currently processed */
151 osd_state_t *p_state_prev = NULL; /* previous state processed button */
154 char action[25] = "";
157 char path[PATH_MAX] = "";
161 result = fscanf( fd, "%24s %24s (%d,%d)", &cmd[0], &action[0], &i_x, &i_y );
164 if( strncmp( &cmd[0], "action", 6 ) != 0 )
166 msg_Dbg( p_this, " + %s hotkey=%s (%d,%d)", &cmd[0], &action[0], i_x, i_y );
169 p_current = osd_ButtonNew( &action[0], i_x, i_y );
174 p_prev->p_next = p_current;
176 p_menu->p_button = p_current;
177 p_current->p_prev = p_prev;
179 /* parse all states */
184 result = fscanf( fd, "\t%24s", &state[0] );
188 /* FIXME: We only parse one level deep now */
189 if( strncmp( &state[0], "action", 6 ) == 0 )
191 osd_button_t *p_up = NULL;
193 result = fscanf( fd, "%24s (%d,%d)", &action[0], &i_x, &i_y );
196 /* create new button */
197 p_up = osd_ButtonNew( &action[0], i_x, i_y );
201 p_up->p_down = p_current;
202 p_current->p_up = p_up;
203 msg_Dbg( p_this, " + (menu up) hotkey=%s (%d,%d)", &action[0], i_x, i_y );
204 /* Parse type state */
205 result = fscanf( fd, "\t%24s %24s", &cmd[0], &type[0] );
208 if( strncmp( &cmd[0], "type", 4 ) == 0 )
210 if( strncmp( &type[0], "volume", 6 ) == 0 )
212 p_menu->p_state->p_volume = p_up;
213 msg_Dbg( p_this, " + type=%s", &type[0] );
216 /* Parse range state */
217 result = fscanf( fd, "\t%24s", &state[0] );
220 /* Parse the range state */
221 if( strncmp( &state[0], "range", 5 ) == 0 )
223 osd_state_t *p_range_current = NULL; /* range state currently processed */
224 osd_state_t *p_range_prev = NULL; /* previous state processed range */
227 p_up->b_range = true;
229 result = fscanf( fd, "\t%24s", &action[0] );
233 result = fscanf( fd, "\t%d", &i_index );
237 msg_Dbg( p_this, " + (menu up) hotkey down %s, file=%s%s",
238 &action[0], p_menu->psz_path, &file[0] );
240 free( p_up->psz_action_down );
241 p_up->psz_action_down = strdup( &action[0] );
243 /* Parse range contstruction :
252 result = fscanf( fd, "\t%255s", &file[0] );
255 if( strncmp( &file[0], "end", 3 ) == 0 )
258 p_range_prev = p_range_current;
260 if( p_menu->psz_path )
262 size_t i_path_size = strlen( p_menu->psz_path );
263 size_t i_file_size = strlen( &file[0] );
265 if( (i_path_size + i_file_size >= PATH_MAX) ||
266 (i_path_size >= PATH_MAX) )
269 strncpy( &path[0], p_menu->psz_path, i_path_size );
270 strncpy( &path[i_path_size], &file[0],
271 PATH_MAX - (i_path_size + i_file_size) );
272 path[ i_path_size + i_file_size ] = '\0';
274 p_range_current = osd_StateNew( p_menu, &path[0], "pressed" );
276 else /* absolute paths are used. */
277 p_range_current = osd_StateNew( p_menu, &file[0], "pressed" );
279 if( !p_range_current )
282 if( !p_range_current->p_pic )
284 osd_StatesFree( p_menu, p_range_current );
288 p_range_current->i_x = i_x;
289 p_range_current->i_y = i_y;
291 /* increment the number of ranges for this button */
295 p_range_prev->p_next = p_range_current;
297 p_up->p_states = p_range_current;
298 p_range_current->p_prev = p_range_prev;
300 msg_Dbg( p_this, " |- range=%d, file=%s%s",
302 p_menu->psz_path, &file[0] );
306 osd_state_t *p_range = NULL;
308 /* Find the default index for state range */
309 p_range = p_up->p_states;
310 while( (--i_index > 0) && p_range->p_next )
312 osd_state_t *p_temp = NULL;
313 p_temp = p_range->p_next;
316 p_up->p_current_state = p_range;
318 else p_up->p_current_state = p_up->p_states;
321 result = fscanf( fd, "\t%24s", &state[0] );
324 if( strncmp( &state[0], "end", 3 ) != 0 )
327 /* Continue at the beginning of the while() */
331 /* Parse the range state */
332 if( strncmp( &state[0], "range", 5 ) == 0 )
334 osd_state_t *p_range_current = NULL; /* range state currently processed */
335 osd_state_t *p_range_prev = NULL; /* previous state processed range */
338 p_current->b_range = true;
340 result = fscanf( fd, "\t%24s", &action[0] );
344 result = fscanf( fd, "\t%d", &i_index );
348 msg_Dbg( p_this, " + hotkey down %s, file=%s%s",
349 &action[0], p_menu->psz_path, &file[0] );
350 free( p_current->psz_action_down );
351 p_current->psz_action_down = strdup( &action[0] );
353 /* Parse range contstruction :
362 result = fscanf( fd, "\t%255s", &file[0] );
365 if( strncmp( &file[0], "end", 3 ) == 0 )
368 p_range_prev = p_range_current;
370 if( p_menu->psz_path )
372 size_t i_path_size = strlen( p_menu->psz_path );
373 size_t i_file_size = strlen( &file[0] );
375 if( (i_path_size + i_file_size >= PATH_MAX) ||
376 (i_path_size >= PATH_MAX) )
379 strncpy( &path[0], p_menu->psz_path, i_path_size );
380 strncpy( &path[i_path_size], &file[0],
381 PATH_MAX - (i_path_size + i_file_size) );
382 path[ i_path_size + i_file_size ] = '\0';
384 p_range_current = osd_StateNew( p_menu, &path[0], "pressed" );
386 else /* absolute paths are used. */
387 p_range_current = osd_StateNew( p_menu, &file[0], "pressed" );
389 if( !p_range_current )
392 if( !p_range_current->p_pic )
394 osd_StatesFree( p_menu, p_range_current );
398 p_range_current->i_x = i_x;
399 p_range_current->i_y = i_y;
401 /* increment the number of ranges for this button */
402 p_current->i_ranges++;
405 p_range_prev->p_next = p_range_current;
407 p_current->p_states = p_range_current;
408 p_range_current->p_prev = p_range_prev;
410 msg_Dbg( p_this, " |- range=%d, file=%s%s",
412 p_menu->psz_path, &file[0] );
416 osd_state_t *p_range = NULL;
418 /* Find the default index for state range */
419 p_range = p_current->p_states;
420 while( (--i_index > 0) && p_range->p_next )
422 osd_state_t *p_temp = NULL;
423 p_temp = p_range->p_next;
426 p_current->p_current_state = p_range;
428 else p_current->p_current_state = p_current->p_states;
429 /* Continue at the beginning of the while() */
432 if( strncmp( &state[0], "end", 3 ) == 0 )
435 result = fscanf( fd, "\t%255s", &file[0] );
439 p_state_prev = p_state_current;
441 if( ( strncmp( ppsz_button_states[0], &state[0], strlen(ppsz_button_states[0]) ) != 0 ) &&
442 ( strncmp( ppsz_button_states[1], &state[0], strlen(ppsz_button_states[1]) ) != 0 ) &&
443 ( strncmp( ppsz_button_states[2], &state[0], strlen(ppsz_button_states[2]) ) != 0 ) )
445 msg_Err( p_this, "invalid button state %s for button %s "
446 "expected %u: unselect, select or pressed)",
447 &state[0], &action[0], (unsigned)strlen(&state[0]));
451 if( p_menu->psz_path )
453 size_t i_path_size = strlen( p_menu->psz_path );
454 size_t i_file_size = strlen( &file[0] );
456 if( (i_path_size + i_file_size >= PATH_MAX) ||
457 (i_path_size >= PATH_MAX) )
460 strncpy( &path[0], p_menu->psz_path, i_path_size );
461 strncpy( &path[i_path_size], &file[0],
462 PATH_MAX - (i_path_size + i_file_size) );
463 path[ i_path_size + i_file_size ] = '\0';
465 p_state_current = osd_StateNew( p_menu, &path[0], &state[0] );
467 else /* absolute paths are used. */
468 p_state_current = osd_StateNew( p_menu, &file[0], &state[0] );
470 if( !p_state_current )
473 if( !p_state_current->p_pic )
475 osd_StatesFree( p_menu, p_state_current );
479 p_state_current->i_x = i_x;
480 p_state_current->i_y = i_y;
483 p_state_prev->p_next = p_state_current;
485 p_current->p_states = p_state_current;
486 p_state_current->p_prev = p_state_prev;
488 msg_Dbg( p_this, " |- state=%s, file=%s%s", &state[0],
489 p_menu->psz_path, &file[0] );
491 p_current->p_current_state = p_current->p_states;
494 /* Find the last button and store its pointer.
495 * The OSD menu behaves like a roundrobin list.
497 p_current = p_menu->p_button;
498 while( p_current && p_current->p_next )
500 osd_button_t *p_temp = NULL;
501 p_temp = p_current->p_next;
504 p_menu->p_last_button = p_current;
509 msg_Err( p_menu, "parsing file failed (returned %d)", result );
510 osd_MenuFree( p_menu );