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