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