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