1 /*****************************************************************************
2 * gnome.c : Gnome plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: gnome.c,v 1.18 2002/04/23 14:16:20 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() */
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.")
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-notooltips", NULL, TOOLTIPS_TEXT,
76 ADD_INTEGER ( "gnome-prefs-maxh", 480, NULL, PREFS_MAXH_TEXT,
81 SET_DESCRIPTION( _("Gnome interface module") )
83 if( getenv( "DISPLAY" ) == NULL )
85 ADD_CAPABILITY( INTF, 15 )
90 ADD_CAPABILITY( INTF, 100 )
92 ADD_SHORTCUT( "gnome" )
93 ADD_PROGRAM( "gnome-vlc" )
97 intf_getfunctions( &p_module->p_functions->intf );
100 MODULE_DEACTIVATE_START
101 MODULE_DEACTIVATE_STOP
103 /*****************************************************************************
104 * g_atexit: kludge to avoid the Gnome thread to segfault at exit
105 *****************************************************************************
106 * gtk_init() makes several calls to g_atexit() which calls atexit() to
107 * register tidying callbacks to be called at program exit. Since the Gnome
108 * plugin is likely to be unloaded at program exit, we have to export this
109 * symbol to intercept the g_atexit() calls. Talk about crude hack.
110 *****************************************************************************/
111 void g_atexit( GVoidFunc func )
113 intf_thread_t *p_intf = p_main->p_intf;
117 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
123 if( i_dummy >= MAX_ATEXIT - 1 )
125 intf_ErrMsg( "intf error: too many atexit() callbacks to register" );
129 p_intf->p_sys->pf_callback[i_dummy] = func;
130 p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
133 /*****************************************************************************
134 * Functions exported as capabilities. They are declared as static so that
135 * we don't pollute the namespace too much.
136 *****************************************************************************/
137 static void intf_getfunctions( function_list_t * p_function_list )
139 p_function_list->functions.intf.pf_open = intf_Open;
140 p_function_list->functions.intf.pf_close = intf_Close;
141 p_function_list->functions.intf.pf_run = intf_Run;
144 /*****************************************************************************
145 * intf_Open: initialize and create window
146 *****************************************************************************/
147 static int intf_Open( intf_thread_t *p_intf )
149 /* Allocate instance and initialize some members */
150 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
151 if( p_intf->p_sys == NULL )
153 intf_ErrMsg("error: %s", strerror(ENOMEM));
157 p_intf->p_sys->p_sub = intf_MsgSub();
159 /* Initialize Gnome thread */
160 p_intf->p_sys->b_playing = 0;
161 p_intf->p_sys->b_popup_changed = 0;
162 p_intf->p_sys->b_window_changed = 0;
163 p_intf->p_sys->b_playlist_changed = 0;
165 p_intf->p_sys->i_playing = -1;
166 p_intf->p_sys->b_slider_free = 1;
168 p_intf->p_sys->pf_callback[0] = NULL;
173 /*****************************************************************************
174 * intf_Close: destroy interface window
175 *****************************************************************************/
176 static void intf_Close( intf_thread_t *p_intf )
178 intf_MsgUnsub( p_intf->p_sys->p_sub );
180 /* Destroy structure */
181 free( p_intf->p_sys );
184 /*****************************************************************************
185 * intf_Run: Gnome thread
186 *****************************************************************************
187 * this part of the interface is in a separate thread so that we can call
188 * gtk_main() from within it without annoying the rest of the program.
189 * XXX: the approach may look kludgy, and probably is, but I could not find
190 * a better way to dynamically load a Gnome interface at runtime.
191 *****************************************************************************/
192 static void intf_Run( intf_thread_t *p_intf )
194 /* gnome_init needs to know the command line. We don't care, so we
195 * give it an empty one */
196 char *p_args[] = { "" };
200 /* The data types we are allowed to receive */
201 static GtkTargetEntry target_table[] =
203 { "STRING", 0, DROP_ACCEPT_STRING },
204 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
205 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
208 /* Initialize Gnome */
209 gnome_init( p_main->psz_arg0, VERSION, i_args, p_args );
211 /* Create some useful widgets that will certainly be used */
212 p_intf->p_sys->p_window = create_intf_window( );
213 p_intf->p_sys->p_popup = create_intf_popup( );
214 p_intf->p_sys->p_playlist = create_intf_playlist();
215 p_intf->p_sys->p_messages = create_intf_messages();
217 /* Set the title of the main window */
218 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
219 VOUT_TITLE " (Gnome interface)");
221 /* Accept file drops on the main window */
222 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
223 GTK_DEST_DEFAULT_ALL, target_table,
224 1, GDK_ACTION_COPY );
225 /* Accept file drops on the playlist window */
226 gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
227 p_intf->p_sys->p_playlist ), "playlist_clist") ),
228 GTK_DEST_DEFAULT_ALL, target_table,
229 1, GDK_ACTION_COPY );
231 /* Get the slider object */
232 p_intf->p_sys->p_slider_frame = gtk_object_get_data(
233 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
235 /* Configure the log window */
236 p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
237 GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
238 gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
239 gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
241 /* Get the interface labels */
242 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
243 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
244 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
245 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
248 /* Connect the date display to the slider */
249 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
250 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
251 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
253 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
254 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
255 p_intf->p_sys->f_adj_oldvalue = 0;
258 /* We don't create these ones yet because we perhaps won't need them */
259 p_intf->p_sys->p_about = NULL;
260 p_intf->p_sys->p_modules = NULL;
261 p_intf->p_sys->p_fileopen = NULL;
262 p_intf->p_sys->p_disc = NULL;
263 p_intf->p_sys->p_network = NULL;
264 p_intf->p_sys->p_sat = NULL;
265 p_intf->p_sys->p_jump = NULL;
266 p_intf->p_sys->p_tooltips = gtk_tooltips_new();
268 /* Hide tooltips if the option is set */
269 if( config_GetIntVariable( "gnome-notooltips" ) )
270 gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
272 /* Store p_intf to keep an eye on it */
273 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
276 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
279 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
282 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
285 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
288 /* Show the control window */
289 gtk_widget_show( p_intf->p_sys->p_window );
291 /* Sleep to avoid using all CPU - since some interfaces needs to access
292 * keyboard events, a 100ms delay is a good compromise */
293 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
295 /* Enter gnome mode */
298 /* Remove the timeout */
299 gtk_timeout_remove( i_dummy );
301 /* Destroy the Tooltips structure */
302 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
304 /* Get rid of stored callbacks so we can unload the plugin */
306 i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
309 p_intf->p_sys->pf_callback[i_dummy]();
313 /* following functions are local */
315 /*****************************************************************************
316 * GnomeManage: manage main thread messages
317 *****************************************************************************
318 * In this function, called approx. 10 times a second, we check what the
319 * main program wanted to tell us.
320 *****************************************************************************/
321 static gint GnomeManage( gpointer p_data )
323 #define p_intf ((intf_thread_t *)p_data)
324 static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
325 static GdkColor red = { 0, 0xffff, 0x6666, 0x6666 };
326 static GdkColor gray = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
331 vlc_mutex_lock( &p_intf->change_lock );
333 /* If the "display popup" flag has changed */
334 if( p_intf->b_menu_change )
336 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
338 p_intf->p_sys->p_popup = create_intf_popup();
339 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
343 gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
344 NULL, NULL, NULL, NULL );
345 p_intf->b_menu_change = 0;
348 /* Update the log window */
349 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
350 i_stop = *p_intf->p_sys->p_sub->pi_stop;
351 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
353 if( p_intf->p_sys->p_sub->i_start != i_stop )
355 for( i_start = p_intf->p_sys->p_sub->i_start;
357 i_start = (i_start+1) % INTF_MSG_QSIZE )
359 /* Append all messages to log window */
360 switch( p_intf->p_sys->p_sub->p_msg[i_start].i_type )
373 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, p_color,
374 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
375 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, p_color,
379 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
380 p_intf->p_sys->p_sub->i_start = i_start;
381 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
383 gtk_text_set_point( p_intf->p_sys->p_messages_text,
384 gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
387 /* Update the playlist */
388 GtkPlayListManage( p_intf );
390 if( p_input_bank->pp_input[0] != NULL && !p_intf->b_die )
392 vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
394 if( !p_input_bank->pp_input[0]->b_die )
396 /* New input or stream map change */
397 if( p_input_bank->pp_input[0]->stream.b_changed )
399 GtkModeManage( p_intf );
400 GtkSetupMenus( p_intf );
401 p_intf->p_sys->b_playing = 1;
404 /* Manage the slider */
405 if( p_input_bank->pp_input[0]->stream.b_seekable &&
406 p_intf->p_sys->b_playing )
409 newvalue = p_intf->p_sys->p_adj->value;
411 #define p_area p_input_bank->pp_input[0]->stream.p_selected_area
412 /* If the user hasn't touched the slider since the last time,
413 * then the input can safely change it */
414 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
416 /* Update the value */
417 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
418 ( 100. * p_area->i_tell ) / p_area->i_size;
420 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
423 /* Otherwise, send message to the input if the user has
424 * finished dragging the slider */
425 else if( p_intf->p_sys->b_slider_free )
427 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
429 vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
430 input_Seek( p_input_bank->pp_input[0], i_seek );
431 vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
433 /* Update the old value */
434 p_intf->p_sys->f_adj_oldvalue = newvalue;
439 if( p_intf->p_sys->i_part !=
440 p_input_bank->pp_input[0]->stream.p_selected_area->i_part )
442 p_intf->p_sys->b_chapter_update = 1;
443 GtkSetupMenus( p_intf );
447 vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
449 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
451 GtkModeManage( p_intf );
452 p_intf->p_sys->b_playing = 0;
455 /* Manage core vlc functions through the callback */
456 p_intf->pf_manage( p_intf );
460 vlc_mutex_unlock( &p_intf->change_lock );
462 /* Prepare to die, young Skywalker */
469 vlc_mutex_unlock( &p_intf->change_lock );