1 /*****************************************************************************
2 * gtk.c : Gtk+ plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: gtk.c,v 1.18 2002/03/31 22:35:44 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() */
32 #include <videolan/vlc.h>
36 #include "stream_control.h"
37 #include "input_ext-intf.h"
39 #include "interface.h"
40 #include "intf_playlist.h"
43 #include "video_output.h"
45 #include "gtk_callbacks.h"
46 #include "gtk_interface.h"
47 #include "gtk_support.h"
49 #include "gtk_display.h"
50 #include "gtk_common.h"
52 /*****************************************************************************
54 *****************************************************************************/
55 static void intf_getfunctions ( function_list_t * p_function_list );
56 static int intf_Open ( intf_thread_t *p_intf );
57 static void intf_Close ( intf_thread_t *p_intf );
58 static void intf_Run ( intf_thread_t *p_intf );
60 static gint GtkManage ( gpointer p_data );
62 /*****************************************************************************
63 * Building configuration tree
64 *****************************************************************************/
65 #define TOOLTIPS_TEXT "hide tooltips"
66 #define TOOLTIPS_LONGTEXT "Do not show tooltips for configuration options"
68 #define PREFS_MAXH_TEXT "maximum height for the configuration windows"
69 #define PREFS_MAXH_LONGTEXT "You can set the maximum height that the " \
70 "configuration windows in the prefences menu " \
74 ADD_CATEGORY_HINT( "Misc Options", NULL )
75 ADD_BOOL ( "gtk_hide_tooltips", NULL, TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT )
76 ADD_INTEGER ( "gtk_prefs_maxh", 480, NULL, PREFS_MAXH_TEXT,
81 SET_DESCRIPTION( "Gtk+ interface module" )
83 if( getenv( "DISPLAY" ) == NULL )
85 ADD_CAPABILITY( INTF, 10 )
90 ADD_CAPABILITY( INTF, 90 )
97 intf_getfunctions( &p_module->p_functions->intf );
100 MODULE_DEACTIVATE_START
101 MODULE_DEACTIVATE_STOP
103 /*****************************************************************************
104 * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
105 *****************************************************************************
106 * gtk_init() makes several calls to g_atexit() which calls atexit() to
107 * register tidying callbacks to be called at program exit. Since the Gtk+
108 * plugin is likely to be unloaded at program exit, we have to export this
109 * symbol to intercept the g_atexit() calls. Talk about crude hack.
110 *****************************************************************************/
111 void g_atexit( GVoidFunc func )
113 intf_thread_t *p_intf = p_main->p_intf;
117 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
123 if( i_dummy >= MAX_ATEXIT - 1 )
125 intf_ErrMsg( "intf error: too many atexit() callbacks to register" );
129 p_intf->p_sys->pf_callback[i_dummy] = func;
130 p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
133 /*****************************************************************************
134 * Functions exported as capabilities. They are declared as static so that
135 * we don't pollute the namespace too much.
136 *****************************************************************************/
137 static void intf_getfunctions( function_list_t * p_function_list )
139 p_function_list->functions.intf.pf_open = intf_Open;
140 p_function_list->functions.intf.pf_close = intf_Close;
141 p_function_list->functions.intf.pf_run = intf_Run;
144 /*****************************************************************************
145 * intf_Open: initialize and create window
146 *****************************************************************************/
147 static int intf_Open( intf_thread_t *p_intf )
149 /* Allocate instance and initialize some members */
150 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
151 if( p_intf->p_sys == NULL )
153 intf_ErrMsg("error: %s", strerror(ENOMEM));
157 p_intf->p_sys->p_sub = intf_MsgSub();
159 /* Initialize Gtk+ thread */
160 p_intf->p_sys->b_playing = 0;
161 p_intf->p_sys->b_popup_changed = 0;
162 p_intf->p_sys->b_window_changed = 0;
163 p_intf->p_sys->b_playlist_changed = 0;
165 p_intf->p_sys->i_playing = -1;
166 p_intf->p_sys->b_slider_free = 1;
168 p_intf->p_sys->pf_callback[0] = NULL;
173 /*****************************************************************************
174 * intf_Close: destroy interface window
175 *****************************************************************************/
176 static void intf_Close( intf_thread_t *p_intf )
178 intf_MsgUnsub( p_intf->p_sys->p_sub );
180 /* Destroy structure */
181 free( p_intf->p_sys );
184 /*****************************************************************************
185 * intf_Run: Gtk+ thread
186 *****************************************************************************
187 * this part of the interface is in a separate thread so that we can call
188 * gtk_main() from within it without annoying the rest of the program.
189 * XXX: the approach may look kludgy, and probably is, but I could not find
190 * a better way to dynamically load a Gtk+ interface at runtime.
191 *****************************************************************************/
192 static void intf_Run( intf_thread_t *p_intf )
194 /* gtk_init needs to know the command line. We don't care, so we
195 * give it an empty one */
196 char *p_args[] = { "" };
197 char **pp_args = p_args;
201 /* The data types we are allowed to receive */
202 static GtkTargetEntry target_table[] =
204 { "STRING", 0, DROP_ACCEPT_STRING },
205 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
206 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
209 /* Initialize Gtk+ */
210 gtk_init( &i_args, &pp_args );
212 /* Create some useful widgets that will certainly be used */
213 p_intf->p_sys->p_window = create_intf_window();
214 p_intf->p_sys->p_popup = create_intf_popup();
215 p_intf->p_sys->p_playlist = create_intf_playlist();
216 p_intf->p_sys->p_messages = create_intf_messages();
217 p_intf->p_sys->p_tooltips = gtk_tooltips_new();
219 /* Set the title of the main window */
220 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
221 VOUT_TITLE " (Gtk+ interface)");
223 /* Accept file drops on the main window */
224 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
225 GTK_DEST_DEFAULT_ALL, target_table,
226 1, GDK_ACTION_COPY );
228 /* Accept file drops on the playlist window */
229 gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playlist,
231 GTK_DEST_DEFAULT_ALL, target_table,
232 1, GDK_ACTION_COPY );
234 /* Get the slider object */
235 p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
236 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" ) );
238 /* Configure the log window */
239 p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
240 GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
241 gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
242 gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
244 /* Get the interface labels */
245 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
246 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
247 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
248 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
251 /* Connect the date display to the slider */
252 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
253 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
254 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
256 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
257 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
258 p_intf->p_sys->f_adj_oldvalue = 0;
261 /* We don't create these ones yet because we perhaps won't need them */
262 p_intf->p_sys->p_about = NULL;
263 p_intf->p_sys->p_modules = NULL;
264 p_intf->p_sys->p_fileopen = NULL;
265 p_intf->p_sys->p_disc = NULL;
266 p_intf->p_sys->p_sat = NULL;
267 p_intf->p_sys->p_network = NULL;
268 p_intf->p_sys->p_jump = NULL;
270 /* Hide tooltips if the option is set */
271 if( config_GetIntVariable( "gtk_hide_tooltips" ) )
272 gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
274 /* Store p_intf to keep an eye on it */
275 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
278 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
281 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
284 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
287 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
290 /* Show the control window */
291 gtk_widget_show( p_intf->p_sys->p_window );
293 /* Sleep to avoid using all CPU - since some interfaces needs to access
294 * keyboard events, a 100ms delay is a good compromise */
295 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GtkManage, p_intf );
300 /* Remove the timeout */
301 gtk_timeout_remove( i_dummy );
303 /* Destroy the Tooltips structure */
304 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
306 /* Launch stored callbacks */
308 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
311 p_intf->p_sys->pf_callback[i_dummy]();
315 /* following functions are local */
317 /*****************************************************************************
318 * GtkManage: manage main thread messages
319 *****************************************************************************
320 * In this function, called approx. 10 times a second, we check what the
321 * main program wanted to tell us.
322 *****************************************************************************/
323 static gint GtkManage( gpointer p_data )
325 #define p_intf ((intf_thread_t *)p_data)
326 static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
327 static GdkColor red = { 0, 0xffff, 0x6666, 0x6666 };
328 static GdkColor gray = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
333 vlc_mutex_lock( &p_intf->change_lock );
335 /* If the "display popup" flag has changed */
336 if( p_intf->b_menu_change )
338 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
340 p_intf->p_sys->p_popup = create_intf_popup();
341 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
344 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
345 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
346 p_intf->b_menu_change = 0;
349 /* Update the log window */
350 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
351 i_stop = *p_intf->p_sys->p_sub->pi_stop;
352 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
354 if( p_intf->p_sys->p_sub->i_start != i_stop )
356 for( i_start = p_intf->p_sys->p_sub->i_start;
358 i_start = (i_start+1) % INTF_MSG_QSIZE )
360 /* Append all messages to log window */
361 switch( p_intf->p_sys->p_sub->p_msg[i_start].i_type )
374 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, p_color,
375 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
376 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, p_color,
380 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
381 p_intf->p_sys->p_sub->i_start = i_start;
382 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
384 gtk_text_set_point( p_intf->p_sys->p_messages_text,
385 gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
388 /* Update the playlist */
389 GtkPlayListManage( p_data );
391 if( p_input_bank->pp_input[0] != NULL )
393 vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
395 if( !p_input_bank->pp_input[0]->b_die )
397 /* New input or stream map change */
398 if( p_input_bank->pp_input[0]->stream.b_changed )
400 GtkModeManage( p_intf );
401 GtkSetupMenus( p_intf );
402 p_intf->p_sys->b_playing = 1;
405 /* Manage the slider */
406 if( p_input_bank->pp_input[0]->stream.b_seekable &&
407 p_intf->p_sys->b_playing )
409 float newvalue = p_intf->p_sys->p_adj->value;
411 #define p_area p_input_bank->pp_input[0]->stream.p_selected_area
412 /* If the user hasn't touched the slider since the last time,
413 * then the input can safely change it */
414 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
416 /* Update the value */
417 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
418 ( 100. * p_area->i_tell ) / p_area->i_size;
420 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
423 /* Otherwise, send message to the input if the user has
424 * finished dragging the slider */
425 else if( p_intf->p_sys->b_slider_free )
427 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
429 /* release the lock to be able to seek */
430 vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
431 input_Seek( p_input_bank->pp_input[0], i_seek );
432 vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
434 /* Update the old value */
435 p_intf->p_sys->f_adj_oldvalue = newvalue;
440 if( p_intf->p_sys->i_part !=
441 p_input_bank->pp_input[0]->stream.p_selected_area->i_part )
443 p_intf->p_sys->b_chapter_update = 1;
444 GtkSetupMenus( p_intf );
448 vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
450 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
452 GtkModeManage( p_intf );
453 p_intf->p_sys->b_playing = 0;
456 /* Manage core vlc functions through the callback */
457 p_intf->pf_manage( p_intf );
461 vlc_mutex_unlock( &p_intf->change_lock );
463 /* Prepare to die, young Skywalker */
470 vlc_mutex_unlock( &p_intf->change_lock );