* modules/*: sanitization of the modules description strings.
[vlc.git] / modules / gui / gtk / gtk.c
1 /*****************************************************************************
2  * gtk.c : Gtk+ plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: gtk.c,v 1.20 2003/03/30 18:14:37 gbazin 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
41 #include "menu.h"
42 #include "display.h"
43 #include "common.h"
44
45 /*****************************************************************************
46  * Local prototypes.
47  *****************************************************************************/
48 static int  Open         ( vlc_object_t * );
49 static void Close        ( vlc_object_t * );
50
51 static void Run          ( intf_thread_t * );
52 static int  Manage       ( intf_thread_t * );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 #define TOOLTIPS_TEXT N_("show tooltips")
58 #define TOOLTIPS_LONGTEXT N_("Show tooltips for configuration options.")
59
60 #define PREFS_MAXH_TEXT N_("maximum height for the configuration windows")
61 #define PREFS_MAXH_LONGTEXT N_( \
62     "You can set the maximum height that the configuration windows in the " \
63     "preferences menu will occupy.")
64
65 vlc_module_begin();
66 #ifdef WIN32
67     int i = 90;
68 #else
69     int i = getenv( "DISPLAY" ) == NULL ? 10 : 90;
70 #endif
71     add_category_hint( N_("Gtk+"), NULL, VLC_FALSE );
72     add_bool( "gtk-tooltips", 1, E_(GtkHideTooltips),
73               TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT, VLC_FALSE );
74     add_integer( "gtk-prefs-maxh", 480, NULL,
75                  PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT, VLC_TRUE );
76
77     set_description( _("Gtk+ interface") );
78     set_capability( "interface", i );
79     set_callbacks( Open, Close );
80     add_shortcut( "gtk" );
81     set_program( "gvlc" );
82 vlc_module_end();
83
84 /*****************************************************************************
85  * Open: initialize and create window
86  *****************************************************************************/
87 static int Open( vlc_object_t *p_this )
88 {
89     intf_thread_t *p_intf = (intf_thread_t *)p_this;
90
91     /* Allocate instance and initialize some members */
92     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
93     if( p_intf->p_sys == NULL )
94     {
95         msg_Err( p_intf, "out of memory" );
96         return VLC_ENOMEM;
97     }
98
99 #ifdef NEED_GTK_MAIN
100     p_intf->p_sys->p_gtk_main = module_Need( p_this, "gui-helper", "gtk" );
101     if( p_intf->p_sys->p_gtk_main == NULL )
102     {
103         free( p_intf->p_sys );
104         return VLC_ENOMOD;
105     }
106 #endif
107
108     p_intf->pf_run = Run;
109
110     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
111
112     /* Initialize Gtk+ thread */
113     p_intf->p_sys->b_playing = VLC_FALSE;
114     p_intf->p_sys->b_deinterlace_update = VLC_FALSE;
115
116     p_intf->p_sys->b_aout_update = VLC_FALSE;
117     p_intf->p_sys->b_vout_update = VLC_FALSE;
118
119     p_intf->p_sys->b_popup_changed = VLC_FALSE;
120     p_intf->p_sys->b_window_changed = VLC_FALSE;
121     p_intf->p_sys->b_playlist_changed = VLC_FALSE;
122     p_intf->p_sys->b_program_update = VLC_FALSE;
123     p_intf->p_sys->b_title_update = VLC_FALSE;
124     p_intf->p_sys->b_chapter_update = VLC_FALSE;
125     p_intf->p_sys->b_spu_update = VLC_FALSE;
126     p_intf->p_sys->b_audio_update = VLC_FALSE;
127
128     p_intf->p_sys->p_input = NULL;
129     p_intf->p_sys->i_playing = -1;
130     p_intf->p_sys->b_slider_free = VLC_TRUE;
131
132     p_intf->p_sys->i_part = 0;
133     p_intf->p_sys->b_mute = VLC_FALSE;
134
135     return VLC_SUCCESS;
136 }
137
138 /*****************************************************************************
139  * Close: destroy interface window
140  *****************************************************************************/
141 static void Close( vlc_object_t *p_this )
142 {
143     intf_thread_t *p_intf = (intf_thread_t *)p_this;
144
145     if( p_intf->p_sys->p_input )
146     {
147         vlc_object_release( p_intf->p_sys->p_input );
148     }
149
150     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
151
152 #ifdef NEED_GTK_MAIN
153     module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
154 #endif
155
156     /* Destroy structure */
157     free( p_intf->p_sys );
158 }
159
160 /*****************************************************************************
161  * Run: Gtk+ thread
162  *****************************************************************************
163  * this part of the interface is in a separate thread so that we can call
164  * gtk_main() from within it without annoying the rest of the program.
165  *****************************************************************************/
166 static void Run( intf_thread_t *p_intf )
167 {
168     /* The data types we are allowed to receive */
169     static GtkTargetEntry target_table[] =
170     {
171         { "STRING", 0, DROP_ACCEPT_STRING },
172         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
173         { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
174     };
175     char *psz_sout;
176     GString *       p_target;
177
178 #ifdef NEED_GTK_MAIN
179     gdk_threads_enter();
180 #else
181     /* gtk_init needs to know the command line. We don't care, so we
182      * give it an empty one */
183     char  *p_args[] = { "", NULL };
184     char **pp_args  = p_args;
185     int    i_args   = 1;
186     int    i_dummy;
187
188     gtk_init( &i_args, &pp_args );
189 #endif
190
191     /* Create some useful widgets that will certainly be used */
192     p_intf->p_sys->p_window = create_intf_window();
193     p_intf->p_sys->p_popup = create_intf_popup();
194     p_intf->p_sys->p_playwin = create_intf_playlist();
195     p_intf->p_sys->p_messages = create_intf_messages();
196     p_intf->p_sys->p_tooltips = gtk_tooltips_new();
197     p_intf->p_sys->p_sout = create_intf_sout();
198
199     /* Set the title of the main window */
200     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
201                           VOUT_TITLE " (Gtk+ interface)");
202
203     /* Accept file drops on the main window */
204     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
205                        GTK_DEST_DEFAULT_ALL, target_table,
206                        DROP_ACCEPT_END, GDK_ACTION_COPY );
207
208     /* Accept file drops on the playlist window */
209     gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playwin,
210                                    "playlist_clist") ),
211                        GTK_DEST_DEFAULT_ALL, target_table,
212                        DROP_ACCEPT_END, GDK_ACTION_COPY );
213
214     /* Get the slider object */
215     p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
216         GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" ) );
217
218     /* Configure the log window */
219     p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
220         GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
221     gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
222     gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
223
224     /* Get the interface labels */
225 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
226                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
227     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
228     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
229 #undef P_LABEL
230
231     /* Connect the date display to the slider */
232 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
233                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
234     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
235
236     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
237                          GTK_SIGNAL_FUNC( E_(GtkDisplayDate) ), NULL );
238     p_intf->p_sys->f_adj_oldvalue = 0;
239 #undef P_SLIDER
240
241
242
243     /* We don't create these ones yet because we perhaps won't need them */
244     p_intf->p_sys->p_about = NULL;
245     p_intf->p_sys->p_modules = NULL;
246     p_intf->p_sys->p_open = NULL;
247     p_intf->p_sys->p_jump = NULL;
248
249     /* Hide tooltips if the option is set */
250     if( !config_GetInt( p_intf, "gtk-tooltips" ) )
251     {
252         gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
253     }
254
255     /* Store p_intf to keep an eye on it */
256     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
257                          "p_intf", p_intf );
258
259     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
260                          "p_intf", p_intf );
261     gtk_object_set_data( GTK_OBJECT( gtk_object_get_data(
262                              GTK_OBJECT(p_intf->p_sys->p_popup),
263                              "popup_audio" ) ), "p_intf", p_intf );
264     gtk_object_set_data( GTK_OBJECT( gtk_object_get_data(
265                              GTK_OBJECT(p_intf->p_sys->p_popup),
266                              "popup_video" ) ), "p_intf", p_intf );
267
268     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
269                          "p_intf", p_intf );
270
271     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
272                          "p_intf", p_intf );
273
274     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
275                          "p_intf", p_intf );
276     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
277                          "p_intf", p_intf );
278
279     psz_sout = config_GetPsz( p_intf, "sout" );
280     p_target = g_string_new( psz_sout ? psz_sout : "" );
281     if( psz_sout ) free( psz_sout );
282
283     gtk_entry_set_text( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ), "sout_entry_target" ), p_target->str );
284     g_string_free( p_target, TRUE );
285
286     /* FIXME it's to be sure that only file entry is selected */
287     gtk_toggle_button_set_active(  gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
288                                    "sout_access_udp" ), TRUE );
289
290     gtk_toggle_button_set_active(  gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
291                                    "sout_access_file" ), TRUE );
292
293     /* Show the control window */
294     gtk_widget_show( p_intf->p_sys->p_window );
295
296 #ifdef NEED_GTK_MAIN
297     while( !p_intf->b_die )
298     {
299         Manage( p_intf );
300
301         /* Sleep to avoid using all CPU - since some interfaces need to
302          * access keyboard events, a 100ms delay is a good compromise */
303         gdk_threads_leave();
304         msleep( INTF_IDLE_SLEEP );
305         gdk_threads_enter();
306     }
307 #else
308     /* Sleep to avoid using all CPU - since some interfaces needs to access
309      * keyboard events, a 100ms delay is a good compromise */
310     i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, (GtkFunction)Manage,
311                                p_intf );
312     /* Enter Gtk mode */
313     gtk_main();
314     /* Remove the timeout */
315     gtk_timeout_remove( i_dummy );
316 #endif
317
318     /* Destroy the Tooltips structure */
319     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
320     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_messages) );
321     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_playwin) );
322     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_popup) );
323     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
324
325 #ifdef NEED_GTK_MAIN
326     gdk_threads_leave();
327 #endif
328 }
329
330 /* following functions are local */
331
332 /*****************************************************************************
333  * Manage: manage main thread messages
334  *****************************************************************************
335  * In this function, called approx. 10 times a second, we check what the
336  * main program wanted to tell us.
337  *****************************************************************************/
338 static int Manage( intf_thread_t *p_intf )
339 {
340     int i_start, i_stop;
341
342     vlc_mutex_lock( &p_intf->change_lock );
343
344     /* If the "display popup" flag has changed */
345     if( p_intf->b_menu_change )
346     {
347         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
348         {
349             p_intf->p_sys->p_popup = create_intf_popup();
350             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
351                                  "p_intf", p_intf );
352         }
353         gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
354                         NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
355         p_intf->b_menu_change = 0;
356     }
357
358     /* Update the log window */
359     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
360     i_stop = *p_intf->p_sys->p_sub->pi_stop;
361     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
362
363     if( p_intf->p_sys->p_sub->i_start != i_stop )
364     {
365         static GdkColor white  = { 0, 0xffff, 0xffff, 0xffff };
366         static GdkColor gray   = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
367         static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
368         static GdkColor red    = { 0, 0xffff, 0x6666, 0x6666 };
369
370         static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
371                                              " debug: " };
372         static GdkColor *   pp_color[4] = { &white, &red, &yellow, &gray };
373
374         for( i_start = p_intf->p_sys->p_sub->i_start;
375              i_start != i_stop;
376              i_start = (i_start+1) % VLC_MSG_QSIZE )
377         {
378             /* Append all messages to log window */
379             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
380              NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
381
382             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
383                 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
384                 -1 );
385
386             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
387                 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
388                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
389
390             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
391                 NULL, "\n", -1 );
392         }
393
394         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
395         p_intf->p_sys->p_sub->i_start = i_start;
396         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
397
398         /* If the messages list becomes too big, just clean half of it. */
399         if( gtk_text_get_length( p_intf->p_sys->p_messages_text ) >
400             VLC_MSG_QSIZE * 1000 )
401         {
402             gtk_text_set_point( p_intf->p_sys->p_messages_text, 0 );
403             gtk_text_forward_delete( p_intf->p_sys->p_messages_text,
404                 gtk_text_get_length( p_intf->p_sys->p_messages_text ) / 2 );
405         }
406
407         gtk_text_set_point( p_intf->p_sys->p_messages_text,
408                     gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
409     }
410
411     /* Update the playlist */
412     GtkPlayListManage( p_intf );
413
414     /* Update the input */
415     if( p_intf->p_sys->p_input == NULL )
416     {
417         p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
418                                                           FIND_ANYWHERE );
419     }
420     else if( p_intf->p_sys->p_input->b_dead )
421     {
422         vlc_object_release( p_intf->p_sys->p_input );
423         p_intf->p_sys->p_input = NULL;
424     }
425
426     if( p_intf->p_sys->p_input )
427     {
428         input_thread_t  *p_input = p_intf->p_sys->p_input;
429         aout_instance_t *p_aout  = NULL;
430         vout_thread_t   *p_vout  = NULL;
431         vlc_bool_t      b_need_menus = VLC_FALSE;
432
433         vlc_mutex_lock( &p_input->stream.stream_lock );
434
435         if( !p_input->b_die )
436         {
437             /* New input or stream map change */
438             if( p_input->stream.b_changed )
439             {
440                 E_(GtkModeManage)( p_intf );
441                 GtkSetupMenus( p_intf );
442                 p_intf->p_sys->b_playing = 1;
443             }
444
445             /* Manage the slider */
446             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
447             {
448                 float newvalue = p_intf->p_sys->p_adj->value;
449
450 #define p_area p_input->stream.p_selected_area
451                 /* If the user hasn't touched the slider since the last time,
452                  * then the input can safely change it */
453                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
454                 {
455                     /* Update the value */
456                     p_intf->p_sys->p_adj->value =
457                     p_intf->p_sys->f_adj_oldvalue =
458                         ( 100. * p_area->i_tell ) / p_area->i_size;
459
460                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
461                                              "value_changed" );
462                 }
463                 /* Otherwise, send message to the input if the user has
464                  * finished dragging the slider.
465                  * Beware, the hack below is needed by the dvdplay plugin! */
466                 else if( p_intf->p_sys->b_slider_free
467                 /* hack -> */ && (p_intf->p_sys->f_adj_oldvalue < 100.) )
468                 {
469                     if( newvalue >= 0. && newvalue < 100. )
470                     {
471                         off_t i_seek = ( newvalue * p_area->i_size ) / 100;
472
473                         /* release the lock to be able to seek */
474                         vlc_mutex_unlock( &p_input->stream.stream_lock );
475                         input_Seek( p_input, i_seek, INPUT_SEEK_SET );
476                         vlc_mutex_lock( &p_input->stream.stream_lock );
477                     }
478
479                     /* Update the old value */
480                     p_intf->p_sys->f_adj_oldvalue = newvalue;
481                 }
482 #undef p_area
483             }
484
485             if( p_intf->p_sys->i_part !=
486                 p_input->stream.p_selected_area->i_part )
487             {
488                 p_intf->p_sys->b_chapter_update = 1;
489                 b_need_menus = VLC_TRUE;
490             }
491
492             /* Does the audio output require to update the menus ? */
493             p_aout = (aout_instance_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
494                                                          FIND_ANYWHERE );
495             if( p_aout != NULL )
496             {
497                 vlc_value_t val;
498                 if( var_Get( (vlc_object_t *)p_aout, "intf-change", &val ) >= 0
499                     && val.b_bool )
500                 {
501                     p_intf->p_sys->b_aout_update = 1;
502                     b_need_menus = 1;
503                 }
504
505                 vlc_object_release( (vlc_object_t *)p_aout );
506             }
507
508             /* Does the video output require to update the menus ? */
509             p_vout = (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
510                                                        FIND_ANYWHERE );
511             if( p_vout != NULL ) 
512             {
513                 vlc_value_t val;
514                 if( var_Get( (vlc_object_t *)p_vout, "intf-change", &val ) >= 0
515                     && val.b_bool )
516                 {
517                     p_intf->p_sys->b_vout_update = 1;
518                     b_need_menus = 1;
519                 }
520
521                 vlc_object_release( (vlc_object_t *)p_vout );
522             }
523             if( b_need_menus )
524             {
525                 GtkSetupMenus( p_intf );
526             }
527         }
528
529         vlc_mutex_unlock( &p_input->stream.stream_lock );
530     }
531     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
532     {
533         E_(GtkModeManage)( p_intf );
534         p_intf->p_sys->b_playing = 0;
535     }
536
537 #ifndef NEED_GTK_MAIN
538     if( p_intf->b_die )
539     {
540         vlc_mutex_unlock( &p_intf->change_lock );
541
542         /* Prepare to die, young Skywalker */
543         gtk_main_quit();
544
545         return FALSE;
546     }
547 #endif
548
549     vlc_mutex_unlock( &p_intf->change_lock );
550
551     return TRUE;
552 }