1 /*****************************************************************************
2 * gtk.c : Gtk+ plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: gtk.c,v 1.6 2002/10/14 16:46:55 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 "gtk_callbacks.h"
38 #include "gtk_interface.h"
39 #include "gtk_support.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t * );
49 static void Close ( vlc_object_t * );
51 static void Run ( intf_thread_t * );
52 static int Manage ( intf_thread_t * );
54 /*****************************************************************************
56 *****************************************************************************/
57 #define TOOLTIPS_TEXT N_("show tooltips")
58 #define TOOLTIPS_LONGTEXT N_("Show tooltips for configuration options.")
60 #define PREFS_MAXH_TEXT N_("maximum height for the configuration windows")
61 #define PREFS_MAXH_LONGTEXT N_( \
62 "You can set the maximum height that the configuration windows in the " \
63 "preferences menu will occupy.")
69 int i = getenv( "DISPLAY" ) == NULL ? 10 : 90;
71 add_category_hint( N_("Gtk+"), NULL );
72 add_bool( "gtk-tooltips", 1, E_(GtkHideTooltips),
73 TOOLTIPS_TEXT, TOOLTIPS_LONGTEXT );
74 add_integer( "gtk-prefs-maxh", 480, NULL,
75 PREFS_MAXH_TEXT, PREFS_MAXH_LONGTEXT );
77 set_description( _("Gtk+ interface module") );
78 set_capability( "interface", i );
79 set_callbacks( Open, Close );
80 set_program( "gvlc" );
83 /*****************************************************************************
84 * Open: initialize and create window
85 *****************************************************************************/
86 static int Open( vlc_object_t *p_this )
88 intf_thread_t *p_intf = (intf_thread_t *)p_this;
90 /* Allocate instance and initialize some members */
91 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
92 if( p_intf->p_sys == NULL )
94 msg_Err( p_intf, "out of memory" );
99 p_intf->p_sys->p_gtk_main = module_Need( p_this, "gtk_main", "gtk" );
100 if( p_intf->p_sys->p_gtk_main == NULL )
102 free( p_intf->p_sys );
107 p_intf->pf_run = Run;
109 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
111 /* Initialize Gtk+ thread */
112 p_intf->p_sys->b_playing = 0;
113 p_intf->p_sys->b_popup_changed = 0;
114 p_intf->p_sys->b_window_changed = 0;
115 p_intf->p_sys->b_playlist_changed = 0;
117 p_intf->p_sys->p_input = NULL;
118 p_intf->p_sys->i_playing = -1;
119 p_intf->p_sys->b_slider_free = 1;
121 p_intf->p_sys->i_part = 0;
126 /*****************************************************************************
127 * Close: destroy interface window
128 *****************************************************************************/
129 static void Close( vlc_object_t *p_this )
131 intf_thread_t *p_intf = (intf_thread_t *)p_this;
133 if( p_intf->p_sys->p_input )
135 vlc_object_release( p_intf->p_sys->p_input );
138 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
141 module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
144 /* Destroy structure */
145 free( p_intf->p_sys );
148 /*****************************************************************************
150 *****************************************************************************
151 * this part of the interface is in a separate thread so that we can call
152 * gtk_main() from within it without annoying the rest of the program.
153 *****************************************************************************/
154 static void Run( intf_thread_t *p_intf )
156 /* The data types we are allowed to receive */
157 static GtkTargetEntry target_table[] =
159 { "STRING", 0, DROP_ACCEPT_STRING },
160 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
161 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
167 /* gtk_init needs to know the command line. We don't care, so we
168 * give it an empty one */
169 char *p_args[] = { "" };
170 char **pp_args = p_args;
174 gtk_init( &i_args, &pp_args );
177 /* Create some useful widgets that will certainly be used */
178 p_intf->p_sys->p_window = create_intf_window();
179 p_intf->p_sys->p_popup = create_intf_popup();
180 p_intf->p_sys->p_playwin = create_intf_playlist();
181 p_intf->p_sys->p_messages = create_intf_messages();
182 p_intf->p_sys->p_tooltips = gtk_tooltips_new();
184 /* Set the title of the main window */
185 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
186 VOUT_TITLE " (Gtk+ interface)");
188 /* Accept file drops on the main window */
189 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
190 GTK_DEST_DEFAULT_ALL, target_table,
191 1, GDK_ACTION_COPY );
193 /* Accept file drops on the playlist window */
194 gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playwin,
196 GTK_DEST_DEFAULT_ALL, target_table,
197 1, GDK_ACTION_COPY );
199 /* Get the slider object */
200 p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
201 GTK_OBJECT( p_intf->p_sys->p_window ), "slider_frame" ) );
203 /* Configure the log window */
204 p_intf->p_sys->p_messages_text = GTK_TEXT( gtk_object_get_data(
205 GTK_OBJECT(p_intf->p_sys->p_messages ), "messages_textbox" ) );
206 gtk_text_set_line_wrap( p_intf->p_sys->p_messages_text, TRUE);
207 gtk_text_set_word_wrap( p_intf->p_sys->p_messages_text, FALSE);
209 /* Get the interface labels */
210 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
211 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
212 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
213 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
216 /* Connect the date display to the slider */
217 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
218 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
219 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
221 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
222 GTK_SIGNAL_FUNC( E_(GtkDisplayDate) ), NULL );
223 p_intf->p_sys->f_adj_oldvalue = 0;
226 /* We don't create these ones yet because we perhaps won't need them */
227 p_intf->p_sys->p_about = NULL;
228 p_intf->p_sys->p_modules = NULL;
229 p_intf->p_sys->p_open = NULL;
230 p_intf->p_sys->p_jump = NULL;
232 /* Hide tooltips if the option is set */
233 if( !config_GetInt( p_intf, "gtk-tooltips" ) )
235 gtk_tooltips_disable( p_intf->p_sys->p_tooltips );
238 /* Store p_intf to keep an eye on it */
239 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
242 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
245 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playwin ),
248 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_messages ),
251 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
254 /* Show the control window */
255 gtk_widget_show( p_intf->p_sys->p_window );
258 while( !p_intf->b_die )
262 /* Sleep to avoid using all CPU - since some interfaces need to
263 * access keyboard events, a 100ms delay is a good compromise */
265 msleep( INTF_IDLE_SLEEP );
269 /* Sleep to avoid using all CPU - since some interfaces needs to access
270 * keyboard events, a 100ms delay is a good compromise */
271 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, (GtkFunction)Manage,
275 /* Remove the timeout */
276 gtk_timeout_remove( i_dummy );
279 /* Destroy the Tooltips structure */
280 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_tooltips) );
281 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_messages) );
282 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_playwin) );
283 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_popup) );
284 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
291 /* following functions are local */
293 /*****************************************************************************
294 * Manage: manage main thread messages
295 *****************************************************************************
296 * In this function, called approx. 10 times a second, we check what the
297 * main program wanted to tell us.
298 *****************************************************************************/
299 static int Manage( intf_thread_t *p_intf )
303 vlc_mutex_lock( &p_intf->change_lock );
305 /* If the "display popup" flag has changed */
306 if( p_intf->b_menu_change )
308 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
310 p_intf->p_sys->p_popup = create_intf_popup();
311 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
314 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
315 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
316 p_intf->b_menu_change = 0;
319 /* Update the log window */
320 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
321 i_stop = *p_intf->p_sys->p_sub->pi_stop;
322 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
324 if( p_intf->p_sys->p_sub->i_start != i_stop )
326 static GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
327 static GdkColor gray = { 0, 0xaaaa, 0xaaaa, 0xaaaa };
328 static GdkColor yellow = { 0, 0xffff, 0xffff, 0x6666 };
329 static GdkColor red = { 0, 0xffff, 0x6666, 0x6666 };
331 static const char * ppsz_type[4] = { ": ", " error: ", " warning: ",
333 static GdkColor * pp_color[4] = { &white, &red, &yellow, &gray };
335 for( i_start = p_intf->p_sys->p_sub->i_start;
337 i_start = (i_start+1) % VLC_MSG_QSIZE )
339 /* Append all messages to log window */
340 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
341 NULL, p_intf->p_sys->p_sub->p_msg[i_start].psz_module, -1 );
343 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
344 NULL, ppsz_type[p_intf->p_sys->p_sub->p_msg[i_start].i_type],
347 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL,
348 pp_color[p_intf->p_sys->p_sub->p_msg[i_start].i_type], NULL,
349 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg, -1 );
351 gtk_text_insert( p_intf->p_sys->p_messages_text, NULL, &gray,
355 vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
356 p_intf->p_sys->p_sub->i_start = i_start;
357 vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
359 gtk_text_set_point( p_intf->p_sys->p_messages_text,
360 gtk_text_get_length( p_intf->p_sys->p_messages_text ) );
363 /* Update the playlist */
364 GtkPlayListManage( p_intf );
366 /* Update the input */
367 if( p_intf->p_sys->p_input == NULL )
369 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
372 else if( p_intf->p_sys->p_input->b_dead )
374 vlc_object_release( p_intf->p_sys->p_input );
375 p_intf->p_sys->p_input = NULL;
378 if( p_intf->p_sys->p_input )
380 input_thread_t *p_input = p_intf->p_sys->p_input;
382 vlc_mutex_lock( &p_input->stream.stream_lock );
384 if( !p_input->b_die )
386 /* New input or stream map change */
387 if( p_input->stream.b_changed )
389 E_(GtkModeManage)( p_intf );
390 GtkSetupMenus( p_intf );
391 p_intf->p_sys->b_playing = 1;
394 /* Manage the slider */
395 if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
397 float newvalue = p_intf->p_sys->p_adj->value;
399 #define p_area p_input->stream.p_selected_area
400 /* If the user hasn't touched the slider since the last time,
401 * then the input can safely change it */
402 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
404 /* Update the value */
405 p_intf->p_sys->p_adj->value =
406 p_intf->p_sys->f_adj_oldvalue =
407 ( 100. * p_area->i_tell ) / p_area->i_size;
409 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
412 /* Otherwise, send message to the input if the user has
413 * finished dragging the slider */
414 else if( p_intf->p_sys->b_slider_free )
416 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
418 /* release the lock to be able to seek */
419 vlc_mutex_unlock( &p_input->stream.stream_lock );
420 input_Seek( p_input, i_seek, INPUT_SEEK_SET );
421 vlc_mutex_lock( &p_input->stream.stream_lock );
423 /* Update the old value */
424 p_intf->p_sys->f_adj_oldvalue = newvalue;
429 if( p_intf->p_sys->i_part !=
430 p_input->stream.p_selected_area->i_part )
432 p_intf->p_sys->b_chapter_update = 1;
433 GtkSetupMenus( p_intf );
437 vlc_mutex_unlock( &p_input->stream.stream_lock );
439 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
441 E_(GtkModeManage)( p_intf );
442 p_intf->p_sys->b_playing = 0;
445 #ifndef NEED_GTK_MAIN
448 vlc_mutex_unlock( &p_intf->change_lock );
450 /* Prepare to die, young Skywalker */
457 vlc_mutex_unlock( &p_intf->change_lock );