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 *****************************************************************************/
32 #include <vlc_image.h>
36 /*****************************************************************************
38 *****************************************************************************/
40 static void osd_UpdateState( osd_menu_state_t *, int, int, int, int, picture_t * );
41 static inline osd_state_t *osd_VolumeStateChange( osd_state_t *, int );
42 static int osd_VolumeStep( vlc_object_t *, int, int );
43 static vlc_bool_t osd_isVisible( osd_menu_t *p_osd );
44 static int osd_ParserLoad( vlc_object_t *, const char *, osd_menu_t ** );
45 static void osd_ParserUnload( vlc_object_t *, osd_menu_t ** );
47 static vlc_bool_t osd_isVisible( osd_menu_t *p_osd )
51 var_Get( p_osd, "osd-menu-visible", &val );
55 /*****************************************************************************
56 * Wrappers for loading and unloading osd parser modules.
57 *****************************************************************************/
58 static int osd_ParserLoad( vlc_object_t *p_this, const char *psz_file, osd_menu_t **pp_menu )
60 osd_menu_t *p_menu = *pp_menu;
62 if( pp_menu && p_menu ) return VLC_EGENERIC;
64 p_menu = vlc_object_create( p_this, VLC_OBJECT_OSDMENU );
67 msg_Err( p_this, "out of memory" );
70 vlc_object_attach( p_this, p_menu );
72 /* Stuff needed for Parser */
73 p_menu->psz_file = strdup( psz_file );
74 p_menu->p_image = image_HandlerCreate( p_this );
75 if( !p_menu->p_image || !p_menu->psz_file )
77 msg_Err( p_this, "unable to load images, aborting .." );
78 osd_ParserUnload( p_this, pp_menu );
84 char *psz_ext = strrchr( p_menu->psz_file, '.' );
86 if( psz_ext && !strcmp( psz_ext, ".cfg") )
87 psz_type = "import-osd";
89 psz_type = "import-osd-xml";
91 p_menu->p_parser = module_Need( p_menu, "osd parser", psz_type, VLC_TRUE );
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 **pp_menu )
103 osd_menu_t *p_menu = (osd_menu_t *) *pp_menu;
105 if( p_menu->p_parser )
107 module_Unneed( p_menu, p_menu->p_parser );
109 p_menu->p_parser = NULL;
111 if( p_menu->p_image )
112 image_HandlerDelete( p_menu->p_image );
113 if( p_menu->psz_file )
114 free( p_menu->psz_file );
116 vlc_object_detach( p_menu );
117 vlc_object_destroy( p_menu );
121 /*****************************************************************************
123 *****************************************************************************/
124 osd_menu_t *__osd_MenuCreate( vlc_object_t *p_this, const char *psz_file )
126 osd_menu_t *p_osd = NULL;
131 /* to be sure to avoid multiple creation */
132 var_Create( p_this->p_libvlc, "osd_mutex", VLC_VAR_MUTEX );
133 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
134 vlc_mutex_lock( lockval.p_address );
136 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
141 /* Parse configuration file */
142 if( osd_ParserLoad( p_this, psz_file, &p_osd ) )
145 /* Setup default button (first button) */
146 p_osd->p_state->p_visible = p_osd->p_button;
147 p_osd->p_state->p_visible->p_current_state =
148 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
149 p_osd->i_width = p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch;
150 p_osd->i_height = p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines;
152 if( p_osd->p_state->p_volume )
154 /* Update the volume state images to match the current volume */
155 i_volume = config_GetInt( p_this, "volume" );
156 i_steps = osd_VolumeStep( p_this, i_volume, p_osd->p_state->p_volume->i_ranges );
157 p_osd->p_state->p_volume->p_current_state = osd_VolumeStateChange(
158 p_osd->p_state->p_volume->p_states, i_steps );
160 /* Initialize OSD state */
161 osd_UpdateState( p_osd->p_state, p_osd->i_x, p_osd->i_y,
162 p_osd->i_width, p_osd->i_height, NULL );
164 vlc_object_yield( p_osd );
165 vlc_object_attach( p_osd, p_this->p_libvlc );
167 /* Signal when an update of OSD menu is needed */
168 var_Create( p_osd, "osd-menu-update", VLC_VAR_BOOL );
169 var_Create( p_osd, "osd-menu-visible", VLC_VAR_BOOL );
171 val.b_bool = VLC_FALSE;
172 var_Set( p_osd, "osd-menu-update", val );
173 var_Set( p_osd, "osd-menu-visible", val );
175 vlc_mutex_unlock( lockval.p_address );
179 msg_Err( p_this, "creating OSD menu object failed" );
182 image_HandlerDelete( p_osd->p_image );
183 if( p_osd->psz_file )
184 free( p_osd->psz_file );
186 vlc_mutex_unlock( lockval.p_address );
187 vlc_object_destroy( p_osd );
188 vlc_mutex_unlock( lockval.p_address );
192 void __osd_MenuDelete( vlc_object_t *p_this, osd_menu_t *p_osd )
196 if( !p_osd || !p_this ) return;
198 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
199 vlc_mutex_lock( lockval.p_address );
201 vlc_object_release( p_osd );
202 if( p_osd->p_internals->i_refcount > 0 )
204 vlc_mutex_unlock( lockval.p_address );
208 var_Destroy( p_osd, "osd-menu-visible" );
209 var_Destroy( p_osd, "osd-menu-update" );
211 osd_ParserUnload( p_this, &p_osd );
213 vlc_mutex_unlock( lockval.p_address );
216 osd_state_t *__osd_StateChange( osd_state_t *p_states, const int i_state )
218 osd_state_t *p_current = p_states;
219 osd_state_t *p_temp = NULL;
222 for( i=0; p_current != NULL; i++ )
224 if( p_current->i_state == i_state )
226 p_temp = p_current->p_next;
232 /* The volume can be modified in another interface while the OSD Menu
233 * has not been instantiated yet. This routines updates the "volume OSD menu item"
234 * to reflect the current state of the GUI.
236 static inline osd_state_t *osd_VolumeStateChange( osd_state_t *p_current, int i_steps )
238 osd_state_t *p_temp = NULL;
241 if( i_steps < 0 ) i_steps = 0;
243 for( i=0; (i < i_steps) && (p_current != NULL); i++ )
245 p_temp = p_current->p_next;
246 if( !p_temp ) return p_current;
249 return (!p_temp) ? p_current : p_temp;
252 /* Update the state of the OSD Menu */
253 static void osd_UpdateState( osd_menu_state_t *p_state, int i_x, int i_y,
254 int i_width, int i_height, picture_t *p_pic )
258 p_state->i_width = i_width;
259 p_state->i_height = i_height;
260 p_state->p_pic = p_pic;
263 void __osd_MenuShow( vlc_object_t *p_this )
265 osd_menu_t *p_osd = NULL;
266 osd_button_t *p_button = NULL;
269 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
272 msg_Err( p_this, "osd_MenuNext failed" );
276 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
277 vlc_mutex_lock( lockval.p_address );
279 #if defined(OSD_MENU_DEBUG)
280 msg_Dbg( p_osd, "menu on" );
282 p_button = p_osd->p_state->p_visible;
285 if( !p_button->b_range )
286 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
287 p_osd->p_state->p_visible = p_osd->p_button;
289 if( !p_osd->p_state->p_visible->b_range )
290 p_osd->p_state->p_visible->p_current_state =
291 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
293 osd_UpdateState( p_osd->p_state,
294 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
295 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
296 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
297 p_osd->p_state->p_visible->p_current_state->p_pic );
298 osd_SetMenuUpdate( p_osd, VLC_TRUE );
300 osd_SetMenuVisible( p_osd, VLC_TRUE );
302 vlc_object_release( (vlc_object_t*) p_osd );
303 vlc_mutex_unlock( lockval.p_address );
306 void __osd_MenuHide( vlc_object_t *p_this )
308 osd_menu_t *p_osd = NULL;
311 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
314 msg_Err( p_this, "osd_MenuNext failed" );
318 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
319 vlc_mutex_lock( lockval.p_address );
321 #if defined(OSD_MENU_DEBUG)
322 msg_Dbg( p_osd, "menu off" );
324 osd_UpdateState( p_osd->p_state,
325 p_osd->p_state->i_x, p_osd->p_state->i_y,
327 osd_SetMenuUpdate( p_osd, VLC_TRUE );
329 vlc_object_release( (vlc_object_t*) p_osd );
330 vlc_mutex_unlock( lockval.p_address );
333 void __osd_MenuActivate( vlc_object_t *p_this )
335 osd_menu_t *p_osd = NULL;
336 osd_button_t *p_button = NULL;
339 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
342 msg_Err( p_this, "osd_MenuNext failed" );
346 if( osd_isVisible( p_osd ) == VLC_FALSE )
348 vlc_object_release( (vlc_object_t*) p_osd );
352 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
353 vlc_mutex_lock( lockval.p_address );
355 #if defined(OSD_MENU_DEBUG)
356 msg_Dbg( p_osd, "select" );
358 p_button = p_osd->p_state->p_visible;
360 * Is there a menu item above or below? If so, then select it.
362 if( p_button && p_button->p_up )
364 vlc_object_release( (vlc_object_t*) p_osd );
365 vlc_mutex_unlock( lockval.p_address );
366 __osd_MenuUp( p_this ); /* "menu select" means go to menu item above. */
369 if( p_button && p_button->p_down )
371 vlc_object_release( (vlc_object_t*) p_osd );
372 vlc_mutex_unlock( lockval.p_address );
373 __osd_MenuDown( p_this ); /* "menu select" means go to menu item below. */
377 if( p_button && !p_button->b_range )
379 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_PRESSED );
380 osd_UpdateState( p_osd->p_state,
381 p_button->i_x, p_button->i_y,
382 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
383 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
384 p_button->p_current_state->p_pic );
385 osd_SetMenuUpdate( p_osd, VLC_TRUE );
386 osd_SetMenuVisible( p_osd, VLC_TRUE );
387 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt( p_osd, p_button->psz_action ) );
388 #if defined(OSD_MENU_DEBUG)
389 msg_Dbg( p_osd, "select (%d, %s)", config_GetInt( p_osd, p_button->psz_action ), p_button->psz_action );
392 vlc_object_release( (vlc_object_t*) p_osd );
393 vlc_mutex_unlock( lockval.p_address );
396 void __osd_MenuNext( vlc_object_t *p_this )
398 osd_menu_t *p_osd = NULL;
399 osd_button_t *p_button = NULL;
402 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
405 msg_Err( p_this, "osd_MenuNext failed" );
409 if( osd_isVisible( p_osd ) == VLC_FALSE )
411 vlc_object_release( (vlc_object_t*) p_osd );
415 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
416 vlc_mutex_lock( lockval.p_address );
418 p_button = p_osd->p_state->p_visible;
421 if( !p_button->b_range )
422 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
423 if( p_button->p_next )
424 p_osd->p_state->p_visible = p_button->p_next;
426 p_osd->p_state->p_visible = p_osd->p_button;
428 if( !p_osd->p_state->p_visible->b_range )
429 p_osd->p_state->p_visible->p_current_state =
430 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
432 osd_UpdateState( p_osd->p_state,
433 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
434 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
435 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
436 p_osd->p_state->p_visible->p_current_state->p_pic );
437 osd_SetMenuUpdate( p_osd, VLC_TRUE );
439 #if defined(OSD_MENU_DEBUG)
440 msg_Dbg( p_osd, "direction right [button %s]", p_osd->p_state->p_visible->psz_action );
443 vlc_object_release( (vlc_object_t*) p_osd );
444 vlc_mutex_unlock( lockval.p_address );
447 void __osd_MenuPrev( vlc_object_t *p_this )
449 osd_menu_t *p_osd = NULL;
450 osd_button_t *p_button = NULL;
453 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
456 msg_Err( p_this, "osd_MenuPrev failed" );
460 if( osd_isVisible( p_osd ) == VLC_FALSE )
462 vlc_object_release( (vlc_object_t*) p_osd );
466 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
467 vlc_mutex_lock( lockval.p_address );
469 p_button = p_osd->p_state->p_visible;
472 if( !p_button->b_range )
473 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
474 if( p_button->p_prev )
475 p_osd->p_state->p_visible = p_button->p_prev;
477 p_osd->p_state->p_visible = p_osd->p_last_button;
479 if( !p_osd->p_state->p_visible->b_range )
480 p_osd->p_state->p_visible->p_current_state =
481 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
483 osd_UpdateState( p_osd->p_state,
484 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
485 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
486 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
487 p_osd->p_state->p_visible->p_current_state->p_pic );
488 osd_SetMenuUpdate( p_osd, VLC_TRUE );
490 #if defined(OSD_MENU_DEBUG)
491 msg_Dbg( p_osd, "direction left [button %s]", p_osd->p_state->p_visible->psz_action );
494 vlc_object_release( (vlc_object_t*) p_osd );
495 vlc_mutex_unlock( lockval.p_address );
498 void __osd_MenuUp( vlc_object_t *p_this )
500 osd_menu_t *p_osd = NULL;
501 osd_button_t *p_button = NULL;
503 #if defined(OSD_MENU_DEBUG)
506 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
509 msg_Err( p_this, "osd_MenuDown failed" );
513 if( osd_isVisible( p_osd ) == VLC_FALSE )
515 vlc_object_release( (vlc_object_t*) p_osd );
519 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
520 vlc_mutex_lock( lockval.p_address );
522 p_button = p_osd->p_state->p_visible;
525 if( !p_button->b_range )
527 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_SELECT );
529 p_osd->p_state->p_visible = p_button->p_up;
532 if( p_button->b_range && p_osd->p_state->p_visible->b_range )
534 osd_state_t *p_temp = p_osd->p_state->p_visible->p_current_state;
535 if( p_temp && p_temp->p_next )
536 p_osd->p_state->p_visible->p_current_state = p_temp->p_next;
538 else if( !p_osd->p_state->p_visible->b_range )
540 p_osd->p_state->p_visible->p_current_state =
541 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
544 osd_UpdateState( p_osd->p_state,
545 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
546 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
547 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
548 p_osd->p_state->p_visible->p_current_state->p_pic );
549 osd_SetMenuUpdate( p_osd, VLC_TRUE );
550 /* If this is a range style action with associated images of only one state,
551 * then perform "menu select" on every menu navigation
553 if( p_button->b_range )
555 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt(p_osd, p_button->psz_action) );
556 #if defined(OSD_MENU_DEBUG)
557 msg_Dbg( p_osd, "select (%d, %s)", val.i_int, p_button->psz_action );
561 #if defined(OSD_MENU_DEBUG)
562 msg_Dbg( p_osd, "direction up [button %s]", p_osd->p_state->p_visible->psz_action );
565 vlc_object_release( (vlc_object_t*) p_osd );
566 vlc_mutex_unlock( lockval.p_address );
569 void __osd_MenuDown( vlc_object_t *p_this )
571 osd_menu_t *p_osd = NULL;
572 osd_button_t *p_button = NULL;
574 #if defined(OSD_MENU_DEBUG)
578 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
581 msg_Err( p_this, "osd_MenuDown failed" );
585 if( osd_isVisible( p_osd ) == VLC_FALSE )
587 vlc_object_release( (vlc_object_t*) p_osd );
591 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
592 vlc_mutex_lock( lockval.p_address );
594 p_button = p_osd->p_state->p_visible;
597 if( !p_button->b_range )
599 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_SELECT );
600 if( p_button->p_down )
601 p_osd->p_state->p_visible = p_button->p_down;
604 if( p_button->b_range && p_osd->p_state->p_visible->b_range )
606 osd_state_t *p_temp = p_osd->p_state->p_visible->p_current_state;
607 if( p_temp && p_temp->p_prev )
608 p_osd->p_state->p_visible->p_current_state = p_temp->p_prev;
610 else if( !p_osd->p_state->p_visible->b_range )
612 p_osd->p_state->p_visible->p_current_state =
613 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
616 osd_UpdateState( p_osd->p_state,
617 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
618 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
619 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
620 p_osd->p_state->p_visible->p_current_state->p_pic );
621 osd_SetMenuUpdate( p_osd, VLC_TRUE );
622 /* If this is a range style action with associated images of only one state,
623 * then perform "menu select" on every menu navigation
625 if( p_button->b_range )
627 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt(p_osd, p_button->psz_action_down) );
628 #if defined(OSD_MENU_DEBUG)
629 msg_Dbg( p_osd, "select (%d, %s)", val.i_int, p_button->psz_action_down );
633 #if defined(OSD_MENU_DEBUG)
634 msg_Dbg( p_osd, "direction down [button %s]", p_osd->p_state->p_visible->psz_action );
637 vlc_object_release( (vlc_object_t*) p_osd );
638 vlc_mutex_unlock( lockval.p_address );
641 static int osd_VolumeStep( vlc_object_t *p_this, int i_volume, int i_steps )
643 int i_volume_step = 0;
646 i_volume_step = config_GetInt( p_this->p_libvlc, "volume-step" );
647 return (i_volume/i_volume_step);
651 * Display current audio volume bitmap
653 * The OSD Menu audio volume bar is updated to reflect the new audio volume. Call this function
654 * when the audio volume is updated outside the OSD menu command "menu up", "menu down" or "menu select".
656 void __osd_Volume( vlc_object_t *p_this )
658 osd_menu_t *p_osd = NULL;
659 osd_button_t *p_button = NULL;
664 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
667 msg_Err( p_this, "OSD menu volume update failed" );
671 if( p_osd->p_state && p_osd->p_state->p_volume )
673 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
674 vlc_mutex_lock( lockval.p_address );
676 p_button = p_osd->p_state->p_volume;
677 if( p_osd->p_state->p_volume )
678 p_osd->p_state->p_visible = p_osd->p_state->p_volume;
679 if( p_button && p_button->b_range )
681 /* Update the volume state images to match the current volume */
682 i_volume = config_GetInt( p_this, "volume" );
683 i_steps = osd_VolumeStep( p_this, i_volume, p_button->i_ranges );
684 p_button->p_current_state = osd_VolumeStateChange( p_button->p_states, i_steps );
686 osd_UpdateState( p_osd->p_state,
687 p_button->i_x, p_button->i_y,
688 p_button->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
689 p_button->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
690 p_button->p_current_state->p_pic );
691 osd_SetMenuUpdate( p_osd, VLC_TRUE );
692 osd_SetMenuVisible( p_osd, VLC_TRUE );
694 vlc_object_release( (vlc_object_t*) p_osd );
695 vlc_mutex_unlock( lockval.p_address );