8c8a27e1a7469ef4ffea57611bb90be81e308060
[vlc.git] / 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.3 2002/09/30 11:05:39 sam 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 void 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     set_program( "gvlc" );
81 vlc_module_end();
82
83 /*****************************************************************************
84  * Open: initialize and create window
85  *****************************************************************************/
86 static int Open( vlc_object_t *p_this )
87 {
88     intf_thread_t *p_intf = (intf_thread_t *)p_this;
89
90     /* Allocate instance and initialize some members */
91     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
92     if( p_intf->p_sys == NULL )
93     {
94         msg_Err( p_intf, "out of memory" );
95         return VLC_ENOMEM;
96     }
97
98     p_intf->p_sys->p_gtk_main = module_Need( p_this, "gtk_main", "gtk" );
99     if( p_intf->p_sys->p_gtk_main == NULL )
100     {
101         free( p_intf->p_sys );
102         return VLC_EMODULE;
103     }
104
105     p_intf->pf_run = Run;
106
107     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
108
109     /* Initialize Gtk+ thread */
110     p_intf->p_sys->b_playing = 0;
111     p_intf->p_sys->b_popup_changed = 0;
112     p_intf->p_sys->b_window_changed = 0;
113     p_intf->p_sys->b_playlist_changed = 0;
114
115     p_intf->p_sys->p_input = NULL;
116     p_intf->p_sys->i_playing = -1;
117     p_intf->p_sys->b_slider_free = 1;
118
119     p_intf->p_sys->i_part = 0;
120
121     return VLC_SUCCESS;
122 }
123
124 /*****************************************************************************
125  * Close: destroy interface window
126  *****************************************************************************/
127 static void Close( vlc_object_t *p_this )
128 {
129     intf_thread_t *p_intf = (intf_thread_t *)p_this;
130
131     if( p_intf->p_sys->p_input )
132     {
133         vlc_object_release( p_intf->p_sys->p_input );
134     }
135
136     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
137
138     module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
139
140     /* Destroy structure */
141     free( p_intf->p_sys );
142 }
143
144 /*****************************************************************************
145  * Run: Gtk+ thread
146  *****************************************************************************
147  * this part of the interface is in a separate thread so that we can call
148  * gtk_main() from within it without annoying the rest of the program.
149  *****************************************************************************/
150 static void Run( intf_thread_t *p_intf )
151 {
152     /* The data types we are allowed to receive */
153     static GtkTargetEntry target_table[] =
154     {
155         { "STRING", 0, DROP_ACCEPT_STRING },
156         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
157         { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
158     };
159
160     gdk_threads_enter();
161
162     /* Create some useful widgets that will certainly be used */
163     p_intf->p_sys->p_window = create_intf_window();
164     p_intf->p_sys->p_popup = create_intf_popup();
165     p_intf->p_sys->p_playwin = create_intf_playlist();
166     p_intf->p_sys->p_messages = create_intf_messages();
167     p_intf->p_sys->p_tooltips = gtk_tooltips_new();
168
169     /* Set the title of the main window */
170     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
171                           VOUT_TITLE " (Gtk+ interface)");
172
173     /* Accept file drops on the main window */
174     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
175                        GTK_DEST_DEFAULT_ALL, target_table,
176                        1, GDK_ACTION_COPY );
177
178     /* Accept file drops on the playlist window */
179     gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playwin,
180                                    "playlist_clist") ),
181                        GTK_DEST_DEFAULT_ALL, target_table,
182                        1, GDK_ACTION_COPY );
183
184     /* Get the slider object */
185     p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
186         GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" ) );
187
188     /* Configure the log window */
189     p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
190         GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
191     gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
192     gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
193
194     /* Get the interface labels */
195 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
196                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
197     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
198     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
199 #undef P_LABEL
200
201     /* Connect the date display to the slider */
202 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
203                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
204     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
205
206     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
207                          GTK_SIGNAL_FUNC( E_(GtkDisplayDate) ), NULL );
208     p_intf->p_sys->f_adj_oldvalue = 0;
209 #undef P_SLIDER
210
211     /* We don't create these ones yet because we perhaps won't need them */
212     p_intf->p_sys->p_about = NULL;
213     p_intf->p_sys->p_modules = NULL;
214     p_intf->p_sys->p_open = NULL;
215     p_intf->p_sys->p_jump = NULL;
216
217     /* Hide tooltips if the option is set */
218     if( !config_GetInt( p_intf, "gtk-tooltips" ) )
219     {
220         gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
221     }
222
223     /* Store p_intf to keep an eye on it */
224     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
225                          "p_intf", p_intf );
226
227     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
228                          "p_intf", p_intf );
229
230     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
231                          "p_intf", p_intf );
232
233     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
234                          "p_intf", p_intf );
235
236     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
237                          "p_intf", p_intf );
238
239     /* Show the control window */
240     gtk_widget_show( p_intf->p_sys->p_window );
241
242     while( !p_intf->b_die )
243     {
244         Manage( p_intf );
245
246         /* Sleep to avoid using all CPU - since some interfaces need to
247          * access keyboard events, a 100ms delay is a good compromise */
248         gdk_threads_leave();
249         msleep( INTF_IDLE_SLEEP );
250         gdk_threads_enter();
251     }
252
253     /* Destroy the Tooltips structure */
254     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
255     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_messages) );
256     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_playwin) );
257     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_popup) );
258     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
259
260     gdk_threads_leave();
261 }
262
263 /* following functions are local */
264
265 /*****************************************************************************
266  * Manage: manage main thread messages
267  *****************************************************************************
268  * In this function, called approx. 10 times a second, we check what the
269  * main program wanted to tell us.
270  *****************************************************************************/
271 static void Manage( intf_thread_t *p_intf )
272 {
273     int i_start, i_stop;
274
275     vlc_mutex_lock( &p_intf->change_lock );
276
277     /* If the "display popup" flag has changed */
278     if( p_intf->b_menu_change )
279     {
280         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
281         {
282             p_intf->p_sys->p_popup = create_intf_popup();
283             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
284                                  "p_intf", p_intf );
285         }
286         gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
287                         NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
288         p_intf->b_menu_change = 0;
289     }
290
291     /* Update the log window */
292     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
293     i_stop = *p_intf->p_sys->p_sub->pi_stop;
294     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
295
296     if( p_intf->p_sys->p_sub->i_start != i_stop )
297     {
298         static GdkColor white  = { 0, 0xffff, 0xffff, 0xffff };
299         static GdkColor gray   = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
300         static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
301         static GdkColor red    = { 0, 0xffff, 0x6666, 0x6666 };
302
303         static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
304                                              " debug: " };
305         static GdkColor *   pp_color[4] = { &white, &red, &yellow, &gray };
306
307         for( i_start = p_intf->p_sys->p_sub->i_start;
308              i_start != i_stop;
309              i_start = (i_start+1) % VLC_MSG_QSIZE )
310         {
311             /* Append all messages to log window */
312             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
313              NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
314
315             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
316                 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
317                 -1 );
318
319             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
320                 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
321                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
322
323             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
324                 NULL, "\n", -1 );
325         }
326
327         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
328         p_intf->p_sys->p_sub->i_start = i_start;
329         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
330
331         gtk_text_set_point( p_intf->p_sys->p_messages_text,
332                     gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
333     }
334
335     /* Update the playlist */
336     GtkPlayListManage( p_intf );
337
338     /* Update the input */
339     if( p_intf->p_sys->p_input == NULL )
340     {
341         p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
342                                                           FIND_ANYWHERE );
343     }
344     else if( p_intf->p_sys->p_input->b_dead )
345     {
346         vlc_object_release( p_intf->p_sys->p_input );
347         p_intf->p_sys->p_input = NULL;
348     }
349
350     if( p_intf->p_sys->p_input )
351     {
352         input_thread_t *p_input = p_intf->p_sys->p_input;
353
354         vlc_mutex_lock( &p_input->stream.stream_lock );
355
356         if( !p_input->b_die )
357         {
358             /* New input or stream map change */
359             if( p_input->stream.b_changed )
360             {
361                 E_(GtkModeManage)( p_intf );
362                 GtkSetupMenus( p_intf );
363                 p_intf->p_sys->b_playing = 1;
364             }
365
366             /* Manage the slider */
367             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
368             {
369                 float newvalue = p_intf->p_sys->p_adj->value;
370
371 #define p_area p_input->stream.p_selected_area
372                 /* If the user hasn't touched the slider since the last time,
373                  * then the input can safely change it */
374                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
375                 {
376                     /* Update the value */
377                     p_intf->p_sys->p_adj->value =
378                     p_intf->p_sys->f_adj_oldvalue =
379                         ( 100. * p_area->i_tell ) / p_area->i_size;
380
381                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
382                                              "value_changed" );
383                 }
384                 /* Otherwise, send message to the input if the user has
385                  * finished dragging the slider */
386                 else if( p_intf->p_sys->b_slider_free )
387                 {
388                     off_t i_seek = ( newvalue * p_area->i_size ) / 100;
389
390                     /* release the lock to be able to seek */
391                     vlc_mutex_unlock( &p_input->stream.stream_lock );
392                     input_Seek( p_input, i_seek, INPUT_SEEK_SET );
393                     vlc_mutex_lock( &p_input->stream.stream_lock );
394
395                     /* Update the old value */
396                     p_intf->p_sys->f_adj_oldvalue = newvalue;
397                 }
398 #undef p_area
399             }
400
401             if( p_intf->p_sys->i_part !=
402                 p_input->stream.p_selected_area->i_part )
403             {
404                 p_intf->p_sys->b_chapter_update = 1;
405                 GtkSetupMenus( p_intf );
406             }
407         }
408
409         vlc_mutex_unlock( &p_input->stream.stream_lock );
410     }
411     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
412     {
413         E_(GtkModeManage)( p_intf );
414         p_intf->p_sys->b_playing = 0;
415     }
416
417     vlc_mutex_unlock( &p_intf->change_lock );
418 }