1 /*****************************************************************************
2 * gnome.c : Gnome plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: gnome.c,v 1.10 2002/02/19 00:50:19 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() */
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 "gnome_callbacks.h"
46 #include "gnome_interface.h"
47 #include "gnome_support.h"
48 #include "gtk_display.h"
49 #include "gtk_common.h"
51 /*****************************************************************************
53 *****************************************************************************/
54 static void intf_getfunctions( function_list_t * p_function_list );
55 static int intf_Open ( intf_thread_t *p_intf );
56 static void intf_Close ( intf_thread_t *p_intf );
57 static void intf_Run ( intf_thread_t *p_intf );
59 static gint GnomeManage ( gpointer p_data );
61 /*****************************************************************************
62 * Building configuration tree
63 *****************************************************************************/
68 SET_DESCRIPTION( "Gnome interface module" )
70 if( getenv( "DISPLAY" ) == NULL )
72 ADD_CAPABILITY( INTF, 15 )
77 ADD_CAPABILITY( INTF, 100 )
79 ADD_SHORTCUT( "gnome" )
80 ADD_PROGRAM( "gnome-vlc" )
84 intf_getfunctions( &p_module->p_functions->intf );
87 MODULE_DEACTIVATE_START
88 MODULE_DEACTIVATE_STOP
90 /*****************************************************************************
91 * g_atexit: kludge to avoid the Gnome thread to segfault at exit
92 *****************************************************************************
93 * gtk_init() makes several calls to g_atexit() which calls atexit() to
94 * register tidying callbacks to be called at program exit. Since the Gnome
95 * plugin is likely to be unloaded at program exit, we have to export this
96 * symbol to intercept the g_atexit() calls. Talk about crude hack.
97 *****************************************************************************/
98 void g_atexit( GVoidFunc func )
100 intf_thread_t *p_intf = p_main->p_intf;
104 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
110 if( i_dummy >= MAX_ATEXIT - 1 )
112 intf_ErrMsg( "intf error: too many atexit() callbacks to register" );
116 p_intf->p_sys->pf_callback[i_dummy] = func;
117 p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
120 /*****************************************************************************
121 * Functions exported as capabilities. They are declared as static so that
122 * we don't pollute the namespace too much.
123 *****************************************************************************/
124 static void intf_getfunctions( function_list_t * p_function_list )
126 p_function_list->functions.intf.pf_open = intf_Open;
127 p_function_list->functions.intf.pf_close = intf_Close;
128 p_function_list->functions.intf.pf_run = intf_Run;
131 /*****************************************************************************
132 * intf_Open: initialize and create window
133 *****************************************************************************/
134 static int intf_Open( intf_thread_t *p_intf )
136 /* Allocate instance and initialize some members */
137 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
138 if( p_intf->p_sys == NULL )
140 intf_ErrMsg("error: %s", strerror(ENOMEM));
144 /* Initialize Gnome thread */
145 p_intf->p_sys->b_playing = 0;
146 p_intf->p_sys->b_popup_changed = 0;
147 p_intf->p_sys->b_window_changed = 0;
148 p_intf->p_sys->b_playlist_changed = 0;
150 p_intf->p_sys->i_playing = -1;
151 p_intf->p_sys->b_slider_free = 1;
153 p_intf->p_sys->pf_callback[0] = NULL;
158 /*****************************************************************************
159 * intf_Close: destroy interface window
160 *****************************************************************************/
161 static void intf_Close( intf_thread_t *p_intf )
163 /* Destroy structure */
164 free( p_intf->p_sys );
167 /*****************************************************************************
168 * intf_Run: Gnome thread
169 *****************************************************************************
170 * this part of the interface is in a separate thread so that we can call
171 * gtk_main() from within it without annoying the rest of the program.
172 * XXX: the approach may look kludgy, and probably is, but I could not find
173 * a better way to dynamically load a Gnome interface at runtime.
174 *****************************************************************************/
175 static void intf_Run( intf_thread_t *p_intf )
177 /* gnome_init needs to know the command line. We don't care, so we
178 * give it an empty one */
179 char *p_args[] = { "" };
183 /* The data types we are allowed to receive */
184 static GtkTargetEntry target_table[] =
186 { "STRING", 0, DROP_ACCEPT_STRING },
187 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
188 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
191 /* Initialize Gnome */
192 gnome_init( p_main->psz_arg0, VERSION, i_args, p_args );
194 /* Create some useful widgets that will certainly be used */
195 p_intf->p_sys->p_window = create_intf_window( );
196 p_intf->p_sys->p_popup = create_intf_popup( );
197 p_intf->p_sys->p_playlist = create_intf_playlist();
199 /* Set the title of the main window */
200 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
201 VOUT_TITLE " (Gnome 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 1, GDK_ACTION_COPY );
207 /* Accept file drops on the playlist window */
208 gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
209 p_intf->p_sys->p_playlist ), "playlist_clist") ),
210 GTK_DEST_DEFAULT_ALL, target_table,
211 1, GDK_ACTION_COPY );
213 /* Get the interface labels */
214 p_intf->p_sys->p_slider_frame = gtk_object_get_data(
215 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
216 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
217 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
218 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
219 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
222 /* Connect the date display to the slider */
223 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
224 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
225 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
227 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
228 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
229 p_intf->p_sys->f_adj_oldvalue = 0;
232 /* We don't create these ones yet because we perhaps won't need them */
233 p_intf->p_sys->p_about = NULL;
234 p_intf->p_sys->p_modules = NULL;
235 p_intf->p_sys->p_fileopen = NULL;
236 p_intf->p_sys->p_disc = NULL;
237 p_intf->p_sys->p_network = NULL;
238 p_intf->p_sys->p_preferences = NULL;
239 p_intf->p_sys->p_jump = NULL;
241 /* Store p_intf to keep an eye on it */
242 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
245 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
248 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
251 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
254 /* Show the control window */
255 gtk_widget_show( p_intf->p_sys->p_window );
257 /* Sleep to avoid using all CPU - since some interfaces needs to access
258 * keyboard events, a 100ms delay is a good compromise */
259 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
261 /* Enter gnome mode */
264 /* Remove the timeout */
265 gtk_timeout_remove( i_dummy );
267 /* Get rid of stored callbacks so we can unload the plugin */
269 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
272 p_intf->p_sys->pf_callback[i_dummy]();
276 /* following functions are local */
278 /*****************************************************************************
279 * GnomeManage: manage main thread messages
280 *****************************************************************************
281 * In this function, called approx. 10 times a second, we check what the
282 * main program wanted to tell us.
283 *****************************************************************************/
284 static gint GnomeManage( gpointer p_data )
286 #define p_intf ((intf_thread_t *)p_data)
288 vlc_mutex_lock( &p_intf->change_lock );
290 /* If the "display popup" flag has changed */
291 if( p_intf->b_menu_change )
293 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
295 p_intf->p_sys->p_popup = create_intf_popup();
296 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
300 gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
301 NULL, NULL, NULL, NULL );
302 p_intf->b_menu_change = 0;
305 /* update the playlist */
306 GtkPlayListManage( p_intf );
308 if( p_input_bank->pp_input[0] != NULL && !p_intf->b_die )
310 vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
312 if( !p_input_bank->pp_input[0]->b_die )
314 /* New input or stream map change */
315 if( p_input_bank->pp_input[0]->stream.b_changed )
317 GtkModeManage( p_intf );
318 GtkSetupMenus( p_intf );
319 p_intf->p_sys->b_playing = 1;
322 /* Manage the slider */
323 if( p_input_bank->pp_input[0]->stream.b_seekable &&
324 p_intf->p_sys->b_playing )
327 newvalue = p_intf->p_sys->p_adj->value;
329 #define p_area p_input_bank->pp_input[0]->stream.p_selected_area
330 /* If the user hasn't touched the slider since the last time,
331 * then the input can safely change it */
332 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
334 /* Update the value */
335 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
336 ( 100. * p_area->i_tell ) / p_area->i_size;
338 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
341 /* Otherwise, send message to the input if the user has
342 * finished dragging the slider */
343 else if( p_intf->p_sys->b_slider_free )
345 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
347 vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
348 input_Seek( p_input_bank->pp_input[0], i_seek );
349 vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
351 /* Update the old value */
352 p_intf->p_sys->f_adj_oldvalue = newvalue;
357 if( p_intf->p_sys->i_part !=
358 p_input_bank->pp_input[0]->stream.p_selected_area->i_part )
360 p_intf->p_sys->b_chapter_update = 1;
361 GtkSetupMenus( p_intf );
365 vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
367 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
369 GtkModeManage( p_intf );
370 p_intf->p_sys->b_playing = 0;
373 /* Manage core vlc functions through the callback */
374 p_intf->pf_manage( p_intf );
378 vlc_mutex_unlock( &p_intf->change_lock );
380 /* Prepare to die, young Skywalker */
387 vlc_mutex_unlock( &p_intf->change_lock );