]> git.sesse.net Git - vlc/blob - modules/misc/gtk_main.c
* ./configure.ac.in: removed now unnecessary --force-exe-suffix flag.
[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.9 2002/10/04 18:07:21 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 MODULE_NAME_IS_gnome_main
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 } gtk_main_t;
55
56 /*****************************************************************************
57  * Local variables (mutex-protected).
58  *****************************************************************************/
59 static vlc_mutex_t * p_gtklock;
60 static int           i_refcount = 0;
61 static gtk_main_t *  p_gtk_main = NULL;
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66 vlc_module_begin();
67     set_description( _("Gtk+ helper module") );
68 #ifdef MODULE_NAME_IS_gtk_main
69     set_capability( "gtk_main", 90 );
70 #else
71     set_capability( "gtk_main", 100 );
72     add_shortcut( "gnome" );
73 #endif
74     add_shortcut( "gtk" );
75     set_callbacks( Open, Close );
76     linked_with_a_crap_library_which_uses_atexit();
77 vlc_module_end();
78
79 /*****************************************************************************
80  * Open: initialize and create window
81  *****************************************************************************/
82 static int Open( vlc_object_t *p_this )
83 {
84     /* FIXME: put this in the module (de)initialization ASAP */
85     p_gtklock = vlc_mutex_need( p_this, "gtk" );
86
87     vlc_mutex_lock( p_gtklock );
88
89     if( i_refcount > 0 )
90     {
91         i_refcount++;
92         vlc_mutex_unlock( p_gtklock );
93
94         return VLC_SUCCESS;
95     }
96
97     p_gtk_main = vlc_object_create( p_this, sizeof(gtk_main_t) );
98
99     /* Only initialize gthreads if it's the first time we do it */
100     if( !g_thread_supported() )
101     {
102         g_thread_init( NULL );
103     }
104
105     /* Launch the gtk_main() thread. It will not return until it has
106      * called gdk_threads_enter(), which ensures us thread safety. */
107     if( vlc_thread_create( p_gtk_main, "gtk_main", GtkMain,
108                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
109     {
110         vlc_object_destroy( p_gtk_main );
111         i_refcount--;
112         vlc_mutex_unlock( p_gtklock );
113         vlc_mutex_unneed( p_this, "gtk" );
114         return VLC_ETHREAD;
115     }
116
117     i_refcount++;
118     vlc_mutex_unlock( p_gtklock );
119
120     return VLC_SUCCESS;
121 }
122
123 /*****************************************************************************
124  * Close: destroy interface window
125  *****************************************************************************/
126 static void Close( vlc_object_t *p_this )
127 {
128     vlc_mutex_lock( p_gtklock );
129
130     i_refcount--;
131
132     if( i_refcount > 0 )
133     {
134         vlc_mutex_unlock( p_gtklock );
135         vlc_mutex_unneed( p_this, "gtk" );
136         return;
137     }
138
139     gtk_main_quit();
140     vlc_thread_join( p_gtk_main );
141
142     vlc_object_destroy( p_gtk_main );
143     p_gtk_main = NULL;
144
145     vlc_mutex_unlock( p_gtklock );
146     vlc_mutex_unneed( p_this, "gtk" );
147 }
148
149 static gint foo( gpointer bar ) { return TRUE; }
150
151 /*****************************************************************************
152  * GtkMain: Gtk+ thread
153  *****************************************************************************
154  * this part of the interface is in a separate thread so that we can call
155  * gtk_main() from within it without annoying the rest of the program.
156  *****************************************************************************/
157 static void GtkMain( vlc_object_t *p_this )
158 {
159     /* gtk_init needs to know the command line. We don't care, so we
160      * give it an empty one */
161     static char  *p_args[] = { "" };
162 #ifdef MODULE_NAME_IS_gtk_main
163     static char **pp_args  = p_args;
164 #endif
165     static int    i_args   = 1;
166
167     /* FIXME: deprecated ? */
168     /* gdk_threads_init(); */
169
170 #ifdef MODULE_NAME_IS_gnome_main
171     gnome_init( p_this->p_vlc->psz_object_name, VERSION, i_args, p_args );
172 #else
173     gtk_set_locale();
174     gtk_init( &i_args, &pp_args );
175 #endif
176
177     gdk_threads_enter();
178
179     vlc_thread_ready( p_this );
180
181     /* If we don't add this simple timeout, gtk_main remains stuck if
182      * we try to close the window without having sent any gtk event. */
183     gtk_timeout_add( INTF_IDLE_SLEEP / 1000, foo, p_this );
184
185     /* Enter Gtk mode */
186     gtk_main();
187
188     gdk_threads_leave();
189 }
190