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