1 /*****************************************************************************
2 * intf_gtk.c: Gtk+ interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: intf_gtk.c,v 1.16 2001/05/06 18:32:30 stef Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Stéphane Borel <stef@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 #define MODULE_NAME gtk
26 #include "modules_inner.h"
28 /*****************************************************************************
30 *****************************************************************************/
33 #include <errno.h> /* ENOMEM */
34 #include <stdlib.h> /* free() */
35 #include <string.h> /* strerror() */
47 #include "stream_control.h"
48 #include "input_ext-intf.h"
51 #include "interface.h"
53 #include "gtk_callbacks.h"
54 #include "gtk_interface.h"
55 #include "gtk_support.h"
60 /*****************************************************************************
62 *****************************************************************************/
63 static int intf_Probe ( probedata_t *p_data );
64 static int intf_Open ( intf_thread_t *p_intf );
65 static void intf_Close ( intf_thread_t *p_intf );
66 static void intf_Run ( intf_thread_t *p_intf );
68 static gint GtkManage ( gpointer p_data );
69 static gint GtkLanguageMenus( gpointer, GtkWidget *, es_descriptor_t *, gint,
70 void (*pf_activate)(GtkMenuItem *, gpointer) );
71 static gint GtkChapterMenu ( gpointer, GtkWidget *,
72 void (*pf_activate)(GtkMenuItem *, gpointer) );
73 static gint GtkTitleMenu ( gpointer, GtkWidget *,
74 void (*pf_activate)(GtkMenuItem *, gpointer) );
75 static void GtkDisplayDate ( GtkAdjustment *p_adj );
77 void GtkPlayListManage( gpointer p_data );
79 /*****************************************************************************
80 * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
81 *****************************************************************************
82 * gtk_init() makes several calls to g_atexit() which calls atexit() to
83 * register tidying callbacks to be called at program exit. Since the Gtk+
84 * plugin is likely to be unloaded at program exit, we have to export this
85 * symbol to intercept the g_atexit() calls. Talk about crude hack.
86 *****************************************************************************/
87 void g_atexit( GVoidFunc func )
89 intf_thread_t *p_intf = p_main->p_intf;
91 if( p_intf->p_sys->pf_gdk_callback == NULL )
93 p_intf->p_sys->pf_gdk_callback = func;
95 else if( p_intf->p_sys->pf_gtk_callback == NULL )
97 p_intf->p_sys->pf_gtk_callback = func;
99 /* else nothing, but we could do something here */
103 /*****************************************************************************
104 * Functions exported as capabilities. They are declared as static so that
105 * we don't pollute the namespace too much.
106 *****************************************************************************/
107 void _M( intf_getfunctions )( function_list_t * p_function_list )
109 p_function_list->pf_probe = intf_Probe;
110 p_function_list->functions.intf.pf_open = intf_Open;
111 p_function_list->functions.intf.pf_close = intf_Close;
112 p_function_list->functions.intf.pf_run = intf_Run;
115 /*****************************************************************************
116 * intf_Probe: probe the interface and return a score
117 *****************************************************************************
118 * This function tries to initialize Gtk+ and returns a score to the
119 * plugin manager so that it can select the best plugin.
120 *****************************************************************************/
121 static int intf_Probe( probedata_t *p_data )
123 if( TestMethod( INTF_METHOD_VAR, "gtk" ) )
128 if( TestProgram( "gvlc" ) )
136 /*****************************************************************************
137 * intf_Open: initialize and create window
138 *****************************************************************************/
139 static int intf_Open( intf_thread_t *p_intf )
141 /* Allocate instance and initialize some members */
142 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
143 if( p_intf->p_sys == NULL )
145 intf_ErrMsg("error: %s", strerror(ENOMEM));
149 /* Initialize Gtk+ thread */
150 p_intf->p_sys->b_popup_changed = 0;
151 p_intf->p_sys->b_window_changed = 0;
152 p_intf->p_sys->b_playlist_changed = 0;
154 p_intf->p_sys->b_menus_update = 1;
155 p_intf->p_sys->b_slider_free = 1;
158 p_intf->p_sys->i_playing = -1;
160 p_intf->p_sys->pf_gtk_callback = NULL;
161 p_intf->p_sys->pf_gdk_callback = NULL;
166 /*****************************************************************************
167 * intf_Close: destroy interface window
168 *****************************************************************************/
169 static void intf_Close( intf_thread_t *p_intf )
171 /* Destroy structure */
172 free( p_intf->p_sys );
175 /*****************************************************************************
176 * intf_Run: Gtk+ thread
177 *****************************************************************************
178 * this part of the interface is in a separate thread so that we can call
179 * gtk_main() from within it without annoying the rest of the program.
180 * XXX: the approach may look kludgy, and probably is, but I could not find
181 * a better way to dynamically load a Gtk+ interface at runtime.
182 *****************************************************************************/
183 static void intf_Run( intf_thread_t *p_intf )
185 /* gtk_init needs to know the command line. We don't care, so we
186 * give it an empty one */
187 char *p_args[] = { "" };
188 char **pp_args = p_args;
191 /* The data types we are allowed to receive */
192 static GtkTargetEntry target_table[] =
194 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
195 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
198 /* intf_Manage callback timeout */
201 /* Initialize Gtk+ */
202 gtk_init( &i_args, &pp_args );
204 /* Create some useful widgets that will certainly be used */
205 p_intf->p_sys->p_window = create_intf_window( );
206 p_intf->p_sys->p_popup = create_intf_popup( );
207 p_intf->p_sys->p_disc = create_intf_disc( );
208 p_intf->p_sys->p_network = create_intf_network( );
209 p_intf->p_sys->p_playlist = create_intf_playlist( );
212 /* Set the title of the main window */
213 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
214 VOUT_TITLE " (Gtk+ interface)");
216 /* Accept file drops on the main window */
217 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
218 GTK_DEST_DEFAULT_ALL, target_table,
219 1, GDK_ACTION_COPY );
221 /* Accept file drops on the playlist window */
222 gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playlist,
224 GTK_DEST_DEFAULT_ALL, target_table,
225 1, GDK_ACTION_COPY );
227 /* Get the interface labels */
228 p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
229 GTK_OBJECT(p_intf->p_sys->p_window ), "slider_frame" ) );
231 /* Connect the date display to the slider */
232 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
233 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
234 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
236 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
237 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
238 p_intf->p_sys->f_adj_oldvalue = 0;
241 /* We don't create these ones yet because we perhaps won't need them */
242 p_intf->p_sys->p_about = NULL;
243 p_intf->p_sys->p_modules = NULL;
244 p_intf->p_sys->p_fileopen = NULL;
246 /* Store p_intf to keep an eye on it */
247 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
250 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
253 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_playlist),
256 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_disc),
259 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_network),
262 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
265 /* Show the control window */
266 gtk_widget_show( p_intf->p_sys->p_window );
268 /* Sleep to avoid using all CPU - since some interfaces needs to access
269 * keyboard events, a 100ms delay is a good compromise */
270 i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GtkManage, p_intf );
275 /* Remove the timeout */
276 gtk_timeout_remove( i_timeout );
278 /* Launch stored callbacks */
279 if( p_intf->p_sys->pf_gtk_callback != NULL )
281 p_intf->p_sys->pf_gtk_callback();
283 if( p_intf->p_sys->pf_gdk_callback != NULL )
285 p_intf->p_sys->pf_gdk_callback();
290 /* following functions are local */
292 /*****************************************************************************
293 * GtkManage: manage main thread messages
294 *****************************************************************************
295 * In this function, called approx. 10 times a second, we check what the
296 * main program wanted to tell us.
297 *****************************************************************************/
299 static gint GtkManage( gpointer p_data )
301 #define p_intf ((intf_thread_t *)p_data)
303 GtkPlayListManage( p_data );
305 vlc_mutex_lock( &p_intf->change_lock );
307 /* If the "display popup" flag has changed */
308 if( p_intf->b_menu_change )
310 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
312 p_intf->p_sys->p_popup = create_intf_popup();
313 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
316 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
317 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
318 p_intf->b_menu_change = 0;
321 if( p_intf->p_input != NULL )
323 /* Used by TS input when PMT changes */
324 if( p_intf->p_input->stream.b_changed )
326 p_intf->p_sys->b_menus_update = 1;
327 p_intf->p_input->stream.b_changed = 0;
329 "Interface menus refreshed as stream has changed" );
334 /* Update language/chapter menus after user request */
335 if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL &&
336 p_intf->p_sys->b_menus_update )
338 es_descriptor_t * p_audio_es;
339 es_descriptor_t * p_spu_es;
340 GtkWidget * p_menubar_menu;
341 GtkWidget * p_popup_menu;
344 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
345 p_intf->p_sys->p_window ), "menubar_title" ) );
347 GtkTitleMenu( p_intf, p_menubar_menu, on_menubar_title_activate );
349 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
350 p_intf->p_sys->p_window ), "menubar_chapter" ) );
352 GtkChapterMenu( p_intf, p_menubar_menu, on_menubar_chapter_activate );
354 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
355 p_intf->p_sys->p_popup ), "popup_navigation" ) );
357 GtkTitleMenu( p_intf, p_popup_menu, on_popup_navigation_activate );
359 /* look for selected ES */
363 for( i = 0 ; i < p_intf->p_input->stream.i_selected_es_number ; i++ )
365 if( p_intf->p_input->stream.pp_es[i]->i_cat == AUDIO_ES )
367 p_audio_es = p_intf->p_input->stream.pp_es[i];
370 if( p_intf->p_input->stream.pp_es[i]->i_cat == SPU_ES )
372 p_spu_es = p_intf->p_input->stream.pp_es[i];
378 /* find audio root menu */
379 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
380 p_intf->p_sys->p_window ), "menubar_audio" ) );
382 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
383 p_intf->p_sys->p_popup ), "popup_audio" ) );
385 GtkLanguageMenus( p_intf, p_menubar_menu, p_audio_es, AUDIO_ES,
386 on_menubar_audio_activate );
387 GtkLanguageMenus( p_intf, p_popup_menu, p_audio_es, AUDIO_ES,
388 on_popup_audio_activate );
390 /* sub picture menus */
392 /* find spu root menu */
393 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
394 p_intf->p_sys->p_window ), "menubar_subpictures" ) );
396 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
397 p_intf->p_sys->p_popup ), "popup_subpictures" ) );
399 GtkLanguageMenus( p_intf, p_menubar_menu, p_spu_es, SPU_ES,
400 on_menubar_subpictures_activate );
401 GtkLanguageMenus( p_intf, p_popup_menu, p_spu_es, SPU_ES,
402 on_popup_subpictures_activate );
404 /* everything is ready */
405 p_intf->p_sys->b_menus_update = 0;
408 /* Manage the slider */
409 if( p_intf->p_input != NULL && p_intf->p_input->stream.b_seekable )
411 float newvalue = p_intf->p_sys->p_adj->value;
413 #define p_area p_intf->p_input->stream.p_selected_area
414 /* If the user hasn't touched the slider since the last time,
415 * then the input can safely change it */
416 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
418 /* Update the value */
419 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
420 ( 100. * p_area->i_tell ) / p_area->i_size;
422 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
425 /* Otherwise, send message to the input if the user has
426 * finished dragging the slider */
427 else if( p_intf->p_sys->b_slider_free )
429 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
431 input_Seek( p_intf->p_input, i_seek );
433 /* Update the old value */
434 p_intf->p_sys->f_adj_oldvalue = newvalue;
439 /* Manage core vlc functions through the callback */
440 p_intf->pf_manage( p_intf );
444 vlc_mutex_unlock( &p_intf->change_lock );
446 /* Prepare to die, young Skywalker */
453 vlc_mutex_unlock( &p_intf->change_lock );
460 /*****************************************************************************
461 * GtkMenuRadioItem: give a menu item adapted to language/title selection,
462 * ie the menu item is a radio button.
463 *****************************************************************************/
464 static GtkWidget * GtkMenuRadioItem( GtkWidget * p_menu,
465 GSList ** p_button_group,
472 GtkWidget * p_button;
476 gtk_radio_button_new_with_label( *p_button_group, psz_name );
478 /* add button to group */
480 gtk_radio_button_group( GTK_RADIO_BUTTON( p_button ) );
482 /* prepare button for display */
483 gtk_widget_show( p_button );
485 /* create menu item to store button */
486 p_item = gtk_menu_item_new();
488 /* put button inside item */
489 gtk_container_add( GTK_CONTAINER( p_item ), p_button );
491 /* add item to menu */
492 gtk_menu_append( GTK_MENU( p_menu ), p_item );
494 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
495 GTK_SIGNAL_FUNC( on_audio_toggle ),
499 /* prepare item for display */
500 gtk_widget_show( p_item );
502 /* is it the selected item ? */
505 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( p_button ), TRUE );
508 p_item = gtk_menu_item_new_with_label( psz_name );
509 gtk_menu_append( GTK_MENU( p_menu ), p_item );
510 gtk_widget_show( p_item );
516 /*****************************************************************************
517 * GtkLanguageMenus: update interactive menus of the interface
518 *****************************************************************************
519 * Sets up menus with information from input:
522 * Warning: since this function is designed to be called by management
523 * function, the interface lock has to be taken
524 *****************************************************************************/
525 static gint GtkLanguageMenus( gpointer p_data,
527 es_descriptor_t * p_es,
529 void(*pf_activate )( GtkMenuItem *, gpointer ) )
531 intf_thread_t * p_intf;
533 GtkWidget * p_separator;
535 GSList * p_button_group;
543 p_intf = (intf_thread_t *)p_data;
545 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
547 p_button_group = NULL;
549 /* menu container for audio */
550 p_menu = gtk_menu_new();
552 /* create a set of language buttons and append them to the container */
553 b_active = ( p_es == NULL );
556 p_item = GtkMenuRadioItem( p_menu, &p_button_group, b_active, psz_name );
558 /* setup signal hanling */
559 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
560 GTK_SIGNAL_FUNC ( pf_activate ), NULL );
562 p_separator = gtk_menu_item_new();
563 gtk_widget_show( p_separator );
564 gtk_menu_append( GTK_MENU( p_menu ), p_separator );
565 gtk_widget_set_sensitive( p_separator, FALSE );
567 for( i = 0 ; i < p_intf->p_input->stream.i_es_number ; i++ )
569 if( p_intf->p_input->stream.pp_es[i]->i_cat == i_cat )
571 b_active = ( p_es == p_intf->p_input->stream.pp_es[i] ) ? 1 : 0;
572 psz_name = p_intf->p_input->stream.pp_es[i]->psz_desc;
574 p_item = GtkMenuRadioItem( p_menu, &p_button_group,
575 b_active, psz_name );
577 /* setup signal hanling */
578 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
579 GTK_SIGNAL_FUNC( pf_activate ),
580 (gpointer)( p_intf->p_input->stream.pp_es[i] ) );
585 /* link the new menu to the menubar item */
586 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
588 /* be sure that menu is sensitive */
589 gtk_widget_set_sensitive( p_root, TRUE );
591 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
596 /*****************************************************************************
597 * GtkChapterMenu: generate chapter menu for current title
598 *****************************************************************************/
599 static gint GtkChapterMenu( gpointer p_data, GtkWidget * p_chapter,
600 void(*pf_activate )( GtkMenuItem *, gpointer ) )
602 intf_thread_t * p_intf;
603 char psz_name[ GTK_MENU_LABEL_SIZE ];
604 GtkWidget * p_chapter_menu;
606 GSList * p_chapter_button_group;
612 p_intf = (intf_thread_t*)p_data;
614 i_title = p_intf->p_input->stream.p_selected_area->i_id;
615 p_chapter_menu = gtk_menu_new();
618 i_chapter < p_intf->p_input->stream.pp_areas[i_title]->i_part_nb ;
621 b_active = ( p_intf->p_input->stream.pp_areas[i_title]->i_part
622 == i_chapter + 1 ) ? 1 : 0;
624 snprintf( psz_name, GTK_MENU_LABEL_SIZE,
625 "Chapter %d", i_chapter + 1 );
626 psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
628 p_item = GtkMenuRadioItem( p_chapter_menu, &p_chapter_button_group,
629 b_active, psz_name );
630 /* setup signal hanling */
631 gtk_signal_connect( GTK_OBJECT( p_item ),
633 GTK_SIGNAL_FUNC( pf_activate ),
634 (gpointer)(i_chapter + 1) );
637 /* link the new menu to the title menu item */
638 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter ),
641 /* be sure that chapter menu is sensitive */
642 gtk_widget_set_sensitive( p_chapter, TRUE );
647 /*****************************************************************************
648 * GtkTitleMenu: sets menus for titles and chapters selection
649 *****************************************************************************
650 * Generates two type of menus:
651 * -simple list of titles
652 * -cascaded lists of chapters for each title
653 *****************************************************************************/
654 static gint GtkTitleMenu( gpointer p_data,
655 GtkWidget * p_navigation,
656 void(*pf_activate )( GtkMenuItem *, gpointer ) )
658 intf_thread_t * p_intf;
659 char psz_name[ GTK_MENU_LABEL_SIZE ];
660 GtkWidget * p_title_menu;
661 GtkWidget * p_title_item;
662 GtkWidget * p_chapter_menu;
664 GSList * p_title_button_group;
665 GSList * p_chapter_button_group;
671 p_intf = (intf_thread_t*)p_data;
673 p_title_menu = gtk_menu_new();
674 p_title_button_group = NULL;
675 p_chapter_button_group = NULL;
679 i_title < p_intf->p_input->stream.i_area_nb ;
682 b_active = ( p_intf->p_input->stream.pp_areas[i_title] ==
683 p_intf->p_input->stream.p_selected_area ) ? 1 : 0;
684 snprintf( psz_name, GTK_MENU_LABEL_SIZE, "Title %d", i_title );
685 psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
687 p_title_item = GtkMenuRadioItem( p_title_menu, &p_title_button_group,
688 b_active, psz_name );
690 if( pf_activate == on_menubar_title_activate )
692 /* setup signal hanling */
693 gtk_signal_connect( GTK_OBJECT( p_title_item ),
695 GTK_SIGNAL_FUNC( pf_activate ),
696 (gpointer)(p_intf->p_input->stream.pp_areas[i_title]) );
700 p_chapter_menu = gtk_menu_new();
704 p_intf->p_input->stream.pp_areas[i_title]->i_part_nb ;
707 b_active = ( p_intf->p_input->stream.pp_areas[i_title]->i_part
708 == i_chapter + 1 ) ? 1 : 0;
710 snprintf( psz_name, GTK_MENU_LABEL_SIZE,
711 "Chapter %d", i_chapter + 1 );
712 psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
714 p_item = GtkMenuRadioItem( p_chapter_menu,
715 &p_chapter_button_group,
716 b_active, psz_name );
718 /* setup signal hanling */
719 gtk_signal_connect( GTK_OBJECT( p_item ),
721 GTK_SIGNAL_FUNC( pf_activate ),
722 (gpointer)( ( i_title * 100 ) + ( i_chapter + 1) ) );
725 /* link the new menu to the title menu item */
726 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_item ),
730 /* be sure that chapter menu is sensitive */
731 gtk_widget_set_sensitive( p_title_menu, TRUE );
735 /* link the new menu to the menubar audio item */
736 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_navigation ), p_title_menu );
738 /* be sure that audio menu is sensitive */
739 gtk_widget_set_sensitive( p_navigation, TRUE );
745 void GtkDisplayDate( GtkAdjustment *p_adj )
747 intf_thread_t *p_intf;
749 p_intf = gtk_object_get_data( GTK_OBJECT( p_adj ), "p_intf" );
751 if( p_intf->p_input != NULL )
753 #define p_area p_intf->p_input->stream.p_selected_area
754 char psz_time[ OFFSETTOTIME_MAX_SIZE ];
756 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
758 gtk_frame_set_label( p_intf->p_sys->p_slider_frame,
759 input_OffsetToTime( p_intf->p_input, psz_time,
760 ( p_area->i_size * p_adj->value ) / 100 ) );
762 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );