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