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