]> git.sesse.net Git - vlc/blob - plugins/gtk/intf_gtk.c
* Forgot a file in my last commit.
[vlc] / plugins / gtk / intf_gtk.c
1 /*****************************************************************************
2  * intf_gtk.c: Gtk+ interface
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: intf_gtk.c,v 1.29 2001/12/07 18:33:07 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Stéphane Borel <stef@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #define MODULE_NAME gtk
26 #include "modules_inner.h"
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include "defs.h"
32
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <stdlib.h>                                                /* free() */
35 #include <string.h>                                            /* strerror() */
36 #include <stdio.h>
37
38 #include <gtk/gtk.h>
39
40 #include "common.h"
41 #include "intf_msg.h"
42 #include "threads.h"
43 #include "mtime.h"
44 #include "tests.h"
45
46 #include "stream_control.h"
47 #include "input_ext-intf.h"
48
49 #include "interface.h"
50 #include "intf_playlist.h"
51
52 #include "video.h"
53 #include "video_output.h"
54
55 #include "gtk_callbacks.h"
56 #include "gtk_interface.h"
57 #include "gtk_support.h"
58 #include "gtk_menu.h"
59 #include "gtk_display.h"
60 #include "intf_gtk.h"
61
62 #include "modules.h"
63 #include "modules_export.h"
64
65 /*****************************************************************************
66  * Local prototypes.
67  *****************************************************************************/
68 static int  intf_Probe      ( probedata_t *p_data );
69 static int  intf_Open       ( intf_thread_t *p_intf );
70 static void intf_Close      ( intf_thread_t *p_intf );
71 static void intf_Run        ( intf_thread_t *p_intf );
72
73 static gint GtkManage       ( gpointer p_data );
74
75 /*****************************************************************************
76  * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
77  *****************************************************************************
78  * gtk_init() makes several calls to g_atexit() which calls atexit() to
79  * register tidying callbacks to be called at program exit. Since the Gtk+
80  * plugin is likely to be unloaded at program exit, we have to export this
81  * symbol to intercept the g_atexit() calls. Talk about crude hack.
82  *****************************************************************************/
83 void g_atexit( GVoidFunc func )
84 {
85     intf_thread_t *p_intf = p_main->p_intf;
86
87     if( p_intf->p_sys->pf_gdk_callback == NULL )
88     {
89         p_intf->p_sys->pf_gdk_callback = func;
90     }
91     else if( p_intf->p_sys->pf_gtk_callback == NULL )
92     {
93         p_intf->p_sys->pf_gtk_callback = func;
94     }
95     /* else nothing, but we could do something here */
96     return;
97 }
98
99 /*****************************************************************************
100  * Functions exported as capabilities. They are declared as static so that
101  * we don't pollute the namespace too much.
102  *****************************************************************************/
103 void _M( intf_getfunctions )( function_list_t * p_function_list )
104 {
105     p_function_list->pf_probe = intf_Probe;
106     p_function_list->functions.intf.pf_open  = intf_Open;
107     p_function_list->functions.intf.pf_close = intf_Close;
108     p_function_list->functions.intf.pf_run   = intf_Run;
109 }
110
111 /*****************************************************************************
112  * intf_Probe: probe the interface and return a score
113  *****************************************************************************
114  * This function tries to initialize Gtk+ and returns a score to the
115  * plugin manager so that it can select the best plugin.
116  *****************************************************************************/
117 static int intf_Probe( probedata_t *p_data )
118 {
119     if( TestMethod( INTF_METHOD_VAR, "gtk" ) )
120     {
121         return( 999 );
122     }
123
124     if( TestProgram( "gvlc" ) )
125     {
126         return( 190 );
127     }
128
129     return( 90 );
130 }
131
132 /*****************************************************************************
133  * intf_Open: initialize and create window
134  *****************************************************************************/
135 static int intf_Open( intf_thread_t *p_intf )
136 {
137     /* Allocate instance and initialize some members */
138     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
139     if( p_intf->p_sys == NULL )
140     {
141         intf_ErrMsg("error: %s", strerror(ENOMEM));
142         return( 1 );
143     }
144
145     /* Initialize Gtk+ thread */
146     p_intf->p_sys->b_playing = 1;
147     p_intf->p_sys->b_popup_changed = 0;
148     p_intf->p_sys->b_window_changed = 0;
149     p_intf->p_sys->b_playlist_changed = 0;
150
151     p_intf->p_sys->i_playing = -1;
152     p_intf->p_sys->b_slider_free = 1;
153
154     p_intf->p_sys->pf_gtk_callback = NULL;
155     p_intf->p_sys->pf_gdk_callback = NULL;
156
157     return( 0 );
158 }
159
160 /*****************************************************************************
161  * intf_Close: destroy interface window
162  *****************************************************************************/
163 static void intf_Close( intf_thread_t *p_intf )
164 {
165     /* Destroy structure */
166     free( p_intf->p_sys );
167 }
168
169 /*****************************************************************************
170  * intf_Run: Gtk+ thread
171  *****************************************************************************
172  * this part of the interface is in a separate thread so that we can call
173  * gtk_main() from within it without annoying the rest of the program.
174  * XXX: the approach may look kludgy, and probably is, but I could not find
175  * a better way to dynamically load a Gtk+ interface at runtime.
176  *****************************************************************************/
177 static void intf_Run( intf_thread_t *p_intf )
178 {
179     /* gtk_init needs to know the command line. We don't care, so we
180      * give it an empty one */
181     char  *p_args[] = { "" };
182     char **pp_args  = p_args;
183     int    i_args   = 1;
184
185     /* The data types we are allowed to receive */
186     static GtkTargetEntry target_table[] =
187     {
188         { "STRING", 0, DROP_ACCEPT_STRING },
189         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
190         { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
191     };
192
193     /* intf_Manage callback timeout */
194     int i_timeout;
195
196     /* Initialize Gtk+ */
197     gtk_init( &i_args, &pp_args );
198
199     /* Create some useful widgets that will certainly be used */
200     p_intf->p_sys->p_window = create_intf_window( );
201     p_intf->p_sys->p_popup = create_intf_popup( );
202     p_intf->p_sys->p_playlist = create_intf_playlist();
203     
204     /* Set the title of the main window */
205     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
206                           VOUT_TITLE " (Gtk+ interface)");
207
208     /* Accept file drops on the main window */
209     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
210                        GTK_DEST_DEFAULT_ALL, target_table,
211                        1, GDK_ACTION_COPY );
212
213     /* Accept file drops on the playlist window */
214     gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playlist,
215                                    "playlist_clist") ),
216                        GTK_DEST_DEFAULT_ALL, target_table,
217                        1, GDK_ACTION_COPY );
218
219     /* Get the interface labels */
220     p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
221         GTK_OBJECT(p_intf->p_sys->p_window ), "slider_frame" ) ); 
222
223 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
224                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
225     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
226     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
227 #undef P_LABEL
228
229     /* Connect the date display to the slider */
230 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
231                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
232     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
233
234     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
235                          GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
236     p_intf->p_sys->f_adj_oldvalue = 0;
237 #undef P_SLIDER
238
239     /* We don't create these ones yet because we perhaps won't need them */
240     p_intf->p_sys->p_about = NULL;
241     p_intf->p_sys->p_modules = NULL;
242     p_intf->p_sys->p_fileopen = NULL;
243     p_intf->p_sys->p_disc = NULL;
244     p_intf->p_sys->p_network = NULL;
245     p_intf->p_sys->p_preferences = NULL;
246     p_intf->p_sys->p_jump = NULL;
247
248     /* Store p_intf to keep an eye on it */
249     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
250                          "p_intf", p_intf );
251
252     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
253                          "p_intf", p_intf );
254
255     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
256                          "p_intf", p_intf );
257
258     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
259                          "p_intf", p_intf );
260
261     /* Show the control window */
262     gtk_widget_show( p_intf->p_sys->p_window );
263
264     /* Sleep to avoid using all CPU - since some interfaces needs to access
265      * keyboard events, a 100ms delay is a good compromise */
266     i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GtkManage, p_intf );
267
268     /* Enter Gtk mode */
269     gtk_main();
270
271     /* Remove the timeout */
272     gtk_timeout_remove( i_timeout );
273
274     /* Launch stored callbacks */
275     if( p_intf->p_sys->pf_gtk_callback != NULL )
276     {
277         p_intf->p_sys->pf_gtk_callback();
278
279         if( p_intf->p_sys->pf_gdk_callback != NULL )
280         {
281             p_intf->p_sys->pf_gdk_callback();
282         }
283     }
284 }
285
286 /* following functions are local */
287
288 /*****************************************************************************
289  * GtkManage: manage main thread messages
290  *****************************************************************************
291  * In this function, called approx. 10 times a second, we check what the
292  * main program wanted to tell us.
293  *****************************************************************************/
294 static gint GtkManage( gpointer p_data )
295 {
296 #define p_intf ((intf_thread_t *)p_data)
297
298     vlc_mutex_lock( &p_intf->change_lock );
299     
300     /* If the "display popup" flag has changed */
301     if( p_intf->b_menu_change )
302     {
303         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
304         {
305             p_intf->p_sys->p_popup = create_intf_popup();
306             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
307                                  "p_popup", p_intf );
308         }
309         gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
310                         NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
311         p_intf->b_menu_change = 0;
312     }
313
314     /* update the playlist */
315     GtkPlayListManage( p_data );
316
317     if( p_intf->p_input != NULL && !p_intf->b_die )
318     {
319         vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
320
321         /* New input or stream map change */
322         if( p_intf->p_input->stream.b_changed )
323         {
324             GtkModeManage( p_intf );
325             GtkSetupMenus( p_intf );
326             p_intf->p_sys->b_playing = 1;
327         }
328
329         /* Manage the slider */
330         if( p_intf->p_input->stream.b_seekable )
331         {
332             float newvalue = p_intf->p_sys->p_adj->value;
333     
334 #define p_area p_intf->p_input->stream.p_selected_area
335             /* If the user hasn't touched the slider since the last time,
336              * then the input can safely change it */
337             if( newvalue == p_intf->p_sys->f_adj_oldvalue )
338             {
339                 /* Update the value */
340                 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
341                     ( 100. * p_area->i_tell ) / p_area->i_size;
342     
343                 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
344                                          "value_changed" );
345             }
346             /* Otherwise, send message to the input if the user has
347              * finished dragging the slider */
348             else if( p_intf->p_sys->b_slider_free )
349             {
350                 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
351
352                 /* release the lock to be able to seek */
353                 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
354                 input_Seek( p_intf->p_input, i_seek );
355                 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
356     
357                 /* Update the old value */
358                 p_intf->p_sys->f_adj_oldvalue = newvalue;
359             }
360 #undef p_area
361         }
362
363         if( p_intf->p_sys->i_part !=
364             p_intf->p_input->stream.p_selected_area->i_part )
365         {
366             p_intf->p_sys->b_chapter_update = 1;
367             GtkSetupMenus( p_intf );
368         }
369
370         vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
371
372     }
373     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
374     {
375         GtkModeManage( p_intf );
376         p_intf->p_sys->b_playing = 0;
377     }
378
379     /* Manage core vlc functions through the callback */
380     p_intf->pf_manage( p_intf );
381
382     if( p_intf->b_die )
383     {
384         vlc_mutex_unlock( &p_intf->change_lock );
385
386         /* Prepare to die, young Skywalker */
387         gtk_main_quit();
388
389         /* Just in case */
390         return( FALSE );
391     }
392
393     vlc_mutex_unlock( &p_intf->change_lock );
394
395     return( TRUE );
396
397 #undef p_intf
398 }