]> git.sesse.net Git - vlc/blob - plugins/gnome/intf_gnome.c
14a6afbd8324bb12c15dbb4bab3aa24b09618f07
[vlc] / plugins / gnome / intf_gnome.c
1 /*****************************************************************************
2  * intf_gnome.c: Gnome interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: intf_gnome.c,v 1.34 2001/05/01 04:18:18 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Stéphane Borel <stef@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #define MODULE_NAME gnome
26 #include "modules_inner.h"
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include "defs.h"
32
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <stdlib.h>                                                /* free() */
35 #include <string.h>                                            /* strerror() */
36 #include <stdio.h>
37
38 #include <gnome.h>
39
40 #include "config.h"
41 #include "common.h"
42 #include "threads.h"
43 #include "mtime.h"
44 #include "tests.h"
45 #include "modules.h"
46
47 #include "stream_control.h"
48 #include "input_ext-intf.h"
49
50 #include "intf_msg.h"
51 #include "interface.h"
52 #include "intf_playlist.h"
53
54 #include "gnome_callbacks.h"
55 #include "gnome_interface.h"
56 #include "gnome_support.h"
57 #include "intf_gnome.h"
58
59 #include "main.h"
60
61 /*****************************************************************************
62  * Local prototypes.
63  *****************************************************************************/
64 static int  intf_Probe     ( probedata_t *p_data );
65 static int  intf_Open      ( intf_thread_t *p_intf );
66 static void intf_Close     ( intf_thread_t *p_intf );
67 static void intf_Run       ( intf_thread_t *p_intf );
68
69 static gint GnomeManage    ( gpointer p_data );
70 static gint GnomeLanguageMenus( gpointer, GtkWidget *, es_descriptor_t *, gint,
71                               void (*pf_toggle)(GtkCheckMenuItem *, gpointer) );
72 static gint GnomeChapterMenu  ( gpointer, GtkWidget *,
73                               void (*pf_toggle)(GtkCheckMenuItem *, gpointer) );
74 static gint GnomeAngleMenu    ( gpointer, GtkWidget *,
75                               void (*pf_toggle)(GtkCheckMenuItem *, gpointer) );
76 static gint GnomeTitleMenu    ( gpointer, GtkWidget *, 
77                               void (*pf_toggle)(GtkCheckMenuItem *, gpointer) );
78 static gint GnomeSetupMenu    ( intf_thread_t * p_intf );
79 static void GnomeDisplayDate  ( GtkAdjustment *p_adj );
80 static gint GnomeDiscModeManage( intf_thread_t * p_intf );
81 static gint GnomeFileModeManage( intf_thread_t * p_intf );
82 static gint GnomeNetworkModeManage( intf_thread_t * p_intf );
83
84 /*****************************************************************************
85  * g_atexit: kludge to avoid the Gnome thread to segfault at exit
86  *****************************************************************************
87  * gtk_init() makes several calls to g_atexit() which calls atexit() to
88  * register tidying callbacks to be called at program exit. Since the Gnome
89  * plugin is likely to be unloaded at program exit, we have to export this
90  * symbol to intercept the g_atexit() calls. Talk about crude hack.
91  *****************************************************************************/
92 void g_atexit( GVoidFunc func )
93 {
94     intf_thread_t *p_intf = p_main->p_intf;
95
96     if( p_intf->p_sys->pf_gdk_callback == NULL )
97     {
98         p_intf->p_sys->pf_gdk_callback = func;
99     }
100     else if( p_intf->p_sys->pf_gtk_callback == NULL )
101     {
102         p_intf->p_sys->pf_gtk_callback = func;
103     }
104     /* else nothing, but we could do something here */
105     return;
106 }
107
108 /*****************************************************************************
109  * Functions exported as capabilities. They are declared as static so that
110  * we don't pollute the namespace too much.
111  *****************************************************************************/
112 void _M( intf_getfunctions )( function_list_t * p_function_list )
113 {
114     p_function_list->pf_probe = intf_Probe;
115     p_function_list->functions.intf.pf_open  = intf_Open;
116     p_function_list->functions.intf.pf_close = intf_Close;
117     p_function_list->functions.intf.pf_run   = intf_Run;
118 }
119
120 /*****************************************************************************
121  * intf_Probe: probe the interface and return a score
122  *****************************************************************************
123  * This function tries to initialize Gnome and returns a score to the
124  * plugin manager so that it can select the best plugin.
125  *****************************************************************************/
126 static int intf_Probe( probedata_t *p_data )
127 {
128     if( TestMethod( INTF_METHOD_VAR, "gnome" ) )
129     {
130         return( 999 );
131     }
132
133     if( TestProgram( "gnome-vlc" ) )
134     {
135         return( 200 );
136     }
137
138     return( 100 );
139 }
140
141 /*****************************************************************************
142  * intf_Open: initialize and create window
143  *****************************************************************************/
144 static int intf_Open( intf_thread_t *p_intf )
145 {
146     /* Allocate instance and initialize some members */
147     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
148     if( p_intf->p_sys == NULL )
149     {
150         intf_ErrMsg("error: %s", strerror(ENOMEM));
151         return( 1 );
152     }
153
154     /* Initialize Gnome thread */
155     p_intf->p_sys->b_popup_changed = 0;
156     p_intf->p_sys->b_window_changed = 0;
157     p_intf->p_sys->b_playlist_changed = 0;
158
159     p_intf->p_sys->b_slider_free = 1;
160
161     p_intf->p_sys->b_mode_changed = 1;
162     p_intf->p_sys->i_intf_mode = FILE_MODE;
163
164     p_intf->p_sys->pf_gtk_callback = NULL;
165     p_intf->p_sys->pf_gdk_callback = NULL;
166
167     return( 0 );
168 }
169
170 /*****************************************************************************
171  * intf_Close: destroy interface window
172  *****************************************************************************/
173 static void intf_Close( intf_thread_t *p_intf )
174 {
175     /* Destroy structure */
176     free( p_intf->p_sys );
177 }
178
179 /*****************************************************************************
180  * intf_Run: Gnome thread
181  *****************************************************************************
182  * this part of the interface is in a separate thread so that we can call
183  * gtk_main() from within it without annoying the rest of the program.
184  * XXX: the approach may look kludgy, and probably is, but I could not find
185  * a better way to dynamically load a Gnome interface at runtime.
186  *****************************************************************************/
187 static void intf_Run( intf_thread_t *p_intf )
188 {
189     /* gnome_init needs to know the command line. We don't care, so we
190      * give it an empty one */
191     char *p_args[] = { "" };
192     int   i_args   = 1;
193
194     /* The data types we are allowed to receive */
195     static GtkTargetEntry target_table[] =
196     {
197         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
198         { "text/plain",    0, DROP_ACCEPT_TEXT_PLAIN }
199     };
200
201     /* intf_Manage callback timeout */
202     int i_timeout;
203
204     /* Initialize Gnome */
205     gnome_init( p_main->psz_arg0, VERSION, i_args, p_args );
206
207     /* Create some useful widgets that will certainly be used */
208     p_intf->p_sys->p_window = create_intf_window( );
209     p_intf->p_sys->p_popup = create_intf_popup( );
210     p_intf->p_sys->p_disc = create_intf_disc( );
211     p_intf->p_sys->p_network = create_intf_network( );
212
213     /* Set the title of the main window */
214     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
215                           VOUT_TITLE " (Gnome interface)");
216
217     /* Accept file drops on the main window */
218     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
219                        GTK_DEST_DEFAULT_ALL, target_table,
220                        1, GDK_ACTION_COPY );
221
222     /* Get the interface labels */
223     #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
224                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
225     p_intf->p_sys->p_label_date = P_LABEL( "label_date" );
226     p_intf->p_sys->p_label_status = P_LABEL( "label_status" );
227     p_intf->p_sys->p_label_title = P_LABEL( "label_title" );
228     p_intf->p_sys->p_label_chapter = P_LABEL( "label_chapter" );
229     #undef P_LABEL
230
231     /* Connect the date display to the slider */
232     #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
233                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
234     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
235
236     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
237                          GTK_SIGNAL_FUNC( GnomeDisplayDate ), NULL );
238     p_intf->p_sys->f_adj_oldvalue = 0;
239     #undef P_SLIDER
240
241     /* We don't create these ones yet because we perhaps won't need them */
242     p_intf->p_sys->p_about = NULL;
243     p_intf->p_sys->p_playlist = NULL;
244     p_intf->p_sys->p_modules = NULL;
245     p_intf->p_sys->p_fileopen = NULL;
246
247     /* Store p_intf to keep an eye on it */
248     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
249                          "p_intf", p_intf );
250
251     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
252                          "p_intf", p_intf );
253
254     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_disc),
255                          "p_intf", p_intf );
256
257     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_network),
258                          "p_intf", p_intf );
259
260     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
261                          "p_intf", p_intf );
262
263     /* Show the control window */
264     gtk_widget_show( p_intf->p_sys->p_window );
265
266     /* Sleep to avoid using all CPU - since some interfaces needs to access
267      * keyboard events, a 100ms delay is a good compromise */
268     i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
269
270     /* Enter gnome mode */
271     gtk_main();
272
273     /* Remove the timeout */
274     gtk_timeout_remove( i_timeout );
275
276     /* Get rid of stored callbacks so we can unload the plugin */
277     if( p_intf->p_sys->pf_gtk_callback != NULL )
278     {
279         p_intf->p_sys->pf_gtk_callback( );
280         p_intf->p_sys->pf_gtk_callback = NULL;
281
282     }
283
284     if( p_intf->p_sys->pf_gdk_callback != NULL )
285     {
286         p_intf->p_sys->pf_gdk_callback( );
287         p_intf->p_sys->pf_gdk_callback = NULL;
288     }
289 }
290
291 /* following functions are local */
292
293 /*****************************************************************************
294  * GnomeManage: manage main thread messages
295  *****************************************************************************
296  * In this function, called approx. 10 times a second, we check what the
297  * main program wanted to tell us.
298  *****************************************************************************/
299 static gint GnomeManage( gpointer p_data )
300 {
301 #define p_intf ((intf_thread_t *)p_data)
302
303     vlc_mutex_lock( &p_intf->change_lock );
304
305     /* If the "display popup" flag has changed */
306     if( p_intf->b_menu_change )
307     {
308         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
309                                    NULL, NULL, NULL, NULL );
310         p_intf->b_menu_change = 0;
311     }
312
313     if( p_intf->p_input != NULL )
314     {
315         GtkWidget *     p_slider;
316         float           newvalue;
317
318 //        vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
319         /* New input or stream map change */
320         if( p_intf->p_input->stream.b_changed || p_intf->p_sys->b_mode_changed )
321         {
322             switch( p_intf->p_input->stream.i_method & 0xf0 )
323             {
324                 case INPUT_METHOD_FILE:
325                     GnomeFileModeManage( p_intf );
326                     break;
327                 case INPUT_METHOD_DISC:
328                     GnomeDiscModeManage( p_intf );
329                     break;
330                 case INPUT_METHOD_NETWORK:
331                     GnomeNetworkModeManage( p_intf );
332                     break;
333                 default:
334                     intf_ErrMsg( "intf error: can't determine input method" );
335                     break;
336             }
337             p_slider = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
338                                    p_intf->p_sys->p_window ), "slider" ) );
339
340             if( p_intf->p_input->stream.b_seekable )
341             {
342                 gtk_widget_show( GTK_WIDGET( p_slider ) );
343             }
344             else
345             {
346                 gtk_widget_hide( GTK_WIDGET( p_slider ) );
347             }
348
349             /* get ready for menu regeneration */
350             p_intf->p_sys->b_title_update = 1;
351             p_intf->p_sys->b_chapter_update = 1;
352             p_intf->p_sys->b_angle_update = 1;
353             p_intf->p_sys->b_audio_update = 1;
354             p_intf->p_sys->b_spu_update = 1;
355             p_intf->p_sys->i_part = 0;
356
357             p_intf->p_input->stream.b_changed = 0;
358             p_intf->p_sys->b_mode_changed = 0;
359             intf_WarnMsg( 2, 
360                           "intf info: menus refreshed as stream has changed" );
361
362         }
363
364 //        vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
365
366         /* Update language/chapter menus after user request */
367         GnomeSetupMenu( p_intf );
368
369 #define p_area p_intf->p_input->stream.p_selected_area
370         /* Update menus when chapter changes */
371         p_intf->p_sys->b_chapter_update =
372                     ( p_intf->p_sys->i_part != p_area->i_part );
373
374         if( p_intf->p_input->stream.b_seekable )
375         {
376             /* Manage the slider */
377             newvalue = p_intf->p_sys->p_adj->value;
378     
379             /* If the user hasn't touched the slider since the last time,
380              * then the input can safely change it */
381             if( newvalue == p_intf->p_sys->f_adj_oldvalue )
382             {
383                 /* Update the value */
384                 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
385                     ( 100. * p_area->i_tell ) / p_area->i_size;
386     
387                 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
388                                          "value_changed" );
389             }
390             /* Otherwise, send message to the input if the user has
391              * finished dragging the slider */
392             else if( p_intf->p_sys->b_slider_free )
393             {
394                 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
395     
396                 input_Seek( p_intf->p_input, i_seek );
397     
398                 /* Update the old value */
399                 p_intf->p_sys->f_adj_oldvalue = newvalue;
400             }
401         }
402 #undef p_area
403     }
404
405     /* Manage core vlc functions through the callback */
406     p_intf->pf_manage( p_intf );
407
408     if( p_intf->b_die )
409     {
410         vlc_mutex_unlock( &p_intf->change_lock );
411
412         /* Prepare to die, young Skywalker */
413         gtk_main_quit();
414
415         /* Just in case */
416         return( FALSE );
417     }
418
419     vlc_mutex_unlock( &p_intf->change_lock );
420
421     return( TRUE );
422
423 #undef p_intf
424 }
425
426 /*****************************************************************************
427  * GnomeLanguageMenus: update interactive menus of the interface
428  *****************************************************************************
429  * Sets up menus with information from input:
430  *  -languages
431  *  -sub-pictures
432  * Warning: since this function is designed to be called by management
433  * function, the interface lock has to be taken
434  *****************************************************************************/
435 static gint GnomeLanguageMenus( gpointer          p_data,
436                                 GtkWidget *       p_root,
437                                 es_descriptor_t * p_es,
438                                 gint              i_cat,
439                           void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
440 {
441 #define GNOME_LANGUAGE_MENU_SIZE 64
442     intf_thread_t *     p_intf;
443     GtkWidget *         p_menu;
444     GtkWidget *         p_separator;
445     GtkWidget *         p_item;
446     GtkWidget *         p_item_active;
447     GSList *            p_group;
448     char                psz_name[ GNOME_LANGUAGE_MENU_SIZE ];
449     gint                i_item;
450     gint                i;
451
452     
453
454     /* cast */
455     p_intf = (intf_thread_t *)p_data;
456
457     /* removes previous menu */
458     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_root ) );
459     gtk_widget_set_sensitive( p_root, FALSE );
460
461     p_group = NULL;
462
463     /* menu container */
464     p_menu = gtk_menu_new();
465
466     /* special case for "off" item */
467     snprintf( psz_name, GNOME_LANGUAGE_MENU_SIZE, "Off" );
468     psz_name[ GNOME_LANGUAGE_MENU_SIZE - 1 ] = '\0';
469
470     p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
471     p_group = gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
472
473     gtk_widget_show( p_item );
474
475     /* signal hanling for off */
476     gtk_signal_connect( GTK_OBJECT( p_item ), "toggled",
477                         GTK_SIGNAL_FUNC ( pf_toggle ), NULL );
478
479     gtk_menu_append( GTK_MENU( p_menu ), p_item );
480
481     p_separator = gtk_menu_item_new();
482     gtk_widget_set_sensitive( p_separator, FALSE );
483     gtk_widget_show( p_separator );
484     gtk_menu_append( GTK_MENU( p_menu ), p_separator );
485
486     vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
487     p_item_active = NULL;
488     i_item = 0;
489
490     /* create a set of language buttons and append them to the container */
491     for( i = 0 ; i < p_intf->p_input->stream.i_es_number ; i++ )
492     {
493         if( p_intf->p_input->stream.pp_es[i]->i_cat == i_cat )
494         {
495             i_item++;
496             strcpy( psz_name, p_intf->p_input->stream.pp_es[i]->psz_desc );
497             if( psz_name[0] == '\0' )
498             {
499                 snprintf( psz_name, GNOME_LANGUAGE_MENU_SIZE,
500                           "Language %d", i_item );
501                 psz_name[ GNOME_LANGUAGE_MENU_SIZE - 1 ] = '\0';
502             }
503
504             p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
505             p_group =
506                 gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
507
508             if( p_es == p_intf->p_input->stream.pp_es[i] )
509             {
510                 /* don't lose p_item when we append into menu */
511                 p_item_active = p_item;
512             }
513
514             gtk_widget_show( p_item );
515
516             /* setup signal hanling */
517             gtk_signal_connect( GTK_OBJECT( p_item ), "toggled",
518                             GTK_SIGNAL_FUNC( pf_toggle ),
519                             (gpointer)( p_intf->p_input->stream.pp_es[i] ) );
520
521             gtk_menu_append( GTK_MENU( p_menu ), p_item );
522         }
523     }
524
525     vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
526
527     /* link the new menu to the menubar item */
528     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
529
530     /* acitvation will call signals so we can only do it
531      * when submenu is attached to menu - to get intf_window */
532     if( p_item_active != NULL )
533     {
534         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_active ),
535                                         TRUE );
536     }
537
538     /* be sure that menu is sensitive if non empty */
539     if( i_item > 0 )
540     {
541         gtk_widget_set_sensitive( p_root, TRUE );
542     }
543
544     return TRUE;
545 }
546
547 /*****************************************************************************
548  * GnomeAngleMenu: generate angle menu for current title
549  *****************************************************************************/
550 static gint GnomeAngleMenu( gpointer p_data, GtkWidget * p_angle,
551                         void(*pf_toggle)( GtkCheckMenuItem *, gpointer ) )
552 {
553 #define GNOME_ANGLE_MENU_SIZE 64
554     intf_thread_t *     p_intf;
555     char                psz_name[ GNOME_ANGLE_MENU_SIZE ];
556     GtkWidget *         p_angle_menu;
557     GSList *            p_angle_group;
558     GtkWidget *         p_item;
559     GtkWidget *         p_item_active;
560     gint                i_angle;
561
562     /* cast */
563     p_intf = (intf_thread_t*)p_data;
564
565     /* removes previous menu */
566     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_angle ) );
567     gtk_widget_set_sensitive( p_angle, FALSE );
568
569     p_angle_menu = gtk_menu_new();;
570     p_angle_group = NULL;
571     p_item = NULL;
572     p_item_active = NULL;
573
574     for( i_angle = 0 ;
575          i_angle < p_intf->p_input->stream.p_selected_area->i_angle_nb ;
576          i_angle++ )
577     {
578         snprintf( psz_name, GNOME_ANGLE_MENU_SIZE, "Angle %d", i_angle + 1 );
579         psz_name[ GNOME_ANGLE_MENU_SIZE - 1 ] = '\0';
580
581         p_item = gtk_radio_menu_item_new_with_label( p_angle_group,
582                                                      psz_name );
583         p_angle_group =
584             gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
585
586         if( p_intf->p_input->stream.p_selected_area->i_angle ==
587                                                             ( i_angle + 1 ) )
588         {
589             p_item_active = p_item;
590         }
591
592         gtk_widget_show( p_item );
593
594         /* setup signal hanling */
595         gtk_signal_connect( GTK_OBJECT( p_item ),
596                         "toggled",
597                         GTK_SIGNAL_FUNC( pf_toggle ),
598                         (gpointer)(i_angle + 1) );
599
600         gtk_menu_append( GTK_MENU( p_angle_menu ), p_item );
601     }
602
603     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_angle ), p_angle_menu );
604
605     if( p_item_active != NULL )
606     {
607         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_active ),
608                                         TRUE );
609     }
610
611     /* be sure that menu is sensitive if non empty */
612     if( p_intf->p_input->stream.p_selected_area->i_angle_nb > 1 )
613     {
614         gtk_widget_set_sensitive( p_angle, TRUE );
615     }
616
617     return TRUE;
618 }
619
620 /*****************************************************************************
621  * GnomeChapterMenu: generate chapter menu for current title
622  *****************************************************************************/
623 static gint GnomeChapterMenu( gpointer p_data, GtkWidget * p_chapter,
624                         void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
625 {
626     intf_thread_t *     p_intf;
627     char                psz_name[12];
628     GtkWidget *         p_chapter_menu;
629     GtkWidget *         p_chapter_submenu;
630     GtkWidget *         p_menu_item;
631     GtkWidget *         p_item;
632     GtkWidget *         p_item_selected;
633     GSList *            p_chapter_group;
634     gint                i_title;
635     gint                i_chapter;
636     gint                i_nb;
637
638     /* cast */
639     p_intf = (intf_thread_t*)p_data;
640
641     /* removes previous menu */
642     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_chapter ) );
643     gtk_widget_set_sensitive( p_chapter, FALSE );
644
645     p_chapter_submenu = NULL;
646     p_chapter_group = NULL;
647     p_item_selected = NULL;
648     p_menu_item = NULL;
649
650     i_title = p_intf->p_input->stream.p_selected_area->i_id;
651     p_chapter_menu = gtk_menu_new();
652     i_nb = p_intf->p_input->stream.pp_areas[i_title]->i_part_nb;
653
654     for( i_chapter = 0 ; i_chapter < i_nb ; i_chapter++ )
655     {
656         /* we group chapters in packets of ten for small screens */
657         if( ( i_chapter % 10 == 0 ) && ( i_nb > 20 ) )
658         {
659             if( i_chapter != 0 )
660             {
661                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_menu_item ),
662                                            p_chapter_submenu );
663                 gtk_menu_append( GTK_MENU( p_chapter_menu ), p_menu_item );
664             }
665
666             snprintf( psz_name, GNOME_ANGLE_MENU_SIZE,
667                       "%d - %d", i_chapter + 1, i_chapter + 10);
668             psz_name[ GNOME_ANGLE_MENU_SIZE - 1 ] = '\0';
669             p_menu_item = gtk_menu_item_new_with_label( psz_name );
670             gtk_widget_show( p_menu_item );
671             p_chapter_submenu = gtk_menu_new();
672         }
673
674         snprintf( psz_name, GNOME_ANGLE_MENU_SIZE,
675                   "Chapter %d", i_chapter + 1 );
676         psz_name[ GNOME_ANGLE_MENU_SIZE - 1 ] = '\0';
677
678         p_item = gtk_radio_menu_item_new_with_label( p_chapter_group,
679                                                      psz_name );
680         p_chapter_group =
681             gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
682
683         if( p_intf->p_input->stream.pp_areas[i_title]->i_part
684                      == i_chapter + 1 )
685         {
686             p_item_selected = p_item;
687         }
688         
689         gtk_widget_show( p_item );
690
691         /* setup signal hanling */
692         gtk_signal_connect( GTK_OBJECT( p_item ),
693                         "toggled",
694                         GTK_SIGNAL_FUNC( pf_toggle ),
695                         (gpointer)(i_chapter + 1) );
696
697         if( i_nb > 20 )
698         {
699             gtk_menu_append( GTK_MENU( p_chapter_submenu ), p_item );
700         }
701         else
702         {
703             gtk_menu_append( GTK_MENU( p_chapter_menu ), p_item );
704         }
705     }
706
707     if( i_nb > 20 )
708     {
709         gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_menu_item ),
710                                    p_chapter_submenu );
711         gtk_menu_append( GTK_MENU( p_chapter_menu ), p_menu_item );
712     }
713
714     /* link the new menu to the title menu item */
715     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter ),
716                                p_chapter_menu );
717
718     /* toggle currently selected chapter */
719     if( p_item_selected != NULL )
720     {
721         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_selected ),
722                                         TRUE );
723     }
724
725     /* be sure that chapter menu is sensitive, if there are several items */
726     if( p_intf->p_input->stream.pp_areas[i_title]->i_part_nb > 1 )
727     {
728         gtk_widget_set_sensitive( p_chapter, TRUE );
729     }
730
731     return TRUE;
732 }
733
734 /*****************************************************************************
735  * GnomeTitleMenu: sets menus for titles and chapters selection
736  *****************************************************************************
737  * Generates two types of menus:
738  *  -simple list of titles
739  *  -cascaded lists of chapters for each title
740  *****************************************************************************/
741 static gint GnomeTitleMenu( gpointer       p_data,
742                             GtkWidget *    p_navigation, 
743                             void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
744 {
745 #define GNOME_TITLE_MENU_SIZE 64
746     intf_thread_t *     p_intf;
747     char                psz_name[ GNOME_TITLE_MENU_SIZE ];
748     GtkWidget *         p_title_menu;
749     GtkWidget *         p_title_submenu;
750     GtkWidget *         p_title_item;
751     GtkWidget *         p_item_active;
752     GtkWidget *         p_chapter_menu;
753     GtkWidget *         p_chapter_submenu;
754     GtkWidget *         p_title_menu_item;
755     GtkWidget *         p_chapter_menu_item;
756     GtkWidget *         p_item;
757     GSList *            p_title_group;
758     GSList *            p_chapter_group;
759     gint                i_title;
760     gint                i_chapter;
761     gint                i_title_nb;
762     gint                i_chapter_nb;
763
764     /* cast */
765     p_intf = (intf_thread_t*)p_data;
766
767     /* removes previous menu */
768     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_navigation ) );
769     gtk_widget_set_sensitive( p_navigation, FALSE );
770
771     p_title_menu = gtk_menu_new();
772     p_title_group = NULL;
773     p_title_submenu = NULL;
774     p_title_menu_item = NULL;
775     p_chapter_group = NULL;
776     p_chapter_submenu = NULL;
777     p_chapter_menu_item = NULL;
778     p_item_active = NULL;
779     i_title_nb = p_intf->p_input->stream.i_area_nb;
780
781     /* loop on titles */
782     for( i_title = 1 ; i_title < i_title_nb ; i_title++ )
783     {
784         /* we group titles in packets of ten for small screens */
785         if( ( i_title % 10 == 1 ) && ( i_title_nb > 20 ) )
786         {
787             if( i_title != 1 )
788             {
789                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_menu_item ),
790                                            p_title_submenu );
791                 gtk_menu_append( GTK_MENU( p_title_menu ), p_title_menu_item );
792             }
793
794             snprintf( psz_name, GNOME_TITLE_MENU_SIZE,
795                       "%d - %d", i_title, i_title + 9 );
796             psz_name[ GNOME_TITLE_MENU_SIZE - 1 ] = '\0';
797             p_title_menu_item = gtk_menu_item_new_with_label( psz_name );
798             gtk_widget_show( p_title_menu_item );
799             p_title_submenu = gtk_menu_new();
800         }
801
802         snprintf( psz_name, GNOME_TITLE_MENU_SIZE, "Title %d (%d)", i_title,
803                   p_intf->p_input->stream.pp_areas[i_title]->i_part_nb );
804         psz_name[ GNOME_TITLE_MENU_SIZE - 1 ] = '\0';
805
806         if( pf_toggle == on_menubar_title_toggle )
807         {
808             p_title_item = gtk_radio_menu_item_new_with_label( p_title_group,
809                                                            psz_name );
810             p_title_group =
811               gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_title_item ) );
812
813             if( p_intf->p_input->stream.pp_areas[i_title] ==
814                          p_intf->p_input->stream.p_selected_area )
815             {
816                 p_item_active = p_title_item;
817             }
818
819             /* setup signal hanling */
820             gtk_signal_connect( GTK_OBJECT( p_title_item ),
821                      "toggled",
822                      GTK_SIGNAL_FUNC( pf_toggle ),
823                      (gpointer)(p_intf->p_input->stream.pp_areas[i_title]) );
824
825             if( p_intf->p_input->stream.i_area_nb > 1 )
826             {
827                 /* be sure that menu is sensitive */
828                 gtk_widget_set_sensitive( p_navigation, TRUE );
829             }
830         }
831         else
832         {
833     
834             p_title_item = gtk_menu_item_new_with_label( psz_name );
835             p_chapter_menu = gtk_menu_new();
836             i_chapter_nb =
837                     p_intf->p_input->stream.pp_areas[i_title]->i_part_nb;
838     
839             for( i_chapter = 0 ; i_chapter < i_chapter_nb ; i_chapter++ )
840             {
841                 /* we group chapters in packets of ten for small screens */
842                 if( ( i_chapter % 10 == 0 ) && ( i_chapter_nb > 20 ) )
843                 {
844                     if( i_chapter != 0 )
845                     {
846                         gtk_menu_item_set_submenu(
847                                     GTK_MENU_ITEM( p_chapter_menu_item ),
848                                     p_chapter_submenu );
849                         gtk_menu_append( GTK_MENU( p_chapter_menu ),
850                                          p_chapter_menu_item );
851                     }
852
853                     snprintf( psz_name, GNOME_TITLE_MENU_SIZE,
854                               "%d - %d", i_chapter + 1, i_chapter + 10 );
855                     psz_name[ GNOME_TITLE_MENU_SIZE - 1 ] = '\0';
856                     p_chapter_menu_item =
857                             gtk_menu_item_new_with_label( psz_name );
858                     gtk_widget_show( p_chapter_menu_item );
859                     p_chapter_submenu = gtk_menu_new();
860                 }
861
862                 snprintf( psz_name, GNOME_TITLE_MENU_SIZE,
863                           "Chapter %d", i_chapter + 1 );
864                 psz_name[ GNOME_TITLE_MENU_SIZE - 1 ] = '\0';
865     
866                 p_item = gtk_radio_menu_item_new_with_label(
867                                                 p_chapter_group, psz_name );
868                 p_chapter_group = gtk_radio_menu_item_group(
869                                                 GTK_RADIO_MENU_ITEM( p_item ) );
870                 gtk_widget_show( p_item );
871
872 #define p_area p_intf->p_input->stream.pp_areas[i_title]
873                 if( ( p_area == p_intf->p_input->stream.p_selected_area ) &&
874                     ( p_area->i_part == i_chapter + 1 ) )
875                 {
876                     p_item_active = p_item;
877                 }
878 #undef p_area
879
880                 /* setup signal hanling */
881                 gtk_signal_connect( GTK_OBJECT( p_item ),
882                            "toggled",
883                            GTK_SIGNAL_FUNC( pf_toggle ),
884                            (gpointer)( ( i_title * 100 ) + ( i_chapter + 1) ) );
885
886                 if( i_chapter_nb > 20 )
887                 {
888                     gtk_menu_append( GTK_MENU( p_chapter_submenu ), p_item );
889                 }
890                 else
891                 {
892                     gtk_menu_append( GTK_MENU( p_chapter_menu ), p_item );
893                 }
894             }
895
896             if( i_chapter_nb > 20 )
897             {
898                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter_menu_item ),
899                                            p_chapter_submenu );
900                 gtk_menu_append( GTK_MENU( p_chapter_menu ),
901                                  p_chapter_menu_item );
902             }
903
904             /* link the new menu to the title menu item */
905             gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_item ),
906                                        p_chapter_menu );
907
908             if( p_intf->p_input->stream.pp_areas[i_title]->i_part_nb > 1 )
909             {
910                 /* be sure that menu is sensitive */
911                 gtk_widget_set_sensitive( p_navigation, TRUE );
912             }
913         }
914         gtk_widget_show( p_title_item );
915
916         if( i_title_nb > 20 )
917         {
918             gtk_menu_append( GTK_MENU( p_title_submenu ), p_title_item );
919         }
920         else
921         {
922             gtk_menu_append( GTK_MENU( p_title_menu ), p_title_item );
923         }
924     }
925
926     if( i_title_nb > 20 )
927     {
928         gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_menu_item ),
929                                    p_title_submenu );
930         gtk_menu_append( GTK_MENU( p_title_menu ), p_title_menu_item );
931     }
932
933     /* be sure that menu is sensitive */
934     gtk_widget_set_sensitive( p_title_menu, TRUE );
935
936     /* link the new menu to the menubar item */
937     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_navigation ), p_title_menu );
938
939     if( p_item_active != NULL )
940     {
941         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_active ),
942                                         TRUE );
943     }
944
945     return TRUE;
946 }
947
948 /*****************************************************************************
949  * GnomeSetupMenu: function that generates title/chapter/audio/subpic
950  * menus with help from preceding functions
951  *****************************************************************************/
952 static gint GnomeSetupMenu( intf_thread_t * p_intf )
953 {
954     es_descriptor_t *   p_audio_es;
955     es_descriptor_t *   p_spu_es;
956     GtkWidget *         p_menubar_menu;
957     GtkWidget *         p_popup_menu;
958     gint                i;
959
960     p_intf->p_sys->b_chapter_update |= p_intf->p_sys->b_title_update;
961     p_intf->p_sys->b_angle_update |= p_intf->p_sys->b_title_update;
962     p_intf->p_sys->b_audio_update |= p_intf->p_sys->b_title_update;
963     p_intf->p_sys->b_spu_update |= p_intf->p_sys->b_title_update;
964
965     if( p_intf->p_sys->b_title_update )
966     { 
967         char            psz_title[5];
968
969         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
970                             p_intf->p_sys->p_window ), "menubar_title" ) );
971         GnomeTitleMenu( p_intf, p_menubar_menu, on_menubar_title_toggle );
972
973         snprintf( psz_title, 5, "%d",
974                   p_intf->p_input->stream.p_selected_area->i_id );
975         psz_title[ 4 ] = '\0';
976         gtk_label_set_text( p_intf->p_sys->p_label_title, psz_title );
977
978         p_intf->p_sys->b_title_update = 0;
979     }
980
981     if( p_intf->p_sys->b_chapter_update )
982     {
983         char            psz_chapter[5];
984
985         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
986                              p_intf->p_sys->p_popup ), "popup_navigation" ) );
987         GnomeTitleMenu( p_intf, p_popup_menu, on_popup_navigation_toggle );
988      
989         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
990                              p_intf->p_sys->p_window ), "menubar_chapter" ) );
991         GnomeChapterMenu( p_intf, p_menubar_menu, on_menubar_chapter_toggle );
992
993         snprintf( psz_chapter, 5, "%d", 
994                   p_intf->p_input->stream.p_selected_area->i_part );
995         psz_chapter[ 4 ] = '\0';
996         gtk_label_set_text( p_intf->p_sys->p_label_chapter, psz_chapter );
997
998         p_intf->p_sys->i_part =
999                 p_intf->p_input->stream.p_selected_area->i_part;
1000
1001         p_intf->p_sys->b_chapter_update = 0;
1002     }
1003
1004     if( p_intf->p_sys->b_angle_update )
1005     {
1006         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
1007                              p_intf->p_sys->p_window ), "menubar_angle" ) );
1008         GnomeAngleMenu( p_intf, p_menubar_menu, on_menubar_angle_toggle );
1009
1010         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
1011                              p_intf->p_sys->p_popup ), "popup_angle" ) );
1012         GnomeAngleMenu( p_intf, p_popup_menu, on_popup_angle_toggle );
1013
1014         p_intf->p_sys->b_angle_update = 0;
1015     }
1016     
1017     /* look for selected ES */
1018     p_audio_es = NULL;
1019     p_spu_es = NULL;
1020
1021     for( i = 0 ; i < p_intf->p_input->stream.i_selected_es_number ; i++ )
1022     {
1023         if( p_intf->p_input->stream.pp_selected_es[i]->i_cat == AUDIO_ES )
1024         {
1025             p_audio_es = p_intf->p_input->stream.pp_selected_es[i];
1026         }
1027
1028         if( p_intf->p_input->stream.pp_selected_es[i]->i_cat == SPU_ES )
1029         {
1030             p_spu_es = p_intf->p_input->stream.pp_selected_es[i];
1031         }
1032     }
1033
1034     /* audio menus */
1035     if( p_intf->p_sys->b_audio_update )
1036     {
1037         /* find audio root menu */
1038         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1039                              p_intf->p_sys->p_window ), "menubar_audio" ) );
1040     
1041         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
1042                      p_intf->p_sys->p_popup ), "popup_audio" ) );
1043     
1044         GnomeLanguageMenus( p_intf, p_menubar_menu, p_audio_es, AUDIO_ES,
1045                             on_menubar_audio_toggle );
1046         GnomeLanguageMenus( p_intf, p_popup_menu, p_audio_es, AUDIO_ES,
1047                             on_popup_audio_toggle );
1048
1049         p_intf->p_sys->b_audio_update = 0;
1050     }
1051     
1052     /* sub picture menus */
1053     if( p_intf->p_sys->b_spu_update )
1054     {
1055         /* find spu root menu */
1056         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1057                           p_intf->p_sys->p_window ), "menubar_subtitle" ) );
1058     
1059         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
1060                      p_intf->p_sys->p_popup ), "popup_subtitle" ) );
1061     
1062         GnomeLanguageMenus( p_intf, p_menubar_menu, p_spu_es, SPU_ES,
1063                             on_menubar_subtitle_toggle  );
1064         GnomeLanguageMenus( p_intf, p_popup_menu, p_spu_es, SPU_ES,
1065                             on_popup_subtitle_toggle );
1066
1067         p_intf->p_sys->b_spu_update = 0;
1068     }
1069
1070     return TRUE;
1071 }
1072
1073 /*****************************************************************************
1074  * GnomeDisplayDate: display stream date
1075  *****************************************************************************
1076  * This function displays the current date related to the position in
1077  * the stream. It is called whenever the slider changes its value.
1078  *****************************************************************************/
1079 void GnomeDisplayDate( GtkAdjustment *p_adj )
1080 {
1081     intf_thread_t *p_intf;
1082    
1083     p_intf = gtk_object_get_data( GTK_OBJECT( p_adj ), "p_intf" );
1084
1085     if( p_intf->p_input != NULL )
1086     {
1087 #define p_area p_intf->p_input->stream.p_selected_area
1088         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
1089
1090         vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
1091
1092         gtk_label_set_text( p_intf->p_sys->p_label_date,
1093                             input_OffsetToTime( p_intf->p_input, psz_time,
1094                                    ( p_area->i_size * p_adj->value ) / 100 ) );
1095
1096         vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
1097 #undef p_area
1098      }
1099 }
1100
1101
1102 /*****************************************************************************
1103  * GnomeDiscModeManage
1104  *****************************************************************************/
1105 static gint GnomeDiscModeManage( intf_thread_t * p_intf )
1106 {
1107     GtkWidget *     p_dvd_box;
1108     GtkWidget *     p_file_box;
1109     GtkWidget *     p_network_box;
1110
1111     p_file_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1112                  p_intf->p_sys->p_window ), "file_box" ) );
1113     gtk_widget_hide( GTK_WIDGET( p_file_box ) );
1114
1115     p_network_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1116                  p_intf->p_sys->p_window ), "network_box" ) );
1117     gtk_widget_hide( GTK_WIDGET( p_network_box ) );
1118
1119     p_dvd_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1120                  p_intf->p_sys->p_window ), "dvd_box" ) );
1121     gtk_widget_show( GTK_WIDGET( p_dvd_box ) );
1122
1123     gtk_label_set_text( p_intf->p_sys->p_label_status,
1124                         "Status: playing DVD" );
1125
1126     return TRUE;
1127 }
1128
1129 /*****************************************************************************
1130  * GnomeFileModeManage
1131  *****************************************************************************/
1132 static gint GnomeFileModeManage( intf_thread_t * p_intf )
1133 {
1134     GtkWidget *     p_dvd_box;
1135     GtkWidget *     p_file_box;
1136     GtkWidget *     p_network_box;
1137     char *          psz_name;
1138
1139     p_network_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1140                  p_intf->p_sys->p_window ), "network_box" ) );
1141     gtk_widget_hide( GTK_WIDGET( p_network_box ) );
1142
1143     p_dvd_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1144                  p_intf->p_sys->p_window ), "dvd_box" ) );
1145     gtk_widget_hide( GTK_WIDGET( p_dvd_box ) );
1146
1147     p_file_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1148                  p_intf->p_sys->p_window ), "file_box" ) );
1149     gtk_widget_show( GTK_WIDGET( p_file_box ) );
1150
1151 #if 1
1152 //    psz_name = malloc( 16 + strlen( p_intf->p_input->p_source ) );
1153 //    sprintf( psz_name, "Status: playing %s", p_intf->p_input->p_source );
1154
1155     psz_name = strdup( p_intf->p_input->p_source );
1156
1157     gtk_label_set_text( p_intf->p_sys->p_label_status, psz_name );
1158
1159     free( psz_name );
1160 #else
1161     gtk_label_set_text( p_intf->p_sys->p_label_status,
1162                         "Status: foo" );
1163 #endif
1164
1165     return TRUE;
1166 }
1167
1168 /*****************************************************************************
1169  * GnomeNetworkModeManage
1170  *****************************************************************************/
1171 static gint GnomeNetworkModeManage( intf_thread_t * p_intf )
1172 {
1173     GtkWidget *     p_dvd_box;
1174     GtkWidget *     p_file_box;
1175     GtkWidget *     p_network_box;
1176
1177     p_dvd_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1178                  p_intf->p_sys->p_window ), "dvd_box" ) );
1179     gtk_widget_hide( GTK_WIDGET( p_dvd_box ) );
1180
1181     p_file_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1182                  p_intf->p_sys->p_window ), "file_box" ) );
1183     gtk_widget_hide( GTK_WIDGET( p_file_box ) );
1184
1185     p_network_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1186                  p_intf->p_sys->p_window ), "network_box" ) );
1187     gtk_widget_show( GTK_WIDGET( p_network_box ) );
1188
1189     gtk_label_set_text( p_intf->p_sys->p_label_status,
1190                         "Status: waiting for stream" );
1191
1192     return TRUE;
1193 }