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