]> git.sesse.net Git - vlc/blob - plugins/gtk/intf_gnome.c
* Forgot a file in my last commit.
[vlc] / plugins / gtk / intf_gnome.c
1 /*****************************************************************************
2  * intf_gnome.c: Gnome interface
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: intf_gnome.c,v 1.8 2001/12/10 04:53:10 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Stéphane Borel <stef@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #define MODULE_NAME gnome
26 #include "modules_inner.h"
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include "defs.h"
32
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <stdlib.h>                                                /* free() */
35 #include <string.h>                                            /* strerror() */
36 #include <stdio.h>
37
38 #include <gnome.h>
39
40 #include "common.h"
41 #include "intf_msg.h"
42 #include "threads.h"
43 #include "mtime.h"
44 #include "tests.h"
45
46 #include "stream_control.h"
47 #include "input_ext-intf.h"
48
49 #include "interface.h"
50 #include "intf_playlist.h"
51
52 #include "video.h"
53 #include "video_output.h"
54
55 #include "gnome_callbacks.h"
56 #include "gnome_interface.h"
57 #include "gnome_support.h"
58 #include "gtk_display.h"
59 #include "intf_gtk.h"
60
61 #include "modules.h"
62 #include "modules_export.h"
63
64 /*****************************************************************************
65  * Local prototypes.
66  *****************************************************************************/
67 static int  intf_Probe      ( probedata_t *p_data );
68 static int  intf_Open       ( intf_thread_t *p_intf );
69 static void intf_Close      ( intf_thread_t *p_intf );
70 static void intf_Run        ( intf_thread_t *p_intf );
71
72 static gint GnomeManage     ( gpointer p_data );
73
74 /*****************************************************************************
75  * g_atexit: kludge to avoid the Gnome thread to segfault at exit
76  *****************************************************************************
77  * gtk_init() makes several calls to g_atexit() which calls atexit() to
78  * register tidying callbacks to be called at program exit. Since the Gnome
79  * plugin is likely to be unloaded at program exit, we have to export this
80  * symbol to intercept the g_atexit() calls. Talk about crude hack.
81  *****************************************************************************/
82 void g_atexit( GVoidFunc func )
83 {
84     intf_thread_t *p_intf = p_main->p_intf;
85
86     if( p_intf->p_sys->pf_gdk_callback == NULL )
87     {
88         p_intf->p_sys->pf_gdk_callback = func;
89     }
90     else if( p_intf->p_sys->pf_gtk_callback == NULL )
91     {
92         p_intf->p_sys->pf_gtk_callback = func;
93     }
94     /* else nothing, but we could do something here */
95     return;
96 }
97
98 /*****************************************************************************
99  * Functions exported as capabilities. They are declared as static so that
100  * we don't pollute the namespace too much.
101  *****************************************************************************/
102 void _M( intf_getfunctions )( function_list_t * p_function_list )
103 {
104     p_function_list->pf_probe = intf_Probe;
105     p_function_list->functions.intf.pf_open  = intf_Open;
106     p_function_list->functions.intf.pf_close = intf_Close;
107     p_function_list->functions.intf.pf_run   = intf_Run;
108 }
109
110 /*****************************************************************************
111  * intf_Probe: probe the interface and return a score
112  *****************************************************************************
113  * This function tries to initialize Gnome and returns a score to the
114  * plugin manager so that it can select the best plugin.
115  *****************************************************************************/
116 static int intf_Probe( probedata_t *p_data )
117 {
118     if( TestMethod( INTF_METHOD_VAR, "gnome" ) )
119     {
120         return( 999 );
121     }
122
123     if( TestProgram( "gnome-vlc" ) )
124     {
125         return( 200 );
126     }
127
128     return( 100 );
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 = 1;
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_gtk_callback = NULL;
154     p_intf->p_sys->pf_gdk_callback = NULL;
155
156     return( 0 );
157 }
158
159 /*****************************************************************************
160  * intf_Close: destroy interface window
161  *****************************************************************************/
162 static void intf_Close( intf_thread_t *p_intf )
163 {
164     /* Destroy structure */
165     free( p_intf->p_sys );
166 }
167
168 /*****************************************************************************
169  * intf_Run: Gnome thread
170  *****************************************************************************
171  * this part of the interface is in a separate thread so that we can call
172  * gtk_main() from within it without annoying the rest of the program.
173  * XXX: the approach may look kludgy, and probably is, but I could not find
174  * a better way to dynamically load a Gnome interface at runtime.
175  *****************************************************************************/
176 static void intf_Run( intf_thread_t *p_intf )
177 {
178     /* gnome_init needs to know the command line. We don't care, so we
179      * give it an empty one */
180     char *p_args[] = { "" };
181     int   i_args   = 1;
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     /* intf_Manage callback timeout */
192     int i_timeout;
193
194     /* Initialize Gnome */
195     gnome_init( p_main->psz_arg0, VLC_VERSION, i_args, p_args );
196
197     /* Create some useful widgets that will certainly be used */
198     p_intf->p_sys->p_window = create_intf_window( );
199     p_intf->p_sys->p_popup = create_intf_popup( );
200     p_intf->p_sys->p_playlist = create_intf_playlist();
201
202     /* Set the title of the main window */
203     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
204                           VOUT_TITLE " (Gnome interface)");
205
206     /* Accept file drops on the main window */
207     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
208                        GTK_DEST_DEFAULT_ALL, target_table,
209                        1, GDK_ACTION_COPY );
210     /* Accept file drops on the playlist window */
211     gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
212                             p_intf->p_sys->p_playlist ), "playlist_clist") ),
213                        GTK_DEST_DEFAULT_ALL, target_table,
214                        1, GDK_ACTION_COPY );
215
216     /* Get the interface labels */
217     p_intf->p_sys->p_slider_frame = gtk_object_get_data(
218                       GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
219     #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
220                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
221     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
222     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
223     #undef P_LABEL
224
225     /* Connect the date display to the slider */
226     #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
227                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
228     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
229
230     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
231                          GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
232     p_intf->p_sys->f_adj_oldvalue = 0;
233     #undef P_SLIDER
234
235     /* We don't create these ones yet because we perhaps won't need them */
236     p_intf->p_sys->p_about = NULL;
237     p_intf->p_sys->p_modules = NULL;
238     p_intf->p_sys->p_fileopen = NULL;
239     p_intf->p_sys->p_disc = NULL;
240     p_intf->p_sys->p_network = NULL;
241     p_intf->p_sys->p_preferences = NULL;
242     p_intf->p_sys->p_jump = NULL;
243
244     /* Store p_intf to keep an eye on it */
245     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
246                          "p_intf", p_intf );
247
248     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
249                          "p_intf", p_intf );
250
251     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
252                          "p_intf", p_intf );
253
254     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
255                          "p_intf", p_intf );
256
257     /* Show the control window */
258     gtk_widget_show( p_intf->p_sys->p_window );
259
260     /* Sleep to avoid using all CPU - since some interfaces needs to access
261      * keyboard events, a 100ms delay is a good compromise */
262     i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
263
264     /* Enter gnome mode */
265     gtk_main();
266
267     /* Remove the timeout */
268     gtk_timeout_remove( i_timeout );
269
270     /* Get rid of stored callbacks so we can unload the plugin */
271     if( p_intf->p_sys->pf_gtk_callback != NULL )
272     {
273         p_intf->p_sys->pf_gtk_callback( );
274         p_intf->p_sys->pf_gtk_callback = NULL;
275
276     }
277
278     if( p_intf->p_sys->pf_gdk_callback != NULL )
279     {
280         p_intf->p_sys->pf_gdk_callback( );
281         p_intf->p_sys->pf_gdk_callback = NULL;
282     }
283 }
284
285 /* following functions are local */
286
287 /*****************************************************************************
288  * GnomeManage: manage main thread messages
289  *****************************************************************************
290  * In this function, called approx. 10 times a second, we check what the
291  * main program wanted to tell us.
292  *****************************************************************************/
293 static gint GnomeManage( gpointer p_data )
294 {
295 #define p_intf ((intf_thread_t *)p_data)
296
297     vlc_mutex_lock( &p_intf->change_lock );
298
299     /* If the "display popup" flag has changed */
300     if( p_intf->b_menu_change )
301     {
302         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
303         {
304             p_intf->p_sys->p_popup = create_intf_popup();
305             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
306                                  "p_popup", p_intf );
307         }
308
309         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
310                                    NULL, NULL, NULL, NULL );
311         p_intf->b_menu_change = 0;
312     }
313
314     /* update the playlist */
315     GtkPlayListManage( p_intf ); 
316
317     if( p_intf->p_input != NULL && !p_intf->b_die )
318     {
319         vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
320
321         /* New input or stream map change */
322         if( p_intf->p_input->stream.b_changed )
323         {
324             GtkModeManage( p_intf );
325             GtkSetupMenus( p_intf );
326             p_intf->p_sys->b_playing = 1;
327         }
328
329         /* Manage the slider */
330         if( p_intf->p_input->stream.b_seekable )
331         {
332             float           newvalue;
333             newvalue = p_intf->p_sys->p_adj->value;
334     
335 #define p_area p_intf->p_input->stream.p_selected_area
336             /* If the user hasn't touched the slider since the last time,
337              * then the input can safely change it */
338             if( newvalue == p_intf->p_sys->f_adj_oldvalue )
339             {
340                 /* Update the value */
341                 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
342                     ( 100. * p_area->i_tell ) / p_area->i_size;
343     
344                 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
345                                          "value_changed" );
346             }
347             /* Otherwise, send message to the input if the user has
348              * finished dragging the slider */
349             else if( p_intf->p_sys->b_slider_free )
350             {
351                 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
352     
353                 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
354                 input_Seek( p_intf->p_input, i_seek );
355                 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
356     
357                 /* Update the old value */
358                 p_intf->p_sys->f_adj_oldvalue = newvalue;
359             }
360 #undef p_area
361         }
362
363         if( p_intf->p_sys->i_part !=
364             p_intf->p_input->stream.p_selected_area->i_part )
365         {
366             p_intf->p_sys->b_chapter_update = 1;
367             GtkSetupMenus( p_intf );
368         }
369
370         vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
371     }
372     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
373     {
374         GtkModeManage( p_intf );
375         p_intf->p_sys->b_playing = 0;
376     }
377
378     /* Manage core vlc functions through the callback */
379     p_intf->pf_manage( p_intf );
380
381     if( p_intf->b_die )
382     {
383         vlc_mutex_unlock( &p_intf->change_lock );
384
385         /* Prepare to die, young Skywalker */
386         gtk_main_quit();
387
388         /* Just in case */
389         return( FALSE );
390     }
391
392     vlc_mutex_unlock( &p_intf->change_lock );
393
394     return( TRUE );
395
396 #undef p_intf
397 }