1 /*****************************************************************************
2 * gtk.c : Gtk+ plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: gtk.c,v 1.31 2002/07/31 20:56:51 sam 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"
41 #include "gtk_display.h"
42 #include "gtk_common.h"
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
50 static void Run ( intf_thread_t * );
51 static gint Manage ( gpointer );
53 /*****************************************************************************
54 * Local variables (mutex-protected).
55 *****************************************************************************/
56 static void ** pp_global_data = NULL;
58 /*****************************************************************************
60 *****************************************************************************/
61 #define TOOLTIPS_TEXT N_("show tooltips")
62 #define TOOLTIPS_LONGTEXT N_("Show tooltips for configuration options.")
64 #define PREFS_MAXH_TEXT N_("maximum height for the configuration windows")
65 #define PREFS_MAXH_LONGTEXT N_( \
66 "You can set the maximum height that the configuration windows in the " \
67 "preferences menu will occupy.")
73 int i = getenv( "DISPLAY" ) == NULL ? 10 : 90;
75 pp_global_data = p_module->p_vlc->pp_global_data;
77 add_category_hint( N_("Miscellaneous"), NULL );
78 add_bool( "gtk-tooltips", 1, GtkHideTooltips,
79 TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT );
80 add_integer( "gtk-prefs-maxh", 480, NULL,
81 PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT );
83 set_description( _("Gtk+ interface module") );
84 set_capability( "interface", i );
85 set_callbacks( Open, Close );
86 set_program( "gvlc" );
89 /*****************************************************************************
90 * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
91 *****************************************************************************
92 * gtk_init() makes several calls to g_atexit() which calls atexit() to
93 * register tidying callbacks to be called at program exit. Since the Gtk+
94 * plugin is likely to be unloaded at program exit, we have to export this
95 * symbol to intercept the g_atexit() calls. Talk about crude hack.
96 *****************************************************************************/
97 void g_atexit( GVoidFunc func )
99 intf_thread_t *p_intf;
103 if( pp_global_data == NULL )
109 p_intf = (intf_thread_t *)*pp_global_data;
116 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
122 if( i_dummy >= MAX_ATEXIT - 1 )
124 msg_Err( p_intf, "too many atexit() callbacks to register" );
128 p_intf->p_sys->pf_callback[i_dummy] = func;
129 p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
132 /*****************************************************************************
133 * Open: initialize and create window
134 *****************************************************************************/
135 static int Open( vlc_object_t *p_this )
137 intf_thread_t *p_intf = (intf_thread_t *)p_this;
139 /* Allocate instance and initialize some members */
140 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
141 if( p_intf->p_sys == NULL )
143 msg_Err( p_intf, "out of memory" );
147 p_intf->pf_run = Run;
149 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
151 /* Initialize Gtk+ thread */
152 p_intf->p_sys->b_playing = 0;
153 p_intf->p_sys->b_popup_changed = 0;
154 p_intf->p_sys->b_window_changed = 0;
155 p_intf->p_sys->b_playlist_changed = 0;
157 p_intf->p_sys->p_input = NULL;
158 p_intf->p_sys->i_playing = -1;
159 p_intf->p_sys->b_slider_free = 1;
161 p_intf->p_sys->pf_callback[0] = NULL;
163 p_intf->p_sys->i_part = 0;
168 /*****************************************************************************
169 * Close: destroy interface window
170 *****************************************************************************/
171 static void Close( vlc_object_t *p_this )
173 intf_thread_t *p_intf = (intf_thread_t *)p_this;
175 if( p_intf->p_sys->p_input )
177 vlc_object_release( p_intf->p_sys->p_input );
180 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
182 /* Destroy structure */
183 free( p_intf->p_sys );
186 /*****************************************************************************
188 *****************************************************************************
189 * this part of the interface is in a separate thread so that we can call
190 * gtk_main() from within it without annoying the rest of the program.
191 * XXX: the approach may look kludgy, and probably is, but I could not find
192 * a better way to dynamically load a Gtk+ interface at runtime.
193 *****************************************************************************/
194 static void Run( intf_thread_t *p_intf )
196 /* gtk_init needs to know the command line. We don't care, so we
197 * give it an empty one */
198 char *p_args[] = { "" };
199 char **pp_args = p_args;
203 /* The data types we are allowed to receive */
204 static GtkTargetEntry target_table[] =
206 { "STRING", 0, DROP_ACCEPT_STRING },
207 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
208 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
211 /* Initialize Gtk+ */
213 /* gtk_init will register stuff with g_atexit, so we need to take
214 * the global lock if we want to be able to intercept the calls */
215 vlc_mutex_lock( p_intf->p_vlc->p_global_lock );
216 *p_intf->p_vlc->pp_global_data = p_intf;
217 gtk_init( &i_args, &pp_args );
218 vlc_mutex_unlock( p_intf->p_vlc->p_global_lock );
220 /* Create some useful widgets that will certainly be used */
221 p_intf->p_sys->p_window = create_intf_window();
222 p_intf->p_sys->p_popup = create_intf_popup();
223 p_intf->p_sys->p_playwin = create_intf_playlist();
224 p_intf->p_sys->p_messages = create_intf_messages();
225 p_intf->p_sys->p_tooltips = gtk_tooltips_new();
227 /* Set the title of the main window */
228 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
229 VOUT_TITLE " (Gtk+ interface)");
231 /* Accept file drops on the main window */
232 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
233 GTK_DEST_DEFAULT_ALL, target_table,
234 1, GDK_ACTION_COPY );
236 /* Accept file drops on the playlist window */
237 gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playwin,
239 GTK_DEST_DEFAULT_ALL, target_table,
240 1, GDK_ACTION_COPY );
242 /* Get the slider object */
243 p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
244 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" ) );
246 /* Configure the log window */
247 p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
248 GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
249 gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
250 gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
252 /* Get the interface labels */
253 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
254 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
255 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
256 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
259 /* Connect the date display to the slider */
260 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
261 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
262 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
264 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
265 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
266 p_intf->p_sys->f_adj_oldvalue = 0;
269 /* We don't create these ones yet because we perhaps won't need them */
270 p_intf->p_sys->p_about = NULL;
271 p_intf->p_sys->p_modules = NULL;
272 p_intf->p_sys->p_open = NULL;
273 p_intf->p_sys->p_jump = NULL;
275 /* Hide tooltips if the option is set */
276 if( !config_GetInt( p_intf, "gtk-tooltips" ) )
278 gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
281 /* Store p_intf to keep an eye on it */
282 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
285 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
288 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
291 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
294 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
297 /* Show the control window */
298 gtk_widget_show( p_intf->p_sys->p_window );
300 /* Sleep to avoid using all CPU - since some interfaces needs to access
301 * keyboard events, a 100ms delay is a good compromise */
302 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, Manage, p_intf );
307 /* Remove the timeout */
308 gtk_timeout_remove( i_dummy );
310 /* Destroy the Tooltips structure */
311 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
313 /* Launch stored callbacks */
315 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
318 p_intf->p_sys->pf_callback[i_dummy]();
322 /* following functions are local */
324 /*****************************************************************************
325 * Manage: manage main thread messages
326 *****************************************************************************
327 * In this function, called approx. 10 times a second, we check what the
328 * main program wanted to tell us.
329 *****************************************************************************/
330 static gint Manage( gpointer p_data )
332 #define p_intf ((intf_thread_t *)p_data)
335 vlc_mutex_lock( &p_intf->change_lock );
337 /* If the "display popup" flag has changed */
338 if( p_intf->b_menu_change )
340 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
342 p_intf->p_sys->p_popup = create_intf_popup();
343 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
346 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
347 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
348 p_intf->b_menu_change = 0;
351 /* Update the log window */
352 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
353 i_stop = *p_intf->p_sys->p_sub->pi_stop;
354 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
356 if( p_intf->p_sys->p_sub->i_start != i_stop )
358 static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
359 static GdkColor gray = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
360 static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
361 static GdkColor red = { 0, 0xffff, 0x6666, 0x6666 };
363 static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
365 static GdkColor * pp_color[4] = { &white, &red, &yellow, &gray };
367 for( i_start = p_intf->p_sys->p_sub->i_start;
369 i_start = (i_start+1) % VLC_MSG_QSIZE )
371 /* Append all messages to log window */
372 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
373 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
375 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
376 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
379 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
380 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
381 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
383 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
387 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
388 p_intf->p_sys->p_sub->i_start = i_start;
389 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
391 gtk_text_set_point( p_intf->p_sys->p_messages_text,
392 gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
395 /* Update the playlist */
396 GtkPlayListManage( p_data );
398 /* Update the input */
399 if( p_intf->p_sys->p_input == NULL )
401 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
404 else if( p_intf->p_sys->p_input->b_dead )
406 vlc_object_release( p_intf->p_sys->p_input );
407 p_intf->p_sys->p_input = NULL;
410 if( p_intf->p_sys->p_input )
412 input_thread_t *p_input = p_intf->p_sys->p_input;
414 vlc_mutex_lock( &p_input->stream.stream_lock );
416 if( !p_input->b_die )
418 /* New input or stream map change */
419 if( p_input->stream.b_changed )
421 GtkModeManage( p_intf );
422 GtkSetupMenus( p_intf );
423 p_intf->p_sys->b_playing = 1;
426 /* Manage the slider */
427 if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
429 float newvalue = p_intf->p_sys->p_adj->value;
431 #define p_area p_input->stream.p_selected_area
432 /* If the user hasn't touched the slider since the last time,
433 * then the input can safely change it */
434 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
436 /* Update the value */
437 p_intf->p_sys->p_adj->value =
438 p_intf->p_sys->f_adj_oldvalue =
439 ( 100. * p_area->i_tell ) / p_area->i_size;
441 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
444 /* Otherwise, send message to the input if the user has
445 * finished dragging the slider */
446 else if( p_intf->p_sys->b_slider_free )
448 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
450 /* release the lock to be able to seek */
451 vlc_mutex_unlock( &p_input->stream.stream_lock );
452 input_Seek( p_input, i_seek, INPUT_SEEK_SET );
453 vlc_mutex_lock( &p_input->stream.stream_lock );
455 /* Update the old value */
456 p_intf->p_sys->f_adj_oldvalue = newvalue;
461 if( p_intf->p_sys->i_part !=
462 p_input->stream.p_selected_area->i_part )
464 p_intf->p_sys->b_chapter_update = 1;
465 GtkSetupMenus( p_intf );
469 vlc_mutex_unlock( &p_input->stream.stream_lock );
471 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
473 GtkModeManage( p_intf );
474 p_intf->p_sys->b_playing = 0;
479 vlc_mutex_unlock( &p_intf->change_lock );
481 /* Prepare to die, young Skywalker */
488 vlc_mutex_unlock( &p_intf->change_lock );