]> git.sesse.net Git - vlc/blob - src/osd/osd.c
4eb576d11d1137f76e0c87dbbc75da1e15008c80
[vlc] / src / osd / osd.c
1 /*****************************************************************************
2  * osd.c - The OSD Menu core code.
3  *****************************************************************************
4  * Copyright (C) 2005-2007 M2X
5  * $Id$
6  *
7  * Authors: Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_keys.h>
34 #include <vlc_osd.h>
35 #include <vlc_image.h>
36
37 #include "libvlc.h"
38
39 #undef OSD_MENU_DEBUG
40
41 #if 0
42 static const char *ppsz_button_states[] = { "unselect", "select", "pressed" };
43 #endif
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48
49 static void osd_UpdateState( osd_menu_state_t *, int, int, int, int, picture_t * );
50 static inline osd_state_t *osd_VolumeStateChange( osd_state_t *, int );
51 static int osd_VolumeStep( vlc_object_t *, int, int );
52 static bool osd_isVisible( osd_menu_t *p_osd );
53 static osd_menu_t *osd_ParserLoad( vlc_object_t *, const char * );
54 static void osd_ParserUnload( osd_menu_t * );
55
56 static bool osd_isVisible( osd_menu_t *p_osd )
57 {
58     vlc_value_t val;
59
60     var_Get( p_osd, "osd-menu-visible", &val );
61     return val.b_bool;
62 }
63
64 /*****************************************************************************
65  * Wrappers for loading and unloading osd parser modules.
66  *****************************************************************************/
67 static osd_menu_t *osd_ParserLoad( vlc_object_t *p_this, const char *psz_file )
68 {
69     osd_menu_t *p_menu;
70     static const char osdmenu_name[] = "osd menu";
71
72     p_menu = vlc_custom_create( p_this, sizeof( *p_menu ), VLC_OBJECT_OSDMENU,
73                                 osdmenu_name );
74     if( !p_menu )
75         return NULL;
76
77     vlc_object_attach( p_menu, p_this->p_libvlc );
78
79     /* Stuff needed for Parser */
80     p_menu->psz_file = strdup( psz_file );
81     p_menu->p_image = image_HandlerCreate( p_this );
82     if( !p_menu->p_image || !p_menu->psz_file )
83     {
84         msg_Err( p_this, "unable to load images, aborting .." );
85         osd_ParserUnload( p_menu );
86         return NULL;
87     }
88     else
89     {
90         char *psz_type;
91         char *psz_ext = strrchr( p_menu->psz_file, '.' );
92
93         if( psz_ext && !strcmp( psz_ext, ".cfg") )
94             psz_type = (char*)"import-osd";
95         else
96             psz_type = (char*)"import-osd-xml";
97
98         p_menu->p_parser = module_Need( p_menu, "osd parser",
99                                         psz_type, true );
100         if( !p_menu->p_parser )
101         {
102             osd_ParserUnload( p_menu );
103             return NULL;
104         }
105     }
106     return p_menu;
107 }
108
109 static void osd_ParserUnload( osd_menu_t *p_menu )
110 {
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 );
115
116     if( p_menu->p_parser )
117         module_Unneed( p_menu, p_menu->p_parser );
118
119     vlc_object_detach( p_menu );
120     vlc_object_release( p_menu );
121 }
122
123 /**
124  * Change state on an osd_button_t.
125  *
126  * This function selects the specified state and returns a pointer to it. The
127  * following states are currently supported:
128  * \see OSD_BUTTON_UNSELECT
129  * \see OSD_BUTTON_SELECT
130  * \see OSD_BUTTON_PRESSED
131  */
132 static osd_state_t *osd_StateChange( osd_button_t *p_button, const int i_state )
133 {
134     osd_state_t *p_current = p_button->p_states;
135     osd_state_t *p_temp = NULL;
136     int i = 0;
137
138     for( i=0; p_current != NULL; i++ )
139     {
140         if( p_current->i_state == i_state )
141         {
142             p_button->i_x = p_current->i_x;
143             p_button->i_y = p_current->i_y;
144             p_button->i_width = p_current->i_width;
145             p_button->i_height = p_current->i_height;
146             return p_current;
147         }
148         p_temp = p_current->p_next;
149         p_current = p_temp;
150     }
151     return p_button->p_states;
152 }
153
154 /*****************************************************************************
155  * OSD menu Funtions
156  *****************************************************************************/
157 osd_menu_t *__osd_MenuCreate( vlc_object_t *p_this, const char *psz_file )
158 {
159     osd_menu_t  *p_osd = NULL;
160     vlc_value_t lockval;
161     int         i_volume = 0;
162     int         i_steps = 0;
163
164     /* to be sure to avoid multiple creation */
165     var_Create( p_this->p_libvlc, "osd_mutex", VLC_VAR_MUTEX );
166     var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
167     vlc_mutex_lock( lockval.p_address );
168
169     p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
170     if( p_osd == NULL )
171     {
172         vlc_value_t val;
173
174         /* Parse configuration file */
175         p_osd = osd_ParserLoad( p_this, psz_file );
176         if( !p_osd )
177             goto error;
178
179         /* Setup default button (first button) */
180         p_osd->p_state->p_visible = p_osd->p_button;
181         p_osd->p_state->p_visible->p_current_state =
182             osd_StateChange( p_osd->p_state->p_visible, OSD_BUTTON_SELECT );
183         p_osd->i_width  = p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_pitch;
184         p_osd->i_height = p_osd->p_state->p_visible->p_current_state->p_pic->p[Y_PLANE].i_visible_lines;
185
186         if( p_osd->p_state->p_volume )
187         {
188             /* Update the volume state images to match the current volume */
189             i_volume = config_GetInt( p_this, "volume" );
190             i_steps = osd_VolumeStep( p_this, i_volume, p_osd->p_state->p_volume->i_ranges );
191             p_osd->p_state->p_volume->p_current_state = osd_VolumeStateChange(
192                                     p_osd->p_state->p_volume->p_states, i_steps );
193         }
194         /* Initialize OSD state */
195         osd_UpdateState( p_osd->p_state, p_osd->i_x, p_osd->i_y,
196                          p_osd->i_width, p_osd->i_height, NULL );
197
198         /* Signal when an update of OSD menu is needed */
199         var_Create( p_osd, "osd-menu-update", VLC_VAR_BOOL );
200         var_Create( p_osd, "osd-menu-visible", VLC_VAR_BOOL );
201
202         val.b_bool = false;
203         var_Set( p_osd, "osd-menu-update", val );
204         var_Set( p_osd, "osd-menu-visible", val );
205     }
206     vlc_mutex_unlock( lockval.p_address );
207     return p_osd;
208
209 error:
210     msg_Err( p_this, "creating OSD menu object failed" );
211
212     if( p_osd->p_image )
213         image_HandlerDelete( p_osd->p_image );
214     if( p_osd->psz_file )
215         free( p_osd->psz_file );
216
217     vlc_object_detach( p_osd );
218     vlc_object_release( p_osd );
219     vlc_mutex_unlock( lockval.p_address );
220     return NULL;
221 }
222
223 void __osd_MenuDelete( vlc_object_t *p_this, osd_menu_t *p_osd )
224 {
225     vlc_value_t lockval;
226
227     if( !p_osd || !p_this ) return;
228
229     var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
230     vlc_mutex_lock( lockval.p_address );
231
232     vlc_object_release( p_osd );
233     if( vlc_internals( VLC_OBJECT(p_osd) )->i_refcount > 0 )
234     {
235         vlc_mutex_unlock( lockval.p_address );
236         return;
237     }
238
239     var_Destroy( p_osd, "osd-menu-visible" );
240     var_Destroy( p_osd, "osd-menu-update" );
241
242     osd_ParserUnload( p_osd );
243     p_osd = NULL;
244     vlc_mutex_unlock( lockval.p_address );
245 }
246
247 /* The volume can be modified in another interface while the OSD Menu
248  * has not been instantiated yet. This routines updates the "volume OSD menu item"
249  * to reflect the current state of the GUI.
250  */
251 static inline osd_state_t *osd_VolumeStateChange( osd_state_t *p_current, int i_steps )
252 {
253     osd_state_t *p_temp = NULL;
254     int i;
255
256     if( i_steps < 0 ) i_steps = 0;
257
258     for( i=0; (i < i_steps) && (p_current != NULL); i++ )
259     {
260         p_temp = p_current->p_next;
261         if( !p_temp ) return p_current;
262         p_current = p_temp;
263     }
264     return (!p_temp) ? p_current : p_temp;
265 }
266
267 /* Update the state of the OSD Menu */
268 static void osd_UpdateState( osd_menu_state_t *p_state, int i_x, int i_y,
269         int i_width, int i_height, picture_t *p_pic )
270 {
271     p_state->i_x = i_x;
272     p_state->i_y = i_y;
273     p_state->i_width = i_width;
274     p_state->i_height = i_height;
275     p_state->p_pic = p_pic;
276 }
277
278 void __osd_MenuShow( vlc_object_t *p_this )
279 {
280     osd_menu_t *p_osd = NULL;
281     osd_button_t *p_button = NULL;
282     vlc_value_t lockval;
283
284     p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
285     if( p_osd == NULL )
286     {
287         msg_Err( p_this, "osd_MenuNext failed" );
288         return;
289     }
290
291     var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
292     vlc_mutex_lock( lockval.p_address );
293
294 #if defined(OSD_MENU_DEBUG)
295     msg_Dbg( p_osd, "menu on" );
296 #endif
297     p_button = p_osd->p_state->p_visible;
298     if( p_button )
299     {
300         if( !p_button->b_range )
301             p_button->p_current_state = osd_StateChange( p_button, OSD_BUTTON_UNSELECT );
302         p_osd->p_state->p_visible = p_osd->p_button;
303
304         if( !p_osd->p_state->p_visible->b_range )
305             p_osd->p_state->p_visible->p_current_state =
306                 osd_StateChange( p_osd->p_state->p_visible, OSD_BUTTON_SELECT );
307
308         osd_UpdateState( p_osd->p_state,
309                 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
310                 p_osd->p_state->p_visible->p_current_state->i_width,
311                 p_osd->p_state->p_visible->p_current_state->i_height,
312                 p_osd->p_state->p_visible->p_current_state->p_pic );
313         osd_SetMenuUpdate( p_osd, true );
314     }
315     osd_SetMenuVisible( p_osd, true );
316
317     vlc_object_release( (vlc_object_t*) p_osd );
318     vlc_mutex_unlock( lockval.p_address );
319 }
320
321 void __osd_MenuHide( vlc_object_t *p_this )
322 {
323     osd_menu_t *p_osd = NULL;
324     vlc_value_t lockval;
325
326     p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
327     if( p_osd == NULL )
328     {
329         msg_Err( p_this, "osd_MenuNext failed" );
330         return;
331     }
332
333     var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
334     vlc_mutex_lock( lockval.p_address );
335
336 #if defined(OSD_MENU_DEBUG)
337     msg_Dbg( p_osd, "menu off" );
338 #endif
339     osd_UpdateState( p_osd->p_state,
340                 p_osd->p_state->i_x, p_osd->p_state->i_y,
341                 0, 0, NULL );
342     osd_SetMenuUpdate( p_osd, true );
343
344     vlc_object_release( (vlc_object_t*) p_osd );
345     vlc_mutex_unlock( lockval.p_address );
346 }
347
348 void __osd_MenuActivate( vlc_object_t *p_this )
349 {
350     osd_menu_t *p_osd = NULL;
351     osd_button_t *p_button = NULL;
352     vlc_value_t lockval;
353
354     p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
355     if( p_osd == NULL )
356     {
357         msg_Err( p_this, "osd_MenuNext failed" );
358         return;
359     }
360
361     if( osd_isVisible( p_osd ) == false )
362     {
363         vlc_object_release( (vlc_object_t*) p_osd );
364         return;
365     }
366
367     var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
368     vlc_mutex_lock( lockval.p_address );
369
370 #if defined(OSD_MENU_DEBUG)
371     msg_Dbg( p_osd, "select" );
372 #endif
373     p_button = p_osd->p_state->p_visible;
374     /*
375      * Is there a menu item above or below? If so, then select it.
376      */
377     if( p_button && p_button->p_up )
378     {
379         vlc_object_release( (vlc_object_t*) p_osd );
380         vlc_mutex_unlock( lockval.p_address );
381         __osd_MenuUp( p_this );   /* "menu select" means go to menu item above. */
382         return;
383     }
384     if( p_button && p_button->p_down )
385     {
386         vlc_object_release( (vlc_object_t*) p_osd );
387         vlc_mutex_unlock( lockval.p_address );
388         __osd_MenuDown( p_this ); /* "menu select" means go to menu item below. */
389         return;
390     }
391
392     if( p_button && !p_button->b_range )
393     {
394         p_button->p_current_state = osd_StateChange( p_button, OSD_BUTTON_PRESSED );
395         osd_UpdateState( p_osd->p_state,
396                 p_button->i_x, p_button->i_y,
397                 p_osd->p_state->p_visible->p_current_state->i_width,
398                 p_osd->p_state->p_visible->p_current_state->i_height,
399                 p_button->p_current_state->p_pic );
400         osd_SetMenuUpdate( p_osd, true );
401         osd_SetMenuVisible( p_osd, true );
402         osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt( p_osd, p_button->psz_action ) );
403 #if defined(OSD_MENU_DEBUG)
404         msg_Dbg( p_osd, "select (%d, %s)", config_GetInt( p_osd, p_button->psz_action ), p_button->psz_action );
405 #endif
406     }
407     vlc_object_release( (vlc_object_t*) p_osd );
408     vlc_mutex_unlock( lockval.p_address );
409 }
410
411 void __osd_MenuNext( vlc_object_t *p_this )
412 {
413     osd_menu_t *p_osd = NULL;
414     osd_button_t *p_button = NULL;
415     vlc_value_t lockval;
416
417     p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
418     if( p_osd == NULL )
419     {
420         msg_Err( p_this, "osd_MenuNext failed" );
421         return;
422     }
423
424     if( osd_isVisible( p_osd ) == false )
425     {
426         vlc_object_release( (vlc_object_t*) p_osd );
427         return;
428     }
429
430     var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
431     vlc_mutex_lock( lockval.p_address );
432
433     p_button = p_osd->p_state->p_visible;
434     if( p_button )
435     {
436         if( !p_button->b_range )
437             p_button->p_current_state = osd_StateChange( p_button, OSD_BUTTON_UNSELECT );
438         if( p_button->p_next )
439             p_osd->p_state->p_visible = p_button->p_next;
440         else
441             p_osd->p_state->p_visible = p_osd->p_button;
442
443         if( !p_osd->p_state->p_visible->b_range )
444             p_osd->p_state->p_visible->p_current_state =
445                 osd_StateChange( p_osd->p_state->p_visible, OSD_BUTTON_SELECT );
446
447         osd_UpdateState( p_osd->p_state,
448                 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
449                 p_osd->p_state->p_visible->p_current_state->i_width,
450                 p_osd->p_state->p_visible->p_current_state->i_height,
451                 p_osd->p_state->p_visible->p_current_state->p_pic );
452         osd_SetMenuUpdate( p_osd, true );
453     }
454 #if defined(OSD_MENU_DEBUG)
455     msg_Dbg( p_osd, "direction right [button %s]", p_osd->p_state->p_visible->psz_action );
456 #endif
457
458     vlc_object_release( (vlc_object_t*) p_osd );
459     vlc_mutex_unlock( lockval.p_address );
460 }
461
462 void __osd_MenuPrev( vlc_object_t *p_this )
463 {
464     osd_menu_t *p_osd = NULL;
465     osd_button_t *p_button = NULL;
466     vlc_value_t lockval;
467
468     p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
469     if( p_osd == NULL )
470     {
471         msg_Err( p_this, "osd_MenuPrev failed" );
472         return;
473     }
474
475     if( osd_isVisible( p_osd ) == false )
476     {
477         vlc_object_release( (vlc_object_t*) p_osd );
478         return;
479     }
480
481     var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
482     vlc_mutex_lock( lockval.p_address );
483
484     p_button = p_osd->p_state->p_visible;
485     if( p_button )
486     {
487         if( !p_button->b_range )
488             p_button->p_current_state = osd_StateChange( p_button, OSD_BUTTON_UNSELECT );
489         if( p_button->p_prev )
490             p_osd->p_state->p_visible = p_button->p_prev;
491         else
492             p_osd->p_state->p_visible = p_osd->p_last_button;
493
494         if( !p_osd->p_state->p_visible->b_range )
495             p_osd->p_state->p_visible->p_current_state =
496                 osd_StateChange( p_osd->p_state->p_visible, OSD_BUTTON_SELECT );
497
498         osd_UpdateState( p_osd->p_state,
499                 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
500                 p_osd->p_state->p_visible->p_current_state->i_width,
501                 p_osd->p_state->p_visible->p_current_state->i_height,
502                 p_osd->p_state->p_visible->p_current_state->p_pic );
503         osd_SetMenuUpdate( p_osd, true );
504     }
505 #if defined(OSD_MENU_DEBUG)
506     msg_Dbg( p_osd, "direction left [button %s]", p_osd->p_state->p_visible->psz_action );
507 #endif
508
509     vlc_object_release( (vlc_object_t*) p_osd );
510     vlc_mutex_unlock( lockval.p_address );
511 }
512
513 void __osd_MenuUp( vlc_object_t *p_this )
514 {
515     osd_menu_t *p_osd = NULL;
516     osd_button_t *p_button = NULL;
517     vlc_value_t lockval;
518 #if defined(OSD_MENU_DEBUG)
519     vlc_value_t val;
520 #endif
521     p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
522     if( p_osd == NULL )
523     {
524         msg_Err( p_this, "osd_MenuDown failed" );
525         return;
526     }
527
528     if( osd_isVisible( p_osd ) == false )
529     {
530         vlc_object_release( (vlc_object_t*) p_osd );
531         return;
532     }
533
534     var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
535     vlc_mutex_lock( lockval.p_address );
536
537     p_button = p_osd->p_state->p_visible;
538     if( p_button )
539     {
540         if( !p_button->b_range )
541         {
542             p_button->p_current_state = osd_StateChange( p_button, OSD_BUTTON_SELECT );
543             if( p_button->p_up )
544                 p_osd->p_state->p_visible = p_button->p_up;
545         }
546
547         if( p_button->b_range && p_osd->p_state->p_visible->b_range )
548         {
549             osd_state_t *p_temp = p_osd->p_state->p_visible->p_current_state;
550             if( p_temp && p_temp->p_next )
551                 p_osd->p_state->p_visible->p_current_state = p_temp->p_next;
552         }
553         else if( !p_osd->p_state->p_visible->b_range )
554         {
555             p_osd->p_state->p_visible->p_current_state =
556                 osd_StateChange( p_osd->p_state->p_visible, OSD_BUTTON_SELECT );
557         }
558
559         osd_UpdateState( p_osd->p_state,
560                 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
561                 p_osd->p_state->p_visible->p_current_state->i_width,
562                 p_osd->p_state->p_visible->p_current_state->i_height,
563                 p_osd->p_state->p_visible->p_current_state->p_pic );
564         osd_SetMenuUpdate( p_osd, true );
565         /* If this is a range style action with associated images of only one state,
566             * then perform "menu select" on every menu navigation
567             */
568         if( p_button->b_range )
569         {
570             osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt(p_osd, p_button->psz_action) );
571 #if defined(OSD_MENU_DEBUG)
572             msg_Dbg( p_osd, "select (%d, %s)", val.i_int, p_button->psz_action );
573 #endif
574         }
575     }
576 #if defined(OSD_MENU_DEBUG)
577     msg_Dbg( p_osd, "direction up [button %s]", p_osd->p_state->p_visible->psz_action );
578 #endif
579
580     vlc_object_release( (vlc_object_t*) p_osd );
581     vlc_mutex_unlock( lockval.p_address );
582 }
583
584 void __osd_MenuDown( vlc_object_t *p_this )
585 {
586     osd_menu_t *p_osd = NULL;
587     osd_button_t *p_button = NULL;
588     vlc_value_t lockval;
589 #if defined(OSD_MENU_DEBUG)
590     vlc_value_t val;
591 #endif
592
593     p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
594     if( p_osd == NULL )
595     {
596         msg_Err( p_this, "osd_MenuDown failed" );
597         return;
598     }
599
600     if( osd_isVisible( p_osd ) == false )
601     {
602         vlc_object_release( (vlc_object_t*) p_osd );
603         return;
604     }
605
606     var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
607     vlc_mutex_lock( lockval.p_address );
608
609     p_button = p_osd->p_state->p_visible;
610     if( p_button )
611     {
612         if( !p_button->b_range )
613         {
614             p_button->p_current_state = osd_StateChange( p_button, OSD_BUTTON_SELECT );
615             if( p_button->p_down )
616                 p_osd->p_state->p_visible = p_button->p_down;
617         }
618
619         if( p_button->b_range && p_osd->p_state->p_visible->b_range )
620         {
621             osd_state_t *p_temp = p_osd->p_state->p_visible->p_current_state;
622             if( p_temp && p_temp->p_prev )
623                 p_osd->p_state->p_visible->p_current_state = p_temp->p_prev;
624         }
625         else if( !p_osd->p_state->p_visible->b_range )
626         {
627             p_osd->p_state->p_visible->p_current_state =
628                 osd_StateChange( p_osd->p_state->p_visible, OSD_BUTTON_SELECT );
629         }
630
631         osd_UpdateState( p_osd->p_state,
632                 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
633                 p_osd->p_state->p_visible->p_current_state->i_width,
634                 p_osd->p_state->p_visible->p_current_state->i_height,
635                 p_osd->p_state->p_visible->p_current_state->p_pic );
636         osd_SetMenuUpdate( p_osd, true );
637         /* If this is a range style action with associated images of only one state,
638          * then perform "menu select" on every menu navigation
639          */
640         if( p_button->b_range )
641         {
642             osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt(p_osd, p_button->psz_action_down) );
643 #if defined(OSD_MENU_DEBUG)
644             msg_Dbg( p_osd, "select (%d, %s)", val.i_int, p_button->psz_action_down );
645 #endif
646         }
647     }
648 #if defined(OSD_MENU_DEBUG)
649     msg_Dbg( p_osd, "direction down [button %s]", p_osd->p_state->p_visible->psz_action );
650 #endif
651
652     vlc_object_release( (vlc_object_t*) p_osd );
653     vlc_mutex_unlock( lockval.p_address );
654 }
655
656 static int osd_VolumeStep( vlc_object_t *p_this, int i_volume, int i_steps )
657 {
658     int i_volume_step = 0;
659     (void)i_steps;
660
661     i_volume_step = config_GetInt( p_this->p_libvlc, "volume-step" );
662     return (i_volume/i_volume_step);
663 }
664
665 /**
666  * Display current audio volume bitmap
667  *
668  * The OSD Menu audio volume bar is updated to reflect the new audio volume. Call this function
669  * when the audio volume is updated outside the OSD menu command "menu up", "menu down" or "menu select".
670  */
671 void __osd_Volume( vlc_object_t *p_this )
672 {
673     osd_menu_t *p_osd = NULL;
674     osd_button_t *p_button = NULL;
675     vlc_value_t lockval;
676     int i_volume = 0;
677     int i_steps = 0;
678
679     p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
680     if( p_osd == NULL )
681     {
682         msg_Err( p_this, "OSD menu volume update failed" );
683         return;
684     }
685
686     if( p_osd->p_state && p_osd->p_state->p_volume )
687     {
688         var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
689         vlc_mutex_lock( lockval.p_address );
690
691         p_button = p_osd->p_state->p_volume;
692         if( p_osd->p_state->p_volume )
693             p_osd->p_state->p_visible = p_osd->p_state->p_volume;
694         if( p_button && p_button->b_range )
695         {
696             /* Update the volume state images to match the current volume */
697             i_volume = config_GetInt( p_this, "volume" );
698             i_steps = osd_VolumeStep( p_this, i_volume, p_button->i_ranges );
699             p_button->p_current_state = osd_VolumeStateChange( p_button->p_states, i_steps );
700
701             osd_UpdateState( p_osd->p_state,
702                     p_button->i_x, p_button->i_y,
703                     p_button->p_current_state->i_width,
704                     p_button->p_current_state->i_height,
705                     p_button->p_current_state->p_pic );
706             osd_SetMenuUpdate( p_osd, true );
707             osd_SetMenuVisible( p_osd, true );
708         }
709         vlc_object_release( (vlc_object_t*) p_osd );
710         vlc_mutex_unlock( lockval.p_address );
711     }
712 }
713
714 osd_button_t *__osd_ButtonFind( vlc_object_t *p_this, int i_x, int i_y,
715     int i_window_height, int i_window_width,
716     int i_scale_width, int i_scale_height )
717 {
718     osd_menu_t *p_osd;
719     osd_button_t *p_button;
720     vlc_value_t lockval;
721
722     p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
723     if( p_osd == NULL )
724     {
725         msg_Err( p_this, "OSD menu button find failed" );
726         return NULL;
727     }
728
729     if( osd_isVisible( p_osd ) == false )
730     {
731         vlc_object_release( (vlc_object_t*) p_osd );
732         return NULL;
733     }
734
735     var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
736     vlc_mutex_lock( lockval.p_address );
737
738     p_button = p_osd->p_button;
739     for( ; p_button != NULL; p_button = p_button->p_next )
740     {
741         int i_source_video_width  = ( i_window_width  * 1000 ) / i_scale_width;
742         int i_source_video_height = ( i_window_height * 1000 ) / i_scale_height;
743         int i_y_offset = p_button->i_y;
744         int i_x_offset = p_button->i_x;
745         int i_width = p_button->i_width;
746         int i_height = p_button->i_height;
747
748         if( p_osd->i_position > 0 )
749         {
750             int i_inv_scale_y = i_source_video_height;
751             int i_inv_scale_x = i_source_video_width;
752             int pi_x = 0;
753
754             if( p_osd->i_position & SUBPICTURE_ALIGN_BOTTOM )
755             {
756                 i_y_offset = i_window_height - p_button->i_height -
757                     (p_osd->i_y + p_button->i_y) * i_inv_scale_y / 1000;
758             }
759             else if ( !(p_osd->i_position & SUBPICTURE_ALIGN_TOP) )
760             {
761                 i_y_offset = i_window_height / 2 - p_button->i_height / 2;
762             }
763
764             if( p_osd->i_position & SUBPICTURE_ALIGN_RIGHT )
765             {
766                 i_x_offset = i_window_width - p_button->i_width -
767                     (pi_x + p_button->i_x)
768                     * i_inv_scale_x / 1000;
769             }
770             else if ( !(p_osd->i_position & SUBPICTURE_ALIGN_LEFT) )
771             {
772                 i_x_offset = i_window_width / 2 - p_button->i_width / 2;
773             }
774
775             i_width = i_window_width - p_button->i_width - i_inv_scale_x / 1000;
776             i_height = i_window_height - p_button->i_height - i_inv_scale_y / 1000;
777         }
778
779         // TODO: write for Up / Down case too.
780         // TODO: handle absolute positioning case
781         if( ( i_x >= i_x_offset ) && ( i_x <= i_x_offset + i_width ) &&
782             ( i_y >= i_y_offset ) && ( i_y <= i_y_offset + i_height ) )
783         {
784             vlc_object_release( (vlc_object_t*) p_osd );
785             vlc_mutex_unlock( lockval.p_address );
786             return p_button;
787         }
788     }
789
790     vlc_object_release( (vlc_object_t*) p_osd );
791     vlc_mutex_unlock( lockval.p_address );
792     return NULL;
793 }
794
795 /**
796  * Select the button provided as the new active button
797  */
798 void __osd_ButtonSelect( vlc_object_t *p_this, osd_button_t *p_button )
799 {
800     osd_menu_t *p_osd;
801     osd_button_t *p_old;
802     vlc_value_t lockval;
803
804     p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
805     if( p_osd == NULL )
806     {
807         msg_Err( p_this, "OSD menu button select failed" );
808         return;
809     }
810
811     if( osd_isVisible( p_osd ) == false )
812     {
813         vlc_object_release( (vlc_object_t*) p_osd );
814         return;
815     }
816
817     var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
818     vlc_mutex_lock( lockval.p_address );
819
820     p_old = p_osd->p_state->p_visible;
821     if( p_old )
822     {
823         if( !p_old->b_range )
824             p_old->p_current_state = osd_StateChange( p_old, OSD_BUTTON_UNSELECT );
825         p_osd->p_state->p_visible = p_button;
826
827         if( !p_osd->p_state->p_visible->b_range )
828             p_osd->p_state->p_visible->p_current_state =
829                 osd_StateChange( p_osd->p_state->p_visible, OSD_BUTTON_SELECT );
830
831         osd_UpdateState( p_osd->p_state,
832                 p_osd->p_state->p_visible->i_x, p_osd->p_state->p_visible->i_y,
833                 p_osd->p_state->p_visible->p_current_state->i_width,
834                 p_osd->p_state->p_visible->p_current_state->i_height,
835                 p_osd->p_state->p_visible->p_current_state->p_pic );
836         osd_SetMenuUpdate( p_osd, true );
837     }
838 #if defined(OSD_MENU_DEBUG)
839     msg_Dbg( p_osd, "button selected is [button %s]", p_osd->p_state->p_visible->psz_action );
840 #endif
841
842     vlc_object_release( (vlc_object_t*) p_osd );
843     vlc_mutex_unlock( lockval.p_address );
844 }