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