1 /*****************************************************************************
2 * gtk.c : Gtk+ plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: gtk.c,v 1.10 2002/01/15 19:01:28 stef 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_Probe ( probedata_t *p_data );
57 static int intf_Open ( intf_thread_t *p_intf );
58 static void intf_Close ( intf_thread_t *p_intf );
59 static void intf_Run ( intf_thread_t *p_intf );
61 static gint GtkManage ( gpointer p_data );
63 /*****************************************************************************
64 * Building configuration tree
65 *****************************************************************************/
70 SET_DESCRIPTION( "Gtk+ interface module" )
72 if( getenv( "DISPLAY" ) == NULL )
74 ADD_CAPABILITY( INTF, 10 )
79 ADD_CAPABILITY( INTF, 90 )
86 intf_getfunctions( &p_module->p_functions->intf );
89 MODULE_DEACTIVATE_START
90 MODULE_DEACTIVATE_STOP
92 /*****************************************************************************
93 * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
94 *****************************************************************************
95 * gtk_init() makes several calls to g_atexit() which calls atexit() to
96 * register tidying callbacks to be called at program exit. Since the Gtk+
97 * plugin is likely to be unloaded at program exit, we have to export this
98 * symbol to intercept the g_atexit() calls. Talk about crude hack.
99 *****************************************************************************/
100 void g_atexit( GVoidFunc func )
102 intf_thread_t *p_intf = p_main->p_intf;
106 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
112 if( i_dummy >= MAX_ATEXIT - 1 )
114 intf_ErrMsg( "intf error: too many atexit() callbacks to register" );
118 p_intf->p_sys->pf_callback[i_dummy] = func;
119 p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
122 /*****************************************************************************
123 * Functions exported as capabilities. They are declared as static so that
124 * we don't pollute the namespace too much.
125 *****************************************************************************/
126 static void intf_getfunctions( function_list_t * p_function_list )
128 p_function_list->pf_probe = intf_Probe;
129 p_function_list->functions.intf.pf_open = intf_Open;
130 p_function_list->functions.intf.pf_close = intf_Close;
131 p_function_list->functions.intf.pf_run = intf_Run;
134 /*****************************************************************************
135 * intf_Probe: probe the interface and return a score
136 *****************************************************************************
137 * This function tries to initialize Gtk+ and returns a score to the
138 * plugin manager so that it can select the best plugin.
139 *****************************************************************************/
140 static int intf_Probe( probedata_t *p_data )
143 if( getenv( "DISPLAY" ) == NULL )
152 /*****************************************************************************
153 * intf_Open: initialize and create window
154 *****************************************************************************/
155 static int intf_Open( intf_thread_t *p_intf )
157 /* Allocate instance and initialize some members */
158 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
159 if( p_intf->p_sys == NULL )
161 intf_ErrMsg("error: %s", strerror(ENOMEM));
165 /* Initialize Gtk+ thread */
166 p_intf->p_sys->b_playing = 0;
167 p_intf->p_sys->b_popup_changed = 0;
168 p_intf->p_sys->b_window_changed = 0;
169 p_intf->p_sys->b_playlist_changed = 0;
171 p_intf->p_sys->i_playing = -1;
172 p_intf->p_sys->b_slider_free = 1;
174 p_intf->p_sys->pf_callback[0] = NULL;
179 /*****************************************************************************
180 * intf_Close: destroy interface window
181 *****************************************************************************/
182 static void intf_Close( intf_thread_t *p_intf )
184 /* Destroy structure */
185 free( p_intf->p_sys );
188 /*****************************************************************************
189 * intf_Run: Gtk+ thread
190 *****************************************************************************
191 * this part of the interface is in a separate thread so that we can call
192 * gtk_main() from within it without annoying the rest of the program.
193 * XXX: the approach may look kludgy, and probably is, but I could not find
194 * a better way to dynamically load a Gtk+ interface at runtime.
195 *****************************************************************************/
196 static void intf_Run( intf_thread_t *p_intf )
198 /* gtk_init needs to know the command line. We don't care, so we
199 * give it an empty one */
200 char *p_args[] = { "" };
201 char **pp_args = p_args;
205 /* The data types we are allowed to receive */
206 static GtkTargetEntry target_table[] =
208 { "STRING", 0, DROP_ACCEPT_STRING },
209 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
210 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
213 /* Initialize Gtk+ */
214 gtk_init( &i_args, &pp_args );
216 /* Create some useful widgets that will certainly be used */
217 p_intf->p_sys->p_window = create_intf_window( );
218 p_intf->p_sys->p_popup = create_intf_popup( );
219 p_intf->p_sys->p_playlist = create_intf_playlist();
221 /* Set the title of the main window */
222 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
223 VOUT_TITLE " (Gtk+ interface)");
225 /* Accept file drops on the main window */
226 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
227 GTK_DEST_DEFAULT_ALL, target_table,
228 1, GDK_ACTION_COPY );
230 /* Accept file drops on the playlist window */
231 gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playlist,
233 GTK_DEST_DEFAULT_ALL, target_table,
234 1, GDK_ACTION_COPY );
236 /* Get the interface labels */
237 p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
238 GTK_OBJECT(p_intf->p_sys->p_window ), "slider_frame" ) );
240 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
241 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
242 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
243 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
246 /* Connect the date display to the slider */
247 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
248 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
249 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
251 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
252 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
253 p_intf->p_sys->f_adj_oldvalue = 0;
256 /* We don't create these ones yet because we perhaps won't need them */
257 p_intf->p_sys->p_about = NULL;
258 p_intf->p_sys->p_modules = NULL;
259 p_intf->p_sys->p_fileopen = NULL;
260 p_intf->p_sys->p_disc = NULL;
261 p_intf->p_sys->p_network = NULL;
262 p_intf->p_sys->p_preferences = NULL;
263 p_intf->p_sys->p_jump = NULL;
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),
272 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
275 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
278 /* Show the control window */
279 gtk_widget_show( p_intf->p_sys->p_window );
281 /* Sleep to avoid using all CPU - since some interfaces needs to access
282 * keyboard events, a 100ms delay is a good compromise */
283 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GtkManage, p_intf );
288 /* Remove the timeout */
289 gtk_timeout_remove( i_dummy );
291 /* Launch stored callbacks */
293 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
296 p_intf->p_sys->pf_callback[i_dummy]();
300 /* following functions are local */
302 /*****************************************************************************
303 * GtkManage: manage main thread messages
304 *****************************************************************************
305 * In this function, called approx. 10 times a second, we check what the
306 * main program wanted to tell us.
307 *****************************************************************************/
308 static gint GtkManage( gpointer p_data )
310 #define p_intf ((intf_thread_t *)p_data)
312 vlc_mutex_lock( &p_intf->change_lock );
314 /* If the "display popup" flag has changed */
315 if( p_intf->b_menu_change )
317 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
319 p_intf->p_sys->p_popup = create_intf_popup();
320 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
323 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
324 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
325 p_intf->b_menu_change = 0;
328 /* update the playlist */
329 GtkPlayListManage( p_data );
331 if( p_input_bank->pp_input[0] != NULL )
333 vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
335 if( !p_input_bank->pp_input[0]->b_die )
337 /* New input or stream map change */
338 if( p_input_bank->pp_input[0]->stream.b_changed )
340 GtkModeManage( p_intf );
341 GtkSetupMenus( p_intf );
342 p_intf->p_sys->b_playing = 1;
345 /* Manage the slider */
346 if( p_input_bank->pp_input[0]->stream.b_seekable &&
347 p_intf->p_sys->b_playing )
349 float newvalue = p_intf->p_sys->p_adj->value;
351 #define p_area p_input_bank->pp_input[0]->stream.p_selected_area
352 /* If the user hasn't touched the slider since the last time,
353 * then the input can safely change it */
354 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
356 /* Update the value */
357 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
358 ( 100. * p_area->i_tell ) / p_area->i_size;
360 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
363 /* Otherwise, send message to the input if the user has
364 * finished dragging the slider */
365 else if( p_intf->p_sys->b_slider_free )
367 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
369 /* release the lock to be able to seek */
370 vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
371 input_Seek( p_input_bank->pp_input[0], i_seek );
372 vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
374 /* Update the old value */
375 p_intf->p_sys->f_adj_oldvalue = newvalue;
380 if( p_intf->p_sys->i_part !=
381 p_input_bank->pp_input[0]->stream.p_selected_area->i_part )
383 p_intf->p_sys->b_chapter_update = 1;
384 GtkSetupMenus( p_intf );
388 vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
390 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
392 GtkModeManage( p_intf );
393 p_intf->p_sys->b_playing = 0;
396 /* Manage core vlc functions through the callback */
397 p_intf->pf_manage( p_intf );
401 vlc_mutex_unlock( &p_intf->change_lock );
403 /* Prepare to die, young Skywalker */
410 vlc_mutex_unlock( &p_intf->change_lock );