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