]> git.sesse.net Git - vlc/blob - plugins/gtk/gnome.c
* Got rid of TRACE and intf_DbgMsg which were seldom used anyway.
[vlc] / plugins / gtk / gnome.c
1 /*****************************************************************************
2  * gnome.c : Gnome plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id: gnome.c,v 1.10 2002/02/19 00:50:19 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *      
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <videolan/vlc.h>
33
34 #include <gnome.h>
35
36 #include "stream_control.h"
37 #include "input_ext-intf.h"
38
39 #include "interface.h"
40 #include "intf_playlist.h"
41
42 #include "video.h"
43 #include "video_output.h"
44
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"
50
51 /*****************************************************************************
52  * Local prototypes.
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 );
58
59 static gint GnomeManage      ( gpointer p_data );
60
61 /*****************************************************************************
62  * Building configuration tree
63  *****************************************************************************/
64 MODULE_CONFIG_START
65 MODULE_CONFIG_STOP
66
67 MODULE_INIT_START
68     SET_DESCRIPTION( "Gnome interface module" )
69 #ifndef WIN32
70     if( getenv( "DISPLAY" ) == NULL )
71     {
72         ADD_CAPABILITY( INTF, 15 )
73     }
74     else
75 #endif
76     {
77         ADD_CAPABILITY( INTF, 100 )
78     }
79     ADD_SHORTCUT( "gnome" )
80     ADD_PROGRAM( "gnome-vlc" )
81 MODULE_INIT_STOP
82
83 MODULE_ACTIVATE_START
84     intf_getfunctions( &p_module->p_functions->intf );
85 MODULE_ACTIVATE_STOP
86
87 MODULE_DEACTIVATE_START
88 MODULE_DEACTIVATE_STOP
89
90 /*****************************************************************************
91  * g_atexit: kludge to avoid the Gnome thread to segfault at exit
92  *****************************************************************************
93  * gtk_init() makes several calls to g_atexit() which calls atexit() to
94  * register tidying callbacks to be called at program exit. Since the Gnome
95  * plugin is likely to be unloaded at program exit, we have to export this
96  * symbol to intercept the g_atexit() calls. Talk about crude hack.
97  *****************************************************************************/
98 void g_atexit( GVoidFunc func )
99 {
100     intf_thread_t *p_intf = p_main->p_intf;
101     int i_dummy;
102
103     for( i_dummy = 0;
104          i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
105          i_dummy++ )
106     {
107         ;
108     }
109
110     if( i_dummy >= MAX_ATEXIT - 1 )
111     {
112         intf_ErrMsg( "intf error: too many atexit() callbacks to register" );
113         return;
114     }
115
116     p_intf->p_sys->pf_callback[i_dummy]     = func;
117     p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
118 }
119
120 /*****************************************************************************
121  * Functions exported as capabilities. They are declared as static so that
122  * we don't pollute the namespace too much.
123  *****************************************************************************/
124 static void intf_getfunctions( function_list_t * p_function_list )
125 {
126     p_function_list->functions.intf.pf_open  = intf_Open;
127     p_function_list->functions.intf.pf_close = intf_Close;
128     p_function_list->functions.intf.pf_run   = intf_Run;
129 }
130
131 /*****************************************************************************
132  * intf_Open: initialize and create window
133  *****************************************************************************/
134 static int intf_Open( intf_thread_t *p_intf )
135 {
136     /* Allocate instance and initialize some members */
137     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
138     if( p_intf->p_sys == NULL )
139     {
140         intf_ErrMsg("error: %s", strerror(ENOMEM));
141         return( 1 );
142     }
143
144     /* Initialize Gnome thread */
145     p_intf->p_sys->b_playing = 0;
146     p_intf->p_sys->b_popup_changed = 0;
147     p_intf->p_sys->b_window_changed = 0;
148     p_intf->p_sys->b_playlist_changed = 0;
149
150     p_intf->p_sys->i_playing = -1;
151     p_intf->p_sys->b_slider_free = 1;
152
153     p_intf->p_sys->pf_callback[0] = NULL;
154
155     return( 0 );
156 }
157
158 /*****************************************************************************
159  * intf_Close: destroy interface window
160  *****************************************************************************/
161 static void intf_Close( intf_thread_t *p_intf )
162 {
163     /* Destroy structure */
164     free( p_intf->p_sys );
165 }
166
167 /*****************************************************************************
168  * intf_Run: Gnome thread
169  *****************************************************************************
170  * this part of the interface is in a separate thread so that we can call
171  * gtk_main() from within it without annoying the rest of the program.
172  * XXX: the approach may look kludgy, and probably is, but I could not find
173  * a better way to dynamically load a Gnome interface at runtime.
174  *****************************************************************************/
175 static void intf_Run( intf_thread_t *p_intf )
176 {
177     /* gnome_init needs to know the command line. We don't care, so we
178      * give it an empty one */
179     char *p_args[] = { "" };
180     int   i_args   = 1;
181     int   i_dummy;
182
183     /* The data types we are allowed to receive */
184     static GtkTargetEntry target_table[] =
185     {
186         { "STRING", 0, DROP_ACCEPT_STRING },
187         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
188         { "text/plain",    0, DROP_ACCEPT_TEXT_PLAIN }
189     };
190
191     /* Initialize Gnome */
192     gnome_init( p_main->psz_arg0, VERSION, i_args, p_args );
193
194     /* Create some useful widgets that will certainly be used */
195     p_intf->p_sys->p_window = create_intf_window( );
196     p_intf->p_sys->p_popup = create_intf_popup( );
197     p_intf->p_sys->p_playlist = create_intf_playlist();
198
199     /* Set the title of the main window */
200     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
201                           VOUT_TITLE " (Gnome interface)");
202
203     /* Accept file drops on the main window */
204     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
205                        GTK_DEST_DEFAULT_ALL, target_table,
206                        1, GDK_ACTION_COPY );
207     /* Accept file drops on the playlist window */
208     gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
209                             p_intf->p_sys->p_playlist ), "playlist_clist") ),
210                        GTK_DEST_DEFAULT_ALL, target_table,
211                        1, GDK_ACTION_COPY );
212
213     /* Get the interface labels */
214     p_intf->p_sys->p_slider_frame = gtk_object_get_data(
215                       GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
216     #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
217                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
218     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
219     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
220     #undef P_LABEL
221
222     /* Connect the date display to the slider */
223     #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
224                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
225     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
226
227     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
228                          GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
229     p_intf->p_sys->f_adj_oldvalue = 0;
230     #undef P_SLIDER
231
232     /* We don't create these ones yet because we perhaps won't need them */
233     p_intf->p_sys->p_about = NULL;
234     p_intf->p_sys->p_modules = NULL;
235     p_intf->p_sys->p_fileopen = NULL;
236     p_intf->p_sys->p_disc = NULL;
237     p_intf->p_sys->p_network = NULL;
238     p_intf->p_sys->p_preferences = NULL;
239     p_intf->p_sys->p_jump = NULL;
240
241     /* Store p_intf to keep an eye on it */
242     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
243                          "p_intf", p_intf );
244
245     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
246                          "p_intf", p_intf );
247
248     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
249                          "p_intf", p_intf );
250
251     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
252                          "p_intf", p_intf );
253
254     /* Show the control window */
255     gtk_widget_show( p_intf->p_sys->p_window );
256
257     /* Sleep to avoid using all CPU - since some interfaces needs to access
258      * keyboard events, a 100ms delay is a good compromise */
259     i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
260
261     /* Enter gnome mode */
262     gtk_main();
263
264     /* Remove the timeout */
265     gtk_timeout_remove( i_dummy );
266
267     /* Get rid of stored callbacks so we can unload the plugin */
268     for( i_dummy = 0;
269          i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
270          i_dummy++ )
271     {
272         p_intf->p_sys->pf_callback[i_dummy]();
273     }
274 }
275
276 /* following functions are local */
277
278 /*****************************************************************************
279  * GnomeManage: manage main thread messages
280  *****************************************************************************
281  * In this function, called approx. 10 times a second, we check what the
282  * main program wanted to tell us.
283  *****************************************************************************/
284 static gint GnomeManage( gpointer p_data )
285 {
286 #define p_intf ((intf_thread_t *)p_data)
287
288     vlc_mutex_lock( &p_intf->change_lock );
289
290     /* If the "display popup" flag has changed */
291     if( p_intf->b_menu_change )
292     {
293         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
294         {
295             p_intf->p_sys->p_popup = create_intf_popup();
296             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
297                                  "p_popup", p_intf );
298         }
299
300         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
301                                    NULL, NULL, NULL, NULL );
302         p_intf->b_menu_change = 0;
303     }
304
305     /* update the playlist */
306     GtkPlayListManage( p_intf ); 
307
308     if( p_input_bank->pp_input[0] != NULL && !p_intf->b_die )
309     {
310         vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
311
312         if( !p_input_bank->pp_input[0]->b_die )
313         {
314             /* New input or stream map change */
315             if( p_input_bank->pp_input[0]->stream.b_changed )
316             {
317                 GtkModeManage( p_intf );
318                 GtkSetupMenus( p_intf );
319                 p_intf->p_sys->b_playing = 1;
320             }
321
322             /* Manage the slider */
323             if( p_input_bank->pp_input[0]->stream.b_seekable &&
324                 p_intf->p_sys->b_playing )
325             {
326                 float           newvalue;
327                 newvalue = p_intf->p_sys->p_adj->value;
328     
329 #define p_area p_input_bank->pp_input[0]->stream.p_selected_area
330                 /* If the user hasn't touched the slider since the last time,
331                  * then the input can safely change it */
332                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
333                 {
334                     /* Update the value */
335                     p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
336                         ( 100. * p_area->i_tell ) / p_area->i_size;
337     
338                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
339                                              "value_changed" );
340                 }
341                 /* Otherwise, send message to the input if the user has
342                  * finished dragging the slider */
343                 else if( p_intf->p_sys->b_slider_free )
344                 {
345                     off_t i_seek = ( newvalue * p_area->i_size ) / 100;
346         
347                     vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
348                     input_Seek( p_input_bank->pp_input[0], i_seek );
349                     vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
350     
351                     /* Update the old value */
352                     p_intf->p_sys->f_adj_oldvalue = newvalue;
353                 }
354 #undef p_area
355             }
356
357             if( p_intf->p_sys->i_part !=
358                 p_input_bank->pp_input[0]->stream.p_selected_area->i_part )
359             {
360                 p_intf->p_sys->b_chapter_update = 1;
361                 GtkSetupMenus( p_intf );
362             }
363         }
364
365         vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
366     }
367     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
368     {
369         GtkModeManage( p_intf );
370         p_intf->p_sys->b_playing = 0;
371     }
372
373     /* Manage core vlc functions through the callback */
374     p_intf->pf_manage( p_intf );
375
376     if( p_intf->b_die )
377     {
378         vlc_mutex_unlock( &p_intf->change_lock );
379
380         /* Prepare to die, young Skywalker */
381         gtk_main_quit();
382
383         /* Just in case */
384         return( FALSE );
385     }
386
387     vlc_mutex_unlock( &p_intf->change_lock );
388
389     return( TRUE );
390
391 #undef p_intf
392 }
393