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