]> git.sesse.net Git - vlc/blob - modules/misc/gtk_main.c
583a536ff7f7e0349525f210783c14921b76e3d2
[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.5 2002/08/29 23:53:22 massiot 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 } gtk_main_t;
55
56 /*****************************************************************************
57  * Local variables (mutex-protected).
58  *****************************************************************************/
59 static int           i_refcount = 0;
60 static gtk_main_t *  p_gtk_main = NULL;
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin();
66     set_description( _("Gtk+ helper module") );
67     set_capability( "gtk_main", 100 );
68     add_shortcut( "gtk" );
69 #ifdef HAVE_GNOME_H
70     add_shortcut( "gnome" );
71 #endif
72     set_callbacks( Open, Close );
73     linked_with_a_crap_library_which_uses_atexit();
74 vlc_module_end();
75
76 /*****************************************************************************
77  * Open: initialize and create window
78  *****************************************************************************/
79 static int Open( vlc_object_t *p_this )
80 {
81     vlc_mutex_lock( p_this->p_vlc->p_global_lock );
82
83     if( i_refcount > 0 )
84     {
85         i_refcount++;
86         vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
87
88         return VLC_SUCCESS;
89     }
90
91     p_gtk_main = vlc_object_create( p_this, sizeof(gtk_main_t) );
92
93     /* Only initialize gthreads if it's the first time we do it */
94     if( !g_thread_supported() )
95     {
96         g_thread_init( NULL );
97     }
98
99     /* Launch the gtk_main() thread. It will not return until it has
100      * called gdk_threads_enter(), which ensures us thread safety. */
101     if( vlc_thread_create( p_gtk_main, "gtk_main", GtkMain,
102                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
103     {
104         vlc_object_destroy( p_gtk_main );
105         i_refcount--;
106         vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
107         return VLC_ETHREAD;
108     }
109
110     i_refcount++;
111     vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
112
113     return VLC_SUCCESS;
114 }
115
116 /*****************************************************************************
117  * Close: destroy interface window
118  *****************************************************************************/
119 static void Close( vlc_object_t *p_this )
120 {
121     vlc_mutex_lock( p_this->p_vlc->p_global_lock );
122
123     i_refcount--;
124
125     if( i_refcount > 0 )
126     {
127         vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
128         return;
129     }
130
131     gtk_main_quit();
132     vlc_thread_join( p_gtk_main );
133
134     vlc_object_destroy( p_gtk_main );
135     p_gtk_main = NULL;
136
137     vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
138 }
139
140 static gint foo(gpointer foo)
141 {
142     return TRUE;
143 }
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 #ifndef HAVE_GNOME_H
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 HAVE_GNOME_H
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 ... */
176     gtk_timeout_add( INTF_IDLE_SLEEP / 1000, foo, p_this );
177
178     /* Enter Gtk mode */
179     gtk_main();
180
181     gdk_threads_leave();
182 }
183