1 /*****************************************************************************
2 * maemo_menus.c : menus creation for the maemo plugin
3 *****************************************************************************
4 * Copyright (C) 2010 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include <vlc_common.h>
25 #include <vlc_interface.h>
30 #include "maemo_callbacks.h"
32 /*****************************************************************************
34 *****************************************************************************/
35 static GtkMenu *Populate( intf_thread_t *p_intf, GtkMenu *menu,
36 const char **varnames, vlc_object_t **objects,
37 unsigned int elements );
39 /*****************************************************************************
41 *****************************************************************************/
42 static input_thread_t *get_input(intf_thread_t *p_intf)
44 return p_intf->p_sys->p_input;
47 static vlc_object_t *get_vout(intf_thread_t *p_intf)
49 return (vlc_object_t *)(p_intf->p_sys->p_input ?
50 input_GetVout( p_intf->p_sys->p_input ) : 0);
53 static vlc_object_t *get_aout(intf_thread_t *p_intf)
55 return (vlc_object_t *)(p_intf->p_sys->p_input ?
56 input_GetAout( p_intf->p_sys->p_input ) : 0);
59 static gint quit_event( GtkWidget *widget, gpointer data )
61 intf_thread_t *p_intf = (intf_thread_t *)data;
63 libvlc_Quit( p_intf->p_libvlc );
67 /*****************************************************************************
68 * Definitions of variables for the dynamic menus
69 *****************************************************************************/
72 #define PUSH_VAR( var ) \
73 ppsz_varnames[index] = var; \
74 p_objects[index] = VLC_OBJECT(p_object); \
75 if(index < SIZE_LIST - 1) index++; \
76 ppsz_varnames[index] = 0; p_objects[index] = 0;
78 #define PUSH_INPUTVAR( var ) \
79 ppsz_varnames[index] = var; \
80 p_objects[index] = VLC_OBJECT(p_input); \
81 if(index < SIZE_LIST - 1) index++; \
82 ppsz_varnames[index] = 0; p_objects[index] = 0;
84 #define ADD_MENU_ITEM( label, callback ) \
85 item = gtk_menu_item_new_with_label( label ); \
86 gtk_menu_append( main_menu, item ); \
87 g_signal_connect( GTK_OBJECT( item ), "activate", G_CALLBACK( callback ), \
90 #define ADD_SEPARATOR() \
91 item = gtk_separator_menu_item_new(); \
92 gtk_menu_append( main_menu, item );
96 static GtkMenu *create_video_menu( intf_thread_t *p_intf )
98 vlc_object_t *p_object = get_vout(p_intf);
99 input_thread_t *p_input = get_input(p_intf);;
101 vlc_object_t *p_objects[SIZE_LIST];
102 const char *ppsz_varnames[SIZE_LIST];
105 PUSH_INPUTVAR( "video-es" );
106 PUSH_INPUTVAR( "spu-es" );
107 PUSH_VAR( "fullscreen" );
108 PUSH_VAR( "video-wallpaper" );
109 PUSH_VAR( "video-snapshot" );
111 PUSH_VAR( "autoscale" );
112 PUSH_VAR( "aspect-ratio" );
114 PUSH_VAR( "deinterlace" );
115 PUSH_VAR( "deinterlace-mode" );
116 PUSH_VAR( "postprocess" );
118 GtkWidget *menu = gtk_menu_new();
119 return Populate( p_intf, GTK_MENU(menu), ppsz_varnames, p_objects, index );
122 static GtkMenu *create_audio_menu( intf_thread_t *p_intf )
124 vlc_object_t *p_object = get_aout(p_intf);
125 input_thread_t *p_input = get_input(p_intf);;
127 vlc_object_t *p_objects[SIZE_LIST];
128 const char *ppsz_varnames[SIZE_LIST];
131 PUSH_INPUTVAR( "audio-es" );
132 PUSH_VAR( "audio-channels" );
133 PUSH_VAR( "audio-device" );
134 PUSH_VAR( "visual" );
136 GtkWidget *menu = gtk_menu_new();
137 return Populate( p_intf, GTK_MENU(menu), ppsz_varnames, p_objects, index );
140 static GtkMenu *create_input_menu( intf_thread_t *p_intf )
142 input_thread_t *p_object = get_input(p_intf);
144 vlc_object_t *p_objects[SIZE_LIST];
145 const char *ppsz_varnames[SIZE_LIST];
148 PUSH_VAR( "bookmark" );
150 PUSH_VAR( "chapter" );
151 PUSH_VAR( "navigation" );
152 PUSH_VAR( "program" );
154 GtkWidget *menu = gtk_menu_new();
155 return Populate( p_intf, GTK_MENU(menu), ppsz_varnames, p_objects, index );
158 static void toplevel_menu_callback(GtkMenuItem *menuitem, gpointer data)
160 intf_thread_t *p_intf = (intf_thread_t *)data;
161 GtkMenu *(*pf_menu)(intf_thread_t *) = 0;
164 if(menuitem == p_intf->p_sys->menu_input) pf_menu = create_input_menu;
165 else if(menuitem == p_intf->p_sys->menu_audio) pf_menu = create_audio_menu;
166 else if(menuitem == p_intf->p_sys->menu_video) pf_menu = create_video_menu;
169 menu = GTK_MENU(gtk_menu_item_get_submenu(menuitem));
170 if(menu) gtk_object_destroy( GTK_OBJECT(menu) );
171 menu = pf_menu(p_intf);
172 gtk_menu_item_set_submenu(menuitem, GTK_WIDGET(menu));
173 gtk_widget_show_all( GTK_WIDGET(menuitem) );
176 GtkWidget *create_menu( intf_thread_t *p_intf )
178 intf_sys_t *p_sys = p_intf->p_sys;
179 GtkWidget *main_menu, *item;
181 /* Creating the main menu */
182 main_menu = gtk_menu_new();
184 /* Filling the menu */
185 ADD_MENU_ITEM( "Open", open_cb );
186 ADD_MENU_ITEM( "Open Address", open_address_cb );
187 ADD_MENU_ITEM( "Open Webcam", open_webcam_cb );
190 item = gtk_menu_item_new_with_label ("Playback");
191 p_sys->menu_input = GTK_MENU_ITEM(item);
192 gtk_menu_bar_append(main_menu, item);
193 g_signal_connect( GTK_OBJECT(item), "activate",
194 G_CALLBACK( toplevel_menu_callback ), p_intf );
196 item = gtk_menu_item_new_with_label ("Audio");
197 p_sys->menu_audio = GTK_MENU_ITEM(item);
198 gtk_menu_bar_append(main_menu, item);
199 g_signal_connect( GTK_OBJECT(item), "activate",
200 G_CALLBACK( toplevel_menu_callback ), p_intf );
202 item = gtk_menu_item_new_with_label ("Video");
203 p_sys->menu_video = GTK_MENU_ITEM(item);
204 gtk_menu_bar_append(main_menu, item);
205 g_signal_connect( GTK_OBJECT(item), "activate",
206 G_CALLBACK( toplevel_menu_callback ), p_intf );
208 toplevel_menu_callback(p_sys->menu_input, p_intf);
209 toplevel_menu_callback(p_sys->menu_video, p_intf);
210 toplevel_menu_callback(p_sys->menu_audio, p_intf);
213 ADD_MENU_ITEM( "Exit", quit_event );
215 gtk_widget_show_all( main_menu );
219 /*************************************************************************
220 * Builders for automenus
221 *************************************************************************/
229 typedef struct VlcMenuItemClass
231 GtkRadioMenuItemClass menuitemclass;
235 typedef struct VlcMenuItem
237 GtkRadioMenuItem menuitem;
246 static void vlc_menu_item_destroy (GtkObject *object)
248 VlcMenuItem *menuitem = (VlcMenuItem *)object;
250 if(menuitem->i_type == VLC_VAR_STRING && menuitem->val.psz_string)
251 free(menuitem->val.psz_string);
252 gtk_object_destroy( object );
255 static void vlc_menu_item_class_init (VlcMenuItemClass *klass)
257 GtkObjectClass *object_class = (GtkObjectClass*)klass;
258 object_class->destroy = vlc_menu_item_destroy;
261 static void vlc_menu_item_init (VlcMenuItem *menuitem){(void)menuitem;}
263 static GtkType vlc_menu_item_get_type (void)
265 static GtkType vlc_menu_item_type = 0;
267 if (!vlc_menu_item_type)
269 static const GtkTypeInfo vlc_menu_item_info =
271 (char *)"VlcMenuItem",
272 sizeof (VlcMenuItem),
273 sizeof (VlcMenuItemClass),
274 (GtkClassInitFunc) vlc_menu_item_class_init,
275 (GtkObjectInitFunc) vlc_menu_item_init,
276 /* reserved_1 */ NULL,
277 /* reserved_2 */ NULL,
278 (GtkClassInitFunc) NULL,
281 vlc_menu_item_type = gtk_type_unique (GTK_TYPE_MENU_ITEM, &vlc_menu_item_info);
284 return vlc_menu_item_type;
287 static GtkType vlc_check_menu_item_get_type (void)
289 static GtkType vlc_check_menu_item_type = 0;
291 if (!vlc_check_menu_item_type)
293 static const GtkTypeInfo vlc_check_menu_item_info =
295 (char *)"VlcCheckMenuItem",
296 sizeof (VlcMenuItem),
297 sizeof (VlcMenuItemClass),
298 (GtkClassInitFunc) vlc_menu_item_class_init,
299 (GtkObjectInitFunc) vlc_menu_item_init,
300 /* reserved_1 */ NULL,
301 /* reserved_2 */ NULL,
302 (GtkClassInitFunc) NULL,
305 vlc_check_menu_item_type = gtk_type_unique (GTK_TYPE_CHECK_MENU_ITEM, &vlc_check_menu_item_info);
308 return vlc_check_menu_item_type;
311 static GtkType vlc_radio_menu_item_get_type (void)
313 static GtkType vlc_radio_menu_item_type = 0;
315 if (!vlc_radio_menu_item_type)
317 static const GtkTypeInfo vlc_radio_menu_item_info =
319 (char *)"VlcRadioMenuItem",
320 sizeof (VlcMenuItem),
321 sizeof (VlcMenuItemClass),
322 (GtkClassInitFunc) vlc_menu_item_class_init,
323 (GtkObjectInitFunc) vlc_menu_item_init,
324 /* reserved_1 */ NULL,
325 /* reserved_2 */ NULL,
326 (GtkClassInitFunc) NULL,
329 vlc_radio_menu_item_type = gtk_type_unique (GTK_TYPE_RADIO_MENU_ITEM, &vlc_radio_menu_item_info);
332 return vlc_radio_menu_item_type;
335 static GtkWidget *vlc_menu_item_new (vlc_object_t *p_obj, int i_type,
336 vlc_value_t val, const char *var)
343 item = (VlcMenuItem *)gtk_type_new (vlc_check_menu_item_get_type ());
346 item = (VlcMenuItem *)gtk_type_new (vlc_radio_menu_item_get_type ());
349 item = (VlcMenuItem *)gtk_type_new (vlc_menu_item_get_type ());
353 item->i_type = i_type;
356 return GTK_WIDGET (item);
361 static int CreateChoicesMenu( intf_thread_t *p_intf, GtkMenu *submenu, const char *psz_var,
362 vlc_object_t *p_object, bool b_root );
364 static void menu_callback(GtkMenuItem *menuitem, gpointer user_data)
366 VlcMenuItem *item = (VlcMenuItem *)menuitem;
367 vlc_object_t *p_object = item->p_obj;
369 if( p_object == NULL ) return;
370 var_Set( p_object, item->psz_var, item->val );
373 static void CreateAndConnect( intf_thread_t *p_intf,
374 GtkMenu *menu, const char *psz_var,
375 const char *text, const char *help,
376 int i_item_type, vlc_object_t *p_obj,
377 vlc_value_t val, int i_val_type,
380 GtkMenuItem *menu_item =
381 (GtkMenuItem *)vlc_menu_item_new (p_obj, i_item_type, val, psz_var );
383 (void)help; (void)i_val_type;
385 #if GTK_CHECK_VERSION(2,16,0)
386 gtk_menu_item_set_label (menu_item, text ? text : psz_var);
388 GtkWidget *accel_label = gtk_accel_label_new(text ? text : psz_var);
389 gtk_misc_set_alignment(GTK_MISC (accel_label), 0.0, 0.5);
390 gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
391 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), GTK_WIDGET(menu_item));
392 gtk_widget_show (accel_label);
393 #endif /* GTK_CHECK_VERSION(2,16,0) */
395 gtk_menu_append( GTK_WIDGET(menu), GTK_WIDGET(menu_item) );
397 if( i_item_type == ITEM_CHECK || i_item_type == ITEM_RADIO )
398 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), checked);
400 g_signal_connect( GTK_OBJECT(menu_item), "activate", G_CALLBACK( menu_callback ),
404 static bool IsMenuEmpty( const char *psz_var,
405 vlc_object_t *p_object,
408 vlc_value_t val, val_list;
409 int i_type, i_result, i;
411 /* Check the type of the object variable */
412 i_type = var_Type( p_object, psz_var );
414 /* Check if we want to display the variable */
415 if( !( i_type & VLC_VAR_HASCHOICE ) ) return false;
417 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
418 if( val.i_int == 0 ) return true;
420 if( ( i_type & VLC_VAR_TYPE ) != VLC_VAR_VARIABLE )
422 if( val.i_int == 1 && b_root ) return true;
426 /* Check children variables in case of VLC_VAR_VARIABLE */
427 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
432 for( i = 0, i_result = true; i < val_list.p_list->i_count; i++ )
434 if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
442 /* clean up everything */
443 var_FreeList( &val_list, NULL );
448 static void UpdateItem( intf_thread_t *p_intf, GtkMenu *menu,
449 const char *psz_var, vlc_object_t *p_object, bool b_submenu )
451 vlc_value_t val, text;
454 /* Check the type of the object variable */
455 /* This HACK is needed so we have a radio button for audio and video tracks
456 instread of a checkbox */
457 if( !strcmp( psz_var, "audio-es" )
458 || !strcmp( psz_var, "video-es" ) )
459 i_type = VLC_VAR_INTEGER | VLC_VAR_HASCHOICE;
461 i_type = var_Type( p_object, psz_var );
463 switch( i_type & VLC_VAR_TYPE )
467 case VLC_VAR_VARIABLE:
469 case VLC_VAR_INTEGER:
473 /* Variable doesn't exist or isn't handled */
477 /* Make sure we want to display the variable */
478 if( !g_list_length(GTK_MENU_SHELL(menu)->children) && IsMenuEmpty( psz_var, p_object, true ) )
483 /* Get the descriptive name of the variable */
484 int i_ret = var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
485 if( i_ret != VLC_SUCCESS )
487 text.psz_string = NULL;
490 /* Some specific stuff */
491 bool forceDisabled = false;
492 if( !strcmp( psz_var, "spu-es" ) )
494 vlc_object_t *p_vout = get_vout(p_intf);
495 forceDisabled = ( p_vout == NULL );
496 if( p_vout ) vlc_object_release( p_vout );
499 if( i_type & VLC_VAR_HASCHOICE )
501 /* Append choices menu */
505 gtk_menu_item_new_with_label( text.psz_string ? text.psz_string : psz_var );
506 GtkWidget *submenu = gtk_menu_new( );
507 gtk_menu_append( menu, item );
508 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
509 if( CreateChoicesMenu( p_intf, GTK_MENU(submenu), psz_var, p_object, true ) )
510 gtk_widget_set_sensitive(item, false);
513 gtk_widget_set_sensitive(item, false);
518 if( CreateChoicesMenu( p_intf, menu, psz_var, p_object, true ) )
519 gtk_widget_set_sensitive(menu, false);
521 FREENULL( text.psz_string );
525 switch( i_type & VLC_VAR_TYPE )
528 var_Get( p_object, psz_var, &val );
529 CreateAndConnect( p_intf, menu, psz_var, text.psz_string, "",
530 ITEM_NORMAL, p_object, val, i_type, false );
534 var_Get( p_object, psz_var, &val );
535 val.b_bool = !val.b_bool;
536 CreateAndConnect( p_intf, menu, psz_var, text.psz_string, "",
537 ITEM_CHECK, p_object, val, i_type, !val.b_bool );
540 FREENULL( text.psz_string );
543 /** HACK for the navigation submenu:
544 * "title %2i" variables take the value 0 if not set
546 static bool CheckTitle( vlc_object_t *p_object, const char *psz_var )
549 if( sscanf( psz_var, "title %2i", &i_title ) <= 0 )
552 int i_current_title = var_GetInteger( p_object, "title" );
553 return ( i_title == i_current_title );
557 static int CreateChoicesMenu( intf_thread_t *p_intf, GtkMenu *submenu, const char *psz_var,
558 vlc_object_t *p_object, bool b_root )
560 vlc_value_t val, val_list, text_list;
563 /* Check the type of the object variable */
564 i_type = var_Type( p_object, psz_var );
566 /* Make sure we want to display the variable */
567 if( !g_list_length(GTK_MENU_SHELL(submenu)->children) &&
568 IsMenuEmpty( psz_var, p_object, b_root ) )
571 switch( i_type & VLC_VAR_TYPE )
575 case VLC_VAR_VARIABLE:
577 case VLC_VAR_INTEGER:
581 /* Variable doesn't exist or isn't handled */
585 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
586 &val_list, &text_list ) < 0 )
591 #define CURVAL val_list.p_list->p_values[i]
592 #define CURTEXT text_list.p_list->p_values[i].psz_string
594 for( i = 0; i < val_list.p_list->i_count; i++ )
596 vlc_value_t another_val;
597 char string[16] = {0};
598 char *menutext = string;
600 switch( i_type & VLC_VAR_TYPE )
602 case VLC_VAR_VARIABLE:
604 GtkWidget *subsubmenu = gtk_menu_new();
605 GtkWidget *submenuitem =
606 gtk_menu_item_new_with_label( CURTEXT ? CURTEXT : CURVAL.psz_string );
607 gtk_menu_item_set_submenu(GTK_MENU_ITEM(submenuitem), subsubmenu);
608 gtk_menu_append( submenu, submenuitem );
609 CreateChoicesMenu( p_intf, GTK_MENU(subsubmenu), CURVAL.psz_string, p_object, false );
614 var_Get( p_object, psz_var, &val );
615 another_val.psz_string = strdup( CURVAL.psz_string );
616 menutext = CURTEXT ? CURTEXT : another_val.psz_string;
617 CreateAndConnect( p_intf, submenu, psz_var, menutext, "",
618 ITEM_RADIO, p_object, another_val, i_type,
619 val.psz_string && !strcmp( val.psz_string, CURVAL.psz_string ) );
620 free( val.psz_string );
623 case VLC_VAR_INTEGER:
624 var_Get( p_object, psz_var, &val );
625 if( CURTEXT ) menutext = CURTEXT;
626 else snprintf( menutext, sizeof(string)-1, "%"PRId64, CURVAL.i_int );
627 CreateAndConnect( p_intf, submenu, psz_var, menutext, "",
628 ITEM_RADIO, p_object, CURVAL, i_type,
629 ( CURVAL.i_int == val.i_int )
630 && CheckTitle( p_object, psz_var ) );
634 var_Get( p_object, psz_var, &val );
635 if( CURTEXT ) menutext = CURTEXT;
636 else snprintf( menutext, sizeof(string)-1, "%.2f", CURVAL.f_float );
637 CreateAndConnect( p_intf, submenu, psz_var, menutext, "",
638 ITEM_RADIO, p_object, CURVAL, i_type,
639 CURVAL.f_float == val.f_float );
647 /* clean up everything */
648 var_FreeList( &val_list, &text_list );
652 return !g_list_length(GTK_MENU_SHELL(submenu)->children) ? VLC_EGENERIC : VLC_SUCCESS;
655 static GtkMenu *Populate( intf_thread_t *p_intf, GtkMenu *menu,
656 const char **varnames, vlc_object_t **objects,
657 unsigned int elements)
659 for( unsigned int i = 0; i < elements ; i++ )
661 if( (!varnames[i] || !*varnames[i]) &&
662 g_list_length(GTK_MENU_SHELL(menu)->children) )
664 gtk_menu_append( menu, gtk_separator_menu_item_new() );
670 UpdateItem( p_intf, menu, varnames[i], objects[i], true );
674 if(!g_list_length(GTK_MENU_SHELL(menu)->children))
676 GtkWidget *menuitem = gtk_menu_item_new_with_label( "Empty" );
677 gtk_menu_append( menu, menuitem );
678 gtk_widget_set_sensitive(menuitem, false);