]> git.sesse.net Git - vlc/blob - plugins/gtk/gnome.c
* modified the gtk interface to save an empty <string> option as a NULL pointer
[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.14 2002/03/25 20:37:00 lool 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     p_intf->p_sys->p_tooltips = gtk_tooltips_new();
254
255     /* Store p_intf to keep an eye on it */
256     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
257                          "p_intf", p_intf );
258
259     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
260                          "p_intf", p_intf );
261
262     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
263                          "p_intf", p_intf );
264
265     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
266                          "p_intf", p_intf );
267
268     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
269                          "p_intf", p_intf );
270
271     /* Show the control window */
272     gtk_widget_show( p_intf->p_sys->p_window );
273
274     /* Sleep to avoid using all CPU - since some interfaces needs to access
275      * keyboard events, a 100ms delay is a good compromise */
276     i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
277
278     /* Enter gnome mode */
279     gtk_main();
280
281     /* Remove the timeout */
282     gtk_timeout_remove( i_dummy );
283
284     /* Destroy the Tooltips structure */
285     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
286
287     /* Get rid of stored callbacks so we can unload the plugin */
288     for( i_dummy = 0;
289          i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
290          i_dummy++ )
291     {
292         p_intf->p_sys->pf_callback[i_dummy]();
293     }
294 }
295
296 /* following functions are local */
297
298 /*****************************************************************************
299  * GnomeManage: manage main thread messages
300  *****************************************************************************
301  * In this function, called approx. 10 times a second, we check what the
302  * main program wanted to tell us.
303  *****************************************************************************/
304 static gint GnomeManage( gpointer p_data )
305 {
306 #define p_intf ((intf_thread_t *)p_data)
307     static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
308     static GdkColor red   = { 0, 0xffff, 0x6666, 0x6666 };
309     static GdkColor gray  = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
310     GdkColor *p_color;
311
312     int i_start, i_stop;
313
314     vlc_mutex_lock( &p_intf->change_lock );
315
316     /* If the "display popup" flag has changed */
317     if( p_intf->b_menu_change )
318     {
319         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
320         {
321             p_intf->p_sys->p_popup = create_intf_popup();
322             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
323                                  "p_popup", p_intf );
324         }
325
326         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
327                                    NULL, NULL, NULL, NULL );
328         p_intf->b_menu_change = 0;
329     }
330
331     /* Update the log window */
332     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
333     i_stop = *p_intf->p_sys->p_sub->pi_stop;
334     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
335
336     if( p_intf->p_sys->p_sub->i_start != i_stop )
337     {
338         for( i_start = p_intf->p_sys->p_sub->i_start;
339              i_start != i_stop;
340              i_start = (i_start+1) % INTF_MSG_QSIZE )
341         {
342             /* Append all messages to log window */
343             switch( p_intf->p_sys->p_sub->p_msg[i_start].i_type )
344             {
345             case INTF_MSG_ERR:
346                 p_color = &red;
347                 break;
348             case INTF_MSG_WARN:
349                 p_color = &gray;
350                 break;
351             default:
352                 p_color = &white;
353                 break;
354             }
355
356             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, p_color,
357                 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
358             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, p_color,
359                 NULL, "\n", -1 );
360         }
361
362         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
363         p_intf->p_sys->p_sub->i_start = i_start;
364         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
365
366         gtk_text_set_point( p_intf->p_sys->p_messages_text,
367                     gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
368     }
369
370     /* Update the playlist */
371     GtkPlayListManage( p_intf ); 
372
373     if( p_input_bank->pp_input[0] != NULL && !p_intf->b_die )
374     {
375         vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
376
377         if( !p_input_bank->pp_input[0]->b_die )
378         {
379             /* New input or stream map change */
380             if( p_input_bank->pp_input[0]->stream.b_changed )
381             {
382                 GtkModeManage( p_intf );
383                 GtkSetupMenus( p_intf );
384                 p_intf->p_sys->b_playing = 1;
385             }
386
387             /* Manage the slider */
388             if( p_input_bank->pp_input[0]->stream.b_seekable &&
389                 p_intf->p_sys->b_playing )
390             {
391                 float           newvalue;
392                 newvalue = p_intf->p_sys->p_adj->value;
393     
394 #define p_area p_input_bank->pp_input[0]->stream.p_selected_area
395                 /* If the user hasn't touched the slider since the last time,
396                  * then the input can safely change it */
397                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
398                 {
399                     /* Update the value */
400                     p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
401                         ( 100. * p_area->i_tell ) / p_area->i_size;
402     
403                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
404                                              "value_changed" );
405                 }
406                 /* Otherwise, send message to the input if the user has
407                  * finished dragging the slider */
408                 else if( p_intf->p_sys->b_slider_free )
409                 {
410                     off_t i_seek = ( newvalue * p_area->i_size ) / 100;
411         
412                     vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
413                     input_Seek( p_input_bank->pp_input[0], i_seek );
414                     vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
415     
416                     /* Update the old value */
417                     p_intf->p_sys->f_adj_oldvalue = newvalue;
418                 }
419 #undef p_area
420             }
421
422             if( p_intf->p_sys->i_part !=
423                 p_input_bank->pp_input[0]->stream.p_selected_area->i_part )
424             {
425                 p_intf->p_sys->b_chapter_update = 1;
426                 GtkSetupMenus( p_intf );
427             }
428         }
429
430         vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
431     }
432     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
433     {
434         GtkModeManage( p_intf );
435         p_intf->p_sys->b_playing = 0;
436     }
437
438     /* Manage core vlc functions through the callback */
439     p_intf->pf_manage( p_intf );
440
441     if( p_intf->b_die )
442     {
443         vlc_mutex_unlock( &p_intf->change_lock );
444
445         /* Prepare to die, young Skywalker */
446         gtk_main_quit();
447
448         /* Just in case */
449         return( FALSE );
450     }
451
452     vlc_mutex_unlock( &p_intf->change_lock );
453
454     return( TRUE );
455
456 #undef p_intf
457 }
458