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 *****************************************************************************/
31 #include <vlc_config.h>
32 #include <vlc_video.h>
35 #include <vlc_image.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static const char *ppsz_button_states[] = { "unselect", "select", "pressed" };
46 /* OSD Menu structure support routines */
47 static osd_menu_t *osd_MenuNew( osd_menu_t *, const char *, int, int );
48 static osd_button_t *osd_ButtonNew( const char *, int, int );
49 static osd_state_t *osd_StateNew( vlc_object_t *, const char *, const char * );
51 static void osd_MenuFree ( vlc_object_t *, osd_menu_t * );
52 static void osd_ButtonFree( vlc_object_t *, osd_button_t * );
53 static void osd_StatesFree( vlc_object_t *, osd_state_t * );
55 static picture_t *osd_LoadImage( vlc_object_t *, const char *);
57 /*****************************************************************************
58 * osd_LoadImage: loads the logo image into memory
59 *****************************************************************************/
60 static picture_t *osd_LoadImage( vlc_object_t *p_this, const char *psz_filename )
62 picture_t *p_pic = NULL;
63 image_handler_t *p_image;
64 video_format_t fmt_in = {0}, fmt_out = {0};
66 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
67 p_image = image_HandlerCreate( p_this );
70 p_pic = image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
71 image_HandlerDelete( p_image );
73 p_pic = osd_YuvaYuvp( p_this, p_pic );
76 else msg_Err( p_this, "unable to handle this chroma" );
81 /*****************************************************************************
82 * Create a new Menu structure
83 *****************************************************************************/
84 static osd_menu_t *osd_MenuNew( osd_menu_t *p_menu, const char *psz_path, int i_x, int i_y )
86 if( !p_menu ) return NULL;
88 p_menu->p_state = (osd_menu_state_t *) malloc( sizeof( osd_menu_state_t ) );
89 if( !p_menu->p_state )
90 msg_Err( p_menu, "memory allocation for OSD Menu state failed." );
92 if( psz_path != NULL )
93 p_menu->psz_path = strdup( psz_path );
95 p_menu->psz_path = NULL;
102 /*****************************************************************************
104 *****************************************************************************/
105 static void osd_MenuFree( vlc_object_t *p_this, osd_menu_t *p_menu )
107 msg_Dbg( p_this, "freeing menu" );
108 osd_ButtonFree( p_this, p_menu->p_button );
109 p_menu->p_button = NULL;
110 p_menu->p_last_button = NULL;
111 if( p_menu->psz_path ) free( p_menu->psz_path );
112 p_menu->psz_path = NULL;
113 if( p_menu->p_state ) free( p_menu->p_state );
114 p_menu->p_state = NULL;
117 /*****************************************************************************
118 * Create a new button
119 *****************************************************************************/
120 static osd_button_t *osd_ButtonNew( const char *psz_action, int i_x, int i_y )
122 osd_button_t *p_button = NULL;
123 p_button = (osd_button_t*) malloc( sizeof(osd_button_t) );
127 memset( p_button, 0, sizeof(osd_button_t) );
128 p_button->psz_action = strdup(psz_action);
129 p_button->psz_action_down = NULL;
130 p_button->p_feedback = NULL;
137 /*****************************************************************************
139 *****************************************************************************/
140 static void osd_ButtonFree( vlc_object_t *p_this, osd_button_t *p_button )
142 osd_button_t *p_current = p_button;
143 osd_button_t *p_next = NULL;
144 osd_button_t *p_prev = NULL;
146 /* First walk to the end. */
147 while( p_current->p_next )
149 p_next = p_current->p_next;
152 /* Then free end first and walk to the start. */
153 while( p_current->p_prev )
155 msg_Dbg( p_this, "+ freeing button %s [%p]", p_current->psz_action, p_current );
156 p_prev = p_current->p_prev;
158 if( p_current->p_next )
160 if( p_current->p_next->psz_name )
161 free( p_current->p_next->psz_name );
162 if( p_current->p_next->psz_action )
163 free( p_current->p_next->psz_action );
164 if( p_current->p_next->psz_action_down )
165 free( p_current->p_next->psz_action_down );
166 if( p_current->p_feedback && p_current->p_feedback->p_data_orig )
167 free( p_current->p_feedback->p_data_orig );
168 if( p_current->p_feedback )
169 free( p_current->p_feedback );
171 p_current->p_next->psz_action_down = NULL;
172 p_current->p_next->psz_action = NULL;
173 p_current->p_next->psz_name = NULL;
174 p_current->p_feedback = NULL;
176 /* Free all states first */
177 if( p_current->p_next->p_states )
178 osd_StatesFree( p_this, p_current->p_next->p_states );
179 p_current->p_next->p_states = NULL;
180 if( p_current->p_next) free( p_current->p_next );
181 p_current->p_next = NULL;
184 if( p_current->p_up )
186 if( p_current->p_up->psz_name )
187 free( p_current->p_up->psz_name );
188 if( p_current->p_up->psz_action )
189 free( p_current->p_up->psz_action );
190 if( p_current->p_up->psz_action_down )
191 free( p_current->p_up->psz_action_down );
192 if( p_current->p_feedback && p_current->p_feedback->p_data_orig )
193 free( p_current->p_feedback->p_data_orig );
194 if( p_current->p_feedback )
195 free( p_current->p_feedback );
197 p_current->p_up->psz_action_down = NULL;
198 p_current->p_up->psz_action = NULL;
199 p_current->p_up->psz_name = NULL;
200 p_current->p_feedback = NULL;
202 /* Free all states first */
203 if( p_current->p_up->p_states )
204 osd_StatesFree( p_this, p_current->p_up->p_states );
205 p_current->p_up->p_states = NULL;
206 if( p_current->p_up ) free( p_current->p_up );
207 p_current->p_up = NULL;
210 /* Free the last one. */
213 msg_Dbg( p_this, "+ freeing button %s [%p]", p_button->psz_action, p_button );
214 if( p_button->psz_name ) free( p_button->psz_name );
215 if( p_button->psz_action ) free( p_button->psz_action );
216 if( p_button->psz_action_down ) free( p_button->psz_action_down );
217 if( p_current->p_feedback && p_current->p_feedback->p_data_orig )
218 free( p_current->p_feedback->p_data_orig );
219 if( p_current->p_feedback )
220 free( p_current->p_feedback );
222 p_button->psz_name = NULL;
223 p_button->psz_action = NULL;
224 p_button->psz_action_down = NULL;
225 p_current->p_feedback = NULL;
227 if( p_button->p_states )
228 osd_StatesFree( p_this, p_button->p_states );
229 p_button->p_states = NULL;
235 /*****************************************************************************
236 * Create a new state image
237 *****************************************************************************/
238 static osd_state_t *osd_StateNew( vlc_object_t *p_this, const char *psz_file, const char *psz_state )
240 osd_state_t *p_state = NULL;
241 p_state = (osd_state_t*) malloc( sizeof(osd_state_t) );
245 memset( p_state, 0, sizeof(osd_state_t) );
246 p_state->p_pic = osd_LoadImage( p_this, psz_file );
250 p_state->psz_state = strdup( psz_state );
251 if( strncmp( ppsz_button_states[0], psz_state, strlen(ppsz_button_states[0]) ) == 0 )
252 p_state->i_state = OSD_BUTTON_UNSELECT;
253 else if( strncmp( ppsz_button_states[1], psz_state, strlen(ppsz_button_states[1]) ) == 0 )
254 p_state->i_state = OSD_BUTTON_SELECT;
255 else if( strncmp( ppsz_button_states[2], psz_state, strlen(ppsz_button_states[2]) ) == 0 )
256 p_state->i_state = OSD_BUTTON_PRESSED;
261 /*****************************************************************************
263 *****************************************************************************/
264 static void osd_StatesFree( vlc_object_t *p_this, osd_state_t *p_states )
266 osd_state_t *p_state = p_states;
267 osd_state_t *p_next = NULL;
268 osd_state_t *p_prev = NULL;
270 while( p_state->p_next )
272 p_next = p_state->p_next;
275 /* Then free end first and walk to the start. */
276 while( p_state->p_prev )
278 msg_Dbg( p_this, " |- freeing state %s [%p]", p_state->psz_state, p_state );
279 p_prev = p_state->p_prev;
281 if( p_state->p_next )
283 if( p_state->p_next->p_pic && p_state->p_next->p_pic->p_data_orig )
284 free( p_state->p_next->p_pic->p_data_orig );
285 if( p_state->p_next->p_pic ) free( p_state->p_next->p_pic );
286 p_state->p_next->p_pic = NULL;
287 if( p_state->p_next->psz_state ) free( p_state->p_next->psz_state );
288 p_state->p_next->psz_state = NULL;
289 free( p_state->p_next );
290 p_state->p_next = NULL;
293 /* Free the last one. */
296 msg_Dbg( p_this, " |- freeing state %s [%p]", p_state->psz_state, p_states );
297 if( p_states->p_pic && p_states->p_pic->p_data_orig )
298 free( p_states->p_pic->p_data_orig );
299 if( p_states->p_pic ) free( p_states->p_pic );
300 p_states->p_pic = NULL;
301 if( p_state->psz_state ) free( p_state->psz_state );
302 p_state->psz_state = NULL;
308 /*****************************************************************************
309 * osd_ConfigLoader: Load and parse osd text configurationfile
310 *****************************************************************************/
311 int osd_ConfigLoader( vlc_object_t *p_this, const char *psz_file,
312 osd_menu_t **p_menu )
314 osd_button_t *p_current = NULL; /* button currently processed */
315 osd_button_t *p_prev = NULL; /* previous processed button */
317 #define MAX_FILE_PATH 256
321 msg_Dbg( p_this, "opening osd definition file %s", psz_file );
322 fd = fopen( psz_file, "r" );
325 msg_Err( p_this, "failed opening osd definition file %s", psz_file );
329 /* Read first line */
332 char action[25] = "";
333 char path[MAX_FILE_PATH] = "";
334 char *psz_path = NULL;
337 /* override images path ? */
338 psz_path = config_GetPsz( p_this, "osdmenu-file-path" );
339 if( psz_path == NULL )
341 result = fscanf(fd, "%24s %255s", &action[0], &path[0] );
345 /* psz_path is not null and therefor &path[0] cannot be NULL
346 * it might be null terminated.
348 strncpy( &path[0], psz_path, MAX_FILE_PATH );
352 /* NULL terminate before asking the length of path[] */
353 path[MAX_FILE_PATH-1] = '\0';
354 i_len = strlen(&path[0]);
355 if( i_len == MAX_FILE_PATH )
356 i_len--; /* truncate to prevent buffer overflow */
357 #if defined(WIN32) || defined(UNDER_CE)
358 if( (i_len > 0) && path[i_len] != '\\' )
361 if( (i_len > 0) && path[i_len] != '/' )
364 path[i_len+1] = '\0';
365 if( result == 0 || result == EOF )
367 msg_Dbg( p_this, "%s=%s", &action[0], &path[0] );
370 *p_menu = osd_MenuNew( *p_menu, NULL, 0, 0 );
372 *p_menu = osd_MenuNew( *p_menu, &path[0], 0, 0 );
378 /* read successive lines */
381 osd_state_t *p_state_current = NULL; /* button state currently processed */
382 osd_state_t *p_state_prev = NULL; /* previous state processed button */
385 char action[25] = "";
392 result = fscanf( fd, "%24s %24s (%d,%d)", &cmd[0], &action[0], &i_x, &i_y );
395 if( strncmp( &cmd[0], "action", 6 ) != 0 )
397 msg_Dbg( p_this, " + %s hotkey=%s (%d,%d)", &cmd[0], &action[0], i_x, i_y );
400 p_current = osd_ButtonNew( &action[0], i_x, i_y );
405 p_prev->p_next = p_current;
407 (*p_menu)->p_button = p_current;
408 p_current->p_prev = p_prev;
410 /* parse all states */
415 result = fscanf( fd, "\t%24s", &state[0] );
419 /* FIXME: We only parse one level deep now */
420 if( strncmp( &state[0], "action", 6 ) == 0 )
422 osd_button_t *p_up = NULL;
424 result = fscanf( fd, "%24s (%d,%d)", &action[0], &i_x, &i_y );
427 /* create new button */
428 p_up = osd_ButtonNew( &action[0], i_x, i_y );
432 p_up->p_down = p_current;
433 p_current->p_up = p_up;
434 msg_Dbg( p_this, " + (menu up) hotkey=%s (%d,%d)", &action[0], i_x, i_y );
435 /* Parse type state */
436 result = fscanf( fd, "\t%24s %24s", &cmd[0], &type[0] );
439 if( strncmp( &cmd[0], "type", 4 ) == 0 )
441 if( strncmp( &type[0], "volume", 6 ) == 0 )
443 (*p_menu)->p_state->p_volume = p_up;
444 msg_Dbg( p_this, " + type=%s", &type[0] );
447 /* Parse range state */
448 result = fscanf( fd, "\t%24s", &state[0] );
451 /* Parse the range state */
452 if( strncmp( &state[0], "range", 5 ) == 0 )
454 osd_state_t *p_range_current = NULL; /* range state currently processed */
455 osd_state_t *p_range_prev = NULL; /* previous state processed range */
458 p_up->b_range = VLC_TRUE;
460 result = fscanf( fd, "\t%24s", &action[0] );
464 result = fscanf( fd, "\t%d", &i_index );
468 msg_Dbg( p_this, " + (menu up) hotkey down %s, file=%s%s", &action[0], (*p_menu)->psz_path, &file[0] );
470 if( p_up->psz_action_down ) free( p_up->psz_action_down );
471 p_up->psz_action_down = strdup( &action[0] );
473 /* Parse range contstruction :
482 result = fscanf( fd, "\t%255s", &file[0] );
485 if( strncmp( &file[0], "end", 3 ) == 0 )
488 p_range_prev = p_range_current;
490 if( (*p_menu)->psz_path )
492 size_t i_path_size = strlen( (*p_menu)->psz_path );
493 size_t i_file_size = strlen( &file[0] );
495 strncpy( &path[0], (*p_menu)->psz_path, i_path_size );
496 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
497 path[ i_path_size + i_file_size ] = '\0';
499 p_range_current = osd_StateNew( p_this, &path[0], "pressed" );
501 else /* absolute paths are used. */
502 p_range_current = osd_StateNew( p_this, &file[0], "pressed" );
504 if( !p_range_current || !p_range_current->p_pic )
507 /* increment the number of ranges for this button */
511 p_range_prev->p_next = p_range_current;
513 p_up->p_states = p_range_current;
514 p_range_current->p_prev = p_range_prev;
516 msg_Dbg( p_this, " |- range=%d, file=%s%s",
518 (*p_menu)->psz_path, &file[0] );
522 osd_state_t *p_range = NULL;
524 /* Find the default index for state range */
525 p_range = p_up->p_states;
526 while( (--i_index > 0) && p_range->p_next )
528 osd_state_t *p_temp = NULL;
529 p_temp = p_range->p_next;
532 p_up->p_current_state = p_range;
534 else p_up->p_current_state = p_up->p_states;
537 result = fscanf( fd, "\t%24s", &state[0] );
540 if( strncmp( &state[0], "end", 3 ) != 0 )
543 /* Continue at the beginning of the while() */
547 /* Parse the range state */
548 if( strncmp( &state[0], "range", 5 ) == 0 )
550 osd_state_t *p_range_current = NULL; /* range state currently processed */
551 osd_state_t *p_range_prev = NULL; /* previous state processed range */
554 p_current->b_range = VLC_TRUE;
556 result = fscanf( fd, "\t%24s", &action[0] );
560 result = fscanf( fd, "\t%d", &i_index );
564 msg_Dbg( p_this, " + hotkey down %s, file=%s%s", &action[0], (*p_menu)->psz_path, &file[0] );
565 if( p_current->psz_action_down ) free( p_current->psz_action_down );
566 p_current->psz_action_down = strdup( &action[0] );
568 /* Parse range contstruction :
577 result = fscanf( fd, "\t%255s", &file[0] );
580 if( strncmp( &file[0], "end", 3 ) == 0 )
583 p_range_prev = p_range_current;
585 if( (*p_menu)->psz_path )
587 size_t i_path_size = strlen( (*p_menu)->psz_path );
588 size_t i_file_size = strlen( &file[0] );
590 strncpy( &path[0], (*p_menu)->psz_path, i_path_size );
591 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
592 path[ i_path_size + i_file_size ] = '\0';
594 p_range_current = osd_StateNew( p_this, &path[0], "pressed" );
596 else /* absolute paths are used. */
597 p_range_current = osd_StateNew( p_this, &file[0], "pressed" );
599 if( !p_range_current || !p_range_current->p_pic )
602 /* increment the number of ranges for this button */
603 p_current->i_ranges++;
606 p_range_prev->p_next = p_range_current;
608 p_current->p_states = p_range_current;
609 p_range_current->p_prev = p_range_prev;
611 msg_Dbg( p_this, " |- range=%d, file=%s%s",
613 (*p_menu)->psz_path, &file[0] );
617 osd_state_t *p_range = NULL;
619 /* Find the default index for state range */
620 p_range = p_current->p_states;
621 while( (--i_index > 0) && p_range->p_next )
623 osd_state_t *p_temp = NULL;
624 p_temp = p_range->p_next;
627 p_current->p_current_state = p_range;
629 else p_current->p_current_state = p_current->p_states;
630 /* Continue at the beginning of the while() */
633 if( strncmp( &state[0], "end", 3 ) == 0 )
636 result = fscanf( fd, "\t%255s", &file[0] );
640 p_state_prev = p_state_current;
642 if( ( strncmp( ppsz_button_states[0], &state[0], strlen(ppsz_button_states[0]) ) != 0 ) &&
643 ( strncmp( ppsz_button_states[1], &state[0], strlen(ppsz_button_states[1]) ) != 0 ) &&
644 ( strncmp( ppsz_button_states[2], &state[0], strlen(ppsz_button_states[2]) ) != 0 ) )
646 msg_Err( p_this, "invalid button state %s for button %s expected %d: unselect, select or pressed)",
647 &state[0], &action[0], strlen(&state[0]));
651 if( (*p_menu)->psz_path )
653 size_t i_path_size = strlen( (*p_menu)->psz_path );
654 size_t i_file_size = strlen( &file[0] );
656 strncpy( &path[0], (*p_menu)->psz_path, i_path_size );
657 strncpy( &path[i_path_size], &file[0], 512 - (i_path_size + i_file_size) );
658 path[ i_path_size + i_file_size ] = '\0';
660 p_state_current = osd_StateNew( p_this, &path[0], &state[0] );
662 else /* absolute paths are used. */
663 p_state_current = osd_StateNew( p_this, &file[0], &state[0] );
665 if( !p_state_current || !p_state_current->p_pic )
669 p_state_prev->p_next = p_state_current;
671 p_current->p_states = p_state_current;
672 p_state_current->p_prev = p_state_prev;
674 msg_Dbg( p_this, " |- state=%s, file=%s%s", &state[0], (*p_menu)->psz_path, &file[0] );
676 p_current->p_current_state = p_current->p_states;
679 /* Find the last button and store its pointer.
680 * The OSD menu behaves like a roundrobin list.
682 p_current = (*p_menu)->p_button;
683 while( p_current && p_current->p_next )
685 osd_button_t *p_temp = NULL;
686 p_temp = p_current->p_next;
689 (*p_menu)->p_last_button = p_current;
695 msg_Err( p_this, "parsing file failed (returned %d)", result );
700 /*****************************************************************************
701 * osd_ConfigUnload: Load and parse osd text configurationfile
702 *****************************************************************************/
703 void osd_ConfigUnload( vlc_object_t *p_this, osd_menu_t **p_osd)
705 msg_Dbg( p_this, "unloading OSD menu structure" );
706 osd_MenuFree( p_this, *p_osd );