]> git.sesse.net Git - vlc/blob - plugins/gtk/gtk_open.c
*** empty log message ***
[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.17 2002/03/04 01:53:56 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 <sys/types.h>                                              /* off_t */
29 #include <stdlib.h>
30
31 #include <videolan/vlc.h>
32
33 #ifdef MODULE_NAME_IS_gnome
34 #   include <gnome.h>
35 #else
36 #   include <gtk/gtk.h>
37 #endif
38
39 #include <string.h>
40
41 #include "stream_control.h"
42 #include "input_ext-intf.h"
43
44 #include "interface.h"
45 #include "intf_playlist.h"
46
47 #include "gtk_callbacks.h"
48 #include "gtk_interface.h"
49 #include "gtk_support.h"
50 #include "gtk_playlist.h"
51 #include "gtk_common.h"
52
53 #include "netutils.h"
54
55 /*****************************************************************************
56  * Fileopen callbacks
57  *****************************************************************************
58  * The following callbacks are related to the file requester.
59  *****************************************************************************/
60 gboolean GtkFileOpenShow( GtkWidget       *widget,
61                           GdkEventButton  *event,
62                           gpointer         user_data )
63 {
64     intf_thread_t *p_intf = GetIntf( GTK_WIDGET(widget), (char*)user_data );
65
66     /* If we have never used the file selector, open it */
67     if( !GTK_IS_WIDGET( p_intf->p_sys->p_fileopen ) )
68     {
69         char *psz_path;
70
71         p_intf->p_sys->p_fileopen = create_intf_fileopen();
72         gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_fileopen ),
73                              "p_intf", p_intf );
74
75         if( (psz_path = config_GetPszVariable( INTF_PATH_VAR )) )
76             gtk_file_selection_set_filename( GTK_FILE_SELECTION(
77                 p_intf->p_sys->p_fileopen ), psz_path );
78         if( psz_path ) free( psz_path );
79     }
80
81     gtk_widget_show( p_intf->p_sys->p_fileopen );
82     gdk_window_raise( p_intf->p_sys->p_fileopen->window );
83
84     return TRUE;
85 }
86
87
88 void GtkFileOpenCancel( GtkButton * button, gpointer user_data )
89 {
90     gtk_widget_hide( gtk_widget_get_toplevel( GTK_WIDGET (button) ) );
91 }
92
93 void GtkFileOpenOk( GtkButton * button, gpointer user_data )
94 {
95     intf_thread_t * p_intf = GetIntf( GTK_WIDGET(button), "intf_fileopen" );
96     GtkCList *      p_playlist_clist;
97     GtkWidget *     p_filesel;
98     gchar *         psz_filename;
99     int             i_end = p_main->p_playlist->i_size;
100
101     /* hide the file selector */
102     p_filesel = gtk_widget_get_toplevel( GTK_WIDGET(button) );
103     gtk_widget_hide( p_filesel );
104
105     /* add the new file to the interface playlist */
106     psz_filename =
107         gtk_file_selection_get_filename( GTK_FILE_SELECTION( p_filesel ) );
108     intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END, (char*)psz_filename );
109
110     /* catch the GTK CList */
111     p_playlist_clist = GTK_CLIST( gtk_object_get_data(
112         GTK_OBJECT( p_intf->p_sys->p_playlist ), "playlist_clist" ) );
113     /* update the plugin display */
114     GtkRebuildCList( p_playlist_clist, p_main->p_playlist );
115
116     /* end current item, select added item  */
117     if( p_input_bank->pp_input[0] != NULL )
118     {
119         p_input_bank->pp_input[0]->b_eof = 1;
120     }
121
122     intf_PlaylistJumpto( p_main->p_playlist, i_end - 1 );
123 }
124
125 /*****************************************************************************
126  * Open disc callbacks
127  *****************************************************************************
128  * The following callbacks are related to the disc manager.
129  *****************************************************************************/
130 gboolean GtkDiscOpenShow( GtkWidget       *widget,
131                           GdkEventButton  *event,
132                           gpointer         user_data)
133 {
134     intf_thread_t *p_intf = GetIntf( GTK_WIDGET(widget), (char*)user_data );
135
136     if( !GTK_IS_WIDGET( p_intf->p_sys->p_disc ) )
137     {
138         p_intf->p_sys->p_disc = create_intf_disc();
139         gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_disc ),
140                              "p_intf", p_intf );
141     }
142
143     gtk_widget_show( p_intf->p_sys->p_disc );
144     gdk_window_raise( p_intf->p_sys->p_disc->window );
145
146     return TRUE;
147 }
148
149
150 void GtkDiscOpenDvd( GtkToggleButton * togglebutton, gpointer user_data )
151 {
152     if( togglebutton->active )
153     {
154         char *psz_dvd_device;
155
156         if( (psz_dvd_device = config_GetPszVariable( INPUT_DVD_DEVICE_VAR )) )
157             gtk_entry_set_text(
158                 GTK_ENTRY( lookup_widget( GTK_WIDGET(togglebutton),
159                                           "disc_name" ) ), psz_dvd_device );
160         if( psz_dvd_device ) free( psz_dvd_device );
161     }
162 }
163
164 void GtkDiscOpenVcd( GtkToggleButton * togglebutton, gpointer user_data )
165 {
166     if( togglebutton->active )
167     {
168         char *psz_vcd_device;
169
170         if( (psz_vcd_device = config_GetPszVariable( INPUT_VCD_DEVICE_VAR )) )
171             gtk_entry_set_text(
172                 GTK_ENTRY( lookup_widget( GTK_WIDGET(togglebutton),
173                                           "disc_name" ) ), psz_vcd_device );
174         if( psz_vcd_device ) free( psz_vcd_device );
175     }
176 }
177
178 void GtkDiscOpenOk( GtkButton * button, gpointer user_data )
179 {
180     intf_thread_t * p_intf = GetIntf( GTK_WIDGET(button), "intf_disc" );
181     GtkCList *      p_playlist_clist;
182     char *          psz_device, *psz_source, *psz_method;
183     int             i_end = p_main->p_playlist->i_size;
184     int             i_title, i_chapter;
185
186     gtk_widget_hide( p_intf->p_sys->p_disc );
187     psz_device = gtk_entry_get_text( GTK_ENTRY( lookup_widget(
188                                          GTK_WIDGET(button), "disc_name" ) ) );
189
190     /* Check which method was activated */
191     if( GTK_TOGGLE_BUTTON( lookup_widget( GTK_WIDGET(button),
192                                           "disc_dvd" ) )->active )
193     {
194         psz_method = "dvd";
195     }
196     else if( GTK_TOGGLE_BUTTON( lookup_widget( GTK_WIDGET(button),
197                                                "disc_vcd" ) )->active )
198     {
199         psz_method = "vcd";
200     }
201     else
202     {
203         intf_ErrMsg( "intf error: unknown disc type toggle button position" );
204         free( psz_source );
205         return;
206     }
207     
208     /* Select title and chapter */
209     i_title = gtk_spin_button_get_value_as_int(
210                               GTK_SPIN_BUTTON( lookup_widget(
211                                   GTK_WIDGET(button), "disc_title" ) ) );
212
213     i_chapter = gtk_spin_button_get_value_as_int(
214                               GTK_SPIN_BUTTON( lookup_widget(
215                                   GTK_WIDGET(button), "disc_chapter" ) ) );
216     
217     /* "dvd:foo" has size 5 + strlen(foo) */
218     psz_source = malloc( 3 /* "dvd" */ + 1 /* ":" */
219                            + strlen( psz_device ) + 2 /* @, */
220                            + 4 /* i_title & i_chapter < 100 */ + 1 /* "\0" */ );
221     if( psz_source == NULL )
222     {
223         return;
224     }
225
226     /* Build source name and add it to playlist */
227     sprintf( psz_source, "%s:%s@%d,%d",
228              psz_method, psz_device, i_title, i_chapter );
229     intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END, psz_source );
230     free( psz_source );
231
232     /* catch the GTK CList */
233     p_playlist_clist = GTK_CLIST( gtk_object_get_data(
234         GTK_OBJECT( p_intf->p_sys->p_playlist ), "playlist_clist" ) );
235
236     /* update the display */
237     GtkRebuildCList( p_playlist_clist, p_main->p_playlist );
238
239     /* stop current item, select added item */
240     if( p_input_bank->pp_input[0] != NULL )
241     {
242         p_input_bank->pp_input[0]->b_eof = 1;
243     }
244
245     intf_PlaylistJumpto( p_main->p_playlist, i_end - 1 );
246 }
247
248
249 void GtkDiscOpenCancel( GtkButton * button, gpointer user_data )
250 {
251     gtk_widget_hide( gtk_widget_get_toplevel( GTK_WIDGET (button) ) );
252 }
253
254
255 /*****************************************************************************
256  * Network stream callbacks
257  *****************************************************************************
258  * The following callbacks are related to the network stream manager.
259  *****************************************************************************/
260 gboolean GtkNetworkOpenShow( GtkWidget       *widget,
261                              GdkEventButton  *event,
262                              gpointer         user_data )
263 {
264     intf_thread_t *p_intf = GetIntf( GTK_WIDGET(widget), (char*)user_data );
265
266     if( !GTK_IS_WIDGET( p_intf->p_sys->p_network ) )
267     {
268         char *psz_channel_server;
269
270         p_intf->p_sys->p_network = create_intf_network();
271         gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_network ),
272                              "p_intf", p_intf );
273
274         gtk_spin_button_set_value( GTK_SPIN_BUTTON( gtk_object_get_data(
275             GTK_OBJECT( p_intf->p_sys->p_network ), "network_port" ) ),
276             config_GetIntVariable( INPUT_PORT_VAR ) );
277
278         psz_channel_server = config_GetPszVariable( INPUT_CHANNEL_SERVER_VAR );
279         if( psz_channel_server )
280             gtk_entry_set_text( GTK_ENTRY( gtk_object_get_data(
281                 GTK_OBJECT( p_intf->p_sys->p_network ), "network_channel" ) ),
282                 psz_channel_server );
283         if( psz_channel_server ) free( psz_channel_server );
284
285         gtk_spin_button_set_value( GTK_SPIN_BUTTON( gtk_object_get_data(
286             GTK_OBJECT( p_intf->p_sys->p_network ), "network_channel_port" ) ),
287             config_GetIntVariable( INPUT_CHANNEL_PORT_VAR ) );
288
289         gtk_toggle_button_set_active( gtk_object_get_data( GTK_OBJECT(
290             p_intf->p_sys->p_network ), "network_channel_check" ),
291             config_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR ) );
292     }
293
294     gtk_widget_show( p_intf->p_sys->p_network );
295     gdk_window_raise( p_intf->p_sys->p_network->window );
296
297     return TRUE;
298 }
299
300
301 void GtkNetworkOpenOk( GtkButton *button, gpointer user_data )
302 {
303     intf_thread_t * p_intf = GetIntf( GTK_WIDGET(button), "intf_network" );
304     GtkCList *      p_playlist_clist;
305     char *          psz_source, *psz_server, *psz_protocol;
306     unsigned int    i_port;
307     boolean_t       b_broadcast;
308     boolean_t       b_channel;
309     int             i_end = p_main->p_playlist->i_size;
310
311     gtk_widget_hide( p_intf->p_sys->p_network );
312     psz_server = gtk_entry_get_text( GTK_ENTRY( lookup_widget(
313                                  GTK_WIDGET(button), "network_server" ) ) );
314
315     /* select added item */
316     if( p_input_bank->pp_input[0] != NULL )
317     {
318         p_input_bank->pp_input[0]->b_eof = 1;
319     }
320
321     /* Check which protocol was activated */
322     if( GTK_TOGGLE_BUTTON( lookup_widget( GTK_WIDGET(button),
323                                           "network_ts" ) )->active )
324     {
325         psz_protocol = "udpstream";
326     }
327     else if( GTK_TOGGLE_BUTTON( lookup_widget( GTK_WIDGET(button),
328                                                "network_rtp" ) )->active )
329     {
330         psz_protocol = "rtp";
331     }
332     else
333     {
334         intf_ErrMsg( "intf error: unknown protocol toggle button position" );
335         return;
336     }
337
338     /* Manage channel server */
339     b_channel = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(
340         lookup_widget( GTK_WIDGET(button), "network_channel_check" ) ) );
341     config_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, b_channel );
342     if( b_channel )
343     {
344         char *          psz_channel;
345         unsigned int    i_channel_port;
346
347         if( p_main->p_channel == NULL )
348         {
349             network_ChannelCreate();
350         }
351
352         psz_channel = gtk_entry_get_text( GTK_ENTRY( lookup_widget(
353                              GTK_WIDGET(button), "network_channel" ) ) );
354         i_channel_port = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON(
355             lookup_widget( GTK_WIDGET(button), "network_channel_port" ) ) );
356
357         config_PutPszVariable( INPUT_CHANNEL_SERVER_VAR, psz_channel );
358         if( i_channel_port < 65536 )
359         {
360             config_PutIntVariable( INPUT_CHANNEL_PORT_VAR, i_channel_port );
361         }
362
363         p_intf->p_sys->b_playing = 1;
364
365     }
366     else
367     {
368         /* Get the port number and make sure it will not
369          * overflow 5 characters */
370         i_port = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON(
371                      lookup_widget( GTK_WIDGET(button), "network_port" ) ) );
372         if( i_port > 65535 )
373         {
374             intf_ErrMsg( "intf error: invalid port %i", i_port );
375         }
376
377         /* do we have a broadcast address */
378         b_broadcast = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(
379             lookup_widget( GTK_WIDGET(button), "network_broadcast_check" ) ) );
380         if( b_broadcast )
381         {
382             char *  psz_broadcast;
383             psz_broadcast = gtk_entry_get_text( GTK_ENTRY( lookup_widget(
384                             GTK_WIDGET(button), "network_broadcast" ) ) );
385             /* Allocate room for "protocol://server:port" */
386             psz_source = malloc( strlen( psz_protocol ) + 3 /* "://" */
387                                    + strlen( psz_server ) + 2 /* "@:" */
388                                    + 5 /* 0-65535 */
389                                    + strlen( psz_broadcast ) + 2 /* "::" */ 
390                                    + 1 /* "\0" */ );
391             if( psz_source == NULL )
392             {
393                 return;
394             }
395
396             /* Build source name and add it to playlist */
397             sprintf( psz_source, "%s://%s@:%i/%s", psz_protocol,
398                                                   psz_server,
399                                                   i_port,
400                                                   psz_broadcast );
401         }
402         else
403         {
404             /* Allocate room for "protocol://server:port" */
405             psz_source = malloc( strlen( psz_protocol ) + 3 /* "://" */
406                                    + strlen( psz_server ) + 2 /* "@:" */
407                                    + 5 /* 0-65535 */ + 1 /* "\0" */ );
408             if( psz_source == NULL )
409             {
410                 return;
411             }
412            
413             /* Build source name and add it to playlist */
414             sprintf( psz_source, "%s://%s@:%i",
415                      psz_protocol, psz_server, i_port );
416         }
417
418         intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END, psz_source );
419         free( psz_source );
420         
421         /* catch the GTK CList */
422         p_playlist_clist = GTK_CLIST( gtk_object_get_data(
423             GTK_OBJECT( p_intf->p_sys->p_playlist ), "playlist_clist" ) );
424         /* update the display */
425         GtkRebuildCList( p_playlist_clist, p_main->p_playlist );
426
427         intf_PlaylistJumpto( p_main->p_playlist, i_end - 1 );
428     }
429 }
430
431 void GtkNetworkOpenCancel( GtkButton * button, gpointer user_data)
432 {
433     gtk_widget_hide( gtk_widget_get_toplevel( GTK_WIDGET (button) ) );
434 }
435
436
437 void GtkNetworkOpenBroadcast( GtkToggleButton * togglebutton,
438                               gpointer user_data )
439 {
440     GtkWidget *     p_network;
441
442     p_network = gtk_widget_get_toplevel( GTK_WIDGET (togglebutton) );
443
444     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
445             "network_broadcast_combo" ),
446             gtk_toggle_button_get_active( togglebutton ) );
447
448     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
449             "network_broadcast" ),
450             gtk_toggle_button_get_active( togglebutton ) );
451 }
452
453
454 void GtkNetworkOpenChannel( GtkToggleButton * togglebutton,
455                             gpointer user_data )
456 {
457     GtkWidget *     p_network;
458     boolean_t       b_channel;
459     boolean_t       b_broadcast;
460
461     p_network = gtk_widget_get_toplevel( GTK_WIDGET (togglebutton) );
462     b_channel = gtk_toggle_button_get_active( togglebutton );
463     b_broadcast = gtk_toggle_button_get_active( gtk_object_get_data(
464                   GTK_OBJECT( p_network ), "network_broadcast_check" ) );
465         
466     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
467             "network_channel_combo" ), b_channel ) ;
468
469     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
470             "network_channel" ), b_channel );
471
472     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
473             "network_channel_port" ), b_channel );
474
475     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
476             "network_channel_port_label" ), b_channel );
477
478     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
479             "network_server_combo" ), ! b_channel );
480
481     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
482             "network_server_label" ), ! b_channel );
483
484     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
485             "network_server" ), ! b_channel );
486
487     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
488             "network_port_label" ), ! b_channel );
489
490     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
491             "network_port" ), ! b_channel );
492
493     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
494             "network_broadcast_check" ), ! b_channel );
495     
496     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
497             "network_broadcast_combo" ), b_broadcast && ! b_channel );
498
499     gtk_widget_set_sensitive( gtk_object_get_data( GTK_OBJECT( p_network ),
500             "network_broadcast" ), b_broadcast && ! b_channel );
501 }
502
503
504 /****************************************************************************
505  * Callbacks for menuitem
506  ****************************************************************************/
507 void GtkFileOpenActivate( GtkMenuItem * menuitem, gpointer user_data )
508 {
509     GtkFileOpenShow( GTK_WIDGET( menuitem ), NULL, user_data );
510 }
511
512
513 void GtkDiscOpenActivate( GtkMenuItem * menuitem, gpointer user_data )
514 {
515     GtkDiscOpenShow( GTK_WIDGET( menuitem ), NULL, user_data );
516 }
517
518
519 void GtkNetworkOpenActivate( GtkMenuItem * menuitem, gpointer user_data )
520 {
521     GtkNetworkOpenShow( GTK_WIDGET( menuitem ), NULL, user_data );
522 }
523