1 /*****************************************************************************
2 * gnome.c : Gnome plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: gnome.c,v 1.32 2002/07/31 20:56:51 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 int Open ( vlc_object_t * );
47 static void Close ( vlc_object_t * );
49 static void Run ( intf_thread_t * );
50 static gint Manage ( gpointer );
52 /*****************************************************************************
53 * Local variables (mutex-protected).
54 *****************************************************************************/
55 static void ** pp_global_data = NULL;
57 /*****************************************************************************
59 *****************************************************************************/
60 #define TOOLTIPS_TEXT N_("show tooltips")
61 #define TOOLTIPS_LONGTEXT N_("Show tooltips for configuration options.")
63 #define TOOLBAR_TEXT N_("show text on toolbar buttons")
64 #define TOOLBAR_LONGTEXT N_("Show the text below icons on the toolbar.")
66 #define PREFS_MAXH_TEXT N_("maximum height for the configuration windows")
67 #define PREFS_MAXH_LONGTEXT N_( \
68 "You can set the maximum height that the configuration windows in the " \
69 "preferences menu will occupy.")
75 int i = getenv( "DISPLAY" ) == NULL ? 15 : 100;
77 pp_global_data = p_module->p_vlc->pp_global_data;
79 add_category_hint( N_("Miscellaneous"), NULL );
80 add_bool( "gnome-tooltips", 1, GtkHideTooltips,
81 TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT );
82 add_bool( "gnome-toolbartext", 1, GtkHideToolbarText, TOOLBAR_TEXT,
84 add_integer( "gnome-prefs-maxh", 480, NULL,
85 PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT );
87 set_description( _("GNOME interface module") );
88 set_capability( "interface", i );
89 set_callbacks( Open, Close );
90 set_program( "gnome-vlc" );
93 /*****************************************************************************
94 * g_atexit: kludge to avoid the Gnome thread to segfault at exit
95 *****************************************************************************
96 * gtk_init() makes several calls to g_atexit() which calls atexit() to
97 * register tidying callbacks to be called at program exit. Since the Gnome
98 * plugin is likely to be unloaded at program exit, we have to export this
99 * symbol to intercept the g_atexit() calls. Talk about crude hack.
100 *****************************************************************************/
101 void g_atexit( GVoidFunc func )
103 intf_thread_t *p_intf;
107 if( pp_global_data == NULL )
113 p_intf = (intf_thread_t *)*pp_global_data;
120 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
126 if( i_dummy >= MAX_ATEXIT - 1 )
128 msg_Err( p_intf, "too many atexit() callbacks to register" );
132 p_intf->p_sys->pf_callback[i_dummy] = func;
133 p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
136 /*****************************************************************************
137 * Open: initialize and create window
138 *****************************************************************************/
139 static int Open( vlc_object_t *p_this )
141 intf_thread_t *p_intf = (intf_thread_t *)p_this;
143 /* Allocate instance and initialize some members */
144 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
145 if( p_intf->p_sys == NULL )
147 msg_Err( p_intf, "out of memory" );
151 p_intf->pf_run = Run;
153 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
155 /* Initialize Gnome thread */
156 p_intf->p_sys->b_playing = 0;
157 p_intf->p_sys->b_popup_changed = 0;
158 p_intf->p_sys->b_window_changed = 0;
159 p_intf->p_sys->b_playlist_changed = 0;
161 p_intf->p_sys->p_input = NULL;
162 p_intf->p_sys->i_playing = -1;
163 p_intf->p_sys->b_slider_free = 1;
165 p_intf->p_sys->pf_callback[0] = NULL;
167 p_intf->p_sys->i_part = 0;
172 /*****************************************************************************
173 * Close: destroy interface window
174 *****************************************************************************/
175 static void Close( vlc_object_t *p_this )
177 intf_thread_t *p_intf = (intf_thread_t *)p_this;
179 if( p_intf->p_sys->p_input )
181 vlc_object_release( p_intf->p_sys->p_input );
184 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
186 /* Destroy structure */
187 free( p_intf->p_sys );
190 /*****************************************************************************
192 *****************************************************************************
193 * this part of the interface is in a separate thread so that we can call
194 * gtk_main() from within it without annoying the rest of the program.
195 * XXX: the approach may look kludgy, and probably is, but I could not find
196 * a better way to dynamically load a Gnome interface at runtime.
197 *****************************************************************************/
198 static void Run( intf_thread_t *p_intf )
200 /* gnome_init needs to know the command line. We don't care, so we
201 * give it an empty one */
202 char *p_args[] = { "" };
206 /* The data types we are allowed to receive */
207 static GtkTargetEntry target_table[] =
209 { "STRING", 0, DROP_ACCEPT_STRING },
210 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
211 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
214 /* Initialize Gnome */
216 /* gnome_init will register stuff with g_atexit, so we need to take
217 * the global lock if we want to be able to intercept the calls */
218 vlc_mutex_lock( p_intf->p_vlc->p_global_lock );
219 *p_intf->p_vlc->pp_global_data = p_intf;
220 gnome_init( p_intf->p_vlc->psz_object_name, VERSION, i_args, p_args );
221 vlc_mutex_unlock( p_intf->p_vlc->p_global_lock );
223 /* Create some useful widgets that will certainly be used */
224 p_intf->p_sys->p_window = create_intf_window( );
225 p_intf->p_sys->p_popup = create_intf_popup( );
226 p_intf->p_sys->p_playwin = create_intf_playlist();
227 p_intf->p_sys->p_messages = create_intf_messages();
229 /* Set the title of the main window */
230 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
231 VOUT_TITLE " (Gnome interface)");
233 /* Accept file drops on the main window */
234 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
235 GTK_DEST_DEFAULT_ALL, target_table,
236 1, GDK_ACTION_COPY );
237 /* Accept file drops on the playlist window */
238 gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
239 p_intf->p_sys->p_playwin ), "playlist_clist") ),
240 GTK_DEST_DEFAULT_ALL, target_table,
241 1, GDK_ACTION_COPY );
243 /* Get the slider object */
244 p_intf->p_sys->p_slider_frame = gtk_object_get_data(
245 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
247 /* Configure the log window */
248 p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
249 GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
250 gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
251 gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
253 /* Get the interface labels */
254 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
255 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
256 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
257 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
260 /* Connect the date display to the slider */
261 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
262 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
263 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
265 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
266 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
267 p_intf->p_sys->f_adj_oldvalue = 0;
270 p_intf->p_sys->p_tooltips = gtk_tooltips_new();
272 /* We don't create these ones yet because we perhaps won't need them */
273 p_intf->p_sys->p_about = NULL;
274 p_intf->p_sys->p_modules = NULL;
275 p_intf->p_sys->p_open = NULL;
276 p_intf->p_sys->p_jump = NULL;
278 /* Hide tooltips if the option is set */
279 if( !config_GetInt( p_intf, "gnome-tooltips" ) )
281 gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
284 /* Hide toolbar text of the option is set */
285 if( !config_GetInt( p_intf, "gnome-toolbartext" ) )
287 gtk_toolbar_set_style(
288 GTK_TOOLBAR(lookup_widget( p_intf->p_sys->p_window, "toolbar" )),
292 /* Store p_intf to keep an eye on it */
293 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
296 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
299 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
302 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
305 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
308 /* Show the control window */
309 gtk_widget_show( p_intf->p_sys->p_window );
311 /* Sleep to avoid using all CPU - since some interfaces needs to access
312 * keyboard events, a 100ms delay is a good compromise */
313 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, Manage, p_intf );
315 /* Enter gnome mode */
318 /* Remove the timeout */
319 gtk_timeout_remove( i_dummy );
321 /* Destroy the Tooltips structure */
322 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
324 /* Get rid of stored callbacks so we can unload the plugin */
326 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
329 p_intf->p_sys->pf_callback[i_dummy]();
333 /* following functions are local */
335 /*****************************************************************************
336 * Manage: manage main thread messages
337 *****************************************************************************
338 * In this function, called approx. 10 times a second, we check what the
339 * main program wanted to tell us.
340 *****************************************************************************/
341 static gint Manage( gpointer p_data )
343 #define p_intf ((intf_thread_t *)p_data)
346 vlc_mutex_lock( &p_intf->change_lock );
348 /* If the "display popup" flag has changed */
349 if( p_intf->b_menu_change )
351 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
353 p_intf->p_sys->p_popup = create_intf_popup();
354 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
358 gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
359 NULL, NULL, NULL, NULL );
360 p_intf->b_menu_change = 0;
363 /* Update the log window */
364 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
365 i_stop = *p_intf->p_sys->p_sub->pi_stop;
366 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
368 if( p_intf->p_sys->p_sub->i_start != i_stop )
370 static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
371 static GdkColor gray = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
372 static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
373 static GdkColor red = { 0, 0xffff, 0x6666, 0x6666 };
375 static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
377 static GdkColor * pp_color[4] = { &white, &red, &yellow, &gray };
379 for( i_start = p_intf->p_sys->p_sub->i_start;
381 i_start = (i_start+1) % VLC_MSG_QSIZE )
383 /* Append all messages to log window */
384 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
385 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
387 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
388 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
391 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
392 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
393 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
395 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
399 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
400 p_intf->p_sys->p_sub->i_start = i_start;
401 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
403 gtk_text_set_point( p_intf->p_sys->p_messages_text,
404 gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
407 /* Update the playlist */
408 GtkPlayListManage( p_intf );
410 /* Update the input */
411 if( p_intf->p_sys->p_input != NULL )
413 if( p_intf->p_sys->p_input->b_dead )
415 vlc_object_release( p_intf->p_sys->p_input );
416 p_intf->p_sys->p_input = NULL;
420 if( p_intf->p_sys->p_input == NULL )
422 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
426 if( p_intf->p_sys->p_input )
428 input_thread_t *p_input = p_intf->p_sys->p_input;
430 vlc_mutex_lock( &p_input->stream.stream_lock );
432 if( !p_input->b_die )
434 /* New input or stream map change */
435 if( p_input->stream.b_changed )
437 GtkModeManage( p_intf );
438 GtkSetupMenus( p_intf );
439 p_intf->p_sys->b_playing = 1;
442 /* Manage the slider */
443 if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
445 float newvalue = p_intf->p_sys->p_adj->value;
447 #define p_area p_input->stream.p_selected_area
448 /* If the user hasn't touched the slider since the last time,
449 * then the input can safely change it */
450 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
452 /* Update the value */
453 p_intf->p_sys->p_adj->value =
454 p_intf->p_sys->f_adj_oldvalue =
455 ( 100. * p_area->i_tell ) / p_area->i_size;
457 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
460 /* Otherwise, send message to the input if the user has
461 * finished dragging the slider */
462 else if( p_intf->p_sys->b_slider_free )
464 if( newvalue > 0. && newvalue < 100. )
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 );
473 /* Update the old value */
474 p_intf->p_sys->f_adj_oldvalue = newvalue;
479 if( p_intf->p_sys->i_part !=
480 p_input->stream.p_selected_area->i_part )
482 p_intf->p_sys->b_chapter_update = 1;
483 GtkSetupMenus( p_intf );
487 vlc_mutex_unlock( &p_input->stream.stream_lock );
489 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
491 GtkModeManage( p_intf );
492 p_intf->p_sys->b_playing = 0;
497 vlc_mutex_unlock( &p_intf->change_lock );
499 /* Prepare to die, young Skywalker */
506 vlc_mutex_unlock( &p_intf->change_lock );