]> git.sesse.net Git - vlc/blob - modules/gui/familiar/familiar.c
2d1f968594e7b20f1a2a9faf1203a05ed5227852
[vlc] / modules / gui / familiar / familiar.c
1 /*****************************************************************************
2  * familiar.c : familiar plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: familiar.c,v 1.28 2003/01/27 17:41:01 ipkiss 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 <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/intf.h>
34
35 #include <gtk/gtk.h>
36
37 #ifdef HAVE_GPE_INIT_H
38 #include <gpe/init.h>
39 #endif
40
41 #include "callbacks.h"
42 #include "interface.h"
43 #include "support.h"
44 #include "familiar.h"
45
46 /*****************************************************************************
47  * Local prototypes.
48  *****************************************************************************/
49 static int  Open         ( vlc_object_t * );
50 static void Close        ( vlc_object_t * );
51 static void Run          ( intf_thread_t * );
52
53 void GtkAutoPlayFile     ( vlc_object_t * );
54 static int Manage        ( intf_thread_t *p_intf );
55 void E_(GtkDisplayDate)  ( GtkAdjustment *p_adj );
56 gint E_(GtkModeManage)   ( intf_thread_t * p_intf );
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 #define AUTOPLAYFILE_TEXT  N_("autoplay selected file")
62 #define AUTOPLAYFILE_LONGTEXT N_("automatically play a file when selected in the "\
63         "file selection list")
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68 vlc_module_begin();
69     add_category_hint( N_("Miscellaneous"), NULL );
70     add_bool( "familiar-autoplayfile", 1, GtkAutoPlayFile, AUTOPLAYFILE_TEXT, AUTOPLAYFILE_LONGTEXT );
71     set_description( _("Familiar Linux Gtk+ interface module") );
72     set_capability( "interface", 70 );
73     set_callbacks( Open, Close );
74 vlc_module_end();
75
76 /*****************************************************************************
77  * Open: initialize and create window
78  *****************************************************************************/
79 static int Open( vlc_object_t *p_this )
80 {
81     intf_thread_t *p_intf = (intf_thread_t *)p_this;
82
83     /* Allocate instance and initialize some members */
84     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
85     if( p_intf->p_sys == NULL )
86     {
87         msg_Err( p_intf, "out of memory" );
88         return VLC_ENOMEM;
89     }
90
91 #ifdef NEED_GTK_MAIN
92     msg_Dbg( p_intf, "Using gtk_main" );
93     p_intf->p_sys->p_gtk_main = module_Need( p_this, "gtk_main", "gtk" );
94     if( p_intf->p_sys->p_gtk_main == NULL )
95     {
96         free( p_intf->p_sys );
97         return VLC_ENOMOD;
98     }
99 #endif
100
101     /* Initialize Gtk+ thread */
102     p_intf->p_sys->p_input = NULL;
103
104     p_intf->p_sys->b_autoplayfile = 1;
105     p_intf->p_sys->b_playing = 0;
106     p_intf->p_sys->b_slider_free = 1;
107
108     p_intf->pf_run = Run;
109
110     return VLC_SUCCESS;
111 }
112
113 /*****************************************************************************
114  * Close: destroy interface window
115  *****************************************************************************/
116 static void Close( vlc_object_t *p_this )
117 {
118     intf_thread_t *p_intf = (intf_thread_t *)p_this;
119
120     if( p_intf->p_sys->p_input )
121     {
122         vlc_object_release( p_intf->p_sys->p_input );
123     }
124
125 #ifdef NEED_GTK_MAIN
126     msg_Dbg( p_intf, "Releasing gtk_main" );
127     module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
128 #endif
129
130     /* Destroy structure */
131     free( p_intf->p_sys );
132 }
133
134 /*****************************************************************************
135  * Run: Gtk+ thread
136  *****************************************************************************
137  * this part of the interface is in a separate thread so that we can call
138  * gtk_main() from within it without annoying the rest of the program.
139  *****************************************************************************/
140 static void Run( intf_thread_t *p_intf )
141 {
142 #ifndef NEED_GTK_MAIN
143     /* gtk_init needs to know the command line. We don't care, so we
144      * give it an empty one */
145     char  *p_args[] = { "" };
146     char **pp_args  = p_args;
147     int    i_args   = 1;
148     int    i_dummy;
149 #endif
150
151 #ifdef HAVE_GPE_INIT_H
152     /* Initialize GPE interface */
153     msg_Dbg( p_intf, "Starting familiar GPE interface" );
154     if (gpe_application_init(&i_args, &pp_args) == FALSE)
155         exit (1);
156 #else
157     gtk_set_locale ();
158  #ifndef NEED_GTK_MAIN
159     msg_Dbg( p_intf, "Starting familiar GTK+ interface" );
160     gtk_init( &i_args, &pp_args );
161  #else
162     /* Initialize Gtk+ */
163     msg_Dbg( p_intf, "Starting familiar GTK+ interface thread" );
164     gdk_threads_enter();
165  #endif
166 #endif
167
168     /* Create some useful widgets that will certainly be used */
169 // FIXME: magic path
170     add_pixmap_directory("share");
171     add_pixmap_directory("/usr/share/vlc");
172
173     p_intf->p_sys->p_window = create_familiar();
174     if (p_intf->p_sys->p_window == NULL)
175     {
176         msg_Err( p_intf, "unable to create familiar interface" );
177     }
178
179     /* Set the title of the main window */
180     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
181                           VOUT_TITLE " (Familiar Linux interface)");
182
183     p_intf->p_sys->p_notebook = GTK_NOTEBOOK( gtk_object_get_data(
184         GTK_OBJECT( p_intf->p_sys->p_window ), "notebook" ) );
185
186     /* Get the slider object */
187     p_intf->p_sys->p_slider = GTK_HSCALE( gtk_object_get_data(
188         GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) );
189     p_intf->p_sys->p_slider_label = GTK_LABEL( gtk_object_get_data(
190         GTK_OBJECT( p_intf->p_sys->p_window ), "slider_label" ) );
191
192     /* Connect the date display to the slider */
193 #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
194                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
195     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
196
197     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
198                          GTK_SIGNAL_FUNC( E_(GtkDisplayDate) ), NULL );
199     p_intf->p_sys->f_adj_oldvalue = 0;
200     p_intf->p_sys->i_adj_oldvalue = 0;
201 #undef P_SLIDER
202
203     p_intf->p_sys->p_clist = GTK_CLIST( gtk_object_get_data(
204         GTK_OBJECT( p_intf->p_sys->p_window ), "clistmedia" ) );
205     gtk_clist_set_column_visibility (GTK_CLIST (p_intf->p_sys->p_clist), 2, FALSE);
206     gtk_clist_set_column_visibility (GTK_CLIST (p_intf->p_sys->p_clist), 3, FALSE);
207     gtk_clist_set_column_visibility (GTK_CLIST (p_intf->p_sys->p_clist), 4, FALSE);
208     gtk_clist_column_titles_show (GTK_CLIST (p_intf->p_sys->p_clist));
209
210     /* Store p_intf to keep an eye on it */
211     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
212                          "p_intf", p_intf );
213     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
214                          "p_intf", p_intf );
215
216     /* Show the control window */
217     gtk_widget_show( p_intf->p_sys->p_window );
218     ReadDirectory(p_intf->p_sys->p_clist, "/mnt");
219
220 #ifdef NEED_GTK_MAIN
221     msg_Dbg( p_intf, "Manage GTK keyboard events using threads" );
222     while( !p_intf->b_die )
223     {
224         Manage( p_intf );
225
226         /* Sleep to avoid using all CPU - since some interfaces need to
227          * access keyboard events, a 100ms delay is a good compromise */
228         gdk_threads_leave();
229         if (p_intf->p_libvlc->i_cpu & CPU_CAPABILITY_FPU)
230             msleep( INTF_IDLE_SLEEP );
231         else
232             msleep( 1000 );
233         gdk_threads_enter();
234     }
235 #else
236     msg_Dbg( p_intf, "Manage GTK keyboard events using timeouts" );
237     /* Sleep to avoid using all CPU - since some interfaces needs to access
238      * keyboard events, a 1000ms delay is a good compromise */
239     if (p_intf->p_libvlc->i_cpu & CPU_CAPABILITY_FPU)
240         i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, (GtkFunction)Manage, p_intf );
241     else
242         i_dummy = gtk_timeout_add( 1000, (GtkFunction)Manage, p_intf );
243
244     /* Enter Gtk mode */
245     gtk_main();
246     /* Remove the timeout */
247     gtk_timeout_remove( i_dummy );
248 #endif
249
250     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
251 #ifdef NEED_GTK_MAIN
252     gdk_threads_leave();
253 #endif
254 }
255
256 /*****************************************************************************
257  * GtkAutoplayFile: Autoplay file depending on configuration settings
258  *****************************************************************************/
259 void GtkAutoPlayFile( vlc_object_t *p_this )
260 {
261     GtkWidget *cbautoplay;
262     intf_thread_t *p_intf;
263     int i_index;
264     vlc_list_t *p_list = vlc_list_find( p_this, VLC_OBJECT_INTF,
265                                         FIND_ANYWHERE );
266
267     for( i_index = 0; i_index < p_list->i_count; i_index++ )
268     {
269         p_intf = (intf_thread_t *)p_list->p_values[i_index].p_object ;
270
271         if( strcmp( MODULE_STRING, p_intf->p_module->psz_object_name ) )
272         {
273             continue;
274         }
275         cbautoplay = GTK_WIDGET( gtk_object_get_data(
276                             GTK_OBJECT( p_intf->p_sys->p_window ),
277                             "cbautoplay" ) );
278
279         if( !config_GetInt( p_this, "familiar-autoplayfile" ) )
280         {
281             p_intf->p_sys->b_autoplayfile = VLC_FALSE;
282         }
283         else
284         {
285             p_intf->p_sys->b_autoplayfile = VLC_TRUE;
286         }
287         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( cbautoplay ),
288                                       p_intf->p_sys->b_autoplayfile );
289     }
290     vlc_list_release( p_list );
291 }
292
293 /* following functions are local */
294
295 /*****************************************************************************
296  * Manage: manage main thread messages
297  *****************************************************************************
298  * In this function, called approx. 10 times a second, we check what the
299  * main program wanted to tell us.
300  *****************************************************************************/
301 static int Manage( intf_thread_t *p_intf )
302 {
303     vlc_mutex_lock( &p_intf->change_lock );
304
305     /* Update the input */
306     if( p_intf->p_sys->p_input == NULL )
307     {
308         p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
309                                                           FIND_ANYWHERE );
310     }
311     else if( p_intf->p_sys->p_input->b_dead )
312     {
313         vlc_object_release( p_intf->p_sys->p_input );
314         p_intf->p_sys->p_input = NULL;
315     }
316
317     if( p_intf->p_sys->p_input )
318     {
319         input_thread_t *p_input = p_intf->p_sys->p_input;
320
321         vlc_mutex_lock( &p_input->stream.stream_lock );
322         if( !p_input->b_die )
323         {
324             /* New input or stream map change */
325             if( p_input->stream.b_changed )
326             {
327                 E_(GtkModeManage)( p_intf );
328                 p_intf->p_sys->b_playing = 1;
329             }
330
331             /* Manage the slider */
332             if (p_intf->p_libvlc->i_cpu & CPU_CAPABILITY_FPU)
333             {
334                 /* Manage the slider for CPU_CAPABILITY_FPU hardware */ 
335                 if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
336                 {
337                     float newvalue = p_intf->p_sys->p_adj->value;
338
339 #define p_area p_input->stream.p_selected_area
340                     /* If the user hasn't touched the slider since the last time,
341                      * then the input can safely change it */
342                     if( newvalue == p_intf->p_sys->f_adj_oldvalue )
343                     {
344                         /* Update the value */
345                         p_intf->p_sys->p_adj->value =
346                         p_intf->p_sys->f_adj_oldvalue =
347                             ( 100. * p_area->i_tell ) / p_area->i_size;
348                         gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
349                                                  "value_changed" );
350                     }
351                     /* Otherwise, send message to the input if the user has
352                      * finished dragging the slider */
353                     else if( p_intf->p_sys->b_slider_free )
354                     {
355                         off_t i_seek = ( newvalue * p_area->i_size ) / 100;
356
357                         /* release the lock to be able to seek */
358                         vlc_mutex_unlock( &p_input->stream.stream_lock );
359                         input_Seek( p_input, i_seek, INPUT_SEEK_SET );
360                         vlc_mutex_lock( &p_input->stream.stream_lock );
361
362                         /* Update the old value */
363                         p_intf->p_sys->f_adj_oldvalue = newvalue;
364                     }
365 #undef p_area
366                 }
367             }
368             else
369             {
370                 /* Manage the slider without CPU_CAPABILITY_FPU hardware */
371                 if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
372                 {
373                     off_t newvalue = p_intf->p_sys->p_adj->value;
374
375 #define p_area p_input->stream.p_selected_area
376                     /* If the user hasn't touched the slider since the last time,
377                      * then the input can safely change it */
378                     if( newvalue == p_intf->p_sys->i_adj_oldvalue )
379                     {
380                         /* Update the value */
381                         p_intf->p_sys->p_adj->value =
382                         p_intf->p_sys->i_adj_oldvalue =
383                             ( 100 * p_area->i_tell ) / p_area->i_size;
384                         gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
385                                                  "value_changed" );
386                     }
387                     /* Otherwise, send message to the input if the user has
388                      * finished dragging the slider */
389                     else if( p_intf->p_sys->b_slider_free )
390                     {
391                         off_t i_seek = ( newvalue * p_area->i_size ) / 100;
392
393                         /* release the lock to be able to seek */
394                         vlc_mutex_unlock( &p_input->stream.stream_lock );
395                         input_Seek( p_input, i_seek, INPUT_SEEK_SET );
396                         vlc_mutex_lock( &p_input->stream.stream_lock );
397
398                         /* Update the old value */
399                         p_intf->p_sys->i_adj_oldvalue = newvalue;
400                     }
401 #undef p_area
402                 }
403             }
404         }
405         vlc_mutex_unlock( &p_input->stream.stream_lock );
406     }
407     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
408     {
409         E_(GtkModeManage)( p_intf );
410         p_intf->p_sys->b_playing = 0;
411     }
412
413 #ifndef NEED_GTK_MAIN
414     if( p_intf->b_die )
415     {
416         vlc_mutex_unlock( &p_intf->change_lock );
417
418         /* Prepare to die, young Skywalker */
419         gtk_main_quit();
420
421         return FALSE;
422     }
423 #endif
424
425     vlc_mutex_unlock( &p_intf->change_lock );
426
427     return TRUE;
428 }
429
430 /*****************************************************************************
431  * GtkDisplayDate: display stream date
432  *****************************************************************************
433  * This function displays the current date related to the position in
434  * the stream. It is called whenever the slider changes its value.
435  * The lock has to be taken before you call the function.
436  *****************************************************************************/
437 void E_(GtkDisplayDate)( GtkAdjustment *p_adj )
438 {
439     intf_thread_t *p_intf;
440
441     p_intf = gtk_object_get_data( GTK_OBJECT( p_adj ), "p_intf" );
442
443     if( p_intf->p_sys->p_input )
444     {
445 #define p_area p_intf->p_sys->p_input->stream.p_selected_area
446         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
447
448         gtk_label_set_text( GTK_LABEL( p_intf->p_sys->p_slider_label ),
449                         input_OffsetToTime( p_intf->p_sys->p_input, psz_time,
450                                    ( p_area->i_size * p_adj->value ) / 100 ) );
451 #undef p_area
452      }
453 }
454
455 /*****************************************************************************
456  * GtkModeManage: actualize the aspect of the interface whenever the input
457  *                changes.
458  *****************************************************************************
459  * The lock has to be taken before you call the function.
460  *****************************************************************************/
461 gint E_(GtkModeManage)( intf_thread_t * p_intf )
462 {
463     GtkWidget *     p_slider;
464     vlc_bool_t      b_control;
465
466 #define GETWIDGET( ptr, name ) GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( \
467                            p_intf->p_sys->ptr ) , ( name ) ) )
468     /* hide slider */
469     p_slider = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
470                            p_intf->p_sys->p_window ), "slider" ) );
471     gtk_widget_hide( GTK_WIDGET( p_slider ) );
472
473     /* controls unavailable */
474     b_control = 0;
475
476     /* show the box related to current input mode */
477     if( p_intf->p_sys->p_input )
478     {
479         /* initialize and show slider for seekable streams */
480         if( p_intf->p_sys->p_input->stream.b_seekable )
481         {
482             if (p_intf->p_libvlc->i_cpu & CPU_CAPABILITY_FPU)
483                 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue = 0;
484             else
485                 p_intf->p_sys->p_adj->value = p_intf->p_sys->i_adj_oldvalue = 0;
486             gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
487                                      "value_changed" );
488             gtk_widget_show( GTK_WIDGET( p_slider ) );
489         }
490
491         /* control buttons for free pace streams */
492         b_control = p_intf->p_sys->p_input->stream.b_pace_control;
493
494         p_intf->p_sys->p_input->stream.b_changed = 0;
495         msg_Dbg( p_intf, "stream has changed, refreshing interface" );
496     }
497
498     /* set control items */
499     gtk_widget_set_sensitive( GETWIDGET(p_window, "toolbar_rewind"), b_control );
500     gtk_widget_set_sensitive( GETWIDGET(p_window, "toolbar_pause"), b_control );
501     gtk_widget_set_sensitive( GETWIDGET(p_window, "toolbar_forward"), b_control );
502
503 #undef GETWIDGET
504     return TRUE;
505 }
506