1 /*****************************************************************************
2 * gnome.c : Gnome plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: gnome.c,v 1.20 2002/05/04 02:05:03 lool 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 *****************************************************************************/
64 #define TOOLTIPS_TEXT N_("hide tooltips")
65 #define TOOLTIPS_LONGTEXT N_("Do not show tooltips for configuration options.")
66 #define TOOLBAR_TEXT N_("hide text on toolbar buttons")
67 #define TOOLBAR_LONGTEXT N_("Do not show the text below icons on the toolbar.")
69 #define PREFS_MAXH_TEXT N_("maximum height for the configuration windows")
70 #define PREFS_MAXH_LONGTEXT N_( \
71 "You can set the maximum height that the configuration windows in the " \
72 "preferences menu will occupy.")
75 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
76 ADD_BOOL ( "gnome-notooltips", GtkHideTooltips, TOOLTIPS_TEXT,
78 ADD_BOOL ( "gnome-notoolbartext", GtkHideToolbarText, TOOLBAR_TEXT,
80 ADD_INTEGER ( "gnome-prefs-maxh", 480, NULL, PREFS_MAXH_TEXT,
85 SET_DESCRIPTION( _("Gnome interface module") )
87 if( getenv( "DISPLAY" ) == NULL )
89 ADD_CAPABILITY( INTF, 15 )
94 ADD_CAPABILITY( INTF, 100 )
96 ADD_SHORTCUT( "gnome" )
97 ADD_PROGRAM( "gnome-vlc" )
100 MODULE_ACTIVATE_START
101 intf_getfunctions( &p_module->p_functions->intf );
104 MODULE_DEACTIVATE_START
105 MODULE_DEACTIVATE_STOP
107 /*****************************************************************************
108 * g_atexit: kludge to avoid the Gnome thread to segfault at exit
109 *****************************************************************************
110 * gtk_init() makes several calls to g_atexit() which calls atexit() to
111 * register tidying callbacks to be called at program exit. Since the Gnome
112 * plugin is likely to be unloaded at program exit, we have to export this
113 * symbol to intercept the g_atexit() calls. Talk about crude hack.
114 *****************************************************************************/
115 void g_atexit( GVoidFunc func )
117 intf_thread_t *p_intf = p_main->p_intf;
121 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
127 if( i_dummy >= MAX_ATEXIT - 1 )
129 intf_ErrMsg( "intf error: too many atexit() callbacks to register" );
133 p_intf->p_sys->pf_callback[i_dummy] = func;
134 p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
137 /*****************************************************************************
138 * Functions exported as capabilities. They are declared as static so that
139 * we don't pollute the namespace too much.
140 *****************************************************************************/
141 static void intf_getfunctions( function_list_t * p_function_list )
143 p_function_list->functions.intf.pf_open = intf_Open;
144 p_function_list->functions.intf.pf_close = intf_Close;
145 p_function_list->functions.intf.pf_run = intf_Run;
148 /*****************************************************************************
149 * intf_Open: initialize and create window
150 *****************************************************************************/
151 static int intf_Open( intf_thread_t *p_intf )
153 /* Allocate instance and initialize some members */
154 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
155 if( p_intf->p_sys == NULL )
157 intf_ErrMsg("error: %s", strerror(ENOMEM));
161 p_intf->p_sys->p_sub = intf_MsgSub();
163 /* Initialize Gnome thread */
164 p_intf->p_sys->b_playing = 0;
165 p_intf->p_sys->b_popup_changed = 0;
166 p_intf->p_sys->b_window_changed = 0;
167 p_intf->p_sys->b_playlist_changed = 0;
169 p_intf->p_sys->i_playing = -1;
170 p_intf->p_sys->b_slider_free = 1;
172 p_intf->p_sys->pf_callback[0] = NULL;
177 /*****************************************************************************
178 * intf_Close: destroy interface window
179 *****************************************************************************/
180 static void intf_Close( intf_thread_t *p_intf )
182 intf_MsgUnsub( p_intf->p_sys->p_sub );
184 /* Destroy structure */
185 free( p_intf->p_sys );
188 /*****************************************************************************
189 * intf_Run: Gnome 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 Gnome interface at runtime.
195 *****************************************************************************/
196 static void intf_Run( intf_thread_t *p_intf )
198 /* gnome_init needs to know the command line. We don't care, so we
199 * give it an empty one */
200 char *p_args[] = { "" };
204 /* The data types we are allowed to receive */
205 static GtkTargetEntry target_table[] =
207 { "STRING", 0, DROP_ACCEPT_STRING },
208 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
209 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
212 /* Initialize Gnome */
213 gnome_init( p_main->psz_arg0, VERSION, i_args, p_args );
215 /* Create some useful widgets that will certainly be used */
216 p_intf->p_sys->p_window = create_intf_window( );
217 p_intf->p_sys->p_popup = create_intf_popup( );
218 p_intf->p_sys->p_playlist = create_intf_playlist();
219 p_intf->p_sys->p_messages = create_intf_messages();
221 /* Set the title of the main window */
222 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
223 VOUT_TITLE " (Gnome 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 );
229 /* Accept file drops on the playlist window */
230 gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
231 p_intf->p_sys->p_playlist ), "playlist_clist") ),
232 GTK_DEST_DEFAULT_ALL, target_table,
233 1, GDK_ACTION_COPY );
235 /* Get the slider object */
236 p_intf->p_sys->p_slider_frame = gtk_object_get_data(
237 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
239 /* Configure the log window */
240 p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
241 GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
242 gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
243 gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
245 /* Get the interface labels */
246 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
247 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
248 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
249 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
252 /* Connect the date display to the slider */
253 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
254 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
255 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
257 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
258 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
259 p_intf->p_sys->f_adj_oldvalue = 0;
262 /* We don't create these ones yet because we perhaps won't need them */
263 p_intf->p_sys->p_about = NULL;
264 p_intf->p_sys->p_modules = NULL;
265 p_intf->p_sys->p_fileopen = NULL;
266 p_intf->p_sys->p_disc = NULL;
267 p_intf->p_sys->p_network = NULL;
268 p_intf->p_sys->p_sat = NULL;
269 p_intf->p_sys->p_jump = NULL;
270 p_intf->p_sys->p_tooltips = gtk_tooltips_new();
272 /* Hide tooltips if the option is set */
273 if( config_GetIntVariable( "gnome-notooltips" ) )
274 gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
276 /* Hide toolbar text of the option is set */
277 if ( config_GetIntVariable( "gnome-notoolbartext" ) )
278 gtk_toolbar_set_style(
279 GTK_TOOLBAR(lookup_widget( p_intf->p_sys->p_window, "toolbar" )),
282 /* Store p_intf to keep an eye on it */
283 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
286 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
289 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
292 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
295 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
298 /* Show the control window */
299 gtk_widget_show( p_intf->p_sys->p_window );
301 /* Sleep to avoid using all CPU - since some interfaces needs to access
302 * keyboard events, a 100ms delay is a good compromise */
303 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
305 /* Enter gnome mode */
308 /* Remove the timeout */
309 gtk_timeout_remove( i_dummy );
311 /* Destroy the Tooltips structure */
312 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
314 /* Get rid of stored callbacks so we can unload the plugin */
316 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
319 p_intf->p_sys->pf_callback[i_dummy]();
323 /* following functions are local */
325 /*****************************************************************************
326 * GnomeManage: manage main thread messages
327 *****************************************************************************
328 * In this function, called approx. 10 times a second, we check what the
329 * main program wanted to tell us.
330 *****************************************************************************/
331 static gint GnomeManage( gpointer p_data )
333 #define p_intf ((intf_thread_t *)p_data)
334 static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
335 static GdkColor red = { 0, 0xffff, 0x6666, 0x6666 };
336 static GdkColor gray = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
341 vlc_mutex_lock( &p_intf->change_lock );
343 /* If the "display popup" flag has changed */
344 if( p_intf->b_menu_change )
346 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
348 p_intf->p_sys->p_popup = create_intf_popup();
349 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
353 gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
354 NULL, NULL, NULL, NULL );
355 p_intf->b_menu_change = 0;
358 /* Update the log window */
359 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
360 i_stop = *p_intf->p_sys->p_sub->pi_stop;
361 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
363 if( p_intf->p_sys->p_sub->i_start != i_stop )
365 for( i_start = p_intf->p_sys->p_sub->i_start;
367 i_start = (i_start+1) % INTF_MSG_QSIZE )
369 /* Append all messages to log window */
370 switch( p_intf->p_sys->p_sub->p_msg[i_start].i_type )
383 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, p_color,
384 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
385 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, p_color,
389 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
390 p_intf->p_sys->p_sub->i_start = i_start;
391 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
393 gtk_text_set_point( p_intf->p_sys->p_messages_text,
394 gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
397 /* Update the playlist */
398 GtkPlayListManage( p_intf );
400 if( p_input_bank->pp_input[0] != NULL && !p_intf->b_die )
402 vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
404 if( !p_input_bank->pp_input[0]->b_die )
406 /* New input or stream map change */
407 if( p_input_bank->pp_input[0]->stream.b_changed )
409 GtkModeManage( p_intf );
410 GtkSetupMenus( p_intf );
411 p_intf->p_sys->b_playing = 1;
414 /* Manage the slider */
415 if( p_input_bank->pp_input[0]->stream.b_seekable &&
416 p_intf->p_sys->b_playing )
419 newvalue = p_intf->p_sys->p_adj->value;
421 #define p_area p_input_bank->pp_input[0]->stream.p_selected_area
422 /* If the user hasn't touched the slider since the last time,
423 * then the input can safely change it */
424 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
426 /* Update the value */
427 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
428 ( 100. * p_area->i_tell ) / p_area->i_size;
430 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
433 /* Otherwise, send message to the input if the user has
434 * finished dragging the slider */
435 else if( p_intf->p_sys->b_slider_free )
437 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
439 vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
440 input_Seek( p_input_bank->pp_input[0], i_seek );
441 vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
443 /* Update the old value */
444 p_intf->p_sys->f_adj_oldvalue = newvalue;
449 if( p_intf->p_sys->i_part !=
450 p_input_bank->pp_input[0]->stream.p_selected_area->i_part )
452 p_intf->p_sys->b_chapter_update = 1;
453 GtkSetupMenus( p_intf );
457 vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
459 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
461 GtkModeManage( p_intf );
462 p_intf->p_sys->b_playing = 0;
465 /* Manage core vlc functions through the callback */
466 p_intf->pf_manage( p_intf );
470 vlc_mutex_unlock( &p_intf->change_lock );
472 /* Prepare to die, young Skywalker */
479 vlc_mutex_unlock( &p_intf->change_lock );