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