]> git.sesse.net Git - vlc/blob - modules/misc/gtk_main.c
Copyright fixes
[vlc] / modules / misc / gtk_main.c
1 /*****************************************************************************
2  * gtk_main.c : Gtk+ wrapper for gtk_main
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN (Centrale Réseaux) and its contributors
5  * $Id$
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 #if defined(MODULE_NAME_IS_gtk2_main)
34 #   include <glib.h>
35 #endif
36
37 #if defined(MODULE_NAME_IS_gnome_main) || defined(MODULE_NAME_IS_gnome2_main)
38 #   include <gnome.h>
39 #endif
40
41 /*****************************************************************************
42  * Local prototypes.
43  *****************************************************************************/
44 static int  Open    ( vlc_object_t * );
45 static void Close   ( vlc_object_t * );
46
47 static void GtkMain ( vlc_object_t * );
48
49 /*****************************************************************************
50  * Local variables (mutex-protected).
51  *****************************************************************************/
52 static int            i_refcount = 0;
53 static vlc_object_t * p_gtk_main = NULL;
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58 vlc_module_begin();
59     int i_cap;
60     set_description( _("Gtk+ GUI helper") );
61 #if defined(MODULE_NAME_IS_gtk_main)
62     i_cap = 90;
63     add_shortcut( "gtk" );
64 #elif defined(MODULE_NAME_IS_gnome_main)
65     i_cap = 100;
66     add_shortcut( "gtk" );
67     add_shortcut( "gnome" );
68 #elif defined(MODULE_NAME_IS_gtk2_main)
69     i_cap = 95;
70     add_shortcut( "gtk2" );
71 #elif defined(MODULE_NAME_IS_gnome2_main)
72     i_cap = 105;
73     add_shortcut( "gtk2" );
74     add_shortcut( "gnome2" );
75 #endif
76     set_capability( "gui-helper", i_cap );
77     set_callbacks( Open, Close );
78     linked_with_a_crap_library_which_uses_atexit();
79 vlc_module_end();
80
81 /*****************************************************************************
82  * Open: initialize and create window
83  *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
85 {
86     vlc_value_t lockval;
87
88     /* FIXME: put this in the module (de)initialization ASAP */
89     var_Create( p_this->p_libvlc, "gtk", VLC_VAR_MUTEX );
90
91     var_Get( p_this->p_libvlc, "gtk", &lockval );
92     vlc_mutex_lock( lockval.p_address );
93
94     if( i_refcount > 0 )
95     {
96         i_refcount++;
97         vlc_mutex_unlock( lockval.p_address );
98
99         return VLC_SUCCESS;
100     }
101
102     p_gtk_main = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
103
104     /* Only initialize gthreads if it's the first time we do it */
105     if( !g_thread_supported() )
106     {
107         g_thread_init( NULL );
108     }
109
110     /* Launch the gtk_main() thread. It will not return until it has
111      * called gdk_threads_enter(), which ensures us thread safety. */
112     if( vlc_thread_create( p_gtk_main, "gtk_main", GtkMain,
113                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
114     {
115         vlc_object_destroy( p_gtk_main );
116         i_refcount--;
117         vlc_mutex_unlock( lockval.p_address );
118         var_Destroy( p_this->p_libvlc, "gtk" );
119         return VLC_ETHREAD;
120     }
121
122     i_refcount++;
123     vlc_mutex_unlock( lockval.p_address );
124
125     return VLC_SUCCESS;
126 }
127
128 /*****************************************************************************
129  * Close: destroy interface window
130  *****************************************************************************/
131 static void Close( vlc_object_t *p_this )
132 {
133     vlc_value_t lockval;
134
135     var_Get( p_this->p_libvlc, "gtk", &lockval );
136     vlc_mutex_lock( lockval.p_address );
137
138     i_refcount--;
139
140     if( i_refcount > 0 )
141     {
142         vlc_mutex_unlock( lockval.p_address );
143         var_Destroy( p_this->p_libvlc, "gtk" );
144         return;
145     }
146
147     gtk_main_quit();
148     vlc_thread_join( p_gtk_main );
149
150     vlc_object_destroy( p_gtk_main );
151     p_gtk_main = NULL;
152
153     vlc_mutex_unlock( lockval.p_address );
154     var_Destroy( p_this->p_libvlc, "gtk" );
155 }
156
157 static gint foo( gpointer bar ) { return TRUE; }
158
159 /*****************************************************************************
160  * GtkMain: Gtk+ 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  *****************************************************************************/
165 static void GtkMain( vlc_object_t *p_this )
166 {
167     /* gtk_init needs to know the command line. We don't care, so we
168      * give it an empty one */
169     static char  *p_args[] = { "", NULL };
170 #if defined(MODULE_NAME_IS_gtk_main) || defined(MODULE_NAME_IS_gtk2_main)
171     static char **pp_args  = p_args;
172 #endif
173     static int    i_args   = 1;
174
175     /* FIXME: deprecated ? */
176 #if defined(MODULE_NAME_IS_gtk2_main) || defined(MODULE_NAME_IS_gnome2_main)
177     gdk_threads_init();
178 #endif
179
180 #if defined(MODULE_NAME_IS_gnome_main)
181     gnome_init( p_this->p_vlc->psz_object_name, VERSION, i_args, p_args );
182 #elif defined(MODULE_NAME_IS_gnome2_main)
183     gnome_program_init( PACKAGE, VERSION, LIBGNOMEUI_MODULE,
184                         i_args, p_args,
185                         GNOME_PARAM_APP_DATADIR, "",//PACKAGE_DATA_DIR,
186                         NULL );
187 #else
188     gtk_set_locale();
189     gtk_init( &i_args, &pp_args );
190 #endif
191
192     gdk_threads_enter();
193
194     vlc_thread_ready( p_this );
195
196     /* If we don't add this simple timeout, gtk_main remains stuck if
197      * we try to close the window without having sent any gtk event. */
198     gtk_timeout_add( INTF_IDLE_SLEEP / 1000, foo, p_this );
199
200     /* Enter Gtk mode */
201     gtk_main();
202
203     gdk_threads_leave();
204 }
205