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 $
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <errno.h> /* ENOMEM */
29 #include <string.h> /* strerror() */
37 #include "gnome_callbacks.h"
38 #include "gnome_interface.h"
39 #include "gnome_support.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close ( vlc_object_t * );
49 static void Run ( intf_thread_t * );
50 static void Manage ( intf_thread_t * );
52 /*****************************************************************************
54 *****************************************************************************/
55 #define TOOLTIPS_TEXT N_("show tooltips")
56 #define TOOLTIPS_LONGTEXT N_("Show tooltips for configuration options.")
58 #define TOOLBAR_TEXT N_("show text on toolbar buttons")
59 #define TOOLBAR_LONGTEXT N_("Show the text below icons on the toolbar.")
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.")
70 int i = getenv( "DISPLAY" ) == NULL ? 15 : 100;
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,
77 add_integer( "gnome-prefs-maxh", 480, NULL,
78 PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT );
80 set_description( _("GNOME interface module") );
81 set_capability( "interface", i );
82 set_callbacks( Open, Close );
83 set_program( "gnome-vlc" );
86 /*****************************************************************************
87 * Open: initialize and create window
88 *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
91 intf_thread_t *p_intf = (intf_thread_t *)p_this;
93 /* Allocate instance and initialize some members */
94 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
95 if( p_intf->p_sys == NULL )
97 msg_Err( p_intf, "out of memory" );
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 )
104 free( p_intf->p_sys );
108 p_intf->pf_run = Run;
110 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
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;
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;
122 p_intf->p_sys->i_part = 0;
127 /*****************************************************************************
128 * Close: destroy interface window
129 *****************************************************************************/
130 static void Close( vlc_object_t *p_this )
132 intf_thread_t *p_intf = (intf_thread_t *)p_this;
134 if( p_intf->p_sys->p_input )
136 vlc_object_release( p_intf->p_sys->p_input );
139 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
141 module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
143 /* Destroy structure */
144 free( p_intf->p_sys );
147 /*****************************************************************************
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 )
157 /* The data types we are allowed to receive */
158 static GtkTargetEntry target_table[] =
160 { "STRING", 0, DROP_ACCEPT_STRING },
161 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
162 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
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();
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)");
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 );
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" );
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);
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" );
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 );
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;
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;
221 /* Hide tooltips if the option is set */
222 if( !config_GetInt( p_intf, "gnome-tooltips" ) )
224 gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
227 /* Hide toolbar text of the option is set */
228 if( !config_GetInt( p_intf, "gnome-toolbartext" ) )
230 gtk_toolbar_set_style(
231 GTK_TOOLBAR(lookup_widget( p_intf->p_sys->p_window, "toolbar" )),
235 /* Store p_intf to keep an eye on it */
236 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
239 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
242 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
245 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
248 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
251 /* Show the control window */
252 gtk_widget_show( p_intf->p_sys->p_window );
254 while( !p_intf->b_die )
258 /* Sleep to avoid using all CPU - since some interfaces need to
259 * access keyboard events, a 100ms delay is a good compromise */
261 msleep( INTF_IDLE_SLEEP );
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) );
275 /* following functions are local */
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 )
287 vlc_mutex_lock( &p_intf->change_lock );
289 /* If the "display popup" flag has changed */
290 if( p_intf->b_menu_change )
292 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
294 p_intf->p_sys->p_popup = create_intf_popup();
295 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
299 gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
300 NULL, NULL, NULL, NULL );
301 p_intf->b_menu_change = 0;
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 );
309 if( p_intf->p_sys->p_sub->i_start != i_stop )
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 };
316 static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
318 static GdkColor * pp_color[4] = { &white, &red, &yellow, &gray };
320 for( i_start = p_intf->p_sys->p_sub->i_start;
322 i_start = (i_start+1) % VLC_MSG_QSIZE )
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 );
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],
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 );
336 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
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 );
344 gtk_text_set_point( p_intf->p_sys->p_messages_text,
345 gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
348 /* Update the playlist */
349 GtkPlayListManage( p_intf );
351 /* Update the input */
352 if( p_intf->p_sys->p_input != NULL )
354 if( p_intf->p_sys->p_input->b_dead )
356 vlc_object_release( p_intf->p_sys->p_input );
357 p_intf->p_sys->p_input = NULL;
361 if( p_intf->p_sys->p_input == NULL )
363 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
367 if( p_intf->p_sys->p_input )
369 input_thread_t *p_input = p_intf->p_sys->p_input;
371 vlc_mutex_lock( &p_input->stream.stream_lock );
373 if( !p_input->b_die )
375 /* New input or stream map change */
376 if( p_input->stream.b_changed )
378 GtkModeManage( p_intf );
379 GtkSetupMenus( p_intf );
380 p_intf->p_sys->b_playing = 1;
383 /* Manage the slider */
384 if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
386 float newvalue = p_intf->p_sys->p_adj->value;
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 )
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;
398 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
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 )
405 if( newvalue > 0. && newvalue < 100. )
407 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
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 );
414 /* Update the old value */
415 p_intf->p_sys->f_adj_oldvalue = newvalue;
420 if( p_intf->p_sys->i_part !=
421 p_input->stream.p_selected_area->i_part )
423 p_intf->p_sys->b_chapter_update = 1;
424 GtkSetupMenus( p_intf );
428 vlc_mutex_unlock( &p_input->stream.stream_lock );
430 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
432 GtkModeManage( p_intf );
433 p_intf->p_sys->b_playing = 0;
436 vlc_mutex_unlock( &p_intf->change_lock );