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