Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

gtk_main.c

00001 /*****************************************************************************
00002  * gtk_main.c : Gtk+ wrapper for gtk_main
00003  *****************************************************************************
00004  * Copyright (C) 2002 the VideoLAN team
00005  * $Id: gtk_main.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Samuel Hocevar <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <vlc/vlc.h>
00028 
00029 #include <stdlib.h>                                              /* atexit() */
00030 
00031 #include <gtk/gtk.h>
00032 
00033 #if defined(MODULE_NAME_IS_gtk2_main)
00034 #   include <glib.h>
00035 #endif
00036 
00037 #if defined(MODULE_NAME_IS_gnome_main) || defined(MODULE_NAME_IS_gnome2_main)
00038 #   include <gnome.h>
00039 #endif
00040 
00041 /*****************************************************************************
00042  * Local prototypes.
00043  *****************************************************************************/
00044 static int  Open    ( vlc_object_t * );
00045 static void Close   ( vlc_object_t * );
00046 
00047 static void GtkMain ( vlc_object_t * );
00048 
00049 /*****************************************************************************
00050  * Local variables (mutex-protected).
00051  *****************************************************************************/
00052 static int            i_refcount = 0;
00053 static vlc_object_t * p_gtk_main = NULL;
00054 
00055 /*****************************************************************************
00056  * Module descriptor
00057  *****************************************************************************/
00058 vlc_module_begin();
00059     int i_cap;
00060     set_description( _("Gtk+ GUI helper") );
00061 #if defined(MODULE_NAME_IS_gtk_main)
00062     i_cap = 90;
00063     add_shortcut( "gtk" );
00064 #elif defined(MODULE_NAME_IS_gnome_main)
00065     i_cap = 100;
00066     add_shortcut( "gtk" );
00067     add_shortcut( "gnome" );
00068 #elif defined(MODULE_NAME_IS_gtk2_main)
00069     i_cap = 95;
00070     add_shortcut( "gtk2" );
00071 #elif defined(MODULE_NAME_IS_gnome2_main)
00072     i_cap = 105;
00073     add_shortcut( "gtk2" );
00074     add_shortcut( "gnome2" );
00075 #endif
00076     set_capability( "gui-helper", i_cap );
00077     set_callbacks( Open, Close );
00078     linked_with_a_crap_library_which_uses_atexit();
00079 vlc_module_end();
00080 
00081 /*****************************************************************************
00082  * Open: initialize and create window
00083  *****************************************************************************/
00084 static int Open( vlc_object_t *p_this )
00085 {
00086     vlc_value_t lockval;
00087 
00088     /* FIXME: put this in the module (de)initialization ASAP */
00089     var_Create( p_this->p_libvlc, "gtk", VLC_VAR_MUTEX );
00090 
00091     var_Get( p_this->p_libvlc, "gtk", &lockval );
00092     vlc_mutex_lock( lockval.p_address );
00093 
00094     if( i_refcount > 0 )
00095     {
00096         i_refcount++;
00097         vlc_mutex_unlock( lockval.p_address );
00098 
00099         return VLC_SUCCESS;
00100     }
00101 
00102     p_gtk_main = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
00103 
00104     /* Only initialize gthreads if it's the first time we do it */
00105     if( !g_thread_supported() )
00106     {
00107         g_thread_init( NULL );
00108     }
00109 
00110     /* Launch the gtk_main() thread. It will not return until it has
00111      * called gdk_threads_enter(), which ensures us thread safety. */
00112     if( vlc_thread_create( p_gtk_main, "gtk_main", GtkMain,
00113                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
00114     {
00115         vlc_object_destroy( p_gtk_main );
00116         i_refcount--;
00117         vlc_mutex_unlock( lockval.p_address );
00118         var_Destroy( p_this->p_libvlc, "gtk" );
00119         return VLC_ETHREAD;
00120     }
00121 
00122     i_refcount++;
00123     vlc_mutex_unlock( lockval.p_address );
00124 
00125     return VLC_SUCCESS;
00126 }
00127 
00128 /*****************************************************************************
00129  * Close: destroy interface window
00130  *****************************************************************************/
00131 static void Close( vlc_object_t *p_this )
00132 {
00133     vlc_value_t lockval;
00134 
00135     var_Get( p_this->p_libvlc, "gtk", &lockval );
00136     vlc_mutex_lock( lockval.p_address );
00137 
00138     i_refcount--;
00139 
00140     if( i_refcount > 0 )
00141     {
00142         vlc_mutex_unlock( lockval.p_address );
00143         var_Destroy( p_this->p_libvlc, "gtk" );
00144         return;
00145     }
00146 
00147     gtk_main_quit();
00148     vlc_thread_join( p_gtk_main );
00149 
00150     vlc_object_destroy( p_gtk_main );
00151     p_gtk_main = NULL;
00152 
00153     vlc_mutex_unlock( lockval.p_address );
00154     var_Destroy( p_this->p_libvlc, "gtk" );
00155 }
00156 
00157 static gint foo( gpointer bar ) { return TRUE; }
00158 
00159 /*****************************************************************************
00160  * GtkMain: Gtk+ thread
00161  *****************************************************************************
00162  * this part of the interface is in a separate thread so that we can call
00163  * gtk_main() from within it without annoying the rest of the program.
00164  *****************************************************************************/
00165 static void GtkMain( vlc_object_t *p_this )
00166 {
00167     /* gtk_init needs to know the command line. We don't care, so we
00168      * give it an empty one */
00169     static char  *p_args[] = { "", NULL };
00170 #if defined(MODULE_NAME_IS_gtk_main) || defined(MODULE_NAME_IS_gtk2_main)
00171     static char **pp_args  = p_args;
00172 #endif
00173     static int    i_args   = 1;
00174 
00175     /* FIXME: deprecated ? */
00176 #if defined(MODULE_NAME_IS_gtk2_main) || defined(MODULE_NAME_IS_gnome2_main)
00177     gdk_threads_init();
00178 #endif
00179 
00180 #if defined(MODULE_NAME_IS_gnome_main)
00181     gnome_init( p_this->p_vlc->psz_object_name, VERSION, i_args, p_args );
00182 #elif defined(MODULE_NAME_IS_gnome2_main)
00183     gnome_program_init( PACKAGE, VERSION, LIBGNOMEUI_MODULE,
00184                         i_args, p_args,
00185                         GNOME_PARAM_APP_DATADIR, "",//PACKAGE_DATA_DIR,
00186                         NULL );
00187 #else
00188     gtk_set_locale();
00189     gtk_init( &i_args, &pp_args );
00190 #endif
00191 
00192     gdk_threads_enter();
00193 
00194     vlc_thread_ready( p_this );
00195 
00196     /* If we don't add this simple timeout, gtk_main remains stuck if
00197      * we try to close the window without having sent any gtk event. */
00198     gtk_timeout_add( INTF_IDLE_SLEEP / 1000, foo, p_this );
00199 
00200     /* Enter Gtk mode */
00201     gtk_main();
00202 
00203     gdk_threads_leave();
00204 }
00205 

Generated on Tue Dec 20 10:14:48 2005 for vlc-0.8.4a by  doxygen 1.4.2