* modules/gui/gtk/gnome.c, modules/gui/gtk/gtk.c: input_Seek() was incorrectly trigge...
[vlc.git] / 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.8 2003/01/26 22:57:21 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 );
73     add_bool( "gnome-tooltips", 1, E_(GtkHideTooltips),
74               TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT );
75     add_bool( "gnome-toolbartext", 1, GtkHideToolbarText, TOOLBAR_TEXT,
76               TOOLBAR_LONGTEXT );
77     add_integer( "gnome-prefs-maxh", 480, NULL,
78                  PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT );
79
80     set_description( _("GNOME interface module") );
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, "gtk_main", "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 = 0;
114     p_intf->p_sys->b_deinterlace_update = 0;
115
116     p_intf->p_sys->b_aout_update = 0;
117     p_intf->p_sys->b_vout_update = 0;
118
119     p_intf->p_sys->b_popup_changed = 0;
120     p_intf->p_sys->b_window_changed = 0;
121     p_intf->p_sys->b_playlist_changed = 0;
122
123     p_intf->p_sys->p_input = NULL;
124     p_intf->p_sys->i_playing = -1;
125     p_intf->p_sys->b_slider_free = 1;
126
127     p_intf->p_sys->i_part = 0;
128     p_intf->p_sys->b_mute = 0;
129
130     return VLC_SUCCESS;
131 }
132
133 /*****************************************************************************
134  * Close: destroy interface window
135  *****************************************************************************/
136 static void Close( vlc_object_t *p_this )
137 {
138     intf_thread_t *p_intf = (intf_thread_t *)p_this;
139
140     if( p_intf->p_sys->p_input )
141     {
142         vlc_object_release( p_intf->p_sys->p_input );
143     }
144
145     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
146
147     module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
148
149     /* Destroy structure */
150     free( p_intf->p_sys );
151 }
152
153 /*****************************************************************************
154  * Run: Gnome thread
155  *****************************************************************************
156  * this part of the interface is in a separate thread so that we can call
157  * gtk_main() from within it without annoying the rest of the program.
158  * XXX: the approach may look kludgy, and probably is, but I could not find
159  * a better way to dynamically load a Gnome interface at runtime.
160  *****************************************************************************/
161 static void Run( intf_thread_t *p_intf )
162 {
163     /* The data types we are allowed to receive */
164     static GtkTargetEntry target_table[] =
165     {
166         { "STRING", 0, DROP_ACCEPT_STRING },
167         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
168         { "text/plain",    0, DROP_ACCEPT_TEXT_PLAIN }
169     };
170     char *psz_sout;
171     GString *       p_target;
172
173     gdk_threads_enter();
174
175     /* Create some useful widgets that will certainly be used */
176     p_intf->p_sys->p_window = create_intf_window( );
177     p_intf->p_sys->p_popup = create_intf_popup( );
178     p_intf->p_sys->p_playwin = create_intf_playlist();
179     p_intf->p_sys->p_messages = create_intf_messages();
180     p_intf->p_sys->p_tooltips = gtk_tooltips_new();
181     p_intf->p_sys->p_sout = create_intf_sout();
182
183     /* Set the title of the main window */
184     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
185                           VOUT_TITLE " (Gnome interface)");
186
187     /* Accept file drops on the main window */
188     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
189                        GTK_DEST_DEFAULT_ALL, target_table,
190                        DROP_ACCEPT_END, GDK_ACTION_COPY );
191     /* Accept file drops on the playlist window */
192     gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
193                             p_intf->p_sys->p_playwin ), "playlist_clist") ),
194                        GTK_DEST_DEFAULT_ALL, target_table,
195                        DROP_ACCEPT_END, GDK_ACTION_COPY );
196
197     /* Get the slider object */
198     p_intf->p_sys->p_slider_frame = gtk_object_get_data(
199                       GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
200
201     /* Configure the log window */
202     p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
203         GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
204     gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
205     gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
206
207     /* Get the interface labels */
208     #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
209                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
210     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
211     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
212     #undef P_LABEL
213
214     /* Connect the date display to the slider */
215     #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
216                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
217     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
218
219     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
220                          GTK_SIGNAL_FUNC( E_(GtkDisplayDate) ), NULL );
221     p_intf->p_sys->f_adj_oldvalue = 0;
222     #undef P_SLIDER
223
224     /* We don't create these ones yet because we perhaps won't need them */
225     p_intf->p_sys->p_about = NULL;
226     p_intf->p_sys->p_modules = NULL;
227     p_intf->p_sys->p_open = NULL;
228     p_intf->p_sys->p_jump = NULL;
229
230     /* Hide tooltips if the option is set */
231     if( !config_GetInt( p_intf, "gnome-tooltips" ) )
232     {
233         gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
234     }
235
236     /* Hide toolbar text of the option is set */
237     if( !config_GetInt( p_intf, "gnome-toolbartext" ) )
238     {
239         gtk_toolbar_set_style(
240             GTK_TOOLBAR(lookup_widget( p_intf->p_sys->p_window, "toolbar" )),
241             GTK_TOOLBAR_ICONS );
242     }
243
244     /* Store p_intf to keep an eye on it */
245     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
246                          "p_intf", p_intf );
247
248     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
249                          "p_intf", p_intf );
250
251     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
252                          "p_intf", p_intf );
253
254     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
255                          "p_intf", p_intf );
256
257     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
258                          "p_intf", p_intf );
259
260     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
261                                      "p_intf", p_intf );
262
263     psz_sout = config_GetPsz( p_intf, "sout" );
264     p_target = g_string_new( psz_sout ? psz_sout : "" );
265     if( psz_sout ) free( psz_sout );
266
267     gtk_entry_set_text( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ), "sout_entry_target" ), p_target->str );
268     g_string_free( p_target, TRUE );
269
270     /* FIXME it's to be sure that only file entry is selected */
271     gtk_toggle_button_set_active(  gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
272                                    "sout_access_udp" ), TRUE );
273
274     gtk_toggle_button_set_active(  gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
275                                    "sout_access_file" ), TRUE );
276
277     /* Show the control window */
278     gtk_widget_show( p_intf->p_sys->p_window );
279
280     while( !p_intf->b_die )
281     {
282         Manage( p_intf );
283
284         /* Sleep to avoid using all CPU - since some interfaces need to
285          * access keyboard events, a 100ms delay is a good compromise */
286         gdk_threads_leave();
287         msleep( INTF_IDLE_SLEEP );
288         gdk_threads_enter();
289     }
290
291     /* Destroy the Tooltips structure */
292     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
293     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_messages) );
294     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_playwin) );
295     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_popup) );
296     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
297
298     gdk_threads_leave();
299 }
300
301 /* following functions are local */
302
303 /*****************************************************************************
304  * Manage: manage main thread messages
305  *****************************************************************************
306  * In this function, called approx. 10 times a second, we check what the
307  * main program wanted to tell us.
308  *****************************************************************************/
309 static void Manage( intf_thread_t *p_intf )
310 {
311     int i_start, i_stop;
312
313     vlc_mutex_lock( &p_intf->change_lock );
314
315     /* If the "display popup" flag has changed */
316     if( p_intf->b_menu_change )
317     {
318         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
319         {
320             p_intf->p_sys->p_popup = create_intf_popup();
321             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
322                                  "p_popup", p_intf );
323         }
324
325         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
326                                    NULL, NULL, NULL, NULL );
327         p_intf->b_menu_change = 0;
328     }
329
330     /* Update the log window */
331     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
332     i_stop = *p_intf->p_sys->p_sub->pi_stop;
333     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
334
335     if( p_intf->p_sys->p_sub->i_start != i_stop )
336     {
337         static GdkColor white  = { 0, 0xffff, 0xffff, 0xffff };
338         static GdkColor gray   = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
339         static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
340         static GdkColor red    = { 0, 0xffff, 0x6666, 0x6666 };
341
342         static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
343                                              " debug: " };
344         static GdkColor *   pp_color[4] = { &white, &red, &yellow, &gray };
345
346         for( i_start = p_intf->p_sys->p_sub->i_start;
347              i_start != i_stop;
348              i_start = (i_start+1) % VLC_MSG_QSIZE )
349         {
350             /* Append all messages to log window */
351             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
352              NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
353
354             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
355                 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
356                 -1 );
357
358             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
359                 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
360                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
361
362             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
363                 NULL, "\n", -1 );
364         }
365
366         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
367         p_intf->p_sys->p_sub->i_start = i_start;
368         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
369
370         gtk_text_set_point( p_intf->p_sys->p_messages_text,
371                     gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
372     }
373
374     /* Update the playlist */
375     GtkPlayListManage( p_intf );
376
377     /* Update the input */
378     if( p_intf->p_sys->p_input != NULL )
379     {
380         if( p_intf->p_sys->p_input->b_dead )
381         {
382             vlc_object_release( p_intf->p_sys->p_input );
383             p_intf->p_sys->p_input = NULL;
384         }
385     }
386
387     if( p_intf->p_sys->p_input == NULL )
388     {
389         p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
390                                                           FIND_ANYWHERE );
391     }
392
393     if( p_intf->p_sys->p_input )
394     {
395         input_thread_t *p_input = p_intf->p_sys->p_input;
396         aout_instance_t *p_aout  = NULL;
397         vout_thread_t   *p_vout  = NULL;
398         vlc_bool_t      b_need_menus = VLC_FALSE;
399
400         vlc_mutex_lock( &p_input->stream.stream_lock );
401
402         if( !p_input->b_die )
403         {
404             /* New input or stream map change */
405             if( p_input->stream.b_changed )
406             {
407                 E_(GtkModeManage)( p_intf );
408                 GtkSetupMenus( p_intf );
409                 p_intf->p_sys->b_playing = 1;
410             }
411
412             /* Manage the slider */
413             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
414             {
415                 float newvalue = p_intf->p_sys->p_adj->value;
416
417 #define p_area p_input->stream.p_selected_area
418                 /* If the user hasn't touched the slider since the last time,
419                  * then the input can safely change it */
420                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
421                 {
422                     /* Update the value */
423                     p_intf->p_sys->p_adj->value =
424                     p_intf->p_sys->f_adj_oldvalue =
425                         ( 100. * p_area->i_tell ) / p_area->i_size;
426
427                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
428                                              "value_changed" );
429                 }
430                 /* Otherwise, send message to the input if the user has
431                  * finished dragging the slider.
432                  * Beware, the hack below is needed by the dvdplay plugin! */
433                 else if( p_intf->p_sys->b_slider_free
434                 /* hack -> */ && (p_intf->p_sys->f_adj_oldvalue <= 100.) )
435                 {
436                     if( newvalue > 0. && newvalue < 100. )
437                     {
438                         off_t i_seek = ( newvalue * p_area->i_size ) / 100;
439
440                         vlc_mutex_unlock( &p_input->stream.stream_lock );
441                         input_Seek( p_input, i_seek, INPUT_SEEK_SET );
442                         vlc_mutex_lock( &p_input->stream.stream_lock );
443                     }
444
445                     /* Update the old value */
446                     p_intf->p_sys->f_adj_oldvalue = newvalue;
447                 }
448 #undef p_area
449             }
450
451             if( p_intf->p_sys->i_part !=
452                 p_input->stream.p_selected_area->i_part )
453             {
454                 p_intf->p_sys->b_chapter_update = 1;
455                 b_need_menus = VLC_TRUE;
456             }
457
458             /* Does the audio output require to update the menus ? */
459             p_aout = (aout_instance_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
460                                                          FIND_ANYWHERE );
461             if( p_aout != NULL )
462             {
463                 vlc_value_t val;
464                 if( var_Get( (vlc_object_t *)p_aout, "intf-change", &val ) >= 0
465                     && val.b_bool )
466                 {
467                     p_intf->p_sys->b_aout_update = 1;
468                     b_need_menus = 1;
469                 }
470
471                 vlc_object_release( (vlc_object_t *)p_aout );
472             }
473
474
475             /* Does the video output require to update the menus ? */
476             p_vout = (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
477                                                        FIND_ANYWHERE );
478             if( p_vout != NULL ) 
479             {
480                 vlc_value_t val;
481                 if( var_Get( (vlc_object_t *)p_vout, "intf-change", &val ) >= 0
482                     && val.b_bool )
483                 {
484                     p_intf->p_sys->b_vout_update = 1;
485                     b_need_menus = 1;
486                 }
487
488                 vlc_object_release( (vlc_object_t *)p_vout );
489             }
490
491             if( b_need_menus )
492             {
493                 GtkSetupMenus( p_intf );
494             }
495         }
496
497         vlc_mutex_unlock( &p_input->stream.stream_lock );
498     }
499     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
500     {
501         E_(GtkModeManage)( p_intf );
502         p_intf->p_sys->b_playing = 0;
503     }
504
505     vlc_mutex_unlock( &p_intf->change_lock );
506
507     return;
508 }
509