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