]> git.sesse.net Git - vlc/blob - plugins/gtk/gtk.c
* ./plugins/gtk/gtk.c: the log window is now black with colourful text.
[vlc] / plugins / gtk / gtk.c
1 /*****************************************************************************
2  * gtk.c : Gtk+ plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: gtk.c,v 1.13 2002/02/20 05:56:17 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 <videolan/vlc.h>
33
34 #include <gtk/gtk.h>
35
36 #include "stream_control.h"
37 #include "input_ext-intf.h"
38
39 #include "interface.h"
40 #include "intf_playlist.h"
41
42 #include "video.h"
43 #include "video_output.h"
44
45 #include "gtk_callbacks.h"
46 #include "gtk_interface.h"
47 #include "gtk_support.h"
48 #include "gtk_menu.h"
49 #include "gtk_display.h"
50 #include "gtk_common.h"
51
52 /*****************************************************************************
53  * Local prototypes.
54  *****************************************************************************/
55 static void intf_getfunctions ( function_list_t * p_function_list );
56 static int  intf_Open         ( intf_thread_t *p_intf );
57 static void intf_Close        ( intf_thread_t *p_intf );
58 static void intf_Run          ( intf_thread_t *p_intf );
59
60 static gint GtkManage         ( gpointer p_data );
61
62 /*****************************************************************************
63  * Building configuration tree
64  *****************************************************************************/
65 MODULE_CONFIG_START
66 MODULE_CONFIG_STOP
67
68 MODULE_INIT_START
69     SET_DESCRIPTION( "Gtk+ interface module" )
70 #ifndef WIN32
71     if( getenv( "DISPLAY" ) == NULL )
72     {
73         ADD_CAPABILITY( INTF, 10 )
74     }
75     else
76 #endif
77     {
78         ADD_CAPABILITY( INTF, 90 )
79     }
80     ADD_SHORTCUT( "gtk" )
81     ADD_PROGRAM( "gvlc" )
82 MODULE_INIT_STOP
83
84 MODULE_ACTIVATE_START
85     intf_getfunctions( &p_module->p_functions->intf );
86 MODULE_ACTIVATE_STOP
87
88 MODULE_DEACTIVATE_START
89 MODULE_DEACTIVATE_STOP
90
91 /*****************************************************************************
92  * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
93  *****************************************************************************
94  * gtk_init() makes several calls to g_atexit() which calls atexit() to
95  * register tidying callbacks to be called at program exit. Since the Gtk+
96  * plugin is likely to be unloaded at program exit, we have to export this
97  * symbol to intercept the g_atexit() calls. Talk about crude hack.
98  *****************************************************************************/
99 void g_atexit( GVoidFunc func )
100 {
101     intf_thread_t *p_intf = p_main->p_intf;
102     int i_dummy;
103
104     for( i_dummy = 0;
105          i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
106          i_dummy++ )
107     {
108         ;
109     }
110
111     if( i_dummy >= MAX_ATEXIT - 1 )
112     {
113         intf_ErrMsg( "intf error: too many atexit() callbacks to register" );
114         return;
115     }
116
117     p_intf->p_sys->pf_callback[i_dummy]     = func;
118     p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
119 }
120
121 /*****************************************************************************
122  * Functions exported as capabilities. They are declared as static so that
123  * we don't pollute the namespace too much.
124  *****************************************************************************/
125 static void intf_getfunctions( function_list_t * p_function_list )
126 {
127     p_function_list->functions.intf.pf_open  = intf_Open;
128     p_function_list->functions.intf.pf_close = intf_Close;
129     p_function_list->functions.intf.pf_run   = intf_Run;
130 }
131
132 /*****************************************************************************
133  * intf_Open: initialize and create window
134  *****************************************************************************/
135 static int intf_Open( intf_thread_t *p_intf )
136 {
137     /* Allocate instance and initialize some members */
138     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
139     if( p_intf->p_sys == NULL )
140     {
141         intf_ErrMsg("error: %s", strerror(ENOMEM));
142         return( 1 );
143     }
144
145     p_intf->p_sys->p_sub = intf_MsgSub();
146
147     /* Initialize Gtk+ thread */
148     p_intf->p_sys->b_playing = 0;
149     p_intf->p_sys->b_popup_changed = 0;
150     p_intf->p_sys->b_window_changed = 0;
151     p_intf->p_sys->b_playlist_changed = 0;
152
153     p_intf->p_sys->i_playing = -1;
154     p_intf->p_sys->b_slider_free = 1;
155
156     p_intf->p_sys->pf_callback[0] = NULL;
157
158     return( 0 );
159 }
160
161 /*****************************************************************************
162  * intf_Close: destroy interface window
163  *****************************************************************************/
164 static void intf_Close( intf_thread_t *p_intf )
165 {
166     intf_MsgUnsub( p_intf->p_sys->p_sub );
167
168     /* Destroy structure */
169     free( p_intf->p_sys );
170 }
171
172 /*****************************************************************************
173  * intf_Run: Gtk+ thread
174  *****************************************************************************
175  * this part of the interface is in a separate thread so that we can call
176  * gtk_main() from within it without annoying the rest of the program.
177  * XXX: the approach may look kludgy, and probably is, but I could not find
178  * a better way to dynamically load a Gtk+ interface at runtime.
179  *****************************************************************************/
180 static void intf_Run( intf_thread_t *p_intf )
181 {
182     /* gtk_init needs to know the command line. We don't care, so we
183      * give it an empty one */
184     char  *p_args[] = { "" };
185     char **pp_args  = p_args;
186     int    i_args   = 1;
187     int    i_dummy;
188
189     /* The data types we are allowed to receive */
190     static GtkTargetEntry target_table[] =
191     {
192         { "STRING", 0, DROP_ACCEPT_STRING },
193         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
194         { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
195     };
196
197     /* Initialize Gtk+ */
198     gtk_init( &i_args, &pp_args );
199
200     /* Create some useful widgets that will certainly be used */
201     p_intf->p_sys->p_window = create_intf_window( );
202     p_intf->p_sys->p_popup = create_intf_popup( );
203     p_intf->p_sys->p_playlist = create_intf_playlist();
204     p_intf->p_sys->p_messages = create_intf_messages();
205     
206     /* Set the title of the main window */
207     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
208                           VOUT_TITLE " (Gtk+ interface)");
209
210     /* Accept file drops on the main window */
211     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
212                        GTK_DEST_DEFAULT_ALL, target_table,
213                        1, GDK_ACTION_COPY );
214
215     /* Accept file drops on the playlist window */
216     gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playlist,
217                                    "playlist_clist") ),
218                        GTK_DEST_DEFAULT_ALL, target_table,
219                        1, GDK_ACTION_COPY );
220
221     /* Get the slider object */
222     p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
223         GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" ) ); 
224
225     /* Configure the log window */
226     p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
227         GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
228     gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
229     gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
230
231     /* Get the interface labels */
232 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
233                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
234     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
235     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
236 #undef P_LABEL
237
238     /* Connect the date display to the slider */
239 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
240                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
241     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
242
243     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
244                          GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
245     p_intf->p_sys->f_adj_oldvalue = 0;
246 #undef P_SLIDER
247
248     /* We don't create these ones yet because we perhaps won't need them */
249     p_intf->p_sys->p_about = NULL;
250     p_intf->p_sys->p_modules = NULL;
251     p_intf->p_sys->p_fileopen = NULL;
252     p_intf->p_sys->p_disc = NULL;
253     p_intf->p_sys->p_network = NULL;
254     p_intf->p_sys->p_preferences = NULL;
255     p_intf->p_sys->p_jump = NULL;
256
257     /* Store p_intf to keep an eye on it */
258     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
259                          "p_intf", p_intf );
260
261     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
262                          "p_intf", p_intf );
263
264     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
265                          "p_intf", p_intf );
266
267     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
268                          "p_intf", p_intf );
269
270     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
271                          "p_intf", p_intf );
272
273     /* Show the control window */
274     gtk_widget_show( p_intf->p_sys->p_window );
275
276     /* Sleep to avoid using all CPU - since some interfaces needs to access
277      * keyboard events, a 100ms delay is a good compromise */
278     i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GtkManage, p_intf );
279
280     /* Enter Gtk mode */
281     gtk_main();
282
283     /* Remove the timeout */
284     gtk_timeout_remove( i_dummy );
285
286     /* Launch stored callbacks */
287     for( i_dummy = 0;
288          i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
289          i_dummy++ )
290     {
291         p_intf->p_sys->pf_callback[i_dummy]();
292     }
293 }
294
295 /* following functions are local */
296
297 /*****************************************************************************
298  * GtkManage: 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 gint GtkManage( gpointer p_data )
304 {
305 #define p_intf ((intf_thread_t *)p_data)
306     static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
307     static GdkColor red   = { 0, 0xffff, 0x6666, 0x6666 };
308     static GdkColor gray  = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
309     GdkColor *p_color;
310
311     int i_start, i_stop;
312
313     vlc_mutex_lock( &p_intf->change_lock );
314     
315     /* If the "display popup" flag has changed */
316     if( p_intf->b_menu_change )
317     {
318         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
319         {
320             p_intf->p_sys->p_popup = create_intf_popup();
321             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
322                                  "p_popup", p_intf );
323         }
324         gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
325                         NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
326         p_intf->b_menu_change = 0;
327     }
328
329     /* Update the log window */
330     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
331     i_stop = *p_intf->p_sys->p_sub->pi_stop;
332     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
333
334     if( p_intf->p_sys->p_sub->i_start != i_stop )
335     {
336         for( i_start = p_intf->p_sys->p_sub->i_start;
337              i_start != i_stop;
338              i_start = (i_start+1) % INTF_MSG_QSIZE )
339         {
340             /* Append all messages to log window */
341             switch( p_intf->p_sys->p_sub->p_msg[i_start].i_type )
342             {
343             case INTF_MSG_ERR:
344                 p_color = &red;
345                 break;
346             case INTF_MSG_WARN:
347                 p_color = &gray;
348                 break;
349             default:
350                 p_color = &white;
351                 break;
352             }
353
354             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, p_color,
355                 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
356             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, p_color,
357                 NULL, "\n", -1 );
358         }
359
360         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
361         p_intf->p_sys->p_sub->i_start = i_start;
362         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
363
364         gtk_text_set_point( p_intf->p_sys->p_messages_text,
365                     gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
366     }
367
368     /* Update the playlist */
369     GtkPlayListManage( p_data );
370
371     if( p_input_bank->pp_input[0] != NULL )
372     {
373         vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
374
375         if( !p_input_bank->pp_input[0]->b_die )
376         {
377             /* New input or stream map change */
378             if( p_input_bank->pp_input[0]->stream.b_changed )
379             {
380                 GtkModeManage( p_intf );
381                 GtkSetupMenus( p_intf );
382                 p_intf->p_sys->b_playing = 1;
383             }
384
385             /* Manage the slider */
386             if( p_input_bank->pp_input[0]->stream.b_seekable &&
387                 p_intf->p_sys->b_playing )
388             {
389                 float newvalue = p_intf->p_sys->p_adj->value;
390     
391 #define p_area p_input_bank->pp_input[0]->stream.p_selected_area
392                 /* If the user hasn't touched the slider since the last time,
393                  * then the input can safely change it */
394                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
395                 {
396                     /* Update the value */
397                     p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
398                         ( 100. * p_area->i_tell ) / p_area->i_size;
399         
400                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
401                                              "value_changed" );
402                 }
403                 /* Otherwise, send message to the input if the user has
404                  * finished dragging the slider */
405                 else if( p_intf->p_sys->b_slider_free )
406                 {
407                     off_t i_seek = ( newvalue * p_area->i_size ) / 100;
408     
409                     /* release the lock to be able to seek */
410                     vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
411                     input_Seek( p_input_bank->pp_input[0], i_seek );
412                     vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
413         
414                     /* Update the old value */
415                     p_intf->p_sys->f_adj_oldvalue = newvalue;
416                 }
417 #    undef p_area
418             }
419     
420             if( p_intf->p_sys->i_part !=
421                 p_input_bank->pp_input[0]->stream.p_selected_area->i_part )
422             {
423                 p_intf->p_sys->b_chapter_update = 1;
424                 GtkSetupMenus( p_intf );
425             }
426         }
427
428         vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
429     }
430     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
431     {
432         GtkModeManage( p_intf );
433         p_intf->p_sys->b_playing = 0;
434     }
435
436     /* Manage core vlc functions through the callback */
437     p_intf->pf_manage( p_intf );
438
439     if( p_intf->b_die )
440     {
441         vlc_mutex_unlock( &p_intf->change_lock );
442
443         /* Prepare to die, young Skywalker */
444         gtk_main_quit();
445
446         /* Just in case */
447         return( FALSE );
448     }
449
450     vlc_mutex_unlock( &p_intf->change_lock );
451
452     return( TRUE );
453
454 #undef p_intf
455 }