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