]> git.sesse.net Git - vlc/blob - modules/codec/cmml/intf.c
Do not access vout_thread_t fields when it can be avoided.
[vlc] / modules / codec / cmml / intf.c
1 /*****************************************************************************
2  * intf.c: interface for CMML annotations/hyperlinks
3  *****************************************************************************
4  * Copyright (C) 2003-2004 Commonwealth Scientific and Industrial Research
5  *                         Organisation (CSIRO) Australia
6  * Copyright (C) 2004 the VideoLAN team
7  *
8  * $Id$
9  *
10  * Authors: Andre Pang <Andre.Pang@csiro.au>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35
36 #include <stdio.h>
37
38 #ifdef HAVE_UNISTD_H
39 #    include <unistd.h>
40 #endif
41
42 #include <vlc_codec.h>
43 #include <vlc_playlist.h>
44 #include <vlc_osd.h>
45
46 #include <vlc_keys.h>
47
48 #include "browser_open.h"
49 #include "history.h"
50 #include "xstrcat.h"
51 #include "xurl.h"
52
53 #undef CMML_INTF_USE_TIMED_URIS
54
55 #undef  CMML_INTF_DEBUG
56 #undef  CMML_INTF_HISTORY_DEBUG
57
58 /*****************************************************************************
59  * intf_sys_t: description and status of interface
60  *****************************************************************************/
61 typedef struct decoder_sys_t
62 {
63     VLC_COMMON_MEMBERS
64
65     vlc_mutex_t         lock;
66     decoder_t *         p_cmml_decoder;
67     input_thread_t *    p_input;
68
69     int                 i_key_action;
70 } intf_thread_t;
71
72 struct navigation_history_t
73 {
74     int i_history_size;
75     int i_last_item;
76 };
77
78 /*****************************************************************************
79  * Local prototypes.
80  *****************************************************************************/
81
82 decoder_sys_t *OpenIntf                 ( vlc_object_t * );
83 void         CloseIntf                  ( decoder_sys_t * );
84
85 static int   InitThread                 ( intf_thread_t * );
86 static int   MouseEvent                 ( vlc_object_t *, char const *,
87                                           vlc_value_t, vlc_value_t, void * );
88 static int   KeyEvent                   ( vlc_object_t *, char const *,
89                                           vlc_value_t, vlc_value_t, void * );
90
91 static void  FollowAnchor               ( intf_thread_t * );
92 static void  GoBack                     ( intf_thread_t * );
93 static void  GoForward                  ( intf_thread_t * );
94
95 static int   FollowAnchorCallback       ( vlc_object_t *, char const *,
96                                           vlc_value_t, vlc_value_t, void * );
97 static int   GoBackCallback             ( vlc_object_t *, char const *,
98                                           vlc_value_t, vlc_value_t, void * );
99 static int   GoForwardCallback          ( vlc_object_t *, char const *,
100                                           vlc_value_t, vlc_value_t, void * );
101
102 static char *GetTimedURLFromPlaylistItem( intf_thread_t *, playlist_item_t * );
103 #ifdef CMML_INTF_USE_TIMED_URIS
104 static int   GetCurrentTimeInSeconds    ( input_thread_t * );
105 static char *GetTimedURIFragmentForTime ( int );
106 #endif
107 static int   DisplayAnchor              ( intf_thread_t *, vout_thread_t *,
108                                           char *, char * );
109 static int   DisplayPendingAnchor       ( intf_thread_t *, vout_thread_t * );
110 static history_t * GetHistory           ( playlist_t * );
111 static void  ReplacePlaylistItem        ( playlist_t *, char * );
112
113 static void *RunIntf        ( vlc_object_t * );
114
115 /*****************************************************************************
116  * OpenIntf: initialize CMML interface
117  *****************************************************************************/
118 decoder_sys_t *OpenIntf ( vlc_object_t *p_this )
119 {
120     decoder_sys_t *p_intf = vlc_object_create( p_this, sizeof( *p_intf ) );
121     if( p_intf == NULL )
122         return NULL;
123
124     vlc_mutex_init( &p_intf->lock );
125
126     var_AddCallback( p_intf->p_libvlc, "key-action", KeyEvent, p_intf );
127     /* we also need to add the callback for "mouse-clicked", but do that later
128      * when we've found a p_vout */
129
130     var_Create( p_intf->p_libvlc, "browse-go-back", VLC_VAR_VOID );
131     var_AddCallback( p_intf->p_libvlc, "browse-go-back",
132                      GoBackCallback, p_intf );
133     var_Create( p_intf->p_libvlc, "browse-go-forward", VLC_VAR_VOID );
134     var_AddCallback( p_intf->p_libvlc, "browse-go-forward",
135                      GoForwardCallback, p_intf );
136     var_Create( p_intf->p_libvlc, "browse-follow-anchor", VLC_VAR_VOID );
137     var_AddCallback( p_intf->p_libvlc, "browse-follow-anchor",
138                      FollowAnchorCallback, p_intf );
139
140     vlc_thread_create( p_intf, "cmml", RunIntf, VLC_THREAD_PRIORITY_LOW );
141     return p_intf;
142 }
143
144 /*****************************************************************************
145  * CloseIntf: destroy dummy interface
146  *****************************************************************************/
147 void CloseIntf ( decoder_sys_t *p_intf )
148 {
149     vout_thread_t * p_vout;
150
151 #ifdef CMML_INTF_DEBUG
152     msg_Dbg( p_intf, "freeing CMML interface" );
153 #endif
154
155     /* erase the anchor text description from the video output if it exists */
156     p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
157     if( p_vout )
158     {
159         /* enable CMML as a subtitle track */
160         spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
161         vlc_object_release( p_vout );
162     }
163
164     var_DelCallback( p_intf->p_libvlc, "key-action", KeyEvent, p_intf );
165     vlc_object_kill( p_intf );
166     vlc_thread_join( p_intf );
167
168     vlc_object_release( p_intf->p_cmml_decoder );
169
170     vlc_mutex_destroy( &p_intf->lock );
171     vlc_object_release( p_intf );
172 }
173
174
175 /*****************************************************************************
176  * RunIntf: main loop
177  *****************************************************************************/
178 static void *RunIntf( vlc_object_t *p_obj )
179 {
180     decoder_sys_t *p_intf = (decoder_sys_t *)p_obj;
181     int canc = vlc_savecancel();
182     vout_thread_t * p_vout = NULL;
183
184     if( InitThread( p_intf ) < 0 )
185     {
186         msg_Err( p_intf, "can't initialize CMML interface" );
187         return NULL;
188     }
189 #ifdef CMML_INTF_DEBUG
190     msg_Dbg( p_intf, "CMML intf initialized" );
191 #endif
192
193     /* Main loop */
194     while( vlc_object_alive (p_intf) )
195     {
196         /* if video output is dying, disassociate ourselves from it */
197         if( p_vout && !vlc_object_alive (p_vout) )
198         {
199             var_DelCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
200             vlc_object_release( p_vout );
201             p_vout = NULL;
202         }
203
204         /* find a video output if we currently don't have one */
205         if( p_vout == NULL )
206         {
207             p_vout = vlc_object_find( p_intf->p_input,
208                                       VLC_OBJECT_VOUT, FIND_CHILD );
209             if( p_vout )
210             {
211 #ifdef CMML_INTF_DEBUG
212                 msg_Dbg( p_intf, "found vout thread" );
213 #endif
214                 var_AddCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
215             }
216         }
217
218         vlc_mutex_lock( &p_intf->lock );
219
220         /*
221          * keyboard event
222          */
223         switch( p_intf->i_key_action )
224         {
225             case ACTIONID_NAV_ACTIVATE:
226                 FollowAnchor( p_intf );
227                 break;
228             case ACTIONID_HISTORY_BACK:
229                 GoBack( p_intf );
230                 break;
231             case ACTIONID_HISTORY_FORWARD:
232                 GoForward( p_intf );
233                 break;
234             default:
235                 break;
236         }
237         p_intf->i_key_action = 0;
238         vlc_mutex_unlock( &p_intf->lock );
239
240         (void) DisplayPendingAnchor( p_intf, p_vout );
241
242         /* Wait a bit */
243         msleep( INTF_IDLE_SLEEP );
244     }
245
246     /* if we're here, the video output is dying: release the vout object */
247
248     if( p_vout )
249     {
250         var_DelCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
251         vlc_object_release( p_vout );
252     }
253
254     vlc_object_release( p_intf->p_input );
255     vlc_restorecancel( canc );
256     return NULL;
257 }
258
259 /*****************************************************************************
260  * DisplayPendingAnchor: get a pending anchor description/URL from the CMML
261  * decoder and display it on screen
262  *****************************************************************************/
263 static int DisplayPendingAnchor( intf_thread_t *p_intf, vout_thread_t *p_vout )
264 {
265     decoder_t *p_cmml_decoder;
266     char *psz_description = NULL;
267     char *psz_url = NULL;
268
269     vlc_value_t val;
270
271     p_cmml_decoder = p_intf->p_cmml_decoder;
272     if( var_Get( p_cmml_decoder, "psz-current-anchor-description", &val )
273             != VLC_SUCCESS )
274     {
275         return true;
276     }
277
278     if( !val.p_address )
279         return true;
280
281     psz_description = val.p_address;
282
283     if( var_Get( p_cmml_decoder, "psz-current-anchor-url", &val )
284             == VLC_SUCCESS )
285     {
286         psz_url = val.p_address;
287     }
288
289     if( p_vout != NULL )
290     {
291         /* display anchor as subtitle on-screen */
292         if( DisplayAnchor( p_intf, p_vout, psz_description, psz_url )
293                 != VLC_SUCCESS )
294         {
295             /* text render unsuccessful: do nothing */
296             return false;
297         }
298
299         /* text render successful: clear description */
300         val.p_address = NULL;
301         if( var_Set( p_cmml_decoder, "psz-current-anchor-description", val )
302                 != VLC_SUCCESS )
303         {
304             msg_Dbg( p_intf,
305                      "reset of psz-current-anchor-description failed" );
306         }
307         free( psz_description );
308         psz_url = NULL;
309     }
310
311     return true;
312 }
313
314
315 /*****************************************************************************
316  * InitThread:
317  *****************************************************************************/
318 static int InitThread( intf_thread_t * p_intf )
319 {
320     /* We might need some locking here */
321     if( vlc_object_alive (p_intf) )
322     {
323         input_thread_t * p_input;
324         decoder_t *p_cmml_decoder;
325
326         p_cmml_decoder = vlc_object_find( p_intf, VLC_OBJECT_DECODER, FIND_PARENT );
327         p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );
328
329 #ifdef CMML_INTF_DEBUG
330         msg_Dbg( p_intf, "cmml decoder at %p, input thread at %p",
331                  p_cmml_decoder, p_input );
332 #endif
333
334         /* Maybe the input just died */
335         if( p_input == NULL )
336         {
337             return VLC_EGENERIC;
338         }
339
340         vlc_mutex_lock( &p_intf->lock );
341
342         p_intf->p_input = p_input;
343         p_intf->p_cmml_decoder = p_cmml_decoder;
344
345         p_intf->i_key_action = 0;
346
347         vlc_mutex_unlock( &p_intf->lock );
348
349         return VLC_SUCCESS;
350     }
351     else
352     {
353         return VLC_EGENERIC;
354     }
355 }
356
357 /*****************************************************************************
358  * MouseEvent: callback for mouse events
359  *****************************************************************************/
360 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
361                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
362 {
363     VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
364     VLC_UNUSED(oldval); VLC_UNUSED(newval);
365     VLC_UNUSED(p_data);
366     /* TODO: handle mouse clicks on the anchor text */
367
368     return VLC_SUCCESS;
369 }
370
371 /*****************************************************************************
372  * KeyEvent: callback for keyboard events
373  *****************************************************************************/
374 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
375                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
376 {
377     VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
378     VLC_UNUSED(oldval); VLC_UNUSED(newval);
379     intf_thread_t *p_intf = (intf_thread_t *)p_data;
380
381
382     vlc_mutex_lock( &p_intf->lock );
383     /* FIXME: key presses might get lost here... */
384     p_intf->i_key_action = newval.i_int;
385
386     vlc_mutex_unlock( &p_intf->lock );
387
388     return VLC_SUCCESS;
389 }
390
391 /*****************************************************************************
392  * FollowAnchor: follow the current anchor being displayed to the user
393  *****************************************************************************/
394 static void FollowAnchor ( intf_thread_t *p_intf )
395 {
396     decoder_t *p_cmml_decoder;
397     char *psz_url = NULL;
398     vlc_value_t val;
399
400     msg_Dbg( p_intf, "User followed anchor" );
401
402     p_cmml_decoder = p_intf->p_cmml_decoder;
403
404     if( var_Get( p_cmml_decoder, "psz-current-anchor-url", &val ) ==
405             VLC_SUCCESS )
406     {
407         if( val.p_address ) psz_url = val.p_address;
408     }
409
410 #ifdef CMML_INTF_DEBUG
411     msg_Dbg( p_intf, "Current URL is \"%s\"", psz_url );
412 #endif
413
414     if( psz_url )
415     {
416         playlist_t *p_playlist;
417         playlist_item_t *p_current_item;
418         char *psz_uri_to_load;
419         mtime_t i_seconds;
420         vlc_value_t time;
421
422         p_playlist = pl_Hold( p_intf );
423
424         /* Get new URL */
425         p_current_item = playlist_CurrentPlayingItem( p_playlist );
426         char *psz_uri = input_item_GetURI( p_current_item->p_input );
427 #ifdef CMML_INTF_DEBUG
428         msg_Dbg( p_intf, "Current playlist item URL is \"%s\"", psz_uri );
429 #endif
430
431         psz_uri_to_load = XURL_Concat( psz_uri, psz_url );
432         free( psz_uri );
433
434 #ifdef CMML_INTF_DEBUG
435         msg_Dbg( p_intf, "URL to load is \"%s\"", psz_uri_to_load );
436 #endif
437
438         if( var_Get( p_intf->p_input, "time", &time ) )
439         {
440             msg_Dbg( p_intf, "couldn't get time from current clip" );
441             time.i_time = 0;
442         }
443         i_seconds = time.i_time / 1000000;
444 #ifdef CMML_INTF_DEBUG
445         msg_Dbg( p_intf, "Current time is \"%lld\"", i_seconds );
446 #endif
447
448         /* TODO: we need a (much) more robust way of detecting whether
449          * the file's a media file ... */
450         if( strstr( psz_uri_to_load, ".anx" ) != NULL )
451         {
452             history_t *p_history = NULL;
453             history_item_t *p_history_item = NULL;
454             char *psz_timed_url;
455
456             p_history = GetHistory( p_playlist );
457
458             /* create history item */
459             psz_timed_url = GetTimedURLFromPlaylistItem( p_intf, p_current_item );
460             p_history_item = historyItem_New( psz_timed_url, psz_timed_url );
461             free( psz_timed_url );
462
463             if( !p_history_item )
464             {
465                 msg_Warn( p_intf, "could not initialise history item" );
466             }
467             else
468             {
469 #ifdef CMML_INTF_DEBUG
470                 msg_Dbg( p_intf, "history pre-index %d", p_history->i_index );
471 #endif
472                 history_PruneAndInsert( p_history, p_history_item );
473 #ifdef CMML_INTF_DEBUG
474                 msg_Dbg( p_intf, "new history item at %p, uri is \"%s\"",
475                          p_history_item, p_history_item->psz_uri );
476                 msg_Dbg( p_intf, "history index now %d", p_history->i_index );
477 #endif
478             }
479
480             /* free current-anchor-url */
481             free( psz_url );
482             val.p_address = NULL;
483             if( var_Set( p_cmml_decoder, "psz-current-anchor-url", val ) !=
484                     VLC_SUCCESS )
485             {
486                 msg_Dbg( p_intf, "couldn't reset psz-current-anchor-url" );
487             }
488
489             ReplacePlaylistItem( p_playlist, psz_uri_to_load );
490         }
491         else
492         {
493 #ifdef CMML_INTF_DEBUG
494             msg_Dbg( p_intf, "calling browser_Open with \"%s\"", psz_url );
495 #endif
496             (void) browser_Open( psz_url );
497             playlist_Control( p_playlist, PLAYLIST_PAUSE, pl_Unlocked, 0 );
498         }
499
500         free( psz_uri_to_load );
501
502         pl_Release( p_intf );
503     }
504 }
505
506 static
507 char *GetTimedURLFromPlaylistItem( intf_thread_t *p_intf,
508         playlist_item_t *p_current_item )
509 {
510 #ifdef CMML_INTF_USE_TIMED_URIS
511     char *psz_url = NULL;
512     char *psz_return_value = NULL;
513     char *psz_seconds = NULL;
514     int i_seconds;
515
516     char *psz_uri = input_item_GetURI( p_current_item->p_input );
517     psz_url = XURL_GetWithoutFragment( psz_uri );
518     free( psz_uri );
519
520     /* Get current time as a string */
521     if( XURL_IsFileURL( psz_url ) == true )
522         psz_url = xstrcat( psz_url, "#" );
523     else
524         psz_url = xstrcat( psz_url, "?" );
525
526     /* jump back to 2 seconds before where we are now */
527     i_seconds = GetCurrentTimeInSeconds( p_intf->p_input ) - 2;
528     psz_seconds = GetTimedURIFragmentForTime( i_seconds < 0 ? 0 : i_seconds );
529     if( psz_seconds )
530     {
531         psz_url = xstrcat( psz_url, psz_seconds );
532         free( psz_seconds );
533         psz_return_value = psz_url;
534     }
535
536     return psz_return_value;
537 #else
538     VLC_UNUSED(p_intf);
539
540     return input_item_GetURI( p_current_item->p_input );
541 #endif
542 }
543
544
545
546 #ifdef CMML_INTF_USE_TIMED_URIS
547 /*
548  * Get the current time, rounded down to the nearest second
549  *
550  * http://www.ietf.org/internet-drafts/draft-pfeiffer-temporal-fragments-02.txt
551  */
552 static
553 int GetCurrentTimeInSeconds( input_thread_t *p_input )
554 {
555     vlc_value_t time;
556     mtime_t i_seconds;
557
558     var_Get( p_input, "time", &time );
559     i_seconds = time.i_time / 1000000;
560     return i_seconds;
561 }
562
563 static
564 char *GetTimedURIFragmentForTime( int seconds )
565 {
566     char *psz_time;
567
568     if( asprintf( &psz_time, "%d", seconds ) == -1 )
569         return NULL;
570     return psz_time;
571 }
572 #endif
573
574 static
575 int GoBackCallback( vlc_object_t *p_this, char const *psz_var,
576                     vlc_value_t oldval, vlc_value_t newval, void *p_data )
577 {
578     VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
579     VLC_UNUSED(oldval); VLC_UNUSED(newval);
580     intf_thread_t *p_intf = (intf_thread_t *) p_data;
581     GoBack( p_intf );
582     return VLC_SUCCESS;
583 }
584
585 static
586 int GoForwardCallback( vlc_object_t *p_this, char const *psz_var,
587                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
588 {
589     VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
590     VLC_UNUSED(oldval); VLC_UNUSED(newval);
591     intf_thread_t *p_intf = (intf_thread_t *) p_data;
592     GoForward( p_intf );
593     return VLC_SUCCESS;
594 }
595
596 static
597 int FollowAnchorCallback( vlc_object_t *p_this, char const *psz_var,
598                           vlc_value_t oldval, vlc_value_t newval,
599                           void *p_data )
600 {
601     VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
602     VLC_UNUSED(oldval); VLC_UNUSED(newval);
603     intf_thread_t *p_intf = (intf_thread_t *) p_data;
604     FollowAnchor( p_intf );
605     return VLC_SUCCESS;
606 }
607
608 static
609 void GoBack( intf_thread_t *p_intf )
610 {
611     vlc_value_t history;
612     history_t *p_history = NULL;
613     history_item_t *p_history_item = NULL;
614     history_item_t *p_new_history_item = NULL;
615     playlist_t *p_playlist = NULL;
616     char *psz_timed_url = NULL;
617     playlist_item_t *p_current_item;
618
619 #ifdef CMML_INTF_DEBUG
620     msg_Dbg( p_intf, "Going back in navigation history" );
621 #endif
622
623     /* Find the playlist */
624     p_playlist = pl_Hold( p_intf );
625
626     /* Retrieve navigation history from playlist */
627     if( var_Get( p_playlist, "navigation-history", &history ) != VLC_SUCCESS ||
628         !history.p_address )
629     {
630         /* History doesn't exist yet: ignore user's request */
631         msg_Warn( p_intf, "can't go back: no history exists yet" );
632         pl_Release( p_intf );
633         return;
634     }
635
636     p_history = history.p_address;
637 #ifdef CMML_INTF_DEBUG
638     msg_Dbg( p_intf, "back: nav history retrieved from %p", p_history );
639     msg_Dbg( p_intf, "nav history index:%d, p_xarray:%p", p_history->i_index,
640              p_history->p_xarray );
641 #endif
642
643     /* Check whether we can go back in the history */
644     if( history_CanGoBack( p_history ) == false )
645     {
646         msg_Warn( p_intf, "can't go back: already at beginning of history" );
647         pl_Release( p_intf );
648         return;
649     }
650
651     p_current_item = playlist_CurrentPlayingItem( p_playlist );
652
653     /* Save the currently-playing media in a new history item */
654     psz_timed_url = GetTimedURLFromPlaylistItem( p_intf, p_current_item );
655     p_new_history_item = historyItem_New( psz_timed_url, psz_timed_url );
656     free( psz_timed_url );
657
658     if( !p_new_history_item )
659     {
660 #ifdef CMML_INTF_DEBUG
661         msg_Dbg( p_intf, "back: could not initialise new history item" );
662 #endif
663         pl_Release( p_intf );
664         return;
665     }
666
667     /* Go back in the history, saving the currently-playing item */
668     (void) history_GoBackSavingCurrentItem( p_history, p_new_history_item );
669     p_history_item = history_Item( p_history );
670
671 #ifdef CMML_INTF_DEBUG
672     msg_Dbg( p_intf, "retrieving item from h index %d", p_history->i_index );
673     msg_Dbg( p_intf, "got previous history item: %p", p_history_item );
674     msg_Dbg( p_intf, "prev history item URL: \"%s\"", p_history_item->psz_uri );
675 #endif
676
677     ReplacePlaylistItem( p_playlist, p_history_item->psz_uri );
678     pl_Release( p_intf );
679 }
680
681 static
682 void GoForward( intf_thread_t *p_intf )
683 {
684     vlc_value_t history;
685     history_t *p_history = NULL;
686     history_item_t *p_history_item = NULL;
687     history_item_t *p_new_history_item = NULL;
688     playlist_t *p_playlist = NULL;
689     playlist_item_t *p_current_item;
690
691 #ifdef CMML_INTF_DEBUG
692     msg_Dbg( p_intf, "Going forward in navigation history" );
693 #endif
694
695     /* Find the playlist */
696     p_playlist = pl_Hold( p_intf );
697
698     /* Retrieve navigation history from playlist */
699     if( var_Get( p_playlist, "navigation-history", &history ) != VLC_SUCCESS ||
700         !history.p_address )
701     {
702         /* History doesn't exist yet: ignore user's request */
703         msg_Warn( p_intf, "can't go back: no history exists yet" );
704         pl_Release( p_intf );
705         return;
706     }
707
708     p_history = history.p_address;
709 #ifdef CMML_INTF_DEBUG
710     msg_Dbg( p_intf, "forward: nav history retrieved from %p", p_history );
711     msg_Dbg( p_intf, "nav history index:%d, p_xarray:%p", p_history->i_index,
712              p_history->p_xarray );
713 #endif
714
715     /* Check whether we can go forward in the history */
716     if( history_CanGoForward( p_history ) == false )
717     {
718         msg_Warn( p_intf, "can't go forward: already at end of history" );
719         pl_Release( p_intf );
720         return;
721     }
722
723     /* Save the currently-playing media in a new history item */
724     p_new_history_item = malloc( sizeof(history_item_t) );
725     if( !p_new_history_item )
726     {
727 #ifdef CMML_INTF_DEBUG
728         msg_Dbg( p_intf, "forward: could not initialise new history item" );
729 #endif
730         pl_Release( p_intf );
731         return;
732     }
733     p_current_item = playlist_CurrentPlayingItem( p_playlist );
734     p_new_history_item->psz_uri = GetTimedURLFromPlaylistItem( p_intf,
735             p_current_item );
736     p_new_history_item->psz_name = p_new_history_item->psz_uri;
737
738     /* Go forward in the history, saving the currently-playing item */
739     (void) history_GoForwardSavingCurrentItem( p_history, p_new_history_item );
740     p_history_item = history_Item( p_history );
741
742 #ifdef CMML_INTF_DEBUG
743     msg_Dbg( p_intf, "retrieving item from h index %d", p_history->i_index );
744     msg_Dbg( p_intf, "got next history item: %p", p_history_item );
745     msg_Dbg( p_intf, "next history item URL: \"%s\"", p_history_item->psz_uri );
746 #endif
747
748     ReplacePlaylistItem( p_playlist, p_history_item->psz_uri );
749     pl_Release( p_intf );
750 }
751
752 static void ReplacePlaylistItem( playlist_t *p_playlist, char *psz_uri )
753 {
754     playlist_Stop( p_playlist );
755     (void) playlist_Add( p_playlist, psz_uri, psz_uri,
756                          PLAYLIST_INSERT /* FIXME: used to be PLAYLIST_REPLACE */, PLAYLIST_END|PLAYLIST_GO, true /* FIXME: p_playlist->status.i_index */,
757                          false);
758 }
759
760 /****************************************************************************
761  * DisplayAnchor: displays an anchor on the given video output
762  ****************************************************************************/
763 static int DisplayAnchor( intf_thread_t *p_intf,
764         vout_thread_t *p_vout,
765         char *psz_anchor_description,
766         char *psz_anchor_url )
767 {
768     int i_margin_h, i_margin_v;
769     mtime_t i_now;
770
771     i_margin_h = 0;
772     i_margin_v = 10;
773
774     i_now = mdate();
775
776     if( p_vout )
777     {
778         if( psz_anchor_url )
779         {
780             /* Should display subtitle underlined and in blue, but it looks
781              * like VLC doesn't implement any text styles yet.  D'oh! */
782             // p_style = &blue_with_underline;
783
784         }
785
786         /* TODO: p_subpicture doesn't have the proper i_x and i_y
787          * coordinates.  Need to look at the subpicture display system to
788          * work out why. */
789         if ( vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
790                 psz_anchor_description, NULL, OSD_ALIGN_BOTTOM,
791                 i_margin_h, i_margin_v, i_now, 0 ) == VLC_SUCCESS )
792         {
793             /* Displayed successfully */
794         }
795         else
796         {
797             return VLC_EGENERIC;
798         }
799     }
800     else
801     {
802         msg_Dbg( p_intf, "DisplayAnchor couldn't find a video output" );
803         return VLC_EGENERIC;
804     }
805
806     return VLC_SUCCESS;
807 }
808
809 static history_t * GetHistory( playlist_t *p_playlist )
810 {
811     vlc_value_t val;
812     history_t *p_history = NULL;
813
814     if( var_Get( p_playlist, "navigation-history", &val ) != VLC_SUCCESS )
815     {
816         /* history doesn't exist yet: need to create it */
817         history_t *new_history = history_New();
818         val.p_address = new_history;
819         var_Create( p_playlist, "navigation-history",
820                 VLC_VAR_ADDRESS|VLC_VAR_DOINHERIT );
821         if( var_Set( p_playlist, "navigation-history", val ) != VLC_SUCCESS )
822         {
823             msg_Warn( p_playlist, "could not initialise history" );
824         }
825         else
826         {
827             p_history = new_history;
828 #ifdef CMML_INTF_HISTORY_DEBUG
829             msg_Dbg( p_playlist, "nav history created at %p", new_history );
830             msg_Dbg( p_playlist, "nav history index:%d, p_xarray:%p",
831                      p_history->i_index, p_history->p_xarray );
832 #endif
833         }
834     }
835     else
836     {
837         p_history = val.p_address;
838 #ifdef CMML_INTF_HISTORY_DEBUG
839         msg_Dbg( p_playlist, "nav history retrieved from %p", p_history );
840 #endif
841     }
842
843     return p_history;
844 }
845