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