]> git.sesse.net Git - vlc/blob - plugins/gnome/intf_gnome.c
* Ported Glide and MGA plugins to the new module API. MGA never worked,
[vlc] / plugins / gnome / intf_gnome.c
1 /*****************************************************************************
2  * intf_gnome.c: Gnome interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: intf_gnome.c,v 1.15 2001/02/20 07:49:13 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 #define MODULE_NAME gnome
25 #include "modules_inner.h"
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include "defs.h"
31
32 #include <errno.h>                                                 /* ENOMEM */
33 #include <stdlib.h>                                                /* free() */
34 #include <string.h>                                            /* strerror() */
35 #include <stdio.h>
36
37 #include <gnome.h>
38
39 #include "config.h"
40 #include "common.h"
41 #include "threads.h"
42 #include "mtime.h"
43 #include "tests.h"
44 #include "modules.h"
45
46 #include "stream_control.h"
47 #include "input_ext-intf.h"
48
49 #include "intf_msg.h"
50 #include "interface.h"
51
52 #include "gnome_sys.h"
53 #include "gnome_interface.h"
54 #include "gnome_support.h"
55
56 #include "main.h"
57
58 /*****************************************************************************
59  * Local prototypes.
60  *****************************************************************************/
61 static int  intf_Probe     ( probedata_t *p_data );
62 static int  intf_Open      ( intf_thread_t *p_intf );
63 static void intf_Close     ( intf_thread_t *p_intf );
64 static void intf_Run       ( intf_thread_t *p_intf );
65
66 static gint GnomeManage    ( gpointer p_data );
67
68 /*****************************************************************************
69  * g_atexit: kludge to avoid the Gnome thread to segfault at exit
70  *****************************************************************************
71  * gtk_init() makes several calls to g_atexit() which calls atexit() to
72  * register tidying callbacks to be called at program exit. Since the Gnome
73  * plugin is likely to be unloaded at program exit, we have to export this
74  * symbol to intercept the g_atexit() calls. Talk about crude hack.
75  *****************************************************************************/
76 void g_atexit( GVoidFunc func )
77 {
78     intf_thread_t *p_intf = p_main->p_intf;
79
80     if( p_intf->p_sys->pf_gdk_callback == NULL )
81     {
82         p_intf->p_sys->pf_gdk_callback = func;
83     }
84     else if( p_intf->p_sys->pf_gtk_callback == NULL )
85     {
86         p_intf->p_sys->pf_gtk_callback = func;
87     }
88     /* else nothing, but we could do something here */
89     return;
90 }
91
92 /*****************************************************************************
93  * Functions exported as capabilities. They are declared as static so that
94  * we don't pollute the namespace too much.
95  *****************************************************************************/
96 void _M( intf_getfunctions )( function_list_t * p_function_list )
97 {
98     p_function_list->pf_probe = intf_Probe;
99     p_function_list->functions.intf.pf_open  = intf_Open;
100     p_function_list->functions.intf.pf_close = intf_Close;
101     p_function_list->functions.intf.pf_run   = intf_Run;
102 }
103
104 /*****************************************************************************
105  * intf_Probe: probe the interface and return a score
106  *****************************************************************************
107  * This function tries to initialize Gnome and returns a score to the
108  * plugin manager so that it can select the best plugin.
109  *****************************************************************************/
110 static int intf_Probe( probedata_t *p_data )
111 {
112     if( TestMethod( INTF_METHOD_VAR, "gnome" ) )
113     {
114         return( 999 );
115     }
116
117     return( 100 );
118 }
119
120 /*****************************************************************************
121  * intf_Open: initialize and create window
122  *****************************************************************************/
123 static int intf_Open( intf_thread_t *p_intf )
124 {
125     /* Allocate instance and initialize some members */
126     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
127     if( p_intf->p_sys == NULL )
128     {
129         intf_ErrMsg("error: %s", strerror(ENOMEM));
130         return( 1 );
131     }
132
133     /* Initialize Gnome thread */
134     p_intf->p_sys->b_popup_changed = 0;
135     p_intf->p_sys->b_window_changed = 0;
136     p_intf->p_sys->b_playlist_changed = 0;
137
138     p_intf->p_sys->b_scale_isfree = 1;
139
140     p_intf->p_sys->pf_gtk_callback = NULL;
141     p_intf->p_sys->pf_gdk_callback = NULL;
142
143     return( 0 );
144 }
145
146 /*****************************************************************************
147  * intf_Close: destroy interface window
148  *****************************************************************************/
149 static void intf_Close( intf_thread_t *p_intf )
150 {
151     /* Destroy structure */
152     free( p_intf->p_sys );
153 }
154
155 /*****************************************************************************
156  * intf_Run: Gnome thread
157  *****************************************************************************
158  * this part of the interface is in a separate thread so that we can call
159  * gtk_main() from within it without annoying the rest of the program.
160  * XXX: the approach may look kludgy, and probably is, but I could not find
161  * a better way to dynamically load a Gnome interface at runtime.
162  *****************************************************************************/
163 static void intf_Run( intf_thread_t *p_intf )
164 {
165     /* gnome_init needs to know the command line. We don't care, so we
166      * give it an empty one */
167     char *p_args[] = { };
168
169     /* The data types we are allowed to receive */
170     static GtkTargetEntry target_table[] =
171     {
172         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
173         { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
174     };
175
176     /* Initialize Gnome */
177     gnome_init( p_main->psz_arg0, VERSION, 1, p_args );
178
179     /* create some useful widgets that will certainly be used */
180     p_intf->p_sys->p_window = create_intf_window( );
181     p_intf->p_sys->p_popup = create_intf_popup( );
182
183     /* accept file drops on the main window */
184     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
185                        GTK_DEST_DEFAULT_ALL, target_table,
186                        1, GDK_ACTION_COPY );
187
188     /* we don't create these ones yet because we perhaps won't need them */
189     p_intf->p_sys->p_about = NULL;
190     p_intf->p_sys->p_playlist = NULL;
191     p_intf->p_sys->p_modules = NULL;
192     p_intf->p_sys->p_fileopen = NULL;
193
194     /* store p_sys to keep an eye on it */
195     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
196                          "p_intf", p_intf );
197
198     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
199                          "p_intf", p_intf );
200
201     /* show the control window */
202     gtk_widget_show( p_intf->p_sys->p_window );
203
204     /* Sleep to avoid using all CPU - since some interfaces needs to access
205      * keyboard events, a 100ms delay is a good compromise */
206     p_intf->p_sys->i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000,
207                                                 GnomeManage, p_intf );
208  
209
210     /* enter gnome mode */
211     gtk_main();
212
213     /* launch stored callbacks */
214     if( p_intf->p_sys->pf_gtk_callback != NULL )
215     {
216         p_intf->p_sys->pf_gtk_callback();
217
218         if( p_intf->p_sys->pf_gdk_callback != NULL )
219         {
220             p_intf->p_sys->pf_gdk_callback();
221         }
222     }
223 }
224
225 /* following functions are local */
226
227 /*****************************************************************************
228  * GnomeManage: manage main thread messages
229  *****************************************************************************
230  * In this function, called approx. 10 times a second, we check what the
231  * main program wanted to tell us.
232  *****************************************************************************/
233 static gint GnomeManage( gpointer p_data )
234 {
235     intf_thread_t *p_intf = (void *)p_data;
236
237     vlc_mutex_lock( &p_intf->p_sys->change_lock );
238
239     /* If the "display popup" flag has changed */
240     if( p_intf->b_menu_change )
241     {
242         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
243                                    NULL, NULL, NULL, NULL );
244         p_intf->b_menu_change = 0;
245     }
246
247     /* Manage the slider */
248     if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL
249          && p_intf->p_sys->b_scale_isfree )
250     {
251         GtkWidget *p_scale;
252         GtkAdjustment *p_adj;
253    
254         p_scale = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
255                                   p_intf->p_sys->p_window ), "hscale" ) );
256         p_adj = gtk_range_get_adjustment ( GTK_RANGE( p_scale ) );
257
258         /* Update the value */
259         p_adj->value = ( 100. * p_intf->p_input->stream.pp_areas[0]->i_tell )
260                            / p_intf->p_input->stream.pp_areas[0]->i_size;
261
262         /* Gtv does it this way. Why not. */
263         gtk_range_set_adjustment ( GTK_RANGE( p_scale ), p_adj );
264         gtk_range_slider_update ( GTK_RANGE( p_scale ) );
265         gtk_range_clear_background ( GTK_RANGE( p_scale ) );
266         gtk_range_draw_background ( GTK_RANGE( p_scale ) );
267     }
268
269     /* Manage core vlc functions through the callback */
270     p_intf->pf_manage( p_intf );
271
272     if( p_intf->b_die )
273     {
274         /* Make sure we won't be called again */
275         gtk_timeout_remove( p_intf->p_sys->i_timeout );
276
277         vlc_mutex_unlock( &p_intf->p_sys->change_lock );
278
279         /* Prepare to die, young Skywalker */
280         gtk_main_quit();
281         return( FALSE );
282     }
283
284     vlc_mutex_unlock( &p_intf->p_sys->change_lock );
285
286     return( TRUE );
287 }
288