]> git.sesse.net Git - vlc/blob - plugins/gtk/gnome.c
* ALL: got rid of p_object->p_this which is now useless.
[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.25 2002/06/01 18:04:48 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 <vlc/vlc.h>
33 #include <vlc/intf.h>
34
35 #include <gnome.h>
36
37 #include "gnome_callbacks.h"
38 #include "gnome_interface.h"
39 #include "gnome_support.h"
40 #include "gtk_display.h"
41 #include "gtk_common.h"
42
43 /*****************************************************************************
44  * Local prototypes.
45  *****************************************************************************/
46 static void intf_getfunctions( function_list_t * p_function_list );
47 static int  intf_Open        ( intf_thread_t *p_intf );
48 static void intf_Close       ( intf_thread_t *p_intf );
49 static void intf_Run         ( intf_thread_t *p_intf );
50
51 static gint GnomeManage      ( gpointer p_data );
52
53 /*****************************************************************************
54  * Local variables (mutex-protected).
55  *****************************************************************************/
56 static void ** pp_global_data;
57
58 /*****************************************************************************
59  * Building configuration tree
60  *****************************************************************************/
61 #define TOOLTIPS_TEXT N_("show tooltips")
62 #define TOOLTIPS_LONGTEXT N_("Show tooltips for configuration options.")
63
64 #define TOOLBAR_TEXT N_("show text on toolbar buttons")
65 #define TOOLBAR_LONGTEXT N_("Show the text below icons on the toolbar.")
66
67 #define PREFS_MAXH_TEXT N_("maximum height for the configuration windows")
68 #define PREFS_MAXH_LONGTEXT N_( \
69     "You can set the maximum height that the configuration windows in the " \
70     "preferences menu will occupy.")
71
72 MODULE_CONFIG_START
73 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
74 ADD_BOOL   ( "gnome-tooltips", 1, GtkHideTooltips, TOOLTIPS_TEXT,
75              TOOLTIPS_LONGTEXT )
76 ADD_BOOL   ( "gnome-toolbartext", 1, GtkHideToolbarText, TOOLBAR_TEXT,
77              TOOLBAR_LONGTEXT )
78 ADD_INTEGER( "gnome-prefs-maxh", 480, NULL, PREFS_MAXH_TEXT,
79              PREFS_MAXH_LONGTEXT )
80 MODULE_CONFIG_STOP
81
82 MODULE_INIT_START
83     pp_global_data = p_module->p_vlc->pp_global_data;
84     SET_DESCRIPTION( _("Gnome interface module") )
85 #ifndef WIN32
86     if( getenv( "DISPLAY" ) == NULL )
87     {
88         ADD_CAPABILITY( INTF, 15 )
89     }
90     else
91 #endif
92     {
93         ADD_CAPABILITY( INTF, 100 )
94     }
95     ADD_PROGRAM( "gnome-vlc" )
96 MODULE_INIT_STOP
97
98 MODULE_ACTIVATE_START
99     intf_getfunctions( &p_module->p_functions->intf );
100 MODULE_ACTIVATE_STOP
101
102 MODULE_DEACTIVATE_START
103 MODULE_DEACTIVATE_STOP
104
105 /*****************************************************************************
106  * g_atexit: kludge to avoid the Gnome thread to segfault at exit
107  *****************************************************************************
108  * gtk_init() makes several calls to g_atexit() which calls atexit() to
109  * register tidying callbacks to be called at program exit. Since the Gnome
110  * plugin is likely to be unloaded at program exit, we have to export this
111  * symbol to intercept the g_atexit() calls. Talk about crude hack.
112  *****************************************************************************/
113 void g_atexit( GVoidFunc func )
114 {
115     intf_thread_t *p_intf = (intf_thread_t *)*pp_global_data;
116     int i_dummy;
117
118     for( i_dummy = 0;
119          i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
120          i_dummy++ )
121     {
122         ;
123     }
124
125     if( i_dummy >= MAX_ATEXIT - 1 )
126     {
127         msg_Err( p_intf, "too many atexit() callbacks to register" );
128         return;
129     }
130
131     p_intf->p_sys->pf_callback[i_dummy]     = func;
132     p_intf->p_sys->pf_callback[i_dummy + 1] = NULL;
133 }
134
135 /*****************************************************************************
136  * Functions exported as capabilities. They are declared as static so that
137  * we don't pollute the namespace too much.
138  *****************************************************************************/
139 static void intf_getfunctions( function_list_t * p_function_list )
140 {
141     p_function_list->functions.intf.pf_open  = intf_Open;
142     p_function_list->functions.intf.pf_close = intf_Close;
143     p_function_list->functions.intf.pf_run   = intf_Run;
144 }
145
146 /*****************************************************************************
147  * intf_Open: initialize and create window
148  *****************************************************************************/
149 static int intf_Open( intf_thread_t *p_intf )
150 {
151     /* Allocate instance and initialize some members */
152     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
153     if( p_intf->p_sys == NULL )
154     {
155         msg_Err( p_intf, "out of memory" );
156         return( 1 );
157     }
158
159     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
160
161     /* Initialize Gnome thread */
162     p_intf->p_sys->b_playing = 0;
163     p_intf->p_sys->b_popup_changed = 0;
164     p_intf->p_sys->b_window_changed = 0;
165     p_intf->p_sys->b_playlist_changed = 0;
166
167     p_intf->p_sys->p_input = NULL;
168     p_intf->p_sys->i_playing = -1;
169     p_intf->p_sys->b_slider_free = 1;
170
171     p_intf->p_sys->pf_callback[0] = NULL;
172
173     p_intf->p_sys->i_part = 0;
174
175     return( 0 );
176 }
177
178 /*****************************************************************************
179  * intf_Close: destroy interface window
180  *****************************************************************************/
181 static void intf_Close( intf_thread_t *p_intf )
182 {
183     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
184
185     /* Destroy structure */
186     free( p_intf->p_sys );
187 }
188
189 /*****************************************************************************
190  * intf_Run: Gnome thread
191  *****************************************************************************
192  * this part of the interface is in a separate thread so that we can call
193  * gtk_main() from within it without annoying the rest of the program.
194  * XXX: the approach may look kludgy, and probably is, but I could not find
195  * a better way to dynamically load a Gnome interface at runtime.
196  *****************************************************************************/
197 static void intf_Run( intf_thread_t *p_intf )
198 {
199     /* gnome_init needs to know the command line. We don't care, so we
200      * give it an empty one */
201     char *p_args[] = { "" };
202     int   i_args   = 1;
203     int   i_dummy;
204
205     /* The data types we are allowed to receive */
206     static GtkTargetEntry target_table[] =
207     {
208         { "STRING", 0, DROP_ACCEPT_STRING },
209         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
210         { "text/plain",    0, DROP_ACCEPT_TEXT_PLAIN }
211     };
212
213     /* Initialize Gnome */
214
215     /* gnome_init will register stuff with g_atexit, so we need to take
216      * the global lock if we want to be able to intercept the calls */
217     vlc_mutex_lock( p_intf->p_vlc->p_global_lock );
218     *p_intf->p_vlc->pp_global_data = p_intf;
219     gnome_init( p_intf->p_vlc->psz_object_name, VERSION, i_args, p_args );
220     vlc_mutex_unlock( p_intf->p_vlc->p_global_lock );
221
222     /* Create some useful widgets that will certainly be used */
223     p_intf->p_sys->p_window = create_intf_window( );
224     p_intf->p_sys->p_popup = create_intf_popup( );
225     p_intf->p_sys->p_playlist = create_intf_playlist();
226     p_intf->p_sys->p_messages = create_intf_messages();
227
228     /* Set the title of the main window */
229     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
230                           VOUT_TITLE " (Gnome interface)");
231
232     /* Accept file drops on the main window */
233     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
234                        GTK_DEST_DEFAULT_ALL, target_table,
235                        1, GDK_ACTION_COPY );
236     /* Accept file drops on the playlist window */
237     gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
238                             p_intf->p_sys->p_playlist ), "playlist_clist") ),
239                        GTK_DEST_DEFAULT_ALL, target_table,
240                        1, GDK_ACTION_COPY );
241
242     /* Get the slider object */
243     p_intf->p_sys->p_slider_frame = gtk_object_get_data(
244                       GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
245
246     /* Configure the log window */
247     p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
248         GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
249     gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
250     gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
251
252     /* Get the interface labels */
253     #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
254                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
255     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
256     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
257     #undef P_LABEL
258
259     /* Connect the date display to the slider */
260     #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
261                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
262     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
263
264     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
265                          GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
266     p_intf->p_sys->f_adj_oldvalue = 0;
267     #undef P_SLIDER
268
269     p_intf->p_sys->p_tooltips = gtk_tooltips_new();
270
271     /* We don't create these ones yet because we perhaps won't need them */
272     p_intf->p_sys->p_about = NULL;
273     p_intf->p_sys->p_modules = NULL;
274     p_intf->p_sys->p_fileopen = NULL;
275     p_intf->p_sys->p_disc = NULL;
276     p_intf->p_sys->p_network = NULL;
277     p_intf->p_sys->p_sat = NULL;
278     p_intf->p_sys->p_jump = NULL;
279
280     /* Hide tooltips if the option is set */
281     if( !config_GetInt( p_intf, "gnome-tooltips" ) )
282     {
283         gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
284     }
285
286     /* Hide toolbar text of the option is set */
287     if( !config_GetInt( p_intf, "gnome-toolbartext" ) )
288     {
289         gtk_toolbar_set_style(
290             GTK_TOOLBAR(lookup_widget( p_intf->p_sys->p_window, "toolbar" )),
291             GTK_TOOLBAR_ICONS );
292     }
293
294     /* Store p_intf to keep an eye on it */
295     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
296                          "p_intf", p_intf );
297
298     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
299                          "p_intf", p_intf );
300
301     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
302                          "p_intf", p_intf );
303
304     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
305                          "p_intf", p_intf );
306
307     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
308                          "p_intf", p_intf );
309
310     /* Show the control window */
311     gtk_widget_show( p_intf->p_sys->p_window );
312
313     /* Sleep to avoid using all CPU - since some interfaces needs to access
314      * keyboard events, a 100ms delay is a good compromise */
315     i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
316
317     /* Enter gnome mode */
318     gtk_main();
319
320     /* Remove the timeout */
321     gtk_timeout_remove( i_dummy );
322
323     /* Destroy the Tooltips structure */
324     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
325
326     /* Get rid of stored callbacks so we can unload the plugin */
327     for( i_dummy = 0;
328          i_dummy < MAX_ATEXIT && p_intf->p_sys->pf_callback[i_dummy] != NULL;
329          i_dummy++ )
330     {
331         p_intf->p_sys->pf_callback[i_dummy]();
332     }
333 }
334
335 /* following functions are local */
336
337 /*****************************************************************************
338  * GnomeManage: manage main thread messages
339  *****************************************************************************
340  * In this function, called approx. 10 times a second, we check what the
341  * main program wanted to tell us.
342  *****************************************************************************/
343 static gint GnomeManage( gpointer p_data )
344 {
345 #define p_intf ((intf_thread_t *)p_data)
346     int i_start, i_stop;
347
348     vlc_mutex_lock( &p_intf->change_lock );
349
350     /* If the "display popup" flag has changed */
351     if( p_intf->b_menu_change )
352     {
353         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
354         {
355             p_intf->p_sys->p_popup = create_intf_popup();
356             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
357                                  "p_popup", p_intf );
358         }
359
360         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
361                                    NULL, NULL, NULL, NULL );
362         p_intf->b_menu_change = 0;
363     }
364
365     /* Update the log window */
366     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
367     i_stop = *p_intf->p_sys->p_sub->pi_stop;
368     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
369
370     if( p_intf->p_sys->p_sub->i_start != i_stop )
371     {
372         static GdkColor white  = { 0, 0xffff, 0xffff, 0xffff };
373         static GdkColor gray   = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
374         static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
375         static GdkColor red    = { 0, 0xffff, 0x6666, 0x6666 };
376
377         static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
378                                              " debug: " };
379         static GdkColor *   pp_color[4] = { &white, &red, &yellow, &gray };
380
381         for( i_start = p_intf->p_sys->p_sub->i_start;
382              i_start != i_stop;
383              i_start = (i_start+1) % VLC_MSG_QSIZE )
384         {
385             /* Append all messages to log window */
386             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
387              NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
388
389             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
390                 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
391                 -1 );
392
393             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
394                 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
395                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
396
397             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
398                 NULL, "\n", -1 );
399         }
400
401         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
402         p_intf->p_sys->p_sub->i_start = i_start;
403         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
404
405         gtk_text_set_point( p_intf->p_sys->p_messages_text,
406                     gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
407     }
408
409     /* Update the playlist */
410     GtkPlayListManage( p_intf );
411
412     /* Update the input */
413     if( p_intf->p_sys->p_input != NULL )
414     {
415         if( p_intf->p_sys->p_input->b_dead )
416         {
417             vlc_object_release( p_intf->p_sys->p_input );
418             p_intf->p_sys->p_input = NULL;
419         }
420     }
421     
422     if( p_intf->p_sys->p_input == NULL )
423     {
424         p_intf->p_sys->p_input = vlc_object_find( p_intf->p_vlc,
425                                               VLC_OBJECT_INPUT, FIND_CHILD );
426     }
427
428     if( p_intf->p_sys->p_input )
429     {
430         input_thread_t *p_input = p_intf->p_sys->p_input;
431
432         vlc_mutex_lock( &p_input->stream.stream_lock );
433
434         if( !p_input->b_die )
435         {
436             /* New input or stream map change */
437             if( p_input->stream.b_changed )
438             {
439                 GtkModeManage( p_intf );
440                 GtkSetupMenus( p_intf );
441                 p_intf->p_sys->b_playing = 1;
442             }
443
444             /* Manage the slider */
445             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
446             {
447                 float newvalue = p_intf->p_sys->p_adj->value;
448
449 #define p_area p_input->stream.p_selected_area
450                 /* If the user hasn't touched the slider since the last time,
451                  * then the input can safely change it */
452                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
453                 {
454                     /* Update the value */
455                     p_intf->p_sys->p_adj->value =
456                     p_intf->p_sys->f_adj_oldvalue =
457                         ( 100. * p_area->i_tell ) / p_area->i_size;
458
459                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
460                                              "value_changed" );
461                 }
462                 /* Otherwise, send message to the input if the user has
463                  * finished dragging the slider */
464                 else if( p_intf->p_sys->b_slider_free )
465                 {
466                     off_t i_seek = ( newvalue * p_area->i_size ) / 100;
467
468                     vlc_mutex_unlock( &p_input->stream.stream_lock );
469                     input_Seek( p_input, i_seek, INPUT_SEEK_SET );
470                     vlc_mutex_lock( &p_input->stream.stream_lock );
471
472                     /* Update the old value */
473                     p_intf->p_sys->f_adj_oldvalue = newvalue;
474                 }
475 #undef p_area
476             }
477
478             if( p_intf->p_sys->i_part !=
479                 p_input->stream.p_selected_area->i_part )
480             {
481                 p_intf->p_sys->b_chapter_update = 1;
482                 GtkSetupMenus( p_intf );
483             }
484         }
485
486         vlc_mutex_unlock( &p_input->stream.stream_lock );
487     }
488     else if( p_intf->p_sys->b_playing && !p_intf->p_vlc->b_die )
489     {
490         GtkModeManage( p_intf );
491         p_intf->p_sys->b_playing = 0;
492     }
493
494     if( p_intf->p_vlc->b_die )
495     {
496         vlc_mutex_unlock( &p_intf->change_lock );
497
498         /* Prepare to die, young Skywalker */
499         gtk_main_quit();
500
501         /* Just in case */
502         return( FALSE );
503     }
504
505     vlc_mutex_unlock( &p_intf->change_lock );
506
507     return( TRUE );
508
509 #undef p_intf
510 }
511