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