* Win32 plugin support by Gildas Bazin <gbazin@netcourrier.com>.
[vlc.git] / plugins / gtk / intf_gtk.c
blob79aeade9a6c27028d67d35c186160e980c46d7ac
1 /*****************************************************************************
2 * intf_gtk.c: Gtk+ interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: intf_gtk.c,v 1.25 2001/05/31 12:45:39 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Stéphane Borel <stef@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #define MODULE_NAME gtk
26 #include "modules_inner.h"
28 /*****************************************************************************
29 * Preamble
30 *****************************************************************************/
31 #include "defs.h"
33 #include <errno.h> /* ENOMEM */
34 #include <stdlib.h> /* free() */
35 #include <string.h> /* strerror() */
36 #include <stdio.h>
38 #include <gtk/gtk.h>
40 #include "config.h"
41 #include "common.h"
42 #include "threads.h"
43 #include "mtime.h"
44 #include "tests.h"
46 #include "stream_control.h"
47 #include "input_ext-intf.h"
49 #include "interface.h"
50 #include "intf_msg.h"
51 #include "intf_playlist.h"
53 #include "video.h"
54 #include "video_output.h"
56 #include "gtk_callbacks.h"
57 #include "gtk_interface.h"
58 #include "gtk_support.h"
59 #include "gtk_menu.h"
60 #include "gtk_display.h"
61 #include "intf_gtk.h"
63 #include "main.h"
65 #include "modules.h"
66 #include "modules_export.h"
68 /*****************************************************************************
69 * Local prototypes.
70 *****************************************************************************/
71 static int intf_Probe ( probedata_t *p_data );
72 static int intf_Open ( intf_thread_t *p_intf );
73 static void intf_Close ( intf_thread_t *p_intf );
74 static void intf_Run ( intf_thread_t *p_intf );
76 static gint GtkManage ( gpointer p_data );
78 /*****************************************************************************
79 * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
80 *****************************************************************************
81 * gtk_init() makes several calls to g_atexit() which calls atexit() to
82 * register tidying callbacks to be called at program exit. Since the Gtk+
83 * plugin is likely to be unloaded at program exit, we have to export this
84 * symbol to intercept the g_atexit() calls. Talk about crude hack.
85 *****************************************************************************/
86 void g_atexit( GVoidFunc func )
88 intf_thread_t *p_intf = p_main->p_intf;
90 if( p_intf->p_sys->pf_gdk_callback == NULL )
92 p_intf->p_sys->pf_gdk_callback = func;
94 else if( p_intf->p_sys->pf_gtk_callback == NULL )
96 p_intf->p_sys->pf_gtk_callback = func;
98 /* else nothing, but we could do something here */
99 return;
102 /*****************************************************************************
103 * Functions exported as capabilities. They are declared as static so that
104 * we don't pollute the namespace too much.
105 *****************************************************************************/
106 void _M( intf_getfunctions )( function_list_t * p_function_list )
108 p_function_list->pf_probe = intf_Probe;
109 p_function_list->functions.intf.pf_open = intf_Open;
110 p_function_list->functions.intf.pf_close = intf_Close;
111 p_function_list->functions.intf.pf_run = intf_Run;
114 /*****************************************************************************
115 * intf_Probe: probe the interface and return a score
116 *****************************************************************************
117 * This function tries to initialize Gtk+ and returns a score to the
118 * plugin manager so that it can select the best plugin.
119 *****************************************************************************/
120 static int intf_Probe( probedata_t *p_data )
122 if( TestMethod( INTF_METHOD_VAR, "gtk" ) )
124 return( 999 );
127 if( TestProgram( "gvlc" ) )
129 return( 190 );
132 return( 90 );
135 /*****************************************************************************
136 * intf_Open: initialize and create window
137 *****************************************************************************/
138 static int intf_Open( intf_thread_t *p_intf )
140 /* Allocate instance and initialize some members */
141 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
142 if( p_intf->p_sys == NULL )
144 intf_ErrMsg("error: %s", strerror(ENOMEM));
145 return( 1 );
148 /* Initialize Gtk+ thread */
149 p_intf->p_sys->b_popup_changed = 0;
150 p_intf->p_sys->b_window_changed = 0;
151 p_intf->p_sys->b_playlist_changed = 0;
153 p_intf->p_sys->i_playing = -1;
154 p_intf->p_sys->b_slider_free = 1;
156 p_intf->p_sys->pf_gtk_callback = NULL;
157 p_intf->p_sys->pf_gdk_callback = NULL;
159 return( 0 );
162 /*****************************************************************************
163 * intf_Close: destroy interface window
164 *****************************************************************************/
165 static void intf_Close( intf_thread_t *p_intf )
167 /* Destroy structure */
168 free( p_intf->p_sys );
171 /*****************************************************************************
172 * intf_Run: Gtk+ thread
173 *****************************************************************************
174 * this part of the interface is in a separate thread so that we can call
175 * gtk_main() from within it without annoying the rest of the program.
176 * XXX: the approach may look kludgy, and probably is, but I could not find
177 * a better way to dynamically load a Gtk+ interface at runtime.
178 *****************************************************************************/
179 static void intf_Run( intf_thread_t *p_intf )
181 /* gtk_init needs to know the command line. We don't care, so we
182 * give it an empty one */
183 char *p_args[] = { "" };
184 char **pp_args = p_args;
185 int i_args = 1;
187 /* The data types we are allowed to receive */
188 static GtkTargetEntry target_table[] =
190 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
191 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
194 /* intf_Manage callback timeout */
195 int i_timeout;
197 /* Initialize Gtk+ */
198 gtk_init( &i_args, &pp_args );
200 /* Create some useful widgets that will certainly be used */
201 p_intf->p_sys->p_window = create_intf_window( );
202 p_intf->p_sys->p_popup = create_intf_popup( );
203 p_intf->p_sys->p_playlist = create_intf_playlist();
205 /* Set the title of the main window */
206 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
207 VOUT_TITLE " (Gtk+ interface)");
209 /* Accept file drops on the main window */
210 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
211 GTK_DEST_DEFAULT_ALL, target_table,
212 1, GDK_ACTION_COPY );
214 /* Accept file drops on the playlist window */
215 gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playlist,
216 "playlist_clist") ),
217 GTK_DEST_DEFAULT_ALL, target_table,
218 1, GDK_ACTION_COPY );
220 /* Get the interface labels */
221 p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
222 GTK_OBJECT(p_intf->p_sys->p_window ), "slider_frame" ) );
224 #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
225 GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
226 p_intf->p_sys->p_label_title = P_LABEL( "title_label" );
227 p_intf->p_sys->p_label_chapter = P_LABEL( "chapter_label" );
228 #undef P_LABEL
230 /* Connect the date display to the slider */
231 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
232 GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
233 p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
235 gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
236 GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
237 p_intf->p_sys->f_adj_oldvalue = 0;
238 #undef P_SLIDER
240 /* We don't create these ones yet because we perhaps won't need them */
241 p_intf->p_sys->p_about = NULL;
242 p_intf->p_sys->p_modules = NULL;
243 p_intf->p_sys->p_fileopen = NULL;
244 p_intf->p_sys->p_disc = NULL;
245 p_intf->p_sys->p_network = NULL;
246 p_intf->p_sys->p_preferences = NULL;
247 p_intf->p_sys->p_jump = NULL;
249 /* Store p_intf to keep an eye on it */
250 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
251 "p_intf", p_intf );
253 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
254 "p_intf", p_intf );
256 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
257 "p_intf", p_intf );
259 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
260 "p_intf", p_intf );
262 /* Show the control window */
263 gtk_widget_show( p_intf->p_sys->p_window );
265 /* Sleep to avoid using all CPU - since some interfaces needs to access
266 * keyboard events, a 100ms delay is a good compromise */
267 i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GtkManage, p_intf );
269 /* Enter Gtk mode */
270 gtk_main();
272 /* Remove the timeout */
273 gtk_timeout_remove( i_timeout );
275 /* Launch stored callbacks */
276 if( p_intf->p_sys->pf_gtk_callback != NULL )
278 p_intf->p_sys->pf_gtk_callback();
280 if( p_intf->p_sys->pf_gdk_callback != NULL )
282 p_intf->p_sys->pf_gdk_callback();
287 /* following functions are local */
289 /*****************************************************************************
290 * GtkManage: manage main thread messages
291 *****************************************************************************
292 * In this function, called approx. 10 times a second, we check what the
293 * main program wanted to tell us.
294 *****************************************************************************/
295 static gint GtkManage( gpointer p_data )
297 #define p_intf ((intf_thread_t *)p_data)
299 vlc_mutex_lock( &p_intf->change_lock );
301 /* If the "display popup" flag has changed */
302 if( p_intf->b_menu_change )
304 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
306 p_intf->p_sys->p_popup = create_intf_popup();
307 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
308 "p_popup", p_intf );
310 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
311 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
312 p_intf->b_menu_change = 0;
315 /* update the playlist */
316 GtkPlayListManage( p_data );
318 if( p_intf->p_input != NULL && !p_intf->b_die )
320 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
322 /* New input or stream map change */
323 if( p_intf->p_input->stream.b_changed )
325 GtkModeManage( p_intf );
326 GtkSetupMenus( p_intf );
329 /* Manage the slider */
330 if( p_intf->p_input->stream.b_seekable )
332 float newvalue = p_intf->p_sys->p_adj->value;
334 #define p_area p_intf->p_input->stream.p_selected_area
335 /* If the user hasn't touched the slider since the last time,
336 * then the input can safely change it */
337 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
339 /* Update the value */
340 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
341 ( 100. * p_area->i_tell ) / p_area->i_size;
343 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
344 "value_changed" );
346 /* Otherwise, send message to the input if the user has
347 * finished dragging the slider */
348 else if( p_intf->p_sys->b_slider_free )
350 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
352 /* release the lock to be able to seek */
353 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
354 input_Seek( p_intf->p_input, i_seek );
355 vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
357 /* Update the old value */
358 p_intf->p_sys->f_adj_oldvalue = newvalue;
360 #undef p_area
363 if( p_intf->p_sys->i_part !=
364 p_intf->p_input->stream.p_selected_area->i_part )
366 p_intf->p_sys->b_chapter_update = 1;
367 GtkSetupMenus( p_intf );
370 vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
373 else if( !p_intf->b_die )
375 GtkModeManage( p_intf );
378 /* Manage core vlc functions through the callback */
379 p_intf->pf_manage( p_intf );
381 if( p_intf->b_die )
383 vlc_mutex_unlock( &p_intf->change_lock );
385 /* Prepare to die, young Skywalker */
386 gtk_main_quit();
388 /* Just in case */
389 return( FALSE );
392 vlc_mutex_unlock( &p_intf->change_lock );
394 return( TRUE );
396 #undef p_intf