1 /*****************************************************************************
2 * gnome.c : Gnome plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: gnome.c,v 1.13 2002/03/25 02:06:24 jobi 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 p_intf->p_sys->p_sub = intf_MsgSub();
146 /* Initialize Gnome thread */
147 p_intf->p_sys->b_playing = 0;
148 p_intf->p_sys->b_popup_changed = 0;
149 p_intf->p_sys->b_window_changed = 0;
150 p_intf->p_sys->b_playlist_changed = 0;
152 p_intf->p_sys->i_playing = -1;
153 p_intf->p_sys->b_slider_free = 1;
155 p_intf->p_sys->pf_callback[0] = NULL;
160 /*****************************************************************************
161 * intf_Close: destroy interface window
162 *****************************************************************************/
163 static void intf_Close( intf_thread_t *p_intf )
165 intf_MsgUnsub( p_intf->p_sys->p_sub );
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[] = { "" };
187 /* The data types we are allowed to receive */
188 static GtkTargetEntry target_table[] =
190 { "STRING", 0, DROP_ACCEPT_STRING },
191 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
192 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
195 /* Initialize Gnome */
196 gnome_init( p_main->psz_arg0, VERSION, i_args, p_args );
198 /* Create some useful widgets that will certainly be used */
199 p_intf->p_sys->p_window = create_intf_window( );
200 p_intf->p_sys->p_popup = create_intf_popup( );
201 p_intf->p_sys->p_playlist = create_intf_playlist();
202 p_intf->p_sys->p_messages = create_intf_messages();
204 /* Set the title of the main window */
205 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
206 VOUT_TITLE " (Gnome interface)");
208 /* Accept file drops on the main window */
209 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
210 GTK_DEST_DEFAULT_ALL, target_table,
211 1, GDK_ACTION_COPY );
212 /* Accept file drops on the playlist window */
213 gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
214 p_intf->p_sys->p_playlist ), "playlist_clist") ),
215 GTK_DEST_DEFAULT_ALL, target_table,
216 1, GDK_ACTION_COPY );
218 /* Get the slider object */
219 p_intf->p_sys->p_slider_frame = gtk_object_get_data(
220 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
222 /* Configure the log window */
223 p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
224 GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
225 gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
226 gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
228 /* Get the interface labels */
229 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
230 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
231 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
232 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
235 /* Connect the date display to the slider */
236 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
237 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
238 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
240 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
241 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
242 p_intf->p_sys->f_adj_oldvalue = 0;
245 /* We don't create these ones yet because we perhaps won't need them */
246 p_intf->p_sys->p_about = NULL;
247 p_intf->p_sys->p_modules = NULL;
248 p_intf->p_sys->p_fileopen = NULL;
249 p_intf->p_sys->p_disc = NULL;
250 p_intf->p_sys->p_network = NULL;
251 p_intf->p_sys->p_sat = NULL;
252 p_intf->p_sys->p_jump = NULL;
254 /* Store p_intf to keep an eye on it */
255 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
258 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
261 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
264 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
267 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
270 /* Show the control window */
271 gtk_widget_show( p_intf->p_sys->p_window );
273 /* Sleep to avoid using all CPU - since some interfaces needs to access
274 * keyboard events, a 100ms delay is a good compromise */
275 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
277 /* Enter gnome mode */
280 /* Remove the timeout */
281 gtk_timeout_remove( i_dummy );
283 /* Get rid of stored callbacks so we can unload the plugin */
285 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
288 p_intf->p_sys->pf_callback[i_dummy]();
292 /* following functions are local */
294 /*****************************************************************************
295 * GnomeManage: manage main thread messages
296 *****************************************************************************
297 * In this function, called approx. 10 times a second, we check what the
298 * main program wanted to tell us.
299 *****************************************************************************/
300 static gint GnomeManage( gpointer p_data )
302 #define p_intf ((intf_thread_t *)p_data)
303 static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
304 static GdkColor red = { 0, 0xffff, 0x6666, 0x6666 };
305 static GdkColor gray = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
310 vlc_mutex_lock( &p_intf->change_lock );
312 /* If the "display popup" flag has changed */
313 if( p_intf->b_menu_change )
315 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
317 p_intf->p_sys->p_popup = create_intf_popup();
318 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
322 gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
323 NULL, NULL, NULL, NULL );
324 p_intf->b_menu_change = 0;
327 /* Update the log window */
328 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
329 i_stop = *p_intf->p_sys->p_sub->pi_stop;
330 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
332 if( p_intf->p_sys->p_sub->i_start != i_stop )
334 for( i_start = p_intf->p_sys->p_sub->i_start;
336 i_start = (i_start+1) % INTF_MSG_QSIZE )
338 /* Append all messages to log window */
339 switch( p_intf->p_sys->p_sub->p_msg[i_start].i_type )
352 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, p_color,
353 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
354 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, p_color,
358 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
359 p_intf->p_sys->p_sub->i_start = i_start;
360 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
362 gtk_text_set_point( p_intf->p_sys->p_messages_text,
363 gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
366 /* Update the playlist */
367 GtkPlayListManage( p_intf );
369 if( p_input_bank->pp_input[0] != NULL && !p_intf->b_die )
371 vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
373 if( !p_input_bank->pp_input[0]->b_die )
375 /* New input or stream map change */
376 if( p_input_bank->pp_input[0]->stream.b_changed )
378 GtkModeManage( p_intf );
379 GtkSetupMenus( p_intf );
380 p_intf->p_sys->b_playing = 1;
383 /* Manage the slider */
384 if( p_input_bank->pp_input[0]->stream.b_seekable &&
385 p_intf->p_sys->b_playing )
388 newvalue = p_intf->p_sys->p_adj->value;
390 #define p_area p_input_bank->pp_input[0]->stream.p_selected_area
391 /* If the user hasn't touched the slider since the last time,
392 * then the input can safely change it */
393 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
395 /* Update the value */
396 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
397 ( 100. * p_area->i_tell ) / p_area->i_size;
399 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
402 /* Otherwise, send message to the input if the user has
403 * finished dragging the slider */
404 else if( p_intf->p_sys->b_slider_free )
406 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
408 vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
409 input_Seek( p_input_bank->pp_input[0], i_seek );
410 vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
412 /* Update the old value */
413 p_intf->p_sys->f_adj_oldvalue = newvalue;
418 if( p_intf->p_sys->i_part !=
419 p_input_bank->pp_input[0]->stream.p_selected_area->i_part )
421 p_intf->p_sys->b_chapter_update = 1;
422 GtkSetupMenus( p_intf );
426 vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
428 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
430 GtkModeManage( p_intf );
431 p_intf->p_sys->b_playing = 0;
434 /* Manage core vlc functions through the callback */
435 p_intf->pf_manage( p_intf );
439 vlc_mutex_unlock( &p_intf->change_lock );
441 /* Prepare to die, young Skywalker */
448 vlc_mutex_unlock( &p_intf->change_lock );