]> git.sesse.net Git - vlc/blob - modules/gui/gtk/gnome.c
e84a8c5d79629e6324f1878dbe6b7cd1857239d4
[vlc] / modules / gui / gtk / gnome.c
1 /*****************************************************************************
2  * gnome.c : Gnome plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id: gnome.c,v 1.2 2002/08/20 18:08: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 "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 vlc_module_begin();
67 #ifdef WIN32
68     int i = 90;
69 #else
70     int i = getenv( "DISPLAY" ) == NULL ? 15 : 100;
71 #endif
72     add_category_hint( N_("GNOME"), NULL );
73     add_bool( "gnome-tooltips", 1, GtkHideTooltips,
74               TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT );
75     add_bool( "gnome-toolbartext", 1, GtkHideToolbarText, TOOLBAR_TEXT,
76               TOOLBAR_LONGTEXT );
77     add_integer( "gnome-prefs-maxh", 480, NULL,
78                  PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT );
79     
80     set_description( _("GNOME interface module") );
81     set_capability( "interface", i );
82     set_callbacks( Open, Close );
83     set_program( "gnome-vlc" );
84 vlc_module_end();
85
86 /*****************************************************************************
87  * Open: initialize and create window
88  *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
90 {
91     intf_thread_t *p_intf = (intf_thread_t *)p_this;
92
93     /* Allocate instance and initialize some members */
94     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
95     if( p_intf->p_sys == NULL )
96     {
97         msg_Err( p_intf, "out of memory" );
98         return VLC_ENOMEM;
99     }
100
101     p_intf->p_sys->p_gtk_main = module_Need( p_this, "gtk_main", "gnome" );
102     if( p_intf->p_sys->p_gtk_main == NULL )
103     {
104         free( p_intf->p_sys );
105         return VLC_EMODULE;
106     }
107
108     p_intf->pf_run = Run;
109
110     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
111
112     /* Initialize Gnome thread */
113     p_intf->p_sys->b_playing = 0;
114     p_intf->p_sys->b_popup_changed = 0;
115     p_intf->p_sys->b_window_changed = 0;
116     p_intf->p_sys->b_playlist_changed = 0;
117
118     p_intf->p_sys->p_input = NULL;
119     p_intf->p_sys->i_playing = -1;
120     p_intf->p_sys->b_slider_free = 1;
121
122     p_intf->p_sys->i_part = 0;
123
124     return VLC_SUCCESS;
125 }
126
127 /*****************************************************************************
128  * Close: destroy interface window
129  *****************************************************************************/
130 static void Close( vlc_object_t *p_this )
131 {
132     intf_thread_t *p_intf = (intf_thread_t *)p_this;
133
134     if( p_intf->p_sys->p_input )
135     {
136         vlc_object_release( p_intf->p_sys->p_input );
137     }
138
139     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
140
141     module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
142
143     /* Destroy structure */
144     free( p_intf->p_sys );
145 }
146
147 /*****************************************************************************
148  * Run: Gnome thread
149  *****************************************************************************
150  * this part of the interface is in a separate thread so that we can call
151  * gtk_main() from within it without annoying the rest of the program.
152  * XXX: the approach may look kludgy, and probably is, but I could not find
153  * a better way to dynamically load a Gnome interface at runtime.
154  *****************************************************************************/
155 static void Run( intf_thread_t *p_intf )
156 {
157     /* The data types we are allowed to receive */
158     static GtkTargetEntry target_table[] =
159     {
160         { "STRING", 0, DROP_ACCEPT_STRING },
161         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
162         { "text/plain",    0, DROP_ACCEPT_TEXT_PLAIN }
163     };
164
165     gdk_threads_enter();
166
167     /* Create some useful widgets that will certainly be used */
168     p_intf->p_sys->p_window = create_intf_window( );
169     p_intf->p_sys->p_popup = create_intf_popup( );
170     p_intf->p_sys->p_playwin = create_intf_playlist();
171     p_intf->p_sys->p_messages = create_intf_messages();
172     p_intf->p_sys->p_tooltips = gtk_tooltips_new();
173
174     /* Set the title of the main window */
175     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
176                           VOUT_TITLE " (Gnome interface)");
177
178     /* Accept file drops on the main window */
179     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
180                        GTK_DEST_DEFAULT_ALL, target_table,
181                        1, GDK_ACTION_COPY );
182     /* Accept file drops on the playlist window */
183     gtk_drag_dest_set( GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
184                             p_intf->p_sys->p_playwin ), "playlist_clist") ),
185                        GTK_DEST_DEFAULT_ALL, target_table,
186                        1, GDK_ACTION_COPY );
187
188     /* Get the slider object */
189     p_intf->p_sys->p_slider_frame = gtk_object_get_data(
190                       GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" );
191
192     /* Configure the log window */
193     p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
194         GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
195     gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
196     gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
197
198     /* Get the interface labels */
199     #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
200                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
201     p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
202     p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
203     #undef P_LABEL
204
205     /* Connect the date display to the slider */
206     #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
207                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
208     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
209
210     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
211                          GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
212     p_intf->p_sys->f_adj_oldvalue = 0;
213     #undef P_SLIDER
214
215     /* We don't create these ones yet because we perhaps won't need them */
216     p_intf->p_sys->p_about = NULL;
217     p_intf->p_sys->p_modules = NULL;
218     p_intf->p_sys->p_open = NULL;
219     p_intf->p_sys->p_jump = NULL;
220
221     /* Hide tooltips if the option is set */
222     if( !config_GetInt( p_intf, "gnome-tooltips" ) )
223     {
224         gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
225     }
226
227     /* Hide toolbar text of the option is set */
228     if( !config_GetInt( p_intf, "gnome-toolbartext" ) )
229     {
230         gtk_toolbar_set_style(
231             GTK_TOOLBAR(lookup_widget( p_intf->p_sys->p_window, "toolbar" )),
232             GTK_TOOLBAR_ICONS );
233     }
234
235     /* Store p_intf to keep an eye on it */
236     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
237                          "p_intf", p_intf );
238
239     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
240                          "p_intf", p_intf );
241
242     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
243                          "p_intf", p_intf );
244
245     gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
246                          "p_intf", p_intf );
247
248     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
249                          "p_intf", p_intf );
250
251     /* Show the control window */
252     gtk_widget_show( p_intf->p_sys->p_window );
253
254     while( !p_intf->b_die )
255     {
256         Manage( p_intf );
257
258         /* Sleep to avoid using all CPU - since some interfaces need to
259          * access keyboard events, a 100ms delay is a good compromise */
260         gdk_threads_leave();
261         msleep( INTF_IDLE_SLEEP );
262         gdk_threads_enter();
263     }
264
265     /* Destroy the Tooltips structure */
266     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
267     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_messages) );
268     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_playwin) );
269     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_popup) );
270     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
271
272     gdk_threads_leave();
273 }
274
275 /* following functions are local */
276
277 /*****************************************************************************
278  * Manage: manage main thread messages
279  *****************************************************************************
280  * In this function, called approx. 10 times a second, we check what the
281  * main program wanted to tell us.
282  *****************************************************************************/
283 static void Manage( intf_thread_t *p_intf )
284 {
285     int i_start, i_stop;
286
287     vlc_mutex_lock( &p_intf->change_lock );
288
289     /* If the "display popup" flag has changed */
290     if( p_intf->b_menu_change )
291     {
292         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
293         {
294             p_intf->p_sys->p_popup = create_intf_popup();
295             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
296                                  "p_popup", p_intf );
297         }
298
299         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
300                                    NULL, NULL, NULL, NULL );
301         p_intf->b_menu_change = 0;
302     }
303
304     /* Update the log window */
305     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
306     i_stop = *p_intf->p_sys->p_sub->pi_stop;
307     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
308
309     if( p_intf->p_sys->p_sub->i_start != i_stop )
310     {
311         static GdkColor white  = { 0, 0xffff, 0xffff, 0xffff };
312         static GdkColor gray   = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
313         static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
314         static GdkColor red    = { 0, 0xffff, 0x6666, 0x6666 };
315
316         static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
317                                              " debug: " };
318         static GdkColor *   pp_color[4] = { &white, &red, &yellow, &gray };
319
320         for( i_start = p_intf->p_sys->p_sub->i_start;
321              i_start != i_stop;
322              i_start = (i_start+1) % VLC_MSG_QSIZE )
323         {
324             /* Append all messages to log window */
325             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
326              NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
327
328             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
329                 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
330                 -1 );
331
332             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
333                 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
334                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
335
336             gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
337                 NULL, "\n", -1 );
338         }
339
340         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
341         p_intf->p_sys->p_sub->i_start = i_start;
342         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
343
344         gtk_text_set_point( p_intf->p_sys->p_messages_text,
345                     gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
346     }
347
348     /* Update the playlist */
349     GtkPlayListManage( p_intf );
350
351     /* Update the input */
352     if( p_intf->p_sys->p_input != NULL )
353     {
354         if( p_intf->p_sys->p_input->b_dead )
355         {
356             vlc_object_release( p_intf->p_sys->p_input );
357             p_intf->p_sys->p_input = NULL;
358         }
359     }
360     
361     if( p_intf->p_sys->p_input == NULL )
362     {
363         p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
364                                                           FIND_ANYWHERE );
365     }
366
367     if( p_intf->p_sys->p_input )
368     {
369         input_thread_t *p_input = p_intf->p_sys->p_input;
370
371         vlc_mutex_lock( &p_input->stream.stream_lock );
372
373         if( !p_input->b_die )
374         {
375             /* New input or stream map change */
376             if( p_input->stream.b_changed )
377             {
378                 GtkModeManage( p_intf );
379                 GtkSetupMenus( p_intf );
380                 p_intf->p_sys->b_playing = 1;
381             }
382
383             /* Manage the slider */
384             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
385             {
386                 float newvalue = p_intf->p_sys->p_adj->value;
387
388 #define p_area p_input->stream.p_selected_area
389                 /* If the user hasn't touched the slider since the last time,
390                  * then the input can safely change it */
391                 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
392                 {
393                     /* Update the value */
394                     p_intf->p_sys->p_adj->value =
395                     p_intf->p_sys->f_adj_oldvalue =
396                         ( 100. * p_area->i_tell ) / p_area->i_size;
397
398                     gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
399                                              "value_changed" );
400                 }
401                 /* Otherwise, send message to the input if the user has
402                  * finished dragging the slider */
403                 else if( p_intf->p_sys->b_slider_free )
404                 {
405                     if( newvalue > 0. && newvalue < 100. )
406                     {
407                         off_t i_seek = ( newvalue * p_area->i_size ) / 100;
408
409                         vlc_mutex_unlock( &p_input->stream.stream_lock );
410                         input_Seek( p_input, i_seek, INPUT_SEEK_SET );
411                         vlc_mutex_lock( &p_input->stream.stream_lock );
412                     }
413
414                     /* Update the old value */
415                     p_intf->p_sys->f_adj_oldvalue = newvalue;
416                 }
417 #undef p_area
418             }
419
420             if( p_intf->p_sys->i_part !=
421                 p_input->stream.p_selected_area->i_part )
422             {
423                 p_intf->p_sys->b_chapter_update = 1;
424                 GtkSetupMenus( p_intf );
425             }
426         }
427
428         vlc_mutex_unlock( &p_input->stream.stream_lock );
429     }
430     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
431     {
432         GtkModeManage( p_intf );
433         p_intf->p_sys->b_playing = 0;
434     }
435
436     vlc_mutex_unlock( &p_intf->change_lock );
437
438     return;
439 }
440