1 /*****************************************************************************
2 * intf_gtk.c: Gtk+ interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: intf_gtk.c,v 1.13 2001/04/13 01:49:22 henri 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 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
229 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
230 p_intf->p_sys->p_label_date = P_LABEL( "label_date" );
231 p_intf->p_sys->p_label_status = P_LABEL( "label_status" );
234 /* Connect the date display to the slider */
235 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
236 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
237 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
239 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
240 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
241 p_intf->p_sys->f_adj_oldvalue = 0;
244 /* We don't create these ones yet because we perhaps won't need them */
245 p_intf->p_sys->p_about = NULL;
246 p_intf->p_sys->p_modules = NULL;
247 p_intf->p_sys->p_fileopen = NULL;
249 /* Store p_intf to keep an eye on it */
250 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
253 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
256 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_playlist),
259 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_disc),
262 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_network),
265 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
268 /* Show the control window */
269 gtk_widget_show( p_intf->p_sys->p_window );
271 /* Sleep to avoid using all CPU - since some interfaces needs to access
272 * keyboard events, a 100ms delay is a good compromise */
273 i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GtkManage, p_intf );
278 /* Remove the timeout */
279 gtk_timeout_remove( i_timeout );
281 /* Launch stored callbacks */
282 if( p_intf->p_sys->pf_gtk_callback != NULL )
284 p_intf->p_sys->pf_gtk_callback();
286 if( p_intf->p_sys->pf_gdk_callback != NULL )
288 p_intf->p_sys->pf_gdk_callback();
293 /* following functions are local */
295 /*****************************************************************************
296 * GtkManage: manage main thread messages
297 *****************************************************************************
298 * In this function, called approx. 10 times a second, we check what the
299 * main program wanted to tell us.
300 *****************************************************************************/
302 static gint GtkManage( gpointer p_data )
304 #define p_intf ((intf_thread_t *)p_data)
306 GtkPlayListManage( p_data );
308 vlc_mutex_lock( &p_intf->change_lock );
310 /* If the "display popup" flag has changed */
311 if( p_intf->b_menu_change )
313 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
315 p_intf->p_sys->p_popup = create_intf_popup();
316 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
319 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
320 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
321 p_intf->b_menu_change = 0;
324 if( p_intf->p_input != NULL )
326 /* Used by TS input when PMT changes */
327 if( p_intf->p_input->stream.b_changed )
329 p_intf->p_sys->b_menus_update = 1;
330 p_intf->p_input->stream.b_changed = 0;
332 "Interface menus refreshed as stream has changed" );
337 /* Update language/chapter menus after user request */
338 if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL &&
339 p_intf->p_sys->b_menus_update )
341 es_descriptor_t * p_audio_es;
342 es_descriptor_t * p_spu_es;
343 GtkWidget * p_menubar_menu;
344 GtkWidget * p_popup_menu;
347 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
348 p_intf->p_sys->p_window ), "menubar_title" ) );
350 GtkTitleMenu( p_intf, p_menubar_menu, on_menubar_title_activate );
352 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
353 p_intf->p_sys->p_window ), "menubar_chapter" ) );
355 GtkChapterMenu( p_intf, p_menubar_menu, on_menubar_chapter_activate );
357 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
358 p_intf->p_sys->p_popup ), "popup_navigation" ) );
360 GtkTitleMenu( p_intf, p_popup_menu, on_popup_navigation_activate );
362 /* look for selected ES */
366 for( i = 0 ; i < p_intf->p_input->stream.i_selected_es_number ; i++ )
368 if( p_intf->p_input->stream.pp_es[i]->i_cat == AUDIO_ES )
370 p_audio_es = p_intf->p_input->stream.pp_es[i];
373 if( p_intf->p_input->stream.pp_es[i]->i_cat == SPU_ES )
375 p_spu_es = p_intf->p_input->stream.pp_es[i];
381 /* find audio root menu */
382 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
383 p_intf->p_sys->p_window ), "menubar_audio" ) );
385 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
386 p_intf->p_sys->p_popup ), "popup_audio" ) );
388 GtkLanguageMenus( p_intf, p_menubar_menu, p_audio_es, AUDIO_ES,
389 on_menubar_audio_activate );
390 GtkLanguageMenus( p_intf, p_popup_menu, p_audio_es, AUDIO_ES,
391 on_popup_audio_activate );
393 /* sub picture menus */
395 /* find spu root menu */
396 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
397 p_intf->p_sys->p_window ), "menubar_subpictures" ) );
399 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
400 p_intf->p_sys->p_popup ), "popup_subpictures" ) );
402 GtkLanguageMenus( p_intf, p_menubar_menu, p_spu_es, SPU_ES,
403 on_menubar_subpictures_activate );
404 GtkLanguageMenus( p_intf, p_popup_menu, p_spu_es, SPU_ES,
405 on_popup_subpictures_activate );
407 /* everything is ready */
408 p_intf->p_sys->b_menus_update = 0;
411 /* Manage the slider */
412 if( p_intf->p_input != NULL )
414 float newvalue = p_intf->p_sys->p_adj->value;
416 #define p_area p_intf->p_input->stream.p_selected_area
417 /* If the user hasn't touched the slider since the last time,
418 * then the input can safely change it */
419 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
421 /* Update the value */
422 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
423 ( 100. * p_area->i_tell ) / p_area->i_size;
425 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
428 /* Otherwise, send message to the input if the user has
429 * finished dragging the slider */
430 else if( p_intf->p_sys->b_slider_free )
432 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
434 input_Seek( p_intf->p_input, i_seek );
436 /* Update the old value */
437 p_intf->p_sys->f_adj_oldvalue = newvalue;
442 /* Manage core vlc functions through the callback */
443 p_intf->pf_manage( p_intf );
447 vlc_mutex_unlock( &p_intf->change_lock );
449 /* Prepare to die, young Skywalker */
456 vlc_mutex_unlock( &p_intf->change_lock );
463 /*****************************************************************************
464 * GtkMenuRadioItem: give a menu item adapted to language/title selection,
465 * ie the menu item is a radio button.
466 *****************************************************************************/
467 static GtkWidget * GtkMenuRadioItem( GtkWidget * p_menu,
468 GSList ** p_button_group,
475 GtkWidget * p_button;
479 gtk_radio_button_new_with_label( *p_button_group, psz_name );
481 /* add button to group */
483 gtk_radio_button_group( GTK_RADIO_BUTTON( p_button ) );
485 /* prepare button for display */
486 gtk_widget_show( p_button );
488 /* create menu item to store button */
489 p_item = gtk_menu_item_new();
491 /* put button inside item */
492 gtk_container_add( GTK_CONTAINER( p_item ), p_button );
494 /* add item to menu */
495 gtk_menu_append( GTK_MENU( p_menu ), p_item );
497 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
498 GTK_SIGNAL_FUNC( on_audio_toggle ),
502 /* prepare item for display */
503 gtk_widget_show( p_item );
505 /* is it the selected item ? */
508 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( p_button ), TRUE );
511 p_item = gtk_menu_item_new_with_label( psz_name );
512 gtk_menu_append( GTK_MENU( p_menu ), p_item );
513 gtk_widget_show( p_item );
519 /*****************************************************************************
520 * GtkLanguageMenus: update interactive menus of the interface
521 *****************************************************************************
522 * Sets up menus with information from input:
525 * Warning: since this function is designed to be called by management
526 * function, the interface lock has to be taken
527 *****************************************************************************/
528 static gint GtkLanguageMenus( gpointer p_data,
530 es_descriptor_t * p_es,
532 void(*pf_activate )( GtkMenuItem *, gpointer ) )
534 intf_thread_t * p_intf;
536 GtkWidget * p_separator;
538 GSList * p_button_group;
546 p_intf = (intf_thread_t *)p_data;
548 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
550 p_button_group = NULL;
552 /* menu container for audio */
553 p_menu = gtk_menu_new();
555 /* create a set of language buttons and append them to the container */
556 b_active = ( p_es == NULL );
559 p_item = GtkMenuRadioItem( p_menu, &p_button_group, b_active, psz_name );
561 /* setup signal hanling */
562 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
563 GTK_SIGNAL_FUNC ( pf_activate ), NULL );
565 p_separator = gtk_menu_item_new();
566 gtk_widget_show( p_separator );
567 gtk_menu_append( GTK_MENU( p_menu ), p_separator );
568 gtk_widget_set_sensitive( p_separator, FALSE );
570 for( i = 0 ; i < p_intf->p_input->stream.i_es_number ; i++ )
572 if( p_intf->p_input->stream.pp_es[i]->i_cat == i_cat )
574 b_active = ( p_es == p_intf->p_input->stream.pp_es[i] ) ? 1 : 0;
575 psz_name = p_intf->p_input->stream.pp_es[i]->psz_desc;
577 p_item = GtkMenuRadioItem( p_menu, &p_button_group,
578 b_active, psz_name );
580 /* setup signal hanling */
581 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
582 GTK_SIGNAL_FUNC( pf_activate ),
583 (gpointer)( p_intf->p_input->stream.pp_es[i] ) );
588 /* link the new menu to the menubar item */
589 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
591 /* be sure that menu is sensitive */
592 gtk_widget_set_sensitive( p_root, TRUE );
594 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
599 /*****************************************************************************
600 * GtkChapterMenu: generate chapter menu for current title
601 *****************************************************************************/
602 static gint GtkChapterMenu( gpointer p_data, GtkWidget * p_chapter,
603 void(*pf_activate )( GtkMenuItem *, gpointer ) )
605 intf_thread_t * p_intf;
607 GtkWidget * p_chapter_menu;
609 GSList * p_chapter_button_group;
615 p_intf = (intf_thread_t*)p_data;
617 i_title = p_intf->p_input->stream.p_selected_area->i_id;
618 p_chapter_menu = gtk_menu_new();
621 i_chapter < p_intf->p_input->stream.pp_areas[i_title]->i_part_nb ;
624 b_active = ( p_intf->p_input->stream.pp_areas[i_title]->i_part
625 == i_chapter + 1 ) ? 1 : 0;
627 sprintf( psz_name, "Chapter %d", i_chapter + 1 );
629 p_item = GtkMenuRadioItem( p_chapter_menu, &p_chapter_button_group,
630 b_active, psz_name );
631 /* setup signal hanling */
632 gtk_signal_connect( GTK_OBJECT( p_item ),
634 GTK_SIGNAL_FUNC( pf_activate ),
635 (gpointer)(i_chapter + 1) );
638 /* link the new menu to the title menu item */
639 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter ),
642 /* be sure that chapter menu is sensitive */
643 gtk_widget_set_sensitive( p_chapter, TRUE );
648 /*****************************************************************************
649 * GtkTitleMenu: sets menus for titles and chapters selection
650 *****************************************************************************
651 * Generates two type of menus:
652 * -simple list of titles
653 * -cascaded lists of chapters for each title
654 *****************************************************************************/
655 static gint GtkTitleMenu( gpointer p_data,
656 GtkWidget * p_navigation,
657 void(*pf_activate )( GtkMenuItem *, gpointer ) )
659 intf_thread_t * p_intf;
661 GtkWidget * p_title_menu;
662 GtkWidget * p_title_item;
663 GtkWidget * p_chapter_menu;
665 GSList * p_title_button_group;
666 GSList * p_chapter_button_group;
672 p_intf = (intf_thread_t*)p_data;
674 p_title_menu = gtk_menu_new();
675 p_title_button_group = NULL;
676 p_chapter_button_group = NULL;
680 i_title < p_intf->p_input->stream.i_area_nb ;
683 b_active = ( p_intf->p_input->stream.pp_areas[i_title] ==
684 p_intf->p_input->stream.p_selected_area ) ? 1 : 0;
685 sprintf( psz_name, "Title %d", i_title );
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 sprintf( psz_name, "Chapter %d", i_chapter + 1 );
712 p_item = GtkMenuRadioItem( p_chapter_menu,
713 &p_chapter_button_group,
714 b_active, psz_name );
716 /* setup signal hanling */
717 gtk_signal_connect( GTK_OBJECT( p_item ),
719 GTK_SIGNAL_FUNC( pf_activate ),
720 (gpointer)( ( i_title * 100 ) + ( i_chapter + 1) ) );
723 /* link the new menu to the title menu item */
724 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_item ),
728 /* be sure that chapter menu is sensitive */
729 gtk_widget_set_sensitive( p_title_menu, TRUE );
733 /* link the new menu to the menubar audio item */
734 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_navigation ), p_title_menu );
736 /* be sure that audio menu is sensitive */
737 gtk_widget_set_sensitive( p_navigation, TRUE );
743 void GtkDisplayDate( GtkAdjustment *p_adj )
745 intf_thread_t *p_intf;
747 p_intf = gtk_object_get_data( GTK_OBJECT( p_adj ), "p_intf" );
749 if( p_intf->p_input != NULL )
751 #define p_area p_intf->p_input->stream.p_selected_area
752 char psz_time[ OFFSETTOTIME_MAX_SIZE ];
754 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
756 gtk_label_set_text( p_intf->p_sys->p_label_date,
757 input_OffsetToTime( p_intf->p_input, psz_time,
758 ( p_area->i_size * p_adj->value ) / 100 ) );
760 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );