]> git.sesse.net Git - vlc/blob - plugins/gtk/intf_gtk.c
*More cleanification in gtk interface: now to change the
[vlc] / plugins / gtk / intf_gtk.c
1 /*****************************************************************************
2  * intf_gtk.c: Gtk+ interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: intf_gtk.c,v 1.20 2001/05/19 00:39:30 stef 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 "config.h"
41 #include "common.h"
42 #include "threads.h"
43 #include "mtime.h"
44 #include "tests.h"
45 #include "modules.h"
46
47 #include "stream_control.h"
48 #include "input_ext-intf.h"
49
50 #include "interface.h"
51 #include "intf_msg.h"
52 #include "intf_playlist.h"
53
54 #include "video.h"
55 #include "video_output.h"
56
57 #include "gtk_callbacks.h"
58 #include "gtk_interface.h"
59 #include "gtk_support.h"
60 #include "gtk_menu.h"
61 #include "intf_gtk.h"
62
63 #include "main.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 static gint GtkModeManage   ( intf_thread_t * p_intf );
75 static void GtkDisplayDate  ( GtkAdjustment *p_adj );
76
77 /*****************************************************************************
78  * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
79  *****************************************************************************
80  * gtk_init() makes several calls to g_atexit() which calls atexit() to
81  * register tidying callbacks to be called at program exit. Since the Gtk+
82  * plugin is likely to be unloaded at program exit, we have to export this
83  * symbol to intercept the g_atexit() calls. Talk about crude hack.
84  *****************************************************************************/
85 void g_atexit( GVoidFunc func )
86 {
87     intf_thread_t *p_intf = p_main->p_intf;
88
89     if( p_intf->p_sys->pf_gdk_callback == NULL )
90     {
91         p_intf->p_sys->pf_gdk_callback = func;
92     }
93     else if( p_intf->p_sys->pf_gtk_callback == NULL )
94     {
95         p_intf->p_sys->pf_gtk_callback = func;
96     }
97     /* else nothing, but we could do something here */
98     return;
99 }
100
101 /*****************************************************************************
102  * Functions exported as capabilities. They are declared as static so that
103  * we don't pollute the namespace too much.
104  *****************************************************************************/
105 void _M( intf_getfunctions )( function_list_t * p_function_list )
106 {
107     p_function_list->pf_probe = intf_Probe;
108     p_function_list->functions.intf.pf_open  = intf_Open;
109     p_function_list->functions.intf.pf_close = intf_Close;
110     p_function_list->functions.intf.pf_run   = intf_Run;
111 }
112
113 /*****************************************************************************
114  * intf_Probe: probe the interface and return a score
115  *****************************************************************************
116  * This function tries to initialize Gtk+ and returns a score to the
117  * plugin manager so that it can select the best plugin.
118  *****************************************************************************/
119 static int intf_Probe( probedata_t *p_data )
120 {
121     if( TestMethod( INTF_METHOD_VAR, "gtk" ) )
122     {
123         return( 999 );
124     }
125
126     if( TestProgram( "gvlc" ) )
127     {
128         return( 190 );
129     }
130
131     return( 90 );
132 }
133
134 /*****************************************************************************
135  * intf_Open: initialize and create window
136  *****************************************************************************/
137 static int intf_Open( intf_thread_t *p_intf )
138 {
139     /* Allocate instance and initialize some members */
140     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
141     if( p_intf->p_sys == NULL )
142     {
143         intf_ErrMsg("error: %s", strerror(ENOMEM));
144         return( 1 );
145     }
146
147     /* Initialize Gtk+ thread */
148     p_intf->p_sys->b_popup_changed = 0;
149     p_intf->p_sys->b_window_changed = 0;
150     p_intf->p_sys->b_playlist_changed = 0;
151
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         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
189         { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
190     };
191
192     /* intf_Manage callback timeout */
193     int i_timeout;
194
195     /* Initialize Gtk+ */
196     gtk_init( &i_args, &pp_args );
197
198     /* Create some useful widgets that will certainly be used */
199     p_intf->p_sys->p_window = create_intf_window( );
200     p_intf->p_sys->p_popup = create_intf_popup( );
201     p_intf->p_sys->p_playlist = create_intf_playlist();
202     
203     /* Set the title of the main window */
204     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
205                           VOUT_TITLE " (Gtk+ interface)");
206
207     /* Accept file drops on the main window */
208     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
209                        GTK_DEST_DEFAULT_ALL, target_table,
210                        1, GDK_ACTION_COPY );
211
212     /* Accept file drops on the playlist window */
213     gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playlist,
214                                    "playlist_clist") ),
215                        GTK_DEST_DEFAULT_ALL, target_table,
216                        1, GDK_ACTION_COPY );
217
218     /* Get the interface labels */
219     p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
220         GTK_OBJECT(p_intf->p_sys->p_window ), "slider_frame" ) ); 
221
222 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
223                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
224     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
225     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
226 #undef P_LABEL
227
228     /* Connect the date display to the slider */
229 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
230                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
231     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
232
233     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
234                          GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
235     p_intf->p_sys->f_adj_oldvalue = 0;
236 #undef P_SLIDER
237
238     /* We don't create these ones yet because we perhaps won't need them */
239     p_intf->p_sys->p_about = NULL;
240     p_intf->p_sys->p_modules = NULL;
241     p_intf->p_sys->p_fileopen = NULL;
242     p_intf->p_sys->p_disc = NULL;
243     p_intf->p_sys->p_network = NULL;
244     p_intf->p_sys->p_preferences = NULL;
245     p_intf->p_sys->p_jump = NULL;
246
247     /* Store p_intf to keep an eye on it */
248     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
249                          "p_intf", p_intf );
250
251     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
252                          "p_intf", p_intf );
253
254     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
255                          "p_intf", p_intf );
256
257     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
258                          "p_intf", p_intf );
259
260     /* Show the control window */
261     gtk_widget_show( p_intf->p_sys->p_window );
262
263     /* Sleep to avoid using all CPU - since some interfaces needs to access
264      * keyboard events, a 100ms delay is a good compromise */
265     i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GtkManage, p_intf );
266
267     /* Enter Gtk mode */
268     gtk_main();
269
270     /* Remove the timeout */
271     gtk_timeout_remove( i_timeout );
272
273     /* Launch stored callbacks */
274     if( p_intf->p_sys->pf_gtk_callback != NULL )
275     {
276         p_intf->p_sys->pf_gtk_callback();
277
278         if( p_intf->p_sys->pf_gdk_callback != NULL )
279         {
280             p_intf->p_sys->pf_gdk_callback();
281         }
282     }
283 }
284
285 /* following functions are local */
286
287 /*****************************************************************************
288  * GtkManage: manage main thread messages
289  *****************************************************************************
290  * In this function, called approx. 10 times a second, we check what the
291  * main program wanted to tell us.
292  *****************************************************************************/
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         }
326
327         /* Manage the slider */
328         if( p_intf->p_input->stream.b_seekable )
329         {
330             float newvalue = p_intf->p_sys->p_adj->value;
331     
332 #define p_area p_intf->p_input->stream.p_selected_area
333             /* If the user hasn't touched the slider since the last time,
334              * then the input can safely change it */
335             if( newvalue == p_intf->p_sys->f_adj_oldvalue )
336             {
337                 /* Update the value */
338                 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
339                     ( 100. * p_area->i_tell ) / p_area->i_size;
340     
341                 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
342                                          "value_changed" );
343             }
344             /* Otherwise, send message to the input if the user has
345              * finished dragging the slider */
346             else if( p_intf->p_sys->b_slider_free )
347             {
348                 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
349
350                 /* release the lock to be able to seek */
351                 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
352                 input_Seek( p_intf->p_input, i_seek );
353                 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
354     
355                 /* Update the old value */
356                 p_intf->p_sys->f_adj_oldvalue = newvalue;
357             }
358 #undef p_area
359         }
360         vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
361
362         if( p_intf->p_sys->i_part !=
363             p_intf->p_input->stream.p_selected_area->i_part )
364         {
365             p_intf->p_sys->b_chapter_update = 1;
366             GtkSetupMenus( p_intf );
367         }
368
369     }
370     else if( !p_intf->b_die )
371     {
372         GtkModeManage( p_intf );
373     }
374
375     /* Manage core vlc functions through the callback */
376     p_intf->pf_manage( p_intf );
377
378     if( p_intf->b_die )
379     {
380         vlc_mutex_unlock( &p_intf->change_lock );
381
382         /* Prepare to die, young Skywalker */
383         gtk_main_quit();
384
385         /* Just in case */
386         return( FALSE );
387     }
388
389     vlc_mutex_unlock( &p_intf->change_lock );
390
391     return( TRUE );
392
393 #undef p_intf
394 }
395
396 /*****************************************************************************
397  * GtkDisplayDate: display stream date
398  *****************************************************************************
399  * This function displays the current date related to the position in
400  * the stream. It is called whenever the slider changes its value.
401  * The lock has to be taken before you call the function.
402  *****************************************************************************/
403 void GtkDisplayDate( GtkAdjustment *p_adj )
404 {
405     intf_thread_t *p_intf;
406    
407     p_intf = gtk_object_get_data( GTK_OBJECT( p_adj ), "p_intf" );
408
409     if( p_intf->p_input != NULL )
410     {
411 #define p_area p_intf->p_input->stream.p_selected_area
412         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
413
414         gtk_frame_set_label( GTK_FRAME( p_intf->p_sys->p_slider_frame ),
415                             input_OffsetToTime( p_intf->p_input, psz_time,
416                                    ( p_area->i_size * p_adj->value ) / 100 ) );
417 #undef p_area
418      }
419 }
420
421
422 /*****************************************************************************
423  * GtkModeManage: actualise the aspect of the interface whenever the input
424  *                changes.
425  *****************************************************************************
426  * The lock has to be taken before you call the function.
427  *****************************************************************************/
428 static gint GtkModeManage( intf_thread_t * p_intf )
429 {
430     GtkWidget *     p_dvd_box;
431     GtkWidget *     p_file_box;
432     GtkWidget *     p_network_box;
433     GtkWidget *     p_slider;
434     GtkWidget *     p_label;
435     boolean_t       b_control;
436
437 #define GETWIDGET( ptr, name ) GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( \
438                            p_intf->p_sys->ptr ) , ( name ) ) )
439     /* hide all boxes except default file box */
440     p_file_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
441                  p_intf->p_sys->p_window ), "file_box" ) );
442     gtk_widget_hide( GTK_WIDGET( p_file_box ) );
443
444     p_network_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
445                  p_intf->p_sys->p_window ), "network_box" ) );
446     gtk_widget_hide( GTK_WIDGET( p_network_box ) );
447
448     p_dvd_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
449                  p_intf->p_sys->p_window ), "dvd_box" ) );
450     gtk_widget_hide( GTK_WIDGET( p_dvd_box ) );
451
452     /* hide slider */
453     p_slider = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
454                            p_intf->p_sys->p_window ), "slider_frame" ) );
455     gtk_widget_hide( GTK_WIDGET( p_slider ) );
456
457     /* controls unavailable */
458     b_control = 0;
459
460     /* show the box related to current input mode */
461     if( p_intf->p_input != NULL )
462     {
463         switch( p_intf->p_input->stream.i_method & 0xf0 )
464         {
465             case INPUT_METHOD_FILE:
466                 gtk_widget_show( GTK_WIDGET( p_file_box ) );
467                 p_label = gtk_object_get_data( GTK_OBJECT(
468                             p_intf->p_sys->p_window ),
469                             "label_status" );
470                 gtk_label_set_text( GTK_LABEL( p_label ),
471                                     p_intf->p_input->p_source );
472                 break;
473             case INPUT_METHOD_DISC:
474                 gtk_widget_show( GTK_WIDGET( p_dvd_box ) );
475                 break;
476             case INPUT_METHOD_NETWORK:
477                 gtk_widget_show( GTK_WIDGET( p_network_box ) );
478                 p_label = gtk_object_get_data( GTK_OBJECT(
479                             p_intf->p_sys->p_window ),
480                             "network_address_label" );
481                 gtk_label_set_text( GTK_LABEL( p_label ),
482                                     p_intf->p_input->p_source );
483                 break;
484             default:
485                 intf_ErrMsg( "intf error: can't determine input method" );
486                 break;
487         }
488     
489         /* slider for seekable streams */
490         if( p_intf->p_input->stream.b_seekable )
491         {
492             gtk_widget_show( GTK_WIDGET( p_slider ) );
493         }
494     
495         /* control buttons for free pace streams */
496         b_control = p_intf->p_input->stream.b_pace_control;
497
498         /* get ready for menu regeneration */
499         p_intf->p_sys->b_title_update = 1;
500         p_intf->p_sys->b_chapter_update = 1;
501         p_intf->p_sys->b_angle_update = 1;
502         p_intf->p_sys->b_audio_update = 1;
503         p_intf->p_sys->b_spu_update = 1;
504         p_intf->p_sys->i_part = 0;
505     
506         p_intf->p_input->stream.b_changed = 0;
507         intf_WarnMsg( 3, 
508                       "intf info: menus refreshed as stream has changed" );
509     }
510     else
511     {
512         /* default mode */
513         p_label = gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_window ),
514                         "label_status" );
515         gtk_label_set_text( GTK_LABEL( p_label ), "" );
516         gtk_widget_show( GTK_WIDGET( p_file_box ) );
517
518         /* unsensitize menus */
519         gtk_widget_set_sensitive( GETWIDGET(p_window,"menubar_title"), FALSE );
520         gtk_widget_set_sensitive( GETWIDGET(p_window,"menubar_chapter"),
521                                   FALSE );
522         gtk_widget_set_sensitive( GETWIDGET(p_window,"menubar_angle"), FALSE );
523         gtk_widget_set_sensitive( GETWIDGET(p_window,"menubar_audio"), FALSE );
524         gtk_widget_set_sensitive( GETWIDGET(p_window,"menubar_subpictures"),
525                                   FALSE );
526         gtk_widget_set_sensitive( GETWIDGET(p_popup,"popup_navigation"),
527                                   FALSE );
528         gtk_widget_set_sensitive( GETWIDGET(p_popup,"popup_angle"), FALSE );
529         gtk_widget_set_sensitive( GETWIDGET(p_popup,"popup_audio"), FALSE );
530         gtk_widget_set_sensitive( GETWIDGET(p_popup,"popup_subpictures"),
531                                   FALSE );
532     }
533
534     /* set control items */
535     gtk_widget_set_sensitive( GETWIDGET(p_window, "toolbar_back"), FALSE );
536     gtk_widget_set_sensitive( GETWIDGET(p_window, "toolbar_stop"), b_control );
537     gtk_widget_set_sensitive( GETWIDGET(p_window, "toolbar_pause"), b_control );
538     gtk_widget_set_sensitive( GETWIDGET(p_window, "toolbar_slow"), b_control );
539     gtk_widget_set_sensitive( GETWIDGET(p_window, "toolbar_fast"), b_control );
540     gtk_widget_set_sensitive( GETWIDGET(p_popup, "popup_back"), FALSE );
541     gtk_widget_set_sensitive( GETWIDGET(p_popup, "popup_stop"), b_control );
542     gtk_widget_set_sensitive( GETWIDGET(p_popup, "popup_pause"), b_control );
543     gtk_widget_set_sensitive( GETWIDGET(p_popup, "popup_slow"), b_control );
544     gtk_widget_set_sensitive( GETWIDGET(p_popup, "popup_fast"), b_control );
545
546 #undef GETWIDGET
547     return TRUE;
548 }