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