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