]> git.sesse.net Git - vlc/blob - modules/gui/gtk/gnome.c
e3686c62ec147cb8943f428f63fc7b5fe0f850d7
[vlc] / modules / gui / gtk / gnome.c
1 /*****************************************************************************
2  * gnome.c : Gnome plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id$
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 "display.h"
41 #include "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 void Manage       ( intf_thread_t * );
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 #define TOOLTIPS_TEXT N_("Show tooltips")
56 #define TOOLTIPS_LONGTEXT N_("Show tooltips for configuration options.")
57
58 #define TOOLBAR_TEXT N_("Show text on toolbar buttons")
59 #define TOOLBAR_LONGTEXT N_("Show the text below icons on the toolbar.")
60
61 #define PREFS_MAXH_TEXT N_("Maximum height for the configuration windows")
62 #define PREFS_MAXH_LONGTEXT N_( \
63     "You can set the maximum height that the configuration windows in the " \
64     "preferences menu will occupy.")
65
66 #define PATH_TEXT N_("Interface default search path")
67 #define PATH_LONGTEXT N_( \
68     "This option allows you to set the default path that the interface will " \
69     "open when looking for a file.")
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     set_description( _("GNOME interface") );
78
79     add_bool( "gnome-tooltips", 1, E_(GtkHideTooltips),
80               TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT, VLC_FALSE );
81     add_bool( "gnome-toolbartext", 1, GtkHideToolbarText, TOOLBAR_TEXT,
82               TOOLBAR_LONGTEXT, VLC_FALSE );
83     add_integer( "gnome-prefs-maxh", 480, NULL,
84                  PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT, VLC_TRUE );
85     add_directory( "gnome-search-path", NULL, NULL, PATH_TEXT,
86                    PATH_LONGTEXT, VLC_TRUE );
87
88     set_capability( "interface", i );
89     set_callbacks( Open, Close );
90     set_program( "gnome-vlc" );
91 vlc_module_end();
92
93 /*****************************************************************************
94  * Open: initialize and create window
95  *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
97 {
98     intf_thread_t *p_intf = (intf_thread_t *)p_this;
99
100     /* Allocate instance and initialize some members */
101     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
102     if( p_intf->p_sys == NULL )
103     {
104         msg_Err( p_intf, "out of memory" );
105         return VLC_ENOMEM;
106     }
107
108     p_intf->p_sys->p_gtk_main =
109         module_Need( p_this, "gui-helper", "gnome", VLC_TRUE );
110     if( p_intf->p_sys->p_gtk_main == NULL )
111     {
112         free( p_intf->p_sys );
113         return VLC_ENOMOD;
114     }
115
116     p_intf->pf_run = Run;
117
118     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
119
120     /* Initialize Gnome thread */
121     p_intf->p_sys->b_playing = VLC_FALSE;
122     p_intf->p_sys->b_deinterlace_update = VLC_FALSE;
123
124     p_intf->p_sys->b_aout_update = VLC_FALSE;
125     p_intf->p_sys->b_vout_update = VLC_FALSE;
126
127     p_intf->p_sys->b_popup_changed = VLC_FALSE;
128     p_intf->p_sys->b_window_changed = VLC_FALSE;
129     p_intf->p_sys->b_playlist_changed = VLC_FALSE;
130     p_intf->p_sys->b_program_update = VLC_FALSE;
131     p_intf->p_sys->b_title_update = VLC_FALSE;
132     p_intf->p_sys->b_chapter_update = VLC_FALSE;
133     p_intf->p_sys->b_spu_update = VLC_FALSE;
134     p_intf->p_sys->b_audio_update = VLC_FALSE;
135
136     p_intf->p_sys->p_input = NULL;
137     p_intf->p_sys->i_playing = -1;
138     p_intf->p_sys->b_slider_free = VLC_TRUE;
139
140     p_intf->p_sys->i_part = 0;
141     p_intf->p_sys->b_mute = VLC_FALSE;
142
143     return VLC_SUCCESS;
144 }
145
146 /*****************************************************************************
147  * Close: destroy interface window
148  *****************************************************************************/
149 static void Close( vlc_object_t *p_this )
150 {
151     intf_thread_t *p_intf = (intf_thread_t *)p_this;
152
153     if( p_intf->p_sys->p_input )
154     {
155         vlc_object_release( p_intf->p_sys->p_input );
156     }
157
158     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
159
160     module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
161
162     /* Destroy structure */
163     free( p_intf->p_sys );
164 }
165
166 /*****************************************************************************
167  * Run: Gnome thread
168  *****************************************************************************
169  * this part of the interface is in a separate thread so that we can call
170  * gtk_main() from within it without annoying the rest of the program.
171  * XXX: the approach may look kludgy, and probably is, but I could not find
172  * a better way to dynamically load a Gnome interface at runtime.
173  *****************************************************************************/
174 static void Run( intf_thread_t *p_intf )
175 {
176     /* The data types we are allowed to receive */
177     static GtkTargetEntry target_table[] =
178     {
179         { "STRING", 0, DROP_ACCEPT_STRING },
180         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
181         { "text/plain",    0, DROP_ACCEPT_TEXT_PLAIN }
182     };
183     char *psz_sout;
184     GString *       p_target;
185
186     gdk_threads_enter();
187
188     /* Create some useful widgets that will certainly be used */
189     p_intf->p_sys->p_window = create_intf_window( );
190     p_intf->p_sys->p_popup = create_intf_popup( );
191     p_intf->p_sys->p_playwin = create_intf_playlist();
192     p_intf->p_sys->p_messages = create_intf_messages();
193     p_intf->p_sys->p_tooltips = gtk_tooltips_new();
194     p_intf->p_sys->p_sout = create_intf_sout();
195
196     /* Set the title of the main window */
197     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
198                           VOUT_TITLE " (Gnome interface)");
199
200     /* Accept file drops on the main window */
201     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
202                        GTK_DEST_DEFAULT_ALL, target_table,
203                        DROP_ACCEPT_END, GDK_ACTION_COPY );
204     /* Accept file drops on the playlist window */
205     gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
206                             p_intf->p_sys->p_playwin ), "playlist_clist") ),
207                        GTK_DEST_DEFAULT_ALL, target_table,
208                        DROP_ACCEPT_END, GDK_ACTION_COPY );
209
210     /* Get the slider object */
211     p_intf->p_sys->p_slider_frame = gtk_object_get_data(
212                       GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
213
214     /* Configure the log window */
215     p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
216         GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
217     gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
218     gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
219
220     /* Get the interface labels */
221     #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
222                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
223     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
224     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
225     #undef P_LABEL
226
227     /* Connect the date display to the slider */
228     #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
229                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
230     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
231
232     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
233                          GTK_SIGNAL_FUNC( E_(GtkDisplayDate) ), NULL );
234     p_intf->p_sys->f_adj_oldvalue = 0;
235     #undef P_SLIDER
236
237     /* We don't create these ones yet because we perhaps won't need them */
238     p_intf->p_sys->p_about = NULL;
239     p_intf->p_sys->p_modules = NULL;
240     p_intf->p_sys->p_open = NULL;
241     p_intf->p_sys->p_jump = NULL;
242
243     /* Hide tooltips if the option is set */
244     if( !config_GetInt( p_intf, "gnome-tooltips" ) )
245     {
246         gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
247     }
248
249     /* Hide toolbar text of the option is set */
250     if( !config_GetInt( p_intf, "gnome-toolbartext" ) )
251     {
252         gtk_toolbar_set_style(
253             GTK_TOOLBAR(lookup_widget( p_intf->p_sys->p_window, "toolbar" )),
254             GTK_TOOLBAR_ICONS );
255     }
256
257     /* Store p_intf to keep an eye on it */
258     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
259                          "p_intf", p_intf );
260
261     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
262                          "p_intf", p_intf );
263     gtk_object_set_data( GTK_OBJECT( gtk_object_get_data(
264                              GTK_OBJECT(p_intf->p_sys->p_popup),
265                              "popup_audio" ) ), "p_intf", p_intf );
266     gtk_object_set_data( GTK_OBJECT( gtk_object_get_data(
267                              GTK_OBJECT(p_intf->p_sys->p_popup),
268                              "popup_video" ) ), "p_intf", p_intf );
269
270     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
271                          "p_intf", p_intf );
272
273     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
274                          "p_intf", p_intf );
275
276     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
277                          "p_intf", p_intf );
278
279     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
280                                      "p_intf", p_intf );
281
282     psz_sout = config_GetPsz( p_intf, "sout" );
283     p_target = g_string_new( psz_sout ? psz_sout : "" );
284     if( psz_sout ) free( psz_sout );
285
286     gtk_entry_set_text( gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ), "sout_entry_target" ), p_target->str );
287     g_string_free( p_target, TRUE );
288
289     /* FIXME it's to be sure that only file entry is selected */
290     gtk_toggle_button_set_active(  gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
291                                    "sout_access_udp" ), TRUE );
292
293     gtk_toggle_button_set_active(  gtk_object_get_data( GTK_OBJECT( p_intf->p_sys->p_sout ),
294                                    "sout_access_file" ), TRUE );
295
296     /* Show the control window */
297     gtk_widget_show( p_intf->p_sys->p_window );
298
299     while( !p_intf->b_die )
300     {
301         Manage( p_intf );
302
303         /* Sleep to avoid using all CPU - since some interfaces need to
304          * access keyboard events, a 100ms delay is a good compromise */
305         gdk_threads_leave();
306         msleep( INTF_IDLE_SLEEP );
307         gdk_threads_enter();
308     }
309
310     /* Destroy the Tooltips structure */
311     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
312     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_messages) );
313     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_playwin) );
314     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_popup) );
315     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
316
317     gdk_threads_leave();
318 }
319
320 /* following functions are local */
321
322 /*****************************************************************************
323  * Manage: manage main thread messages
324  *****************************************************************************
325  * In this function, called approx. 10 times a second, we check what the
326  * main program wanted to tell us.
327  *****************************************************************************/
328 static void Manage( intf_thread_t *p_intf )
329 {
330     int i_start, i_stop;
331
332     vlc_mutex_lock( &p_intf->change_lock );
333
334     /* If the "display popup" flag has changed */
335     if( p_intf->b_menu_change )
336     {
337         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
338         {
339             p_intf->p_sys->p_popup = create_intf_popup();
340             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
341                                  "p_popup", p_intf );
342         }
343
344         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
345                                    NULL, NULL, NULL, NULL );
346         p_intf->b_menu_change = 0;
347     }
348
349     /* Update the log window */
350     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
351     i_stop = *p_intf->p_sys->p_sub->pi_stop;
352     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
353
354     if( p_intf->p_sys->p_sub->i_start != i_stop )
355     {
356         static GdkColor white  = { 0, 0xffff, 0xffff, 0xffff };
357         static GdkColor gray   = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
358         static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
359         static GdkColor red    = { 0, 0xffff, 0x6666, 0x6666 };
360
361         static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
362                                              " debug: " };
363         static GdkColor *   pp_color[4] = { &white, &red, &yellow, &gray };
364
365         for( i_start = p_intf->p_sys->p_sub->i_start;
366              i_start != i_stop;
367              i_start = (i_start+1) % VLC_MSG_QSIZE )
368         {
369             /* Append all messages to log window */
370             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
371              NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
372
373             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
374                 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
375                 -1 );
376
377             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
378                 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
379                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
380
381             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
382                 NULL, "\n", -1 );
383         }
384
385         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
386         p_intf->p_sys->p_sub->i_start = i_start;
387         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
388
389         /* If the messages list becomes too big, just clean half of it. */
390         if( gtk_text_get_length( p_intf->p_sys->p_messages_text ) >
391             VLC_MSG_QSIZE * 1000 )
392         {
393             gtk_text_set_point( p_intf->p_sys->p_messages_text, 0 );
394             gtk_text_forward_delete( p_intf->p_sys->p_messages_text,
395                 gtk_text_get_length( p_intf->p_sys->p_messages_text ) / 2 );
396         }
397
398         gtk_text_set_point( p_intf->p_sys->p_messages_text,
399                     gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
400     }
401
402     /* Update the playlist */
403     GtkPlayListManage( p_intf );
404
405     /* Update the input */
406     if( p_intf->p_sys->p_input != NULL )
407     {
408         if( p_intf->p_sys->p_input->b_dead )
409         {
410             vlc_object_release( p_intf->p_sys->p_input );
411             p_intf->p_sys->p_input = NULL;
412         }
413     }
414
415     if( p_intf->p_sys->p_input == NULL )
416     {
417         p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
418                                                           FIND_ANYWHERE );
419     }
420
421     if( p_intf->p_sys->p_input )
422     {
423         input_thread_t *p_input = p_intf->p_sys->p_input;
424         aout_instance_t *p_aout  = NULL;
425         vout_thread_t   *p_vout  = NULL;
426         vlc_bool_t      b_need_menus = VLC_FALSE;
427
428         vlc_mutex_lock( &p_input->stream.stream_lock );
429
430         if( !p_input->b_die )
431         {
432             /* New input or stream map change */
433             if( p_input->stream.b_changed )
434             {
435                 E_(GtkModeManage)( p_intf );
436                 GtkSetupMenus( p_intf );
437                 p_intf->p_sys->b_playing = 1;
438             }
439
440             /* Manage the slider */
441             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
442             {
443                 float newvalue = p_intf->p_sys->p_adj->value;
444
445 #define p_area p_input->stream.p_selected_area
446                 /* If the user hasn't touched the slider since the last time,
447                  * then the input can safely change it */
448                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
449                 {
450                     /* Update the value */
451                     p_intf->p_sys->p_adj->value =
452                     p_intf->p_sys->f_adj_oldvalue =
453                         ( 100. * p_area->i_tell ) / p_area->i_size;
454
455                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
456                                              "value_changed" );
457                 }
458                 /* Otherwise, send message to the input if the user has
459                  * finished dragging the slider.
460                  * Beware, the hack below is needed by the dvdplay plugin! */
461                 else if( p_intf->p_sys->b_slider_free
462                 /* hack -> */ && (p_intf->p_sys->f_adj_oldvalue < 100.) )
463                 {
464                     if( newvalue >= 0. && newvalue < 100. )
465                     {
466                         double f_fpos = (double)newvalue / 100.0;
467
468                         /* release the lock to be able to seek */
469                         vlc_mutex_unlock( &p_input->stream.stream_lock );
470                         var_SetFloat( p_input, "position", f_fpos );
471                         vlc_mutex_lock( &p_input->stream.stream_lock );
472
473                     }
474
475                     /* Update the old value */
476                     p_intf->p_sys->f_adj_oldvalue = newvalue;
477                 }
478 #undef p_area
479             }
480
481             if( p_intf->p_sys->i_part !=
482                 p_input->stream.p_selected_area->i_part )
483             {
484                 p_intf->p_sys->b_chapter_update = 1;
485                 b_need_menus = VLC_TRUE;
486             }
487
488             /* Does the audio output require to update the menus ? */
489             p_aout = (aout_instance_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
490                                                          FIND_ANYWHERE );
491             if( p_aout != NULL )
492             {
493                 vlc_value_t val;
494                 if( var_Get( (vlc_object_t *)p_aout, "intf-change", &val ) >= 0
495                     && val.b_bool )
496                 {
497                     p_intf->p_sys->b_aout_update = 1;
498                     b_need_menus = 1;
499                 }
500
501                 vlc_object_release( (vlc_object_t *)p_aout );
502             }
503
504
505             /* Does the video output require to update the menus ? */
506             p_vout = (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
507                                                        FIND_ANYWHERE );
508             if( p_vout != NULL ) 
509             {
510                 vlc_value_t val;
511                 if( var_Get( (vlc_object_t *)p_vout, "intf-change", &val ) >= 0
512                     && val.b_bool )
513                 {
514                     p_intf->p_sys->b_vout_update = 1;
515                     b_need_menus = 1;
516                 }
517
518                 vlc_object_release( (vlc_object_t *)p_vout );
519             }
520
521             if( b_need_menus )
522             {
523                 GtkSetupMenus( p_intf );
524             }
525         }
526
527         vlc_mutex_unlock( &p_input->stream.stream_lock );
528     }
529     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
530     {
531         E_(GtkModeManage)( p_intf );
532         p_intf->p_sys->b_playing = 0;
533     }
534
535     vlc_mutex_unlock( &p_intf->change_lock );
536
537     return;
538 }
539