]> git.sesse.net Git - vlc/blob - modules/misc/gtk_main.c
* ./include/vlc_common.h: we don't set _() and N_() in Gnome-enabled modules
[vlc] / modules / misc / gtk_main.c
1 /*****************************************************************************
2  * gtk_main.c : Gtk+ wrapper for gtk_main
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: gtk_main.c,v 1.2 2002/08/21 15:53:06 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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28
29 #include <stdlib.h>                                              /* atexit() */
30
31 #include <gtk/gtk.h>
32
33 #ifdef HAVE_GNOME_H
34 #   include <gnome.h>
35 #endif
36
37 /*****************************************************************************
38  * Local prototypes.
39  *****************************************************************************/
40 static int  Open    ( vlc_object_t * );
41 static void Close   ( vlc_object_t * );
42
43 static void GtkMain ( vlc_object_t * );
44
45 /*****************************************************************************
46  * The gtk_main_t object.
47  *****************************************************************************/
48 #define MAX_ATEXIT 10
49
50 typedef struct gtk_main_t
51 {
52     VLC_COMMON_MEMBERS
53
54     /* XXX: Ugly kludge, see g_atexit */
55     void ( *pf_callback[MAX_ATEXIT] ) ( void );
56
57 } gtk_main_t;
58
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;
65
66 /*****************************************************************************
67  * Module descriptor
68  *****************************************************************************/
69 vlc_module_begin();
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" );
74 #ifdef HAVE_GNOME_H
75     add_shortcut( "gnome" );
76 #endif
77     set_callbacks( Open, Close );
78 vlc_module_end();
79
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 )
89 {
90     gtk_main_t *p_this;
91
92     int i_dummy;
93
94     if( pp_global_data == NULL )
95     {
96         atexit( func );
97         return;
98     }
99
100     p_this = (gtk_main_t *)*pp_global_data;
101     if( p_this == NULL )
102     {
103         /* Looks like this atexit() call wasn't for us. */
104         return;
105     }
106
107     for( i_dummy = 0;
108          i_dummy < MAX_ATEXIT && p_this->pf_callback[i_dummy] != NULL;
109          i_dummy++ )
110     {
111         ;
112     }
113
114     if( i_dummy >= MAX_ATEXIT - 1 )
115     {
116         msg_Err( p_this, "too many atexit() callbacks to register" );
117         return;
118     }
119
120     p_this->pf_callback[i_dummy]     = func;
121     p_this->pf_callback[i_dummy + 1] = NULL;
122 }
123
124 /*****************************************************************************
125  * Open: initialize and create window
126  *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
128 {
129     /* Initialize Gtk+ */
130
131     vlc_mutex_lock( p_this->p_vlc->p_global_lock );
132
133     if( i_refcount > 0 )
134     {
135         i_refcount++;
136         vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
137
138         return VLC_SUCCESS;
139     }
140
141     p_gtk_main = vlc_object_create( p_this, sizeof(gtk_main_t) );
142     p_gtk_main->pf_callback[0] = NULL;
143
144     /* Only initialize gthreads if it's the first time we do it */
145     if( !g_thread_supported() )
146     {
147         g_thread_init( NULL );
148     }
149
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 ) )
153     {
154         vlc_object_destroy( p_gtk_main );
155         i_refcount--;
156         vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
157         return VLC_ETHREAD;
158     }
159
160     i_refcount++;
161     vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
162
163     return VLC_SUCCESS;
164 }
165
166 /*****************************************************************************
167  * Close: destroy interface window
168  *****************************************************************************/
169 static void Close( vlc_object_t *p_this )
170 {
171     int i_dummy;
172
173     vlc_mutex_lock( p_this->p_vlc->p_global_lock );
174
175     i_refcount--;
176
177     if( i_refcount > 0 )
178     {
179         vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
180         return;
181     }
182
183     gtk_main_quit();
184     vlc_thread_join( p_gtk_main );
185
186     /* Launch stored callbacks */
187     for( i_dummy = 0;
188          i_dummy < MAX_ATEXIT && p_gtk_main->pf_callback[i_dummy] != NULL;
189          i_dummy++ )
190     {
191         p_gtk_main->pf_callback[i_dummy]();
192     }
193
194     vlc_object_destroy( p_gtk_main );
195     p_gtk_main = NULL;
196
197     vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
198 }
199
200 static gint foo(gpointer foo)
201 {
202     return TRUE;
203 }
204
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 )
212 {
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[] = { "" };
216 #ifdef HAVE_GNOME_H
217     static char **pp_args  = p_args;
218 #endif
219     static int    i_args   = 1;
220
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;
224
225     /* FIXME: deprecated ? */
226     /* gdk_threads_init(); */
227
228 #ifdef HAVE_GNOME_H
229     gnome_init( p_this->p_vlc->psz_object_name, VERSION, i_args, p_args );
230 #else
231     gtk_set_locale();
232     gtk_init( &i_args, &pp_args );
233 #endif
234
235     gdk_threads_enter();
236
237     vlc_thread_ready( p_this );
238
239     /* If we don't add this simple timeout, gtk_main remains stuck ... */
240     gtk_timeout_add( INTF_IDLE_SLEEP / 1000, foo, p_this );
241
242     /* Enter Gtk mode */
243     gtk_main();
244
245     gdk_threads_leave();
246 }
247