1 /*****************************************************************************
2 * intf_gnome.c: Gnome interface
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: intf_gnome.c,v 1.6 2001/11/28 15:08:05 massiot Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Stéphane Borel <stef@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #define MODULE_NAME gnome
26 #include "modules_inner.h"
28 /*****************************************************************************
30 *****************************************************************************/
33 #include <errno.h> /* ENOMEM */
34 #include <stdlib.h> /* free() */
35 #include <string.h> /* strerror() */
47 #include "stream_control.h"
48 #include "input_ext-intf.h"
50 #include "interface.h"
51 #include "intf_playlist.h"
54 #include "video_output.h"
56 #include "gnome_callbacks.h"
57 #include "gnome_interface.h"
58 #include "gnome_support.h"
59 #include "gtk_display.h"
65 #include "modules_export.h"
67 /*****************************************************************************
69 *****************************************************************************/
70 static int intf_Probe ( probedata_t *p_data );
71 static int intf_Open ( intf_thread_t *p_intf );
72 static void intf_Close ( intf_thread_t *p_intf );
73 static void intf_Run ( intf_thread_t *p_intf );
75 static gint GnomeManage ( gpointer p_data );
77 /*****************************************************************************
78 * g_atexit: kludge to avoid the Gnome thread to segfault at exit
79 *****************************************************************************
80 * gtk_init() makes several calls to g_atexit() which calls atexit() to
81 * register tidying callbacks to be called at program exit. Since the Gnome
82 * plugin is likely to be unloaded at program exit, we have to export this
83 * symbol to intercept the g_atexit() calls. Talk about crude hack.
84 *****************************************************************************/
85 void g_atexit( GVoidFunc func )
87 intf_thread_t *p_intf = p_main->p_intf;
89 if( p_intf->p_sys->pf_gdk_callback == NULL )
91 p_intf->p_sys->pf_gdk_callback = func;
93 else if( p_intf->p_sys->pf_gtk_callback == NULL )
95 p_intf->p_sys->pf_gtk_callback = func;
97 /* else nothing, but we could do something here */
101 /*****************************************************************************
102 * Functions exported as capabilities. They are declared as static so that
103 * we don't pollute the namespace too much.
104 *****************************************************************************/
105 void _M( intf_getfunctions )( function_list_t * p_function_list )
107 p_function_list->pf_probe = intf_Probe;
108 p_function_list->functions.intf.pf_open = intf_Open;
109 p_function_list->functions.intf.pf_close = intf_Close;
110 p_function_list->functions.intf.pf_run = intf_Run;
113 /*****************************************************************************
114 * intf_Probe: probe the interface and return a score
115 *****************************************************************************
116 * This function tries to initialize Gnome and returns a score to the
117 * plugin manager so that it can select the best plugin.
118 *****************************************************************************/
119 static int intf_Probe( probedata_t *p_data )
121 if( TestMethod( INTF_METHOD_VAR, "gnome" ) )
126 if( TestProgram( "gnome-vlc" ) )
134 /*****************************************************************************
135 * intf_Open: initialize and create window
136 *****************************************************************************/
137 static int intf_Open( intf_thread_t *p_intf )
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 intf_ErrMsg("error: %s", strerror(ENOMEM));
147 /* Initialize Gnome thread */
148 p_intf->p_sys->b_playing = 1;
149 p_intf->p_sys->b_popup_changed = 0;
150 p_intf->p_sys->b_window_changed = 0;
151 p_intf->p_sys->b_playlist_changed = 0;
153 p_intf->p_sys->i_playing = -1;
154 p_intf->p_sys->b_slider_free = 1;
156 p_intf->p_sys->pf_gtk_callback = NULL;
157 p_intf->p_sys->pf_gdk_callback = NULL;
162 /*****************************************************************************
163 * intf_Close: destroy interface window
164 *****************************************************************************/
165 static void intf_Close( intf_thread_t *p_intf )
167 /* Destroy structure */
168 free( p_intf->p_sys );
171 /*****************************************************************************
172 * intf_Run: Gnome thread
173 *****************************************************************************
174 * this part of the interface is in a separate thread so that we can call
175 * gtk_main() from within it without annoying the rest of the program.
176 * XXX: the approach may look kludgy, and probably is, but I could not find
177 * a better way to dynamically load a Gnome interface at runtime.
178 *****************************************************************************/
179 static void intf_Run( intf_thread_t *p_intf )
181 /* gnome_init needs to know the command line. We don't care, so we
182 * give it an empty one */
183 char *p_args[] = { "" };
186 /* The data types we are allowed to receive */
187 static GtkTargetEntry target_table[] =
189 { "STRING", 0, DROP_ACCEPT_STRING },
190 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
191 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
194 /* intf_Manage callback timeout */
197 /* Initialize Gnome */
198 gnome_init( p_main->psz_arg0, VERSION, i_args, p_args );
200 /* Create some useful widgets that will certainly be used */
201 p_intf->p_sys->p_window = create_intf_window( );
202 p_intf->p_sys->p_popup = create_intf_popup( );
203 p_intf->p_sys->p_playlist = create_intf_playlist();
205 /* Set the title of the main window */
206 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
207 VOUT_TITLE " (Gnome interface)");
209 /* Accept file drops on the main window */
210 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
211 GTK_DEST_DEFAULT_ALL, target_table,
212 1, GDK_ACTION_COPY );
213 /* Accept file drops on the playlist window */
214 gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
215 p_intf->p_sys->p_playlist ), "playlist_clist") ),
216 GTK_DEST_DEFAULT_ALL, target_table,
217 1, GDK_ACTION_COPY );
219 /* Get the interface labels */
220 p_intf->p_sys->p_slider_frame = gtk_object_get_data(
221 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
222 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
223 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
224 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
225 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
228 /* Connect the date display to the slider */
229 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
230 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
231 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
233 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
234 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
235 p_intf->p_sys->f_adj_oldvalue = 0;
238 /* We don't create these ones yet because we perhaps won't need them */
239 p_intf->p_sys->p_about = NULL;
240 p_intf->p_sys->p_modules = NULL;
241 p_intf->p_sys->p_fileopen = NULL;
242 p_intf->p_sys->p_disc = NULL;
243 p_intf->p_sys->p_network = NULL;
244 p_intf->p_sys->p_preferences = NULL;
245 p_intf->p_sys->p_jump = NULL;
247 /* Store p_intf to keep an eye on it */
248 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
251 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
254 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
257 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
260 /* Show the control window */
261 gtk_widget_show( p_intf->p_sys->p_window );
263 /* Sleep to avoid using all CPU - since some interfaces needs to access
264 * keyboard events, a 100ms delay is a good compromise */
265 i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
267 /* Enter gnome mode */
270 /* Remove the timeout */
271 gtk_timeout_remove( i_timeout );
273 /* Get rid of stored callbacks so we can unload the plugin */
274 if( p_intf->p_sys->pf_gtk_callback != NULL )
276 p_intf->p_sys->pf_gtk_callback( );
277 p_intf->p_sys->pf_gtk_callback = NULL;
281 if( p_intf->p_sys->pf_gdk_callback != NULL )
283 p_intf->p_sys->pf_gdk_callback( );
284 p_intf->p_sys->pf_gdk_callback = NULL;
288 /* following functions are local */
290 /*****************************************************************************
291 * GnomeManage: manage main thread messages
292 *****************************************************************************
293 * In this function, called approx. 10 times a second, we check what the
294 * main program wanted to tell us.
295 *****************************************************************************/
296 static gint GnomeManage( gpointer p_data )
298 #define p_intf ((intf_thread_t *)p_data)
300 vlc_mutex_lock( &p_intf->change_lock );
302 /* If the "display popup" flag has changed */
303 if( p_intf->b_menu_change )
305 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
307 p_intf->p_sys->p_popup = create_intf_popup();
308 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
312 gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
313 NULL, NULL, NULL, NULL );
314 p_intf->b_menu_change = 0;
317 /* update the playlist */
318 GtkPlayListManage( p_intf );
320 if( p_intf->p_input != NULL && !p_intf->b_die )
322 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
324 /* New input or stream map change */
325 if( p_intf->p_input->stream.b_changed )
327 GtkModeManage( p_intf );
328 GtkSetupMenus( p_intf );
329 p_intf->p_sys->b_playing = 1;
332 /* Manage the slider */
333 if( p_intf->p_input->stream.b_seekable )
336 newvalue = p_intf->p_sys->p_adj->value;
338 #define p_area p_intf->p_input->stream.p_selected_area
339 /* If the user hasn't touched the slider since the last time,
340 * then the input can safely change it */
341 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
343 /* Update the value */
344 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
345 ( 100. * p_area->i_tell ) / p_area->i_size;
347 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
350 /* Otherwise, send message to the input if the user has
351 * finished dragging the slider */
352 else if( p_intf->p_sys->b_slider_free )
354 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
356 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
357 input_Seek( p_intf->p_input, i_seek );
358 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
360 /* Update the old value */
361 p_intf->p_sys->f_adj_oldvalue = newvalue;
366 if( p_intf->p_sys->i_part !=
367 p_intf->p_input->stream.p_selected_area->i_part )
369 p_intf->p_sys->b_chapter_update = 1;
370 GtkSetupMenus( p_intf );
373 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
375 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
377 GtkModeManage( p_intf );
378 p_intf->p_sys->b_playing = 0;
381 /* Manage core vlc functions through the callback */
382 p_intf->pf_manage( p_intf );
386 vlc_mutex_unlock( &p_intf->change_lock );
388 /* Prepare to die, young Skywalker */
395 vlc_mutex_unlock( &p_intf->change_lock );