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