1 /*****************************************************************************
2 * gnome.c : Gnome plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: gnome.c,v 1.31 2002/07/23 19:28:25 stef 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 = NULL;
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;
119 if( pp_global_data == NULL )
125 p_intf = (intf_thread_t *)*pp_global_data;
132 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
138 if( i_dummy >= MAX_ATEXIT - 1 )
140 msg_Err( p_intf, "too many atexit() callbacks to register" );
144 p_intf->p_sys->pf_callback[i_dummy] = func;
145 p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
148 /*****************************************************************************
149 * Functions exported as capabilities. They are declared as static so that
150 * we don't pollute the namespace too much.
151 *****************************************************************************/
152 static void intf_getfunctions( function_list_t * p_function_list )
154 p_function_list->functions.intf.pf_open = intf_Open;
155 p_function_list->functions.intf.pf_close = intf_Close;
156 p_function_list->functions.intf.pf_run = intf_Run;
159 /*****************************************************************************
160 * intf_Open: initialize and create window
161 *****************************************************************************/
162 static int intf_Open( intf_thread_t *p_intf )
164 /* Allocate instance and initialize some members */
165 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
166 if( p_intf->p_sys == NULL )
168 msg_Err( p_intf, "out of memory" );
172 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
174 /* Initialize Gnome thread */
175 p_intf->p_sys->b_playing = 0;
176 p_intf->p_sys->b_popup_changed = 0;
177 p_intf->p_sys->b_window_changed = 0;
178 p_intf->p_sys->b_playlist_changed = 0;
180 p_intf->p_sys->p_input = NULL;
181 p_intf->p_sys->i_playing = -1;
182 p_intf->p_sys->b_slider_free = 1;
184 p_intf->p_sys->pf_callback[0] = NULL;
186 p_intf->p_sys->i_part = 0;
191 /*****************************************************************************
192 * intf_Close: destroy interface window
193 *****************************************************************************/
194 static void intf_Close( intf_thread_t *p_intf )
196 if( p_intf->p_sys->p_input )
198 vlc_object_release( p_intf->p_sys->p_input );
201 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
203 /* Destroy structure */
204 free( p_intf->p_sys );
207 /*****************************************************************************
208 * intf_Run: Gnome thread
209 *****************************************************************************
210 * this part of the interface is in a separate thread so that we can call
211 * gtk_main() from within it without annoying the rest of the program.
212 * XXX: the approach may look kludgy, and probably is, but I could not find
213 * a better way to dynamically load a Gnome interface at runtime.
214 *****************************************************************************/
215 static void intf_Run( intf_thread_t *p_intf )
217 /* gnome_init needs to know the command line. We don't care, so we
218 * give it an empty one */
219 char *p_args[] = { "" };
223 /* The data types we are allowed to receive */
224 static GtkTargetEntry target_table[] =
226 { "STRING", 0, DROP_ACCEPT_STRING },
227 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
228 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
231 /* Initialize Gnome */
233 /* gnome_init will register stuff with g_atexit, so we need to take
234 * the global lock if we want to be able to intercept the calls */
235 vlc_mutex_lock( p_intf->p_vlc->p_global_lock );
236 *p_intf->p_vlc->pp_global_data = p_intf;
237 gnome_init( p_intf->p_vlc->psz_object_name, VERSION, i_args, p_args );
238 vlc_mutex_unlock( p_intf->p_vlc->p_global_lock );
240 /* Create some useful widgets that will certainly be used */
241 p_intf->p_sys->p_window = create_intf_window( );
242 p_intf->p_sys->p_popup = create_intf_popup( );
243 p_intf->p_sys->p_playwin = create_intf_playlist();
244 p_intf->p_sys->p_messages = create_intf_messages();
246 /* Set the title of the main window */
247 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
248 VOUT_TITLE " (Gnome interface)");
250 /* Accept file drops on the main window */
251 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
252 GTK_DEST_DEFAULT_ALL, target_table,
253 1, GDK_ACTION_COPY );
254 /* Accept file drops on the playlist window */
255 gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
256 p_intf->p_sys->p_playwin ), "playlist_clist") ),
257 GTK_DEST_DEFAULT_ALL, target_table,
258 1, GDK_ACTION_COPY );
260 /* Get the slider object */
261 p_intf->p_sys->p_slider_frame = gtk_object_get_data(
262 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
264 /* Configure the log window */
265 p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
266 GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
267 gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
268 gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
270 /* Get the interface labels */
271 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
272 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
273 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
274 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
277 /* Connect the date display to the slider */
278 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
279 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
280 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
282 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
283 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
284 p_intf->p_sys->f_adj_oldvalue = 0;
287 p_intf->p_sys->p_tooltips = gtk_tooltips_new();
289 /* We don't create these ones yet because we perhaps won't need them */
290 p_intf->p_sys->p_about = NULL;
291 p_intf->p_sys->p_modules = NULL;
292 p_intf->p_sys->p_open = NULL;
293 p_intf->p_sys->p_jump = NULL;
295 /* Hide tooltips if the option is set */
296 if( !config_GetInt( p_intf, "gnome-tooltips" ) )
298 gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
301 /* Hide toolbar text of the option is set */
302 if( !config_GetInt( p_intf, "gnome-toolbartext" ) )
304 gtk_toolbar_set_style(
305 GTK_TOOLBAR(lookup_widget( p_intf->p_sys->p_window, "toolbar" )),
309 /* Store p_intf to keep an eye on it */
310 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
313 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
316 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
319 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
322 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
325 /* Show the control window */
326 gtk_widget_show( p_intf->p_sys->p_window );
328 /* Sleep to avoid using all CPU - since some interfaces needs to access
329 * keyboard events, a 100ms delay is a good compromise */
330 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
332 /* Enter gnome mode */
335 /* Remove the timeout */
336 gtk_timeout_remove( i_dummy );
338 /* Destroy the Tooltips structure */
339 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
341 /* Get rid of stored callbacks so we can unload the plugin */
343 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
346 p_intf->p_sys->pf_callback[i_dummy]();
350 /* following functions are local */
352 /*****************************************************************************
353 * GnomeManage: manage main thread messages
354 *****************************************************************************
355 * In this function, called approx. 10 times a second, we check what the
356 * main program wanted to tell us.
357 *****************************************************************************/
358 static gint GnomeManage( gpointer p_data )
360 #define p_intf ((intf_thread_t *)p_data)
363 vlc_mutex_lock( &p_intf->change_lock );
365 /* If the "display popup" flag has changed */
366 if( p_intf->b_menu_change )
368 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
370 p_intf->p_sys->p_popup = create_intf_popup();
371 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
375 gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
376 NULL, NULL, NULL, NULL );
377 p_intf->b_menu_change = 0;
380 /* Update the log window */
381 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
382 i_stop = *p_intf->p_sys->p_sub->pi_stop;
383 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
385 if( p_intf->p_sys->p_sub->i_start != i_stop )
387 static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
388 static GdkColor gray = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
389 static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
390 static GdkColor red = { 0, 0xffff, 0x6666, 0x6666 };
392 static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
394 static GdkColor * pp_color[4] = { &white, &red, &yellow, &gray };
396 for( i_start = p_intf->p_sys->p_sub->i_start;
398 i_start = (i_start+1) % VLC_MSG_QSIZE )
400 /* Append all messages to log window */
401 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
402 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
404 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
405 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
408 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
409 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
410 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
412 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
416 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
417 p_intf->p_sys->p_sub->i_start = i_start;
418 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
420 gtk_text_set_point( p_intf->p_sys->p_messages_text,
421 gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
424 /* Update the playlist */
425 GtkPlayListManage( p_intf );
427 /* Update the input */
428 if( p_intf->p_sys->p_input != NULL )
430 if( p_intf->p_sys->p_input->b_dead )
432 vlc_object_release( p_intf->p_sys->p_input );
433 p_intf->p_sys->p_input = NULL;
437 if( p_intf->p_sys->p_input == NULL )
439 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
443 if( p_intf->p_sys->p_input )
445 input_thread_t *p_input = p_intf->p_sys->p_input;
447 vlc_mutex_lock( &p_input->stream.stream_lock );
449 if( !p_input->b_die )
451 /* New input or stream map change */
452 if( p_input->stream.b_changed )
454 GtkModeManage( p_intf );
455 GtkSetupMenus( p_intf );
456 p_intf->p_sys->b_playing = 1;
459 /* Manage the slider */
460 if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
462 float newvalue = p_intf->p_sys->p_adj->value;
464 #define p_area p_input->stream.p_selected_area
465 /* If the user hasn't touched the slider since the last time,
466 * then the input can safely change it */
467 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
469 /* Update the value */
470 p_intf->p_sys->p_adj->value =
471 p_intf->p_sys->f_adj_oldvalue =
472 ( 100. * p_area->i_tell ) / p_area->i_size;
474 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
477 /* Otherwise, send message to the input if the user has
478 * finished dragging the slider */
479 else if( p_intf->p_sys->b_slider_free )
481 if( newvalue > 0. && newvalue < 100. )
483 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
485 vlc_mutex_unlock( &p_input->stream.stream_lock );
486 input_Seek( p_input, i_seek, INPUT_SEEK_SET );
487 vlc_mutex_lock( &p_input->stream.stream_lock );
490 /* Update the old value */
491 p_intf->p_sys->f_adj_oldvalue = newvalue;
496 if( p_intf->p_sys->i_part !=
497 p_input->stream.p_selected_area->i_part )
499 p_intf->p_sys->b_chapter_update = 1;
500 GtkSetupMenus( p_intf );
504 vlc_mutex_unlock( &p_input->stream.stream_lock );
506 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
508 GtkModeManage( p_intf );
509 p_intf->p_sys->b_playing = 0;
514 vlc_mutex_unlock( &p_intf->change_lock );
516 /* Prepare to die, young Skywalker */
523 vlc_mutex_unlock( &p_intf->change_lock );