]> git.sesse.net Git - vlc/blob - plugins/gnome/intf_gnome.c
* The pure Gnome part of the Gnome interface has been rewritten from
[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.9 2001/02/12 00:20:37 sam Exp $
6  *
7  * Authors:
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 "defs.h"
28
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdlib.h>                                                /* free() */
31 #include <string.h>                                            /* strerror() */
32 #include <stdio.h>
33
34 #include "glib.h"
35
36 #include <X11/Xlib.h>
37 #include <X11/Xutil.h>
38 #include <X11/keysym.h>
39
40 #include <gnome.h>
41
42 #include "config.h"
43 #include "common.h"
44 #include "threads.h"
45 #include "mtime.h"
46 #include "tests.h"
47 #include "modules.h"
48
49 #include "stream_control.h"
50 #include "input_ext-intf.h"
51
52 #include "intf_msg.h"
53 #include "interface.h"
54
55 #include "gnome_sys.h"
56 #include "gnome_interface.h"
57 #include "gnome_support.h"
58
59 #include "main.h"
60
61 /*****************************************************************************
62  * Local prototypes.
63  *****************************************************************************/
64 static int  intf_Probe     ( probedata_t *p_data );
65 static int  intf_Open      ( intf_thread_t *p_intf );
66 static void intf_Close     ( intf_thread_t *p_intf );
67 static void intf_Run       ( intf_thread_t *p_intf );
68
69 static gint GnomeManage    ( gpointer p_data );
70
71 /*****************************************************************************
72  * g_atexit: kludge to avoid the Gnome thread to segfault at exit
73  *****************************************************************************
74  * gtk_init() makes several calls to g_atexit() which calls atexit() to
75  * register tidying callbacks to be called at program exit. Since the Gnome
76  * plugin is likely to be unloaded at program exit, we have to export this
77  * symbol to intercept the g_atexit() calls. Talk about crude hack.
78  *****************************************************************************/
79 void g_atexit( GVoidFunc func )
80 {
81     intf_thread_t *p_intf = p_main->p_intf;
82
83     if( p_intf->p_sys->pf_gdk_callback == NULL )
84     {
85         p_intf->p_sys->pf_gdk_callback = func;
86     }
87     else if( p_intf->p_sys->pf_gtk_callback == NULL )
88     {
89         p_intf->p_sys->pf_gtk_callback = func;
90     }
91     /* else nothing, but we could do something here */
92     return;
93 }
94
95 /*****************************************************************************
96  * Functions exported as capabilities. They are declared as static so that
97  * we don't pollute the namespace too much.
98  *****************************************************************************/
99 void intf_getfunctions( function_list_t * p_function_list )
100 {
101     p_function_list->pf_probe = intf_Probe;
102     p_function_list->functions.intf.pf_open  = intf_Open;
103     p_function_list->functions.intf.pf_close = intf_Close;
104     p_function_list->functions.intf.pf_run   = intf_Run;
105 }
106
107 /*****************************************************************************
108  * intf_Probe: probe the interface and return a score
109  *****************************************************************************
110  * This function tries to initialize Gnome and returns a score to the
111  * plugin manager so that it can select the best plugin.
112  *****************************************************************************/
113 static int intf_Probe( probedata_t *p_data )
114 {
115     if( TestMethod( INTF_METHOD_VAR, "gnome" ) )
116     {
117         return( 999 );
118     }
119
120     return( 40 );
121 }
122
123 /*****************************************************************************
124  * intf_Open: initialize and create window
125  *****************************************************************************/
126 static int intf_Open( intf_thread_t *p_intf )
127 {
128     /* Allocate instance and initialize some members */
129     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
130     if( p_intf->p_sys == NULL )
131     {
132         intf_ErrMsg("error: %s", strerror(ENOMEM));
133         return( 1 );
134     }
135
136     /* Initialize Gnome thread */
137     p_intf->p_sys->b_window = 1;
138     p_intf->p_sys->b_playlist = 0;
139
140     p_intf->p_sys->b_popup_changed = 0;
141     p_intf->p_sys->b_window_changed = 0;
142     p_intf->p_sys->b_playlist_changed = 0;
143
144     p_intf->p_sys->pf_gtk_callback = NULL;
145     p_intf->p_sys->pf_gdk_callback = NULL;
146
147     return( 0 );
148 }
149
150 /*****************************************************************************
151  * intf_Close: destroy interface window
152  *****************************************************************************/
153 static void intf_Close( intf_thread_t *p_intf )
154 {
155     /* Destroy structure */
156     free( p_intf->p_sys );
157 }
158
159 /*****************************************************************************
160  * intf_Run: Gnome thread
161  *****************************************************************************
162  * this part of the interface is in a separate thread so that we can call
163  * gtk_main() from within it without annoying the rest of the program.
164  * XXX: the approach may look kludgy, and probably is, but I could not find
165  * a better way to dynamically load a Gnome interface at runtime.
166  *****************************************************************************/
167 static void intf_Run( intf_thread_t *p_intf )
168 {
169     /* gnome_init needs to know the command line. We don't care, so we
170      * give it an empty one */
171     char *p_args[] = { };
172
173     /* Initialize Gnome */
174     gnome_init( p_main->psz_arg0, VERSION, 1, p_args );
175
176     /* create some useful widgets that will certainly be used */
177     p_intf->p_sys->p_window = create_intf_window();
178     p_intf->p_sys->p_popup = create_intf_popup( );
179
180     /* we don't create these ones yet because we perhaps won't need them */
181     p_intf->p_sys->p_about = NULL;
182     p_intf->p_sys->p_playlist = NULL;
183     p_intf->p_sys->p_fileopen = NULL;
184
185     /* store p_sys to keep an eye on it */
186     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
187                          "p_intf", p_intf );
188
189     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
190                          "p_intf", p_intf );
191
192     /* show the control window */
193     gtk_widget_show( p_intf->p_sys->p_window );
194
195     /* Sleep to avoid using all CPU - since some interfaces needs to access
196      * keyboard events, a 100ms delay is a good compromise */
197     p_intf->p_sys->i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000,
198                                                 GnomeManage, p_intf );
199  
200     /* enter gnome mode */
201     gtk_main();
202
203     /* launch stored callbacks */
204     if( p_intf->p_sys->pf_gtk_callback != NULL )
205     {
206         p_intf->p_sys->pf_gtk_callback();
207
208         if( p_intf->p_sys->pf_gdk_callback != NULL )
209         {
210             p_intf->p_sys->pf_gdk_callback();
211         }
212     }
213 }
214
215 /* following functions are local */
216
217 /*****************************************************************************
218  * GnomeManage: manage main thread messages
219  *****************************************************************************
220  * In this function, called approx. 10 times a second, we check what the
221  * main program wanted to tell us.
222  *****************************************************************************/
223 static gint GnomeManage( gpointer p_data )
224 {
225     intf_thread_t *p_intf = (void *)p_data;
226
227     /* if the "display popup" flag has changed */
228     if( p_intf->b_menu_change )
229     {
230         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
231                                    NULL, NULL, NULL, NULL );
232         p_intf->b_menu_change = 0;
233     }
234
235     /* Manage core vlc functions through the callback */
236     p_intf->pf_manage( p_intf );
237
238     if( p_intf->b_die )
239     {
240         /* prepare to die, young man */
241         gtk_main_quit();
242         return( FALSE );
243     }
244
245     return( TRUE );
246 }
247