1 /*****************************************************************************
2 * intf_gtk.c: Gtk+ interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: intf_gtk.c,v 1.1 2001/02/21 11:49:18 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 #define MODULE_NAME gtk
25 #include "modules_inner.h"
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <errno.h> /* ENOMEM */
33 #include <stdlib.h> /* free() */
34 #include <string.h> /* strerror() */
46 #include "stream_control.h"
47 #include "input_ext-intf.h"
50 #include "interface.h"
53 #include "gtk_interface.h"
54 #include "gtk_support.h"
58 /*****************************************************************************
60 *****************************************************************************/
61 static int intf_Probe ( probedata_t *p_data );
62 static int intf_Open ( intf_thread_t *p_intf );
63 static void intf_Close ( intf_thread_t *p_intf );
64 static void intf_Run ( intf_thread_t *p_intf );
66 static gint GtkManage ( gpointer p_data );
68 /*****************************************************************************
69 * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
70 *****************************************************************************
71 * gtk_init() makes several calls to g_atexit() which calls atexit() to
72 * register tidying callbacks to be called at program exit. Since the Gtk+
73 * plugin is likely to be unloaded at program exit, we have to export this
74 * symbol to intercept the g_atexit() calls. Talk about crude hack.
75 *****************************************************************************/
76 void g_atexit( GVoidFunc func )
78 intf_thread_t *p_intf = p_main->p_intf;
80 if( p_intf->p_sys->pf_gdk_callback == NULL )
82 p_intf->p_sys->pf_gdk_callback = func;
84 else if( p_intf->p_sys->pf_gtk_callback == NULL )
86 p_intf->p_sys->pf_gtk_callback = func;
88 /* else nothing, but we could do something here */
92 /*****************************************************************************
93 * Functions exported as capabilities. They are declared as static so that
94 * we don't pollute the namespace too much.
95 *****************************************************************************/
96 void _M( intf_getfunctions )( function_list_t * p_function_list )
98 p_function_list->pf_probe = intf_Probe;
99 p_function_list->functions.intf.pf_open = intf_Open;
100 p_function_list->functions.intf.pf_close = intf_Close;
101 p_function_list->functions.intf.pf_run = intf_Run;
104 /*****************************************************************************
105 * intf_Probe: probe the interface and return a score
106 *****************************************************************************
107 * This function tries to initialize Gtk+ and returns a score to the
108 * plugin manager so that it can select the best plugin.
109 *****************************************************************************/
110 static int intf_Probe( probedata_t *p_data )
112 if( TestMethod( INTF_METHOD_VAR, "gtk" ) )
120 /*****************************************************************************
121 * intf_Open: initialize and create window
122 *****************************************************************************/
123 static int intf_Open( intf_thread_t *p_intf )
125 /* Allocate instance and initialize some members */
126 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
127 if( p_intf->p_sys == NULL )
129 intf_ErrMsg("error: %s", strerror(ENOMEM));
133 /* Initialize Gtk+ thread */
134 p_intf->p_sys->b_popup_changed = 0;
135 p_intf->p_sys->b_window_changed = 0;
136 p_intf->p_sys->b_playlist_changed = 0;
138 p_intf->p_sys->b_scale_isfree = 1;
140 p_intf->p_sys->pf_gtk_callback = NULL;
141 p_intf->p_sys->pf_gdk_callback = NULL;
143 /* Initialize lock */
144 vlc_mutex_init( &p_intf->p_sys->change_lock );
149 /*****************************************************************************
150 * intf_Close: destroy interface window
151 *****************************************************************************/
152 static void intf_Close( intf_thread_t *p_intf )
155 vlc_mutex_destroy( &p_intf->p_sys->change_lock );
157 /* Destroy structure */
158 free( p_intf->p_sys );
161 /*****************************************************************************
162 * intf_Run: Gtk+ thread
163 *****************************************************************************
164 * this part of the interface is in a separate thread so that we can call
165 * gtk_main() from within it without annoying the rest of the program.
166 * XXX: the approach may look kludgy, and probably is, but I could not find
167 * a better way to dynamically load a Gtk+ interface at runtime.
168 *****************************************************************************/
169 static void intf_Run( intf_thread_t *p_intf )
171 /* gtk_init needs to know the command line. We don't care, so we
172 * give it an empty one */
173 char *p_args[] = { "" };
174 char **pp_args = p_args;
177 /* The data types we are allowed to receive */
178 static GtkTargetEntry target_table[] =
180 { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
181 { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
184 /* Initialize Gtk+ */
185 gtk_init( &i_args, &pp_args );
187 /* Create some useful widgets that will certainly be used */
188 p_intf->p_sys->p_window = create_intf_window( );
189 p_intf->p_sys->p_popup = create_intf_popup( );
191 /* Set the title of the main window */
192 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
193 VOUT_TITLE " (Gtk+ interface)");
195 /* Accept file drops on the main window */
196 gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
197 GTK_DEST_DEFAULT_ALL, target_table,
198 1, GDK_ACTION_COPY );
200 /* We don't create these ones yet because we perhaps won't need them */
201 p_intf->p_sys->p_about = NULL;
202 p_intf->p_sys->p_playlist = NULL;
203 p_intf->p_sys->p_modules = NULL;
204 p_intf->p_sys->p_fileopen = NULL;
206 /* Store p_intf to keep an eye on it */
207 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
210 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
213 /* Show the control window */
214 gtk_widget_show( p_intf->p_sys->p_window );
216 /* Sleep to avoid using all CPU - since some interfaces needs to access
217 * keyboard events, a 100ms delay is a good compromise */
218 p_intf->p_sys->i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000,
225 /* launch stored callbacks */
226 if( p_intf->p_sys->pf_gtk_callback != NULL )
228 p_intf->p_sys->pf_gtk_callback();
230 if( p_intf->p_sys->pf_gdk_callback != NULL )
232 p_intf->p_sys->pf_gdk_callback();
237 /* following functions are local */
239 /*****************************************************************************
240 * GtkManage: manage main thread messages
241 *****************************************************************************
242 * In this function, called approx. 10 times a second, we check what the
243 * main program wanted to tell us.
244 *****************************************************************************/
245 static gint GtkManage( gpointer p_data )
247 intf_thread_t *p_intf = (void *)p_data;
249 vlc_mutex_lock( &p_intf->p_sys->change_lock );
251 /* If the "display popup" flag has changed */
252 if( p_intf->b_menu_change )
254 if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
256 p_intf->p_sys->p_popup = create_intf_popup();
257 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
260 // gtk_widget_show( p_intf->p_sys->p_popup );
261 gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
262 NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
263 p_intf->b_menu_change = 0;
266 /* Manage the slider */
267 if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL
268 && p_intf->p_sys->b_scale_isfree )
271 GtkAdjustment *p_adj;
273 p_scale = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
274 p_intf->p_sys->p_window ), "hscale" ) );
275 p_adj = gtk_range_get_adjustment ( GTK_RANGE( p_scale ) );
277 /* Update the value */
278 p_adj->value = ( 100. *
279 p_intf->p_input->stream.p_selected_area->i_tell ) /
280 p_intf->p_input->stream.p_selected_area->i_size;
282 /* Gtv does it this way. Why not. */
283 gtk_range_set_adjustment ( GTK_RANGE( p_scale ), p_adj );
284 gtk_range_slider_update ( GTK_RANGE( p_scale ) );
285 gtk_range_clear_background ( GTK_RANGE( p_scale ) );
286 gtk_range_draw_background ( GTK_RANGE( p_scale ) );
289 /* Manage core vlc functions through the callback */
290 p_intf->pf_manage( p_intf );
294 /* Make sure we won't be called again */
295 gtk_timeout_remove( p_intf->p_sys->i_timeout );
297 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
299 /* Prepare to die, young Skywalker */
304 vlc_mutex_unlock( &p_intf->p_sys->change_lock );