1 /*****************************************************************************
2 * gnome.c : Gnome plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: gnome.c,v 1.4 2001/12/30 07:09:55 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_Probe ( probedata_t *p_data );
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 GnomeManage ( gpointer p_data );
62 /*****************************************************************************
63 * Building configuration tree
64 *****************************************************************************/
69 SET_DESCRIPTION( "Gnome interface module" )
71 if( getenv( "DISPLAY" ) == NULL )
73 ADD_CAPABILITY( INTF, 15 )
78 ADD_CAPABILITY( INTF, 100 )
81 ADD_PROGRAM( "gnome-vlc" )
85 intf_getfunctions( &p_module->p_functions->intf );
88 MODULE_DEACTIVATE_START
89 MODULE_DEACTIVATE_STOP
91 /*****************************************************************************
92 * g_atexit: kludge to avoid the Gnome thread to segfault at exit
93 *****************************************************************************
94 * gtk_init() makes several calls to g_atexit() which calls atexit() to
95 * register tidying callbacks to be called at program exit. Since the Gnome
96 * plugin is likely to be unloaded at program exit, we have to export this
97 * symbol to intercept the g_atexit() calls. Talk about crude hack.
98 *****************************************************************************/
99 void g_atexit( GVoidFunc func )
101 intf_thread_t *p_intf = p_main->p_intf;
105 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
111 if( i_dummy >= MAX_ATEXIT - 1 )
113 intf_ErrMsg( "intf error: too many atexit() callbacks to register" );
117 p_intf->p_sys->pf_callback[i_dummy] = func;
118 p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
121 /*****************************************************************************
122 * Functions exported as capabilities. They are declared as static so that
123 * we don't pollute the namespace too much.
124 *****************************************************************************/
125 static void intf_getfunctions( function_list_t * p_function_list )
127 p_function_list->pf_probe = intf_Probe;
128 p_function_list->functions.intf.pf_open = intf_Open;
129 p_function_list->functions.intf.pf_close = intf_Close;
130 p_function_list->functions.intf.pf_run = intf_Run;
133 /*****************************************************************************
134 * intf_Probe: probe the interface and return a score
135 *****************************************************************************
136 * This function tries to initialize Gnome and returns a score to the
137 * plugin manager so that it can select the best plugin.
138 *****************************************************************************/
139 static int intf_Probe( probedata_t *p_data )
142 if( getenv( "DISPLAY" ) == NULL )
151 /*****************************************************************************
152 * intf_Open: initialize and create window
153 *****************************************************************************/
154 static int intf_Open( intf_thread_t *p_intf )
156 /* Allocate instance and initialize some members */
157 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
158 if( p_intf->p_sys == NULL )
160 intf_ErrMsg("error: %s", strerror(ENOMEM));
164 /* Initialize Gnome thread */
165 p_intf->p_sys->b_playing = 1;
166 p_intf->p_sys->b_popup_changed = 0;
167 p_intf->p_sys->b_window_changed = 0;
168 p_intf->p_sys->b_playlist_changed = 0;
170 p_intf->p_sys->i_playing = -1;
171 p_intf->p_sys->b_slider_free = 1;
173 p_intf->p_sys->pf_callback[0] = NULL;
178 /*****************************************************************************
179 * intf_Close: destroy interface window
180 *****************************************************************************/
181 static void intf_Close( intf_thread_t *p_intf )
183 /* Destroy structure */
184 free( p_intf->p_sys );
187 /*****************************************************************************
188 * intf_Run: Gnome thread
189 *****************************************************************************
190 * this part of the interface is in a separate thread so that we can call
191 * gtk_main() from within it without annoying the rest of the program.
192 * XXX: the approach may look kludgy, and probably is, but I could not find
193 * a better way to dynamically load a Gnome interface at runtime.
194 *****************************************************************************/
195 static void intf_Run( intf_thread_t *p_intf )
197 /* gnome_init needs to know the command line. We don't care, so we
198 * give it an empty one */
199 char *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 Gnome */
212 gnome_init( p_main->psz_arg0, VLC_VERSION, i_args, p_args );
214 /* Create some useful widgets that will certainly be used */
215 p_intf->p_sys->p_window = create_intf_window( );
216 p_intf->p_sys->p_popup = create_intf_popup( );
217 p_intf->p_sys->p_playlist = create_intf_playlist();
219 /* Set the title of the main window */
220 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
221 VOUT_TITLE " (Gnome 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 );
227 /* Accept file drops on the playlist window */
228 gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
229 p_intf->p_sys->p_playlist ), "playlist_clist") ),
230 GTK_DEST_DEFAULT_ALL, target_table,
231 1, GDK_ACTION_COPY );
233 /* Get the interface labels */
234 p_intf->p_sys->p_slider_frame = gtk_object_get_data(
235 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
236 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
237 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
238 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
239 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
242 /* Connect the date display to the slider */
243 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
244 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
245 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
247 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
248 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
249 p_intf->p_sys->f_adj_oldvalue = 0;
252 /* We don't create these ones yet because we perhaps won't need them */
253 p_intf->p_sys->p_about = NULL;
254 p_intf->p_sys->p_modules = NULL;
255 p_intf->p_sys->p_fileopen = NULL;
256 p_intf->p_sys->p_disc = NULL;
257 p_intf->p_sys->p_network = NULL;
258 p_intf->p_sys->p_preferences = NULL;
259 p_intf->p_sys->p_jump = NULL;
261 /* Store p_intf to keep an eye on it */
262 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
265 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
268 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
271 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
274 /* Show the control window */
275 gtk_widget_show( p_intf->p_sys->p_window );
277 /* Sleep to avoid using all CPU - since some interfaces needs to access
278 * keyboard events, a 100ms delay is a good compromise */
279 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
281 /* Enter gnome mode */
284 /* Remove the timeout */
285 gtk_timeout_remove( i_dummy );
287 /* Get rid of stored callbacks so we can unload the plugin */
289 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
292 p_intf->p_sys->pf_callback[i_dummy]();
296 /* following functions are local */
298 /*****************************************************************************
299 * GnomeManage: manage main thread messages
300 *****************************************************************************
301 * In this function, called approx. 10 times a second, we check what the
302 * main program wanted to tell us.
303 *****************************************************************************/
304 static gint GnomeManage( gpointer p_data )
306 #define p_intf ((intf_thread_t *)p_data)
308 vlc_mutex_lock( &p_intf->change_lock );
310 /* If the "display popup" flag has changed */
311 if( p_intf->b_menu_change )
313 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
315 p_intf->p_sys->p_popup = create_intf_popup();
316 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
320 gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
321 NULL, NULL, NULL, NULL );
322 p_intf->b_menu_change = 0;
325 /* update the playlist */
326 GtkPlayListManage( p_intf );
328 if( p_intf->p_input != NULL && !p_intf->b_die )
330 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
332 /* New input or stream map change */
333 if( p_intf->p_input->stream.b_changed )
335 GtkModeManage( p_intf );
336 GtkSetupMenus( p_intf );
337 p_intf->p_sys->b_playing = 1;
340 /* Manage the slider */
341 if( p_intf->p_input->stream.b_seekable )
344 newvalue = p_intf->p_sys->p_adj->value;
346 #define p_area p_intf->p_input->stream.p_selected_area
347 /* If the user hasn't touched the slider since the last time,
348 * then the input can safely change it */
349 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
351 /* Update the value */
352 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
353 ( 100. * p_area->i_tell ) / p_area->i_size;
355 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
358 /* Otherwise, send message to the input if the user has
359 * finished dragging the slider */
360 else if( p_intf->p_sys->b_slider_free )
362 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
364 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
365 input_Seek( p_intf->p_input, i_seek );
366 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
368 /* Update the old value */
369 p_intf->p_sys->f_adj_oldvalue = newvalue;
374 if( p_intf->p_sys->i_part !=
375 p_intf->p_input->stream.p_selected_area->i_part )
377 p_intf->p_sys->b_chapter_update = 1;
378 GtkSetupMenus( p_intf );
381 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
383 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
385 GtkModeManage( p_intf );
386 p_intf->p_sys->b_playing = 0;
389 /* Manage core vlc functions through the callback */
390 p_intf->pf_manage( p_intf );
394 vlc_mutex_unlock( &p_intf->change_lock );
396 /* Prepare to die, young Skywalker */
403 vlc_mutex_unlock( &p_intf->change_lock );