]> git.sesse.net Git - vlc/blob - modules/access/dvdnav.c
dvdnav: fully fix the still image assertion
[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
41 #include <vlc_dialog.h>
42
43 #ifdef HAVE_UNISTD_H
44 #   include <unistd.h>
45 #endif
46 #include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 #   include <sys/stat.h>
49 #endif
50 #ifdef HAVE_FCNTL_H
51 #   include <fcntl.h>
52 #endif
53
54 #include <vlc_keys.h>
55 #include <vlc_iso_lang.h>
56
57 /* FIXME we should find a better way than including that */
58 #include "../../src/text/iso-639_def.h"
59
60
61 #include <dvdnav/dvdnav.h>
62
63 #include "../demux/ps.h"
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68 #define ANGLE_TEXT N_("DVD angle")
69 #define ANGLE_LONGTEXT N_( \
70      "Default DVD angle." )
71
72 #define CACHING_TEXT N_("Caching value in ms")
73 #define CACHING_LONGTEXT N_( \
74     "Caching value for DVDs. This "\
75     "value should be set in milliseconds." )
76 #define MENU_TEXT N_("Start directly in menu")
77 #define MENU_LONGTEXT N_( \
78     "Start the DVD directly in the main menu. This "\
79     "will try to skip all the useless warning introductions." )
80
81 #define LANGUAGE_DEFAULT ("en")
82
83 static int  Open ( vlc_object_t * );
84 static void Close( vlc_object_t * );
85
86 vlc_module_begin ()
87     set_shortname( N_("DVD with menus") )
88     set_description( N_("DVDnav Input") )
89     set_category( CAT_INPUT )
90     set_subcategory( SUBCAT_INPUT_ACCESS )
91     add_integer( "dvdnav-angle", 1, NULL, ANGLE_TEXT,
92         ANGLE_LONGTEXT, false )
93     add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
94         CACHING_TEXT, CACHING_LONGTEXT, true )
95     add_bool( "dvdnav-menu", true, NULL,
96         MENU_TEXT, MENU_LONGTEXT, false )
97     set_capability( "access_demux", 5 )
98     add_shortcut( "dvd" )
99     add_shortcut( "dvdnav" )
100     add_shortcut( "file" )
101     set_callbacks( Open, Close )
102 vlc_module_end ()
103
104 /* Shall we use libdvdnav's read ahead cache? */
105 #define DVD_READ_CACHE 1
106
107 /*****************************************************************************
108  * Local prototypes
109  *****************************************************************************/
110 struct demux_sys_t
111 {
112     dvdnav_t    *dvdnav;
113
114     /* */
115     bool        b_reset_pcr;
116
117     struct
118     {
119         bool         b_created;
120         bool         b_enabled;
121         vlc_mutex_t  lock;
122         vlc_timer_t  timer;
123     } still;
124
125     /* track */
126     ps_track_t  tk[PS_TK_COUNT];
127     int         i_mux_rate;
128
129     /* for spu variables */
130     input_thread_t *p_input;
131
132     /* event */
133     vlc_object_t *p_vout;
134
135     /* palette for menus */
136     uint32_t clut[16];
137     uint8_t  palette[4][4];
138     bool b_spu_change;
139
140     /* */
141     int           i_title;
142     input_title_t **title;
143
144     /* lenght of program group chain */
145     mtime_t     i_pgc_length;
146 };
147
148 static int Control( demux_t *, int, va_list );
149 static int Demux( demux_t * );
150 static int DemuxBlock( demux_t *, const uint8_t *, int );
151
152 static void DemuxTitles( demux_t * );
153 static void ESSubtitleUpdate( demux_t * );
154 static void ButtonUpdate( demux_t *, bool );
155
156 static void ESNew( demux_t *, int );
157 static int ProbeDVD( demux_t *, char * );
158
159 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
160
161 static int ControlInternal( demux_t *, int, ... );
162
163 static void StillTimer( void * );
164
165 static int EventKey( vlc_object_t *, char const *,
166                      vlc_value_t, vlc_value_t, void * );
167 static int EventMouse( vlc_object_t *, char const *,
168                        vlc_value_t, vlc_value_t, void * );
169 static int EventIntf( vlc_object_t *, char const *,
170                       vlc_value_t, vlc_value_t, void * );
171
172 /*****************************************************************************
173  * DemuxOpen:
174  *****************************************************************************/
175 static int Open( vlc_object_t *p_this )
176 {
177     demux_t     *p_demux = (demux_t*)p_this;
178     demux_sys_t *p_sys;
179     dvdnav_t    *p_dvdnav;
180     int         i_angle;
181     char        *psz_name;
182     char        *psz_code;
183
184     if( !p_demux->psz_path || !*p_demux->psz_path )
185     {
186         /* Only when selected */
187         if( !p_demux->psz_access || !*p_demux->psz_access )
188             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         vlc_timer_schedule( p_sys->still.timer, false, 0, 0 );
609         p_sys->still.b_enabled = false;
610         vlc_mutex_unlock( &p_sys->still.lock );
611         if( p_sys->b_reset_pcr )
612         {
613             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
614             p_sys->b_reset_pcr = false;
615         }
616         DemuxBlock( p_demux, packet, i_len );
617         break;
618
619     case DVDNAV_NOP:    /* Nothing */
620         msg_Dbg( p_demux, "DVDNAV_NOP" );
621         break;
622
623     case DVDNAV_STILL_FRAME:
624     {
625         dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
626         bool b_still_init = false;
627
628         vlc_mutex_lock( &p_sys->still.lock );
629         if( !p_sys->still.b_enabled )
630         {
631             msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
632             msg_Dbg( p_demux, "     - length=0x%x", event->length );
633             p_sys->still.b_enabled = true;
634
635             if( event->length != 0xff && p_sys->still.b_created )
636             {
637                 mtime_t delay = event->length * CLOCK_FREQ;
638                 vlc_timer_schedule( p_sys->still.timer, false, delay, 0 );
639             }
640
641             b_still_init = true;
642         }
643         vlc_mutex_unlock( &p_sys->still.lock );
644
645         if( b_still_init )
646         {
647             /* We send a dummy mpeg2 end of sequence to force still frame display */
648             static const uint8_t buffer[] = {
649                 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
650                 0x80, 0x00, 0x00,
651                 0x00, 0x00, 0x01, 0xB7,
652             };
653             DemuxBlock( p_demux, buffer, sizeof(buffer) );
654
655             bool b_empty;
656             es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
657             p_sys->b_reset_pcr = true;
658         }
659         msleep( 40000 );
660         break;
661     }
662
663     case DVDNAV_SPU_CLUT_CHANGE:
664     {
665         int i;
666
667         msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
668         /* Update color lookup table (16 *uint32_t in packet) */
669         memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
670
671         /* HACK to get the SPU tracks registered in the right order */
672         for( i = 0; i < 0x1f; i++ )
673         {
674             if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
675                 ESNew( p_demux, 0xbd20 + i );
676         }
677         /* END HACK */
678         break;
679     }
680
681     case DVDNAV_SPU_STREAM_CHANGE:
682     {
683         dvdnav_spu_stream_change_event_t *event =
684             (dvdnav_spu_stream_change_event_t*)packet;
685         int i;
686
687         msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
688         msg_Dbg( p_demux, "     - physical_wide=%d",
689                  event->physical_wide );
690         msg_Dbg( p_demux, "     - physical_letterbox=%d",
691                  event->physical_letterbox);
692         msg_Dbg( p_demux, "     - physical_pan_scan=%d",
693                  event->physical_pan_scan );
694
695         ESSubtitleUpdate( p_demux );
696         p_sys->b_spu_change = true;
697
698         /* HACK to get the SPU tracks registered in the right order */
699         for( i = 0; i < 0x1f; i++ )
700         {
701             if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
702                 ESNew( p_demux, 0xbd20 + i );
703         }
704         /* END HACK */
705         break;
706     }
707
708     case DVDNAV_AUDIO_STREAM_CHANGE:
709     {
710         dvdnav_audio_stream_change_event_t *event =
711             (dvdnav_audio_stream_change_event_t*)packet;
712         msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
713         msg_Dbg( p_demux, "     - physical=%d", event->physical );
714         /* TODO */
715         break;
716     }
717
718     case DVDNAV_VTS_CHANGE:
719     {
720         int32_t i_title = 0;
721         int32_t i_part  = 0;
722         int i;
723
724         dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
725         msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
726         msg_Dbg( p_demux, "     - vtsN=%d", event->new_vtsN );
727         msg_Dbg( p_demux, "     - domain=%d", event->new_domain );
728
729         /* reset PCR */
730         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
731
732         for( i = 0; i < PS_TK_COUNT; i++ )
733         {
734             ps_track_t *tk = &p_sys->tk[i];
735             if( tk->b_seen )
736             {
737                 es_format_Clean( &tk->fmt );
738                 if( tk->es ) es_out_Del( p_demux->out, tk->es );
739             }
740             tk->b_seen = false;
741         }
742
743         if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
744                                        &i_part ) == DVDNAV_STATUS_OK )
745         {
746             if( i_title >= 0 && i_title < p_sys->i_title &&
747                 p_demux->info.i_title != i_title )
748             {
749                 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
750                 p_demux->info.i_title = i_title;
751             }
752         }
753         break;
754     }
755
756     case DVDNAV_CELL_CHANGE:
757     {
758         int32_t i_title = 0;
759         int32_t i_part  = 0;
760
761         dvdnav_cell_change_event_t *event =
762             (dvdnav_cell_change_event_t*)packet;
763         msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
764         msg_Dbg( p_demux, "     - cellN=%d", event->cellN );
765         msg_Dbg( p_demux, "     - pgN=%d", event->pgN );
766         msg_Dbg( p_demux, "     - cell_length=%"PRId64, event->cell_length );
767         msg_Dbg( p_demux, "     - pg_length=%"PRId64, event->pg_length );
768         msg_Dbg( p_demux, "     - pgc_length=%"PRId64, event->pgc_length );
769         msg_Dbg( p_demux, "     - cell_start=%"PRId64, event->cell_start );
770         msg_Dbg( p_demux, "     - pg_start=%"PRId64, event->pg_start );
771
772         /* Store the lenght in time of the current PGC */
773         p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
774
775         /* FIXME is it correct or there is better way to know chapter change */
776         if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
777                                        &i_part ) == DVDNAV_STATUS_OK )
778         {
779             if( i_title >= 0 && i_title < p_sys->i_title &&
780                 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
781                 p_demux->info.i_seekpoint != i_part - 1 )
782             {
783                 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
784                 p_demux->info.i_seekpoint = i_part - 1;
785             }
786         }
787         break;
788     }
789
790     case DVDNAV_NAV_PACKET:
791     {
792 #ifdef DVDNAV_DEBUG
793         msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
794 #endif
795         /* A lot of thing to do here :
796          *  - handle packet
797          *  - fetch pts (for time display)
798          *  - ...
799          */
800         DemuxBlock( p_demux, packet, i_len );
801         if( p_sys->b_spu_change )
802         {
803             ButtonUpdate( p_demux, false );
804             p_sys->b_spu_change = false;
805         }
806         break;
807     }
808
809     case DVDNAV_STOP:   /* EOF */
810         msg_Dbg( p_demux, "DVDNAV_STOP" );
811
812 #if DVD_READ_CACHE
813         dvdnav_free_cache_block( p_sys->dvdnav, packet );
814 #endif
815         return 0;
816
817     case DVDNAV_HIGHLIGHT:
818     {
819         dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
820         msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
821         msg_Dbg( p_demux, "     - display=%d", event->display );
822         msg_Dbg( p_demux, "     - buttonN=%d", event->buttonN );
823         ButtonUpdate( p_demux, false );
824         break;
825     }
826
827     case DVDNAV_HOP_CHANNEL:
828         msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
829         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
830         break;
831
832     case DVDNAV_WAIT:
833         msg_Dbg( p_demux, "DVDNAV_WAIT" );
834
835         bool b_empty;
836         es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
837         if( !b_empty )
838         {
839             msleep( 40*1000 );
840         }
841         else
842         {
843             dvdnav_wait_skip( p_sys->dvdnav );
844             p_sys->b_reset_pcr = true;
845         }
846         break;
847
848     default:
849         msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
850         break;
851     }
852
853 #if DVD_READ_CACHE
854     dvdnav_free_cache_block( p_sys->dvdnav, packet );
855 #endif
856
857     return 1;
858 }
859
860 /* Get a 2 char code
861  * FIXME: partiallyy duplicated from src/input/es_out.c
862  */
863 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
864 {
865     const iso639_lang_t *pl;
866     char *psz_lang;
867     char *p;
868
869     psz_lang = var_CreateGetString( p_demux, psz_var );
870     if( !psz_lang )
871         return strdup(LANGUAGE_DEFAULT);
872
873     /* XXX: we will use only the first value
874      * (and ignore other ones in case of a list) */
875     if( ( p = strchr( psz_lang, ',' ) ) )
876         *p = '\0';
877
878     for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
879     {
880         if( *psz_lang == '\0' )
881             continue;
882         if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
883             !strcasecmp( pl->psz_native_name, psz_lang ) ||
884             !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
885             !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
886             !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
887             break;
888     }
889
890     free( psz_lang );
891
892     if( pl->psz_eng_name != NULL )
893         return strdup( pl->psz_iso639_1 );
894
895     return strdup(LANGUAGE_DEFAULT);
896 }
897
898 static void DemuxTitles( demux_t *p_demux )
899 {
900     demux_sys_t *p_sys = p_demux->p_sys;
901     input_title_t *t;
902     seekpoint_t *s;
903     int32_t i_titles;
904     int i;
905
906     /* Menu */
907     t = vlc_input_title_New();
908     t->b_menu = true;
909     t->psz_name = strdup( "DVD Menu" );
910
911     s = vlc_seekpoint_New();
912     s->psz_name = strdup( "Resume" );
913     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
914
915     s = vlc_seekpoint_New();
916     s->psz_name = strdup( "Root" );
917     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
918
919     s = vlc_seekpoint_New();
920     s->psz_name = strdup( "Title" );
921     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
922
923     s = vlc_seekpoint_New();
924     s->psz_name = strdup( "Chapter" );
925     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
926
927     s = vlc_seekpoint_New();
928     s->psz_name = strdup( "Subtitle" );
929     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
930
931     s = vlc_seekpoint_New();
932     s->psz_name = strdup( "Audio" );
933     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
934
935     s = vlc_seekpoint_New();
936     s->psz_name = strdup( "Angle" );
937     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
938
939     TAB_APPEND( p_sys->i_title, p_sys->title, t );
940
941     /* Find out number of titles/chapters */
942     dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
943     for( i = 1; i <= i_titles; i++ )
944     {
945         int32_t i_chapters = 0;
946         int j;
947
948         dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
949
950         t = vlc_input_title_New();
951         for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
952         {
953             s = vlc_seekpoint_New();
954             TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
955         }
956
957         TAB_APPEND( p_sys->i_title, p_sys->title, t );
958     }
959 }
960
961 /*****************************************************************************
962  * Update functions:
963  *****************************************************************************/
964 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
965 {
966     demux_sys_t *p_sys = p_demux->p_sys;
967     vlc_value_t val;
968     int32_t i_title, i_part;
969
970     dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
971
972     if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
973     {
974         vlc_mutex_t *p_mutex = val.p_address;
975         dvdnav_highlight_area_t hl;
976         int32_t i_button;
977         bool    b_button_ok;
978
979         if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
980             != DVDNAV_STATUS_OK )
981         {
982             msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
983             return;
984         }
985
986         b_button_ok = false;
987         if( i_button > 0 && i_title ==  0 )
988         {
989             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
990
991             b_button_ok = DVDNAV_STATUS_OK ==
992                       dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
993         }
994
995         if( b_button_ok )
996         {
997             int i;
998             for( i = 0; i < 4; i++ )
999             {
1000                 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1001                 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1002
1003                 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1004                 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1005                 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1006                 p_sys->palette[i][3] = i_alpha;
1007             }
1008
1009             vlc_mutex_lock( p_mutex );
1010             var_SetInteger( p_sys->p_input, "x-start", hl.sx );
1011             var_SetInteger( p_sys->p_input, "x-end",  hl.ex );
1012             var_SetInteger( p_sys->p_input, "y-start", hl.sy );
1013             var_SetInteger( p_sys->p_input, "y-end", hl.ey );
1014
1015             var_SetAddress( p_sys->p_input, "menu-palette", p_sys->palette );
1016
1017             var_SetBool( p_sys->p_input, "highlight", true );
1018             vlc_mutex_unlock( p_mutex );
1019
1020             msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1021         }
1022         else
1023         {
1024             msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1025                      i_button, i_title );
1026
1027             /* Show all */
1028             vlc_mutex_lock( p_mutex );
1029             var_SetBool( p_sys->p_input, "highlight", false );
1030             vlc_mutex_unlock( p_mutex );
1031         }
1032     }
1033 }
1034
1035 static void ESSubtitleUpdate( demux_t *p_demux )
1036 {
1037     demux_sys_t *p_sys = p_demux->p_sys;
1038     int         i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1039     int32_t i_title, i_part;
1040
1041     ButtonUpdate( p_demux, false );
1042
1043     dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1044     if( i_title > 0 ) return;
1045
1046     if( i_spu >= 0 && i_spu <= 0x1f )
1047     {
1048         ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1049
1050         ESNew( p_demux, 0xbd20 + i_spu );
1051
1052         /* be sure to unselect it (reset) */
1053         es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1054                         (bool)false );
1055
1056         /* now select it */
1057         es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1058     }
1059     else
1060     {
1061         for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1062         {
1063             ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1064             if( tk->b_seen )
1065             {
1066                 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1067                                 (bool)false );
1068             }
1069         }
1070     }
1071 }
1072
1073 /*****************************************************************************
1074  * DemuxBlock: demux a given block
1075  *****************************************************************************/
1076 static int DemuxBlock( demux_t *p_demux, const uint8_t *pkt, int i_pkt )
1077 {
1078     demux_sys_t *p_sys = p_demux->p_sys;
1079     const uint8_t     *p = pkt;
1080
1081     while( p < &pkt[i_pkt] )
1082     {
1083         int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1084         block_t *p_pkt;
1085         if( i_size <= 0 )
1086         {
1087             break;
1088         }
1089
1090         /* Create a block */
1091         p_pkt = block_New( p_demux, i_size );
1092         memcpy( p_pkt->p_buffer, p, i_size);
1093
1094         /* Parse it and send it */
1095         switch( 0x100 | p[3] )
1096         {
1097         case 0x1b9:
1098         case 0x1bb:
1099         case 0x1bc:
1100 #ifdef DVDNAV_DEBUG
1101             if( p[3] == 0xbc )
1102             {
1103                 msg_Warn( p_demux, "received a PSM packet" );
1104             }
1105             else if( p[3] == 0xbb )
1106             {
1107                 msg_Warn( p_demux, "received a SYSTEM packet" );
1108             }
1109 #endif
1110             block_Release( p_pkt );
1111             break;
1112
1113         case 0x1ba:
1114         {
1115             int64_t i_scr;
1116             int i_mux_rate;
1117             if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1118             {
1119                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1120                 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1121             }
1122             block_Release( p_pkt );
1123             break;
1124         }
1125         default:
1126         {
1127             int i_id = ps_pkt_id( p_pkt );
1128             if( i_id >= 0xc0 )
1129             {
1130                 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1131
1132                 if( !tk->b_seen )
1133                 {
1134                     ESNew( p_demux, i_id );
1135                 }
1136                 if( tk->b_seen && tk->es &&
1137                     !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1138                 {
1139                     es_out_Send( p_demux->out, tk->es, p_pkt );
1140                 }
1141                 else
1142                 {
1143                     block_Release( p_pkt );
1144                 }
1145             }
1146             else
1147             {
1148                 block_Release( p_pkt );
1149             }
1150             break;
1151         }
1152         }
1153
1154         p += i_size;
1155     }
1156
1157     return VLC_SUCCESS;
1158 }
1159
1160 /*****************************************************************************
1161  * ESNew: register a new elementary stream
1162  *****************************************************************************/
1163 static void ESNew( demux_t *p_demux, int i_id )
1164 {
1165     demux_sys_t *p_sys = p_demux->p_sys;
1166     ps_track_t  *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1167     bool  b_select = false;
1168
1169     if( tk->b_seen ) return;
1170
1171     if( ps_track_fill( tk, 0, i_id ) )
1172     {
1173         msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1174         return;
1175     }
1176
1177     /* Add a new ES */
1178     if( tk->fmt.i_cat == VIDEO_ES )
1179     {
1180         b_select = true;
1181     }
1182     else if( tk->fmt.i_cat == AUDIO_ES )
1183     {
1184         int i_audio = -1;
1185         /* find the audio number PLEASE find another way */
1186         if( (i_id&0xbdf8) == 0xbd88 )       /* dts */
1187         {
1188             i_audio = i_id&0x07;
1189         }
1190         else if( (i_id&0xbdf0) == 0xbd80 )  /* a52 */
1191         {
1192             i_audio = i_id&0xf;
1193         }
1194         else if( (i_id&0xbdf0) == 0xbda0 )  /* lpcm */
1195         {
1196             i_audio = i_id&0x1f;
1197         }
1198         else if( ( i_id&0xe0 ) == 0xc0 )    /* mpga */
1199         {
1200             i_audio = i_id&0x1f;
1201         }
1202         if( i_audio >= 0 )
1203         {
1204             int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1205             if( i_lang != 0xffff )
1206             {
1207                 tk->fmt.psz_language = malloc( 3 );
1208                 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1209                 tk->fmt.psz_language[1] = (i_lang     )&0xff;
1210                 tk->fmt.psz_language[2] = 0;
1211             }
1212             if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1213             {
1214                 b_select = true;
1215             }
1216         }
1217     }
1218     else if( tk->fmt.i_cat == SPU_ES )
1219     {
1220         int32_t i_title, i_part;
1221         int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1222         if( i_lang != 0xffff )
1223         {
1224             tk->fmt.psz_language = malloc( 3 );
1225             tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1226             tk->fmt.psz_language[1] = (i_lang     )&0xff;
1227             tk->fmt.psz_language[2] = 0;
1228         }
1229
1230         /* Palette */
1231         tk->fmt.subs.spu.palette[0] = 0xBeef;
1232         memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1233                 16 * sizeof( uint32_t ) );
1234
1235         /* We select only when we are not in the menu */
1236         dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1237         if( i_title > 0 &&
1238             dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1239         {
1240             b_select = true;
1241         }
1242     }
1243
1244     tk->es = es_out_Add( p_demux->out, &tk->fmt );
1245     if( b_select )
1246     {
1247         es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1248     }
1249     tk->b_seen = true;
1250
1251     if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1252 }
1253
1254 /*****************************************************************************
1255  * Still image end
1256  *****************************************************************************/
1257 static void StillTimer( void *p_data )
1258 {
1259     demux_sys_t    *p_sys = p_data;
1260
1261     vlc_mutex_lock( &p_sys->still.lock );
1262     if( likely(p_sys->still.b_enabled) )
1263     {
1264         p_sys->still.b_enabled = false;
1265         dvdnav_still_skip( p_sys->dvdnav );
1266     }
1267     vlc_mutex_unlock( &p_sys->still.lock );
1268 }
1269
1270 static int EventMouse( vlc_object_t *p_vout, char const *psz_var,
1271                        vlc_value_t oldval, vlc_value_t val, void *p_data )
1272 {
1273     demux_t *p_demux = p_data;
1274     demux_sys_t *p_sys = p_demux->p_sys;
1275
1276     /* FIXME? PCI usage thread safe? */
1277     pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1278     int x = val.coords.x;
1279     int y = val.coords.y;
1280
1281     if( psz_var[6] == 'm' ) /* mouse-moved */
1282         dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
1283     else
1284     {
1285         assert( psz_var[6] == 'c' ); /* mouse-clicked */
1286
1287         ButtonUpdate( p_demux, true );
1288         dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
1289     }
1290     (void)p_vout;
1291     (void)oldval;
1292     return VLC_SUCCESS;
1293 }
1294
1295 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1296                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
1297 {
1298     demux_t *p_demux = p_data;
1299     demux_sys_t *p_sys = p_demux->p_sys;
1300
1301     /* FIXME: thread-safe ? */
1302     pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1303
1304     switch( newval.i_int )
1305     {
1306     case ACTIONID_NAV_LEFT:
1307         dvdnav_left_button_select( p_sys->dvdnav, pci );
1308         break;
1309     case ACTIONID_NAV_RIGHT:
1310         dvdnav_right_button_select( p_sys->dvdnav, pci );
1311         break;
1312     case ACTIONID_NAV_UP:
1313         dvdnav_upper_button_select( p_sys->dvdnav, pci );
1314         break;
1315     case ACTIONID_NAV_DOWN:
1316         dvdnav_lower_button_select( p_sys->dvdnav, pci );
1317         break;
1318     case ACTIONID_NAV_ACTIVATE:
1319         ButtonUpdate( p_demux, true );
1320         dvdnav_button_activate( p_sys->dvdnav, pci );
1321         break;
1322     }
1323
1324     (void)p_this;    (void)psz_var;    (void)oldval;
1325     return VLC_SUCCESS;
1326 }
1327
1328 static int EventIntf( vlc_object_t *p_input, char const *psz_var,
1329                       vlc_value_t oldval, vlc_value_t val, void *p_data )
1330 {
1331     demux_t *p_demux = p_data;
1332     demux_sys_t *p_sys = p_demux->p_sys;
1333
1334     if (val.i_int == INPUT_EVENT_VOUT)
1335     {
1336         vlc_object_t *p_vout;
1337
1338         p_vout = p_sys->p_vout;
1339         if( p_vout != NULL )
1340         {
1341             var_DelCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1342             var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1343             vlc_object_release( p_vout );
1344         }
1345
1346         p_vout = (vlc_object_t *)input_GetVout( (input_thread_t *)p_input );
1347         p_sys->p_vout = p_vout;
1348         if( p_vout != NULL )
1349         {
1350             var_AddCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1351             var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1352         }
1353     }
1354     (void) psz_var; (void) oldval;
1355     return VLC_SUCCESS;
1356 }
1357
1358 /*****************************************************************************
1359  * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1360  *****************************************************************************/
1361 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1362 {
1363     (void)p_demux;
1364 #ifdef HAVE_SYS_STAT_H
1365     struct stat stat_info;
1366     uint8_t pi_anchor[2];
1367     int i_fd, i_ret;
1368
1369     if( !*psz_name )
1370     {
1371         /* Triggers libdvdcss autodetection */
1372         return VLC_SUCCESS;
1373     }
1374
1375     if( (i_fd = vlc_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 }