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