1 /*****************************************************************************
2 * osd.c - The OSD Menu 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 *****************************************************************************/
35 /*****************************************************************************
37 *****************************************************************************/
39 static void osd_UpdateState( osd_menu_state_t *, int, int, int, int, picture_t * );
40 static inline osd_state_t *osd_VolumeStateChange( osd_state_t *, int );
41 static int osd_VolumeStep( vlc_object_t *, int, int );
42 static vlc_bool_t osd_isVisible( osd_menu_t *p_osd );
44 static vlc_bool_t osd_isVisible( osd_menu_t *p_osd )
48 var_Get( p_osd, "osd-menu-visible", &val );
52 /*****************************************************************************
54 *****************************************************************************/
55 osd_menu_t *__osd_MenuCreate( vlc_object_t *p_this, const char *psz_file )
57 osd_menu_t *p_osd = NULL;
62 /* to be sure to avoid multiple creation */
63 var_Create( p_this->p_libvlc, "osd_mutex", VLC_VAR_MUTEX );
64 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
65 vlc_mutex_lock( lockval.p_address );
67 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
72 msg_Dbg( p_this, "creating OSD menu object" );
73 if( ( p_osd = vlc_object_create( p_this, VLC_OBJECT_OSDMENU ) ) == NULL )
75 msg_Err( p_this, "out of memory" );
76 vlc_mutex_unlock( lockval.p_address );
80 /* Parse configuration file */
81 if( osd_ConfigLoader( p_this, psz_file, &p_osd ) )
84 /* Setup default button (first button) */
85 p_osd->p_state->p_visible = p_osd->p_button;
86 p_osd->p_state->p_visible->p_current_state =
87 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
88 p_osd->i_width = p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch;
89 p_osd->i_height = p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines;
91 if( p_osd->p_state->p_volume )
93 /* Update the volume state images to match the current volume */
94 i_volume = config_GetInt( p_this, "volume" );
95 i_steps = osd_VolumeStep( p_this, i_volume, p_osd->p_state->p_volume->i_ranges );
96 p_osd->p_state->p_volume->p_current_state = osd_VolumeStateChange(
97 p_osd->p_state->p_volume->p_states, i_steps );
99 /* Initialize OSD state */
100 osd_UpdateState( p_osd->p_state, p_osd->i_x, p_osd->i_y,
101 p_osd->i_width, p_osd->i_height, NULL );
103 vlc_object_yield( p_osd );
104 vlc_object_attach( p_osd, p_this->p_libvlc );
106 /* Signal when an update of OSD menu is needed */
107 var_Create( p_osd, "osd-menu-update", VLC_VAR_BOOL );
108 var_Create( p_osd, "osd-menu-visible", VLC_VAR_BOOL );
110 val.b_bool = VLC_FALSE;
111 var_Set( p_osd, "osd-menu-update", val );
112 var_Set( p_osd, "osd-menu-visible", val );
114 vlc_mutex_unlock( lockval.p_address );
118 msg_Err( p_this, "creating OSD menu object failed" );
119 vlc_mutex_unlock( lockval.p_address );
120 vlc_object_destroy( p_osd );
124 void __osd_MenuDelete( vlc_object_t *p_this, osd_menu_t *p_osd )
128 if( !p_osd || !p_this ) return;
130 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
131 vlc_mutex_lock( lockval.p_address );
133 vlc_object_release( p_osd );
134 if( p_osd->p_internals->i_refcount > 0 )
136 vlc_mutex_unlock( lockval.p_address );
140 var_Destroy( p_osd, "osd-menu-visible" );
141 var_Destroy( p_osd, "osd-menu-update" );
143 osd_ConfigUnload( p_this, &p_osd );
144 vlc_object_detach( p_osd );
145 vlc_object_destroy( p_osd );
148 vlc_mutex_unlock( lockval.p_address );
151 osd_state_t *__osd_StateChange( osd_state_t *p_states, const int i_state )
153 osd_state_t *p_current = p_states;
154 osd_state_t *p_temp = NULL;
157 for( i=0; p_current != NULL; i++ )
159 if( p_current->i_state == i_state )
161 p_temp = p_current->p_next;
167 /* The volume can be modified in another interface while the OSD Menu
168 * has not been instantiated yet. This routines updates the "volume OSD menu item"
169 * to reflect the current state of the GUI.
171 static inline osd_state_t *osd_VolumeStateChange( osd_state_t *p_current, int i_steps )
173 osd_state_t *p_temp = NULL;
176 if( i_steps < 0 ) i_steps = 0;
178 for( i=0; (i < i_steps) && (p_current != NULL); i++ )
180 p_temp = p_current->p_next;
181 if( !p_temp ) return p_current;
184 return (!p_temp) ? p_current : p_temp;
187 /* Update the state of the OSD Menu */
188 static void osd_UpdateState( osd_menu_state_t *p_state, int i_x, int i_y,
189 int i_width, int i_height, picture_t *p_pic )
193 p_state->i_width = i_width;
194 p_state->i_height = i_height;
195 p_state->p_pic = p_pic;
198 void __osd_MenuShow( vlc_object_t *p_this )
200 osd_menu_t *p_osd = NULL;
201 osd_button_t *p_button = NULL;
204 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
207 msg_Err( p_this, "osd_MenuNext failed" );
211 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
212 vlc_mutex_lock( lockval.p_address );
214 #if defined(OSD_MENU_DEBUG)
215 msg_Dbg( p_osd, "menu on" );
217 p_button = p_osd->p_state->p_visible;
220 if( !p_button->b_range )
221 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
222 p_osd->p_state->p_visible = p_osd->p_button;
224 if( !p_osd->p_state->p_visible->b_range )
225 p_osd->p_state->p_visible->p_current_state =
226 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
228 osd_UpdateState( p_osd->p_state,
229 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
230 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
231 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
232 p_osd->p_state->p_visible->p_current_state->p_pic );
233 osd_SetMenuUpdate( p_osd, VLC_TRUE );
235 osd_SetMenuVisible( p_osd, VLC_TRUE );
237 vlc_object_release( (vlc_object_t*) p_osd );
238 vlc_mutex_unlock( lockval.p_address );
241 void __osd_MenuHide( vlc_object_t *p_this )
243 osd_menu_t *p_osd = NULL;
246 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
249 msg_Err( p_this, "osd_MenuNext failed" );
253 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
254 vlc_mutex_lock( lockval.p_address );
256 #if defined(OSD_MENU_DEBUG)
257 msg_Dbg( p_osd, "menu off" );
259 osd_UpdateState( p_osd->p_state,
260 p_osd->p_state->i_x, p_osd->p_state->i_y,
262 osd_SetMenuUpdate( p_osd, VLC_TRUE );
264 vlc_object_release( (vlc_object_t*) p_osd );
265 vlc_mutex_unlock( lockval.p_address );
268 void __osd_MenuActivate( vlc_object_t *p_this )
270 osd_menu_t *p_osd = NULL;
271 osd_button_t *p_button = NULL;
274 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
277 msg_Err( p_this, "osd_MenuNext failed" );
281 if( osd_isVisible( p_osd ) == VLC_FALSE )
283 vlc_object_release( (vlc_object_t*) p_osd );
287 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
288 vlc_mutex_lock( lockval.p_address );
290 #if defined(OSD_MENU_DEBUG)
291 msg_Dbg( p_osd, "select" );
293 p_button = p_osd->p_state->p_visible;
295 * Is there a menu item above or below? If so, then select it.
297 if( p_button && p_button->p_up )
299 vlc_object_release( (vlc_object_t*) p_osd );
300 vlc_mutex_unlock( lockval.p_address );
301 __osd_MenuUp( p_this ); /* "menu select" means go to menu item above. */
304 if( p_button && p_button->p_down )
306 vlc_object_release( (vlc_object_t*) p_osd );
307 vlc_mutex_unlock( lockval.p_address );
308 __osd_MenuDown( p_this ); /* "menu select" means go to menu item below. */
312 if( p_button && !p_button->b_range )
314 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_PRESSED );
315 osd_UpdateState( p_osd->p_state,
316 p_button->i_x, p_button->i_y,
317 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
318 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
319 p_button->p_current_state->p_pic );
320 osd_SetMenuUpdate( p_osd, VLC_TRUE );
321 osd_SetMenuVisible( p_osd, VLC_TRUE );
322 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt( p_osd, p_button->psz_action ) );
323 #if defined(OSD_MENU_DEBUG)
324 msg_Dbg( p_osd, "select (%d, %s)", config_GetInt( p_osd, p_button->psz_action ), p_button->psz_action );
327 vlc_object_release( (vlc_object_t*) p_osd );
328 vlc_mutex_unlock( lockval.p_address );
331 void __osd_MenuNext( vlc_object_t *p_this )
333 osd_menu_t *p_osd = NULL;
334 osd_button_t *p_button = NULL;
337 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
340 msg_Err( p_this, "osd_MenuNext failed" );
344 if( osd_isVisible( p_osd ) == VLC_FALSE )
346 vlc_object_release( (vlc_object_t*) p_osd );
350 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
351 vlc_mutex_lock( lockval.p_address );
353 p_button = p_osd->p_state->p_visible;
356 if( !p_button->b_range )
357 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
358 if( p_button->p_next )
359 p_osd->p_state->p_visible = p_button->p_next;
361 p_osd->p_state->p_visible = p_osd->p_button;
363 if( !p_osd->p_state->p_visible->b_range )
364 p_osd->p_state->p_visible->p_current_state =
365 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
367 osd_UpdateState( p_osd->p_state,
368 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
369 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
370 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
371 p_osd->p_state->p_visible->p_current_state->p_pic );
372 osd_SetMenuUpdate( p_osd, VLC_TRUE );
374 #if defined(OSD_MENU_DEBUG)
375 msg_Dbg( p_osd, "direction right [button %s]", p_osd->p_state->p_visible->psz_action );
378 vlc_object_release( (vlc_object_t*) p_osd );
379 vlc_mutex_unlock( lockval.p_address );
382 void __osd_MenuPrev( vlc_object_t *p_this )
384 osd_menu_t *p_osd = NULL;
385 osd_button_t *p_button = NULL;
388 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
391 msg_Err( p_this, "osd_MenuPrev failed" );
395 if( osd_isVisible( p_osd ) == VLC_FALSE )
397 vlc_object_release( (vlc_object_t*) p_osd );
401 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
402 vlc_mutex_lock( lockval.p_address );
404 p_button = p_osd->p_state->p_visible;
407 if( !p_button->b_range )
408 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_UNSELECT );
409 if( p_button->p_prev )
410 p_osd->p_state->p_visible = p_button->p_prev;
412 p_osd->p_state->p_visible = p_osd->p_last_button;
414 if( !p_osd->p_state->p_visible->b_range )
415 p_osd->p_state->p_visible->p_current_state =
416 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
418 osd_UpdateState( p_osd->p_state,
419 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
420 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
421 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
422 p_osd->p_state->p_visible->p_current_state->p_pic );
423 osd_SetMenuUpdate( p_osd, VLC_TRUE );
425 #if defined(OSD_MENU_DEBUG)
426 msg_Dbg( p_osd, "direction left [button %s]", p_osd->p_state->p_visible->psz_action );
429 vlc_object_release( (vlc_object_t*) p_osd );
430 vlc_mutex_unlock( lockval.p_address );
433 void __osd_MenuUp( vlc_object_t *p_this )
435 osd_menu_t *p_osd = NULL;
436 osd_button_t *p_button = NULL;
438 #if defined(OSD_MENU_DEBUG)
441 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
444 msg_Err( p_this, "osd_MenuDown failed" );
448 if( osd_isVisible( p_osd ) == VLC_FALSE )
450 vlc_object_release( (vlc_object_t*) p_osd );
454 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
455 vlc_mutex_lock( lockval.p_address );
457 p_button = p_osd->p_state->p_visible;
460 if( !p_button->b_range )
462 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_SELECT );
464 p_osd->p_state->p_visible = p_button->p_up;
467 if( p_button->b_range && p_osd->p_state->p_visible->b_range )
469 osd_state_t *p_temp = p_osd->p_state->p_visible->p_current_state;
470 if( p_temp && p_temp->p_next )
471 p_osd->p_state->p_visible->p_current_state = p_temp->p_next;
473 else 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 );
479 osd_UpdateState( p_osd->p_state,
480 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
481 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
482 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
483 p_osd->p_state->p_visible->p_current_state->p_pic );
484 osd_SetMenuUpdate( p_osd, VLC_TRUE );
485 /* If this is a range style action with associated images of only one state,
486 * then perform "menu select" on every menu navigation
488 if( p_button->b_range )
490 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt(p_osd, p_button->psz_action) );
491 #if defined(OSD_MENU_DEBUG)
492 msg_Dbg( p_osd, "select (%d, %s)", val.i_int, p_button->psz_action );
496 #if defined(OSD_MENU_DEBUG)
497 msg_Dbg( p_osd, "direction up [button %s]", p_osd->p_state->p_visible->psz_action );
500 vlc_object_release( (vlc_object_t*) p_osd );
501 vlc_mutex_unlock( lockval.p_address );
504 void __osd_MenuDown( vlc_object_t *p_this )
506 osd_menu_t *p_osd = NULL;
507 osd_button_t *p_button = NULL;
509 #if defined(OSD_MENU_DEBUG)
513 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
516 msg_Err( p_this, "osd_MenuDown failed" );
520 if( osd_isVisible( p_osd ) == VLC_FALSE )
522 vlc_object_release( (vlc_object_t*) p_osd );
526 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
527 vlc_mutex_lock( lockval.p_address );
529 p_button = p_osd->p_state->p_visible;
532 if( !p_button->b_range )
534 p_button->p_current_state = osd_StateChange( p_button->p_states, OSD_BUTTON_SELECT );
535 if( p_button->p_down )
536 p_osd->p_state->p_visible = p_button->p_down;
539 if( p_button->b_range && p_osd->p_state->p_visible->b_range )
541 osd_state_t *p_temp = p_osd->p_state->p_visible->p_current_state;
542 if( p_temp && p_temp->p_prev )
543 p_osd->p_state->p_visible->p_current_state = p_temp->p_prev;
545 else if( !p_osd->p_state->p_visible->b_range )
547 p_osd->p_state->p_visible->p_current_state =
548 osd_StateChange( p_osd->p_state->p_visible->p_states, OSD_BUTTON_SELECT );
551 osd_UpdateState( p_osd->p_state,
552 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
553 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
554 p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
555 p_osd->p_state->p_visible->p_current_state->p_pic );
556 osd_SetMenuUpdate( p_osd, VLC_TRUE );
557 /* If this is a range style action with associated images of only one state,
558 * then perform "menu select" on every menu navigation
560 if( p_button->b_range )
562 osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt(p_osd, p_button->psz_action_down) );
563 #if defined(OSD_MENU_DEBUG)
564 msg_Dbg( p_osd, "select (%d, %s)", val.i_int, p_button->psz_action_down );
568 #if defined(OSD_MENU_DEBUG)
569 msg_Dbg( p_osd, "direction down [button %s]", p_osd->p_state->p_visible->psz_action );
572 vlc_object_release( (vlc_object_t*) p_osd );
573 vlc_mutex_unlock( lockval.p_address );
576 static int osd_VolumeStep( vlc_object_t *p_this, int i_volume, int i_steps )
578 int i_volume_step = 0;
581 i_volume_step = config_GetInt( p_this->p_libvlc, "volume-step" );
582 return (i_volume/i_volume_step);
586 * Display current audio volume bitmap
588 * The OSD Menu audio volume bar is updated to reflect the new audio volume. Call this function
589 * when the audio volume is updated outside the OSD menu command "menu up", "menu down" or "menu select".
591 void __osd_Volume( vlc_object_t *p_this )
593 osd_menu_t *p_osd = NULL;
594 osd_button_t *p_button = NULL;
599 p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
602 msg_Err( p_this, "OSD menu volume update failed" );
606 if( p_osd->p_state && p_osd->p_state->p_volume )
608 var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
609 vlc_mutex_lock( lockval.p_address );
611 p_button = p_osd->p_state->p_volume;
612 if( p_osd->p_state->p_volume )
613 p_osd->p_state->p_visible = p_osd->p_state->p_volume;
614 if( p_button && p_button->b_range )
616 /* Update the volume state images to match the current volume */
617 i_volume = config_GetInt( p_this, "volume" );
618 i_steps = osd_VolumeStep( p_this, i_volume, p_button->i_ranges );
619 p_button->p_current_state = osd_VolumeStateChange( p_button->p_states, i_steps );
621 osd_UpdateState( p_osd->p_state,
622 p_button->i_x, p_button->i_y,
623 p_button->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch,
624 p_button->p_current_state->p_pic->p[Y_PLANE].i_visible_lines,
625 p_button->p_current_state->p_pic );
626 osd_SetMenuUpdate( p_osd, VLC_TRUE );
627 osd_SetMenuVisible( p_osd, VLC_TRUE );
629 vlc_object_release( (vlc_object_t*) p_osd );
630 vlc_mutex_unlock( lockval.p_address );