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