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