]> git.sesse.net Git - vlc/blob - plugins/gtk/gnome.c
fbaac117c0735a98a01bb39044829dda6e0471e7
[vlc] / plugins / gtk / gnome.c
1 /*****************************************************************************
2  * gnome.c : Gnome plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id: gnome.c,v 1.6 2002/01/07 02:12:29 sam 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 <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <videolan/vlc.h>
33
34 #include <gnome.h>
35
36 #include "stream_control.h"
37 #include "input_ext-intf.h"
38
39 #include "interface.h"
40 #include "intf_playlist.h"
41
42 #include "video.h"
43 #include "video_output.h"
44
45 #include "gnome_callbacks.h"
46 #include "gnome_interface.h"
47 #include "gnome_support.h"
48 #include "gtk_display.h"
49 #include "gtk_common.h"
50
51 /*****************************************************************************
52  * Local prototypes.
53  *****************************************************************************/
54 static void intf_getfunctions( function_list_t * p_function_list );
55 static int  intf_Probe       ( probedata_t *p_data );
56 static int  intf_Open        ( intf_thread_t *p_intf );
57 static void intf_Close       ( intf_thread_t *p_intf );
58 static void intf_Run         ( intf_thread_t *p_intf );
59
60 static gint GnomeManage      ( gpointer p_data );
61
62 /*****************************************************************************
63  * Building configuration tree
64  *****************************************************************************/
65 MODULE_CONFIG_START
66 MODULE_CONFIG_STOP
67
68 MODULE_INIT_START
69     SET_DESCRIPTION( "Gnome interface module" )
70 #ifndef WIN32
71     if( getenv( "DISPLAY" ) == NULL )
72     {
73         ADD_CAPABILITY( INTF, 15 )
74     }
75     else
76 #endif
77     {
78         ADD_CAPABILITY( INTF, 100 )
79     }
80     ADD_SHORTCUT( "gtk" )
81     ADD_PROGRAM( "gnome-vlc" )
82 MODULE_INIT_STOP
83
84 MODULE_ACTIVATE_START
85     intf_getfunctions( &p_module->p_functions->intf );
86 MODULE_ACTIVATE_STOP
87
88 MODULE_DEACTIVATE_START
89 MODULE_DEACTIVATE_STOP
90
91 /*****************************************************************************
92  * g_atexit: kludge to avoid the Gnome thread to segfault at exit
93  *****************************************************************************
94  * gtk_init() makes several calls to g_atexit() which calls atexit() to
95  * register tidying callbacks to be called at program exit. Since the Gnome
96  * plugin is likely to be unloaded at program exit, we have to export this
97  * symbol to intercept the g_atexit() calls. Talk about crude hack.
98  *****************************************************************************/
99 void g_atexit( GVoidFunc func )
100 {
101     intf_thread_t *p_intf = p_main->p_intf;
102     int i_dummy;
103
104     for( i_dummy = 0;
105          i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
106          i_dummy++ )
107     {
108         ;
109     }
110
111     if( i_dummy >= MAX_ATEXIT - 1 )
112     {
113         intf_ErrMsg( "intf error: too many atexit() callbacks to register" );
114         return;
115     }
116
117     p_intf->p_sys->pf_callback[i_dummy]     = func;
118     p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
119 }
120
121 /*****************************************************************************
122  * Functions exported as capabilities. They are declared as static so that
123  * we don't pollute the namespace too much.
124  *****************************************************************************/
125 static void intf_getfunctions( function_list_t * p_function_list )
126 {
127     p_function_list->pf_probe = intf_Probe;
128     p_function_list->functions.intf.pf_open  = intf_Open;
129     p_function_list->functions.intf.pf_close = intf_Close;
130     p_function_list->functions.intf.pf_run   = intf_Run;
131 }
132
133 /*****************************************************************************
134  * intf_Probe: probe the interface and return a score
135  *****************************************************************************
136  * This function tries to initialize Gnome and returns a score to the
137  * plugin manager so that it can select the best plugin.
138  *****************************************************************************/
139 static int intf_Probe( probedata_t *p_data )
140 {
141 #ifndef WIN32
142     if( getenv( "DISPLAY" ) == NULL )
143     {
144         return( 15 );
145     }
146 #endif
147
148     return( 100 );
149 }
150
151 /*****************************************************************************
152  * intf_Open: initialize and create window
153  *****************************************************************************/
154 static int intf_Open( intf_thread_t *p_intf )
155 {
156     /* Allocate instance and initialize some members */
157     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
158     if( p_intf->p_sys == NULL )
159     {
160         intf_ErrMsg("error: %s", strerror(ENOMEM));
161         return( 1 );
162     }
163
164     /* Initialize Gnome thread */
165     p_intf->p_sys->b_playing = 1;
166     p_intf->p_sys->b_popup_changed = 0;
167     p_intf->p_sys->b_window_changed = 0;
168     p_intf->p_sys->b_playlist_changed = 0;
169
170     p_intf->p_sys->i_playing = -1;
171     p_intf->p_sys->b_slider_free = 1;
172
173     p_intf->p_sys->pf_callback[0] = NULL;
174
175     return( 0 );
176 }
177
178 /*****************************************************************************
179  * intf_Close: destroy interface window
180  *****************************************************************************/
181 static void intf_Close( intf_thread_t *p_intf )
182 {
183     /* Destroy structure */
184     free( p_intf->p_sys );
185 }
186
187 /*****************************************************************************
188  * intf_Run: Gnome thread
189  *****************************************************************************
190  * this part of the interface is in a separate thread so that we can call
191  * gtk_main() from within it without annoying the rest of the program.
192  * XXX: the approach may look kludgy, and probably is, but I could not find
193  * a better way to dynamically load a Gnome interface at runtime.
194  *****************************************************************************/
195 static void intf_Run( intf_thread_t *p_intf )
196 {
197     /* gnome_init needs to know the command line. We don't care, so we
198      * give it an empty one */
199     char *p_args[] = { "" };
200     int   i_args   = 1;
201     int   i_dummy;
202
203     /* The data types we are allowed to receive */
204     static GtkTargetEntry target_table[] =
205     {
206         { "STRING", 0, DROP_ACCEPT_STRING },
207         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
208         { "text/plain",    0, DROP_ACCEPT_TEXT_PLAIN }
209     };
210
211     /* Initialize Gnome */
212     gnome_init( p_main->psz_arg0, VERSION, i_args, p_args );
213
214     /* Create some useful widgets that will certainly be used */
215     p_intf->p_sys->p_window = create_intf_window( );
216     p_intf->p_sys->p_popup = create_intf_popup( );
217     p_intf->p_sys->p_playlist = create_intf_playlist();
218
219     /* Set the title of the main window */
220     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
221                           VOUT_TITLE " (Gnome interface)");
222
223     /* Accept file drops on the main window */
224     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
225                        GTK_DEST_DEFAULT_ALL, target_table,
226                        1, GDK_ACTION_COPY );
227     /* Accept file drops on the playlist window */
228     gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
229                             p_intf->p_sys->p_playlist ), "playlist_clist") ),
230                        GTK_DEST_DEFAULT_ALL, target_table,
231                        1, GDK_ACTION_COPY );
232
233     /* Get the interface labels */
234     p_intf->p_sys->p_slider_frame = gtk_object_get_data(
235                       GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
236     #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
237                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
238     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
239     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
240     #undef P_LABEL
241
242     /* Connect the date display to the slider */
243     #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
244                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
245     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
246
247     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
248                          GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
249     p_intf->p_sys->f_adj_oldvalue = 0;
250     #undef P_SLIDER
251
252     /* We don't create these ones yet because we perhaps won't need them */
253     p_intf->p_sys->p_about = NULL;
254     p_intf->p_sys->p_modules = NULL;
255     p_intf->p_sys->p_fileopen = NULL;
256     p_intf->p_sys->p_disc = NULL;
257     p_intf->p_sys->p_network = NULL;
258     p_intf->p_sys->p_preferences = NULL;
259     p_intf->p_sys->p_jump = NULL;
260
261     /* Store p_intf to keep an eye on it */
262     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
263                          "p_intf", p_intf );
264
265     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
266                          "p_intf", p_intf );
267
268     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
269                          "p_intf", p_intf );
270
271     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
272                          "p_intf", p_intf );
273
274     /* Show the control window */
275     gtk_widget_show( p_intf->p_sys->p_window );
276
277     /* Sleep to avoid using all CPU - since some interfaces needs to access
278      * keyboard events, a 100ms delay is a good compromise */
279     i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
280
281     /* Enter gnome mode */
282     gtk_main();
283
284     /* Remove the timeout */
285     gtk_timeout_remove( i_dummy );
286
287     /* Get rid of stored callbacks so we can unload the plugin */
288     for( i_dummy = 0;
289          i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
290          i_dummy++ )
291     {
292         p_intf->p_sys->pf_callback[i_dummy]();
293     }
294 }
295
296 /* following functions are local */
297
298 /*****************************************************************************
299  * GnomeManage: manage main thread messages
300  *****************************************************************************
301  * In this function, called approx. 10 times a second, we check what the
302  * main program wanted to tell us.
303  *****************************************************************************/
304 static gint GnomeManage( gpointer p_data )
305 {
306 #define p_intf ((intf_thread_t *)p_data)
307
308     vlc_mutex_lock( &p_intf->change_lock );
309
310     /* If the "display popup" flag has changed */
311     if( p_intf->b_menu_change )
312     {
313         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
314         {
315             p_intf->p_sys->p_popup = create_intf_popup();
316             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
317                                  "p_popup", p_intf );
318         }
319
320         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
321                                    NULL, NULL, NULL, NULL );
322         p_intf->b_menu_change = 0;
323     }
324
325     /* update the playlist */
326     GtkPlayListManage( p_intf ); 
327
328     if( p_input_bank->pp_input[0] != NULL && !p_intf->b_die )
329     {
330         vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
331
332         /* New input or stream map change */
333         if( p_input_bank->pp_input[0]->stream.b_changed )
334         {
335             GtkModeManage( p_intf );
336             GtkSetupMenus( p_intf );
337             p_intf->p_sys->b_playing = 1;
338         }
339
340         /* Manage the slider */
341         if( p_input_bank->pp_input[0]->stream.b_seekable )
342         {
343             float           newvalue;
344             newvalue = p_intf->p_sys->p_adj->value;
345     
346 #define p_area p_input_bank->pp_input[0]->stream.p_selected_area
347             /* If the user hasn't touched the slider since the last time,
348              * then the input can safely change it */
349             if( newvalue == p_intf->p_sys->f_adj_oldvalue )
350             {
351                 /* Update the value */
352                 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
353                     ( 100. * p_area->i_tell ) / p_area->i_size;
354     
355                 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
356                                          "value_changed" );
357             }
358             /* Otherwise, send message to the input if the user has
359              * finished dragging the slider */
360             else if( p_intf->p_sys->b_slider_free )
361             {
362                 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
363     
364                 vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
365                 input_Seek( p_input_bank->pp_input[0], i_seek );
366                 vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
367     
368                 /* Update the old value */
369                 p_intf->p_sys->f_adj_oldvalue = newvalue;
370             }
371 #undef p_area
372         }
373
374         if( p_intf->p_sys->i_part !=
375             p_input_bank->pp_input[0]->stream.p_selected_area->i_part )
376         {
377             p_intf->p_sys->b_chapter_update = 1;
378             GtkSetupMenus( p_intf );
379         }
380
381         vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
382     }
383     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
384     {
385         GtkModeManage( p_intf );
386         p_intf->p_sys->b_playing = 0;
387     }
388
389     /* Manage core vlc functions through the callback */
390     p_intf->pf_manage( p_intf );
391
392     if( p_intf->b_die )
393     {
394         vlc_mutex_unlock( &p_intf->change_lock );
395
396         /* Prepare to die, young Skywalker */
397         gtk_main_quit();
398
399         /* Just in case */
400         return( FALSE );
401     }
402
403     vlc_mutex_unlock( &p_intf->change_lock );
404
405     return( TRUE );
406
407 #undef p_intf
408 }
409