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