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_yield( p_menu );
70 vlc_object_attach( p_menu, p_this->p_libvlc );
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, p_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",
93 if( !p_menu->p_parser )
95 osd_ParserUnload( p_this, p_menu );
102 static void osd_ParserUnload( vlc_object_t *p_this, osd_menu_t *p_menu )
104 if( p_menu->p_image )
105 image_HandlerDelete( p_menu->p_image );
106 if( p_menu->psz_file )
107 free( p_menu->psz_file );
109 if( p_menu->p_parser )
110 module_Unneed( p_menu, p_menu->p_parser );
112 vlc_object_detach( p_menu );
113 vlc_object_destroy( p_menu );
116 /*****************************************************************************
118 *****************************************************************************/
119 osd_menu_t *__osd_MenuCreate( vlc_object_t *p_this, const char *psz_file )
121 osd_menu_t *p_osd = NULL;
126 /* to be sure to avoid multiple creation */
127 var_Create( p_this->p_libvlc, "osd_mutex", VLC_VAR_MUTEX );
128 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
129 vlc_mutex_lock( lockval.p_address );
131 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
136 /* Parse configuration file */
137 p_osd = osd_ParserLoad( p_this, psz_file );
141 /* Setup default button (first button) */
142 p_osd->p_state->p_visible = p_osd->p_button;
143 p_osd->p_state->p_visible->p_current_state =
144 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
145 p_osd->i_width = p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch;
146 p_osd->i_height = p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines;
148 if( p_osd->p_state->p_volume )
150 /* Update the volume state images to match the current volume */
151 i_volume = config_GetInt( p_this, "volume" );
152 i_steps = osd_VolumeStep( p_this, i_volume, p_osd->p_state->p_volume->i_ranges );
153 p_osd->p_state->p_volume->p_current_state = osd_VolumeStateChange(
154 p_osd->p_state->p_volume->p_states, i_steps );
156 /* Initialize OSD state */
157 osd_UpdateState( p_osd->p_state, p_osd->i_x, p_osd->i_y,
158 p_osd->i_width, p_osd->i_height, NULL );
160 /* Signal when an update of OSD menu is needed */
161 var_Create( p_osd, "osd-menu-update", VLC_VAR_BOOL );
162 var_Create( p_osd, "osd-menu-visible", VLC_VAR_BOOL );
164 val.b_bool = VLC_FALSE;
165 var_Set( p_osd, "osd-menu-update", val );
166 var_Set( p_osd, "osd-menu-visible", val );
168 vlc_mutex_unlock( lockval.p_address );
172 msg_Err( p_this, "creating OSD menu object failed" );
175 image_HandlerDelete( p_osd->p_image );
176 if( p_osd->psz_file )
177 free( p_osd->psz_file );
179 vlc_object_detach( p_osd );
180 vlc_object_destroy( p_osd );
181 vlc_mutex_unlock( lockval.p_address );
185 void __osd_MenuDelete( vlc_object_t *p_this, osd_menu_t *p_osd )
189 if( !p_osd || !p_this ) return;
191 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
192 vlc_mutex_lock( lockval.p_address );
194 vlc_object_release( p_osd );
195 if( p_osd->p_internals->i_refcount > 0 )
197 vlc_mutex_unlock( lockval.p_address );
201 var_Destroy( p_osd, "osd-menu-visible" );
202 var_Destroy( p_osd, "osd-menu-update" );
204 osd_ParserUnload( p_this, p_osd );
206 vlc_mutex_unlock( lockval.p_address );
209 osd_state_t *__osd_StateChange( osd_state_t *p_states, const int i_state )
211 osd_state_t *p_current = p_states;
212 osd_state_t *p_temp = NULL;
215 for( i=0; p_current != NULL; i++ )
217 if( p_current->i_state == i_state )
219 p_temp = p_current->p_next;
225 /* The volume can be modified in another interface while the OSD Menu
226 * has not been instantiated yet. This routines updates the "volume OSD menu item"
227 * to reflect the current state of the GUI.
229 static inline osd_state_t *osd_VolumeStateChange( osd_state_t *p_current, int i_steps )
231 osd_state_t *p_temp = NULL;
234 if( i_steps < 0 ) i_steps = 0;
236 for( i=0; (i < i_steps) && (p_current != NULL); i++ )
238 p_temp = p_current->p_next;
239 if( !p_temp ) return p_current;
242 return (!p_temp) ? p_current : p_temp;
245 /* Update the state of the OSD Menu */
246 static void osd_UpdateState( osd_menu_state_t *p_state, int i_x, int i_y,
247 int i_width, int i_height, picture_t *p_pic )
251 p_state->i_width = i_width;
252 p_state->i_height = i_height;
253 p_state->p_pic = p_pic;
256 void __osd_MenuShow( vlc_object_t *p_this )
258 osd_menu_t *p_osd = NULL;
259 osd_button_t *p_button = NULL;
262 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
265 msg_Err( p_this, "osd_MenuNext failed" );
269 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
270 vlc_mutex_lock( lockval.p_address );
272 #if defined(OSD_MENU_DEBUG)
273 msg_Dbg( p_osd, "menu on" );
275 p_button = p_osd->p_state->p_visible;
278 if( !p_button->b_range )
279 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
280 p_osd->p_state->p_visible = p_osd->p_button;
282 if( !p_osd->p_state->p_visible->b_range )
283 p_osd->p_state->p_visible->p_current_state =
284 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
286 osd_UpdateState( p_osd->p_state,
287 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
288 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
289 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
290 p_osd->p_state->p_visible->p_current_state->p_pic );
291 osd_SetMenuUpdate( p_osd, VLC_TRUE );
293 osd_SetMenuVisible( p_osd, VLC_TRUE );
295 vlc_object_release( (vlc_object_t*) p_osd );
296 vlc_mutex_unlock( lockval.p_address );
299 void __osd_MenuHide( vlc_object_t *p_this )
301 osd_menu_t *p_osd = NULL;
304 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
307 msg_Err( p_this, "osd_MenuNext failed" );
311 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
312 vlc_mutex_lock( lockval.p_address );
314 #if defined(OSD_MENU_DEBUG)
315 msg_Dbg( p_osd, "menu off" );
317 osd_UpdateState( p_osd->p_state,
318 p_osd->p_state->i_x, p_osd->p_state->i_y,
320 osd_SetMenuUpdate( p_osd, VLC_TRUE );
322 vlc_object_release( (vlc_object_t*) p_osd );
323 vlc_mutex_unlock( lockval.p_address );
326 void __osd_MenuActivate( vlc_object_t *p_this )
328 osd_menu_t *p_osd = NULL;
329 osd_button_t *p_button = NULL;
332 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
335 msg_Err( p_this, "osd_MenuNext failed" );
339 if( osd_isVisible( p_osd ) == VLC_FALSE )
341 vlc_object_release( (vlc_object_t*) p_osd );
345 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
346 vlc_mutex_lock( lockval.p_address );
348 #if defined(OSD_MENU_DEBUG)
349 msg_Dbg( p_osd, "select" );
351 p_button = p_osd->p_state->p_visible;
353 * Is there a menu item above or below? If so, then select it.
355 if( p_button && p_button->p_up )
357 vlc_object_release( (vlc_object_t*) p_osd );
358 vlc_mutex_unlock( lockval.p_address );
359 __osd_MenuUp( p_this ); /* "menu select" means go to menu item above. */
362 if( p_button && p_button->p_down )
364 vlc_object_release( (vlc_object_t*) p_osd );
365 vlc_mutex_unlock( lockval.p_address );
366 __osd_MenuDown( p_this ); /* "menu select" means go to menu item below. */
370 if( p_button && !p_button->b_range )
372 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_PRESSED );
373 osd_UpdateState( p_osd->p_state,
374 p_button->i_x, p_button->i_y,
375 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
376 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
377 p_button->p_current_state->p_pic );
378 osd_SetMenuUpdate( p_osd, VLC_TRUE );
379 osd_SetMenuVisible( p_osd, VLC_TRUE );
380 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt( p_osd, p_button->psz_action ) );
381 #if defined(OSD_MENU_DEBUG)
382 msg_Dbg( p_osd, "select (%d, %s)", config_GetInt( p_osd, p_button->psz_action ), p_button->psz_action );
385 vlc_object_release( (vlc_object_t*) p_osd );
386 vlc_mutex_unlock( lockval.p_address );
389 void __osd_MenuNext( vlc_object_t *p_this )
391 osd_menu_t *p_osd = NULL;
392 osd_button_t *p_button = NULL;
395 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
398 msg_Err( p_this, "osd_MenuNext failed" );
402 if( osd_isVisible( p_osd ) == VLC_FALSE )
404 vlc_object_release( (vlc_object_t*) p_osd );
408 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
409 vlc_mutex_lock( lockval.p_address );
411 p_button = p_osd->p_state->p_visible;
414 if( !p_button->b_range )
415 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
416 if( p_button->p_next )
417 p_osd->p_state->p_visible = p_button->p_next;
419 p_osd->p_state->p_visible = p_osd->p_button;
421 if( !p_osd->p_state->p_visible->b_range )
422 p_osd->p_state->p_visible->p_current_state =
423 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
425 osd_UpdateState( p_osd->p_state,
426 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
427 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
428 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
429 p_osd->p_state->p_visible->p_current_state->p_pic );
430 osd_SetMenuUpdate( p_osd, VLC_TRUE );
432 #if defined(OSD_MENU_DEBUG)
433 msg_Dbg( p_osd, "direction right [button %s]", p_osd->p_state->p_visible->psz_action );
436 vlc_object_release( (vlc_object_t*) p_osd );
437 vlc_mutex_unlock( lockval.p_address );
440 void __osd_MenuPrev( vlc_object_t *p_this )
442 osd_menu_t *p_osd = NULL;
443 osd_button_t *p_button = NULL;
446 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
449 msg_Err( p_this, "osd_MenuPrev failed" );
453 if( osd_isVisible( p_osd ) == VLC_FALSE )
455 vlc_object_release( (vlc_object_t*) p_osd );
459 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
460 vlc_mutex_lock( lockval.p_address );
462 p_button = p_osd->p_state->p_visible;
465 if( !p_button->b_range )
466 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
467 if( p_button->p_prev )
468 p_osd->p_state->p_visible = p_button->p_prev;
470 p_osd->p_state->p_visible = p_osd->p_last_button;
472 if( !p_osd->p_state->p_visible->b_range )
473 p_osd->p_state->p_visible->p_current_state =
474 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
476 osd_UpdateState( p_osd->p_state,
477 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
478 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
479 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
480 p_osd->p_state->p_visible->p_current_state->p_pic );
481 osd_SetMenuUpdate( p_osd, VLC_TRUE );
483 #if defined(OSD_MENU_DEBUG)
484 msg_Dbg( p_osd, "direction left [button %s]", p_osd->p_state->p_visible->psz_action );
487 vlc_object_release( (vlc_object_t*) p_osd );
488 vlc_mutex_unlock( lockval.p_address );
491 void __osd_MenuUp( vlc_object_t *p_this )
493 osd_menu_t *p_osd = NULL;
494 osd_button_t *p_button = NULL;
496 #if defined(OSD_MENU_DEBUG)
499 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
502 msg_Err( p_this, "osd_MenuDown failed" );
506 if( osd_isVisible( p_osd ) == VLC_FALSE )
508 vlc_object_release( (vlc_object_t*) p_osd );
512 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
513 vlc_mutex_lock( lockval.p_address );
515 p_button = p_osd->p_state->p_visible;
518 if( !p_button->b_range )
520 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_SELECT );
522 p_osd->p_state->p_visible = p_button->p_up;
525 if( p_button->b_range && p_osd->p_state->p_visible->b_range )
527 osd_state_t *p_temp = p_osd->p_state->p_visible->p_current_state;
528 if( p_temp && p_temp->p_next )
529 p_osd->p_state->p_visible->p_current_state = p_temp->p_next;
531 else if( !p_osd->p_state->p_visible->b_range )
533 p_osd->p_state->p_visible->p_current_state =
534 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
537 osd_UpdateState( p_osd->p_state,
538 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
539 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
540 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
541 p_osd->p_state->p_visible->p_current_state->p_pic );
542 osd_SetMenuUpdate( p_osd, VLC_TRUE );
543 /* If this is a range style action with associated images of only one state,
544 * then perform "menu select" on every menu navigation
546 if( p_button->b_range )
548 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt(p_osd, p_button->psz_action) );
549 #if defined(OSD_MENU_DEBUG)
550 msg_Dbg( p_osd, "select (%d, %s)", val.i_int, p_button->psz_action );
554 #if defined(OSD_MENU_DEBUG)
555 msg_Dbg( p_osd, "direction up [button %s]", p_osd->p_state->p_visible->psz_action );
558 vlc_object_release( (vlc_object_t*) p_osd );
559 vlc_mutex_unlock( lockval.p_address );
562 void __osd_MenuDown( vlc_object_t *p_this )
564 osd_menu_t *p_osd = NULL;
565 osd_button_t *p_button = NULL;
567 #if defined(OSD_MENU_DEBUG)
571 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
574 msg_Err( p_this, "osd_MenuDown failed" );
578 if( osd_isVisible( p_osd ) == VLC_FALSE )
580 vlc_object_release( (vlc_object_t*) p_osd );
584 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
585 vlc_mutex_lock( lockval.p_address );
587 p_button = p_osd->p_state->p_visible;
590 if( !p_button->b_range )
592 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_SELECT );
593 if( p_button->p_down )
594 p_osd->p_state->p_visible = p_button->p_down;
597 if( p_button->b_range && p_osd->p_state->p_visible->b_range )
599 osd_state_t *p_temp = p_osd->p_state->p_visible->p_current_state;
600 if( p_temp && p_temp->p_prev )
601 p_osd->p_state->p_visible->p_current_state = p_temp->p_prev;
603 else if( !p_osd->p_state->p_visible->b_range )
605 p_osd->p_state->p_visible->p_current_state =
606 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
609 osd_UpdateState( p_osd->p_state,
610 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
611 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
612 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
613 p_osd->p_state->p_visible->p_current_state->p_pic );
614 osd_SetMenuUpdate( p_osd, VLC_TRUE );
615 /* If this is a range style action with associated images of only one state,
616 * then perform "menu select" on every menu navigation
618 if( p_button->b_range )
620 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt(p_osd, p_button->psz_action_down) );
621 #if defined(OSD_MENU_DEBUG)
622 msg_Dbg( p_osd, "select (%d, %s)", val.i_int, p_button->psz_action_down );
626 #if defined(OSD_MENU_DEBUG)
627 msg_Dbg( p_osd, "direction down [button %s]", p_osd->p_state->p_visible->psz_action );
630 vlc_object_release( (vlc_object_t*) p_osd );
631 vlc_mutex_unlock( lockval.p_address );
634 static int osd_VolumeStep( vlc_object_t *p_this, int i_volume, int i_steps )
636 int i_volume_step = 0;
639 i_volume_step = config_GetInt( p_this->p_libvlc, "volume-step" );
640 return (i_volume/i_volume_step);
644 * Display current audio volume bitmap
646 * The OSD Menu audio volume bar is updated to reflect the new audio volume. Call this function
647 * when the audio volume is updated outside the OSD menu command "menu up", "menu down" or "menu select".
649 void __osd_Volume( vlc_object_t *p_this )
651 osd_menu_t *p_osd = NULL;
652 osd_button_t *p_button = NULL;
657 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
660 msg_Err( p_this, "OSD menu volume update failed" );
664 if( p_osd->p_state && p_osd->p_state->p_volume )
666 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
667 vlc_mutex_lock( lockval.p_address );
669 p_button = p_osd->p_state->p_volume;
670 if( p_osd->p_state->p_volume )
671 p_osd->p_state->p_visible = p_osd->p_state->p_volume;
672 if( p_button && p_button->b_range )
674 /* Update the volume state images to match the current volume */
675 i_volume = config_GetInt( p_this, "volume" );
676 i_steps = osd_VolumeStep( p_this, i_volume, p_button->i_ranges );
677 p_button->p_current_state = osd_VolumeStateChange( p_button->p_states, i_steps );
679 osd_UpdateState( p_osd->p_state,
680 p_button->i_x, p_button->i_y,
681 p_button->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
682 p_button->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
683 p_button->p_current_state->p_pic );
684 osd_SetMenuUpdate( p_osd, VLC_TRUE );
685 osd_SetMenuVisible( p_osd, VLC_TRUE );
687 vlc_object_release( (vlc_object_t*) p_osd );
688 vlc_mutex_unlock( lockval.p_address );