1 /*****************************************************************************
2 * gnome.c : Gnome plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: gnome.c,v 1.25 2002/06/01 18:04:48 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() */
37 #include "gnome_callbacks.h"
38 #include "gnome_interface.h"
39 #include "gnome_support.h"
40 #include "gtk_display.h"
41 #include "gtk_common.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static void intf_getfunctions( function_list_t * p_function_list );
47 static int intf_Open ( intf_thread_t *p_intf );
48 static void intf_Close ( intf_thread_t *p_intf );
49 static void intf_Run ( intf_thread_t *p_intf );
51 static gint GnomeManage ( gpointer p_data );
53 /*****************************************************************************
54 * Local variables (mutex-protected).
55 *****************************************************************************/
56 static void ** pp_global_data;
58 /*****************************************************************************
59 * Building configuration tree
60 *****************************************************************************/
61 #define TOOLTIPS_TEXT N_("show tooltips")
62 #define TOOLTIPS_LONGTEXT N_("Show tooltips for configuration options.")
64 #define TOOLBAR_TEXT N_("show text on toolbar buttons")
65 #define TOOLBAR_LONGTEXT N_("Show the text below icons on the toolbar.")
67 #define PREFS_MAXH_TEXT N_("maximum height for the configuration windows")
68 #define PREFS_MAXH_LONGTEXT N_( \
69 "You can set the maximum height that the configuration windows in the " \
70 "preferences menu will occupy.")
73 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
74 ADD_BOOL ( "gnome-tooltips", 1, GtkHideTooltips, TOOLTIPS_TEXT,
76 ADD_BOOL ( "gnome-toolbartext", 1, GtkHideToolbarText, TOOLBAR_TEXT,
78 ADD_INTEGER( "gnome-prefs-maxh", 480, NULL, PREFS_MAXH_TEXT,
83 pp_global_data = p_module->p_vlc->pp_global_data;
84 SET_DESCRIPTION( _("Gnome interface module") )
86 if( getenv( "DISPLAY" ) == NULL )
88 ADD_CAPABILITY( INTF, 15 )
93 ADD_CAPABILITY( INTF, 100 )
95 ADD_PROGRAM( "gnome-vlc" )
99 intf_getfunctions( &p_module->p_functions->intf );
102 MODULE_DEACTIVATE_START
103 MODULE_DEACTIVATE_STOP
105 /*****************************************************************************
106 * g_atexit: kludge to avoid the Gnome thread to segfault at exit
107 *****************************************************************************
108 * gtk_init() makes several calls to g_atexit() which calls atexit() to
109 * register tidying callbacks to be called at program exit. Since the Gnome
110 * plugin is likely to be unloaded at program exit, we have to export this
111 * symbol to intercept the g_atexit() calls. Talk about crude hack.
112 *****************************************************************************/
113 void g_atexit( GVoidFunc func )
115 intf_thread_t *p_intf = (intf_thread_t *)*pp_global_data;
119 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
125 if( i_dummy >= MAX_ATEXIT - 1 )
127 msg_Err( p_intf, "too many atexit() callbacks to register" );
131 p_intf->p_sys->pf_callback[i_dummy] = func;
132 p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
135 /*****************************************************************************
136 * Functions exported as capabilities. They are declared as static so that
137 * we don't pollute the namespace too much.
138 *****************************************************************************/
139 static void intf_getfunctions( function_list_t * p_function_list )
141 p_function_list->functions.intf.pf_open = intf_Open;
142 p_function_list->functions.intf.pf_close = intf_Close;
143 p_function_list->functions.intf.pf_run = intf_Run;
146 /*****************************************************************************
147 * intf_Open: initialize and create window
148 *****************************************************************************/
149 static int intf_Open( intf_thread_t *p_intf )
151 /* Allocate instance and initialize some members */
152 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
153 if( p_intf->p_sys == NULL )
155 msg_Err( p_intf, "out of memory" );
159 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
161 /* Initialize Gnome thread */
162 p_intf->p_sys->b_playing = 0;
163 p_intf->p_sys->b_popup_changed = 0;
164 p_intf->p_sys->b_window_changed = 0;
165 p_intf->p_sys->b_playlist_changed = 0;
167 p_intf->p_sys->p_input = NULL;
168 p_intf->p_sys->i_playing = -1;
169 p_intf->p_sys->b_slider_free = 1;
171 p_intf->p_sys->pf_callback[0] = NULL;
173 p_intf->p_sys->i_part = 0;
178 /*****************************************************************************
179 * intf_Close: destroy interface window
180 *****************************************************************************/
181 static void intf_Close( intf_thread_t *p_intf )
183 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
185 /* Destroy structure */
186 free( p_intf->p_sys );
189 /*****************************************************************************
190 * intf_Run: Gnome thread
191 *****************************************************************************
192 * this part of the interface is in a separate thread so that we can call
193 * gtk_main() from within it without annoying the rest of the program.
194 * XXX: the approach may look kludgy, and probably is, but I could not find
195 * a better way to dynamically load a Gnome interface at runtime.
196 *****************************************************************************/
197 static void intf_Run( intf_thread_t *p_intf )
199 /* gnome_init needs to know the command line. We don't care, so we
200 * give it an empty one */
201 char *p_args[] = { "" };
205 /* The data types we are allowed to receive */
206 static GtkTargetEntry target_table[] =
208 { "STRING", 0, DROP_ACCEPT_STRING },
209 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
210 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
213 /* Initialize Gnome */
215 /* gnome_init will register stuff with g_atexit, so we need to take
216 * the global lock if we want to be able to intercept the calls */
217 vlc_mutex_lock( p_intf->p_vlc->p_global_lock );
218 *p_intf->p_vlc->pp_global_data = p_intf;
219 gnome_init( p_intf->p_vlc->psz_object_name, VERSION, i_args, p_args );
220 vlc_mutex_unlock( p_intf->p_vlc->p_global_lock );
222 /* Create some useful widgets that will certainly be used */
223 p_intf->p_sys->p_window = create_intf_window( );
224 p_intf->p_sys->p_popup = create_intf_popup( );
225 p_intf->p_sys->p_playlist = create_intf_playlist();
226 p_intf->p_sys->p_messages = create_intf_messages();
228 /* Set the title of the main window */
229 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
230 VOUT_TITLE " (Gnome interface)");
232 /* Accept file drops on the main window */
233 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
234 GTK_DEST_DEFAULT_ALL, target_table,
235 1, GDK_ACTION_COPY );
236 /* Accept file drops on the playlist window */
237 gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
238 p_intf->p_sys->p_playlist ), "playlist_clist") ),
239 GTK_DEST_DEFAULT_ALL, target_table,
240 1, GDK_ACTION_COPY );
242 /* Get the slider object */
243 p_intf->p_sys->p_slider_frame = gtk_object_get_data(
244 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
246 /* Configure the log window */
247 p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
248 GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
249 gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
250 gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
252 /* Get the interface labels */
253 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
254 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
255 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
256 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
259 /* Connect the date display to the slider */
260 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
261 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
262 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
264 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
265 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
266 p_intf->p_sys->f_adj_oldvalue = 0;
269 p_intf->p_sys->p_tooltips = gtk_tooltips_new();
271 /* We don't create these ones yet because we perhaps won't need them */
272 p_intf->p_sys->p_about = NULL;
273 p_intf->p_sys->p_modules = NULL;
274 p_intf->p_sys->p_fileopen = NULL;
275 p_intf->p_sys->p_disc = NULL;
276 p_intf->p_sys->p_network = NULL;
277 p_intf->p_sys->p_sat = NULL;
278 p_intf->p_sys->p_jump = NULL;
280 /* Hide tooltips if the option is set */
281 if( !config_GetInt( p_intf, "gnome-tooltips" ) )
283 gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
286 /* Hide toolbar text of the option is set */
287 if( !config_GetInt( p_intf, "gnome-toolbartext" ) )
289 gtk_toolbar_set_style(
290 GTK_TOOLBAR(lookup_widget( p_intf->p_sys->p_window, "toolbar" )),
294 /* Store p_intf to keep an eye on it */
295 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
298 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
301 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
304 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
307 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
310 /* Show the control window */
311 gtk_widget_show( p_intf->p_sys->p_window );
313 /* Sleep to avoid using all CPU - since some interfaces needs to access
314 * keyboard events, a 100ms delay is a good compromise */
315 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
317 /* Enter gnome mode */
320 /* Remove the timeout */
321 gtk_timeout_remove( i_dummy );
323 /* Destroy the Tooltips structure */
324 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
326 /* Get rid of stored callbacks so we can unload the plugin */
328 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
331 p_intf->p_sys->pf_callback[i_dummy]();
335 /* following functions are local */
337 /*****************************************************************************
338 * GnomeManage: manage main thread messages
339 *****************************************************************************
340 * In this function, called approx. 10 times a second, we check what the
341 * main program wanted to tell us.
342 *****************************************************************************/
343 static gint GnomeManage( gpointer p_data )
345 #define p_intf ((intf_thread_t *)p_data)
348 vlc_mutex_lock( &p_intf->change_lock );
350 /* If the "display popup" flag has changed */
351 if( p_intf->b_menu_change )
353 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
355 p_intf->p_sys->p_popup = create_intf_popup();
356 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
360 gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
361 NULL, NULL, NULL, NULL );
362 p_intf->b_menu_change = 0;
365 /* Update the log window */
366 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
367 i_stop = *p_intf->p_sys->p_sub->pi_stop;
368 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
370 if( p_intf->p_sys->p_sub->i_start != i_stop )
372 static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
373 static GdkColor gray = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
374 static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
375 static GdkColor red = { 0, 0xffff, 0x6666, 0x6666 };
377 static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
379 static GdkColor * pp_color[4] = { &white, &red, &yellow, &gray };
381 for( i_start = p_intf->p_sys->p_sub->i_start;
383 i_start = (i_start+1) % VLC_MSG_QSIZE )
385 /* Append all messages to log window */
386 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
387 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
389 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
390 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
393 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
394 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
395 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
397 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
401 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
402 p_intf->p_sys->p_sub->i_start = i_start;
403 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
405 gtk_text_set_point( p_intf->p_sys->p_messages_text,
406 gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
409 /* Update the playlist */
410 GtkPlayListManage( p_intf );
412 /* Update the input */
413 if( p_intf->p_sys->p_input != NULL )
415 if( p_intf->p_sys->p_input->b_dead )
417 vlc_object_release( p_intf->p_sys->p_input );
418 p_intf->p_sys->p_input = NULL;
422 if( p_intf->p_sys->p_input == NULL )
424 p_intf->p_sys->p_input = vlc_object_find( p_intf->p_vlc,
425 VLC_OBJECT_INPUT, FIND_CHILD );
428 if( p_intf->p_sys->p_input )
430 input_thread_t *p_input = p_intf->p_sys->p_input;
432 vlc_mutex_lock( &p_input->stream.stream_lock );
434 if( !p_input->b_die )
436 /* New input or stream map change */
437 if( p_input->stream.b_changed )
439 GtkModeManage( p_intf );
440 GtkSetupMenus( p_intf );
441 p_intf->p_sys->b_playing = 1;
444 /* Manage the slider */
445 if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
447 float newvalue = p_intf->p_sys->p_adj->value;
449 #define p_area p_input->stream.p_selected_area
450 /* If the user hasn't touched the slider since the last time,
451 * then the input can safely change it */
452 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
454 /* Update the value */
455 p_intf->p_sys->p_adj->value =
456 p_intf->p_sys->f_adj_oldvalue =
457 ( 100. * p_area->i_tell ) / p_area->i_size;
459 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
462 /* Otherwise, send message to the input if the user has
463 * finished dragging the slider */
464 else if( p_intf->p_sys->b_slider_free )
466 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
468 vlc_mutex_unlock( &p_input->stream.stream_lock );
469 input_Seek( p_input, i_seek, INPUT_SEEK_SET );
470 vlc_mutex_lock( &p_input->stream.stream_lock );
472 /* Update the old value */
473 p_intf->p_sys->f_adj_oldvalue = newvalue;
478 if( p_intf->p_sys->i_part !=
479 p_input->stream.p_selected_area->i_part )
481 p_intf->p_sys->b_chapter_update = 1;
482 GtkSetupMenus( p_intf );
486 vlc_mutex_unlock( &p_input->stream.stream_lock );
488 else if( p_intf->p_sys->b_playing && !p_intf->p_vlc->b_die )
490 GtkModeManage( p_intf );
491 p_intf->p_sys->b_playing = 0;
494 if( p_intf->p_vlc->b_die )
496 vlc_mutex_unlock( &p_intf->change_lock );
498 /* Prepare to die, young Skywalker */
505 vlc_mutex_unlock( &p_intf->change_lock );