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