]> git.sesse.net Git - vlc/blob - modules/gui/gtk/gtk.c
33e700cd96dcf50e2f6b366d0708b32dec40a667
[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.8 2002/11/12 16:02:51 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 <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_popup_changed = 0;
115     p_intf->p_sys->b_window_changed = 0;
116     p_intf->p_sys->b_playlist_changed = 0;
117     p_intf->p_sys->b_program_update = 0;
118     p_intf->p_sys->b_title_update = 0;
119     p_intf->p_sys->b_chapter_update = 0;
120     p_intf->p_sys->b_spu_update = 0;
121     p_intf->p_sys->b_audio_update = 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
129     return VLC_SUCCESS;
130 }
131
132 /*****************************************************************************
133  * Close: destroy interface window
134  *****************************************************************************/
135 static void Close( vlc_object_t *p_this )
136 {
137     intf_thread_t *p_intf = (intf_thread_t *)p_this;
138
139     if( p_intf->p_sys->p_input )
140     {
141         vlc_object_release( p_intf->p_sys->p_input );
142     }
143
144     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
145
146 #ifdef NEED_GTK_MAIN
147     module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
148 #endif
149
150     /* Destroy structure */
151     free( p_intf->p_sys );
152 }
153
154 /*****************************************************************************
155  * Run: Gtk+ thread
156  *****************************************************************************
157  * this part of the interface is in a separate thread so that we can call
158  * gtk_main() from within it without annoying the rest of the program.
159  *****************************************************************************/
160 static void Run( intf_thread_t *p_intf )
161 {
162     /* The data types we are allowed to receive */
163     static GtkTargetEntry target_table[] =
164     {
165         { "STRING", 0, DROP_ACCEPT_STRING },
166         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
167         { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
168     };
169
170 #ifdef NEED_GTK_MAIN
171     gdk_threads_enter();
172 #else
173     /* gtk_init needs to know the command line. We don't care, so we
174      * give it an empty one */
175     char  *p_args[] = { "" };
176     char **pp_args  = p_args;
177     int    i_args   = 1;
178     int    i_dummy;
179
180     gtk_init( &i_args, &pp_args );
181 #endif
182
183     /* Create some useful widgets that will certainly be used */
184     p_intf->p_sys->p_window = create_intf_window();
185     p_intf->p_sys->p_popup = create_intf_popup();
186     p_intf->p_sys->p_playwin = create_intf_playlist();
187     p_intf->p_sys->p_messages = create_intf_messages();
188     p_intf->p_sys->p_tooltips = gtk_tooltips_new();
189
190     /* Set the title of the main window */
191     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
192                           VOUT_TITLE " (Gtk+ interface)");
193
194     /* Accept file drops on the main window */
195     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
196                        GTK_DEST_DEFAULT_ALL, target_table,
197                        1, GDK_ACTION_COPY );
198
199     /* Accept file drops on the playlist window */
200     gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playwin,
201                                    "playlist_clist") ),
202                        GTK_DEST_DEFAULT_ALL, target_table,
203                        1, GDK_ACTION_COPY );
204
205     /* Get the slider object */
206     p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
207         GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" ) );
208
209     /* Configure the log window */
210     p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
211         GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
212     gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
213     gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
214
215     /* Get the interface labels */
216 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
217                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
218     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
219     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
220 #undef P_LABEL
221
222     /* Connect the date display to the slider */
223 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
224                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
225     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
226
227     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
228                          GTK_SIGNAL_FUNC( E_(GtkDisplayDate) ), NULL );
229     p_intf->p_sys->f_adj_oldvalue = 0;
230 #undef P_SLIDER
231
232     /* We don't create these ones yet because we perhaps won't need them */
233     p_intf->p_sys->p_about = NULL;
234     p_intf->p_sys->p_modules = NULL;
235     p_intf->p_sys->p_open = NULL;
236     p_intf->p_sys->p_jump = NULL;
237
238     /* Hide tooltips if the option is set */
239     if( !config_GetInt( p_intf, "gtk-tooltips" ) )
240     {
241         gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
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     /* Show the control window */
261     gtk_widget_show( p_intf->p_sys->p_window );
262
263 #ifdef NEED_GTK_MAIN
264     while( !p_intf->b_die )
265     {
266         Manage( p_intf );
267
268         /* Sleep to avoid using all CPU - since some interfaces need to
269          * access keyboard events, a 100ms delay is a good compromise */
270         gdk_threads_leave();
271         msleep( INTF_IDLE_SLEEP );
272         gdk_threads_enter();
273     }
274 #else
275     /* Sleep to avoid using all CPU - since some interfaces needs to access
276      * keyboard events, a 100ms delay is a good compromise */
277     i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, (GtkFunction)Manage,
278                                p_intf );
279     /* Enter Gtk mode */
280     gtk_main();
281     /* Remove the timeout */
282     gtk_timeout_remove( i_dummy );
283 #endif
284
285     /* Destroy the Tooltips structure */
286     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
287     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_messages) );
288     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_playwin) );
289     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_popup) );
290     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
291
292 #ifdef NEED_GTK_MAIN
293     gdk_threads_leave();
294 #endif
295 }
296
297 /* following functions are local */
298
299 /*****************************************************************************
300  * Manage: manage main thread messages
301  *****************************************************************************
302  * In this function, called approx. 10 times a second, we check what the
303  * main program wanted to tell us.
304  *****************************************************************************/
305 static int Manage( intf_thread_t *p_intf )
306 {
307     int i_start, i_stop;
308
309     vlc_mutex_lock( &p_intf->change_lock );
310
311     /* If the "display popup" flag has changed */
312     if( p_intf->b_menu_change )
313     {
314         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
315         {
316             p_intf->p_sys->p_popup = create_intf_popup();
317             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
318                                  "p_intf", p_intf );
319         }
320         gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
321                         NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
322         p_intf->b_menu_change = 0;
323     }
324
325     /* Update the log window */
326     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
327     i_stop = *p_intf->p_sys->p_sub->pi_stop;
328     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
329
330     if( p_intf->p_sys->p_sub->i_start != i_stop )
331     {
332         static GdkColor white  = { 0, 0xffff, 0xffff, 0xffff };
333         static GdkColor gray   = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
334         static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
335         static GdkColor red    = { 0, 0xffff, 0x6666, 0x6666 };
336
337         static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
338                                              " debug: " };
339         static GdkColor *   pp_color[4] = { &white, &red, &yellow, &gray };
340
341         for( i_start = p_intf->p_sys->p_sub->i_start;
342              i_start != i_stop;
343              i_start = (i_start+1) % VLC_MSG_QSIZE )
344         {
345             /* Append all messages to log window */
346             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
347              NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
348
349             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
350                 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
351                 -1 );
352
353             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
354                 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
355                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
356
357             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
358                 NULL, "\n", -1 );
359         }
360
361         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
362         p_intf->p_sys->p_sub->i_start = i_start;
363         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
364
365         gtk_text_set_point( p_intf->p_sys->p_messages_text,
366                     gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
367     }
368
369     /* Update the playlist */
370     GtkPlayListManage( p_intf );
371
372     /* Update the input */
373     if( p_intf->p_sys->p_input == NULL )
374     {
375         p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
376                                                           FIND_ANYWHERE );
377     }
378     else if( p_intf->p_sys->p_input->b_dead )
379     {
380         vlc_object_release( p_intf->p_sys->p_input );
381         p_intf->p_sys->p_input = NULL;
382     }
383
384     if( p_intf->p_sys->p_input )
385     {
386         input_thread_t *p_input = p_intf->p_sys->p_input;
387
388         vlc_mutex_lock( &p_input->stream.stream_lock );
389
390         if( !p_input->b_die )
391         {
392             /* New input or stream map change */
393             if( p_input->stream.b_changed )
394             {
395                 E_(GtkModeManage)( p_intf );
396                 GtkSetupMenus( p_intf );
397                 p_intf->p_sys->b_playing = 1;
398             }
399
400             /* Manage the slider */
401             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
402             {
403                 float newvalue = p_intf->p_sys->p_adj->value;
404
405 #define p_area p_input->stream.p_selected_area
406                 /* If the user hasn't touched the slider since the last time,
407                  * then the input can safely change it */
408                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
409                 {
410                     /* Update the value */
411                     p_intf->p_sys->p_adj->value =
412                     p_intf->p_sys->f_adj_oldvalue =
413                         ( 100. * p_area->i_tell ) / p_area->i_size;
414
415                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
416                                              "value_changed" );
417                 }
418                 /* Otherwise, send message to the input if the user has
419                  * finished dragging the slider */
420                 else if( p_intf->p_sys->b_slider_free )
421                 {
422                     off_t i_seek = ( newvalue * p_area->i_size ) / 100;
423
424                     /* release the lock to be able to seek */
425                     vlc_mutex_unlock( &p_input->stream.stream_lock );
426                     input_Seek( p_input, i_seek, INPUT_SEEK_SET );
427                     vlc_mutex_lock( &p_input->stream.stream_lock );
428
429                     /* Update the old value */
430                     p_intf->p_sys->f_adj_oldvalue = newvalue;
431                 }
432 #undef p_area
433             }
434
435             if( p_intf->p_sys->i_part !=
436                 p_input->stream.p_selected_area->i_part )
437             {
438                 p_intf->p_sys->b_chapter_update = 1;
439                 GtkSetupMenus( p_intf );
440             }
441         }
442
443         vlc_mutex_unlock( &p_input->stream.stream_lock );
444     }
445     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
446     {
447         E_(GtkModeManage)( p_intf );
448         p_intf->p_sys->b_playing = 0;
449     }
450
451 #ifndef NEED_GTK_MAIN
452     if( p_intf->b_die )
453     {
454         vlc_mutex_unlock( &p_intf->change_lock );
455
456         /* Prepare to die, young Skywalker */
457         gtk_main_quit();
458
459         return FALSE;
460     }
461 #endif
462
463     vlc_mutex_unlock( &p_intf->change_lock );
464
465     return TRUE;
466 }