]> git.sesse.net Git - vlc/blob - modules/access/dvdnav.c
Removed es_format_t::i_aspect.
[vlc] / modules / access / dvdnav.c
1 /*****************************************************************************
2  * dvdnav.c: DVD module using the dvdnav library.
3  *****************************************************************************
4  * Copyright (C) 2004-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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 along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <assert.h>
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
37 #include <vlc_demux.h>
38 #include <vlc_charset.h>
39
40 #include <vlc_dialog.h>
41
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>
44 #endif
45 #ifdef HAVE_SYS_TYPES_H
46 #   include <sys/types.h>
47 #endif
48 #ifdef HAVE_SYS_STAT_H
49 #   include <sys/stat.h>
50 #endif
51 #ifdef HAVE_FCNTL_H
52 #   include <fcntl.h>
53 #endif
54
55 #include <vlc_keys.h>
56 #include <vlc_iso_lang.h>
57
58 /* FIXME we should find a better way than including that */
59 #include "../../src/text/iso-639_def.h"
60
61
62 #include <dvdnav/dvdnav.h>
63
64 #include "../demux/ps.h"
65
66 /*****************************************************************************
67  * Module descriptor
68  *****************************************************************************/
69 #define ANGLE_TEXT N_("DVD angle")
70 #define ANGLE_LONGTEXT N_( \
71      "Default DVD angle." )
72
73 #define CACHING_TEXT N_("Caching value in ms")
74 #define CACHING_LONGTEXT N_( \
75     "Caching value for DVDs. This "\
76     "value should be set in milliseconds." )
77 #define MENU_TEXT N_("Start directly in menu")
78 #define MENU_LONGTEXT N_( \
79     "Start the DVD directly in the main menu. This "\
80     "will try to skip all the useless warning introductions." )
81
82 #define LANGUAGE_DEFAULT ("en")
83
84 static int  Open ( vlc_object_t * );
85 static void Close( vlc_object_t * );
86
87 vlc_module_begin ()
88     set_shortname( N_("DVD with menus") )
89     set_description( N_("DVDnav Input") )
90     set_category( CAT_INPUT )
91     set_subcategory( SUBCAT_INPUT_ACCESS )
92     add_integer( "dvdnav-angle", 1, NULL, ANGLE_TEXT,
93         ANGLE_LONGTEXT, false )
94     add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
95         CACHING_TEXT, CACHING_LONGTEXT, true )
96     add_bool( "dvdnav-menu", true, NULL,
97         MENU_TEXT, MENU_LONGTEXT, false )
98     set_capability( "access_demux", 5 )
99     add_shortcut( "dvd" )
100     add_shortcut( "dvdnav" )
101     add_shortcut( "file" )
102     set_callbacks( Open, Close )
103 vlc_module_end ()
104
105 /* Shall we use libdvdnav's read ahead cache? */
106 #define DVD_READ_CACHE 1
107
108 /*****************************************************************************
109  * Local prototypes
110  *****************************************************************************/
111 struct demux_sys_t
112 {
113     dvdnav_t    *dvdnav;
114
115     /* */
116     bool        b_reset_pcr;
117
118     struct
119     {
120         bool         b_created;
121         bool         b_enabled;
122         vlc_mutex_t  lock;
123         vlc_timer_t  timer;
124     } still;
125
126     /* track */
127     ps_track_t  tk[PS_TK_COUNT];
128     int         i_mux_rate;
129
130     /* for spu variables */
131     input_thread_t *p_input;
132
133     /* event */
134     vlc_object_t *p_vout;
135
136     /* palette for menus */
137     uint32_t clut[16];
138     uint8_t  palette[4][4];
139     bool b_spu_change;
140
141     /* */
142     int           i_title;
143     input_title_t **title;
144
145     /* lenght of program group chain */
146     mtime_t     i_pgc_length;
147 };
148
149 static int Control( demux_t *, int, va_list );
150 static int Demux( demux_t * );
151 static int DemuxBlock( demux_t *, const uint8_t *, int );
152
153 static void DemuxTitles( demux_t * );
154 static void ESSubtitleUpdate( demux_t * );
155 static void ButtonUpdate( demux_t *, bool );
156
157 static void ESNew( demux_t *, int );
158 static int ProbeDVD( demux_t *, char * );
159
160 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
161
162 static int ControlInternal( demux_t *, int, ... );
163
164 static void StillTimer( void * );
165
166 static int EventKey( vlc_object_t *, char const *,
167                      vlc_value_t, vlc_value_t, void * );
168 static int EventMouse( vlc_object_t *, char const *,
169                        vlc_value_t, vlc_value_t, void * );
170 static int EventIntf( vlc_object_t *, char const *,
171                       vlc_value_t, vlc_value_t, void * );
172
173 /*****************************************************************************
174  * DemuxOpen:
175  *****************************************************************************/
176 static int Open( vlc_object_t *p_this )
177 {
178     demux_t     *p_demux = (demux_t*)p_this;
179     demux_sys_t *p_sys;
180     dvdnav_t    *p_dvdnav;
181     int         i_angle;
182     char        *psz_name;
183     char        *psz_code;
184
185     if( !p_demux->psz_path || !*p_demux->psz_path )
186     {
187         /* Only when selected */
188         if( !p_this->b_force ) return VLC_EGENERIC;
189
190         psz_name = var_CreateGetString( p_this, "dvd" );
191         if( !psz_name )
192         {
193             psz_name = strdup("");
194         }
195     }
196     else
197         psz_name = ToLocaleDup( p_demux->psz_path );
198
199 #ifdef WIN32
200     /* Remove trailing backslash, otherwise dvdnav_open will fail */
201     if( *psz_name && *(psz_name + strlen(psz_name) - 1) == '\\' )
202     {
203         *(psz_name + strlen(psz_name) - 1) = '\0';
204     }
205 #endif
206
207     /* Try some simple probing to avoid going through dvdnav_open too often */
208     if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
209     {
210         free( psz_name );
211         return VLC_EGENERIC;
212     }
213
214     /* Open dvdnav */
215     if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
216     {
217         msg_Warn( p_demux, "cannot open dvdnav" );
218         free( psz_name );
219         return VLC_EGENERIC;
220     }
221     free( psz_name );
222
223     /* Fill p_demux field */
224     DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
225     p_sys->dvdnav = p_dvdnav;
226     p_sys->b_reset_pcr = false;
227
228     ps_track_init( p_sys->tk );
229     p_sys->i_mux_rate = 0;
230     p_sys->i_pgc_length = 0;
231     p_sys->b_spu_change = false;
232
233     if( 1 )
234     {
235         // Hack for libdvdnav CVS.
236         // Without it dvdnav_get_number_of_titles() fails.
237         // Remove when fixed in libdvdnav CVS.
238         uint8_t buffer[DVD_VIDEO_LB_LEN];
239         int i_event, i_len;
240
241         if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
242               == DVDNAV_STATUS_ERR )
243         {
244             msg_Warn( p_demux, "dvdnav_get_next_block failed" );
245         }
246
247         dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
248     }
249
250     /* Configure dvdnav */
251     if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
252           DVDNAV_STATUS_OK )
253     {
254         msg_Warn( p_demux, "cannot set read-a-head flag" );
255     }
256
257     if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
258           DVDNAV_STATUS_OK )
259     {
260         msg_Warn( p_demux, "cannot set PGC positioning flag" );
261     }
262
263     /* Set menu language
264      * XXX A menu-language may be better than sub-language */
265     psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
266     if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
267         DVDNAV_STATUS_OK )
268     {
269         msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
270                   psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
271         /* We try to fall back to 'en' */
272         if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
273             dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
274     }
275     free( psz_code );
276
277     /* Set audio language */
278     psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
279     if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
280         DVDNAV_STATUS_OK )
281     {
282         msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
283                   psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
284         /* We try to fall back to 'en' */
285         if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
286             dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
287     }
288     free( psz_code );
289
290     /* Set spu language */
291     psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
292     if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
293         DVDNAV_STATUS_OK )
294     {
295         msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
296                   psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
297         /* We try to fall back to 'en' */
298         if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
299             dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
300     }
301     free( psz_code );
302
303     DemuxTitles( p_demux );
304
305     if( var_CreateGetBool( p_demux, "dvdnav-menu" ) )
306     {
307         msg_Dbg( p_demux, "trying to go to dvd menu" );
308
309         if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
310         {
311             msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
312             dialog_Fatal( p_demux, _("Playback failure"), "%s",
313                             _("VLC cannot set the DVD's title. It possibly "
314                               "cannot decrypt the entire disc.") );
315             dvdnav_close( p_sys->dvdnav );
316             free( p_sys );
317             return VLC_EGENERIC;
318         }
319
320         if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
321             DVDNAV_STATUS_OK )
322         {
323             /* Try going to menu root */
324             if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
325                 DVDNAV_STATUS_OK )
326                     msg_Warn( p_demux, "cannot go to dvd menu" );
327         }
328     }
329
330     i_angle = var_CreateGetInteger( p_demux, "dvdnav-angle" );
331     if( i_angle <= 0 ) i_angle = 1;
332
333     /* Update default_pts to a suitable value for dvdnav access */
334     var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
335
336     /* FIXME hack hack hack hack FIXME */
337     /* Get p_input and create variable */
338     p_sys->p_input = demux_GetParentInput( p_demux );
339     var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
340     var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
341     var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
342     var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
343     var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
344     var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
345     var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
346     var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
347
348     /* catch all key event */
349     var_AddCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
350     /* catch vout creation event */
351     var_AddCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
352
353     p_sys->still.b_enabled = false;
354     vlc_mutex_init( &p_sys->still.lock );
355     if( !vlc_timer_create( &p_sys->still.timer, StillTimer, p_sys ) )
356         p_sys->still.b_created = true;
357
358     return VLC_SUCCESS;
359 }
360
361 /*****************************************************************************
362  * Close:
363  *****************************************************************************/
364 static void Close( vlc_object_t *p_this )
365 {
366     demux_t     *p_demux = (demux_t*)p_this;
367     demux_sys_t *p_sys = p_demux->p_sys;
368     int i;
369
370     /* Stop vout event handler */
371     var_DelCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
372     if( p_sys->p_vout != NULL )
373     {   /* Should not happen, but better be safe than sorry. */
374         msg_Warn( p_sys->p_vout, "removing dangling mouse DVD callbacks" );
375         var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
376         var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
377     }
378
379     /* Stop key event handler (FIXME: should really be per-vout too) */
380     var_DelCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
381
382     /* Stop still image handler */
383     if( p_sys->still.b_created )
384         vlc_timer_destroy( p_sys->still.timer );
385     vlc_mutex_destroy( &p_sys->still.lock );
386
387     var_Destroy( p_sys->p_input, "highlight-mutex" );
388     var_Destroy( p_sys->p_input, "highlight" );
389     var_Destroy( p_sys->p_input, "x-start" );
390     var_Destroy( p_sys->p_input, "x-end" );
391     var_Destroy( p_sys->p_input, "y-start" );
392     var_Destroy( p_sys->p_input, "y-end" );
393     var_Destroy( p_sys->p_input, "color" );
394     var_Destroy( p_sys->p_input, "menu-palette" );
395
396     vlc_object_release( p_sys->p_input );
397
398     for( i = 0; i < PS_TK_COUNT; i++ )
399     {
400         ps_track_t *tk = &p_sys->tk[i];
401         if( tk->b_seen )
402         {
403             es_format_Clean( &tk->fmt );
404             if( tk->es ) es_out_Del( p_demux->out, tk->es );
405         }
406     }
407
408     dvdnav_close( p_sys->dvdnav );
409     free( p_sys );
410 }
411
412 /*****************************************************************************
413  * Control:
414  *****************************************************************************/
415 static int Control( demux_t *p_demux, int i_query, va_list args )
416 {
417     demux_sys_t *p_sys = p_demux->p_sys;
418     input_title_t ***ppp_title;
419     int i;
420
421     switch( i_query )
422     {
423         case DEMUX_SET_POSITION:
424         case DEMUX_GET_POSITION:
425         case DEMUX_GET_TIME:
426         case DEMUX_GET_LENGTH:
427         {
428             uint32_t pos, len;
429             if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
430                   DVDNAV_STATUS_OK || len == 0 )
431             {
432                 return VLC_EGENERIC;
433             }
434
435             switch( i_query )
436             {
437             case DEMUX_GET_POSITION:
438                 *va_arg( args, double* ) = (double)pos / (double)len;
439                 return VLC_SUCCESS;
440
441             case DEMUX_SET_POSITION:
442                 pos = va_arg( args, double ) * len;
443                 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
444                       DVDNAV_STATUS_OK )
445                 {
446                     return VLC_SUCCESS;
447                 }
448                 break;
449
450             case DEMUX_GET_TIME:
451                 if( p_sys->i_pgc_length > 0 )
452                 {
453                     *va_arg( args, int64_t * ) = p_sys->i_pgc_length*pos/len;
454                     return VLC_SUCCESS;
455                 }
456                 break;
457
458             case DEMUX_GET_LENGTH:
459                 if( p_sys->i_pgc_length > 0 )
460                 {
461                     *va_arg( args, int64_t * ) = (int64_t)p_sys->i_pgc_length;
462                     return VLC_SUCCESS;
463                 }
464                 break;
465             }
466             return VLC_EGENERIC;
467         }
468
469         /* Special for access_demux */
470         case DEMUX_CAN_PAUSE:
471         case DEMUX_CAN_SEEK:
472         case DEMUX_CAN_CONTROL_PACE:
473             /* TODO */
474             *va_arg( args, bool * ) = true;
475             return VLC_SUCCESS;
476
477         case DEMUX_SET_PAUSE_STATE:
478             return VLC_SUCCESS;
479
480         case DEMUX_GET_TITLE_INFO:
481             ppp_title = va_arg( args, input_title_t*** );
482             *va_arg( args, int* ) = p_sys->i_title;
483             *va_arg( args, int* ) = 0; /* Title offset */
484             *va_arg( args, int* ) = 1; /* Chapter offset */
485
486             /* Duplicate title infos */
487             *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
488             for( i = 0; i < p_sys->i_title; i++ )
489             {
490                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
491             }
492             return VLC_SUCCESS;
493
494         case DEMUX_SET_TITLE:
495             i = (int)va_arg( args, int );
496             if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
497                   != DVDNAV_STATUS_OK ) ||
498                 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
499                   != DVDNAV_STATUS_OK ) )
500             {
501                 msg_Warn( p_demux, "cannot set title/chapter" );
502                 return VLC_EGENERIC;
503             }
504             p_demux->info.i_update |=
505                 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
506             p_demux->info.i_title = i;
507             p_demux->info.i_seekpoint = 0;
508             return VLC_SUCCESS;
509
510         case DEMUX_SET_SEEKPOINT:
511             i = va_arg( args, int );
512             if( p_demux->info.i_title == 0 )
513             {
514                 static const int argtab[] = {
515                     DVD_MENU_Escape,
516                     DVD_MENU_Root,
517                     DVD_MENU_Title,
518                     DVD_MENU_Part,
519                     DVD_MENU_Subpicture,
520                     DVD_MENU_Audio,
521                     DVD_MENU_Angle
522                 };
523                 enum { numargs = sizeof(argtab)/sizeof(int) };
524                 if( (unsigned)i >= numargs || DVDNAV_STATUS_OK !=
525                            dvdnav_menu_call(p_sys->dvdnav,argtab[i]) )
526                     return VLC_EGENERIC;
527             }
528             else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
529                                        i + 1 ) != DVDNAV_STATUS_OK )
530             {
531                 msg_Warn( p_demux, "cannot set title/chapter" );
532                 return VLC_EGENERIC;
533             }
534             p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
535             p_demux->info.i_seekpoint = i;
536             return VLC_SUCCESS;
537
538         case DEMUX_GET_PTS_DELAY:
539             *va_arg( args, int64_t * )
540                  = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
541             return VLC_SUCCESS;
542
543         case DEMUX_GET_META:
544         {
545             const char *title_name = NULL;
546
547             dvdnav_get_title_string(p_sys->dvdnav, &title_name);
548             if( (NULL != title_name) && ('\0' != title_name[0]) )
549             {
550                 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
551                 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
552                 return VLC_SUCCESS;
553             }
554             return VLC_EGENERIC;
555         }
556
557         /* TODO implement others */
558         default:
559             return VLC_EGENERIC;
560     }
561 }
562
563 static int ControlInternal( demux_t *p_demux, int i_query, ... )
564 {
565     va_list args;
566     int     i_result;
567
568     va_start( args, i_query );
569     i_result = Control( p_demux, i_query, args );
570     va_end( args );
571
572     return i_result;
573 }
574 /*****************************************************************************
575  * Demux:
576  *****************************************************************************/
577 static int Demux( demux_t *p_demux )
578 {
579     demux_sys_t *p_sys = p_demux->p_sys;
580
581     uint8_t buffer[DVD_VIDEO_LB_LEN];
582     uint8_t *packet = buffer;
583     int i_event;
584     int i_len;
585
586 #if DVD_READ_CACHE
587     if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
588         == DVDNAV_STATUS_ERR )
589 #else
590     if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
591         == DVDNAV_STATUS_ERR )
592 #endif
593     {
594         msg_Warn( p_demux, "cannot get next block (%s)",
595                   dvdnav_err_to_string( p_sys->dvdnav ) );
596         if( p_demux->info.i_title == 0 )
597         {
598             msg_Dbg( p_demux, "jumping to first title" );
599             return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
600         }
601         return -1;
602     }
603
604     switch( i_event )
605     {
606     case DVDNAV_BLOCK_OK:   /* mpeg block */
607         vlc_mutex_lock( &p_sys->still.lock );
608         p_sys->still.b_enabled = false;
609         vlc_mutex_unlock( &p_sys->still.lock );
610         if( p_sys->b_reset_pcr )
611         {
612             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
613             p_sys->b_reset_pcr = false;
614         }
615         DemuxBlock( p_demux, packet, i_len );
616         break;
617
618     case DVDNAV_NOP:    /* Nothing */
619         msg_Dbg( p_demux, "DVDNAV_NOP" );
620         break;
621
622     case DVDNAV_STILL_FRAME:
623     {
624         dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
625         bool b_still_init = false;
626
627         vlc_mutex_lock( &p_sys->still.lock );
628         if( !p_sys->still.b_enabled )
629         {
630             msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
631             msg_Dbg( p_demux, "     - length=0x%x", event->length );
632             p_sys->still.b_enabled = true;
633
634             if( event->length != 0xff && p_sys->still.b_created )
635             {
636                 mtime_t delay = event->length * CLOCK_FREQ;
637                 vlc_timer_schedule( p_sys->still.timer, false, delay, 0 );
638             }
639
640             b_still_init = true;
641         }
642         vlc_mutex_unlock( &p_sys->still.lock );
643
644         if( b_still_init )
645         {
646             /* We send a dummy mpeg2 end of sequence to force still frame display */
647             static const uint8_t buffer[] = {
648                 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
649                 0x80, 0x00, 0x00,
650                 0x00, 0x00, 0x01, 0xB7,
651             };
652             DemuxBlock( p_demux, buffer, sizeof(buffer) );
653
654             bool b_empty;
655             es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
656             p_sys->b_reset_pcr = true;
657         }
658         msleep( 40000 );
659         break;
660     }
661
662     case DVDNAV_SPU_CLUT_CHANGE:
663     {
664         int i;
665
666         msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
667         /* Update color lookup table (16 *uint32_t in packet) */
668         memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
669
670         /* HACK to get the SPU tracks registered in the right order */
671         for( i = 0; i < 0x1f; i++ )
672         {
673             if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
674                 ESNew( p_demux, 0xbd20 + i );
675         }
676         /* END HACK */
677         break;
678     }
679
680     case DVDNAV_SPU_STREAM_CHANGE:
681     {
682         dvdnav_spu_stream_change_event_t *event =
683             (dvdnav_spu_stream_change_event_t*)packet;
684         int i;
685
686         msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
687         msg_Dbg( p_demux, "     - physical_wide=%d",
688                  event->physical_wide );
689         msg_Dbg( p_demux, "     - physical_letterbox=%d",
690                  event->physical_letterbox);
691         msg_Dbg( p_demux, "     - physical_pan_scan=%d",
692                  event->physical_pan_scan );
693
694         ESSubtitleUpdate( p_demux );
695         p_sys->b_spu_change = true;
696
697         /* HACK to get the SPU tracks registered in the right order */
698         for( i = 0; i < 0x1f; i++ )
699         {
700             if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
701                 ESNew( p_demux, 0xbd20 + i );
702         }
703         /* END HACK */
704         break;
705     }
706
707     case DVDNAV_AUDIO_STREAM_CHANGE:
708     {
709         dvdnav_audio_stream_change_event_t *event =
710             (dvdnav_audio_stream_change_event_t*)packet;
711         msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
712         msg_Dbg( p_demux, "     - physical=%d", event->physical );
713         /* TODO */
714         break;
715     }
716
717     case DVDNAV_VTS_CHANGE:
718     {
719         int32_t i_title = 0;
720         int32_t i_part  = 0;
721         int i;
722
723         dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
724         msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
725         msg_Dbg( p_demux, "     - vtsN=%d", event->new_vtsN );
726         msg_Dbg( p_demux, "     - domain=%d", event->new_domain );
727
728         /* reset PCR */
729         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
730
731         for( i = 0; i < PS_TK_COUNT; i++ )
732         {
733             ps_track_t *tk = &p_sys->tk[i];
734             if( tk->b_seen )
735             {
736                 es_format_Clean( &tk->fmt );
737                 if( tk->es ) es_out_Del( p_demux->out, tk->es );
738             }
739             tk->b_seen = false;
740         }
741
742         if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
743                                        &i_part ) == DVDNAV_STATUS_OK )
744         {
745             if( i_title >= 0 && i_title < p_sys->i_title &&
746                 p_demux->info.i_title != i_title )
747             {
748                 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
749                 p_demux->info.i_title = i_title;
750             }
751         }
752         break;
753     }
754
755     case DVDNAV_CELL_CHANGE:
756     {
757         int32_t i_title = 0;
758         int32_t i_part  = 0;
759
760         dvdnav_cell_change_event_t *event =
761             (dvdnav_cell_change_event_t*)packet;
762         msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
763         msg_Dbg( p_demux, "     - cellN=%d", event->cellN );
764         msg_Dbg( p_demux, "     - pgN=%d", event->pgN );
765         msg_Dbg( p_demux, "     - cell_length=%"PRId64, event->cell_length );
766         msg_Dbg( p_demux, "     - pg_length=%"PRId64, event->pg_length );
767         msg_Dbg( p_demux, "     - pgc_length=%"PRId64, event->pgc_length );
768         msg_Dbg( p_demux, "     - cell_start=%"PRId64, event->cell_start );
769         msg_Dbg( p_demux, "     - pg_start=%"PRId64, event->pg_start );
770
771         /* Store the lenght in time of the current PGC */
772         p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
773
774         /* FIXME is it correct or there is better way to know chapter change */
775         if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
776                                        &i_part ) == DVDNAV_STATUS_OK )
777         {
778             if( i_title >= 0 && i_title < p_sys->i_title &&
779                 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
780                 p_demux->info.i_seekpoint != i_part - 1 )
781             {
782                 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
783                 p_demux->info.i_seekpoint = i_part - 1;
784             }
785         }
786         break;
787     }
788
789     case DVDNAV_NAV_PACKET:
790     {
791 #ifdef DVDNAV_DEBUG
792         msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
793 #endif
794         /* A lot of thing to do here :
795          *  - handle packet
796          *  - fetch pts (for time display)
797          *  - ...
798          */
799         DemuxBlock( p_demux, packet, i_len );
800         if( p_sys->b_spu_change )
801         {
802             ButtonUpdate( p_demux, false );
803             p_sys->b_spu_change = false;
804         }
805         break;
806     }
807
808     case DVDNAV_STOP:   /* EOF */
809         msg_Dbg( p_demux, "DVDNAV_STOP" );
810
811 #if DVD_READ_CACHE
812         dvdnav_free_cache_block( p_sys->dvdnav, packet );
813 #endif
814         return 0;
815
816     case DVDNAV_HIGHLIGHT:
817     {
818         dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
819         msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
820         msg_Dbg( p_demux, "     - display=%d", event->display );
821         msg_Dbg( p_demux, "     - buttonN=%d", event->buttonN );
822         ButtonUpdate( p_demux, false );
823         break;
824     }
825
826     case DVDNAV_HOP_CHANNEL:
827         msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
828         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
829         break;
830
831     case DVDNAV_WAIT:
832         msg_Dbg( p_demux, "DVDNAV_WAIT" );
833
834         bool b_empty;
835         es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
836         if( !b_empty )
837         {
838             msleep( 40*1000 );
839         }
840         else
841         {
842             dvdnav_wait_skip( p_sys->dvdnav );
843             p_sys->b_reset_pcr = true;
844         }
845         break;
846
847     default:
848         msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
849         break;
850     }
851
852 #if DVD_READ_CACHE
853     dvdnav_free_cache_block( p_sys->dvdnav, packet );
854 #endif
855
856     return 1;
857 }
858
859 /* Get a 2 char code
860  * FIXME: partiallyy duplicated from src/input/es_out.c
861  */
862 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
863 {
864     const iso639_lang_t *pl;
865     char *psz_lang;
866     char *p;
867
868     psz_lang = var_CreateGetString( p_demux, psz_var );
869     if( !psz_lang )
870         return strdup(LANGUAGE_DEFAULT);
871
872     /* XXX: we will use only the first value
873      * (and ignore other ones in case of a list) */
874     if( ( p = strchr( psz_lang, ',' ) ) )
875         *p = '\0';
876
877     for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
878     {
879         if( *psz_lang == '\0' )
880             continue;
881         if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
882             !strcasecmp( pl->psz_native_name, psz_lang ) ||
883             !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
884             !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
885             !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
886             break;
887     }
888
889     free( psz_lang );
890
891     if( pl->psz_eng_name != NULL )
892         return strdup( pl->psz_iso639_1 );
893
894     return strdup(LANGUAGE_DEFAULT);
895 }
896
897 static void DemuxTitles( demux_t *p_demux )
898 {
899     demux_sys_t *p_sys = p_demux->p_sys;
900     input_title_t *t;
901     seekpoint_t *s;
902     int32_t i_titles;
903     int i;
904
905     /* Menu */
906     t = vlc_input_title_New();
907     t->b_menu = true;
908     t->psz_name = strdup( "DVD Menu" );
909
910     s = vlc_seekpoint_New();
911     s->psz_name = strdup( "Resume" );
912     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
913
914     s = vlc_seekpoint_New();
915     s->psz_name = strdup( "Root" );
916     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
917
918     s = vlc_seekpoint_New();
919     s->psz_name = strdup( "Title" );
920     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
921
922     s = vlc_seekpoint_New();
923     s->psz_name = strdup( "Chapter" );
924     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
925
926     s = vlc_seekpoint_New();
927     s->psz_name = strdup( "Subtitle" );
928     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
929
930     s = vlc_seekpoint_New();
931     s->psz_name = strdup( "Audio" );
932     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
933
934     s = vlc_seekpoint_New();
935     s->psz_name = strdup( "Angle" );
936     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
937
938     TAB_APPEND( p_sys->i_title, p_sys->title, t );
939
940     /* Find out number of titles/chapters */
941     dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
942     for( i = 1; i <= i_titles; i++ )
943     {
944         int32_t i_chapters = 0;
945         int j;
946
947         dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
948
949         t = vlc_input_title_New();
950         for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
951         {
952             s = vlc_seekpoint_New();
953             TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
954         }
955
956         TAB_APPEND( p_sys->i_title, p_sys->title, t );
957     }
958 }
959
960 /*****************************************************************************
961  * Update functions:
962  *****************************************************************************/
963 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
964 {
965     demux_sys_t *p_sys = p_demux->p_sys;
966     vlc_value_t val;
967     int32_t i_title, i_part;
968
969     dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
970
971     if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
972     {
973         vlc_mutex_t *p_mutex = val.p_address;
974         dvdnav_highlight_area_t hl;
975         int32_t i_button;
976         bool    b_button_ok;
977
978         if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
979             != DVDNAV_STATUS_OK )
980         {
981             msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
982             return;
983         }
984
985         b_button_ok = false;
986         if( i_button > 0 && i_title ==  0 )
987         {
988             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
989
990             b_button_ok = DVDNAV_STATUS_OK ==
991                       dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
992         }
993
994         if( b_button_ok )
995         {
996             int i;
997             for( i = 0; i < 4; i++ )
998             {
999                 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1000                 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1001
1002                 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1003                 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1004                 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1005                 p_sys->palette[i][3] = i_alpha;
1006             }
1007
1008             vlc_mutex_lock( p_mutex );
1009             var_SetInteger( p_sys->p_input, "x-start", hl.sx );
1010             var_SetInteger( p_sys->p_input, "x-end",  hl.ex );
1011             var_SetInteger( p_sys->p_input, "y-start", hl.sy );
1012             var_SetInteger( p_sys->p_input, "y-end", hl.ey );
1013
1014             var_SetAddress( p_sys->p_input, "menu-palette", p_sys->palette );
1015
1016             var_SetBool( p_sys->p_input, "highlight", true );
1017             vlc_mutex_unlock( p_mutex );
1018
1019             msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1020         }
1021         else
1022         {
1023             msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1024                      i_button, i_title );
1025
1026             /* Show all */
1027             vlc_mutex_lock( p_mutex );
1028             var_SetBool( p_sys->p_input, "highlight", false );
1029             vlc_mutex_unlock( p_mutex );
1030         }
1031     }
1032 }
1033
1034 static void ESSubtitleUpdate( demux_t *p_demux )
1035 {
1036     demux_sys_t *p_sys = p_demux->p_sys;
1037     int         i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1038     int32_t i_title, i_part;
1039
1040     ButtonUpdate( p_demux, false );
1041
1042     dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1043     if( i_title > 0 ) return;
1044
1045     if( i_spu >= 0 && i_spu <= 0x1f )
1046     {
1047         ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1048
1049         ESNew( p_demux, 0xbd20 + i_spu );
1050
1051         /* be sure to unselect it (reset) */
1052         es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1053                         (bool)false );
1054
1055         /* now select it */
1056         es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1057     }
1058     else
1059     {
1060         for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1061         {
1062             ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1063             if( tk->b_seen )
1064             {
1065                 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1066                                 (bool)false );
1067             }
1068         }
1069     }
1070 }
1071
1072 /*****************************************************************************
1073  * DemuxBlock: demux a given block
1074  *****************************************************************************/
1075 static int DemuxBlock( demux_t *p_demux, const uint8_t *pkt, int i_pkt )
1076 {
1077     demux_sys_t *p_sys = p_demux->p_sys;
1078     const uint8_t     *p = pkt;
1079
1080     while( p < &pkt[i_pkt] )
1081     {
1082         int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1083         block_t *p_pkt;
1084         if( i_size <= 0 )
1085         {
1086             break;
1087         }
1088
1089         /* Create a block */
1090         p_pkt = block_New( p_demux, i_size );
1091         memcpy( p_pkt->p_buffer, p, i_size);
1092
1093         /* Parse it and send it */
1094         switch( 0x100 | p[3] )
1095         {
1096         case 0x1b9:
1097         case 0x1bb:
1098         case 0x1bc:
1099 #ifdef DVDNAV_DEBUG
1100             if( p[3] == 0xbc )
1101             {
1102                 msg_Warn( p_demux, "received a PSM packet" );
1103             }
1104             else if( p[3] == 0xbb )
1105             {
1106                 msg_Warn( p_demux, "received a SYSTEM packet" );
1107             }
1108 #endif
1109             block_Release( p_pkt );
1110             break;
1111
1112         case 0x1ba:
1113         {
1114             int64_t i_scr;
1115             int i_mux_rate;
1116             if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1117             {
1118                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1119                 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1120             }
1121             block_Release( p_pkt );
1122             break;
1123         }
1124         default:
1125         {
1126             int i_id = ps_pkt_id( p_pkt );
1127             if( i_id >= 0xc0 )
1128             {
1129                 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1130
1131                 if( !tk->b_seen )
1132                 {
1133                     ESNew( p_demux, i_id );
1134                 }
1135                 if( tk->b_seen && tk->es &&
1136                     !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1137                 {
1138                     es_out_Send( p_demux->out, tk->es, p_pkt );
1139                 }
1140                 else
1141                 {
1142                     block_Release( p_pkt );
1143                 }
1144             }
1145             else
1146             {
1147                 block_Release( p_pkt );
1148             }
1149             break;
1150         }
1151         }
1152
1153         p += i_size;
1154     }
1155
1156     return VLC_SUCCESS;
1157 }
1158
1159 /*****************************************************************************
1160  * ESNew: register a new elementary stream
1161  *****************************************************************************/
1162 static void ESNew( demux_t *p_demux, int i_id )
1163 {
1164     demux_sys_t *p_sys = p_demux->p_sys;
1165     ps_track_t  *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1166     bool  b_select = false;
1167
1168     if( tk->b_seen ) return;
1169
1170     if( ps_track_fill( tk, 0, i_id ) )
1171     {
1172         msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1173         return;
1174     }
1175
1176     /* Add a new ES */
1177     if( tk->fmt.i_cat == VIDEO_ES )
1178     {
1179         b_select = true;
1180     }
1181     else if( tk->fmt.i_cat == AUDIO_ES )
1182     {
1183         int i_audio = -1;
1184         /* find the audio number PLEASE find another way */
1185         if( (i_id&0xbdf8) == 0xbd88 )       /* dts */
1186         {
1187             i_audio = i_id&0x07;
1188         }
1189         else if( (i_id&0xbdf0) == 0xbd80 )  /* a52 */
1190         {
1191             i_audio = i_id&0xf;
1192         }
1193         else if( (i_id&0xbdf0) == 0xbda0 )  /* lpcm */
1194         {
1195             i_audio = i_id&0x1f;
1196         }
1197         else if( ( i_id&0xe0 ) == 0xc0 )    /* mpga */
1198         {
1199             i_audio = i_id&0x1f;
1200         }
1201         if( i_audio >= 0 )
1202         {
1203             int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1204             if( i_lang != 0xffff )
1205             {
1206                 tk->fmt.psz_language = malloc( 3 );
1207                 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1208                 tk->fmt.psz_language[1] = (i_lang     )&0xff;
1209                 tk->fmt.psz_language[2] = 0;
1210             }
1211             if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1212             {
1213                 b_select = true;
1214             }
1215         }
1216     }
1217     else if( tk->fmt.i_cat == SPU_ES )
1218     {
1219         int32_t i_title, i_part;
1220         int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1221         if( i_lang != 0xffff )
1222         {
1223             tk->fmt.psz_language = malloc( 3 );
1224             tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1225             tk->fmt.psz_language[1] = (i_lang     )&0xff;
1226             tk->fmt.psz_language[2] = 0;
1227         }
1228
1229         /* Palette */
1230         tk->fmt.subs.spu.palette[0] = 0xBeef;
1231         memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1232                 16 * sizeof( uint32_t ) );
1233
1234         /* We select only when we are not in the menu */
1235         dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1236         if( i_title > 0 &&
1237             dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1238         {
1239             b_select = true;
1240         }
1241     }
1242
1243     tk->es = es_out_Add( p_demux->out, &tk->fmt );
1244     if( b_select )
1245     {
1246         es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1247     }
1248     tk->b_seen = true;
1249
1250     if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1251 }
1252
1253 /*****************************************************************************
1254  * Still image end
1255  *****************************************************************************/
1256 static void StillTimer( void *p_data )
1257 {
1258     demux_sys_t    *p_sys = p_data;
1259
1260     vlc_mutex_lock( &p_sys->still.lock );
1261     assert( p_sys->still.b_enabled );
1262     p_sys->still.b_enabled = false;
1263     dvdnav_still_skip( p_sys->dvdnav );
1264     vlc_mutex_unlock( &p_sys->still.lock );
1265 }
1266
1267 static int EventMouse( vlc_object_t *p_vout, char const *psz_var,
1268                        vlc_value_t oldval, vlc_value_t val, void *p_data )
1269 {
1270     demux_t *p_demux = p_data;
1271     demux_sys_t *p_sys = p_demux->p_sys;
1272
1273     /* FIXME? PCI usage thread safe? */
1274     pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1275     int x = var_GetInteger( p_vout, "mouse-x" );
1276     int y = var_GetInteger( p_vout, "mouse-y" );
1277
1278     if( psz_var[6] == 'm' ) /* mouse-moved */
1279         dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
1280     else
1281     {
1282         assert( psz_var[6] == 'c' ); /* mouse-clicked */
1283
1284         ButtonUpdate( p_demux, true );
1285         dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
1286     }
1287
1288     (void)oldval; (void)val;
1289     return VLC_SUCCESS;
1290 }
1291
1292 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1293                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
1294 {
1295     demux_t *p_demux = p_data;
1296     demux_sys_t *p_sys = p_demux->p_sys;
1297
1298     /* FIXME: thread-safe ? */
1299     pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1300
1301     switch( newval.i_int )
1302     {
1303     case ACTIONID_NAV_LEFT:
1304         dvdnav_left_button_select( p_sys->dvdnav, pci );
1305         break;
1306     case ACTIONID_NAV_RIGHT:
1307         dvdnav_right_button_select( p_sys->dvdnav, pci );
1308         break;
1309     case ACTIONID_NAV_UP:
1310         dvdnav_upper_button_select( p_sys->dvdnav, pci );
1311         break;
1312     case ACTIONID_NAV_DOWN:
1313         dvdnav_lower_button_select( p_sys->dvdnav, pci );
1314         break;
1315     case ACTIONID_NAV_ACTIVATE:
1316         ButtonUpdate( p_demux, true );
1317         dvdnav_button_activate( p_sys->dvdnav, pci );
1318         break;
1319     }
1320
1321     (void)p_this;    (void)psz_var;    (void)oldval;
1322     return VLC_SUCCESS;
1323 }
1324
1325 static int EventIntf( vlc_object_t *p_input, char const *psz_var,
1326                       vlc_value_t oldval, vlc_value_t val, void *p_data )
1327 {
1328     demux_t *p_demux = p_data;
1329     demux_sys_t *p_sys = p_demux->p_sys;
1330
1331     if (val.i_int == INPUT_EVENT_VOUT)
1332     {
1333         vlc_object_t *p_vout;
1334
1335         p_vout = p_sys->p_vout;
1336         if( p_vout != NULL )
1337         {
1338             var_DelCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1339             var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1340             vlc_object_release( p_vout );
1341         }
1342
1343         p_vout = (vlc_object_t *)input_GetVout( (input_thread_t *)p_input );
1344         p_sys->p_vout = p_vout;
1345         if( p_vout != NULL )
1346         {
1347             var_AddCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1348             var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1349         }
1350     }
1351     (void) psz_var; (void) oldval;
1352     return VLC_SUCCESS;
1353 }
1354
1355 /*****************************************************************************
1356  * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1357  *****************************************************************************/
1358 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1359 {
1360     (void)p_demux;
1361 #ifdef HAVE_SYS_STAT_H
1362     struct stat stat_info;
1363     uint8_t pi_anchor[2];
1364     int i_fd, i_ret;
1365
1366     if( !*psz_name )
1367     {
1368         /* Triggers libdvdcss autodetection */
1369         return VLC_SUCCESS;
1370     }
1371
1372     if( !strcmp( psz_name, "-" ) ) /* stdin -> file access */
1373         return VLC_EGENERIC;
1374
1375     if( (i_fd = utf8_open( psz_name, O_RDONLY |O_NONBLOCK )) == -1 )
1376     {
1377         return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1378     }
1379
1380     i_ret = VLC_EGENERIC;
1381
1382     if( fstat( i_fd, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1383     {
1384         if( !S_ISFIFO( stat_info.st_mode ) )
1385             i_ret = VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1386         goto bailout;
1387     }
1388
1389     /* Try to find the anchor (2 bytes at LBA 256) */
1390     if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1391      && read( i_fd, pi_anchor, 2 ) == 2
1392      && GetWLE( pi_anchor ) == 2 )
1393         i_ret = VLC_SUCCESS; /* Found a potential anchor */
1394
1395 bailout:
1396     close( i_fd );
1397
1398     return i_ret;
1399 #else
1400
1401     return VLC_SUCCESS;
1402 #endif
1403 }