]> git.sesse.net Git - vlc/blob - plugins/familiar/familiar.c
* ALL: new module API. Makes a few things a lot simpler, and we gain
[vlc] / plugins / familiar / familiar.c
1 /*****************************************************************************
2  * familiar.c : familiar plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: familiar.c,v 1.7 2002/07/31 20:56:51 sam Exp $
6  *
7  * Authors: Jean-Paul Saman <jpsaman@wxs.nl>
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 <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/intf.h>
34
35 #include <gtk/gtk.h>
36
37 #include "familiar_callbacks.h"
38 #include "familiar_interface.h"
39 #include "familiar_support.h"
40 #include "familiar.h"
41
42 /*****************************************************************************
43  * Local variables (mutex-protected).
44  *****************************************************************************/
45 static void ** pp_global_data = NULL;
46
47 /*****************************************************************************
48  * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
49  *****************************************************************************
50  * gtk_init() makes several calls to g_atexit() which calls atexit() to
51  * register tidying callbacks to be called at program exit. Since the Gtk+
52  * plugin is likely to be unloaded at program exit, we have to export this
53  * symbol to intercept the g_atexit() calls. Talk about crude hack.
54  *****************************************************************************/
55 void g_atexit( GVoidFunc func )
56 {
57     intf_thread_t *p_intf;
58
59     int i_dummy;
60
61     if( pp_global_data == NULL )
62     {
63         atexit( func );
64         return;
65     }
66
67     p_intf = (intf_thread_t *)*pp_global_data;
68     if( p_intf == NULL )
69     {
70         return;
71     }
72
73     for( i_dummy = 0;
74          i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
75          i_dummy++ )
76     {
77         ;
78     }
79
80     if( i_dummy >= MAX_ATEXIT - 1 )
81     {
82         msg_Err( p_intf, "too many atexit() callbacks to register" );
83         return;
84     }
85
86     p_intf->p_sys->pf_callback[i_dummy]     = func;
87     p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
88 }
89
90 /*****************************************************************************
91  * Local prototypes.
92  *****************************************************************************/
93 static int  Open         ( vlc_object_t * );
94 static void Close        ( vlc_object_t * );             
95
96 static void Run          ( intf_thread_t * );                  
97
98 /*****************************************************************************
99  * Module descriptor
100  *****************************************************************************/
101 vlc_module_begin();
102     set_description( _("Familiar Linux Gtk+ interface module") );
103     set_capability( "interface", 70 );
104     set_callbacks( Open, Close );
105 vlc_module_end();
106
107 /*****************************************************************************
108  * Open: initialize and create window
109  *****************************************************************************/
110 static int Open( vlc_object_t *p_this )
111 {   
112     intf_thread_t *p_intf = (intf_thread_t *)p_this;
113
114     /* Allocate instance and initialize some members */
115     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
116     if( p_intf->p_sys == NULL )
117     {
118         msg_Err( p_intf, "out of memory" );
119         return( 1 );
120     }
121
122     /* Initialize Gtk+ thread */
123     p_intf->p_sys->p_input = NULL;
124
125     p_intf->pf_run = Run;
126
127     return( 0 );
128 }
129
130 /*****************************************************************************
131  * Close: destroy interface window
132  *****************************************************************************/
133 static void Close( vlc_object_t *p_this )
134 {   
135     intf_thread_t *p_intf = (intf_thread_t *)p_this;
136
137     if( p_intf->p_sys->p_input )
138     {
139         vlc_object_release( p_intf->p_sys->p_input );
140     }
141
142     /* Destroy structure */
143     if (p_intf->p_sys) free( p_intf->p_sys );
144 }
145
146 /*****************************************************************************
147  * Run: Gtk+ thread
148  *****************************************************************************
149  * this part of the interface is in a separate thread so that we can call
150  * gtk_main() from within it without annoying the rest of the program.
151  * XXX: the approach may look kludgy, and probably is, but I could not find
152  * a better way to dynamically load a Gtk+ interface at runtime.
153  *****************************************************************************/
154 static void Run( intf_thread_t *p_intf )
155 {
156     /* gtk_init needs to know the command line. We don't care, so we
157      * give it an empty one */
158     char  *p_args[] = { "" };
159     char **pp_args  = p_args;
160     int    i_args   = 1;
161     int    i_dummy  = 0;
162
163     /* Initialize Gtk+ */
164     gtk_set_locale ();
165
166     /* gtk_init will register stuff with g_atexit, so we need to take
167      * the global lock if we want to be able to intercept the calls */
168     vlc_mutex_lock( p_intf->p_vlc->p_global_lock );
169     *p_intf->p_vlc->pp_global_data = p_intf;
170     gtk_init( &i_args, &pp_args );
171     vlc_mutex_unlock( p_intf->p_vlc->p_global_lock );
172
173     /* Create some useful widgets that will certainly be used */
174 // FIXME: magic path
175     add_pixmap_directory("share");
176     p_intf->p_sys->p_window = create_familiar();
177     if (p_intf->p_sys->p_window == NULL)
178     {
179         msg_Err( p_intf, "unable to create familiar interface" );
180     }
181
182     /* Set the title of the main window */
183     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
184                           VOUT_TITLE " (Familiar Linux interface)");
185
186     /* Get the slider object */
187     p_intf->p_sys->p_notebook = GTK_NOTEBOOK( gtk_object_get_data(
188         GTK_OBJECT( p_intf->p_sys->p_window ), "notebook" ) );
189 //    gtk_widget_hide( GTK_WIDGET(p_intf->p_sys->p_notebook) );
190
191     /* Store p_intf to keep an eye on it */
192     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
193                          "p_intf", p_intf );
194     /* Show the control window */
195     gtk_widget_show( p_intf->p_sys->p_window );
196
197     /* Enter Gtk mode */
198     gtk_main();
199
200     /* Remove the timeout */
201     gtk_timeout_remove( i_dummy );
202 }
203