1 /*****************************************************************************
2 * intf_gnome.c: Gnome interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: intf_gnome.c,v 1.20 2001/03/07 11:56:33 stef Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 #define MODULE_NAME gnome
25 #include "modules_inner.h"
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <errno.h> /* ENOMEM */
33 #include <stdlib.h> /* free() */
34 #include <string.h> /* strerror() */
46 #include "stream_control.h"
47 #include "input_ext-intf.h"
50 #include "interface.h"
52 #include "gnome_sys.h"
53 #include "gnome_callbacks.h"
54 #include "gnome_interface.h"
55 #include "gnome_support.h"
59 /*****************************************************************************
61 *****************************************************************************/
62 static int intf_Probe ( probedata_t *p_data );
63 static int intf_Open ( intf_thread_t *p_intf );
64 static void intf_Close ( intf_thread_t *p_intf );
65 static void intf_Run ( intf_thread_t *p_intf );
67 static gint GnomeManage ( gpointer p_data );
68 static gint GnomeLanguageMenus( gpointer, GtkWidget *, es_descriptor_t *, gint,
69 void (*pf_activate)(GtkMenuItem *, gpointer) );
70 static gint GnomeChapterMenu ( gpointer, GtkWidget *,
71 void (*pf_activate)(GtkMenuItem *, gpointer) );
72 static gint GnomeTitleMenu ( gpointer, GtkWidget *,
73 void (*pf_activate)(GtkMenuItem *, gpointer) );
75 /*****************************************************************************
76 * g_atexit: kludge to avoid the Gnome thread to segfault at exit
77 *****************************************************************************
78 * gtk_init() makes several calls to g_atexit() which calls atexit() to
79 * register tidying callbacks to be called at program exit. Since the Gnome
80 * plugin is likely to be unloaded at program exit, we have to export this
81 * symbol to intercept the g_atexit() calls. Talk about crude hack.
82 *****************************************************************************/
83 void g_atexit( GVoidFunc func )
85 intf_thread_t *p_intf = p_main->p_intf;
87 if( p_intf->p_sys->pf_gdk_callback == NULL )
89 p_intf->p_sys->pf_gdk_callback = func;
91 else if( p_intf->p_sys->pf_gtk_callback == NULL )
93 p_intf->p_sys->pf_gtk_callback = func;
95 /* else nothing, but we could do something here */
99 /*****************************************************************************
100 * Functions exported as capabilities. They are declared as static so that
101 * we don't pollute the namespace too much.
102 *****************************************************************************/
103 void _M( intf_getfunctions )( function_list_t * p_function_list )
105 p_function_list->pf_probe = intf_Probe;
106 p_function_list->functions.intf.pf_open = intf_Open;
107 p_function_list->functions.intf.pf_close = intf_Close;
108 p_function_list->functions.intf.pf_run = intf_Run;
111 /*****************************************************************************
112 * intf_Probe: probe the interface and return a score
113 *****************************************************************************
114 * This function tries to initialize Gnome and returns a score to the
115 * plugin manager so that it can select the best plugin.
116 *****************************************************************************/
117 static int intf_Probe( probedata_t *p_data )
119 if( TestMethod( INTF_METHOD_VAR, "gnome" ) )
127 /*****************************************************************************
128 * intf_Open: initialize and create window
129 *****************************************************************************/
130 static int intf_Open( intf_thread_t *p_intf )
132 /* Allocate instance and initialize some members */
133 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
134 if( p_intf->p_sys == NULL )
136 intf_ErrMsg("error: %s", strerror(ENOMEM));
140 /* Initialize Gnome thread */
141 p_intf->p_sys->b_popup_changed = 0;
142 p_intf->p_sys->b_window_changed = 0;
143 p_intf->p_sys->b_playlist_changed = 0;
145 p_intf->p_sys->b_scale_isfree = 1;
147 p_intf->p_sys->pf_gtk_callback = NULL;
148 p_intf->p_sys->pf_gdk_callback = NULL;
150 /* Initialize lock */
151 vlc_mutex_init( &p_intf->p_sys->change_lock );
156 /*****************************************************************************
157 * intf_Close: destroy interface window
158 *****************************************************************************/
159 static void intf_Close( intf_thread_t *p_intf )
162 vlc_mutex_destroy( &p_intf->p_sys->change_lock );
164 /* Destroy structure */
165 free( p_intf->p_sys );
168 /*****************************************************************************
169 * intf_Run: Gnome thread
170 *****************************************************************************
171 * this part of the interface is in a separate thread so that we can call
172 * gtk_main() from within it without annoying the rest of the program.
173 * XXX: the approach may look kludgy, and probably is, but I could not find
174 * a better way to dynamically load a Gnome interface at runtime.
175 *****************************************************************************/
176 static void intf_Run( intf_thread_t *p_intf )
178 /* gnome_init needs to know the command line. We don't care, so we
179 * give it an empty one */
180 char *p_args[] = { "" };
182 /* The data types we are allowed to receive */
183 static GtkTargetEntry target_table[] =
185 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
186 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
189 /* Initialize Gnome */
190 gnome_init( p_main->psz_arg0, VERSION, 1, p_args );
192 /* Create some useful widgets that will certainly be used */
193 p_intf->p_sys->p_window = create_intf_window( );
194 p_intf->p_sys->p_popup = create_intf_popup( );
196 /* Set the title of the main window */
197 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
198 VOUT_TITLE " (Gnome interface)");
200 /* Accept file drops on the main window */
201 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
202 GTK_DEST_DEFAULT_ALL, target_table,
203 1, GDK_ACTION_COPY );
205 /* We don't create these ones yet because we perhaps won't need them */
206 p_intf->p_sys->p_about = NULL;
207 p_intf->p_sys->p_playlist = NULL;
208 p_intf->p_sys->p_modules = NULL;
209 p_intf->p_sys->p_fileopen = NULL;
210 p_intf->p_sys->p_disc = NULL;
212 /* Store p_intf to keep an eye on it */
213 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
216 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
219 /* Show the control window */
220 gtk_widget_show( p_intf->p_sys->p_window );
222 /* Sleep to avoid using all CPU - since some interfaces needs to access
223 * keyboard events, a 100ms delay is a good compromise */
224 p_intf->p_sys->i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000,
225 GnomeManage, p_intf );
228 /* Enter gnome mode */
231 /* launch stored callbacks */
232 if( p_intf->p_sys->pf_gtk_callback != NULL )
234 p_intf->p_sys->pf_gtk_callback();
236 if( p_intf->p_sys->pf_gdk_callback != NULL )
238 p_intf->p_sys->pf_gdk_callback();
243 /* following functions are local */
245 /*****************************************************************************
246 * GnomeManage: manage main thread messages
247 *****************************************************************************
248 * In this function, called approx. 10 times a second, we check what the
249 * main program wanted to tell us.
250 *****************************************************************************/
251 static gint GnomeManage( gpointer p_data )
253 intf_thread_t *p_intf = (void *)p_data;
255 vlc_mutex_lock( &p_intf->p_sys->change_lock );
257 /* If the "display popup" flag has changed */
258 if( p_intf->b_menu_change )
260 gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
261 NULL, NULL, NULL, NULL );
262 p_intf->b_menu_change = 0;
265 /* Update language/chapter menus after user request */
266 if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL &&
267 p_intf->p_sys->b_menus_update )
269 es_descriptor_t * p_audio_es;
270 es_descriptor_t * p_spu_es;
271 GtkWidget * p_menubar_menu;
272 GtkWidget * p_popup_menu;
275 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
276 p_intf->p_sys->p_window ), "menubar_title" ) );
278 GnomeTitleMenu( p_intf, p_menubar_menu, on_menubar_title_activate );
280 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
281 p_intf->p_sys->p_window ), "menubar_chapter" ) );
283 GnomeChapterMenu( p_intf, p_menubar_menu, on_menubar_chapter_activate );
285 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
286 p_intf->p_sys->p_popup ), "popup_navigation" ) );
288 GnomeTitleMenu( p_intf, p_popup_menu, on_popup_navigation_activate );
290 /* look for selected ES */
294 for( i = 0 ; i < p_intf->p_input->stream.i_selected_es_number ; i++ )
296 if( p_intf->p_input->stream.pp_es[i]->b_audio )
298 p_audio_es = p_intf->p_input->stream.pp_es[i];
301 if( p_intf->p_input->stream.pp_es[i]->b_spu )
303 p_spu_es = p_intf->p_input->stream.pp_es[i];
309 /* find audio root menu */
310 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
311 p_intf->p_sys->p_window ), "menubar_audio" ) );
313 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
314 p_intf->p_sys->p_popup ), "popup_audio" ) );
316 GnomeLanguageMenus( p_intf, p_menubar_menu, p_audio_es, 1,
317 on_menubar_audio_activate );
318 GnomeLanguageMenus( p_intf, p_popup_menu, p_audio_es, 1,
319 on_popup_audio_activate );
321 /* sub picture menus */
323 /* find spu root menu */
324 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
325 p_intf->p_sys->p_window ), "menubar_subtitle" ) );
327 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
328 p_intf->p_sys->p_popup ), "popup_subtitle" ) );
330 GnomeLanguageMenus( p_intf, p_menubar_menu, p_spu_es, 2,
331 on_menubar_subtitle_activate );
332 GnomeLanguageMenus( p_intf, p_popup_menu, p_spu_es, 2,
333 on_popup_subtitle_activate );
335 /* everything is ready */
336 p_intf->p_sys->b_menus_update = 0;
339 /* Manage the slider */
340 if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL
341 && p_intf->p_sys->b_scale_isfree )
344 GtkAdjustment *p_adj;
346 p_scale = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
347 p_intf->p_sys->p_window ), "hscale" ) );
348 p_adj = gtk_range_get_adjustment ( GTK_RANGE( p_scale ) );
350 /* Update the value */
351 p_adj->value = ( 100. *
352 p_intf->p_input->stream.p_selected_area->i_tell ) /
353 p_intf->p_input->stream.p_selected_area->i_size;
355 /* Gtv does it this way. Why not. */
356 gtk_range_set_adjustment ( GTK_RANGE( p_scale ), p_adj );
357 gtk_range_slider_update ( GTK_RANGE( p_scale ) );
358 gtk_range_clear_background ( GTK_RANGE( p_scale ) );
359 gtk_range_draw_background ( GTK_RANGE( p_scale ) );
362 /* Manage core vlc functions through the callback */
363 p_intf->pf_manage( p_intf );
367 /* Make sure we won't be called again */
368 gtk_timeout_remove( p_intf->p_sys->i_timeout );
370 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
372 /* Prepare to die, young Skywalker */
377 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
382 /*****************************************************************************
383 * GnomeMenuRadioItem: give a menu item adapted to language/title selection,
384 * ie the menu item is a radio button.
385 *****************************************************************************/
386 static GtkWidget * GnomeMenuRadioItem( GtkWidget * p_menu,
387 GSList ** p_button_group,
394 GtkWidget * p_button;
398 gtk_radio_button_new_with_label( *p_button_group, psz_name );
400 /* add button to group */
402 gtk_radio_button_group( GTK_RADIO_BUTTON( p_button ) );
404 /* prepare button for display */
405 gtk_widget_show( p_button );
407 /* create menu item to store button */
408 p_item = gtk_menu_item_new();
410 /* put button inside item */
411 gtk_container_add( GTK_CONTAINER( p_item ), p_button );
413 /* add item to menu */
414 gtk_menu_append( GTK_MENU( p_menu ), p_item );
416 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
417 GTK_SIGNAL_FUNC( on_audio_toggle ),
421 /* prepare item for display */
422 gtk_widget_show( p_item );
424 /* is it the selected item ? */
427 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( p_button ), TRUE );
430 p_item = gtk_menu_item_new_with_label( psz_name );
431 gtk_menu_append( GTK_MENU( p_menu ), p_item );
432 gtk_widget_show( p_item );
438 /*****************************************************************************
439 * GnomeLanguageMenus: update interactive menus of the interface
440 *****************************************************************************
441 * Sets up menus with information from input:
444 * Warning: since this function is designed to be called by management
445 * function, the interface lock has to be taken
446 *****************************************************************************/
447 static gint GnomeLanguageMenus( gpointer p_data,
449 es_descriptor_t * p_es,
451 void(*pf_activate )( GtkMenuItem *, gpointer ) )
453 intf_thread_t * p_intf;
455 GtkWidget * p_separator;
457 GSList * p_button_group;
467 p_intf = (intf_thread_t *)p_data;
469 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
471 b_audio = ( i_type == 1 );
472 p_button_group = NULL;
474 /* menu container for audio */
475 p_menu = gtk_menu_new();
477 /* create a set of language buttons and append them to the container */
478 b_active = ( p_es == NULL ) ? 1 : 0;
481 p_item = GnomeMenuRadioItem( p_menu, &p_button_group, b_active, psz_name );
483 /* setup signal hanling */
484 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
485 GTK_SIGNAL_FUNC ( pf_activate ), NULL );
487 p_separator = gtk_menu_item_new();
488 gtk_widget_show( p_separator );
489 gtk_menu_append( GTK_MENU( p_menu ), p_separator );
490 gtk_widget_set_sensitive( p_separator, FALSE );
492 for( i = 0 ; i < p_intf->p_input->stream.i_es_number ; i++ )
495 b_audio = ( i_type == 1 ) && p_intf->p_input->stream.pp_es[i]->b_audio;
496 b_spu = ( i_type == 2 ) && p_intf->p_input->stream.pp_es[i]->b_spu;
498 if( b_audio || b_spu )
500 b_active = ( p_es == p_intf->p_input->stream.pp_es[i] ) ? 1 : 0;
501 psz_name = p_intf->p_input->stream.pp_es[i]->psz_desc;
503 p_item = GnomeMenuRadioItem( p_menu, &p_button_group,
504 b_active, psz_name );
506 /* setup signal hanling */
507 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
508 GTK_SIGNAL_FUNC( pf_activate ),
509 (gpointer)( p_intf->p_input->stream.pp_es[i] ) );
514 /* link the new menu to the menubar item */
515 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
517 /* be sure that menu is sensitive */
518 gtk_widget_set_sensitive( p_root, TRUE );
520 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
525 /*****************************************************************************
526 * GnomeChapterMenu: generate chapter menu foir current title
527 *****************************************************************************/
528 static gint GnomeChapterMenu( gpointer p_data, GtkWidget * p_chapter,
529 void(*pf_activate )( GtkMenuItem *, gpointer ) )
531 intf_thread_t * p_intf;
533 GtkWidget * p_chapter_menu;
535 GSList * p_chapter_button_group;
541 p_intf = (intf_thread_t*)p_data;
543 i_title = p_intf->p_input->stream.p_selected_area->i_id;
544 p_chapter_menu = gtk_menu_new();
547 i_chapter < p_intf->p_input->stream.pp_areas[i_title]->i_part_nb ;
550 b_active = ( p_intf->p_input->stream.pp_areas[i_title]->i_part
551 == i_chapter + 1 ) ? 1 : 0;
553 sprintf( psz_name, "Chapter %d", i_chapter + 1 );
555 p_item = GnomeMenuRadioItem( p_chapter_menu, &p_chapter_button_group,
556 b_active, psz_name );
557 /* setup signal hanling */
558 gtk_signal_connect( GTK_OBJECT( p_item ),
560 GTK_SIGNAL_FUNC( pf_activate ),
561 (gpointer)(i_chapter + 1) );
564 /* link the new menu to the title menu item */
565 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter ),
568 /* be sure that chapter menu is sensitive */
569 gtk_widget_set_sensitive( p_chapter, TRUE );
574 /*****************************************************************************
575 * GnomeTitleMenu: sets menus for titles and chapters selection
576 *****************************************************************************
577 * Generates two type of menus:
578 * -simple list of titles
579 * -cascaded lists of chapters for each title
580 *****************************************************************************/
581 static gint GnomeTitleMenu( gpointer p_data,
582 GtkWidget * p_navigation,
583 void(*pf_activate )( GtkMenuItem *, gpointer ) )
585 intf_thread_t * p_intf;
587 GtkWidget * p_title_menu;
588 GtkWidget * p_title_item;
589 GtkWidget * p_chapter_menu;
591 GSList * p_title_button_group;
592 GSList * p_chapter_button_group;
598 p_intf = (intf_thread_t*)p_data;
600 p_title_menu = gtk_menu_new();
601 p_title_button_group = NULL;
602 p_chapter_button_group = NULL;
606 i_title < p_intf->p_input->stream.i_area_nb ;
609 b_active = ( p_intf->p_input->stream.pp_areas[i_title] ==
610 p_intf->p_input->stream.p_selected_area ) ? 1 : 0;
611 sprintf( psz_name, "Title %d", i_title );
613 p_title_item = GnomeMenuRadioItem( p_title_menu, &p_title_button_group,
614 b_active, psz_name );
616 if( pf_activate == on_menubar_title_activate )
618 /* setup signal hanling */
619 gtk_signal_connect( GTK_OBJECT( p_title_item ),
621 GTK_SIGNAL_FUNC( pf_activate ),
622 (gpointer)(p_intf->p_input->stream.pp_areas[i_title]) );
626 p_chapter_menu = gtk_menu_new();
630 p_intf->p_input->stream.pp_areas[i_title]->i_part_nb ;
633 b_active = ( p_intf->p_input->stream.pp_areas[i_title]->i_part
634 == i_chapter + 1 ) ? 1 : 0;
636 sprintf( psz_name, "Chapter %d", i_chapter + 1 );
638 p_item = GnomeMenuRadioItem( p_chapter_menu,
639 &p_chapter_button_group,
640 b_active, psz_name );
642 /* setup signal hanling */
643 gtk_signal_connect( GTK_OBJECT( p_item ),
645 GTK_SIGNAL_FUNC( pf_activate ),
646 (gpointer)( ( i_title * 100 ) + ( i_chapter + 1) ) );
649 /* link the new menu to the title menu item */
650 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_item ),
654 /* be sure that chapter menu is sensitive */
655 gtk_widget_set_sensitive( p_title_menu, TRUE );
659 /* link the new menu to the menubar audio item */
660 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_navigation ), p_title_menu );
662 /* be sure that audio menu is sensitive */
663 gtk_widget_set_sensitive( p_navigation, TRUE );