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") );
78 add_bool( "gtk-tooltips", 1, E_(GtkHideTooltips),
79 TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT, VLC_FALSE );
80 add_integer( "gtk-prefs-maxh", 480, NULL,
81 PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT, VLC_TRUE );
82 add_directory( "gtk-search-path", NULL, NULL, PATH_TEXT,
83 PATH_LONGTEXT, VLC_TRUE );
85 set_capability( "interface", i );
86 set_callbacks( Open, Close );
87 add_shortcut( "gtk" );
88 set_program( "gvlc" );
91 /*****************************************************************************
92 * Open: initialize and create window
93 *****************************************************************************/
94 static int Open( vlc_object_t *p_this )
96 intf_thread_t *p_intf = (intf_thread_t *)p_this;
98 /* Allocate instance and initialize some members */
99 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
100 if( p_intf->p_sys == NULL )
102 msg_Err( p_intf, "out of memory" );
107 p_intf->p_sys->p_gtk_main =
108 module_Need( p_this, "gui-helper", "gtk", VLC_TRUE );
109 if( p_intf->p_sys->p_gtk_main == NULL )
111 free( p_intf->p_sys );
116 p_intf->pf_run = Run;
118 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
120 /* Initialize Gtk+ thread */
121 p_intf->p_sys->b_playing = VLC_FALSE;
122 p_intf->p_sys->b_deinterlace_update = VLC_FALSE;
124 p_intf->p_sys->b_aout_update = VLC_FALSE;
125 p_intf->p_sys->b_vout_update = VLC_FALSE;
127 p_intf->p_sys->b_popup_changed = VLC_FALSE;
128 p_intf->p_sys->b_window_changed = VLC_FALSE;
129 p_intf->p_sys->b_playlist_changed = VLC_FALSE;
130 p_intf->p_sys->b_program_update = VLC_FALSE;
131 p_intf->p_sys->b_title_update = VLC_FALSE;
132 p_intf->p_sys->b_chapter_update = VLC_FALSE;
133 p_intf->p_sys->b_spu_update = VLC_FALSE;
134 p_intf->p_sys->b_audio_update = VLC_FALSE;
136 p_intf->p_sys->p_input = NULL;
137 p_intf->p_sys->i_playing = -1;
138 p_intf->p_sys->b_slider_free = VLC_TRUE;
140 p_intf->p_sys->i_part = 0;
141 p_intf->p_sys->b_mute = VLC_FALSE;
146 /*****************************************************************************
147 * Close: destroy interface window
148 *****************************************************************************/
149 static void Close( vlc_object_t *p_this )
151 intf_thread_t *p_intf = (intf_thread_t *)p_this;
153 if( p_intf->p_sys->p_input )
155 vlc_object_release( p_intf->p_sys->p_input );
158 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
161 module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
164 /* Destroy structure */
165 free( p_intf->p_sys );
168 /*****************************************************************************
170 *****************************************************************************
171 * this part of the interface is in a separate thread so that we can call
172 * gtk_main() from within it without annoying the rest of the program.
173 *****************************************************************************/
174 static void Run( intf_thread_t *p_intf )
176 /* The data types we are allowed to receive */
177 static GtkTargetEntry target_table[] =
179 { "STRING", 0, DROP_ACCEPT_STRING },
180 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
181 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
189 /* gtk_init needs to know the command line. We don't care, so we
190 * give it an empty one */
191 char *p_args[] = { "", NULL };
192 char **pp_args = p_args;
196 gtk_init( &i_args, &pp_args );
199 /* Create some useful widgets that will certainly be used */
200 p_intf->p_sys->p_window = create_intf_window();
201 p_intf->p_sys->p_popup = create_intf_popup();
202 p_intf->p_sys->p_playwin = create_intf_playlist();
203 p_intf->p_sys->p_messages = create_intf_messages();
204 p_intf->p_sys->p_tooltips = gtk_tooltips_new();
205 p_intf->p_sys->p_sout = create_intf_sout();
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 DROP_ACCEPT_END, GDK_ACTION_COPY );
216 /* Accept file drops on the playlist window */
217 gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playwin,
219 GTK_DEST_DEFAULT_ALL, target_table,
220 DROP_ACCEPT_END, GDK_ACTION_COPY );
222 /* Get the slider object */
223 p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
224 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" ) );
226 /* Configure the log window */
227 p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
228 GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
229 gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
230 gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
232 /* Get the interface labels */
233 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
234 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
235 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
236 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
239 /* Connect the date display to the slider */
240 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
241 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
242 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
244 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
245 GTK_SIGNAL_FUNC( E_(GtkDisplayDate) ), NULL );
246 p_intf->p_sys->f_adj_oldvalue = 0;
251 /* We don't create these ones yet because we perhaps won't need them */
252 p_intf->p_sys->p_about = NULL;
253 p_intf->p_sys->p_modules = NULL;
254 p_intf->p_sys->p_open = NULL;
255 p_intf->p_sys->p_jump = NULL;
257 /* Hide tooltips if the option is set */
258 if( !config_GetInt( p_intf, "gtk-tooltips" ) )
260 gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
263 /* Store p_intf to keep an eye on it */
264 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
267 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
269 gtk_object_set_data( GTK_OBJECT( gtk_object_get_data(
270 GTK_OBJECT(p_intf->p_sys->p_popup),
271 "popup_audio" ) ), "p_intf", p_intf );
272 gtk_object_set_data( GTK_OBJECT( gtk_object_get_data(
273 GTK_OBJECT(p_intf->p_sys->p_popup),
274 "popup_video" ) ), "p_intf", p_intf );
276 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
279 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
282 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
284 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
287 psz_sout = config_GetPsz( p_intf, "sout" );
288 p_target = g_string_new( psz_sout ? psz_sout : "" );
289 if( psz_sout ) free( psz_sout );
291 gtk_entry_set_text( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ), "sout_entry_target" ), p_target->str );
292 g_string_free( p_target, TRUE );
294 /* FIXME it's to be sure that only file entry is selected */
295 gtk_toggle_button_set_active( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
296 "sout_access_udp" ), TRUE );
298 gtk_toggle_button_set_active( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
299 "sout_access_file" ), TRUE );
301 /* Show the control window */
302 gtk_widget_show( p_intf->p_sys->p_window );
305 while( !p_intf->b_die )
309 /* Sleep to avoid using all CPU - since some interfaces need to
310 * access keyboard events, a 100ms delay is a good compromise */
312 msleep( INTF_IDLE_SLEEP );
316 /* Sleep to avoid using all CPU - since some interfaces needs to access
317 * keyboard events, a 100ms delay is a good compromise */
318 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, (GtkFunction)Manage,
322 /* Remove the timeout */
323 gtk_timeout_remove( i_dummy );
326 /* Destroy the Tooltips structure */
327 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
328 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_messages) );
329 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_playwin) );
330 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_popup) );
331 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
338 /* following functions are local */
340 /*****************************************************************************
341 * Manage: manage main thread messages
342 *****************************************************************************
343 * In this function, called approx. 10 times a second, we check what the
344 * main program wanted to tell us.
345 *****************************************************************************/
346 static int Manage( intf_thread_t *p_intf )
350 vlc_mutex_lock( &p_intf->change_lock );
352 /* If the "display popup" flag has changed */
353 if( p_intf->b_menu_change )
355 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
357 p_intf->p_sys->p_popup = create_intf_popup();
358 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
361 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
362 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
363 p_intf->b_menu_change = 0;
366 /* Update the log window */
367 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
368 i_stop = *p_intf->p_sys->p_sub->pi_stop;
369 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
371 if( p_intf->p_sys->p_sub->i_start != i_stop )
373 static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
374 static GdkColor gray = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
375 static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
376 static GdkColor red = { 0, 0xffff, 0x6666, 0x6666 };
378 static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
380 static GdkColor * pp_color[4] = { &white, &red, &yellow, &gray };
382 for( i_start = p_intf->p_sys->p_sub->i_start;
384 i_start = (i_start+1) % VLC_MSG_QSIZE )
386 /* Append all messages to log window */
387 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
388 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
390 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
391 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
394 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
395 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
396 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
398 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
402 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
403 p_intf->p_sys->p_sub->i_start = i_start;
404 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
406 /* If the messages list becomes too big, just clean half of it. */
407 if( gtk_text_get_length( p_intf->p_sys->p_messages_text ) >
408 VLC_MSG_QSIZE * 1000 )
410 gtk_text_set_point( p_intf->p_sys->p_messages_text, 0 );
411 gtk_text_forward_delete( p_intf->p_sys->p_messages_text,
412 gtk_text_get_length( p_intf->p_sys->p_messages_text ) / 2 );
415 gtk_text_set_point( p_intf->p_sys->p_messages_text,
416 gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
419 /* Update the playlist */
420 GtkPlayListManage( p_intf );
422 /* Update the input */
423 if( p_intf->p_sys->p_input == NULL )
425 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
428 else if( p_intf->p_sys->p_input->b_dead )
430 vlc_object_release( p_intf->p_sys->p_input );
431 p_intf->p_sys->p_input = NULL;
434 if( p_intf->p_sys->p_input )
436 input_thread_t *p_input = p_intf->p_sys->p_input;
437 aout_instance_t *p_aout = NULL;
438 vout_thread_t *p_vout = NULL;
439 vlc_bool_t b_need_menus = VLC_FALSE;
441 vlc_mutex_lock( &p_input->stream.stream_lock );
443 if( !p_input->b_die )
445 /* New input or stream map change */
446 if( p_input->stream.b_changed )
448 E_(GtkModeManage)( p_intf );
449 GtkSetupMenus( p_intf );
450 p_intf->p_sys->b_playing = 1;
453 /* Manage the slider */
454 if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
456 float newvalue = p_intf->p_sys->p_adj->value;
458 #define p_area p_input->stream.p_selected_area
459 /* If the user hasn't touched the slider since the last time,
460 * then the input can safely change it */
461 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
463 /* Update the value */
464 p_intf->p_sys->p_adj->value =
465 p_intf->p_sys->f_adj_oldvalue =
466 ( 100. * p_area->i_tell ) / p_area->i_size;
468 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
471 /* Otherwise, send message to the input if the user has
472 * finished dragging the slider.
473 * Beware, the hack below is needed by the dvdplay plugin! */
474 else if( p_intf->p_sys->b_slider_free
475 /* hack -> */ && (p_intf->p_sys->f_adj_oldvalue < 100.) )
477 if( newvalue >= 0. && newvalue < 100. )
479 double f_fpos = (double)newvalue / 100.0;
481 /* release the lock to be able to seek */
482 vlc_mutex_unlock( &p_input->stream.stream_lock );
483 var_SetFloat( p_input, "position", f_fpos );
484 vlc_mutex_lock( &p_input->stream.stream_lock );
487 /* Update the old value */
488 p_intf->p_sys->f_adj_oldvalue = newvalue;
493 if( p_intf->p_sys->i_part !=
494 p_input->stream.p_selected_area->i_part )
496 p_intf->p_sys->b_chapter_update = 1;
497 b_need_menus = VLC_TRUE;
500 /* Does the audio output require to update the menus ? */
501 p_aout = (aout_instance_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
506 if( var_Get( (vlc_object_t *)p_aout, "intf-change", &val ) >= 0
509 p_intf->p_sys->b_aout_update = 1;
513 vlc_object_release( (vlc_object_t *)p_aout );
516 /* Does the video output require to update the menus ? */
517 p_vout = (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
522 if( var_Get( (vlc_object_t *)p_vout, "intf-change", &val ) >= 0
525 p_intf->p_sys->b_vout_update = 1;
529 vlc_object_release( (vlc_object_t *)p_vout );
533 GtkSetupMenus( p_intf );
537 vlc_mutex_unlock( &p_input->stream.stream_lock );
539 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
541 E_(GtkModeManage)( p_intf );
542 p_intf->p_sys->b_playing = 0;
545 #ifndef NEED_GTK_MAIN
548 vlc_mutex_unlock( &p_intf->change_lock );
550 /* Prepare to die, young Skywalker */
557 vlc_mutex_unlock( &p_intf->change_lock );