]> git.sesse.net Git - vlc/blob - plugins/gtk/gtk_open.c
d7bc7980f3aa00384efb53d403ce46ffbf291525
[vlc] / plugins / gtk / gtk_open.c
1 /*****************************************************************************
2  * gtk_open.c : functions to handle file/disc/network open widgets.
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: gtk_open.c,v 1.3 2001/05/23 23:08:20 stef Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Stéphane Borel <stef@via.ecp.fr>
9  *      
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29 #include <sys/types.h>                                              /* off_t */
30 #include <stdlib.h>
31
32 #include <gtk/gtk.h>
33
34 #include <string.h>
35
36 #include "config.h"
37 #include "common.h"
38 #include "threads.h"
39 #include "mtime.h"
40
41 #include "stream_control.h"
42 #include "input_ext-intf.h"
43
44 #include "interface.h"
45 #include "intf_playlist.h"
46 #include "intf_msg.h"
47
48 #include "gtk_callbacks.h"
49 #include "gtk_interface.h"
50 #include "gtk_support.h"
51 #include "gtk_playlist.h"
52 #include "intf_gtk.h"
53
54 #include "main.h"
55
56 /*****************************************************************************
57  * Fileopen callbacks
58  *****************************************************************************
59  * The following callbacks are related to the file requester.
60  *****************************************************************************/
61 gboolean GtkFileOpenShow( GtkWidget       *widget,
62                           GdkEventButton  *event,
63                           gpointer         user_data )
64 {
65     intf_thread_t *p_intf = GetIntf( GTK_WIDGET(widget), (char*)user_data );
66
67     /* If we have never used the file selector, open it */
68     if( !GTK_IS_WIDGET( p_intf->p_sys->p_fileopen ) )
69     {
70         p_intf->p_sys->p_fileopen = create_intf_fileopen();
71         gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_fileopen ),
72                              "p_intf", p_intf );
73
74         gtk_file_selection_set_filename( GTK_FILE_SELECTION(
75             p_intf->p_sys->p_fileopen ),
76             main_GetPszVariable( INTF_PATH_VAR, INTF_PATH_DEFAULT ) );
77     }
78
79     gtk_widget_show( p_intf->p_sys->p_fileopen );
80     gdk_window_raise( p_intf->p_sys->p_fileopen->window );
81
82     return TRUE;
83 }
84
85
86 void GtkFileOpenCancel( GtkButton * button, gpointer user_data )
87 {
88     gtk_widget_hide( gtk_widget_get_toplevel( GTK_WIDGET (button) ) );
89 }
90
91 void GtkFileOpenOk( GtkButton * button, gpointer user_data )
92 {
93     intf_thread_t * p_intf = GetIntf( GTK_WIDGET(button), "intf_fileopen" );
94     GtkCList *      p_playlist_clist;
95     GtkWidget *     p_filesel;
96     gchar *         psz_filename;
97     int             i_end = p_main->p_playlist->i_size;
98
99     /* hide the file selector */
100     p_filesel = gtk_widget_get_toplevel( GTK_WIDGET(button) );
101     gtk_widget_hide( p_filesel );
102
103     /* add the new file to the interface playlist */
104     psz_filename =
105         gtk_file_selection_get_filename( GTK_FILE_SELECTION( p_filesel ) );
106     intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END, (char*)psz_filename );
107
108     /* catch the GTK CList */
109     p_playlist_clist = GTK_CLIST( gtk_object_get_data(
110         GTK_OBJECT( p_intf->p_sys->p_playlist ), "playlist_clist" ) );
111     /* update the plugin display */
112     GtkRebuildCList( p_playlist_clist, p_main->p_playlist );
113
114     /* end current item, select added item  */
115     if( p_intf->p_input != NULL )
116     {
117         p_intf->p_input->b_eof = 1;
118     }
119
120     intf_PlaylistJumpto( p_main->p_playlist, i_end - 1 );
121 }
122
123 /*****************************************************************************
124  * Open disc callbacks
125  *****************************************************************************
126  * The following callbacks are related to the disc manager.
127  *****************************************************************************/
128 gboolean GtkDiscOpenShow( GtkWidget       *widget,
129                           GdkEventButton  *event,
130                           gpointer         user_data)
131 {
132     intf_thread_t *p_intf = GetIntf( GTK_WIDGET(widget), (char*)user_data );
133
134     if( !GTK_IS_WIDGET( p_intf->p_sys->p_disc ) )
135     {
136         p_intf->p_sys->p_disc = create_intf_disc();
137         gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_disc ),
138                              "p_intf", p_intf );
139     }
140
141     gtk_widget_show( p_intf->p_sys->p_disc );
142     gdk_window_raise( p_intf->p_sys->p_disc->window );
143
144     return TRUE;
145 }
146
147
148 void GtkDiscOpenDvd( GtkToggleButton * togglebutton, gpointer user_data )
149 {
150     if( togglebutton->active )
151     {
152         gtk_entry_set_text(
153           GTK_ENTRY( lookup_widget( GTK_WIDGET(togglebutton), "disc_name" ) ),
154           main_GetPszVariable( INPUT_DVD_DEVICE_VAR, INPUT_DVD_DEVICE_DEFAULT )
155         );
156     }
157 }
158
159 void GtkDiscOpenVcd( GtkToggleButton * togglebutton, gpointer user_data )
160 {
161     if( togglebutton->active )
162     {
163         gtk_entry_set_text(
164           GTK_ENTRY( lookup_widget( GTK_WIDGET(togglebutton), "disc_name" ) ),
165           main_GetPszVariable( INPUT_VCD_DEVICE_VAR, INPUT_VCD_DEVICE_DEFAULT )
166         );
167     }
168 }
169
170 void GtkDiscOpenOk( GtkButton * button, gpointer user_data )
171 {
172     intf_thread_t * p_intf = GetIntf( GTK_WIDGET(button), "intf_disc" );
173     GtkCList *      p_playlist_clist;
174     char *          psz_device, *psz_source, *psz_method;
175     int             i_end = p_main->p_playlist->i_size;
176
177     gtk_widget_hide( p_intf->p_sys->p_disc );
178     psz_device = gtk_entry_get_text( GTK_ENTRY( lookup_widget(
179                                          GTK_WIDGET(button), "disc_name" ) ) );
180
181     /* "dvd:foo" has size 5 + strlen(foo) */
182     psz_source = malloc( 3 /* "dvd" */ + 1 /* ":" */
183                            + strlen( psz_device ) + 1 /* "\0" */ );
184     if( psz_source == NULL )
185     {
186         return;
187     }
188
189     /* Check which method was activated */
190     if( GTK_TOGGLE_BUTTON( lookup_widget( GTK_WIDGET(button),
191                                           "disc_dvd" ) )->active )
192     {
193         psz_method = "dvd";
194     }
195     else if( GTK_TOGGLE_BUTTON( lookup_widget( GTK_WIDGET(button),
196                                                "disc_vcd" ) )->active )
197     {
198         psz_method = "vcd";
199     }
200     else
201     {
202         intf_ErrMsg( "intf error: unknown disc type toggle button position" );
203         free( psz_source );
204         return;
205     }
206     
207     /* Select title and chapter */
208     main_PutIntVariable( INPUT_TITLE_VAR, gtk_spin_button_get_value_as_int(
209                               GTK_SPIN_BUTTON( lookup_widget(
210                                   GTK_WIDGET(button), "disc_title" ) ) ) );
211
212     main_PutIntVariable( INPUT_CHAPTER_VAR, gtk_spin_button_get_value_as_int(
213                               GTK_SPIN_BUTTON( lookup_widget(
214                                   GTK_WIDGET(button), "disc_chapter" ) ) ) );
215
216     /* Build source name and add it to playlist */
217     sprintf( psz_source, "%s:%s", psz_method, psz_device );
218     intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END, psz_source );
219     free( psz_source );
220
221     /* catch the GTK CList */
222     p_playlist_clist = GTK_CLIST( gtk_object_get_data(
223         GTK_OBJECT( p_intf->p_sys->p_playlist ), "playlist_clist" ) );
224
225     /* update the display */
226     GtkRebuildCList( p_playlist_clist, p_main->p_playlist );
227
228     /* stop current item, select added item */
229     if( p_intf->p_input != NULL )
230     {
231         p_intf->p_input->b_eof = 1;
232     }
233
234     intf_PlaylistJumpto( p_main->p_playlist, i_end - 1 );
235 }
236
237
238 void GtkDiscOpenCancel( GtkButton * button, gpointer user_data )
239 {
240     gtk_widget_hide( gtk_widget_get_toplevel( GTK_WIDGET (button) ) );
241 }
242
243
244 /*****************************************************************************
245  * Network stream callbacks
246  *****************************************************************************
247  * The following callbacks are related to the network stream manager.
248  *****************************************************************************/
249 gboolean GtkNetworkOpenShow( GtkWidget       *widget,
250                              GdkEventButton  *event,
251                              gpointer         user_data )
252 {
253     intf_thread_t *p_intf = GetIntf( GTK_WIDGET(widget), (char*)user_data );
254
255     if( !GTK_IS_WIDGET( p_intf->p_sys->p_network ) )
256     {
257         p_intf->p_sys->p_network = create_intf_network();
258         gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_network ),
259                              "p_intf", p_intf );
260     }
261
262     gtk_widget_show( p_intf->p_sys->p_network );
263     gdk_window_raise( p_intf->p_sys->p_network->window );
264
265     return TRUE;
266 }
267
268
269 void GtkNetworkOpenOk( GtkButton *button, gpointer user_data )
270 {
271     intf_thread_t * p_intf = GetIntf( GTK_WIDGET(button), "intf_network" );
272     GtkCList *      p_playlist_clist;
273     char *          psz_source, *psz_server, *psz_protocol;
274     unsigned int    i_port;
275     boolean_t       b_broadcast;
276     int             i_end = p_main->p_playlist->i_size;
277
278     gtk_widget_hide( p_intf->p_sys->p_network );
279     psz_server = gtk_entry_get_text( GTK_ENTRY( lookup_widget(
280                                  GTK_WIDGET(button), "network_server" ) ) );
281
282     /* Check which protocol was activated */
283     if( GTK_TOGGLE_BUTTON( lookup_widget( GTK_WIDGET(button),
284                                           "network_ts" ) )->active )
285     {
286         psz_protocol = "ts";
287     }
288     else if( GTK_TOGGLE_BUTTON( lookup_widget( GTK_WIDGET(button),
289                                                "network_rtp" ) )->active )
290     {
291         psz_protocol = "rtp";
292     }
293     else
294     {
295         intf_ErrMsg( "intf error: unknown protocol toggle button position" );
296         return;
297     }
298
299     /* Get the port number and make sure it will not overflow 5 characters */
300     i_port = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON(
301                  lookup_widget( GTK_WIDGET(button), "network_port" ) ) );
302     if( i_port > 65535 )
303     {
304         intf_ErrMsg( "intf error: invalid port %i", i_port );
305     }
306
307     /* do we have a broadcast address */
308     b_broadcast = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(
309         lookup_widget( GTK_WIDGET(button), "broadcast_check" ) ) );
310     if( b_broadcast )
311     {
312         char *  psz_broadcast;
313         psz_broadcast = gtk_entry_get_text( GTK_ENTRY( lookup_widget(
314                                  GTK_WIDGET(button), "network_broadcast" ) ) );
315         /* Allocate room for "protocol://server:port" */
316         psz_source = malloc( strlen( psz_protocol ) + 3 /* "://" */
317                                + strlen( psz_server ) + 1 /* ":" */
318                                + 5 /* 0-65535 */
319                                + strlen( psz_broadcast ) + 2 /* "::" */ 
320                                + 1 /* "\0" */ );
321         if( psz_source == NULL )
322         {
323             return;
324         }
325
326         /* Build source name and add it to playlist */
327         sprintf( psz_source, "%s://%s:%i::%s", psz_protocol,
328                                                psz_server,
329                                                i_port,
330                                                psz_broadcast );
331     }
332     else
333     {
334         /* Allocate room for "protocol://server:port" */
335         psz_source = malloc( strlen( psz_protocol ) + 3 /* "://" */
336                                + strlen( psz_server ) + 1 /* ":" */
337                                + 5 /* 0-65535 */ + 1 /* "\0" */ );
338         if( psz_source == NULL )
339         {
340             return;
341         }
342        
343         /* Build source name and add it to playlist */
344         sprintf( psz_source, "%s://%s:%i", psz_protocol, psz_server, i_port );
345     }
346
347     intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END, psz_source );
348     free( psz_source );
349
350     /* catch the GTK CList */
351     p_playlist_clist = GTK_CLIST( gtk_object_get_data(
352         GTK_OBJECT( p_intf->p_sys->p_playlist ), "playlist_clist" ) );
353     /* update the display */
354     GtkRebuildCList( p_playlist_clist, p_main->p_playlist );
355
356     /* select added item */
357     if( p_intf->p_input != NULL )
358     {
359         p_intf->p_input->b_eof = 1;
360     }
361
362     intf_PlaylistJumpto( p_main->p_playlist, i_end - 1 );
363 }
364
365 void GtkNetworkOpenCancel( GtkButton * button, gpointer user_data)
366 {
367     gtk_widget_hide( gtk_widget_get_toplevel( GTK_WIDGET (button) ) );
368 }
369
370
371 void GtkNetworkOpenBroadcast( GtkToggleButton * togglebutton,
372                               gpointer user_data )
373 {
374     GtkWidget *     p_network;
375
376     p_network = gtk_widget_get_toplevel( GTK_WIDGET (togglebutton) );
377
378     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
379             "network_broadcast_combo" ),
380             gtk_toggle_button_get_active( togglebutton ) );
381
382     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
383             "network_broadcast" ),
384             gtk_toggle_button_get_active( togglebutton ) );
385 }
386
387
388
389 /****************************************************************************
390  * Callbacks for menuitem
391  ****************************************************************************/
392 void GtkFileOpenActivate( GtkMenuItem * menuitem, gpointer user_data )
393 {
394     GtkFileOpenShow( GTK_WIDGET( menuitem ), NULL, user_data );
395 }
396
397
398 void GtkDiscOpenActivate( GtkMenuItem * menuitem, gpointer user_data )
399 {
400     GtkDiscOpenShow( GTK_WIDGET( menuitem ), NULL, user_data );
401 }
402
403
404 void GtkNetworkOpenActivate( GtkMenuItem * menuitem, gpointer user_data )
405 {
406     GtkNetworkOpenShow( GTK_WIDGET( menuitem ), NULL, user_data );
407 }
408