]> git.sesse.net Git - vlc/blob - plugins/gtk/intf_gnome.c
b18c1c6a99a6de92b895b5b983e50ba147f5b407
[vlc] / plugins / gtk / intf_gnome.c
1 /*****************************************************************************
2  * intf_gnome.c: Gnome interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: intf_gnome.c,v 1.1 2001/05/23 23:08:20 stef 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 "config.h"
41 #include "common.h"
42 #include "threads.h"
43 #include "mtime.h"
44 #include "tests.h"
45 #include "modules.h"
46
47 #include "stream_control.h"
48 #include "input_ext-intf.h"
49
50 #include "intf_msg.h"
51 #include "interface.h"
52 #include "intf_playlist.h"
53
54 #include "video.h"
55 #include "video_output.h"
56
57 #include "gnome_callbacks.h"
58 #include "gnome_interface.h"
59 #include "gnome_support.h"
60 #include "gtk_display.h"
61 #include "intf_gtk.h"
62
63 #include "main.h"
64
65 /*****************************************************************************
66  * Local prototypes.
67  *****************************************************************************/
68 static int  intf_Probe      ( probedata_t *p_data );
69 static int  intf_Open       ( intf_thread_t *p_intf );
70 static void intf_Close      ( intf_thread_t *p_intf );
71 static void intf_Run        ( intf_thread_t *p_intf );
72
73 static gint GnomeManage     ( gpointer p_data );
74
75 /*****************************************************************************
76  * g_atexit: kludge to avoid the Gnome thread to segfault at exit
77  *****************************************************************************
78  * gtk_init() makes several calls to g_atexit() which calls atexit() to
79  * register tidying callbacks to be called at program exit. Since the Gnome
80  * plugin is likely to be unloaded at program exit, we have to export this
81  * symbol to intercept the g_atexit() calls. Talk about crude hack.
82  *****************************************************************************/
83 void g_atexit( GVoidFunc func )
84 {
85     intf_thread_t *p_intf = p_main->p_intf;
86
87     if( p_intf->p_sys->pf_gdk_callback == NULL )
88     {
89         p_intf->p_sys->pf_gdk_callback = func;
90     }
91     else if( p_intf->p_sys->pf_gtk_callback == NULL )
92     {
93         p_intf->p_sys->pf_gtk_callback = func;
94     }
95     /* else nothing, but we could do something here */
96     return;
97 }
98
99 /*****************************************************************************
100  * Functions exported as capabilities. They are declared as static so that
101  * we don't pollute the namespace too much.
102  *****************************************************************************/
103 void _M( intf_getfunctions )( function_list_t * p_function_list )
104 {
105     p_function_list->pf_probe = intf_Probe;
106     p_function_list->functions.intf.pf_open  = intf_Open;
107     p_function_list->functions.intf.pf_close = intf_Close;
108     p_function_list->functions.intf.pf_run   = intf_Run;
109 }
110
111 /*****************************************************************************
112  * intf_Probe: probe the interface and return a score
113  *****************************************************************************
114  * This function tries to initialize Gnome and returns a score to the
115  * plugin manager so that it can select the best plugin.
116  *****************************************************************************/
117 static int intf_Probe( probedata_t *p_data )
118 {
119     if( TestMethod( INTF_METHOD_VAR, "gnome" ) )
120     {
121         return( 999 );
122     }
123
124     if( TestProgram( "gnome-vlc" ) )
125     {
126         return( 200 );
127     }
128
129     return( 100 );
130 }
131
132 /*****************************************************************************
133  * intf_Open: initialize and create window
134  *****************************************************************************/
135 static int intf_Open( intf_thread_t *p_intf )
136 {
137     /* Allocate instance and initialize some members */
138     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
139     if( p_intf->p_sys == NULL )
140     {
141         intf_ErrMsg("error: %s", strerror(ENOMEM));
142         return( 1 );
143     }
144
145     /* Initialize Gnome thread */
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         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
187         { "text/plain",    0, DROP_ACCEPT_TEXT_PLAIN }
188     };
189
190     /* intf_Manage callback timeout */
191     int i_timeout;
192
193     /* Initialize Gnome */
194     gnome_init( p_main->psz_arg0, VERSION, i_args, p_args );
195
196     /* Create some useful widgets that will certainly be used */
197     p_intf->p_sys->p_window = create_intf_window( );
198     p_intf->p_sys->p_popup = create_intf_popup( );
199     p_intf->p_sys->p_playlist = create_intf_playlist();
200
201     /* Set the title of the main window */
202     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
203                           VOUT_TITLE " (Gnome interface)");
204
205     /* Accept file drops on the main window */
206     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
207                        GTK_DEST_DEFAULT_ALL, target_table,
208                        1, GDK_ACTION_COPY );
209     /* Accept file drops on the playlist window */
210     gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
211                             p_intf->p_sys->p_playlist ), "playlist_clist") ),
212                        GTK_DEST_DEFAULT_ALL, target_table,
213                        1, GDK_ACTION_COPY );
214
215     /* Get the interface labels */
216     p_intf->p_sys->p_slider_frame = gtk_object_get_data(
217                       GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
218     #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
219                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
220     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
221     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
222     #undef P_LABEL
223
224     /* Connect the date display to the slider */
225     #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
226                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
227     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
228
229     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
230                          GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
231     p_intf->p_sys->f_adj_oldvalue = 0;
232     #undef P_SLIDER
233
234     /* We don't create these ones yet because we perhaps won't need them */
235     p_intf->p_sys->p_about = NULL;
236     p_intf->p_sys->p_modules = NULL;
237     p_intf->p_sys->p_fileopen = NULL;
238     p_intf->p_sys->p_disc = NULL;
239     p_intf->p_sys->p_network = NULL;
240     p_intf->p_sys->p_preferences = NULL;
241     p_intf->p_sys->p_jump = NULL;
242
243     /* Store p_intf to keep an eye on it */
244     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
245                          "p_intf", p_intf );
246
247     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
248                          "p_intf", p_intf );
249
250     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
251                          "p_intf", p_intf );
252
253     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
254                          "p_intf", p_intf );
255
256     /* Show the control window */
257     gtk_widget_show( p_intf->p_sys->p_window );
258
259     /* Sleep to avoid using all CPU - since some interfaces needs to access
260      * keyboard events, a 100ms delay is a good compromise */
261     i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
262
263     /* Enter gnome mode */
264     gtk_main();
265
266     /* Remove the timeout */
267     gtk_timeout_remove( i_timeout );
268
269     /* Get rid of stored callbacks so we can unload the plugin */
270     if( p_intf->p_sys->pf_gtk_callback != NULL )
271     {
272         p_intf->p_sys->pf_gtk_callback( );
273         p_intf->p_sys->pf_gtk_callback = NULL;
274
275     }
276
277     if( p_intf->p_sys->pf_gdk_callback != NULL )
278     {
279         p_intf->p_sys->pf_gdk_callback( );
280         p_intf->p_sys->pf_gdk_callback = NULL;
281     }
282 }
283
284 /* following functions are local */
285
286 /*****************************************************************************
287  * GnomeManage: manage main thread messages
288  *****************************************************************************
289  * In this function, called approx. 10 times a second, we check what the
290  * main program wanted to tell us.
291  *****************************************************************************/
292 static gint GnomeManage( gpointer p_data )
293 {
294 #define p_intf ((intf_thread_t *)p_data)
295
296     vlc_mutex_lock( &p_intf->change_lock );
297
298     /* If the "display popup" flag has changed */
299     if( p_intf->b_menu_change )
300     {
301         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
302         {
303             p_intf->p_sys->p_popup = create_intf_popup();
304             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
305                                  "p_popup", p_intf );
306         }
307
308         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
309                                    NULL, NULL, NULL, NULL );
310         p_intf->b_menu_change = 0;
311     }
312
313     /* update the playlist */
314     GtkPlayListManage( p_intf ); 
315
316     if( p_intf->p_input != NULL && !p_intf->b_die )
317     {
318         vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
319
320         /* New input or stream map change */
321         if( p_intf->p_input->stream.b_changed )
322         {
323             GtkModeManage( p_intf );
324         }
325
326         /* Manage the slider */
327         if( p_intf->p_input->stream.b_seekable )
328         {
329             float           newvalue;
330             newvalue = p_intf->p_sys->p_adj->value;
331     
332 #define p_area p_intf->p_input->stream.p_selected_area
333             /* If the user hasn't touched the slider since the last time,
334              * then the input can safely change it */
335             if( newvalue == p_intf->p_sys->f_adj_oldvalue )
336             {
337                 /* Update the value */
338                 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
339                     ( 100. * p_area->i_tell ) / p_area->i_size;
340     
341                 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
342                                          "value_changed" );
343             }
344             /* Otherwise, send message to the input if the user has
345              * finished dragging the slider */
346             else if( p_intf->p_sys->b_slider_free )
347             {
348                 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
349     
350                 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
351                 input_Seek( p_intf->p_input, i_seek );
352                 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
353     
354                 /* Update the old value */
355                 p_intf->p_sys->f_adj_oldvalue = newvalue;
356             }
357 #undef p_area
358         }
359         vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
360
361         if( p_intf->p_sys->i_part !=
362             p_intf->p_input->stream.p_selected_area->i_part )
363         {
364             p_intf->p_sys->b_chapter_update = 1;
365             GtkSetupMenus( p_intf );
366         }
367
368     }
369     else if( !p_intf->b_die )
370     {
371         GtkModeManage( p_intf );
372     }
373
374     /* Manage core vlc functions through the callback */
375     p_intf->pf_manage( p_intf );
376
377     if( p_intf->b_die )
378     {
379         vlc_mutex_unlock( &p_intf->change_lock );
380
381         /* Prepare to die, young Skywalker */
382         gtk_main_quit();
383
384         /* Just in case */
385         return( FALSE );
386     }
387
388     vlc_mutex_unlock( &p_intf->change_lock );
389
390     return( TRUE );
391
392 #undef p_intf
393 }