1 /*****************************************************************************
2 * intf_gtk.c: Gtk+ interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: intf_gtk.c,v 1.8 2001/03/08 21:47:02 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( );
208 /* Set the title of the main window */
209 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
210 VOUT_TITLE " (Gtk+ interface)");
212 /* Accept file drops on the main window */
213 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
214 GTK_DEST_DEFAULT_ALL, target_table,
215 1, GDK_ACTION_COPY );
217 /* Accept file drops on the playlist window */
218 temp = lookup_widget(p_intf->p_sys->p_playlist, "playlist_clist");
221 gtk_drag_dest_set( GTK_WIDGET( temp ),
222 GTK_DEST_DEFAULT_ALL, target_table,
223 1, GDK_ACTION_COPY );
225 /* We don't create these ones yet because we perhaps won't need them */
226 p_intf->p_sys->p_about = NULL;
227 p_intf->p_sys->p_modules = NULL;
228 p_intf->p_sys->p_fileopen = NULL;
229 p_intf->p_sys->p_disc = NULL;
231 /* Store p_intf to keep an eye on it */
232 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
235 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
238 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_playlist),
243 /* Show the control window */
244 gtk_widget_show( p_intf->p_sys->p_window );
247 /* Sleep to avoid using all CPU - since some interfaces needs to access
248 * keyboard events, a 100ms delay is a good compromise */
249 p_intf->p_sys->i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000,
256 /* launch stored callbacks */
257 if( p_intf->p_sys->pf_gtk_callback != NULL )
259 p_intf->p_sys->pf_gtk_callback();
261 if( p_intf->p_sys->pf_gdk_callback != NULL )
263 p_intf->p_sys->pf_gdk_callback();
268 /* following functions are local */
270 /*****************************************************************************
271 * GtkManage: manage main thread messages
272 *****************************************************************************
273 * In this function, called approx. 10 times a second, we check what the
274 * main program wanted to tell us.
275 *****************************************************************************/
277 static gint GtkManage( gpointer p_data )
279 intf_thread_t *p_intf = (void *)p_data;
281 GtkPlayListManage( p_data );
283 vlc_mutex_lock( &p_intf->p_sys->change_lock );
286 /* If the "display popup" flag has changed */
287 if( p_intf->b_menu_change )
289 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
291 p_intf->p_sys->p_popup = create_intf_popup();
292 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
295 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
296 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
297 p_intf->b_menu_change = 0;
300 /* Update language/chapter menus after user request */
301 if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL &&
302 p_intf->p_sys->b_menus_update )
304 es_descriptor_t * p_audio_es;
305 es_descriptor_t * p_spu_es;
306 GtkWidget * p_menubar_menu;
307 GtkWidget * p_popup_menu;
310 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
311 p_intf->p_sys->p_window ), "menubar_title" ) );
313 GtkTitleMenu( p_intf, p_menubar_menu, on_menubar_title_activate );
315 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
316 p_intf->p_sys->p_window ), "menubar_chapter" ) );
318 GtkChapterMenu( p_intf, p_menubar_menu, on_menubar_chapter_activate );
320 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
321 p_intf->p_sys->p_popup ), "popup_navigation" ) );
323 GtkTitleMenu( p_intf, p_popup_menu, on_popup_navigation_activate );
325 /* look for selected ES */
329 for( i = 0 ; i < p_intf->p_input->stream.i_selected_es_number ; i++ )
331 if( p_intf->p_input->stream.pp_es[i]->b_audio )
333 p_audio_es = p_intf->p_input->stream.pp_es[i];
336 if( p_intf->p_input->stream.pp_es[i]->b_spu )
338 p_spu_es = p_intf->p_input->stream.pp_es[i];
344 /* find audio root menu */
345 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
346 p_intf->p_sys->p_window ), "menubar_audio" ) );
348 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
349 p_intf->p_sys->p_popup ), "popup_audio" ) );
351 GtkLanguageMenus( p_intf, p_menubar_menu, p_audio_es, 1,
352 on_menubar_audio_activate );
353 GtkLanguageMenus( p_intf, p_popup_menu, p_audio_es, 1,
354 on_popup_audio_activate );
356 /* sub picture menus */
358 /* find spu root menu */
359 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
360 p_intf->p_sys->p_window ), "menubar_subpictures" ) );
362 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
363 p_intf->p_sys->p_popup ), "popup_subpictures" ) );
365 GtkLanguageMenus( p_intf, p_menubar_menu, p_spu_es, 2,
366 on_menubar_subpictures_activate );
367 GtkLanguageMenus( p_intf, p_popup_menu, p_spu_es, 2,
368 on_popup_subpictures_activate );
370 /* everything is ready */
371 p_intf->p_sys->b_menus_update = 0;
374 /* Manage the slider */
375 if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL
376 && p_intf->p_sys->b_scale_isfree )
379 GtkAdjustment *p_adj;
381 p_scale = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
382 p_intf->p_sys->p_window ), "hscale" ) );
383 p_adj = gtk_range_get_adjustment ( GTK_RANGE( p_scale ) );
385 /* Update the value */
386 p_adj->value = ( 100. *
387 p_intf->p_input->stream.p_selected_area->i_tell ) /
388 p_intf->p_input->stream.p_selected_area->i_size;
390 /* Gtv does it this way. Why not. */
391 gtk_range_set_adjustment ( GTK_RANGE( p_scale ), p_adj );
392 gtk_range_slider_update ( GTK_RANGE( p_scale ) );
393 gtk_range_clear_background ( GTK_RANGE( p_scale ) );
394 gtk_range_draw_background ( GTK_RANGE( p_scale ) );
398 /* Manage core vlc functions through the callback */
399 p_intf->pf_manage( p_intf );
403 /* Make sure we won't be called again */
404 gtk_timeout_remove( p_intf->p_sys->i_timeout );
406 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
408 /* Prepare to die, young Skywalker */
413 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
418 /*****************************************************************************
419 * GtkMenuRadioItem: give a menu item adapted to language/title selection,
420 * ie the menu item is a radio button.
421 *****************************************************************************/
422 static GtkWidget * GtkMenuRadioItem( GtkWidget * p_menu,
423 GSList ** p_button_group,
430 GtkWidget * p_button;
434 gtk_radio_button_new_with_label( *p_button_group, psz_name );
436 /* add button to group */
438 gtk_radio_button_group( GTK_RADIO_BUTTON( p_button ) );
440 /* prepare button for display */
441 gtk_widget_show( p_button );
443 /* create menu item to store button */
444 p_item = gtk_menu_item_new();
446 /* put button inside item */
447 gtk_container_add( GTK_CONTAINER( p_item ), p_button );
449 /* add item to menu */
450 gtk_menu_append( GTK_MENU( p_menu ), p_item );
452 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
453 GTK_SIGNAL_FUNC( on_audio_toggle ),
457 /* prepare item for display */
458 gtk_widget_show( p_item );
460 /* is it the selected item ? */
463 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( p_button ), TRUE );
466 p_item = gtk_menu_item_new_with_label( psz_name );
467 gtk_menu_append( GTK_MENU( p_menu ), p_item );
468 gtk_widget_show( p_item );
474 /*****************************************************************************
475 * GtkLanguageMenus: update interactive menus of the interface
476 *****************************************************************************
477 * Sets up menus with information from input:
480 * Warning: since this function is designed to be called by management
481 * function, the interface lock has to be taken
482 *****************************************************************************/
483 static gint GtkLanguageMenus( gpointer p_data,
485 es_descriptor_t * p_es,
487 void(*pf_activate )( GtkMenuItem *, gpointer ) )
489 intf_thread_t * p_intf;
491 GtkWidget * p_separator;
493 GSList * p_button_group;
503 p_intf = (intf_thread_t *)p_data;
505 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
507 b_audio = ( i_type == 1 );
508 p_button_group = NULL;
510 /* menu container for audio */
511 p_menu = gtk_menu_new();
513 /* create a set of language buttons and append them to the container */
514 b_active = ( p_es == NULL ) ? 1 : 0;
517 p_item = GtkMenuRadioItem( p_menu, &p_button_group, b_active, psz_name );
519 /* setup signal hanling */
520 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
521 GTK_SIGNAL_FUNC ( pf_activate ), NULL );
523 p_separator = gtk_menu_item_new();
524 gtk_widget_show( p_separator );
525 gtk_menu_append( GTK_MENU( p_menu ), p_separator );
526 gtk_widget_set_sensitive( p_separator, FALSE );
528 for( i = 0 ; i < p_intf->p_input->stream.i_es_number ; i++ )
531 b_audio = ( i_type == 1 ) && p_intf->p_input->stream.pp_es[i]->b_audio;
532 b_spu = ( i_type == 2 ) && p_intf->p_input->stream.pp_es[i]->b_spu;
534 if( b_audio || b_spu )
536 b_active = ( p_es == p_intf->p_input->stream.pp_es[i] ) ? 1 : 0;
537 psz_name = p_intf->p_input->stream.pp_es[i]->psz_desc;
539 p_item = GtkMenuRadioItem( p_menu, &p_button_group,
540 b_active, psz_name );
542 /* setup signal hanling */
543 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
544 GTK_SIGNAL_FUNC( pf_activate ),
545 (gpointer)( p_intf->p_input->stream.pp_es[i] ) );
550 /* link the new menu to the menubar item */
551 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
553 /* be sure that menu is sensitive */
554 gtk_widget_set_sensitive( p_root, TRUE );
556 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
561 /*****************************************************************************
562 * GtkChapterMenu: generate chapter menu for current title
563 *****************************************************************************/
564 static gint GtkChapterMenu( gpointer p_data, GtkWidget * p_chapter,
565 void(*pf_activate )( GtkMenuItem *, gpointer ) )
567 intf_thread_t * p_intf;
569 GtkWidget * p_chapter_menu;
571 GSList * p_chapter_button_group;
577 p_intf = (intf_thread_t*)p_data;
579 i_title = p_intf->p_input->stream.p_selected_area->i_id;
580 p_chapter_menu = gtk_menu_new();
583 i_chapter < p_intf->p_input->stream.pp_areas[i_title]->i_part_nb ;
586 b_active = ( p_intf->p_input->stream.pp_areas[i_title]->i_part
587 == i_chapter + 1 ) ? 1 : 0;
589 sprintf( psz_name, "Chapter %d", i_chapter + 1 );
591 p_item = GtkMenuRadioItem( p_chapter_menu, &p_chapter_button_group,
592 b_active, psz_name );
593 /* setup signal hanling */
594 gtk_signal_connect( GTK_OBJECT( p_item ),
596 GTK_SIGNAL_FUNC( pf_activate ),
597 (gpointer)(i_chapter + 1) );
600 /* link the new menu to the title menu item */
601 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter ),
604 /* be sure that chapter menu is sensitive */
605 gtk_widget_set_sensitive( p_chapter, TRUE );
610 /*****************************************************************************
611 * GtkTitleMenu: sets menus for titles and chapters selection
612 *****************************************************************************
613 * Generates two type of menus:
614 * -simple list of titles
615 * -cascaded lists of chapters for each title
616 *****************************************************************************/
617 static gint GtkTitleMenu( gpointer p_data,
618 GtkWidget * p_navigation,
619 void(*pf_activate )( GtkMenuItem *, gpointer ) )
621 intf_thread_t * p_intf;
623 GtkWidget * p_title_menu;
624 GtkWidget * p_title_item;
625 GtkWidget * p_chapter_menu;
627 GSList * p_title_button_group;
628 GSList * p_chapter_button_group;
634 p_intf = (intf_thread_t*)p_data;
636 p_title_menu = gtk_menu_new();
637 p_title_button_group = NULL;
638 p_chapter_button_group = NULL;
642 i_title < p_intf->p_input->stream.i_area_nb ;
645 b_active = ( p_intf->p_input->stream.pp_areas[i_title] ==
646 p_intf->p_input->stream.p_selected_area ) ? 1 : 0;
647 sprintf( psz_name, "Title %d", i_title );
649 p_title_item = GtkMenuRadioItem( p_title_menu, &p_title_button_group,
650 b_active, psz_name );
652 if( pf_activate == on_menubar_title_activate )
654 /* setup signal hanling */
655 gtk_signal_connect( GTK_OBJECT( p_title_item ),
657 GTK_SIGNAL_FUNC( pf_activate ),
658 (gpointer)(p_intf->p_input->stream.pp_areas[i_title]) );
662 p_chapter_menu = gtk_menu_new();
666 p_intf->p_input->stream.pp_areas[i_title]->i_part_nb ;
669 b_active = ( p_intf->p_input->stream.pp_areas[i_title]->i_part
670 == i_chapter + 1 ) ? 1 : 0;
672 sprintf( psz_name, "Chapter %d", i_chapter + 1 );
674 p_item = GtkMenuRadioItem( p_chapter_menu,
675 &p_chapter_button_group,
676 b_active, psz_name );
678 /* setup signal hanling */
679 gtk_signal_connect( GTK_OBJECT( p_item ),
681 GTK_SIGNAL_FUNC( pf_activate ),
682 (gpointer)( ( i_title * 100 ) + ( i_chapter + 1) ) );
685 /* link the new menu to the title menu item */
686 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_item ),
690 /* be sure that chapter menu is sensitive */
691 gtk_widget_set_sensitive( p_title_menu, TRUE );
695 /* link the new menu to the menubar audio item */
696 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_navigation ), p_title_menu );
698 /* be sure that audio menu is sensitive */
699 gtk_widget_set_sensitive( p_navigation, TRUE );