]> git.sesse.net Git - vlc/blob - modules/gui/gtk/gnome.c
Improvements to preferences
[vlc] / modules / gui / gtk / gnome.c
1 /*****************************************************************************
2  * gnome.c : Gnome plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000 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 <gnome.h>
36
37 #include "gnome_callbacks.h"
38 #include "gnome_interface.h"
39 #include "gnome_support.h"
40 #include "display.h"
41 #include "common.h"
42
43 /*****************************************************************************
44  * Local prototypes.
45  *****************************************************************************/
46 static int  Open         ( vlc_object_t * );
47 static void Close        ( vlc_object_t * );
48
49 static void Run          ( intf_thread_t * );
50 static void Manage       ( intf_thread_t * );
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 #define TOOLTIPS_TEXT N_("Show tooltips")
56 #define TOOLTIPS_LONGTEXT N_("Show tooltips for configuration options.")
57
58 #define TOOLBAR_TEXT N_("Show text on toolbar buttons")
59 #define TOOLBAR_LONGTEXT N_("Show the text below icons on the toolbar.")
60
61 #define PREFS_MAXH_TEXT N_("Maximum height for the configuration windows")
62 #define PREFS_MAXH_LONGTEXT N_( \
63     "You can set the maximum height that the configuration windows in the " \
64     "preferences menu will occupy.")
65
66 #define PATH_TEXT N_("Interface default search path")
67 #define PATH_LONGTEXT N_( \
68     "This option allows you to set the default path that the interface will " \
69     "open when looking for a file.")
70
71 vlc_module_begin();
72 #ifdef WIN32
73     int i = 90;
74 #else
75     int i = getenv( "DISPLAY" ) == NULL ? 15 : 100;
76 #endif
77     set_description( _("GNOME interface") );
78     set_category( CAT_INTERFACE );
79     set_subcategory( SUBCAT_INTERFACE_GENERAL );
80
81     add_bool( "gnome-tooltips", 1, E_(GtkHideTooltips),
82               TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT, VLC_FALSE );
83     add_bool( "gnome-toolbartext", 1, GtkHideToolbarText, TOOLBAR_TEXT,
84               TOOLBAR_LONGTEXT, VLC_FALSE );
85     add_integer( "gnome-prefs-maxh", 480, NULL,
86                  PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT, VLC_TRUE );
87     add_directory( "gnome-search-path", NULL, NULL, PATH_TEXT,
88                    PATH_LONGTEXT, VLC_TRUE );
89
90     set_capability( "interface", i );
91     set_callbacks( Open, Close );
92     set_program( "gnome-vlc" );
93 vlc_module_end();
94
95 /*****************************************************************************
96  * Open: initialize and create window
97  *****************************************************************************/
98 static int Open( vlc_object_t *p_this )
99 {
100     intf_thread_t *p_intf = (intf_thread_t *)p_this;
101
102     /* Allocate instance and initialize some members */
103     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
104     if( p_intf->p_sys == NULL )
105     {
106         msg_Err( p_intf, "out of memory" );
107         return VLC_ENOMEM;
108     }
109
110     p_intf->p_sys->p_gtk_main =
111         module_Need( p_this, "gui-helper", "gnome", VLC_TRUE );
112     if( p_intf->p_sys->p_gtk_main == NULL )
113     {
114         free( p_intf->p_sys );
115         return VLC_ENOMOD;
116     }
117
118     p_intf->pf_run = Run;
119
120     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
121
122     /* Initialize Gnome 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     module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
163
164     /* Destroy structure */
165     free( p_intf->p_sys );
166 }
167
168 /*****************************************************************************
169  * Run: Gnome thread
170  *****************************************************************************
171  * this part of the interface is in a separate thread so that we can call
172  * gtk_main() from within it without annoying the rest of the program.
173  * XXX: the approach may look kludgy, and probably is, but I could not find
174  * a better way to dynamically load a Gnome interface at runtime.
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     gdk_threads_enter();
189
190     /* Create some useful widgets that will certainly be used */
191     p_intf->p_sys->p_window = create_intf_window( );
192     p_intf->p_sys->p_popup = create_intf_popup( );
193     p_intf->p_sys->p_playwin = create_intf_playlist();
194     p_intf->p_sys->p_messages = create_intf_messages();
195     p_intf->p_sys->p_tooltips = gtk_tooltips_new();
196     p_intf->p_sys->p_sout = create_intf_sout();
197
198     /* Set the title of the main window */
199     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
200                           VOUT_TITLE " (Gnome interface)");
201
202     /* Accept file drops on the main window */
203     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
204                        GTK_DEST_DEFAULT_ALL, target_table,
205                        DROP_ACCEPT_END, GDK_ACTION_COPY );
206     /* Accept file drops on the playlist window */
207     gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
208                             p_intf->p_sys->p_playwin ), "playlist_clist") ),
209                        GTK_DEST_DEFAULT_ALL, target_table,
210                        DROP_ACCEPT_END, GDK_ACTION_COPY );
211
212     /* Get the slider object */
213     p_intf->p_sys->p_slider_frame = gtk_object_get_data(
214                       GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
215
216     /* Configure the log window */
217     p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
218         GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
219     gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
220     gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
221
222     /* Get the interface labels */
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( E_(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_open = NULL;
243     p_intf->p_sys->p_jump = NULL;
244
245     /* Hide tooltips if the option is set */
246     if( !config_GetInt( p_intf, "gnome-tooltips" ) )
247     {
248         gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
249     }
250
251     /* Hide toolbar text of the option is set */
252     if( !config_GetInt( p_intf, "gnome-toolbartext" ) )
253     {
254         gtk_toolbar_set_style(
255             GTK_TOOLBAR(lookup_widget( p_intf->p_sys->p_window, "toolbar" )),
256             GTK_TOOLBAR_ICONS );
257     }
258
259     /* Store p_intf to keep an eye on it */
260     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
261                          "p_intf", p_intf );
262
263     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
264                          "p_intf", p_intf );
265     gtk_object_set_data( GTK_OBJECT( gtk_object_get_data(
266                              GTK_OBJECT(p_intf->p_sys->p_popup),
267                              "popup_audio" ) ), "p_intf", p_intf );
268     gtk_object_set_data( GTK_OBJECT( gtk_object_get_data(
269                              GTK_OBJECT(p_intf->p_sys->p_popup),
270                              "popup_video" ) ), "p_intf", p_intf );
271
272     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
273                          "p_intf", p_intf );
274
275     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
276                          "p_intf", p_intf );
277
278     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
279                          "p_intf", p_intf );
280
281     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
282                                      "p_intf", p_intf );
283
284     psz_sout = config_GetPsz( p_intf, "sout" );
285     p_target = g_string_new( psz_sout ? psz_sout : "" );
286     if( psz_sout ) free( psz_sout );
287
288     gtk_entry_set_text( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ), "sout_entry_target" ), p_target->str );
289     g_string_free( p_target, TRUE );
290
291     /* FIXME it's to be sure that only file entry is selected */
292     gtk_toggle_button_set_active(  gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
293                                    "sout_access_udp" ), TRUE );
294
295     gtk_toggle_button_set_active(  gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
296                                    "sout_access_file" ), TRUE );
297
298     /* Show the control window */
299     gtk_widget_show( p_intf->p_sys->p_window );
300
301     while( !p_intf->b_die )
302     {
303         Manage( p_intf );
304
305         /* Sleep to avoid using all CPU - since some interfaces need to
306          * access keyboard events, a 100ms delay is a good compromise */
307         gdk_threads_leave();
308         msleep( INTF_IDLE_SLEEP );
309         gdk_threads_enter();
310     }
311
312     /* Destroy the Tooltips structure */
313     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
314     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_messages) );
315     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_playwin) );
316     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_popup) );
317     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
318
319     gdk_threads_leave();
320 }
321
322 /* following functions are local */
323
324 /*****************************************************************************
325  * Manage: 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 void Manage( intf_thread_t *p_intf )
331 {
332     int i_start, i_stop;
333
334     vlc_mutex_lock( &p_intf->change_lock );
335
336     /* If the "display popup" flag has changed */
337     if( p_intf->b_menu_change )
338     {
339         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
340         {
341             p_intf->p_sys->p_popup = create_intf_popup();
342             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
343                                  "p_popup", p_intf );
344         }
345
346         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
347                                    NULL, NULL, NULL, NULL );
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         /* If the messages list becomes too big, just clean half of it. */
392         if( gtk_text_get_length( p_intf->p_sys->p_messages_text ) >
393             VLC_MSG_QSIZE * 1000 )
394         {
395             gtk_text_set_point( p_intf->p_sys->p_messages_text, 0 );
396             gtk_text_forward_delete( p_intf->p_sys->p_messages_text,
397                 gtk_text_get_length( p_intf->p_sys->p_messages_text ) / 2 );
398         }
399
400         gtk_text_set_point( p_intf->p_sys->p_messages_text,
401                     gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
402     }
403
404     /* Update the playlist */
405     GtkPlayListManage( p_intf );
406
407     /* Update the input */
408     if( p_intf->p_sys->p_input != NULL )
409     {
410         if( p_intf->p_sys->p_input->b_dead )
411         {
412             vlc_object_release( p_intf->p_sys->p_input );
413             p_intf->p_sys->p_input = NULL;
414         }
415     }
416
417     if( p_intf->p_sys->p_input == NULL )
418     {
419         p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
420                                                           FIND_ANYWHERE );
421     }
422
423     if( p_intf->p_sys->p_input )
424     {
425         input_thread_t *p_input = p_intf->p_sys->p_input;
426         aout_instance_t *p_aout  = NULL;
427         vout_thread_t   *p_vout  = NULL;
428         vlc_bool_t      b_need_menus = VLC_FALSE;
429
430         vlc_mutex_lock( &p_input->stream.stream_lock );
431
432         if( !p_input->b_die )
433         {
434             /* New input or stream map change */
435             if( p_input->stream.b_changed )
436             {
437                 E_(GtkModeManage)( p_intf );
438                 GtkSetupMenus( p_intf );
439                 p_intf->p_sys->b_playing = 1;
440             }
441
442             /* Manage the slider */
443             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
444             {
445                 float newvalue = p_intf->p_sys->p_adj->value;
446
447 #define p_area p_input->stream.p_selected_area
448                 /* If the user hasn't touched the slider since the last time,
449                  * then the input can safely change it */
450                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
451                 {
452                     /* Update the value */
453                     p_intf->p_sys->p_adj->value =
454                     p_intf->p_sys->f_adj_oldvalue =
455                         ( 100. * p_area->i_tell ) / p_area->i_size;
456
457                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
458                                              "value_changed" );
459                 }
460                 /* Otherwise, send message to the input if the user has
461                  * finished dragging the slider.
462                  * Beware, the hack below is needed by the dvdplay plugin! */
463                 else if( p_intf->p_sys->b_slider_free
464                 /* hack -> */ && (p_intf->p_sys->f_adj_oldvalue < 100.) )
465                 {
466                     if( newvalue >= 0. && newvalue < 100. )
467                     {
468                         double f_fpos = (double)newvalue / 100.0;
469
470                         /* release the lock to be able to seek */
471                         vlc_mutex_unlock( &p_input->stream.stream_lock );
472                         var_SetFloat( p_input, "position", f_fpos );
473                         vlc_mutex_lock( &p_input->stream.stream_lock );
474
475                     }
476
477                     /* Update the old value */
478                     p_intf->p_sys->f_adj_oldvalue = newvalue;
479                 }
480 #undef p_area
481             }
482
483             if( p_intf->p_sys->i_part !=
484                 p_input->stream.p_selected_area->i_part )
485             {
486                 p_intf->p_sys->b_chapter_update = 1;
487                 b_need_menus = VLC_TRUE;
488             }
489
490             /* Does the audio output require to update the menus ? */
491             p_aout = (aout_instance_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
492                                                          FIND_ANYWHERE );
493             if( p_aout != NULL )
494             {
495                 vlc_value_t val;
496                 if( var_Get( (vlc_object_t *)p_aout, "intf-change", &val ) >= 0
497                     && val.b_bool )
498                 {
499                     p_intf->p_sys->b_aout_update = 1;
500                     b_need_menus = 1;
501                 }
502
503                 vlc_object_release( (vlc_object_t *)p_aout );
504             }
505
506
507             /* Does the video output require to update the menus ? */
508             p_vout = (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
509                                                        FIND_ANYWHERE );
510             if( p_vout != NULL ) 
511             {
512                 vlc_value_t val;
513                 if( var_Get( (vlc_object_t *)p_vout, "intf-change", &val ) >= 0
514                     && val.b_bool )
515                 {
516                     p_intf->p_sys->b_vout_update = 1;
517                     b_need_menus = 1;
518                 }
519
520                 vlc_object_release( (vlc_object_t *)p_vout );
521             }
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     vlc_mutex_unlock( &p_intf->change_lock );
538
539     return;
540 }
541