Support for browsing local media (not finished).
[vlc.git] / modules / gui / familiar / callbacks.c
1 /*****************************************************************************
2  * callbacks.c : Callbacks for the Familiar Linux Gtk+ plugin.
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: callbacks.c,v 1.3 2002/08/12 20:38:19 jpsaman Exp $
6  *
7  * Authors: Jean-Paul Saman <jpsaman@wxs.nl>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <sys/types.h>                                              /* off_t */
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/intf.h>
32 #include <vlc/vout.h>
33
34 #include <unistd.h>
35 #include <string.h>
36 #include <dirent.h>
37
38 #ifdef HAVE_CONFIG_H
39 #  include <config.h>
40 #endif
41
42 #include <gtk/gtk.h>
43
44 #include "callbacks.h"
45 #include "interface.h"
46 #include "support.h"
47 #include "familiar.h"
48
49 //#include "netutils.h"
50
51 static void MediaURLOpenChanged( GtkEditable *editable, gpointer user_data );
52 static void PreferencesURLOpenChanged( GtkEditable *editable, gpointer user_data );
53
54 /*****************************************************************************
55  * Useful function to retrieve p_intf
56  ****************************************************************************/
57 void * __GtkGetIntf( GtkWidget * widget )
58 {
59     void *p_data;
60
61     if( GTK_IS_MENU_ITEM( widget ) )
62     {
63         /* Look for a GTK_MENU */
64         while( widget->parent && !GTK_IS_MENU( widget ) )
65         {
66             widget = widget->parent;
67         }
68
69         /* Maybe this one has the data */
70         p_data = gtk_object_get_data( GTK_OBJECT( widget ), "p_intf" );
71         if( p_data )
72         {
73             return p_data;
74         }
75
76         /* Otherwise, the parent widget has it */
77         widget = gtk_menu_get_attach_widget( GTK_MENU( widget ) );
78     }
79
80     /* We look for the top widget */
81     widget = gtk_widget_get_toplevel( GTK_WIDGET( widget ) );
82
83     p_data = gtk_object_get_data( GTK_OBJECT( widget ), "p_intf" );
84
85     return p_data;
86 }
87
88 /*****************************************************************************
89  * Helper functions for URL changes in Media and Preferences notebook pages.
90  ****************************************************************************/
91 static void MediaURLOpenChanged( GtkEditable *editable, gpointer user_data )
92 {
93     intf_thread_t *p_intf = GtkGetIntf( editable );
94     playlist_t *p_playlist;
95     gchar *       psz_url;
96
97     psz_url = gtk_entry_get_text(GTK_ENTRY(editable));
98     g_print( "%s\n",psz_url );
99 //    p_url = gtk_editable_get_chars(editable,0,-1);
100
101     // Add p_url to playlist .... but how ?
102     if (p_intf)
103     {
104         p_playlist = (playlist_t *)
105                  vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
106         if( p_playlist )
107         {
108            playlist_Add( p_playlist, (char*)psz_url,
109                          PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
110            vlc_object_release( p_playlist );
111         }
112     }
113 }
114
115 static void PreferencesURLOpenChanged( GtkEditable *editable, gpointer user_data )
116 {
117     gchar *       p_url;
118 //    GtkWidget *   item;
119
120     p_url = gtk_entry_get_text(GTK_ENTRY(editable) );
121     g_print( "%s\n",p_url );
122
123 //    p_url = gtk_editable_get_chars(editable,0,-1);
124 //    item = gtk_list_item_new();
125 //    gtk_widget_show (item);
126 //    gtk_combo_set_item_string (GTK_COMBO (combo), GTK_ITEM (item), p_url);
127 //    /* Now we simply add the item to the combo's list. */
128 //    gtk_container_add (GTK_CONTAINER (GTK_COMBO (combo)->list), item);
129 }
130
131 /*****************************************************************
132  * Read directory helper function.
133  ****************************************************************/
134 static void ReadDirectory( GtkWidget *widget, char *psz_dir)
135 {
136     struct dirent **namelist;
137     int n;
138
139     if (psz_dir)
140        n = scandir(psz_dir, &namelist, 0, alphasort);
141     else
142        n = scandir(".", &namelist, 0, alphasort);
143     if (n<0)
144         perror("scandir");
145     else
146     {
147         while(n--) /* reverse printed list */
148         {
149             /* add list to listMedia widget */
150             g_print("%s\n",namelist[n]->d_name);
151             free(namelist[n]);
152         }
153         free(namelist);
154     }
155 }
156
157
158 /*
159  * Main interface callbacks
160  */
161
162 gboolean GtkExit( GtkWidget       *widget,
163                   gpointer         user_data )
164 {
165     intf_thread_t *p_intf = GtkGetIntf( widget );
166
167     vlc_mutex_lock( &p_intf->change_lock );
168     p_intf->p_vlc->b_die = VLC_TRUE;
169     vlc_mutex_unlock( &p_intf->change_lock );
170
171     return TRUE;
172 }
173
174 gboolean
175 on_familiar_destroy_event              (GtkWidget       *widget,
176                                         GdkEvent        *event,
177                                         gpointer         user_data)
178 {
179     GtkExit( GTK_WIDGET( widget ), user_data );
180     return TRUE;
181 }
182
183
184 void
185 on_toolbar_open_clicked                (GtkButton       *button,
186                                         gpointer         user_data)
187 {
188     intf_thread_t *p_intf = GtkGetIntf( button );
189     if (p_intf)
190     {
191         gtk_widget_show( GTK_WIDGET(p_intf->p_sys->p_notebook) );
192         gdk_window_raise( p_intf->p_sys->p_window->window );
193     }
194 }
195
196
197 void
198 on_toolbar_preferences_clicked         (GtkButton       *button,
199                                         gpointer         user_data)
200 {
201     intf_thread_t *p_intf = GtkGetIntf( button );
202     if (p_intf) {
203         gtk_widget_show( GTK_WIDGET(p_intf->p_sys->p_notebook) );
204         gdk_window_raise( p_intf->p_sys->p_window->window );
205     }
206 }
207
208
209 void
210 on_toolbar_rewind_clicked              (GtkButton       *button,
211                                         gpointer         user_data)
212 {
213     intf_thread_t *  p_intf = GtkGetIntf( button );
214
215     if( p_intf )
216     {
217         if( p_intf->p_sys->p_input )
218         {
219             input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_SLOWER );
220         }
221     }
222 }
223
224
225 void
226 on_toolbar_pause_clicked               (GtkButton       *button,
227                                         gpointer         user_data)
228 {
229     intf_thread_t *  p_intf = GtkGetIntf( button );
230
231     if( p_intf )
232     {
233         if( p_intf->p_sys->p_input )
234         {
235             input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PAUSE );
236         }
237     }
238 }
239
240
241 void
242 on_toolbar_play_clicked                (GtkButton       *button,
243                                         gpointer         user_data)
244 {
245     intf_thread_t *  p_intf = GtkGetIntf( button );
246     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
247                                                        FIND_ANYWHERE );
248     if( p_playlist == NULL )
249     {
250         if( p_intf )
251         {
252            gtk_widget_show( GTK_WIDGET(p_intf->p_sys->p_notebook) );
253            gdk_window_raise( p_intf->p_sys->p_window->window );
254         }
255         // Display open page
256     }
257
258     /* If the playlist is empty, open a file requester instead */
259     vlc_mutex_lock( &p_playlist->object_lock );
260     if( p_playlist->i_size )
261     {
262         vlc_mutex_unlock( &p_playlist->object_lock );
263         playlist_Play( p_playlist );
264         vlc_object_release( p_playlist );
265         gdk_window_lower( p_intf->p_sys->p_window->window );
266     }
267     else
268     {
269         vlc_mutex_unlock( &p_playlist->object_lock );
270         vlc_object_release( p_playlist );
271         // Display open page
272     }
273 }
274
275
276 void
277 on_toolbar_stop_clicked                (GtkButton       *button,
278                                         gpointer         user_data)
279 {
280     intf_thread_t *  p_intf = GtkGetIntf( button );
281     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
282                                                        FIND_ANYWHERE );
283     if( p_playlist)
284     {
285         playlist_Stop( p_playlist );
286         vlc_object_release( p_playlist );
287         gdk_window_raise( p_intf->p_sys->p_window->window );
288     }
289 }
290
291
292 void
293 on_toolbar_forward_clicked             (GtkButton       *button,
294                                         gpointer         user_data)
295 {
296     intf_thread_t *  p_intf = GtkGetIntf( button );
297
298     if( p_intf )
299     {
300         if( p_intf->p_sys->p_input )
301         {
302             input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_FASTER );
303         }
304     }
305 }
306
307
308 void
309 on_toolbar_about_clicked               (GtkButton       *button,
310                                         gpointer         user_data)
311 {
312     intf_thread_t *p_intf = GtkGetIntf( button );
313     if (p_intf)
314     {// Toggle notebook
315         if (p_intf->p_sys->p_notebook)
316         {
317 //        if ( gtk_get_data(  GTK_WIDGET(p_intf->p_sys->p_notebook), "visible" ) )
318 //           gtk_widget_hide( GTK_WIDGET(p_intf->p_sys->p_notebook) );
319 //        else
320            gtk_widget_show( GTK_WIDGET(p_intf->p_sys->p_notebook) );
321         }
322         gdk_window_raise( p_intf->p_sys->p_window->window );
323     }
324 }
325
326
327 void
328 on_comboURL_entry_changed              (GtkEditable     *editable,
329                                         gpointer         user_data)
330 {
331     intf_thread_t * p_intf = GtkGetIntf( editable );
332
333     if (p_intf)
334     {
335         MediaURLOpenChanged( editable, NULL );
336     }
337 }
338
339
340 void
341 on_comboPrefs_entry_changed            (GtkEditable     *editable,
342                                         gpointer         user_data)
343 {
344     intf_thread_t * p_intf = GtkGetIntf( editable );
345
346     if (p_intf)
347     {
348         PreferencesURLOpenChanged( editable, NULL );
349     }
350 }
351