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