1 /*****************************************************************************
2 * osd.c - The OSD Menu core code.
3 *****************************************************************************
4 * Copyright (C) 2005-2007 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_image.h>
37 /*****************************************************************************
39 *****************************************************************************/
41 static void osd_UpdateState( osd_menu_state_t *, int, int, int, int, picture_t * );
42 static inline osd_state_t *osd_VolumeStateChange( osd_state_t *, int );
43 static int osd_VolumeStep( vlc_object_t *, int, int );
44 static vlc_bool_t osd_isVisible( osd_menu_t *p_osd );
45 static int osd_ParserLoad( vlc_object_t *, const char *, osd_menu_t ** );
46 static void osd_ParserUnload( vlc_object_t *, osd_menu_t ** );
48 static vlc_bool_t osd_isVisible( osd_menu_t *p_osd )
52 var_Get( p_osd, "osd-menu-visible", &val );
56 /*****************************************************************************
57 * Wrappers for loading and unloading osd parser modules.
58 *****************************************************************************/
59 static osd_menu_t *osd_ParserLoad( vlc_object_t *p_this, const char *psz_file )
63 p_menu = vlc_object_create( p_this, VLC_OBJECT_OSDMENU );
66 msg_Err( p_this, "out of memory" );
69 vlc_object_attach( p_this, p_menu );
71 /* Stuff needed for Parser */
72 p_menu->psz_file = strdup( psz_file );
73 p_menu->p_image = image_HandlerCreate( p_this );
74 if( !p_menu->p_image || !p_menu->psz_file )
76 msg_Err( p_this, "unable to load images, aborting .." );
77 osd_ParserUnload( p_this, p_menu );
83 char *psz_ext = strrchr( p_menu->psz_file, '.' );
85 if( psz_ext && !strcmp( psz_ext, ".cfg") )
86 psz_type = "import-osd";
88 psz_type = "import-osd-xml";
90 p_menu->p_parser = module_Need( p_menu, "osd parser",
92 if( !p_menu->p_parser )
94 osd_ParserUnload( p_this, pp_menu );
101 static void osd_ParserUnload( vlc_object_t *p_this, osd_menu_t *p_menu )
103 if( p_menu->p_parser )
105 module_Unneed( p_menu, p_menu->p_parser );
107 p_menu->p_parser = NULL;
109 if( p_menu->p_image )
110 image_HandlerDelete( p_menu->p_image );
111 if( p_menu->psz_file )
112 free( p_menu->psz_file );
114 vlc_object_detach( p_menu );
115 vlc_object_destroy( p_menu );
119 /*****************************************************************************
121 *****************************************************************************/
122 osd_menu_t *__osd_MenuCreate( vlc_object_t *p_this, const char *psz_file )
124 osd_menu_t *p_osd = NULL;
129 /* to be sure to avoid multiple creation */
130 var_Create( p_this->p_libvlc, "osd_mutex", VLC_VAR_MUTEX );
131 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
132 vlc_mutex_lock( lockval.p_address );
134 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
139 /* Parse configuration file */
140 p_osd = osd_ParserLoad( p_this, psz_file );
144 /* Setup default button (first button) */
145 p_osd->p_state->p_visible = p_osd->p_button;
146 p_osd->p_state->p_visible->p_current_state =
147 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
148 p_osd->i_width = p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch;
149 p_osd->i_height = p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines;
151 if( p_osd->p_state->p_volume )
153 /* Update the volume state images to match the current volume */
154 i_volume = config_GetInt( p_this, "volume" );
155 i_steps = osd_VolumeStep( p_this, i_volume, p_osd->p_state->p_volume->i_ranges );
156 p_osd->p_state->p_volume->p_current_state = osd_VolumeStateChange(
157 p_osd->p_state->p_volume->p_states, i_steps );
159 /* Initialize OSD state */
160 osd_UpdateState( p_osd->p_state, p_osd->i_x, p_osd->i_y,
161 p_osd->i_width, p_osd->i_height, NULL );
163 vlc_object_yield( p_osd );
164 vlc_object_attach( p_osd, p_this->p_libvlc );
166 /* Signal when an update of OSD menu is needed */
167 var_Create( p_osd, "osd-menu-update", VLC_VAR_BOOL );
168 var_Create( p_osd, "osd-menu-visible", VLC_VAR_BOOL );
170 val.b_bool = VLC_FALSE;
171 var_Set( p_osd, "osd-menu-update", val );
172 var_Set( p_osd, "osd-menu-visible", val );
174 vlc_mutex_unlock( lockval.p_address );
178 msg_Err( p_this, "creating OSD menu object failed" );
181 image_HandlerDelete( p_osd->p_image );
182 if( p_osd->psz_file )
183 free( p_osd->psz_file );
185 vlc_mutex_unlock( lockval.p_address );
186 vlc_object_destroy( p_osd );
187 vlc_mutex_unlock( lockval.p_address );
191 void __osd_MenuDelete( vlc_object_t *p_this, osd_menu_t *p_osd )
195 if( !p_osd || !p_this ) return;
197 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
198 vlc_mutex_lock( lockval.p_address );
200 vlc_object_release( p_osd );
201 if( p_osd->p_internals->i_refcount > 0 )
203 vlc_mutex_unlock( lockval.p_address );
207 var_Destroy( p_osd, "osd-menu-visible" );
208 var_Destroy( p_osd, "osd-menu-update" );
210 osd_ParserUnload( p_this, &p_osd );
212 vlc_mutex_unlock( lockval.p_address );
215 osd_state_t *__osd_StateChange( osd_state_t *p_states, const int i_state )
217 osd_state_t *p_current = p_states;
218 osd_state_t *p_temp = NULL;
221 for( i=0; p_current != NULL; i++ )
223 if( p_current->i_state == i_state )
225 p_temp = p_current->p_next;
231 /* The volume can be modified in another interface while the OSD Menu
232 * has not been instantiated yet. This routines updates the "volume OSD menu item"
233 * to reflect the current state of the GUI.
235 static inline osd_state_t *osd_VolumeStateChange( osd_state_t *p_current, int i_steps )
237 osd_state_t *p_temp = NULL;
240 if( i_steps < 0 ) i_steps = 0;
242 for( i=0; (i < i_steps) && (p_current != NULL); i++ )
244 p_temp = p_current->p_next;
245 if( !p_temp ) return p_current;
248 return (!p_temp) ? p_current : p_temp;
251 /* Update the state of the OSD Menu */
252 static void osd_UpdateState( osd_menu_state_t *p_state, int i_x, int i_y,
253 int i_width, int i_height, picture_t *p_pic )
257 p_state->i_width = i_width;
258 p_state->i_height = i_height;
259 p_state->p_pic = p_pic;
262 void __osd_MenuShow( vlc_object_t *p_this )
264 osd_menu_t *p_osd = NULL;
265 osd_button_t *p_button = NULL;
268 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
271 msg_Err( p_this, "osd_MenuNext failed" );
275 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
276 vlc_mutex_lock( lockval.p_address );
278 #if defined(OSD_MENU_DEBUG)
279 msg_Dbg( p_osd, "menu on" );
281 p_button = p_osd->p_state->p_visible;
284 if( !p_button->b_range )
285 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
286 p_osd->p_state->p_visible = p_osd->p_button;
288 if( !p_osd->p_state->p_visible->b_range )
289 p_osd->p_state->p_visible->p_current_state =
290 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
292 osd_UpdateState( p_osd->p_state,
293 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
294 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
295 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
296 p_osd->p_state->p_visible->p_current_state->p_pic );
297 osd_SetMenuUpdate( p_osd, VLC_TRUE );
299 osd_SetMenuVisible( p_osd, VLC_TRUE );
301 vlc_object_release( (vlc_object_t*) p_osd );
302 vlc_mutex_unlock( lockval.p_address );
305 void __osd_MenuHide( vlc_object_t *p_this )
307 osd_menu_t *p_osd = NULL;
310 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
313 msg_Err( p_this, "osd_MenuNext failed" );
317 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
318 vlc_mutex_lock( lockval.p_address );
320 #if defined(OSD_MENU_DEBUG)
321 msg_Dbg( p_osd, "menu off" );
323 osd_UpdateState( p_osd->p_state,
324 p_osd->p_state->i_x, p_osd->p_state->i_y,
326 osd_SetMenuUpdate( p_osd, VLC_TRUE );
328 vlc_object_release( (vlc_object_t*) p_osd );
329 vlc_mutex_unlock( lockval.p_address );
332 void __osd_MenuActivate( vlc_object_t *p_this )
334 osd_menu_t *p_osd = NULL;
335 osd_button_t *p_button = NULL;
338 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
341 msg_Err( p_this, "osd_MenuNext failed" );
345 if( osd_isVisible( p_osd ) == VLC_FALSE )
347 vlc_object_release( (vlc_object_t*) p_osd );
351 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
352 vlc_mutex_lock( lockval.p_address );
354 #if defined(OSD_MENU_DEBUG)
355 msg_Dbg( p_osd, "select" );
357 p_button = p_osd->p_state->p_visible;
359 * Is there a menu item above or below? If so, then select it.
361 if( p_button && p_button->p_up )
363 vlc_object_release( (vlc_object_t*) p_osd );
364 vlc_mutex_unlock( lockval.p_address );
365 __osd_MenuUp( p_this ); /* "menu select" means go to menu item above. */
368 if( p_button && p_button->p_down )
370 vlc_object_release( (vlc_object_t*) p_osd );
371 vlc_mutex_unlock( lockval.p_address );
372 __osd_MenuDown( p_this ); /* "menu select" means go to menu item below. */
376 if( p_button && !p_button->b_range )
378 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_PRESSED );
379 osd_UpdateState( p_osd->p_state,
380 p_button->i_x, p_button->i_y,
381 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
382 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
383 p_button->p_current_state->p_pic );
384 osd_SetMenuUpdate( p_osd, VLC_TRUE );
385 osd_SetMenuVisible( p_osd, VLC_TRUE );
386 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt( p_osd, p_button->psz_action ) );
387 #if defined(OSD_MENU_DEBUG)
388 msg_Dbg( p_osd, "select (%d, %s)", config_GetInt( p_osd, p_button->psz_action ), p_button->psz_action );
391 vlc_object_release( (vlc_object_t*) p_osd );
392 vlc_mutex_unlock( lockval.p_address );
395 void __osd_MenuNext( vlc_object_t *p_this )
397 osd_menu_t *p_osd = NULL;
398 osd_button_t *p_button = NULL;
401 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
404 msg_Err( p_this, "osd_MenuNext failed" );
408 if( osd_isVisible( p_osd ) == VLC_FALSE )
410 vlc_object_release( (vlc_object_t*) p_osd );
414 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
415 vlc_mutex_lock( lockval.p_address );
417 p_button = p_osd->p_state->p_visible;
420 if( !p_button->b_range )
421 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
422 if( p_button->p_next )
423 p_osd->p_state->p_visible = p_button->p_next;
425 p_osd->p_state->p_visible = p_osd->p_button;
427 if( !p_osd->p_state->p_visible->b_range )
428 p_osd->p_state->p_visible->p_current_state =
429 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
431 osd_UpdateState( p_osd->p_state,
432 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
433 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
434 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
435 p_osd->p_state->p_visible->p_current_state->p_pic );
436 osd_SetMenuUpdate( p_osd, VLC_TRUE );
438 #if defined(OSD_MENU_DEBUG)
439 msg_Dbg( p_osd, "direction right [button %s]", p_osd->p_state->p_visible->psz_action );
442 vlc_object_release( (vlc_object_t*) p_osd );
443 vlc_mutex_unlock( lockval.p_address );
446 void __osd_MenuPrev( vlc_object_t *p_this )
448 osd_menu_t *p_osd = NULL;
449 osd_button_t *p_button = NULL;
452 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
455 msg_Err( p_this, "osd_MenuPrev failed" );
459 if( osd_isVisible( p_osd ) == VLC_FALSE )
461 vlc_object_release( (vlc_object_t*) p_osd );
465 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
466 vlc_mutex_lock( lockval.p_address );
468 p_button = p_osd->p_state->p_visible;
471 if( !p_button->b_range )
472 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
473 if( p_button->p_prev )
474 p_osd->p_state->p_visible = p_button->p_prev;
476 p_osd->p_state->p_visible = p_osd->p_last_button;
478 if( !p_osd->p_state->p_visible->b_range )
479 p_osd->p_state->p_visible->p_current_state =
480 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
482 osd_UpdateState( p_osd->p_state,
483 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
484 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
485 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
486 p_osd->p_state->p_visible->p_current_state->p_pic );
487 osd_SetMenuUpdate( p_osd, VLC_TRUE );
489 #if defined(OSD_MENU_DEBUG)
490 msg_Dbg( p_osd, "direction left [button %s]", p_osd->p_state->p_visible->psz_action );
493 vlc_object_release( (vlc_object_t*) p_osd );
494 vlc_mutex_unlock( lockval.p_address );
497 void __osd_MenuUp( vlc_object_t *p_this )
499 osd_menu_t *p_osd = NULL;
500 osd_button_t *p_button = NULL;
502 #if defined(OSD_MENU_DEBUG)
505 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
508 msg_Err( p_this, "osd_MenuDown failed" );
512 if( osd_isVisible( p_osd ) == VLC_FALSE )
514 vlc_object_release( (vlc_object_t*) p_osd );
518 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
519 vlc_mutex_lock( lockval.p_address );
521 p_button = p_osd->p_state->p_visible;
524 if( !p_button->b_range )
526 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_SELECT );
528 p_osd->p_state->p_visible = p_button->p_up;
531 if( p_button->b_range && p_osd->p_state->p_visible->b_range )
533 osd_state_t *p_temp = p_osd->p_state->p_visible->p_current_state;
534 if( p_temp && p_temp->p_next )
535 p_osd->p_state->p_visible->p_current_state = p_temp->p_next;
537 else if( !p_osd->p_state->p_visible->b_range )
539 p_osd->p_state->p_visible->p_current_state =
540 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
543 osd_UpdateState( p_osd->p_state,
544 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
545 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
546 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
547 p_osd->p_state->p_visible->p_current_state->p_pic );
548 osd_SetMenuUpdate( p_osd, VLC_TRUE );
549 /* If this is a range style action with associated images of only one state,
550 * then perform "menu select" on every menu navigation
552 if( p_button->b_range )
554 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt(p_osd, p_button->psz_action) );
555 #if defined(OSD_MENU_DEBUG)
556 msg_Dbg( p_osd, "select (%d, %s)", val.i_int, p_button->psz_action );
560 #if defined(OSD_MENU_DEBUG)
561 msg_Dbg( p_osd, "direction up [button %s]", p_osd->p_state->p_visible->psz_action );
564 vlc_object_release( (vlc_object_t*) p_osd );
565 vlc_mutex_unlock( lockval.p_address );
568 void __osd_MenuDown( vlc_object_t *p_this )
570 osd_menu_t *p_osd = NULL;
571 osd_button_t *p_button = NULL;
573 #if defined(OSD_MENU_DEBUG)
577 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
580 msg_Err( p_this, "osd_MenuDown failed" );
584 if( osd_isVisible( p_osd ) == VLC_FALSE )
586 vlc_object_release( (vlc_object_t*) p_osd );
590 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
591 vlc_mutex_lock( lockval.p_address );
593 p_button = p_osd->p_state->p_visible;
596 if( !p_button->b_range )
598 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_SELECT );
599 if( p_button->p_down )
600 p_osd->p_state->p_visible = p_button->p_down;
603 if( p_button->b_range && p_osd->p_state->p_visible->b_range )
605 osd_state_t *p_temp = p_osd->p_state->p_visible->p_current_state;
606 if( p_temp && p_temp->p_prev )
607 p_osd->p_state->p_visible->p_current_state = p_temp->p_prev;
609 else if( !p_osd->p_state->p_visible->b_range )
611 p_osd->p_state->p_visible->p_current_state =
612 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
615 osd_UpdateState( p_osd->p_state,
616 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
617 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
618 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
619 p_osd->p_state->p_visible->p_current_state->p_pic );
620 osd_SetMenuUpdate( p_osd, VLC_TRUE );
621 /* If this is a range style action with associated images of only one state,
622 * then perform "menu select" on every menu navigation
624 if( p_button->b_range )
626 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt(p_osd, p_button->psz_action_down) );
627 #if defined(OSD_MENU_DEBUG)
628 msg_Dbg( p_osd, "select (%d, %s)", val.i_int, p_button->psz_action_down );
632 #if defined(OSD_MENU_DEBUG)
633 msg_Dbg( p_osd, "direction down [button %s]", p_osd->p_state->p_visible->psz_action );
636 vlc_object_release( (vlc_object_t*) p_osd );
637 vlc_mutex_unlock( lockval.p_address );
640 static int osd_VolumeStep( vlc_object_t *p_this, int i_volume, int i_steps )
642 int i_volume_step = 0;
645 i_volume_step = config_GetInt( p_this->p_libvlc, "volume-step" );
646 return (i_volume/i_volume_step);
650 * Display current audio volume bitmap
652 * The OSD Menu audio volume bar is updated to reflect the new audio volume. Call this function
653 * when the audio volume is updated outside the OSD menu command "menu up", "menu down" or "menu select".
655 void __osd_Volume( vlc_object_t *p_this )
657 osd_menu_t *p_osd = NULL;
658 osd_button_t *p_button = NULL;
663 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
666 msg_Err( p_this, "OSD menu volume update failed" );
670 if( p_osd->p_state && p_osd->p_state->p_volume )
672 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
673 vlc_mutex_lock( lockval.p_address );
675 p_button = p_osd->p_state->p_volume;
676 if( p_osd->p_state->p_volume )
677 p_osd->p_state->p_visible = p_osd->p_state->p_volume;
678 if( p_button && p_button->b_range )
680 /* Update the volume state images to match the current volume */
681 i_volume = config_GetInt( p_this, "volume" );
682 i_steps = osd_VolumeStep( p_this, i_volume, p_button->i_ranges );
683 p_button->p_current_state = osd_VolumeStateChange( p_button->p_states, i_steps );
685 osd_UpdateState( p_osd->p_state,
686 p_button->i_x, p_button->i_y,
687 p_button->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
688 p_button->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
689 p_button->p_current_state->p_pic );
690 osd_SetMenuUpdate( p_osd, VLC_TRUE );
691 osd_SetMenuVisible( p_osd, VLC_TRUE );
693 vlc_object_release( (vlc_object_t*) p_osd );
694 vlc_mutex_unlock( lockval.p_address );