]> git.sesse.net Git - vlc/blob - plugins/gtk/gtk.c
* ALL: the first libvlc commit.
[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.24 2002/06/01 12:31:59 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 <vlc/vlc.h>
33 #include <vlc/intf.h>
34
35 #include <gtk/gtk.h>
36
37 #include "gtk_callbacks.h"
38 #include "gtk_interface.h"
39 #include "gtk_support.h"
40 #include "gtk_menu.h"
41 #include "gtk_display.h"
42 #include "gtk_common.h"
43
44 /*****************************************************************************
45  * Local prototypes.
46  *****************************************************************************/
47 static void intf_getfunctions ( function_list_t * p_function_list );
48 static int  intf_Open         ( intf_thread_t *p_intf );
49 static void intf_Close        ( intf_thread_t *p_intf );
50 static void intf_Run          ( intf_thread_t *p_intf );
51
52 static gint GtkManage         ( gpointer p_data );
53
54 /*****************************************************************************
55  * Local variables (mutex-protected).
56  *****************************************************************************/
57 static void ** pp_global_data;
58
59 /*****************************************************************************
60  * Building configuration tree
61  *****************************************************************************/
62 #define TOOLTIPS_TEXT N_("show tooltips")
63 #define TOOLTIPS_LONGTEXT N_("Show tooltips for configuration options.")
64
65 #define PREFS_MAXH_TEXT N_("maximum height for the configuration windows")
66 #define PREFS_MAXH_LONGTEXT N_( \
67     "You can set the maximum height that the configuration windows in the " \
68     "preferences menu will occupy.")
69
70 MODULE_CONFIG_START
71 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
72 ADD_BOOL( "gtk-tooltips", 1, GtkHideTooltips, TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT)
73 ADD_INTEGER( "gtk-prefs-maxh", 480, NULL, PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT)
74 MODULE_CONFIG_STOP
75
76 MODULE_INIT_START
77     pp_global_data = p_module->p_vlc->pp_global_data;
78     SET_DESCRIPTION( _("Gtk+ interface module") )
79 #ifndef WIN32
80     if( getenv( "DISPLAY" ) == NULL )
81     {
82         ADD_CAPABILITY( INTF, 10 )
83     }
84     else
85 #endif
86     {
87         ADD_CAPABILITY( INTF, 90 )
88     }
89     ADD_PROGRAM( "gvlc" )
90 MODULE_INIT_STOP
91
92 MODULE_ACTIVATE_START
93     intf_getfunctions( &p_module->p_functions->intf );
94 MODULE_ACTIVATE_STOP
95
96 MODULE_DEACTIVATE_START
97 MODULE_DEACTIVATE_STOP
98
99 /*****************************************************************************
100  * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
101  *****************************************************************************
102  * gtk_init() makes several calls to g_atexit() which calls atexit() to
103  * register tidying callbacks to be called at program exit. Since the Gtk+
104  * plugin is likely to be unloaded at program exit, we have to export this
105  * symbol to intercept the g_atexit() calls. Talk about crude hack.
106  *****************************************************************************/
107 void g_atexit( GVoidFunc func )
108 {
109     intf_thread_t *p_intf = (intf_thread_t *)*pp_global_data;
110     int i_dummy;
111
112     for( i_dummy = 0;
113          i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
114          i_dummy++ )
115     {
116         ;
117     }
118
119     if( i_dummy >= MAX_ATEXIT - 1 )
120     {
121         msg_Err( p_intf, "too many atexit() callbacks to register" );
122         return;
123     }
124
125     p_intf->p_sys->pf_callback[i_dummy]     = func;
126     p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
127 }
128
129 /*****************************************************************************
130  * Functions exported as capabilities. They are declared as static so that
131  * we don't pollute the namespace too much.
132  *****************************************************************************/
133 static void intf_getfunctions( function_list_t * p_function_list )
134 {
135     p_function_list->functions.intf.pf_open  = intf_Open;
136     p_function_list->functions.intf.pf_close = intf_Close;
137     p_function_list->functions.intf.pf_run   = intf_Run;
138 }
139
140 /*****************************************************************************
141  * intf_Open: initialize and create window
142  *****************************************************************************/
143 static int intf_Open( intf_thread_t *p_intf )
144 {
145     /* Allocate instance and initialize some members */
146     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
147     if( p_intf->p_sys == NULL )
148     {
149         msg_Err( p_intf, "out of memory" );
150         return( 1 );
151     }
152
153     p_intf->p_sys->p_sub = msg_Subscribe( p_intf->p_this );
154
155     /* Initialize Gtk+ thread */
156     p_intf->p_sys->b_playing = 0;
157     p_intf->p_sys->b_popup_changed = 0;
158     p_intf->p_sys->b_window_changed = 0;
159     p_intf->p_sys->b_playlist_changed = 0;
160
161     p_intf->p_sys->p_input = NULL;
162     p_intf->p_sys->i_playing = -1;
163     p_intf->p_sys->b_slider_free = 1;
164
165     p_intf->p_sys->pf_callback[0] = NULL;
166
167     p_intf->p_sys->i_part = 0;
168
169     return( 0 );
170 }
171
172 /*****************************************************************************
173  * intf_Close: destroy interface window
174  *****************************************************************************/
175 static void intf_Close( intf_thread_t *p_intf )
176 {
177     msg_Unsubscribe( p_intf->p_this, p_intf->p_sys->p_sub );
178
179     /* Destroy structure */
180     free( p_intf->p_sys );
181 }
182
183 /*****************************************************************************
184  * intf_Run: Gtk+ thread
185  *****************************************************************************
186  * this part of the interface is in a separate thread so that we can call
187  * gtk_main() from within it without annoying the rest of the program.
188  * XXX: the approach may look kludgy, and probably is, but I could not find
189  * a better way to dynamically load a Gtk+ interface at runtime.
190  *****************************************************************************/
191 static void intf_Run( intf_thread_t *p_intf )
192 {
193     /* gtk_init needs to know the command line. We don't care, so we
194      * give it an empty one */
195     char  *p_args[] = { "" };
196     char **pp_args  = p_args;
197     int    i_args   = 1;
198     int    i_dummy;
199
200     /* The data types we are allowed to receive */
201     static GtkTargetEntry target_table[] =
202     {
203         { "STRING", 0, DROP_ACCEPT_STRING },
204         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
205         { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
206     };
207
208     /* Initialize Gtk+ */
209
210     /* gtk_init will register stuff with g_atexit, so we need to take
211      * the global lock if we want to be able to intercept the calls */
212     vlc_mutex_lock( p_intf->p_vlc->p_global_lock );
213     *p_intf->p_vlc->pp_global_data = p_intf;
214     gtk_init( &i_args, &pp_args );
215     vlc_mutex_unlock( p_intf->p_vlc->p_global_lock );
216
217     /* Create some useful widgets that will certainly be used */
218     p_intf->p_sys->p_window = create_intf_window();
219     p_intf->p_sys->p_popup = create_intf_popup();
220     p_intf->p_sys->p_playlist = create_intf_playlist();
221     p_intf->p_sys->p_messages = create_intf_messages();
222     p_intf->p_sys->p_tooltips = gtk_tooltips_new();
223
224     /* Set the title of the main window */
225     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
226                           VOUT_TITLE " (Gtk+ interface)");
227
228     /* Accept file drops on the main window */
229     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
230                        GTK_DEST_DEFAULT_ALL, target_table,
231                        1, GDK_ACTION_COPY );
232
233     /* Accept file drops on the playlist window */
234     gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playlist,
235                                    "playlist_clist") ),
236                        GTK_DEST_DEFAULT_ALL, target_table,
237                        1, GDK_ACTION_COPY );
238
239     /* Get the slider object */
240     p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
241         GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" ) );
242
243     /* Configure the log window */
244     p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
245         GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
246     gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
247     gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
248
249     /* Get the interface labels */
250 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
251                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
252     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
253     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
254 #undef P_LABEL
255
256     /* Connect the date display to the slider */
257 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
258                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
259     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
260
261     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
262                          GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
263     p_intf->p_sys->f_adj_oldvalue = 0;
264 #undef P_SLIDER
265
266     /* We don't create these ones yet because we perhaps won't need them */
267     p_intf->p_sys->p_about = NULL;
268     p_intf->p_sys->p_modules = NULL;
269     p_intf->p_sys->p_fileopen = NULL;
270     p_intf->p_sys->p_disc = NULL;
271     p_intf->p_sys->p_sat = NULL;
272     p_intf->p_sys->p_network = NULL;
273     p_intf->p_sys->p_jump = NULL;
274
275     /* Hide tooltips if the option is set */
276     if( !config_GetInt( p_intf, "gtk-tooltips" ) )
277     {
278         gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
279     }
280
281     /* Store p_intf to keep an eye on it */
282     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
283                          "p_intf", p_intf );
284
285     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
286                          "p_intf", p_intf );
287
288     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
289                          "p_intf", p_intf );
290
291     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
292                          "p_intf", p_intf );
293
294     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
295                          "p_intf", p_intf );
296
297     /* Show the control window */
298     gtk_widget_show( p_intf->p_sys->p_window );
299
300     /* Sleep to avoid using all CPU - since some interfaces needs to access
301      * keyboard events, a 100ms delay is a good compromise */
302     i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GtkManage, p_intf );
303
304     /* Enter Gtk mode */
305     gtk_main();
306
307     /* Remove the timeout */
308     gtk_timeout_remove( i_dummy );
309
310     /* Destroy the Tooltips structure */
311     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
312
313     /* Launch stored callbacks */
314     for( i_dummy = 0;
315          i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
316          i_dummy++ )
317     {
318         p_intf->p_sys->pf_callback[i_dummy]();
319     }
320 }
321
322 /* following functions are local */
323
324 /*****************************************************************************
325  * GtkManage: manage main thread messages
326  *****************************************************************************
327  * In this function, called approx. 10 times a second, we check what the
328  * main program wanted to tell us.
329  *****************************************************************************/
330 static gint GtkManage( gpointer p_data )
331 {
332 #define p_intf ((intf_thread_t *)p_data)
333     int i_start, i_stop;
334
335     vlc_mutex_lock( &p_intf->change_lock );
336
337     /* If the "display popup" flag has changed */
338     if( p_intf->b_menu_change )
339     {
340         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
341         {
342             p_intf->p_sys->p_popup = create_intf_popup();
343             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
344                                  "p_popup", p_intf );
345         }
346         gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
347                         NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
348         p_intf->b_menu_change = 0;
349     }
350
351     /* Update the log window */
352     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
353     i_stop = *p_intf->p_sys->p_sub->pi_stop;
354     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
355
356     if( p_intf->p_sys->p_sub->i_start != i_stop )
357     {
358         static GdkColor white  = { 0, 0xffff, 0xffff, 0xffff };
359         static GdkColor gray   = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
360         static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
361         static GdkColor red    = { 0, 0xffff, 0x6666, 0x6666 };
362
363         static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
364                                              " debug: " };
365         static GdkColor *   pp_color[4] = { &white, &red, &yellow, &gray };
366
367         for( i_start = p_intf->p_sys->p_sub->i_start;
368              i_start != i_stop;
369              i_start = (i_start+1) % VLC_MSG_QSIZE )
370         {
371             /* Append all messages to log window */
372             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
373              NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
374
375             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
376                 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
377                 -1 );
378
379             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
380                 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
381                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
382
383             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
384                 NULL, "\n", -1 );
385         }
386
387         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
388         p_intf->p_sys->p_sub->i_start = i_start;
389         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
390
391         gtk_text_set_point( p_intf->p_sys->p_messages_text,
392                     gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
393     }
394
395     /* Update the playlist */
396     GtkPlayListManage( p_data );
397
398     /* Update the input */
399     if( p_intf->p_sys->p_input != NULL )
400     {
401         if( p_intf->p_sys->p_input->b_dead )
402         {
403             vlc_object_release( p_intf->p_sys->p_input );
404             p_intf->p_sys->p_input = NULL;
405         }
406     }
407
408     if( p_intf->p_sys->p_input == NULL )
409     {
410         p_intf->p_sys->p_input = vlc_object_find( p_intf->p_vlc,
411                                               VLC_OBJECT_INPUT, FIND_CHILD );
412     }
413
414     if( p_intf->p_sys->p_input )
415     {
416         input_thread_t *p_input = p_intf->p_sys->p_input;
417
418         vlc_mutex_lock( &p_input->stream.stream_lock );
419
420         if( !p_input->b_die )
421         {
422             /* New input or stream map change */
423             if( p_input->stream.b_changed )
424             {
425                 GtkModeManage( p_intf );
426                 GtkSetupMenus( p_intf );
427                 p_intf->p_sys->b_playing = 1;
428             }
429
430             /* Manage the slider */
431             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
432             {
433                 float newvalue = p_intf->p_sys->p_adj->value;
434
435 #define p_area p_input->stream.p_selected_area
436                 /* If the user hasn't touched the slider since the last time,
437                  * then the input can safely change it */
438                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
439                 {
440                     /* Update the value */
441                     p_intf->p_sys->p_adj->value =
442                     p_intf->p_sys->f_adj_oldvalue =
443                         ( 100. * p_area->i_tell ) / p_area->i_size;
444
445                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
446                                              "value_changed" );
447                 }
448                 /* Otherwise, send message to the input if the user has
449                  * finished dragging the slider */
450                 else if( p_intf->p_sys->b_slider_free )
451                 {
452                     off_t i_seek = ( newvalue * p_area->i_size ) / 100;
453
454                     /* release the lock to be able to seek */
455                     vlc_mutex_unlock( &p_input->stream.stream_lock );
456                     input_Seek( p_input, i_seek, INPUT_SEEK_SET );
457                     vlc_mutex_lock( &p_input->stream.stream_lock );
458
459                     /* Update the old value */
460                     p_intf->p_sys->f_adj_oldvalue = newvalue;
461                 }
462 #undef p_area
463             }
464
465             if( p_intf->p_sys->i_part !=
466                 p_input->stream.p_selected_area->i_part )
467             {
468                 p_intf->p_sys->b_chapter_update = 1;
469                 GtkSetupMenus( p_intf );
470             }
471         }
472
473         vlc_mutex_unlock( &p_input->stream.stream_lock );
474     }
475     else if( p_intf->p_sys->b_playing && !p_intf->p_vlc->b_die )
476     {
477         GtkModeManage( p_intf );
478         p_intf->p_sys->b_playing = 0;
479     }
480
481     if( p_intf->p_vlc->b_die )
482     {
483         vlc_mutex_unlock( &p_intf->change_lock );
484
485         /* Prepare to die, young Skywalker */
486         gtk_main_quit();
487
488         /* Just in case */
489         return( FALSE );
490     }
491
492     vlc_mutex_unlock( &p_intf->change_lock );
493
494     return( TRUE );
495
496 #undef p_intf
497 }