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