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 osd_menu_t *osd_ParserLoad( vlc_object_t *, const char * );
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, p_menu );
101 static void osd_ParserUnload( vlc_object_t *p_this, osd_menu_t *p_menu )
103 if( p_menu->p_image )
104 image_HandlerDelete( p_menu->p_image );
105 if( p_menu->psz_file )
106 free( p_menu->psz_file );
108 if( p_menu->p_parser )
109 module_Unneed( p_menu, p_menu->p_parser );
111 vlc_object_detach( p_menu );
112 vlc_object_destroy( p_menu );
115 /*****************************************************************************
117 *****************************************************************************/
118 osd_menu_t *__osd_MenuCreate( vlc_object_t *p_this, const char *psz_file )
120 osd_menu_t *p_osd = NULL;
125 /* to be sure to avoid multiple creation */
126 var_Create( p_this->p_libvlc, "osd_mutex", VLC_VAR_MUTEX );
127 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
128 vlc_mutex_lock( lockval.p_address );
130 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
135 /* Parse configuration file */
136 p_osd = osd_ParserLoad( p_this, psz_file );
140 /* Setup default button (first button) */
141 p_osd->p_state->p_visible = p_osd->p_button;
142 p_osd->p_state->p_visible->p_current_state =
143 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
144 p_osd->i_width = p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch;
145 p_osd->i_height = p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines;
147 if( p_osd->p_state->p_volume )
149 /* Update the volume state images to match the current volume */
150 i_volume = config_GetInt( p_this, "volume" );
151 i_steps = osd_VolumeStep( p_this, i_volume, p_osd->p_state->p_volume->i_ranges );
152 p_osd->p_state->p_volume->p_current_state = osd_VolumeStateChange(
153 p_osd->p_state->p_volume->p_states, i_steps );
155 /* Initialize OSD state */
156 osd_UpdateState( p_osd->p_state, p_osd->i_x, p_osd->i_y,
157 p_osd->i_width, p_osd->i_height, NULL );
159 vlc_object_yield( p_osd );
160 vlc_object_attach( p_osd, p_this->p_libvlc );
162 /* Signal when an update of OSD menu is needed */
163 var_Create( p_osd, "osd-menu-update", VLC_VAR_BOOL );
164 var_Create( p_osd, "osd-menu-visible", VLC_VAR_BOOL );
166 val.b_bool = VLC_FALSE;
167 var_Set( p_osd, "osd-menu-update", val );
168 var_Set( p_osd, "osd-menu-visible", val );
170 vlc_mutex_unlock( lockval.p_address );
174 msg_Err( p_this, "creating OSD menu object failed" );
177 image_HandlerDelete( p_osd->p_image );
178 if( p_osd->psz_file )
179 free( p_osd->psz_file );
181 vlc_object_detach( p_osd );
182 vlc_object_destroy( p_osd );
183 vlc_mutex_unlock( lockval.p_address );
187 void __osd_MenuDelete( vlc_object_t *p_this, osd_menu_t *p_osd )
191 if( !p_osd || !p_this ) return;
193 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
194 vlc_mutex_lock( lockval.p_address );
196 vlc_object_release( p_osd );
197 if( p_osd->p_internals->i_refcount > 0 )
199 vlc_mutex_unlock( lockval.p_address );
203 var_Destroy( p_osd, "osd-menu-visible" );
204 var_Destroy( p_osd, "osd-menu-update" );
206 osd_ParserUnload( p_this, p_osd );
208 vlc_mutex_unlock( lockval.p_address );
211 osd_state_t *__osd_StateChange( osd_state_t *p_states, const int i_state )
213 osd_state_t *p_current = p_states;
214 osd_state_t *p_temp = NULL;
217 for( i=0; p_current != NULL; i++ )
219 if( p_current->i_state == i_state )
221 p_temp = p_current->p_next;
227 /* The volume can be modified in another interface while the OSD Menu
228 * has not been instantiated yet. This routines updates the "volume OSD menu item"
229 * to reflect the current state of the GUI.
231 static inline osd_state_t *osd_VolumeStateChange( osd_state_t *p_current, int i_steps )
233 osd_state_t *p_temp = NULL;
236 if( i_steps < 0 ) i_steps = 0;
238 for( i=0; (i < i_steps) && (p_current != NULL); i++ )
240 p_temp = p_current->p_next;
241 if( !p_temp ) return p_current;
244 return (!p_temp) ? p_current : p_temp;
247 /* Update the state of the OSD Menu */
248 static void osd_UpdateState( osd_menu_state_t *p_state, int i_x, int i_y,
249 int i_width, int i_height, picture_t *p_pic )
253 p_state->i_width = i_width;
254 p_state->i_height = i_height;
255 p_state->p_pic = p_pic;
258 void __osd_MenuShow( vlc_object_t *p_this )
260 osd_menu_t *p_osd = NULL;
261 osd_button_t *p_button = NULL;
264 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
267 msg_Err( p_this, "osd_MenuNext failed" );
271 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
272 vlc_mutex_lock( lockval.p_address );
274 #if defined(OSD_MENU_DEBUG)
275 msg_Dbg( p_osd, "menu on" );
277 p_button = p_osd->p_state->p_visible;
280 if( !p_button->b_range )
281 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
282 p_osd->p_state->p_visible = p_osd->p_button;
284 if( !p_osd->p_state->p_visible->b_range )
285 p_osd->p_state->p_visible->p_current_state =
286 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
288 osd_UpdateState( p_osd->p_state,
289 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
290 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
291 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
292 p_osd->p_state->p_visible->p_current_state->p_pic );
293 osd_SetMenuUpdate( p_osd, VLC_TRUE );
295 osd_SetMenuVisible( p_osd, VLC_TRUE );
297 vlc_object_release( (vlc_object_t*) p_osd );
298 vlc_mutex_unlock( lockval.p_address );
301 void __osd_MenuHide( vlc_object_t *p_this )
303 osd_menu_t *p_osd = NULL;
306 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
309 msg_Err( p_this, "osd_MenuNext failed" );
313 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
314 vlc_mutex_lock( lockval.p_address );
316 #if defined(OSD_MENU_DEBUG)
317 msg_Dbg( p_osd, "menu off" );
319 osd_UpdateState( p_osd->p_state,
320 p_osd->p_state->i_x, p_osd->p_state->i_y,
322 osd_SetMenuUpdate( p_osd, VLC_TRUE );
324 vlc_object_release( (vlc_object_t*) p_osd );
325 vlc_mutex_unlock( lockval.p_address );
328 void __osd_MenuActivate( vlc_object_t *p_this )
330 osd_menu_t *p_osd = NULL;
331 osd_button_t *p_button = NULL;
334 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
337 msg_Err( p_this, "osd_MenuNext failed" );
341 if( osd_isVisible( p_osd ) == VLC_FALSE )
343 vlc_object_release( (vlc_object_t*) p_osd );
347 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
348 vlc_mutex_lock( lockval.p_address );
350 #if defined(OSD_MENU_DEBUG)
351 msg_Dbg( p_osd, "select" );
353 p_button = p_osd->p_state->p_visible;
355 * Is there a menu item above or below? If so, then select it.
357 if( p_button && p_button->p_up )
359 vlc_object_release( (vlc_object_t*) p_osd );
360 vlc_mutex_unlock( lockval.p_address );
361 __osd_MenuUp( p_this ); /* "menu select" means go to menu item above. */
364 if( p_button && p_button->p_down )
366 vlc_object_release( (vlc_object_t*) p_osd );
367 vlc_mutex_unlock( lockval.p_address );
368 __osd_MenuDown( p_this ); /* "menu select" means go to menu item below. */
372 if( p_button && !p_button->b_range )
374 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_PRESSED );
375 osd_UpdateState( p_osd->p_state,
376 p_button->i_x, p_button->i_y,
377 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
378 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
379 p_button->p_current_state->p_pic );
380 osd_SetMenuUpdate( p_osd, VLC_TRUE );
381 osd_SetMenuVisible( p_osd, VLC_TRUE );
382 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt( p_osd, p_button->psz_action ) );
383 #if defined(OSD_MENU_DEBUG)
384 msg_Dbg( p_osd, "select (%d, %s)", config_GetInt( p_osd, p_button->psz_action ), p_button->psz_action );
387 vlc_object_release( (vlc_object_t*) p_osd );
388 vlc_mutex_unlock( lockval.p_address );
391 void __osd_MenuNext( vlc_object_t *p_this )
393 osd_menu_t *p_osd = NULL;
394 osd_button_t *p_button = NULL;
397 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
400 msg_Err( p_this, "osd_MenuNext failed" );
404 if( osd_isVisible( p_osd ) == VLC_FALSE )
406 vlc_object_release( (vlc_object_t*) p_osd );
410 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
411 vlc_mutex_lock( lockval.p_address );
413 p_button = p_osd->p_state->p_visible;
416 if( !p_button->b_range )
417 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
418 if( p_button->p_next )
419 p_osd->p_state->p_visible = p_button->p_next;
421 p_osd->p_state->p_visible = p_osd->p_button;
423 if( !p_osd->p_state->p_visible->b_range )
424 p_osd->p_state->p_visible->p_current_state =
425 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
427 osd_UpdateState( p_osd->p_state,
428 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
429 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
430 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
431 p_osd->p_state->p_visible->p_current_state->p_pic );
432 osd_SetMenuUpdate( p_osd, VLC_TRUE );
434 #if defined(OSD_MENU_DEBUG)
435 msg_Dbg( p_osd, "direction right [button %s]", p_osd->p_state->p_visible->psz_action );
438 vlc_object_release( (vlc_object_t*) p_osd );
439 vlc_mutex_unlock( lockval.p_address );
442 void __osd_MenuPrev( vlc_object_t *p_this )
444 osd_menu_t *p_osd = NULL;
445 osd_button_t *p_button = NULL;
448 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
451 msg_Err( p_this, "osd_MenuPrev failed" );
455 if( osd_isVisible( p_osd ) == VLC_FALSE )
457 vlc_object_release( (vlc_object_t*) p_osd );
461 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
462 vlc_mutex_lock( lockval.p_address );
464 p_button = p_osd->p_state->p_visible;
467 if( !p_button->b_range )
468 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
469 if( p_button->p_prev )
470 p_osd->p_state->p_visible = p_button->p_prev;
472 p_osd->p_state->p_visible = p_osd->p_last_button;
474 if( !p_osd->p_state->p_visible->b_range )
475 p_osd->p_state->p_visible->p_current_state =
476 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
478 osd_UpdateState( p_osd->p_state,
479 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
480 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
481 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
482 p_osd->p_state->p_visible->p_current_state->p_pic );
483 osd_SetMenuUpdate( p_osd, VLC_TRUE );
485 #if defined(OSD_MENU_DEBUG)
486 msg_Dbg( p_osd, "direction left [button %s]", p_osd->p_state->p_visible->psz_action );
489 vlc_object_release( (vlc_object_t*) p_osd );
490 vlc_mutex_unlock( lockval.p_address );
493 void __osd_MenuUp( vlc_object_t *p_this )
495 osd_menu_t *p_osd = NULL;
496 osd_button_t *p_button = NULL;
498 #if defined(OSD_MENU_DEBUG)
501 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
504 msg_Err( p_this, "osd_MenuDown failed" );
508 if( osd_isVisible( p_osd ) == VLC_FALSE )
510 vlc_object_release( (vlc_object_t*) p_osd );
514 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
515 vlc_mutex_lock( lockval.p_address );
517 p_button = p_osd->p_state->p_visible;
520 if( !p_button->b_range )
522 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_SELECT );
524 p_osd->p_state->p_visible = p_button->p_up;
527 if( p_button->b_range && p_osd->p_state->p_visible->b_range )
529 osd_state_t *p_temp = p_osd->p_state->p_visible->p_current_state;
530 if( p_temp && p_temp->p_next )
531 p_osd->p_state->p_visible->p_current_state = p_temp->p_next;
533 else if( !p_osd->p_state->p_visible->b_range )
535 p_osd->p_state->p_visible->p_current_state =
536 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
539 osd_UpdateState( p_osd->p_state,
540 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
541 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
542 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
543 p_osd->p_state->p_visible->p_current_state->p_pic );
544 osd_SetMenuUpdate( p_osd, VLC_TRUE );
545 /* If this is a range style action with associated images of only one state,
546 * then perform "menu select" on every menu navigation
548 if( p_button->b_range )
550 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt(p_osd, p_button->psz_action) );
551 #if defined(OSD_MENU_DEBUG)
552 msg_Dbg( p_osd, "select (%d, %s)", val.i_int, p_button->psz_action );
556 #if defined(OSD_MENU_DEBUG)
557 msg_Dbg( p_osd, "direction up [button %s]", p_osd->p_state->p_visible->psz_action );
560 vlc_object_release( (vlc_object_t*) p_osd );
561 vlc_mutex_unlock( lockval.p_address );
564 void __osd_MenuDown( vlc_object_t *p_this )
566 osd_menu_t *p_osd = NULL;
567 osd_button_t *p_button = NULL;
569 #if defined(OSD_MENU_DEBUG)
573 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
576 msg_Err( p_this, "osd_MenuDown failed" );
580 if( osd_isVisible( p_osd ) == VLC_FALSE )
582 vlc_object_release( (vlc_object_t*) p_osd );
586 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
587 vlc_mutex_lock( lockval.p_address );
589 p_button = p_osd->p_state->p_visible;
592 if( !p_button->b_range )
594 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_SELECT );
595 if( p_button->p_down )
596 p_osd->p_state->p_visible = p_button->p_down;
599 if( p_button->b_range && p_osd->p_state->p_visible->b_range )
601 osd_state_t *p_temp = p_osd->p_state->p_visible->p_current_state;
602 if( p_temp && p_temp->p_prev )
603 p_osd->p_state->p_visible->p_current_state = p_temp->p_prev;
605 else if( !p_osd->p_state->p_visible->b_range )
607 p_osd->p_state->p_visible->p_current_state =
608 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
611 osd_UpdateState( p_osd->p_state,
612 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
613 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
614 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
615 p_osd->p_state->p_visible->p_current_state->p_pic );
616 osd_SetMenuUpdate( p_osd, VLC_TRUE );
617 /* If this is a range style action with associated images of only one state,
618 * then perform "menu select" on every menu navigation
620 if( p_button->b_range )
622 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt(p_osd, p_button->psz_action_down) );
623 #if defined(OSD_MENU_DEBUG)
624 msg_Dbg( p_osd, "select (%d, %s)", val.i_int, p_button->psz_action_down );
628 #if defined(OSD_MENU_DEBUG)
629 msg_Dbg( p_osd, "direction down [button %s]", p_osd->p_state->p_visible->psz_action );
632 vlc_object_release( (vlc_object_t*) p_osd );
633 vlc_mutex_unlock( lockval.p_address );
636 static int osd_VolumeStep( vlc_object_t *p_this, int i_volume, int i_steps )
638 int i_volume_step = 0;
641 i_volume_step = config_GetInt( p_this->p_libvlc, "volume-step" );
642 return (i_volume/i_volume_step);
646 * Display current audio volume bitmap
648 * The OSD Menu audio volume bar is updated to reflect the new audio volume. Call this function
649 * when the audio volume is updated outside the OSD menu command "menu up", "menu down" or "menu select".
651 void __osd_Volume( vlc_object_t *p_this )
653 osd_menu_t *p_osd = NULL;
654 osd_button_t *p_button = NULL;
659 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
662 msg_Err( p_this, "OSD menu volume update failed" );
666 if( p_osd->p_state && p_osd->p_state->p_volume )
668 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
669 vlc_mutex_lock( lockval.p_address );
671 p_button = p_osd->p_state->p_volume;
672 if( p_osd->p_state->p_volume )
673 p_osd->p_state->p_visible = p_osd->p_state->p_volume;
674 if( p_button && p_button->b_range )
676 /* Update the volume state images to match the current volume */
677 i_volume = config_GetInt( p_this, "volume" );
678 i_steps = osd_VolumeStep( p_this, i_volume, p_button->i_ranges );
679 p_button->p_current_state = osd_VolumeStateChange( p_button->p_states, i_steps );
681 osd_UpdateState( p_osd->p_state,
682 p_button->i_x, p_button->i_y,
683 p_button->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
684 p_button->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
685 p_button->p_current_state->p_pic );
686 osd_SetMenuUpdate( p_osd, VLC_TRUE );
687 osd_SetMenuVisible( p_osd, VLC_TRUE );
689 vlc_object_release( (vlc_object_t*) p_osd );
690 vlc_mutex_unlock( lockval.p_address );