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