]> git.sesse.net Git - vlc/blob - plugins/gnome/intf_gnome.c
TS input : only one audio & spu ES selected at a time
[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.29 2001/04/13 01:49:22 henri 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 GnomeDVDModeManage( 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_sys->b_mode_changed )
315     {
316         /* Sets the interface mode according to playlist item */
317         if( p_main->p_playlist->p_item != NULL )
318         {
319             if( !strncmp( p_main->p_playlist->p_item->psz_name, "dvd:", 4 ) )
320             {
321                 p_intf->p_sys->i_intf_mode = DVD_MODE;
322             }
323             else if( !strncmp(
324                         p_main->p_playlist->p_item->psz_name, "ts:", 4 ) )
325             {
326                 p_intf->p_sys->i_intf_mode = NET_MODE;
327             }
328         }
329
330         switch( p_intf->p_sys->i_intf_mode )
331         {
332             case DVD_MODE:
333                 GnomeDVDModeManage( p_intf );
334                 break;
335             case NET_MODE:
336                 GnomeNetworkModeManage( p_intf );
337                 break;
338             case FILE_MODE:
339             default:
340                 GnomeFileModeManage( p_intf );
341                 break;
342         }
343
344         p_intf->p_sys->b_mode_changed = 0;
345     }
346
347
348     if( p_intf->p_input != NULL )
349     {
350         float           newvalue;
351         char            psz_title[3];
352         char            psz_chapter[3];
353
354         /* Used by TS input when PMT changes */
355         if( p_intf->p_input->stream.b_changed )
356         {
357             p_intf->p_sys->b_menus_update = 1;
358             p_intf->p_input->stream.b_changed = 0;
359             intf_WarnMsg( 2, 
360                           "Interface menus refreshed as stream has changed" );
361         }
362
363 #define p_area p_intf->p_input->stream.p_selected_area
364         /* Update language/chapter menus after user request */
365         if( ( p_intf->p_sys->b_menus_update ) ||
366             ( p_intf->p_sys->i_part != p_area->i_part ) )
367         {
368             p_intf->p_sys->b_menus_update = 1;
369             GnomeSetupMenu( p_intf );
370             p_intf->p_sys->b_menus_update = 0;
371
372             snprintf( psz_title, 3, "%02d", p_area->i_id );
373             gtk_label_set_text( p_intf->p_sys->p_label_title, psz_title );
374
375             p_intf->p_sys->i_part = p_area->i_part;
376             snprintf( psz_chapter, 3, "%02d", p_area->i_part );
377             gtk_label_set_text( p_intf->p_sys->p_label_chapter, psz_chapter );
378         }
379
380         /* Manage the slider */
381         newvalue = p_intf->p_sys->p_adj->value;
382
383         /* If the user hasn't touched the slider since the last time,
384          * then the input can safely change it */
385         if( newvalue == p_intf->p_sys->f_adj_oldvalue )
386         {
387             /* Update the value */
388             p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
389                 ( 100. * p_area->i_tell ) / p_area->i_size;
390
391             gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
392                                      "value_changed" );
393         }
394         /* Otherwise, send message to the input if the user has
395          * finished dragging the slider */
396         else if( p_intf->p_sys->b_slider_free )
397         {
398             off_t i_seek = ( newvalue * p_area->i_size ) / 100;
399
400             input_Seek( p_intf->p_input, i_seek );
401
402             /* Update the old value */
403             p_intf->p_sys->f_adj_oldvalue = newvalue;
404         }
405 #undef p_area
406     }
407
408     /* Manage core vlc functions through the callback */
409     p_intf->pf_manage( p_intf );
410
411     if( p_intf->b_die )
412     {
413         vlc_mutex_unlock( &p_intf->change_lock );
414
415         /* Prepare to die, young Skywalker */
416         gtk_main_quit();
417
418         /* Just in case */
419         return( FALSE );
420     }
421
422     vlc_mutex_unlock( &p_intf->change_lock );
423
424     return( TRUE );
425
426 #undef p_intf
427 }
428
429 /*****************************************************************************
430  * GnomeLanguageMenus: update interactive menus of the interface
431  *****************************************************************************
432  * Sets up menus with information from input:
433  *  -languages
434  *  -sub-pictures
435  * Warning: since this function is designed to be called by management
436  * function, the interface lock has to be taken
437  *****************************************************************************/
438 static gint GnomeLanguageMenus( gpointer          p_data,
439                                 GtkWidget *       p_root,
440                                 es_descriptor_t * p_es,
441                                 gint              i_cat,
442                           void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
443 {
444     intf_thread_t *     p_intf;
445     GtkWidget *         p_menu;
446     GtkWidget *         p_separator;
447     GtkWidget *         p_item;
448     GtkWidget *         p_item_active;
449     GSList *            p_group;
450     char *              psz_name;
451     gint                i_item;
452     gint                i;
453
454     
455
456     /* cast */
457     p_intf = (intf_thread_t *)p_data;
458
459     /* removes previous menu */
460     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_root ) );
461     gtk_widget_set_sensitive( p_root, FALSE );
462
463     p_group = NULL;
464
465     /* menu container */
466     p_menu = gtk_menu_new();
467
468     /* special case for "off" item */
469     psz_name = "Off";
470
471     p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
472     p_group = gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
473
474     gtk_widget_show( p_item );
475
476     /* signal hanling for off */
477     gtk_signal_connect( GTK_OBJECT( p_item ), "toggled",
478                         GTK_SIGNAL_FUNC ( pf_toggle ), NULL );
479
480     gtk_menu_append( GTK_MENU( p_menu ), p_item );
481
482     p_separator = gtk_menu_item_new();
483     gtk_widget_set_sensitive( p_separator, FALSE );
484     gtk_widget_show( p_separator );
485     gtk_menu_append( GTK_MENU( p_menu ), p_separator );
486
487     vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
488     p_item_active = NULL;
489     i_item = 0;
490
491     /* create a set of language buttons and append them to the container */
492     for( i = 0 ; i < p_intf->p_input->stream.i_es_number ; i++ )
493     {
494         if( p_intf->p_input->stream.pp_es[i]->i_cat == i_cat )
495         {
496             i_item++;
497             psz_name = p_intf->p_input->stream.pp_es[i]->psz_desc;
498             if( psz_name[0] == '\0' )
499             {
500                 sprintf( psz_name, "Language %d", i_item );
501             }
502
503             p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
504             p_group =
505                 gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
506
507             if( p_es == p_intf->p_input->stream.pp_es[i] )
508             {
509                 /* don't lose p_item when we append into menu */
510                 p_item_active = p_item;
511             }
512
513             gtk_widget_show( p_item );
514
515             /* setup signal hanling */
516             gtk_signal_connect( GTK_OBJECT( p_item ), "toggled",
517                             GTK_SIGNAL_FUNC( pf_toggle ),
518                             (gpointer)( p_intf->p_input->stream.pp_es[i] ) );
519
520             gtk_menu_append( GTK_MENU( p_menu ), p_item );
521         }
522     }
523
524     vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
525
526     /* link the new menu to the menubar item */
527     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
528
529     /* acitvation will call signals so we can only do it
530      * when submenu is attached to menu - to get intf_window */
531     if( p_item_active != NULL )
532     {
533         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_active ),
534                                         TRUE );
535     }
536
537     /* be sure that menu is sensitive if non empty */
538     if( i_item > 0 )
539     {
540         gtk_widget_set_sensitive( p_root, TRUE );
541     }
542
543     return TRUE;
544 }
545
546 /*****************************************************************************
547  * GnomeChapterMenu: generate chapter menu for current title
548  *****************************************************************************/
549 static gint GnomeChapterMenu( gpointer p_data, GtkWidget * p_chapter,
550                         void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
551 {
552     intf_thread_t *     p_intf;
553     char                psz_name[10];
554     GtkWidget *         p_chapter_menu;
555     GtkWidget *         p_chapter_submenu;
556     GtkWidget *         p_menu_item;
557     GtkWidget *         p_item;
558     GtkWidget *         p_item_selected;
559     GSList *            p_chapter_group;
560     gint                i_title;
561     gint                i_chapter;
562     gint                i_nb;
563
564     /* cast */
565     p_intf = (intf_thread_t*)p_data;
566
567     /* removes previous menu */
568     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_chapter ) );
569     gtk_widget_set_sensitive( p_chapter, FALSE );
570
571     p_chapter_submenu = NULL;
572     p_chapter_group = NULL;
573     p_item_selected = NULL;
574     p_menu_item = NULL;
575
576     i_title = p_intf->p_input->stream.p_selected_area->i_id;
577     p_chapter_menu = gtk_menu_new();
578     i_nb = p_intf->p_input->stream.pp_areas[i_title]->i_part_nb;
579
580     for( i_chapter = 0 ; i_chapter < i_nb ; i_chapter++ )
581     {
582         /* we group chapters in packets of ten for small screens */
583         if( ( i_chapter % 10 == 0 ) && ( i_nb > 20 ) )
584         {
585             if( i_chapter != 0 )
586             {
587                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_menu_item ),
588                                            p_chapter_submenu );
589                 gtk_menu_append( GTK_MENU( p_chapter_menu ), p_menu_item );
590             }
591
592             sprintf( psz_name, "%d - %d", i_chapter + 1, i_chapter + 10);
593             p_menu_item = gtk_menu_item_new_with_label( psz_name );
594             gtk_widget_show( p_menu_item );
595             p_chapter_submenu = gtk_menu_new();
596         }
597
598         sprintf( psz_name, "Chapter %d", i_chapter + 1 );
599
600         p_item = gtk_radio_menu_item_new_with_label( p_chapter_group,
601                                                      psz_name );
602         p_chapter_group =
603             gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
604
605         if( p_intf->p_input->stream.pp_areas[i_title]->i_part
606                      == i_chapter + 1 )
607         {
608             p_item_selected = p_item;
609         }
610         
611         gtk_widget_show( p_item );
612
613         /* setup signal hanling */
614         gtk_signal_connect( GTK_OBJECT( p_item ),
615                         "toggled",
616                         GTK_SIGNAL_FUNC( pf_toggle ),
617                         (gpointer)(i_chapter + 1) );
618
619         if( i_nb > 20 )
620         {
621             gtk_menu_append( GTK_MENU( p_chapter_submenu ), p_item );
622         }
623         else
624         {
625             gtk_menu_append( GTK_MENU( p_chapter_menu ), p_item );
626         }
627     }
628
629     if( i_nb > 20 )
630     {
631         gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_menu_item ),
632                                    p_chapter_submenu );
633         gtk_menu_append( GTK_MENU( p_chapter_menu ), p_menu_item );
634     }
635
636     /* link the new menu to the title menu item */
637     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter ),
638                                p_chapter_menu );
639
640     /* toggle currently selected chapter */
641     if( p_item_selected != NULL )
642     {
643         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_selected ),
644                                         TRUE );
645     }
646
647     /* be sure that chapter menu is sensitive, if there are several items */
648     if( p_intf->p_input->stream.pp_areas[i_title]->i_part_nb > 1 )
649     {
650         gtk_widget_set_sensitive( p_chapter, TRUE );
651     }
652
653     return TRUE;
654 }
655
656 /*****************************************************************************
657  * GnomeTitleMenu: sets menus for titles and chapters selection
658  *****************************************************************************
659  * Generates two types of menus:
660  *  -simple list of titles
661  *  -cascaded lists of chapters for each title
662  *****************************************************************************/
663 static gint GnomeTitleMenu( gpointer       p_data,
664                             GtkWidget *    p_navigation, 
665                             void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
666 {
667     intf_thread_t *     p_intf;
668     char                psz_name[10];
669     GtkWidget *         p_title_menu;
670     GtkWidget *         p_title_submenu;
671     GtkWidget *         p_title_item;
672     GtkWidget *         p_item_active;
673     GtkWidget *         p_chapter_menu;
674     GtkWidget *         p_chapter_submenu;
675     GtkWidget *         p_title_menu_item;
676     GtkWidget *         p_chapter_menu_item;
677     GtkWidget *         p_item;
678     GSList *            p_title_group;
679     GSList *            p_chapter_group;
680     gint                i_title;
681     gint                i_chapter;
682     gint                i_title_nb;
683     gint                i_chapter_nb;
684
685     /* cast */
686     p_intf = (intf_thread_t*)p_data;
687
688     p_title_menu = gtk_menu_new();
689     p_title_group = NULL;
690     p_title_submenu = NULL;
691     p_title_menu_item = NULL;
692     p_chapter_group = NULL;
693     p_chapter_submenu = NULL;
694     p_chapter_menu_item = NULL;
695     p_item_active = NULL;
696     i_title_nb = p_intf->p_input->stream.i_area_nb;
697
698     /* loop on titles */
699     for( i_title = 1 ; i_title < i_title_nb ; i_title++ )
700     {
701         /* we group titles in packets of ten for small screens */
702         if( ( i_title % 10 == 1 ) && ( i_title_nb > 20 ) )
703         {
704             if( i_title != 1 )
705             {
706                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_menu_item ),
707                                            p_title_submenu );
708                 gtk_menu_append( GTK_MENU( p_title_menu ), p_title_menu_item );
709             }
710
711             sprintf( psz_name, "%d - %d", i_title, i_title + 9 );
712             p_title_menu_item = gtk_menu_item_new_with_label( psz_name );
713             gtk_widget_show( p_title_menu_item );
714             p_title_submenu = gtk_menu_new();
715         }
716
717         sprintf( psz_name, "Title %d", i_title );
718
719         if( pf_toggle == on_menubar_title_toggle )
720         {
721             p_title_item = gtk_radio_menu_item_new_with_label( p_title_group,
722                                                            psz_name );
723             p_title_group =
724               gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_title_item ) );
725
726             if( p_intf->p_input->stream.pp_areas[i_title] ==
727                          p_intf->p_input->stream.p_selected_area )
728             {
729                 p_item_active = p_title_item;
730             }
731
732             /* setup signal hanling */
733             gtk_signal_connect( GTK_OBJECT( p_title_item ),
734                      "toggled",
735                      GTK_SIGNAL_FUNC( pf_toggle ),
736                      (gpointer)(p_intf->p_input->stream.pp_areas[i_title]) );
737
738             if( p_intf->p_input->stream.i_area_nb > 1 )
739             {
740                 /* be sure that menu is sensitive */
741                 gtk_widget_set_sensitive( p_navigation, TRUE );
742             }
743         }
744         else
745         {
746     
747             p_title_item = gtk_menu_item_new_with_label( psz_name );
748             p_chapter_menu = gtk_menu_new();
749             i_chapter_nb =
750                     p_intf->p_input->stream.pp_areas[i_title]->i_part_nb;
751     
752             for( i_chapter = 0 ; i_chapter < i_chapter_nb ; i_chapter++ )
753             {
754                 /* we group chapters in packets of ten for small screens */
755                 if( ( i_chapter % 10 == 0 ) && ( i_chapter_nb > 20 ) )
756                 {
757                     if( i_chapter != 0 )
758                     {
759                         gtk_menu_item_set_submenu(
760                                     GTK_MENU_ITEM( p_chapter_menu_item ),
761                                     p_chapter_submenu );
762                         gtk_menu_append( GTK_MENU( p_chapter_menu ),
763                                          p_chapter_menu_item );
764                     }
765
766                     sprintf( psz_name, "%d - %d", i_chapter + 1,
767                                                   i_chapter + 10);
768                     p_chapter_menu_item =
769                             gtk_menu_item_new_with_label( psz_name );
770                     gtk_widget_show( p_chapter_menu_item );
771                     p_chapter_submenu = gtk_menu_new();
772                 }
773
774                 sprintf( psz_name, "Chapter %d", i_chapter + 1 );
775     
776                 p_item = gtk_radio_menu_item_new_with_label(
777                                                 p_chapter_group, psz_name );
778                 p_chapter_group = gtk_radio_menu_item_group(
779                                                 GTK_RADIO_MENU_ITEM( p_item ) );
780                 gtk_widget_show( p_item );
781
782 #define p_area p_intf->p_input->stream.pp_areas[i_title]
783                 if( ( p_area == p_intf->p_input->stream.p_selected_area ) &&
784                     ( p_area->i_part == i_chapter + 1 ) )
785                 {
786                     p_item_active = p_item;
787                 }
788 #undef p_area
789
790                 /* setup signal hanling */
791                 gtk_signal_connect( GTK_OBJECT( p_item ),
792                            "toggled",
793                            GTK_SIGNAL_FUNC( pf_toggle ),
794                            (gpointer)( ( i_title * 100 ) + ( i_chapter + 1) ) );
795
796                 if( i_chapter_nb > 20 )
797                 {
798                     gtk_menu_append( GTK_MENU( p_chapter_submenu ), p_item );
799                 }
800                 else
801                 {
802                     gtk_menu_append( GTK_MENU( p_chapter_menu ), p_item );
803                 }
804             }
805
806             if( i_chapter_nb > 20 )
807             {
808                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter_menu_item ),
809                                            p_chapter_submenu );
810                 gtk_menu_append( GTK_MENU( p_chapter_menu ),
811                                  p_chapter_menu_item );
812             }
813
814             /* link the new menu to the title menu item */
815             gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_item ),
816                                        p_chapter_menu );
817
818             if( p_intf->p_input->stream.pp_areas[i_title]->i_part_nb > 1 )
819             {
820                 /* be sure that menu is sensitive */
821                 gtk_widget_set_sensitive( p_navigation, TRUE );
822             }
823         }
824         gtk_widget_show( p_title_item );
825
826         if( i_title_nb > 20 )
827         {
828             gtk_menu_append( GTK_MENU( p_title_submenu ), p_title_item );
829         }
830         else
831         {
832             gtk_menu_append( GTK_MENU( p_title_menu ), p_title_item );
833         }
834     }
835
836     if( i_title_nb > 20 )
837     {
838         gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_menu_item ),
839                                    p_title_submenu );
840         gtk_menu_append( GTK_MENU( p_title_menu ), p_title_menu_item );
841     }
842
843     /* be sure that menu is sensitive */
844     gtk_widget_set_sensitive( p_title_menu, TRUE );
845
846     /* link the new menu to the menubar item */
847     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_navigation ), p_title_menu );
848
849     if( p_item_active != NULL )
850     {
851         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_active ),
852                                         TRUE );
853     }
854
855
856     return TRUE;
857 }
858
859 /*****************************************************************************
860  * GnomeSetupMenu: function that generates title/chapter/audio/subpic
861  * menus with help from preceding functions
862  *****************************************************************************/
863 static gint GnomeSetupMenu( intf_thread_t * p_intf )
864 {
865     es_descriptor_t *   p_audio_es;
866     es_descriptor_t *   p_spu_es;
867     GtkWidget *         p_menubar_menu;
868     GtkWidget *         p_popup_menu;
869     gint                i;
870
871     if( p_intf->p_input->stream.i_area_nb > 1 )
872     {
873         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
874                             p_intf->p_sys->p_window ), "menubar_title" ) );
875         GnomeTitleMenu( p_intf, p_menubar_menu, on_menubar_title_toggle );
876
877         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
878                              p_intf->p_sys->p_popup ), "popup_navigation" ) );
879         GnomeTitleMenu( p_intf, p_popup_menu, on_popup_navigation_toggle );
880     
881         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
882                              p_intf->p_sys->p_window ), "menubar_chapter" ) );
883         GnomeChapterMenu( p_intf, p_menubar_menu, on_menubar_chapter_toggle );
884     }
885
886     /* look for selected ES */
887     p_audio_es = NULL;
888     p_spu_es = NULL;
889
890     for( i = 0 ; i < p_intf->p_input->stream.i_selected_es_number ; i++ )
891     {
892         if( p_intf->p_input->stream.pp_selected_es[i]->i_cat == AUDIO_ES )
893         {
894             p_audio_es = p_intf->p_input->stream.pp_selected_es[i];
895         }
896
897         if( p_intf->p_input->stream.pp_selected_es[i]->i_cat == SPU_ES )
898         {
899             p_spu_es = p_intf->p_input->stream.pp_selected_es[i];
900         }
901     }
902
903     /* audio menus */
904
905     /* find audio root menu */
906     p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
907                          p_intf->p_sys->p_window ), "menubar_audio" ) );
908
909     p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
910                  p_intf->p_sys->p_popup ), "popup_audio" ) );
911
912     GnomeLanguageMenus( p_intf, p_menubar_menu, p_audio_es, AUDIO_ES,
913                         on_menubar_audio_toggle );
914     GnomeLanguageMenus( p_intf, p_popup_menu, p_audio_es, AUDIO_ES,
915                         on_popup_audio_toggle );
916
917     /* sub picture menus */
918
919     /* find spu root menu */
920     p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
921                       p_intf->p_sys->p_window ), "menubar_subtitle" ) );
922
923     p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
924                  p_intf->p_sys->p_popup ), "popup_subtitle" ) );
925
926     GnomeLanguageMenus( p_intf, p_menubar_menu, p_spu_es, SPU_ES,
927                         on_menubar_subtitle_toggle  );
928     GnomeLanguageMenus( p_intf, p_popup_menu, p_spu_es, SPU_ES,
929                         on_popup_subtitle_toggle );
930
931     return TRUE;
932 }
933
934 /*****************************************************************************
935  * GnomeDisplayDate: display stream date
936  *****************************************************************************
937  * This function displays the current date related to the position in
938  * the stream. It is called whenever the slider changes its value.
939  *****************************************************************************/
940 void GnomeDisplayDate( GtkAdjustment *p_adj )
941 {
942     intf_thread_t *p_intf;
943    
944     p_intf = gtk_object_get_data( GTK_OBJECT( p_adj ), "p_intf" );
945
946     if( p_intf->p_input != NULL )
947     {
948 #define p_area p_intf->p_input->stream.p_selected_area
949         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
950
951         vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
952
953         gtk_label_set_text( p_intf->p_sys->p_label_date,
954                             input_OffsetToTime( p_intf->p_input, psz_time,
955                                    ( p_area->i_size * p_adj->value ) / 100 ) );
956
957         vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
958 #undef p_area
959      }
960 }
961
962
963 /*****************************************************************************
964  * GnomeDVDModeManage
965  *****************************************************************************/
966 static gint GnomeDVDModeManage( intf_thread_t * p_intf )
967 {
968     GtkWidget *     p_dvd_box;
969     GtkWidget *     p_file_box;
970     GtkWidget *     p_network_box;
971
972     p_file_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
973                  p_intf->p_sys->p_window ), "file_box" ) );
974     gtk_widget_hide( GTK_WIDGET( p_file_box ) );
975
976     p_network_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
977                  p_intf->p_sys->p_window ), "network_box" ) );
978     gtk_widget_hide( GTK_WIDGET( p_network_box ) );
979
980     p_dvd_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
981                  p_intf->p_sys->p_window ), "dvd_box" ) );
982     gtk_widget_show( GTK_WIDGET( p_dvd_box ) );
983
984     gtk_label_set_text( p_intf->p_sys->p_label_status,
985                         "Status: playing DVD" );
986
987     return TRUE;
988 }
989
990 /*****************************************************************************
991  * GnomeFileModeManage
992  *****************************************************************************/
993 static gint GnomeFileModeManage( intf_thread_t * p_intf )
994 {
995     GtkWidget *     p_dvd_box;
996     GtkWidget *     p_file_box;
997     GtkWidget *     p_network_box;
998
999     p_network_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1000                  p_intf->p_sys->p_window ), "network_box" ) );
1001     gtk_widget_hide( GTK_WIDGET( p_network_box ) );
1002
1003     p_dvd_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1004                  p_intf->p_sys->p_window ), "dvd_box" ) );
1005     gtk_widget_hide( GTK_WIDGET( p_dvd_box ) );
1006
1007     p_file_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1008                  p_intf->p_sys->p_window ), "file_box" ) );
1009     gtk_widget_show( GTK_WIDGET( p_file_box ) );
1010
1011     gtk_label_set_text( p_intf->p_sys->p_label_status,
1012                         "Status: foo" );
1013
1014     return TRUE;
1015 }
1016
1017 /*****************************************************************************
1018  * GnomeNetworkModeManage
1019  *****************************************************************************/
1020 static gint GnomeNetworkModeManage( intf_thread_t * p_intf )
1021 {
1022     GtkWidget *     p_dvd_box;
1023     GtkWidget *     p_file_box;
1024     GtkWidget *     p_network_box;
1025
1026     p_dvd_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1027                  p_intf->p_sys->p_window ), "dvd_box" ) );
1028     gtk_widget_hide( GTK_WIDGET( p_dvd_box ) );
1029
1030     p_file_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1031                  p_intf->p_sys->p_window ), "file_box" ) );
1032     gtk_widget_hide( GTK_WIDGET( p_file_box ) );
1033
1034     p_network_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1035                  p_intf->p_sys->p_window ), "network_box" ) );
1036     gtk_widget_show( GTK_WIDGET( p_network_box ) );
1037
1038     gtk_label_set_text( p_intf->p_sys->p_label_status,
1039                         "Status: waiting for stream" );
1040
1041     return TRUE;
1042 }