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