1 /*****************************************************************************
2 * gtk.c : Gtk+ plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <errno.h> /* ENOMEM */
29 #include <string.h> /* strerror() */
37 #include "gtk_callbacks.h"
38 #include "gtk_interface.h"
39 #include "gtk_support.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t * );
49 static void Close ( vlc_object_t * );
51 static void Run ( intf_thread_t * );
52 static int Manage ( intf_thread_t * );
54 /*****************************************************************************
56 *****************************************************************************/
57 #define TOOLTIPS_TEXT N_("Show tooltips")
58 #define TOOLTIPS_LONGTEXT N_("Show tooltips for configuration options.")
60 #define PREFS_MAXH_TEXT N_("Maximum height for the configuration windows")
61 #define PREFS_MAXH_LONGTEXT N_( \
62 "You can set the maximum height that the configuration windows in the " \
63 "preferences menu will occupy.")
65 #define PATH_TEXT N_("Interface default search path")
66 #define PATH_LONGTEXT N_( \
67 "This option allows you to set the default path that the interface will " \
68 "open when looking for a file.")
74 int i = getenv( "DISPLAY" ) == NULL ? 10 : 90;
76 set_description( _("Gtk+ interface") );
77 set_category( CAT_INTERFACE );
78 set_subcategory( SUBCAT_INTERFACE_GENERAL );
80 add_bool( "gtk-tooltips", 1, E_(GtkHideTooltips),
81 TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT, VLC_FALSE );
82 add_integer( "gtk-prefs-maxh", 480, NULL,
83 PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT, VLC_TRUE );
84 add_directory( "gtk-search-path", NULL, NULL, PATH_TEXT,
85 PATH_LONGTEXT, VLC_TRUE );
87 set_capability( "interface", i );
88 set_callbacks( Open, Close );
89 add_shortcut( "gtk" );
90 set_program( "gvlc" );
93 /*****************************************************************************
94 * Open: initialize and create window
95 *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
98 intf_thread_t *p_intf = (intf_thread_t *)p_this;
100 /* Allocate instance and initialize some members */
101 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
102 if( p_intf->p_sys == NULL )
104 msg_Err( p_intf, "out of memory" );
109 p_intf->p_sys->p_gtk_main =
110 module_Need( p_this, "gui-helper", "gtk", VLC_TRUE );
111 if( p_intf->p_sys->p_gtk_main == NULL )
113 free( p_intf->p_sys );
118 p_intf->pf_run = Run;
120 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
122 /* Initialize Gtk+ thread */
123 p_intf->p_sys->b_playing = VLC_FALSE;
124 p_intf->p_sys->b_deinterlace_update = VLC_FALSE;
126 p_intf->p_sys->b_aout_update = VLC_FALSE;
127 p_intf->p_sys->b_vout_update = VLC_FALSE;
129 p_intf->p_sys->b_popup_changed = VLC_FALSE;
130 p_intf->p_sys->b_window_changed = VLC_FALSE;
131 p_intf->p_sys->b_playlist_changed = VLC_FALSE;
132 p_intf->p_sys->b_program_update = VLC_FALSE;
133 p_intf->p_sys->b_title_update = VLC_FALSE;
134 p_intf->p_sys->b_chapter_update = VLC_FALSE;
135 p_intf->p_sys->b_spu_update = VLC_FALSE;
136 p_intf->p_sys->b_audio_update = VLC_FALSE;
138 p_intf->p_sys->p_input = NULL;
139 p_intf->p_sys->i_playing = -1;
140 p_intf->p_sys->b_slider_free = VLC_TRUE;
142 p_intf->p_sys->i_part = 0;
143 p_intf->p_sys->b_mute = VLC_FALSE;
148 /*****************************************************************************
149 * Close: destroy interface window
150 *****************************************************************************/
151 static void Close( vlc_object_t *p_this )
153 intf_thread_t *p_intf = (intf_thread_t *)p_this;
155 if( p_intf->p_sys->p_input )
157 vlc_object_release( p_intf->p_sys->p_input );
160 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
163 module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
166 /* Destroy structure */
167 free( p_intf->p_sys );
170 /*****************************************************************************
172 *****************************************************************************
173 * this part of the interface is in a separate thread so that we can call
174 * gtk_main() from within it without annoying the rest of the program.
175 *****************************************************************************/
176 static void Run( intf_thread_t *p_intf )
178 /* The data types we are allowed to receive */
179 static GtkTargetEntry target_table[] =
181 { "STRING", 0, DROP_ACCEPT_STRING },
182 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
183 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
191 /* gtk_init needs to know the command line. We don't care, so we
192 * give it an empty one */
193 char *p_args[] = { "", NULL };
194 char **pp_args = p_args;
198 gtk_init( &i_args, &pp_args );
201 /* Create some useful widgets that will certainly be used */
202 p_intf->p_sys->p_window = create_intf_window();
203 p_intf->p_sys->p_popup = create_intf_popup();
204 p_intf->p_sys->p_playwin = create_intf_playlist();
205 p_intf->p_sys->p_messages = create_intf_messages();
206 p_intf->p_sys->p_tooltips = gtk_tooltips_new();
207 p_intf->p_sys->p_sout = create_intf_sout();
209 /* Set the title of the main window */
210 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
211 VOUT_TITLE " (Gtk+ interface)");
213 /* Accept file drops on the main window */
214 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
215 GTK_DEST_DEFAULT_ALL, target_table,
216 DROP_ACCEPT_END, GDK_ACTION_COPY );
218 /* Accept file drops on the playlist window */
219 gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playwin,
221 GTK_DEST_DEFAULT_ALL, target_table,
222 DROP_ACCEPT_END, GDK_ACTION_COPY );
224 /* Get the slider object */
225 p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
226 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" ) );
228 /* Configure the log window */
229 p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
230 GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
231 gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
232 gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
234 /* Get the interface labels */
235 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
236 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
237 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
238 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
241 /* Connect the date display to the slider */
242 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
243 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
244 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
246 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
247 GTK_SIGNAL_FUNC( E_(GtkDisplayDate) ), NULL );
248 p_intf->p_sys->f_adj_oldvalue = 0;
253 /* We don't create these ones yet because we perhaps won't need them */
254 p_intf->p_sys->p_about = NULL;
255 p_intf->p_sys->p_modules = NULL;
256 p_intf->p_sys->p_open = NULL;
257 p_intf->p_sys->p_jump = NULL;
259 /* Hide tooltips if the option is set */
260 if( !config_GetInt( p_intf, "gtk-tooltips" ) )
262 gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
265 /* Store p_intf to keep an eye on it */
266 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
269 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
271 gtk_object_set_data( GTK_OBJECT( gtk_object_get_data(
272 GTK_OBJECT(p_intf->p_sys->p_popup),
273 "popup_audio" ) ), "p_intf", p_intf );
274 gtk_object_set_data( GTK_OBJECT( gtk_object_get_data(
275 GTK_OBJECT(p_intf->p_sys->p_popup),
276 "popup_video" ) ), "p_intf", p_intf );
278 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
281 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
284 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
286 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
289 psz_sout = config_GetPsz( p_intf, "sout" );
290 p_target = g_string_new( psz_sout ? psz_sout : "" );
291 if( psz_sout ) free( psz_sout );
293 gtk_entry_set_text( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ), "sout_entry_target" ), p_target->str );
294 g_string_free( p_target, TRUE );
296 /* FIXME it's to be sure that only file entry is selected */
297 gtk_toggle_button_set_active( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
298 "sout_access_udp" ), TRUE );
300 gtk_toggle_button_set_active( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
301 "sout_access_file" ), TRUE );
303 /* Show the control window */
304 gtk_widget_show( p_intf->p_sys->p_window );
307 while( !p_intf->b_die )
311 /* Sleep to avoid using all CPU - since some interfaces need to
312 * access keyboard events, a 100ms delay is a good compromise */
314 msleep( INTF_IDLE_SLEEP );
318 /* Sleep to avoid using all CPU - since some interfaces needs to access
319 * keyboard events, a 100ms delay is a good compromise */
320 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, (GtkFunction)Manage,
324 /* Remove the timeout */
325 gtk_timeout_remove( i_dummy );
328 /* Destroy the Tooltips structure */
329 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
330 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_messages) );
331 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_playwin) );
332 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_popup) );
333 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
340 /* following functions are local */
342 /*****************************************************************************
343 * Manage: manage main thread messages
344 *****************************************************************************
345 * In this function, called approx. 10 times a second, we check what the
346 * main program wanted to tell us.
347 *****************************************************************************/
348 static int Manage( intf_thread_t *p_intf )
352 vlc_mutex_lock( &p_intf->change_lock );
354 /* If the "display popup" flag has changed */
355 if( p_intf->b_menu_change )
357 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
359 p_intf->p_sys->p_popup = create_intf_popup();
360 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
363 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
364 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
365 p_intf->b_menu_change = 0;
368 /* Update the log window */
369 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
370 i_stop = *p_intf->p_sys->p_sub->pi_stop;
371 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
373 if( p_intf->p_sys->p_sub->i_start != i_stop )
375 static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
376 static GdkColor gray = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
377 static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
378 static GdkColor red = { 0, 0xffff, 0x6666, 0x6666 };
380 static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
382 static GdkColor * pp_color[4] = { &white, &red, &yellow, &gray };
384 for( i_start = p_intf->p_sys->p_sub->i_start;
386 i_start = (i_start+1) % VLC_MSG_QSIZE )
388 /* Append all messages to log window */
389 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
390 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
392 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
393 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
396 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
397 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
398 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
400 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
404 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
405 p_intf->p_sys->p_sub->i_start = i_start;
406 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
408 /* If the messages list becomes too big, just clean half of it. */
409 if( gtk_text_get_length( p_intf->p_sys->p_messages_text ) >
410 VLC_MSG_QSIZE * 1000 )
412 gtk_text_set_point( p_intf->p_sys->p_messages_text, 0 );
413 gtk_text_forward_delete( p_intf->p_sys->p_messages_text,
414 gtk_text_get_length( p_intf->p_sys->p_messages_text ) / 2 );
417 gtk_text_set_point( p_intf->p_sys->p_messages_text,
418 gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
421 /* Update the playlist */
422 GtkPlayListManage( p_intf );
424 /* Update the input */
425 if( p_intf->p_sys->p_input == NULL )
427 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
430 else if( p_intf->p_sys->p_input->b_dead )
432 vlc_object_release( p_intf->p_sys->p_input );
433 p_intf->p_sys->p_input = NULL;
436 if( p_intf->p_sys->p_input )
438 input_thread_t *p_input = p_intf->p_sys->p_input;
439 aout_instance_t *p_aout = NULL;
440 vout_thread_t *p_vout = NULL;
441 vlc_bool_t b_need_menus = VLC_FALSE;
443 vlc_mutex_lock( &p_input->stream.stream_lock );
445 if( !p_input->b_die )
447 /* New input or stream map change */
448 if( p_input->stream.b_changed )
450 E_(GtkModeManage)( p_intf );
451 GtkSetupMenus( p_intf );
452 p_intf->p_sys->b_playing = 1;
455 /* Manage the slider */
456 if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
458 float newvalue = p_intf->p_sys->p_adj->value;
460 #define p_area p_input->stream.p_selected_area
461 /* If the user hasn't touched the slider since the last time,
462 * then the input can safely change it */
463 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
465 /* Update the value */
466 p_intf->p_sys->p_adj->value =
467 p_intf->p_sys->f_adj_oldvalue =
468 ( 100. * p_area->i_tell ) / p_area->i_size;
470 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
473 /* Otherwise, send message to the input if the user has
474 * finished dragging the slider.
475 * Beware, the hack below is needed by the dvdplay plugin! */
476 else if( p_intf->p_sys->b_slider_free
477 /* hack -> */ && (p_intf->p_sys->f_adj_oldvalue < 100.) )
479 if( newvalue >= 0. && newvalue < 100. )
481 double f_fpos = (double)newvalue / 100.0;
483 /* release the lock to be able to seek */
484 vlc_mutex_unlock( &p_input->stream.stream_lock );
485 var_SetFloat( p_input, "position", f_fpos );
486 vlc_mutex_lock( &p_input->stream.stream_lock );
489 /* Update the old value */
490 p_intf->p_sys->f_adj_oldvalue = newvalue;
495 if( p_intf->p_sys->i_part !=
496 p_input->stream.p_selected_area->i_part )
498 p_intf->p_sys->b_chapter_update = 1;
499 b_need_menus = VLC_TRUE;
502 /* Does the audio output require to update the menus ? */
503 p_aout = (aout_instance_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
508 if( var_Get( (vlc_object_t *)p_aout, "intf-change", &val ) >= 0
511 p_intf->p_sys->b_aout_update = 1;
515 vlc_object_release( (vlc_object_t *)p_aout );
518 /* Does the video output require to update the menus ? */
519 p_vout = (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
524 if( var_Get( (vlc_object_t *)p_vout, "intf-change", &val ) >= 0
527 p_intf->p_sys->b_vout_update = 1;
531 vlc_object_release( (vlc_object_t *)p_vout );
535 GtkSetupMenus( p_intf );
539 vlc_mutex_unlock( &p_input->stream.stream_lock );
541 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
543 E_(GtkModeManage)( p_intf );
544 p_intf->p_sys->b_playing = 0;
547 #ifndef NEED_GTK_MAIN
550 vlc_mutex_unlock( &p_intf->change_lock );
552 /* Prepare to die, young Skywalker */
559 vlc_mutex_unlock( &p_intf->change_lock );