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