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