1 /*****************************************************************************
2 * intf_gtk.c: Gtk+ interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: intf_gtk.c,v 1.6 2001/03/08 13:32:55 octplane 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"
54 #include "gtk_callbacks.h"
55 #include "gtk_interface.h"
56 #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 void GtkPlayListManage( gpointer p_data );
78 /*****************************************************************************
79 * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
80 *****************************************************************************
81 * gtk_init() makes several calls to g_atexit() which calls atexit() to
82 * register tidying callbacks to be called at program exit. Since the Gtk+
83 * plugin is likely to be unloaded at program exit, we have to export this
84 * symbol to intercept the g_atexit() calls. Talk about crude hack.
85 *****************************************************************************/
86 void g_atexit( GVoidFunc func )
88 intf_thread_t *p_intf = p_main->p_intf;
90 if( p_intf->p_sys->pf_gdk_callback == NULL )
92 p_intf->p_sys->pf_gdk_callback = func;
94 else if( p_intf->p_sys->pf_gtk_callback == NULL )
96 p_intf->p_sys->pf_gtk_callback = func;
98 /* else nothing, but we could do something here */
102 /*****************************************************************************
103 * Functions exported as capabilities. They are declared as static so that
104 * we don't pollute the namespace too much.
105 *****************************************************************************/
106 void _M( intf_getfunctions )( function_list_t * p_function_list )
108 p_function_list->pf_probe = intf_Probe;
109 p_function_list->functions.intf.pf_open = intf_Open;
110 p_function_list->functions.intf.pf_close = intf_Close;
111 p_function_list->functions.intf.pf_run = intf_Run;
114 /*****************************************************************************
115 * intf_Probe: probe the interface and return a score
116 *****************************************************************************
117 * This function tries to initialize Gtk+ and returns a score to the
118 * plugin manager so that it can select the best plugin.
119 *****************************************************************************/
120 static int intf_Probe( probedata_t *p_data )
122 if( TestMethod( INTF_METHOD_VAR, "gtk" ) )
130 /*****************************************************************************
131 * intf_Open: initialize and create window
132 *****************************************************************************/
133 static int intf_Open( intf_thread_t *p_intf )
135 /* Allocate instance and initialize some members */
136 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
137 if( p_intf->p_sys == NULL )
139 intf_ErrMsg("error: %s", strerror(ENOMEM));
143 /* Initialize Gtk+ thread */
144 p_intf->p_sys->b_popup_changed = 0;
145 p_intf->p_sys->b_window_changed = 0;
146 p_intf->p_sys->b_playlist_changed = 0;
148 p_intf->p_sys->b_menus_update = 1;
149 p_intf->p_sys->b_scale_isfree = 1;
152 p_intf->p_sys->i_playing = -1;
154 p_intf->p_sys->pf_gtk_callback = NULL;
155 p_intf->p_sys->pf_gdk_callback = NULL;
157 /* Initialize lock */
158 vlc_mutex_init( &p_intf->p_sys->change_lock );
163 /*****************************************************************************
164 * intf_Close: destroy interface window
165 *****************************************************************************/
166 static void intf_Close( intf_thread_t *p_intf )
169 vlc_mutex_destroy( &p_intf->p_sys->change_lock );
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;
192 /* The data types we are allowed to receive */
193 static GtkTargetEntry target_table[] =
195 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
196 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
199 /* Initialize Gtk+ */
200 gtk_init( &i_args, &pp_args );
202 /* Create some useful widgets that will certainly be used */
203 p_intf->p_sys->p_window = create_intf_window( );
204 p_intf->p_sys->p_popup = create_intf_popup( );
205 p_intf->p_sys->p_playlist = create_intf_playlist( );
207 /* Set the title of the main window */
208 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
209 VOUT_TITLE " (Gtk+ interface)");
211 /* Accept file drops on the main window */
212 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
213 GTK_DEST_DEFAULT_ALL, target_table,
214 1, GDK_ACTION_COPY );
216 /* Accept file drops on the playlist window */
217 temp = lookup_widget(p_intf->p_sys->p_playlist, "clist1");
218 gtk_drag_dest_set( GTK_WIDGET( temp ),
219 GTK_DEST_DEFAULT_ALL, target_table,
220 1, GDK_ACTION_COPY );
222 /* We don't create these ones yet because we perhaps won't need them */
223 p_intf->p_sys->p_about = NULL;
224 p_intf->p_sys->p_modules = NULL;
225 p_intf->p_sys->p_fileopen = NULL;
226 p_intf->p_sys->p_disc = NULL;
228 /* Store p_intf to keep an eye on it */
229 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
232 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
235 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_playlist),
240 /* Show the control window */
241 gtk_widget_show( p_intf->p_sys->p_window );
244 /* Sleep to avoid using all CPU - since some interfaces needs to access
245 * keyboard events, a 100ms delay is a good compromise */
246 p_intf->p_sys->i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000,
253 /* launch stored callbacks */
254 if( p_intf->p_sys->pf_gtk_callback != NULL )
256 p_intf->p_sys->pf_gtk_callback();
258 if( p_intf->p_sys->pf_gdk_callback != NULL )
260 p_intf->p_sys->pf_gdk_callback();
265 /* following functions are local */
267 /*****************************************************************************
268 * GtkManage: manage main thread messages
269 *****************************************************************************
270 * In this function, called approx. 10 times a second, we check what the
271 * main program wanted to tell us.
272 *****************************************************************************/
274 static gint GtkManage( gpointer p_data )
276 intf_thread_t *p_intf = (void *)p_data;
278 GtkPlayListManage( p_data );
280 vlc_mutex_lock( &p_intf->p_sys->change_lock );
283 /* If the "display popup" flag has changed */
284 if( p_intf->b_menu_change )
286 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
288 p_intf->p_sys->p_popup = create_intf_popup();
289 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
292 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
293 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
294 p_intf->b_menu_change = 0;
297 /* Update language/chapter menus after user request */
298 if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL &&
299 p_intf->p_sys->b_menus_update )
301 es_descriptor_t * p_audio_es;
302 es_descriptor_t * p_spu_es;
303 GtkWidget * p_menubar_menu;
304 GtkWidget * p_popup_menu;
307 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
308 p_intf->p_sys->p_window ), "menubar_title" ) );
310 GtkTitleMenu( p_intf, p_menubar_menu, on_menubar_title_activate );
312 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
313 p_intf->p_sys->p_window ), "menubar_chapter" ) );
315 GtkChapterMenu( p_intf, p_menubar_menu, on_menubar_chapter_activate );
317 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
318 p_intf->p_sys->p_popup ), "popup_navigation" ) );
320 GtkTitleMenu( p_intf, p_popup_menu, on_popup_navigation_activate );
322 /* look for selected ES */
326 for( i = 0 ; i < p_intf->p_input->stream.i_selected_es_number ; i++ )
328 if( p_intf->p_input->stream.pp_es[i]->b_audio )
330 p_audio_es = p_intf->p_input->stream.pp_es[i];
333 if( p_intf->p_input->stream.pp_es[i]->b_spu )
335 p_spu_es = p_intf->p_input->stream.pp_es[i];
341 /* find audio root menu */
342 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
343 p_intf->p_sys->p_window ), "menubar_audio" ) );
345 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
346 p_intf->p_sys->p_popup ), "popup_audio" ) );
348 GtkLanguageMenus( p_intf, p_menubar_menu, p_audio_es, 1,
349 on_menubar_audio_activate );
350 GtkLanguageMenus( p_intf, p_popup_menu, p_audio_es, 1,
351 on_popup_audio_activate );
353 /* sub picture menus */
355 /* find spu root menu */
356 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
357 p_intf->p_sys->p_window ), "menubar_subpictures" ) );
359 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
360 p_intf->p_sys->p_popup ), "popup_subpictures" ) );
362 GtkLanguageMenus( p_intf, p_menubar_menu, p_spu_es, 2,
363 on_menubar_subpictures_activate );
364 GtkLanguageMenus( p_intf, p_popup_menu, p_spu_es, 2,
365 on_popup_subpictures_activate );
367 /* everything is ready */
368 p_intf->p_sys->b_menus_update = 0;
371 /* Manage the slider */
372 if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL
373 && p_intf->p_sys->b_scale_isfree )
376 GtkAdjustment *p_adj;
378 p_scale = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
379 p_intf->p_sys->p_window ), "hscale" ) );
380 p_adj = gtk_range_get_adjustment ( GTK_RANGE( p_scale ) );
382 /* Update the value */
383 p_adj->value = ( 100. *
384 p_intf->p_input->stream.p_selected_area->i_tell ) /
385 p_intf->p_input->stream.p_selected_area->i_size;
387 /* Gtv does it this way. Why not. */
388 gtk_range_set_adjustment ( GTK_RANGE( p_scale ), p_adj );
389 gtk_range_slider_update ( GTK_RANGE( p_scale ) );
390 gtk_range_clear_background ( GTK_RANGE( p_scale ) );
391 gtk_range_draw_background ( GTK_RANGE( p_scale ) );
395 /* Manage core vlc functions through the callback */
396 p_intf->pf_manage( p_intf );
400 /* Make sure we won't be called again */
401 gtk_timeout_remove( p_intf->p_sys->i_timeout );
403 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
405 /* Prepare to die, young Skywalker */
410 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
415 /*****************************************************************************
416 * GtkMenuRadioItem: give a menu item adapted to language/title selection,
417 * ie the menu item is a radio button.
418 *****************************************************************************/
419 static GtkWidget * GtkMenuRadioItem( GtkWidget * p_menu,
420 GSList ** p_button_group,
427 GtkWidget * p_button;
431 gtk_radio_button_new_with_label( *p_button_group, psz_name );
433 /* add button to group */
435 gtk_radio_button_group( GTK_RADIO_BUTTON( p_button ) );
437 /* prepare button for display */
438 gtk_widget_show( p_button );
440 /* create menu item to store button */
441 p_item = gtk_menu_item_new();
443 /* put button inside item */
444 gtk_container_add( GTK_CONTAINER( p_item ), p_button );
446 /* add item to menu */
447 gtk_menu_append( GTK_MENU( p_menu ), p_item );
449 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
450 GTK_SIGNAL_FUNC( on_audio_toggle ),
454 /* prepare item for display */
455 gtk_widget_show( p_item );
457 /* is it the selected item ? */
460 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( p_button ), TRUE );
463 p_item = gtk_menu_item_new_with_label( psz_name );
464 gtk_menu_append( GTK_MENU( p_menu ), p_item );
465 gtk_widget_show( p_item );
471 /*****************************************************************************
472 * GtkLanguageMenus: update interactive menus of the interface
473 *****************************************************************************
474 * Sets up menus with information from input:
477 * Warning: since this function is designed to be called by management
478 * function, the interface lock has to be taken
479 *****************************************************************************/
480 static gint GtkLanguageMenus( gpointer p_data,
482 es_descriptor_t * p_es,
484 void(*pf_activate )( GtkMenuItem *, gpointer ) )
486 intf_thread_t * p_intf;
488 GtkWidget * p_separator;
490 GSList * p_button_group;
500 p_intf = (intf_thread_t *)p_data;
502 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
504 b_audio = ( i_type == 1 );
505 p_button_group = NULL;
507 /* menu container for audio */
508 p_menu = gtk_menu_new();
510 /* create a set of language buttons and append them to the container */
511 b_active = ( p_es == NULL ) ? 1 : 0;
514 p_item = GtkMenuRadioItem( p_menu, &p_button_group, b_active, psz_name );
516 /* setup signal hanling */
517 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
518 GTK_SIGNAL_FUNC ( pf_activate ), NULL );
520 p_separator = gtk_menu_item_new();
521 gtk_widget_show( p_separator );
522 gtk_menu_append( GTK_MENU( p_menu ), p_separator );
523 gtk_widget_set_sensitive( p_separator, FALSE );
525 for( i = 0 ; i < p_intf->p_input->stream.i_es_number ; i++ )
528 b_audio = ( i_type == 1 ) && p_intf->p_input->stream.pp_es[i]->b_audio;
529 b_spu = ( i_type == 2 ) && p_intf->p_input->stream.pp_es[i]->b_spu;
531 if( b_audio || b_spu )
533 b_active = ( p_es == p_intf->p_input->stream.pp_es[i] ) ? 1 : 0;
534 psz_name = p_intf->p_input->stream.pp_es[i]->psz_desc;
536 p_item = GtkMenuRadioItem( p_menu, &p_button_group,
537 b_active, psz_name );
539 /* setup signal hanling */
540 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
541 GTK_SIGNAL_FUNC( pf_activate ),
542 (gpointer)( p_intf->p_input->stream.pp_es[i] ) );
547 /* link the new menu to the menubar item */
548 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
550 /* be sure that menu is sensitive */
551 gtk_widget_set_sensitive( p_root, TRUE );
553 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
558 /*****************************************************************************
559 * GtkChapterMenu: generate chapter menu for current title
560 *****************************************************************************/
561 static gint GtkChapterMenu( gpointer p_data, GtkWidget * p_chapter,
562 void(*pf_activate )( GtkMenuItem *, gpointer ) )
564 intf_thread_t * p_intf;
566 GtkWidget * p_chapter_menu;
568 GSList * p_chapter_button_group;
574 p_intf = (intf_thread_t*)p_data;
576 i_title = p_intf->p_input->stream.p_selected_area->i_id;
577 p_chapter_menu = gtk_menu_new();
580 i_chapter < p_intf->p_input->stream.pp_areas[i_title]->i_part_nb ;
583 b_active = ( p_intf->p_input->stream.pp_areas[i_title]->i_part
584 == i_chapter + 1 ) ? 1 : 0;
586 sprintf( psz_name, "Chapter %d", i_chapter + 1 );
588 p_item = GtkMenuRadioItem( p_chapter_menu, &p_chapter_button_group,
589 b_active, psz_name );
590 /* setup signal hanling */
591 gtk_signal_connect( GTK_OBJECT( p_item ),
593 GTK_SIGNAL_FUNC( pf_activate ),
594 (gpointer)(i_chapter + 1) );
597 /* link the new menu to the title menu item */
598 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter ),
601 /* be sure that chapter menu is sensitive */
602 gtk_widget_set_sensitive( p_chapter, TRUE );
607 /*****************************************************************************
608 * GtkTitleMenu: sets menus for titles and chapters selection
609 *****************************************************************************
610 * Generates two type of menus:
611 * -simple list of titles
612 * -cascaded lists of chapters for each title
613 *****************************************************************************/
614 static gint GtkTitleMenu( gpointer p_data,
615 GtkWidget * p_navigation,
616 void(*pf_activate )( GtkMenuItem *, gpointer ) )
618 intf_thread_t * p_intf;
620 GtkWidget * p_title_menu;
621 GtkWidget * p_title_item;
622 GtkWidget * p_chapter_menu;
624 GSList * p_title_button_group;
625 GSList * p_chapter_button_group;
631 p_intf = (intf_thread_t*)p_data;
633 p_title_menu = gtk_menu_new();
634 p_title_button_group = NULL;
635 p_chapter_button_group = NULL;
639 i_title < p_intf->p_input->stream.i_area_nb ;
642 b_active = ( p_intf->p_input->stream.pp_areas[i_title] ==
643 p_intf->p_input->stream.p_selected_area ) ? 1 : 0;
644 sprintf( psz_name, "Title %d", i_title );
646 p_title_item = GtkMenuRadioItem( p_title_menu, &p_title_button_group,
647 b_active, psz_name );
649 if( pf_activate == on_menubar_title_activate )
651 /* setup signal hanling */
652 gtk_signal_connect( GTK_OBJECT( p_title_item ),
654 GTK_SIGNAL_FUNC( pf_activate ),
655 (gpointer)(p_intf->p_input->stream.pp_areas[i_title]) );
659 p_chapter_menu = gtk_menu_new();
663 p_intf->p_input->stream.pp_areas[i_title]->i_part_nb ;
666 b_active = ( p_intf->p_input->stream.pp_areas[i_title]->i_part
667 == i_chapter + 1 ) ? 1 : 0;
669 sprintf( psz_name, "Chapter %d", i_chapter + 1 );
671 p_item = GtkMenuRadioItem( p_chapter_menu,
672 &p_chapter_button_group,
673 b_active, psz_name );
675 /* setup signal hanling */
676 gtk_signal_connect( GTK_OBJECT( p_item ),
678 GTK_SIGNAL_FUNC( pf_activate ),
679 (gpointer)( ( i_title * 100 ) + ( i_chapter + 1) ) );
682 /* link the new menu to the title menu item */
683 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_item ),
687 /* be sure that chapter menu is sensitive */
688 gtk_widget_set_sensitive( p_title_menu, TRUE );
692 /* link the new menu to the menubar audio item */
693 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_navigation ), p_title_menu );
695 /* be sure that audio menu is sensitive */
696 gtk_widget_set_sensitive( p_navigation, TRUE );