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