1 /*****************************************************************************
2 * gtk_main.c : Gtk+ wrapper for gtk_main
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: gtk_main.c,v 1.3 2002/08/21 15:55:15 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <stdlib.h> /* atexit() */
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
43 static void GtkMain ( vlc_object_t * );
45 /*****************************************************************************
46 * The gtk_main_t object.
47 *****************************************************************************/
50 typedef struct gtk_main_t
54 /* XXX: Ugly kludge, see g_atexit */
55 void ( *pf_callback[MAX_ATEXIT] ) ( void );
59 /*****************************************************************************
60 * Local variables (mutex-protected).
61 *****************************************************************************/
62 static void ** pp_global_data = NULL;
63 static int i_refcount = 0;
64 static gtk_main_t * p_gtk_main = NULL;
66 /*****************************************************************************
68 *****************************************************************************/
70 pp_global_data = p_module->p_vlc->pp_global_data;
71 set_description( _("Gtk+ helper module") );
72 set_capability( "gtk_main", 100 );
73 add_shortcut( "gtk" );
75 add_shortcut( "gnome" );
77 set_callbacks( Open, Close );
80 /*****************************************************************************
81 * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
82 *****************************************************************************
83 * gtk_init() makes several calls to g_atexit() which calls atexit() to
84 * register tidying callbacks to be called at program exit. Since the Gtk+
85 * plugin is likely to be unloaded at program exit, we have to export this
86 * symbol to intercept the g_atexit() calls. Talk about crude hack.
87 *****************************************************************************/
88 void g_atexit( GVoidFunc func )
94 if( pp_global_data == NULL )
100 p_this = (gtk_main_t *)*pp_global_data;
103 /* Looks like this atexit() call wasn't for us. */
108 i_dummy < MAX_ATEXIT && p_this->pf_callback[i_dummy] != NULL;
114 if( i_dummy >= MAX_ATEXIT - 1 )
116 msg_Err( p_this, "too many atexit() callbacks to register" );
120 p_this->pf_callback[i_dummy] = func;
121 p_this->pf_callback[i_dummy + 1] = NULL;
124 /*****************************************************************************
125 * Open: initialize and create window
126 *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
129 /* Initialize Gtk+ */
131 vlc_mutex_lock( p_this->p_vlc->p_global_lock );
136 vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
141 p_gtk_main = vlc_object_create( p_this, sizeof(gtk_main_t) );
142 p_gtk_main->pf_callback[0] = NULL;
144 /* Only initialize gthreads if it's the first time we do it */
145 if( !g_thread_supported() )
147 g_thread_init( NULL );
150 /* Launch the gtk_main() thread. It will not return until it has
151 * called gdk_threads_enter(), which ensures us thread safety. */
152 if( vlc_thread_create( p_gtk_main, "gtk_main", GtkMain, VLC_TRUE ) )
154 vlc_object_destroy( p_gtk_main );
156 vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
161 vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
166 /*****************************************************************************
167 * Close: destroy interface window
168 *****************************************************************************/
169 static void Close( vlc_object_t *p_this )
173 vlc_mutex_lock( p_this->p_vlc->p_global_lock );
179 vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
184 vlc_thread_join( p_gtk_main );
186 /* Launch stored callbacks */
188 i_dummy < MAX_ATEXIT && p_gtk_main->pf_callback[i_dummy] != NULL;
191 p_gtk_main->pf_callback[i_dummy]();
194 vlc_object_destroy( p_gtk_main );
197 vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
200 static gint foo(gpointer foo)
205 /*****************************************************************************
206 * GtkMain: Gtk+ thread
207 *****************************************************************************
208 * this part of the interface is in a separate thread so that we can call
209 * gtk_main() from within it without annoying the rest of the program.
210 *****************************************************************************/
211 static void GtkMain( vlc_object_t *p_this )
213 /* gtk_init needs to know the command line. We don't care, so we
214 * give it an empty one */
215 static char *p_args[] = { "" };
217 static char **pp_args = p_args;
219 static int i_args = 1;
221 /* gtk_init will register stuff with g_atexit, so we need to have
222 * the global lock if we want to be able to intercept the calls */
223 *p_this->p_vlc->pp_global_data = p_gtk_main;
225 /* FIXME: deprecated ? */
226 /* gdk_threads_init(); */
229 gnome_init( p_this->p_vlc->psz_object_name, VERSION, i_args, p_args );
232 gtk_init( &i_args, &pp_args );
237 vlc_thread_ready( p_this );
239 /* If we don't add this simple timeout, gtk_main remains stuck ... */
240 gtk_timeout_add( INTF_IDLE_SLEEP / 1000, foo, p_this );