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