1 /*****************************************************************************
2 * intf_gtk.c: Gtk+ interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: intf_gtk.c,v 1.4 2001/03/07 10:31:10 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"
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) );
77 /*****************************************************************************
78 * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
79 *****************************************************************************
80 * gtk_init() makes several calls to g_atexit() which calls atexit() to
81 * register tidying callbacks to be called at program exit. Since the Gtk+
82 * plugin is likely to be unloaded at program exit, we have to export this
83 * symbol to intercept the g_atexit() calls. Talk about crude hack.
84 *****************************************************************************/
85 void g_atexit( GVoidFunc func )
87 intf_thread_t *p_intf = p_main->p_intf;
89 if( p_intf->p_sys->pf_gdk_callback == NULL )
91 p_intf->p_sys->pf_gdk_callback = func;
93 else if( p_intf->p_sys->pf_gtk_callback == NULL )
95 p_intf->p_sys->pf_gtk_callback = func;
97 /* else nothing, but we could do something here */
101 /*****************************************************************************
102 * Functions exported as capabilities. They are declared as static so that
103 * we don't pollute the namespace too much.
104 *****************************************************************************/
105 void _M( intf_getfunctions )( function_list_t * p_function_list )
107 p_function_list->pf_probe = intf_Probe;
108 p_function_list->functions.intf.pf_open = intf_Open;
109 p_function_list->functions.intf.pf_close = intf_Close;
110 p_function_list->functions.intf.pf_run = intf_Run;
113 /*****************************************************************************
114 * intf_Probe: probe the interface and return a score
115 *****************************************************************************
116 * This function tries to initialize Gtk+ and returns a score to the
117 * plugin manager so that it can select the best plugin.
118 *****************************************************************************/
119 static int intf_Probe( probedata_t *p_data )
121 if( TestMethod( INTF_METHOD_VAR, "gtk" ) )
129 /*****************************************************************************
130 * intf_Open: initialize and create window
131 *****************************************************************************/
132 static int intf_Open( intf_thread_t *p_intf )
134 /* Allocate instance and initialize some members */
135 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
136 if( p_intf->p_sys == NULL )
138 intf_ErrMsg("error: %s", strerror(ENOMEM));
142 /* Initialize Gtk+ thread */
143 p_intf->p_sys->b_popup_changed = 0;
144 p_intf->p_sys->b_window_changed = 0;
145 p_intf->p_sys->b_playlist_changed = 0;
147 p_intf->p_sys->b_menus_update = 1;
148 p_intf->p_sys->b_scale_isfree = 1;
150 p_intf->p_sys->pf_gtk_callback = NULL;
151 p_intf->p_sys->pf_gdk_callback = NULL;
153 /* Initialize lock */
154 vlc_mutex_init( &p_intf->p_sys->change_lock );
159 /*****************************************************************************
160 * intf_Close: destroy interface window
161 *****************************************************************************/
162 static void intf_Close( intf_thread_t *p_intf )
165 vlc_mutex_destroy( &p_intf->p_sys->change_lock );
167 /* Destroy structure */
168 free( p_intf->p_sys );
171 /*****************************************************************************
172 * intf_Run: Gtk+ thread
173 *****************************************************************************
174 * this part of the interface is in a separate thread so that we can call
175 * gtk_main() from within it without annoying the rest of the program.
176 * XXX: the approach may look kludgy, and probably is, but I could not find
177 * a better way to dynamically load a Gtk+ interface at runtime.
178 *****************************************************************************/
179 static void intf_Run( intf_thread_t *p_intf )
181 /* gtk_init needs to know the command line. We don't care, so we
182 * give it an empty one */
183 char *p_args[] = { "" };
184 char **pp_args = p_args;
187 /* The data types we are allowed to receive */
188 static GtkTargetEntry target_table[] =
190 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
191 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
194 /* Initialize Gtk+ */
195 gtk_init( &i_args, &pp_args );
197 /* Create some useful widgets that will certainly be used */
198 p_intf->p_sys->p_window = create_intf_window( );
199 p_intf->p_sys->p_popup = create_intf_popup( );
201 /* Set the title of the main window */
202 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
203 VOUT_TITLE " (Gtk+ interface)");
205 /* Accept file drops on the main window */
206 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
207 GTK_DEST_DEFAULT_ALL, target_table,
208 1, GDK_ACTION_COPY );
210 /* We don't create these ones yet because we perhaps won't need them */
211 p_intf->p_sys->p_about = NULL;
212 p_intf->p_sys->p_playlist = NULL;
213 p_intf->p_sys->p_modules = NULL;
214 p_intf->p_sys->p_fileopen = NULL;
215 p_intf->p_sys->p_disc = NULL;
217 /* Store p_intf to keep an eye on it */
218 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
221 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
224 /* Show the control window */
225 gtk_widget_show( p_intf->p_sys->p_window );
228 /* Sleep to avoid using all CPU - since some interfaces needs to access
229 * keyboard events, a 100ms delay is a good compromise */
230 p_intf->p_sys->i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000,
237 /* launch stored callbacks */
238 if( p_intf->p_sys->pf_gtk_callback != NULL )
240 p_intf->p_sys->pf_gtk_callback();
242 if( p_intf->p_sys->pf_gdk_callback != NULL )
244 p_intf->p_sys->pf_gdk_callback();
249 /* following functions are local */
251 /*****************************************************************************
252 * GtkManage: manage main thread messages
253 *****************************************************************************
254 * In this function, called approx. 10 times a second, we check what the
255 * main program wanted to tell us.
256 *****************************************************************************/
257 static gint GtkManage( gpointer p_data )
259 intf_thread_t *p_intf = (void *)p_data;
261 vlc_mutex_lock( &p_intf->p_sys->change_lock );
263 /* If the "display popup" flag has changed */
264 if( p_intf->b_menu_change )
266 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
268 p_intf->p_sys->p_popup = create_intf_popup();
269 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
272 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
273 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
274 p_intf->b_menu_change = 0;
277 /* Update language/chapter menus after user request */
278 if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL &&
279 p_intf->p_sys->b_menus_update )
281 es_descriptor_t * p_audio_es;
282 es_descriptor_t * p_spu_es;
283 GtkWidget * p_menubar_menu;
284 GtkWidget * p_popup_menu;
287 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
288 p_intf->p_sys->p_window ), "menubar_title" ) );
290 GtkTitleMenu( p_intf, p_menubar_menu, on_menubar_title_activate );
292 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
293 p_intf->p_sys->p_window ), "menubar_chapter" ) );
295 GtkChapterMenu( p_intf, p_menubar_menu, on_menubar_chapter_activate );
297 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
298 p_intf->p_sys->p_popup ), "popup_navigation" ) );
300 GtkTitleMenu( p_intf, p_popup_menu, on_popup_navigation_activate );
302 /* look for selected ES */
306 for( i = 0 ; i < p_intf->p_input->stream.i_selected_es_number ; i++ )
308 if( p_intf->p_input->stream.pp_es[i]->b_audio )
310 p_audio_es = p_intf->p_input->stream.pp_es[i];
313 if( p_intf->p_input->stream.pp_es[i]->b_spu )
315 p_spu_es = p_intf->p_input->stream.pp_es[i];
321 /* find audio root menu */
322 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
323 p_intf->p_sys->p_window ), "menubar_audio" ) );
325 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
326 p_intf->p_sys->p_popup ), "popup_audio" ) );
328 GtkLanguageMenus( p_intf, p_menubar_menu, p_audio_es, 1,
329 on_menubar_audio_activate );
330 GtkLanguageMenus( p_intf, p_popup_menu, p_audio_es, 1,
331 on_popup_audio_activate );
333 /* sub picture menus */
335 /* find spu root menu */
336 p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
337 p_intf->p_sys->p_window ), "menubar_subpictures" ) );
339 p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
340 p_intf->p_sys->p_popup ), "popup_subpictures" ) );
342 GtkLanguageMenus( p_intf, p_menubar_menu, p_spu_es, 2,
343 on_menubar_subpictures_activate );
344 GtkLanguageMenus( p_intf, p_popup_menu, p_spu_es, 2,
345 on_popup_subpictures_activate );
347 /* everything is ready */
348 p_intf->p_sys->b_menus_update = 0;
351 /* Manage the slider */
352 if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL
353 && p_intf->p_sys->b_scale_isfree )
356 GtkAdjustment *p_adj;
358 p_scale = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
359 p_intf->p_sys->p_window ), "hscale" ) );
360 p_adj = gtk_range_get_adjustment ( GTK_RANGE( p_scale ) );
362 /* Update the value */
363 p_adj->value = ( 100. *
364 p_intf->p_input->stream.p_selected_area->i_tell ) /
365 p_intf->p_input->stream.p_selected_area->i_size;
367 /* Gtv does it this way. Why not. */
368 gtk_range_set_adjustment ( GTK_RANGE( p_scale ), p_adj );
369 gtk_range_slider_update ( GTK_RANGE( p_scale ) );
370 gtk_range_clear_background ( GTK_RANGE( p_scale ) );
371 gtk_range_draw_background ( GTK_RANGE( p_scale ) );
374 /* Manage core vlc functions through the callback */
375 p_intf->pf_manage( p_intf );
379 /* Make sure we won't be called again */
380 gtk_timeout_remove( p_intf->p_sys->i_timeout );
382 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
384 /* Prepare to die, young Skywalker */
389 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
394 /*****************************************************************************
395 * GtkMenuRadioItem: give a menu item adapted to language/title selection,
396 * ie the menu item is a radio button.
397 *****************************************************************************/
398 static GtkWidget * GtkMenuRadioItem( GtkWidget * p_menu,
399 GSList ** p_button_group,
406 GtkWidget * p_button;
410 gtk_radio_button_new_with_label( *p_button_group, psz_name );
412 /* add button to group */
414 gtk_radio_button_group( GTK_RADIO_BUTTON( p_button ) );
416 /* prepare button for display */
417 gtk_widget_show( p_button );
419 /* create menu item to store button */
420 p_item = gtk_menu_item_new();
422 /* put button inside item */
423 gtk_container_add( GTK_CONTAINER( p_item ), p_button );
425 /* add item to menu */
426 gtk_menu_append( GTK_MENU( p_menu ), p_item );
428 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
429 GTK_SIGNAL_FUNC( on_audio_toggle ),
433 /* prepare item for display */
434 gtk_widget_show( p_item );
436 /* is it the selected item ? */
439 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( p_button ), TRUE );
442 p_item = gtk_menu_item_new_with_label( psz_name );
443 gtk_menu_append( GTK_MENU( p_menu ), p_item );
444 gtk_widget_show( p_item );
450 /*****************************************************************************
451 * GtkLanguageMenus: update interactive menus of the interface
452 *****************************************************************************
453 * Sets up menus with information from input:
456 * Warning: since this function is designed to be called by management
457 * function, the interface lock has to be taken
458 *****************************************************************************/
459 static gint GtkLanguageMenus( gpointer p_data,
461 es_descriptor_t * p_es,
463 void(*pf_activate )( GtkMenuItem *, gpointer ) )
465 intf_thread_t * p_intf;
467 GtkWidget * p_separator;
469 GSList * p_button_group;
479 p_intf = (intf_thread_t *)p_data;
481 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
483 b_audio = ( i_type == 1 );
484 p_button_group = NULL;
486 /* menu container for audio */
487 p_menu = gtk_menu_new();
489 /* create a set of language buttons and append them to the container */
490 b_active = ( p_es == NULL ) ? 1 : 0;
493 p_item = GtkMenuRadioItem( p_menu, &p_button_group, b_active, psz_name );
495 /* setup signal hanling */
496 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
497 GTK_SIGNAL_FUNC ( pf_activate ), NULL );
499 p_separator = gtk_menu_item_new();
500 gtk_widget_show( p_separator );
501 gtk_menu_append( GTK_MENU( p_menu ), p_separator );
502 gtk_widget_set_sensitive( p_separator, FALSE );
504 for( i = 0 ; i < p_intf->p_input->stream.i_es_number ; i++ )
507 b_audio = ( i_type == 1 ) && p_intf->p_input->stream.pp_es[i]->b_audio;
508 b_spu = ( i_type == 2 ) && p_intf->p_input->stream.pp_es[i]->b_spu;
510 if( b_audio || b_spu )
512 b_active = ( p_es == p_intf->p_input->stream.pp_es[i] ) ? 1 : 0;
513 psz_name = p_intf->p_input->stream.pp_es[i]->psz_desc;
515 p_item = GtkMenuRadioItem( p_menu, &p_button_group,
516 b_active, psz_name );
518 /* setup signal hanling */
519 gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
520 GTK_SIGNAL_FUNC( pf_activate ),
521 (gpointer)( p_intf->p_input->stream.pp_es[i] ) );
526 /* link the new menu to the menubar item */
527 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
529 /* be sure that menu is sensitive */
530 gtk_widget_set_sensitive( p_root, TRUE );
532 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
537 /*****************************************************************************
538 * GtkChapterMenu: generate chapter menu foir current title
539 *****************************************************************************/
540 static gint GtkChapterMenu( gpointer p_data, GtkWidget * p_chapter,
541 void(*pf_activate )( GtkMenuItem *, gpointer ) )
543 intf_thread_t * p_intf;
545 GtkWidget * p_chapter_menu;
547 GSList * p_chapter_button_group;
553 p_intf = (intf_thread_t*)p_data;
555 i_title = p_intf->p_input->stream.p_selected_area->i_id;
556 p_chapter_menu = gtk_menu_new();
559 i_chapter < p_intf->p_input->stream.pp_areas[i_title]->i_part_nb ;
562 b_active = ( p_intf->p_input->stream.pp_areas[i_title]->i_part
563 == i_chapter + 1 ) ? 1 : 0;
565 sprintf( psz_name, "Chapter %d", i_chapter + 1 );
567 p_item = GtkMenuRadioItem( p_chapter_menu, &p_chapter_button_group,
568 b_active, psz_name );
569 /* setup signal hanling */
570 gtk_signal_connect( GTK_OBJECT( p_item ),
572 GTK_SIGNAL_FUNC( pf_activate ),
573 (gpointer)(i_chapter + 1) );
576 /* link the new menu to the title menu item */
577 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter ),
580 /* be sure that chapter menu is sensitive */
581 gtk_widget_set_sensitive( p_chapter, TRUE );
586 /*****************************************************************************
587 * GtkTitleMenu: sets menus for titles and chapters selection
589 * Generates two type of menus:
590 * -simple list of titles
591 * -cascaded lists of chapters for each title
592 *****************************************************************************/
593 static gint GtkTitleMenu( gpointer p_data,
594 GtkWidget * p_navigation,
595 void(*pf_activate )( GtkMenuItem *, gpointer ) )
597 intf_thread_t * p_intf;
599 GtkWidget * p_title_menu;
600 GtkWidget * p_title_item;
601 GtkWidget * p_chapter_menu;
603 GSList * p_title_button_group;
604 GSList * p_chapter_button_group;
610 p_intf = (intf_thread_t*)p_data;
612 p_title_menu = gtk_menu_new();
613 p_title_button_group = NULL;
614 p_chapter_button_group = NULL;
618 i_title < p_intf->p_input->stream.i_area_nb ;
621 b_active = ( p_intf->p_input->stream.pp_areas[i_title] ==
622 p_intf->p_input->stream.p_selected_area ) ? 1 : 0;
623 sprintf( psz_name, "Title %d", i_title );
625 p_title_item = GtkMenuRadioItem( p_title_menu, &p_title_button_group,
626 b_active, psz_name );
628 if( pf_activate == on_menubar_title_activate )
630 /* setup signal hanling */
631 gtk_signal_connect( GTK_OBJECT( p_title_item ),
633 GTK_SIGNAL_FUNC( pf_activate ),
634 (gpointer)(p_intf->p_input->stream.pp_areas[i_title]) );
638 p_chapter_menu = gtk_menu_new();
642 p_intf->p_input->stream.pp_areas[i_title]->i_part_nb ;
645 b_active = ( p_intf->p_input->stream.pp_areas[i_title]->i_part
646 == i_chapter + 1 ) ? 1 : 0;
648 sprintf( psz_name, "Chapter %d", i_chapter + 1 );
650 p_item = GtkMenuRadioItem( p_chapter_menu,
651 &p_chapter_button_group,
652 b_active, psz_name );
654 /* setup signal hanling */
655 gtk_signal_connect( GTK_OBJECT( p_item ),
657 GTK_SIGNAL_FUNC( pf_activate ),
658 (gpointer)( ( i_title * 100 ) + ( i_chapter + 1) ) );
661 /* link the new menu to the title menu item */
662 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_item ),
666 /* be sure that chapter menu is sensitive */
667 gtk_widget_set_sensitive( p_title_menu, TRUE );
671 /* link the new menu to the menubar audio item */
672 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_navigation ), p_title_menu );
674 /* be sure that audio menu is sensitive */
675 gtk_widget_set_sensitive( p_navigation, TRUE );