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