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