1 /*****************************************************************************
2 * gtk.c : Gtk+ plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: gtk.c,v 1.13 2003/01/26 22:57:21 gbazin Exp $
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.")
69 int i = getenv( "DISPLAY" ) == NULL ? 10 : 90;
71 add_category_hint( N_("Gtk+"), NULL );
72 add_bool( "gtk-tooltips", 1, E_(GtkHideTooltips),
73 TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT );
74 add_integer( "gtk-prefs-maxh", 480, NULL,
75 PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT );
77 set_description( _("Gtk+ interface module") );
78 set_capability( "interface", i );
79 set_callbacks( Open, Close );
80 add_shortcut( "gtk" );
81 set_program( "gvlc" );
84 /*****************************************************************************
85 * Open: initialize and create window
86 *****************************************************************************/
87 static int Open( vlc_object_t *p_this )
89 intf_thread_t *p_intf = (intf_thread_t *)p_this;
91 /* Allocate instance and initialize some members */
92 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
93 if( p_intf->p_sys == NULL )
95 msg_Err( p_intf, "out of memory" );
100 p_intf->p_sys->p_gtk_main = module_Need( p_this, "gtk_main", "gtk" );
101 if( p_intf->p_sys->p_gtk_main == NULL )
103 free( p_intf->p_sys );
108 p_intf->pf_run = Run;
110 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
112 /* Initialize Gtk+ thread */
113 p_intf->p_sys->b_playing = 0;
114 p_intf->p_sys->b_deinterlace_update = 0;
116 p_intf->p_sys->b_aout_update = 0;
117 p_intf->p_sys->b_vout_update = 0;
119 p_intf->p_sys->b_popup_changed = 0;
120 p_intf->p_sys->b_window_changed = 0;
121 p_intf->p_sys->b_playlist_changed = 0;
122 p_intf->p_sys->b_program_update = 0;
123 p_intf->p_sys->b_title_update = 0;
124 p_intf->p_sys->b_chapter_update = 0;
125 p_intf->p_sys->b_spu_update = 0;
126 p_intf->p_sys->b_audio_update = 0;
128 p_intf->p_sys->p_input = NULL;
129 p_intf->p_sys->i_playing = -1;
130 p_intf->p_sys->b_slider_free = 1;
132 p_intf->p_sys->i_part = 0;
134 p_intf->p_sys->b_mute = 0;
138 /*****************************************************************************
139 * Close: destroy interface window
140 *****************************************************************************/
141 static void Close( vlc_object_t *p_this )
143 intf_thread_t *p_intf = (intf_thread_t *)p_this;
145 if( p_intf->p_sys->p_input )
147 vlc_object_release( p_intf->p_sys->p_input );
150 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
153 module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
156 /* Destroy structure */
157 free( p_intf->p_sys );
160 /*****************************************************************************
162 *****************************************************************************
163 * this part of the interface is in a separate thread so that we can call
164 * gtk_main() from within it without annoying the rest of the program.
165 *****************************************************************************/
166 static void Run( intf_thread_t *p_intf )
168 /* The data types we are allowed to receive */
169 static GtkTargetEntry target_table[] =
171 { "STRING", 0, DROP_ACCEPT_STRING },
172 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
173 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
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;
188 gtk_init( &i_args, &pp_args );
191 /* Create some useful widgets that will certainly be used */
192 p_intf->p_sys->p_window = create_intf_window();
193 p_intf->p_sys->p_popup = create_intf_popup();
194 p_intf->p_sys->p_playwin = create_intf_playlist();
195 p_intf->p_sys->p_messages = create_intf_messages();
196 p_intf->p_sys->p_tooltips = gtk_tooltips_new();
197 p_intf->p_sys->p_sout = create_intf_sout();
199 /* Set the title of the main window */
200 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
201 VOUT_TITLE " (Gtk+ interface)");
203 /* Accept file drops on the main window */
204 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
205 GTK_DEST_DEFAULT_ALL, target_table,
206 DROP_ACCEPT_END, GDK_ACTION_COPY );
208 /* Accept file drops on the playlist window */
209 gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playwin,
211 GTK_DEST_DEFAULT_ALL, target_table,
212 DROP_ACCEPT_END, GDK_ACTION_COPY );
214 /* Get the slider object */
215 p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
216 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" ) );
218 /* Configure the log window */
219 p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
220 GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
221 gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
222 gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
224 /* Get the interface labels */
225 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
226 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
227 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
228 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
231 /* Connect the date display to the slider */
232 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
233 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
234 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
236 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
237 GTK_SIGNAL_FUNC( E_(GtkDisplayDate) ), NULL );
238 p_intf->p_sys->f_adj_oldvalue = 0;
243 /* We don't create these ones yet because we perhaps won't need them */
244 p_intf->p_sys->p_about = NULL;
245 p_intf->p_sys->p_modules = NULL;
246 p_intf->p_sys->p_open = NULL;
247 p_intf->p_sys->p_jump = NULL;
249 /* Hide tooltips if the option is set */
250 if( !config_GetInt( p_intf, "gtk-tooltips" ) )
252 gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
255 /* Store p_intf to keep an eye on it */
256 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
259 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
262 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
265 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
268 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
270 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
273 psz_sout = config_GetPsz( p_intf, "sout" );
274 p_target = g_string_new( psz_sout ? psz_sout : "" );
275 if( psz_sout ) free( psz_sout );
277 gtk_entry_set_text( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ), "sout_entry_target" ), p_target->str );
278 g_string_free( p_target, TRUE );
280 /* FIXME it's to be sure that only file entry is selected */
281 gtk_toggle_button_set_active( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
282 "sout_access_udp" ), TRUE );
284 gtk_toggle_button_set_active( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
285 "sout_access_file" ), TRUE );
287 /* Show the control window */
288 gtk_widget_show( p_intf->p_sys->p_window );
291 while( !p_intf->b_die )
295 /* Sleep to avoid using all CPU - since some interfaces need to
296 * access keyboard events, a 100ms delay is a good compromise */
298 msleep( INTF_IDLE_SLEEP );
302 /* Sleep to avoid using all CPU - since some interfaces needs to access
303 * keyboard events, a 100ms delay is a good compromise */
304 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, (GtkFunction)Manage,
308 /* Remove the timeout */
309 gtk_timeout_remove( i_dummy );
312 /* Destroy the Tooltips structure */
313 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
314 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_messages) );
315 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_playwin) );
316 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_popup) );
317 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
324 /* following functions are local */
326 /*****************************************************************************
327 * Manage: manage main thread messages
328 *****************************************************************************
329 * In this function, called approx. 10 times a second, we check what the
330 * main program wanted to tell us.
331 *****************************************************************************/
332 static int Manage( intf_thread_t *p_intf )
336 vlc_mutex_lock( &p_intf->change_lock );
338 /* If the "display popup" flag has changed */
339 if( p_intf->b_menu_change )
341 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
343 p_intf->p_sys->p_popup = create_intf_popup();
344 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
347 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
348 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
349 p_intf->b_menu_change = 0;
352 /* Update the log window */
353 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
354 i_stop = *p_intf->p_sys->p_sub->pi_stop;
355 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
357 if( p_intf->p_sys->p_sub->i_start != i_stop )
359 static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
360 static GdkColor gray = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
361 static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
362 static GdkColor red = { 0, 0xffff, 0x6666, 0x6666 };
364 static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
366 static GdkColor * pp_color[4] = { &white, &red, &yellow, &gray };
368 for( i_start = p_intf->p_sys->p_sub->i_start;
370 i_start = (i_start+1) % VLC_MSG_QSIZE )
372 /* Append all messages to log window */
373 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
374 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
376 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
377 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
380 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
381 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
382 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
384 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
388 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
389 p_intf->p_sys->p_sub->i_start = i_start;
390 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
392 gtk_text_set_point( p_intf->p_sys->p_messages_text,
393 gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
396 /* Update the playlist */
397 GtkPlayListManage( p_intf );
399 /* Update the input */
400 if( p_intf->p_sys->p_input == NULL )
402 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
405 else if( p_intf->p_sys->p_input->b_dead )
407 vlc_object_release( p_intf->p_sys->p_input );
408 p_intf->p_sys->p_input = NULL;
411 if( p_intf->p_sys->p_input )
413 input_thread_t *p_input = p_intf->p_sys->p_input;
414 aout_instance_t *p_aout = NULL;
415 vout_thread_t *p_vout = NULL;
416 vlc_bool_t b_need_menus = VLC_FALSE;
418 vlc_mutex_lock( &p_input->stream.stream_lock );
420 if( !p_input->b_die )
422 /* New input or stream map change */
423 if( p_input->stream.b_changed )
425 E_(GtkModeManage)( p_intf );
426 GtkSetupMenus( p_intf );
427 p_intf->p_sys->b_playing = 1;
430 /* Manage the slider */
431 if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
433 float newvalue = p_intf->p_sys->p_adj->value;
435 #define p_area p_input->stream.p_selected_area
436 /* If the user hasn't touched the slider since the last time,
437 * then the input can safely change it */
438 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
440 /* Update the value */
441 p_intf->p_sys->p_adj->value =
442 p_intf->p_sys->f_adj_oldvalue =
443 ( 100. * p_area->i_tell ) / p_area->i_size;
445 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
448 /* Otherwise, send message to the input if the user has
449 * finished dragging the slider.
450 * Beware, the hack below is needed by the dvdplay plugin! */
451 else if( p_intf->p_sys->b_slider_free
452 /* hack -> */ && (p_intf->p_sys->f_adj_oldvalue <= 100.) )
454 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
456 /* release the lock to be able to seek */
457 vlc_mutex_unlock( &p_input->stream.stream_lock );
458 input_Seek( p_input, i_seek, INPUT_SEEK_SET );
459 vlc_mutex_lock( &p_input->stream.stream_lock );
461 /* Update the old value */
462 p_intf->p_sys->f_adj_oldvalue = newvalue;
467 if( p_intf->p_sys->i_part !=
468 p_input->stream.p_selected_area->i_part )
470 p_intf->p_sys->b_chapter_update = 1;
471 b_need_menus = VLC_TRUE;
474 /* Does the audio output require to update the menus ? */
475 p_aout = (aout_instance_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
480 if( var_Get( (vlc_object_t *)p_aout, "intf-change", &val ) >= 0
483 p_intf->p_sys->b_aout_update = 1;
487 vlc_object_release( (vlc_object_t *)p_aout );
490 /* Does the video output require to update the menus ? */
491 p_vout = (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
496 if( var_Get( (vlc_object_t *)p_vout, "intf-change", &val ) >= 0
499 p_intf->p_sys->b_vout_update = 1;
503 vlc_object_release( (vlc_object_t *)p_vout );
507 GtkSetupMenus( p_intf );
511 vlc_mutex_unlock( &p_input->stream.stream_lock );
513 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
515 E_(GtkModeManage)( p_intf );
516 p_intf->p_sys->b_playing = 0;
519 #ifndef NEED_GTK_MAIN
522 vlc_mutex_unlock( &p_intf->change_lock );
524 /* Prepare to die, young Skywalker */
531 vlc_mutex_unlock( &p_intf->change_lock );