]> git.sesse.net Git - vlc/blob - modules/gui/gtk/gnome.c
* modules/*: sanitization of the modules description strings.
[vlc] / modules / gui / gtk / gnome.c
1 /*****************************************************************************
2  * gnome.c : Gnome plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id: gnome.c,v 1.15 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 <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 vlc_module_begin();
67 #ifdef WIN32
68     int i = 90;
69 #else
70     int i = getenv( "DISPLAY" ) == NULL ? 15 : 100;
71 #endif
72     add_category_hint( N_("GNOME"), NULL, VLC_FALSE );
73     add_bool( "gnome-tooltips", 1, E_(GtkHideTooltips),
74               TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT, VLC_FALSE );
75     add_bool( "gnome-toolbartext", 1, GtkHideToolbarText, TOOLBAR_TEXT,
76               TOOLBAR_LONGTEXT, VLC_FALSE );
77     add_integer( "gnome-prefs-maxh", 480, NULL,
78                  PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT, VLC_TRUE );
79
80     set_description( _("GNOME interface") );
81     set_capability( "interface", i );
82     set_callbacks( Open, Close );
83     set_program( "gnome-vlc" );
84 vlc_module_end();
85
86 /*****************************************************************************
87  * Open: initialize and create window
88  *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
90 {
91     intf_thread_t *p_intf = (intf_thread_t *)p_this;
92
93     /* Allocate instance and initialize some members */
94     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
95     if( p_intf->p_sys == NULL )
96     {
97         msg_Err( p_intf, "out of memory" );
98         return VLC_ENOMEM;
99     }
100
101     p_intf->p_sys->p_gtk_main = module_Need( p_this, "gui-helper", "gnome" );
102     if( p_intf->p_sys->p_gtk_main == NULL )
103     {
104         free( p_intf->p_sys );
105         return VLC_ENOMOD;
106     }
107
108     p_intf->pf_run = Run;
109
110     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
111
112     /* Initialize Gnome 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     module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
153
154     /* Destroy structure */
155     free( p_intf->p_sys );
156 }
157
158 /*****************************************************************************
159  * Run: Gnome thread
160  *****************************************************************************
161  * this part of the interface is in a separate thread so that we can call
162  * gtk_main() from within it without annoying the rest of the program.
163  * XXX: the approach may look kludgy, and probably is, but I could not find
164  * a better way to dynamically load a Gnome interface at runtime.
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     gdk_threads_enter();
179
180     /* Create some useful widgets that will certainly be used */
181     p_intf->p_sys->p_window = create_intf_window( );
182     p_intf->p_sys->p_popup = create_intf_popup( );
183     p_intf->p_sys->p_playwin = create_intf_playlist();
184     p_intf->p_sys->p_messages = create_intf_messages();
185     p_intf->p_sys->p_tooltips = gtk_tooltips_new();
186     p_intf->p_sys->p_sout = create_intf_sout();
187
188     /* Set the title of the main window */
189     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
190                           VOUT_TITLE " (Gnome interface)");
191
192     /* Accept file drops on the main window */
193     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
194                        GTK_DEST_DEFAULT_ALL, target_table,
195                        DROP_ACCEPT_END, GDK_ACTION_COPY );
196     /* Accept file drops on the playlist window */
197     gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
198                             p_intf->p_sys->p_playwin ), "playlist_clist") ),
199                        GTK_DEST_DEFAULT_ALL, target_table,
200                        DROP_ACCEPT_END, GDK_ACTION_COPY );
201
202     /* Get the slider object */
203     p_intf->p_sys->p_slider_frame = gtk_object_get_data(
204                       GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
205
206     /* Configure the log window */
207     p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
208         GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
209     gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
210     gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
211
212     /* Get the interface labels */
213     #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
214                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
215     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
216     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
217     #undef P_LABEL
218
219     /* Connect the date display to the slider */
220     #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
221                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
222     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
223
224     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
225                          GTK_SIGNAL_FUNC( E_(GtkDisplayDate) ), NULL );
226     p_intf->p_sys->f_adj_oldvalue = 0;
227     #undef P_SLIDER
228
229     /* We don't create these ones yet because we perhaps won't need them */
230     p_intf->p_sys->p_about = NULL;
231     p_intf->p_sys->p_modules = NULL;
232     p_intf->p_sys->p_open = NULL;
233     p_intf->p_sys->p_jump = NULL;
234
235     /* Hide tooltips if the option is set */
236     if( !config_GetInt( p_intf, "gnome-tooltips" ) )
237     {
238         gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
239     }
240
241     /* Hide toolbar text of the option is set */
242     if( !config_GetInt( p_intf, "gnome-toolbartext" ) )
243     {
244         gtk_toolbar_set_style(
245             GTK_TOOLBAR(lookup_widget( p_intf->p_sys->p_window, "toolbar" )),
246             GTK_TOOLBAR_ICONS );
247     }
248
249     /* Store p_intf to keep an eye on it */
250     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
251                          "p_intf", p_intf );
252
253     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
254                          "p_intf", p_intf );
255     gtk_object_set_data( GTK_OBJECT( gtk_object_get_data(
256                              GTK_OBJECT(p_intf->p_sys->p_popup),
257                              "popup_audio" ) ), "p_intf", p_intf );
258     gtk_object_set_data( GTK_OBJECT( gtk_object_get_data(
259                              GTK_OBJECT(p_intf->p_sys->p_popup),
260                              "popup_video" ) ), "p_intf", p_intf );
261
262     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
263                          "p_intf", p_intf );
264
265     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
266                          "p_intf", p_intf );
267
268     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
269                          "p_intf", p_intf );
270
271     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
272                                      "p_intf", p_intf );
273
274     psz_sout = config_GetPsz( p_intf, "sout" );
275     p_target = g_string_new( psz_sout ? psz_sout : "" );
276     if( psz_sout ) free( psz_sout );
277
278     gtk_entry_set_text( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ), "sout_entry_target" ), p_target->str );
279     g_string_free( p_target, TRUE );
280
281     /* FIXME it's to be sure that only file entry is selected */
282     gtk_toggle_button_set_active(  gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
283                                    "sout_access_udp" ), TRUE );
284
285     gtk_toggle_button_set_active(  gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
286                                    "sout_access_file" ), TRUE );
287
288     /* Show the control window */
289     gtk_widget_show( p_intf->p_sys->p_window );
290
291     while( !p_intf->b_die )
292     {
293         Manage( p_intf );
294
295         /* Sleep to avoid using all CPU - since some interfaces need to
296          * access keyboard events, a 100ms delay is a good compromise */
297         gdk_threads_leave();
298         msleep( INTF_IDLE_SLEEP );
299         gdk_threads_enter();
300     }
301
302     /* Destroy the Tooltips structure */
303     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
304     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_messages) );
305     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_playwin) );
306     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_popup) );
307     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
308
309     gdk_threads_leave();
310 }
311
312 /* following functions are local */
313
314 /*****************************************************************************
315  * Manage: manage main thread messages
316  *****************************************************************************
317  * In this function, called approx. 10 times a second, we check what the
318  * main program wanted to tell us.
319  *****************************************************************************/
320 static void Manage( intf_thread_t *p_intf )
321 {
322     int i_start, i_stop;
323
324     vlc_mutex_lock( &p_intf->change_lock );
325
326     /* If the "display popup" flag has changed */
327     if( p_intf->b_menu_change )
328     {
329         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
330         {
331             p_intf->p_sys->p_popup = create_intf_popup();
332             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
333                                  "p_popup", p_intf );
334         }
335
336         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
337                                    NULL, NULL, NULL, NULL );
338         p_intf->b_menu_change = 0;
339     }
340
341     /* Update the log window */
342     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
343     i_stop = *p_intf->p_sys->p_sub->pi_stop;
344     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
345
346     if( p_intf->p_sys->p_sub->i_start != i_stop )
347     {
348         static GdkColor white  = { 0, 0xffff, 0xffff, 0xffff };
349         static GdkColor gray   = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
350         static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
351         static GdkColor red    = { 0, 0xffff, 0x6666, 0x6666 };
352
353         static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
354                                              " debug: " };
355         static GdkColor *   pp_color[4] = { &white, &red, &yellow, &gray };
356
357         for( i_start = p_intf->p_sys->p_sub->i_start;
358              i_start != i_stop;
359              i_start = (i_start+1) % VLC_MSG_QSIZE )
360         {
361             /* Append all messages to log window */
362             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
363              NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
364
365             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
366                 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
367                 -1 );
368
369             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
370                 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
371                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
372
373             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
374                 NULL, "\n", -1 );
375         }
376
377         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
378         p_intf->p_sys->p_sub->i_start = i_start;
379         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
380
381         /* If the messages list becomes too big, just clean half of it. */
382         if( gtk_text_get_length( p_intf->p_sys->p_messages_text ) >
383             VLC_MSG_QSIZE * 1000 )
384         {
385             gtk_text_set_point( p_intf->p_sys->p_messages_text, 0 );
386             gtk_text_forward_delete( p_intf->p_sys->p_messages_text,
387                 gtk_text_get_length( p_intf->p_sys->p_messages_text ) / 2 );
388         }
389
390         gtk_text_set_point( p_intf->p_sys->p_messages_text,
391                     gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
392     }
393
394     /* Update the playlist */
395     GtkPlayListManage( p_intf );
396
397     /* Update the input */
398     if( p_intf->p_sys->p_input != NULL )
399     {
400         if( p_intf->p_sys->p_input->b_dead )
401         {
402             vlc_object_release( p_intf->p_sys->p_input );
403             p_intf->p_sys->p_input = NULL;
404         }
405     }
406
407     if( p_intf->p_sys->p_input == NULL )
408     {
409         p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
410                                                           FIND_ANYWHERE );
411     }
412
413     if( p_intf->p_sys->p_input )
414     {
415         input_thread_t *p_input = p_intf->p_sys->p_input;
416         aout_instance_t *p_aout  = NULL;
417         vout_thread_t   *p_vout  = NULL;
418         vlc_bool_t      b_need_menus = VLC_FALSE;
419
420         vlc_mutex_lock( &p_input->stream.stream_lock );
421
422         if( !p_input->b_die )
423         {
424             /* New input or stream map change */
425             if( p_input->stream.b_changed )
426             {
427                 E_(GtkModeManage)( p_intf );
428                 GtkSetupMenus( p_intf );
429                 p_intf->p_sys->b_playing = 1;
430             }
431
432             /* Manage the slider */
433             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
434             {
435                 float newvalue = p_intf->p_sys->p_adj->value;
436
437 #define p_area p_input->stream.p_selected_area
438                 /* If the user hasn't touched the slider since the last time,
439                  * then the input can safely change it */
440                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
441                 {
442                     /* Update the value */
443                     p_intf->p_sys->p_adj->value =
444                     p_intf->p_sys->f_adj_oldvalue =
445                         ( 100. * p_area->i_tell ) / p_area->i_size;
446
447                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
448                                              "value_changed" );
449                 }
450                 /* Otherwise, send message to the input if the user has
451                  * finished dragging the slider.
452                  * Beware, the hack below is needed by the dvdplay plugin! */
453                 else if( p_intf->p_sys->b_slider_free
454                 /* hack -> */ && (p_intf->p_sys->f_adj_oldvalue < 100.) )
455                 {
456                     if( newvalue >= 0. && newvalue < 100. )
457                     {
458                         off_t i_seek = ( newvalue * p_area->i_size ) / 100;
459
460                         vlc_mutex_unlock( &p_input->stream.stream_lock );
461                         input_Seek( p_input, i_seek, INPUT_SEEK_SET );
462                         vlc_mutex_lock( &p_input->stream.stream_lock );
463                     }
464
465                     /* Update the old value */
466                     p_intf->p_sys->f_adj_oldvalue = newvalue;
467                 }
468 #undef p_area
469             }
470
471             if( p_intf->p_sys->i_part !=
472                 p_input->stream.p_selected_area->i_part )
473             {
474                 p_intf->p_sys->b_chapter_update = 1;
475                 b_need_menus = VLC_TRUE;
476             }
477
478             /* Does the audio output require to update the menus ? */
479             p_aout = (aout_instance_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
480                                                          FIND_ANYWHERE );
481             if( p_aout != NULL )
482             {
483                 vlc_value_t val;
484                 if( var_Get( (vlc_object_t *)p_aout, "intf-change", &val ) >= 0
485                     && val.b_bool )
486                 {
487                     p_intf->p_sys->b_aout_update = 1;
488                     b_need_menus = 1;
489                 }
490
491                 vlc_object_release( (vlc_object_t *)p_aout );
492             }
493
494
495             /* Does the video output require to update the menus ? */
496             p_vout = (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
497                                                        FIND_ANYWHERE );
498             if( p_vout != NULL ) 
499             {
500                 vlc_value_t val;
501                 if( var_Get( (vlc_object_t *)p_vout, "intf-change", &val ) >= 0
502                     && val.b_bool )
503                 {
504                     p_intf->p_sys->b_vout_update = 1;
505                     b_need_menus = 1;
506                 }
507
508                 vlc_object_release( (vlc_object_t *)p_vout );
509             }
510
511             if( b_need_menus )
512             {
513                 GtkSetupMenus( p_intf );
514             }
515         }
516
517         vlc_mutex_unlock( &p_input->stream.stream_lock );
518     }
519     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
520     {
521         E_(GtkModeManage)( p_intf );
522         p_intf->p_sys->b_playing = 0;
523     }
524
525     vlc_mutex_unlock( &p_intf->change_lock );
526
527     return;
528 }
529