]> git.sesse.net Git - vlc/blob - modules/gui/gtk/gtk.c
* all: begin to up to date the gtk/gnome interface. All comments are
[vlc] / 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.11 2003/01/20 20:07:06 fenrir 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 );
72     add_bool( "gtk-tooltips", 1, E_(GtkHideTooltips),
73               TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT );
74     add_integer( "gtk-prefs-maxh", 480, NULL,
75                  PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT );
76
77     set_description( _("Gtk+ interface module") );
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, "gtk_main", "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 = 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     p_intf->p_sys->b_program_update = 0;
123     p_intf->p_sys->b_title_update = 0;
124     p_intf->p_sys->b_chapter_update = 0;
125     p_intf->p_sys->b_spu_update = 0;
126     p_intf->p_sys->b_audio_update = 0;
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 = 1;
131
132     p_intf->p_sys->i_part = 0;
133
134     p_intf->p_sys->b_mute = 0;
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
176 #ifdef NEED_GTK_MAIN
177     gdk_threads_enter();
178 #else
179     /* gtk_init needs to know the command line. We don't care, so we
180      * give it an empty one */
181     char  *p_args[] = { "" };
182     char **pp_args  = p_args;
183     int    i_args   = 1;
184     int    i_dummy;
185
186     gtk_init( &i_args, &pp_args );
187 #endif
188
189     /* Create some useful widgets that will certainly be used */
190     p_intf->p_sys->p_window = create_intf_window();
191     p_intf->p_sys->p_popup = create_intf_popup();
192     p_intf->p_sys->p_playwin = create_intf_playlist();
193     p_intf->p_sys->p_messages = create_intf_messages();
194     p_intf->p_sys->p_tooltips = gtk_tooltips_new();
195
196     /* Set the title of the main window */
197     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
198                           VOUT_TITLE " (Gtk+ interface)");
199
200     /* Accept file drops on the main window */
201     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
202                        GTK_DEST_DEFAULT_ALL, target_table,
203                        DROP_ACCEPT_END, GDK_ACTION_COPY );
204
205     /* Accept file drops on the playlist window */
206     gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playwin,
207                                    "playlist_clist") ),
208                        GTK_DEST_DEFAULT_ALL, target_table,
209                        DROP_ACCEPT_END, GDK_ACTION_COPY );
210
211     /* Get the slider object */
212     p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
213         GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" ) );
214
215     /* Configure the log window */
216     p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
217         GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
218     gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
219     gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
220
221     /* Get the interface labels */
222 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
223                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
224     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
225     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
226 #undef P_LABEL
227
228     /* Connect the date display to the slider */
229 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
230                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
231     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
232
233     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
234                          GTK_SIGNAL_FUNC( E_(GtkDisplayDate) ), NULL );
235     p_intf->p_sys->f_adj_oldvalue = 0;
236 #undef P_SLIDER
237
238     /* We don't create these ones yet because we perhaps won't need them */
239     p_intf->p_sys->p_about = NULL;
240     p_intf->p_sys->p_modules = NULL;
241     p_intf->p_sys->p_open = NULL;
242     p_intf->p_sys->p_jump = NULL;
243
244     /* Hide tooltips if the option is set */
245     if( !config_GetInt( p_intf, "gtk-tooltips" ) )
246     {
247         gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
248     }
249
250     /* Store p_intf to keep an eye on it */
251     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
252                          "p_intf", p_intf );
253
254     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
255                          "p_intf", p_intf );
256
257     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
258                          "p_intf", p_intf );
259
260     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
261                          "p_intf", p_intf );
262
263     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
264                          "p_intf", p_intf );
265
266     /* Show the control window */
267     gtk_widget_show( p_intf->p_sys->p_window );
268
269 #ifdef NEED_GTK_MAIN
270     while( !p_intf->b_die )
271     {
272         Manage( p_intf );
273
274         /* Sleep to avoid using all CPU - since some interfaces need to
275          * access keyboard events, a 100ms delay is a good compromise */
276         gdk_threads_leave();
277         msleep( INTF_IDLE_SLEEP );
278         gdk_threads_enter();
279     }
280 #else
281     /* Sleep to avoid using all CPU - since some interfaces needs to access
282      * keyboard events, a 100ms delay is a good compromise */
283     i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, (GtkFunction)Manage,
284                                p_intf );
285     /* Enter Gtk mode */
286     gtk_main();
287     /* Remove the timeout */
288     gtk_timeout_remove( i_dummy );
289 #endif
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 #ifdef NEED_GTK_MAIN
299     gdk_threads_leave();
300 #endif
301 }
302
303 /* following functions are local */
304
305 /*****************************************************************************
306  * Manage: manage main thread messages
307  *****************************************************************************
308  * In this function, called approx. 10 times a second, we check what the
309  * main program wanted to tell us.
310  *****************************************************************************/
311 static int Manage( intf_thread_t *p_intf )
312 {
313     int i_start, i_stop;
314
315     vlc_mutex_lock( &p_intf->change_lock );
316
317     /* If the "display popup" flag has changed */
318     if( p_intf->b_menu_change )
319     {
320         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
321         {
322             p_intf->p_sys->p_popup = create_intf_popup();
323             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
324                                  "p_intf", p_intf );
325         }
326         gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
327                         NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
328         p_intf->b_menu_change = 0;
329     }
330
331     /* Update the log window */
332     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
333     i_stop = *p_intf->p_sys->p_sub->pi_stop;
334     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
335
336     if( p_intf->p_sys->p_sub->i_start != i_stop )
337     {
338         static GdkColor white  = { 0, 0xffff, 0xffff, 0xffff };
339         static GdkColor gray   = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
340         static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
341         static GdkColor red    = { 0, 0xffff, 0x6666, 0x6666 };
342
343         static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
344                                              " debug: " };
345         static GdkColor *   pp_color[4] = { &white, &red, &yellow, &gray };
346
347         for( i_start = p_intf->p_sys->p_sub->i_start;
348              i_start != i_stop;
349              i_start = (i_start+1) % VLC_MSG_QSIZE )
350         {
351             /* Append all messages to log window */
352             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
353              NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
354
355             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
356                 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
357                 -1 );
358
359             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
360                 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
361                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
362
363             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
364                 NULL, "\n", -1 );
365         }
366
367         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
368         p_intf->p_sys->p_sub->i_start = i_start;
369         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
370
371         gtk_text_set_point( p_intf->p_sys->p_messages_text,
372                     gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
373     }
374
375     /* Update the playlist */
376     GtkPlayListManage( p_intf );
377
378     /* Update the input */
379     if( p_intf->p_sys->p_input == NULL )
380     {
381         p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
382                                                           FIND_ANYWHERE );
383     }
384     else if( p_intf->p_sys->p_input->b_dead )
385     {
386         vlc_object_release( p_intf->p_sys->p_input );
387         p_intf->p_sys->p_input = NULL;
388     }
389
390     if( p_intf->p_sys->p_input )
391     {
392         input_thread_t  *p_input = p_intf->p_sys->p_input;
393         aout_instance_t *p_aout  = NULL;
394         vout_thread_t   *p_vout  = NULL;
395         vlc_bool_t      b_need_menus = VLC_FALSE;
396
397         vlc_mutex_lock( &p_input->stream.stream_lock );
398
399         if( !p_input->b_die )
400         {
401             /* New input or stream map change */
402             if( p_input->stream.b_changed )
403             {
404                 E_(GtkModeManage)( p_intf );
405                 GtkSetupMenus( p_intf );
406                 p_intf->p_sys->b_playing = 1;
407             }
408
409             /* Manage the slider */
410             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
411             {
412                 float newvalue = p_intf->p_sys->p_adj->value;
413
414 #define p_area p_input->stream.p_selected_area
415                 /* If the user hasn't touched the slider since the last time,
416                  * then the input can safely change it */
417                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
418                 {
419                     /* Update the value */
420                     p_intf->p_sys->p_adj->value =
421                     p_intf->p_sys->f_adj_oldvalue =
422                         ( 100. * p_area->i_tell ) / p_area->i_size;
423
424                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
425                                              "value_changed" );
426                 }
427                 /* Otherwise, send message to the input if the user has
428                  * finished dragging the slider */
429                 else if( p_intf->p_sys->b_slider_free )
430                 {
431                     off_t i_seek = ( newvalue * p_area->i_size ) / 100;
432
433                     /* release the lock to be able to seek */
434                     vlc_mutex_unlock( &p_input->stream.stream_lock );
435                     input_Seek( p_input, i_seek, INPUT_SEEK_SET );
436                     vlc_mutex_lock( &p_input->stream.stream_lock );
437
438                     /* Update the old value */
439                     p_intf->p_sys->f_adj_oldvalue = newvalue;
440                 }
441 #undef p_area
442             }
443
444             if( p_intf->p_sys->i_part !=
445                 p_input->stream.p_selected_area->i_part )
446             {
447                 p_intf->p_sys->b_chapter_update = 1;
448                 b_need_menus = VLC_TRUE;
449             }
450
451             /* Does the audio output require to update the menus ? */
452             p_aout = (aout_instance_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
453                                                          FIND_ANYWHERE );
454             if( p_aout != NULL )
455             {
456                 vlc_value_t val;
457                 if( var_Get( (vlc_object_t *)p_aout, "intf-change", &val ) >= 0
458                     && val.b_bool )
459                 {
460                     p_intf->p_sys->b_aout_update = 1;
461                     b_need_menus = 1;
462                 }
463
464                 vlc_object_release( (vlc_object_t *)p_aout );
465             }
466
467             /* Does the video output require to update the menus ? */
468             p_vout = (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
469                                                        FIND_ANYWHERE );
470             if( p_vout != NULL ) 
471             {
472                 vlc_value_t val;
473                 if( var_Get( (vlc_object_t *)p_vout, "intf-change", &val ) >= 0
474                     && val.b_bool )
475                 {
476                     p_intf->p_sys->b_vout_update = 1;
477                     b_need_menus = 1;
478                 }
479
480                 vlc_object_release( (vlc_object_t *)p_vout );
481             }
482             if( b_need_menus )
483             {
484                 GtkSetupMenus( p_intf );
485             }
486         }
487
488         vlc_mutex_unlock( &p_input->stream.stream_lock );
489     }
490     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
491     {
492         E_(GtkModeManage)( p_intf );
493         p_intf->p_sys->b_playing = 0;
494     }
495
496 #ifndef NEED_GTK_MAIN
497     if( p_intf->b_die )
498     {
499         vlc_mutex_unlock( &p_intf->change_lock );
500
501         /* Prepare to die, young Skywalker */
502         gtk_main_quit();
503
504         return FALSE;
505     }
506 #endif
507
508     vlc_mutex_unlock( &p_intf->change_lock );
509
510     return TRUE;
511 }