1 /*****************************************************************************
2 * osd_parser.c - The OSD Menu parser core code.
3 *****************************************************************************
4 * Copyright (C) 2005 M2X
7 * Authors: Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
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_config.h>
35 #include <vlc_image.h>
37 #include <vlc_charset.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static const char *ppsz_button_states[] = { "unselect", "select", "pressed" };
47 /* OSD Menu structure support routines */
48 static osd_menu_t *osd_MenuNew( osd_menu_t *, const char *, int, int );
49 static osd_button_t *osd_ButtonNew( const char *, int, int );
50 static osd_state_t *osd_StateNew( vlc_object_t *, const char *, const char * );
52 static void osd_MenuFree ( vlc_object_t *, osd_menu_t * );
53 static void osd_ButtonFree( vlc_object_t *, osd_button_t * );
54 static void osd_StatesFree( vlc_object_t *, osd_state_t * );
56 static picture_t *osd_LoadImage( vlc_object_t *, const char *);
58 /*****************************************************************************
59 * osd_LoadImage: loads the logo image into memory
60 *****************************************************************************/
61 static picture_t *osd_LoadImage( vlc_object_t *p_this, const char *psz_filename )
63 picture_t *p_pic = NULL;
64 image_handler_t *p_image;
65 video_format_t fmt_in, fmt_out;
67 memset( &fmt_in, 0, sizeof(video_format_t) );
68 memset( &fmt_out, 0, sizeof(video_format_t) );
70 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
71 p_image = image_HandlerCreate( p_this );
74 p_pic = image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
75 image_HandlerDelete( p_image );
77 p_pic = osd_YuvaYuvp( p_this, p_pic );
80 else msg_Err( p_this, "unable to handle this chroma" );
85 /*****************************************************************************
86 * Create a new Menu structure
87 *****************************************************************************/
88 static osd_menu_t *osd_MenuNew( osd_menu_t *p_menu, const char *psz_path, int i_x, int i_y )
90 if( !p_menu ) return NULL;
92 p_menu->p_state = (osd_menu_state_t *) malloc( sizeof( osd_menu_state_t ) );
93 if( !p_menu->p_state )
94 msg_Err( p_menu, "Memory allocation for OSD Menu state failed" );
96 if( psz_path != NULL )
97 p_menu->psz_path = strdup( psz_path );
99 p_menu->psz_path = NULL;
106 /*****************************************************************************
108 *****************************************************************************/
109 static void osd_MenuFree( vlc_object_t *p_this, osd_menu_t *p_menu )
111 msg_Dbg( p_this, "freeing menu" );
112 osd_ButtonFree( p_this, p_menu->p_button );
113 p_menu->p_button = NULL;
114 p_menu->p_last_button = NULL;
115 if( p_menu->psz_path ) free( p_menu->psz_path );
116 p_menu->psz_path = NULL;
117 if( p_menu->p_state ) free( p_menu->p_state );
118 p_menu->p_state = NULL;
121 /*****************************************************************************
122 * Create a new button
123 *****************************************************************************/
124 static osd_button_t *osd_ButtonNew( const char *psz_action, int i_x, int i_y )
126 osd_button_t *p_button = NULL;
127 p_button = (osd_button_t*) malloc( sizeof(osd_button_t) );
131 memset( p_button, 0, sizeof(osd_button_t) );
132 p_button->psz_action = strdup(psz_action);
133 p_button->psz_action_down = NULL;
134 p_button->p_feedback = NULL;
141 /*****************************************************************************
143 *****************************************************************************/
144 static void osd_ButtonFree( vlc_object_t *p_this, osd_button_t *p_button )
146 osd_button_t *p_current = p_button;
147 osd_button_t *p_next = NULL;
148 osd_button_t *p_prev = NULL;
150 /* First walk to the end. */
151 while( p_current->p_next )
153 p_next = p_current->p_next;
156 /* Then free end first and walk to the start. */
157 while( p_current->p_prev )
159 msg_Dbg( p_this, "+ freeing button %s [%p]", p_current->psz_action, p_current );
160 p_prev = p_current->p_prev;
162 if( p_current->p_next )
164 if( p_current->p_next->psz_name )
165 free( p_current->p_next->psz_name );
166 if( p_current->p_next->psz_action )
167 free( p_current->p_next->psz_action );
168 if( p_current->p_next->psz_action_down )
169 free( p_current->p_next->psz_action_down );
170 if( p_current->p_feedback && p_current->p_feedback->p_data_orig )
171 free( p_current->p_feedback->p_data_orig );
172 if( p_current->p_feedback )
173 free( p_current->p_feedback );
175 p_current->p_next->psz_action_down = NULL;
176 p_current->p_next->psz_action = NULL;
177 p_current->p_next->psz_name = NULL;
178 p_current->p_feedback = NULL;
180 /* Free all states first */
181 if( p_current->p_next->p_states )
182 osd_StatesFree( p_this, p_current->p_next->p_states );
183 p_current->p_next->p_states = NULL;
184 if( p_current->p_next) free( p_current->p_next );
185 p_current->p_next = NULL;
188 if( p_current->p_up )
190 if( p_current->p_up->psz_name )
191 free( p_current->p_up->psz_name );
192 if( p_current->p_up->psz_action )
193 free( p_current->p_up->psz_action );
194 if( p_current->p_up->psz_action_down )
195 free( p_current->p_up->psz_action_down );
196 if( p_current->p_feedback && p_current->p_feedback->p_data_orig )
197 free( p_current->p_feedback->p_data_orig );
198 if( p_current->p_feedback )
199 free( p_current->p_feedback );
201 p_current->p_up->psz_action_down = NULL;
202 p_current->p_up->psz_action = NULL;
203 p_current->p_up->psz_name = NULL;
204 p_current->p_feedback = NULL;
206 /* Free all states first */
207 if( p_current->p_up->p_states )
208 osd_StatesFree( p_this, p_current->p_up->p_states );
209 p_current->p_up->p_states = NULL;
210 if( p_current->p_up ) free( p_current->p_up );
211 p_current->p_up = NULL;
214 /* Free the last one. */
217 msg_Dbg( p_this, "+ freeing button %s [%p]", p_button->psz_action, p_button );
218 if( p_button->psz_name ) free( p_button->psz_name );
219 if( p_button->psz_action ) free( p_button->psz_action );
220 if( p_button->psz_action_down ) free( p_button->psz_action_down );
221 if( p_current->p_feedback && p_current->p_feedback->p_data_orig )
222 free( p_current->p_feedback->p_data_orig );
223 if( p_current->p_feedback )
224 free( p_current->p_feedback );
226 p_button->psz_name = NULL;
227 p_button->psz_action = NULL;
228 p_button->psz_action_down = NULL;
229 p_current->p_feedback = NULL;
231 if( p_button->p_states )
232 osd_StatesFree( p_this, p_button->p_states );
233 p_button->p_states = NULL;
239 /*****************************************************************************
240 * Create a new state image
241 *****************************************************************************/
242 static osd_state_t *osd_StateNew( vlc_object_t *p_this, const char *psz_file, const char *psz_state )
244 osd_state_t *p_state = NULL;
245 p_state = (osd_state_t*) malloc( sizeof(osd_state_t) );
249 memset( p_state, 0, sizeof(osd_state_t) );
250 p_state->p_pic = osd_LoadImage( p_this, psz_file );
254 p_state->psz_state = strdup( psz_state );
255 if( strncmp( ppsz_button_states[0], psz_state, strlen(ppsz_button_states[0]) ) == 0 )
256 p_state->i_state = OSD_BUTTON_UNSELECT;
257 else if( strncmp( ppsz_button_states[1], psz_state, strlen(ppsz_button_states[1]) ) == 0 )
258 p_state->i_state = OSD_BUTTON_SELECT;
259 else if( strncmp( ppsz_button_states[2], psz_state, strlen(ppsz_button_states[2]) ) == 0 )
260 p_state->i_state = OSD_BUTTON_PRESSED;
265 /*****************************************************************************
267 *****************************************************************************/
268 static void osd_StatesFree( vlc_object_t *p_this, osd_state_t *p_states )
270 osd_state_t *p_state = p_states;
271 osd_state_t *p_next = NULL;
272 osd_state_t *p_prev = NULL;
274 while( p_state->p_next )
276 p_next = p_state->p_next;
279 /* Then free end first and walk to the start. */
280 while( p_state->p_prev )
282 msg_Dbg( p_this, " |- freeing state %s [%p]", p_state->psz_state, p_state );
283 p_prev = p_state->p_prev;
285 if( p_state->p_next )
287 if( p_state->p_next->p_pic && p_state->p_next->p_pic->p_data_orig )
288 free( p_state->p_next->p_pic->p_data_orig );
289 if( p_state->p_next->p_pic ) free( p_state->p_next->p_pic );
290 p_state->p_next->p_pic = NULL;
291 if( p_state->p_next->psz_state ) free( p_state->p_next->psz_state );
292 p_state->p_next->psz_state = NULL;
293 free( p_state->p_next );
294 p_state->p_next = NULL;
297 /* Free the last one. */
300 msg_Dbg( p_this, " |- freeing state %s [%p]", p_state->psz_state, p_states );
301 if( p_states->p_pic && p_states->p_pic->p_data_orig )
302 free( p_states->p_pic->p_data_orig );
303 if( p_states->p_pic ) free( p_states->p_pic );
304 p_states->p_pic = NULL;
305 if( p_state->psz_state ) free( p_state->psz_state );
306 p_state->psz_state = NULL;
312 /*****************************************************************************
313 * osd_ConfigLoader: Load and parse osd text configurationfile
314 *****************************************************************************/
315 int osd_ConfigLoader( vlc_object_t *p_this, const char *psz_file,
316 osd_menu_t **p_menu )
318 osd_button_t *p_current = NULL; /* button currently processed */
319 osd_button_t *p_prev = NULL; /* previous processed button */
321 #define MAX_FILE_PATH 256
325 msg_Dbg( p_this, "opening osd definition file %s", psz_file );
326 fd = utf8_fopen( psz_file, "r" );
329 msg_Err( p_this, "failed to open OSD definition file %s", psz_file );
333 /* Read first line */
336 char action[25] = "";
337 char path[MAX_FILE_PATH] = "";
338 char *psz_path = NULL;
341 /* override images path ? */
342 psz_path = config_GetPsz( p_this, "osdmenu-file-path" );
343 if( psz_path == NULL )
345 result = fscanf(fd, "%24s %255s", &action[0], &path[0] );
349 /* psz_path is not null and therefor &path[0] cannot be NULL
350 * it might be null terminated.
352 strncpy( &path[0], psz_path, MAX_FILE_PATH );
356 /* NULL terminate before asking the length of path[] */
357 path[MAX_FILE_PATH-1] = '\0';
358 i_len = strlen(&path[0]);
359 if( i_len == MAX_FILE_PATH )
360 i_len--; /* truncate to prevent buffer overflow */
361 #if defined(WIN32) || defined(UNDER_CE)
362 if( (i_len > 0) && path[i_len] != '\\' )
365 if( (i_len > 0) && path[i_len] != '/' )
368 path[i_len+1] = '\0';
369 if( result == 0 || result == EOF )
371 msg_Dbg( p_this, "%s=%s", &action[0], &path[0] );
374 *p_menu = osd_MenuNew( *p_menu, NULL, 0, 0 );
376 *p_menu = osd_MenuNew( *p_menu, &path[0], 0, 0 );
382 /* read successive lines */
385 osd_state_t *p_state_current = NULL; /* button state currently processed */
386 osd_state_t *p_state_prev = NULL; /* previous state processed button */
389 char action[25] = "";
396 result = fscanf( fd, "%24s %24s (%d,%d)", &cmd[0], &action[0], &i_x, &i_y );
399 if( strncmp( &cmd[0], "action", 6 ) != 0 )
401 msg_Dbg( p_this, " + %s hotkey=%s (%d,%d)", &cmd[0], &action[0], i_x, i_y );
404 p_current = osd_ButtonNew( &action[0], i_x, i_y );
409 p_prev->p_next = p_current;
411 (*p_menu)->p_button = p_current;
412 p_current->p_prev = p_prev;
414 /* parse all states */
419 result = fscanf( fd, "\t%24s", &state[0] );
423 /* FIXME: We only parse one level deep now */
424 if( strncmp( &state[0], "action", 6 ) == 0 )
426 osd_button_t *p_up = NULL;
428 result = fscanf( fd, "%24s (%d,%d)", &action[0], &i_x, &i_y );
431 /* create new button */
432 p_up = osd_ButtonNew( &action[0], i_x, i_y );
436 p_up->p_down = p_current;
437 p_current->p_up = p_up;
438 msg_Dbg( p_this, " + (menu up) hotkey=%s (%d,%d)", &action[0], i_x, i_y );
439 /* Parse type state */
440 result = fscanf( fd, "\t%24s %24s", &cmd[0], &type[0] );
443 if( strncmp( &cmd[0], "type", 4 ) == 0 )
445 if( strncmp( &type[0], "volume", 6 ) == 0 )
447 (*p_menu)->p_state->p_volume = p_up;
448 msg_Dbg( p_this, " + type=%s", &type[0] );
451 /* Parse range state */
452 result = fscanf( fd, "\t%24s", &state[0] );
455 /* Parse the range state */
456 if( strncmp( &state[0], "range", 5 ) == 0 )
458 osd_state_t *p_range_current = NULL; /* range state currently processed */
459 osd_state_t *p_range_prev = NULL; /* previous state processed range */
462 p_up->b_range = VLC_TRUE;
464 result = fscanf( fd, "\t%24s", &action[0] );
468 result = fscanf( fd, "\t%d", &i_index );
472 msg_Dbg( p_this, " + (menu up) hotkey down %s, file=%s%s", &action[0], (*p_menu)->psz_path, &file[0] );
474 if( p_up->psz_action_down ) free( p_up->psz_action_down );
475 p_up->psz_action_down = strdup( &action[0] );
477 /* Parse range contstruction :
486 result = fscanf( fd, "\t%255s", &file[0] );
489 if( strncmp( &file[0], "end", 3 ) == 0 )
492 p_range_prev = p_range_current;
494 if( (*p_menu)->psz_path )
496 size_t i_path_size = strlen( (*p_menu)->psz_path );
497 size_t i_file_size = strlen( &file[0] );
499 strncpy( &path[0], (*p_menu)->psz_path, i_path_size );
500 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
501 path[ i_path_size + i_file_size ] = '\0';
503 p_range_current = osd_StateNew( p_this, &path[0], "pressed" );
505 else /* absolute paths are used. */
506 p_range_current = osd_StateNew( p_this, &file[0], "pressed" );
508 if( !p_range_current || !p_range_current->p_pic )
511 /* increment the number of ranges for this button */
515 p_range_prev->p_next = p_range_current;
517 p_up->p_states = p_range_current;
518 p_range_current->p_prev = p_range_prev;
520 msg_Dbg( p_this, " |- range=%d, file=%s%s",
522 (*p_menu)->psz_path, &file[0] );
526 osd_state_t *p_range = NULL;
528 /* Find the default index for state range */
529 p_range = p_up->p_states;
530 while( (--i_index > 0) && p_range->p_next )
532 osd_state_t *p_temp = NULL;
533 p_temp = p_range->p_next;
536 p_up->p_current_state = p_range;
538 else p_up->p_current_state = p_up->p_states;
541 result = fscanf( fd, "\t%24s", &state[0] );
544 if( strncmp( &state[0], "end", 3 ) != 0 )
547 /* Continue at the beginning of the while() */
551 /* Parse the range state */
552 if( strncmp( &state[0], "range", 5 ) == 0 )
554 osd_state_t *p_range_current = NULL; /* range state currently processed */
555 osd_state_t *p_range_prev = NULL; /* previous state processed range */
558 p_current->b_range = VLC_TRUE;
560 result = fscanf( fd, "\t%24s", &action[0] );
564 result = fscanf( fd, "\t%d", &i_index );
568 msg_Dbg( p_this, " + hotkey down %s, file=%s%s", &action[0], (*p_menu)->psz_path, &file[0] );
569 if( p_current->psz_action_down ) free( p_current->psz_action_down );
570 p_current->psz_action_down = strdup( &action[0] );
572 /* Parse range contstruction :
581 result = fscanf( fd, "\t%255s", &file[0] );
584 if( strncmp( &file[0], "end", 3 ) == 0 )
587 p_range_prev = p_range_current;
589 if( (*p_menu)->psz_path )
591 size_t i_path_size = strlen( (*p_menu)->psz_path );
592 size_t i_file_size = strlen( &file[0] );
594 strncpy( &path[0], (*p_menu)->psz_path, i_path_size );
595 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
596 path[ i_path_size + i_file_size ] = '\0';
598 p_range_current = osd_StateNew( p_this, &path[0], "pressed" );
600 else /* absolute paths are used. */
601 p_range_current = osd_StateNew( p_this, &file[0], "pressed" );
603 if( !p_range_current || !p_range_current->p_pic )
606 /* increment the number of ranges for this button */
607 p_current->i_ranges++;
610 p_range_prev->p_next = p_range_current;
612 p_current->p_states = p_range_current;
613 p_range_current->p_prev = p_range_prev;
615 msg_Dbg( p_this, " |- range=%d, file=%s%s",
617 (*p_menu)->psz_path, &file[0] );
621 osd_state_t *p_range = NULL;
623 /* Find the default index for state range */
624 p_range = p_current->p_states;
625 while( (--i_index > 0) && p_range->p_next )
627 osd_state_t *p_temp = NULL;
628 p_temp = p_range->p_next;
631 p_current->p_current_state = p_range;
633 else p_current->p_current_state = p_current->p_states;
634 /* Continue at the beginning of the while() */
637 if( strncmp( &state[0], "end", 3 ) == 0 )
640 result = fscanf( fd, "\t%255s", &file[0] );
644 p_state_prev = p_state_current;
646 if( ( strncmp( ppsz_button_states[0], &state[0], strlen(ppsz_button_states[0]) ) != 0 ) &&
647 ( strncmp( ppsz_button_states[1], &state[0], strlen(ppsz_button_states[1]) ) != 0 ) &&
648 ( strncmp( ppsz_button_states[2], &state[0], strlen(ppsz_button_states[2]) ) != 0 ) )
650 msg_Err( p_this, "invalid button state %s for button %s expected %d: unselect, select or pressed)",
651 &state[0], &action[0], strlen(&state[0]));
655 if( (*p_menu)->psz_path )
657 size_t i_path_size = strlen( (*p_menu)->psz_path );
658 size_t i_file_size = strlen( &file[0] );
660 strncpy( &path[0], (*p_menu)->psz_path, i_path_size );
661 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
662 path[ i_path_size + i_file_size ] = '\0';
664 p_state_current = osd_StateNew( p_this, &path[0], &state[0] );
666 else /* absolute paths are used. */
667 p_state_current = osd_StateNew( p_this, &file[0], &state[0] );
669 if( !p_state_current || !p_state_current->p_pic )
673 p_state_prev->p_next = p_state_current;
675 p_current->p_states = p_state_current;
676 p_state_current->p_prev = p_state_prev;
678 msg_Dbg( p_this, " |- state=%s, file=%s%s", &state[0], (*p_menu)->psz_path, &file[0] );
680 p_current->p_current_state = p_current->p_states;
683 /* Find the last button and store its pointer.
684 * The OSD menu behaves like a roundrobin list.
686 p_current = (*p_menu)->p_button;
687 while( p_current && p_current->p_next )
689 osd_button_t *p_temp = NULL;
690 p_temp = p_current->p_next;
693 (*p_menu)->p_last_button = p_current;
699 msg_Err( p_this, "parsing file failed (returned %d)", result );
704 /*****************************************************************************
705 * osd_ConfigUnload: Load and parse osd text configurationfile
706 *****************************************************************************/
707 void osd_ConfigUnload( vlc_object_t *p_this, osd_menu_t **p_osd)
709 msg_Dbg( p_this, "unloading OSD menu structure" );
710 osd_MenuFree( p_this, *p_osd );