intf.c 27.1 KB
Newer Older
1
2
3
4
5
/*****************************************************************************
 * intf.c: interface for CMML annotations/hyperlinks
 *****************************************************************************
 * Copyright (C) 2003-2004 Commonwealth Scientific and Industrial Research
 *                         Organisation (CSIRO) Australia
6
 * Copyright (C) 2004 the VideoLAN team
7
8
9
10
11
12
13
14
15
 *
 * $Id$
 *
 * Authors: Andre Pang <Andre.Pang@csiro.au>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
16
 *
17
18
19
20
21
22
23
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25
26
27
28
29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
30
31
32
33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

Rémi Denis-Courmont's avatar
*Ahem*    
Rémi Denis-Courmont committed
34
35
#include <vlc/vlc.h>

36
#include <stdio.h>
37

38
39
40
41
#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif

zorglub's avatar
zorglub committed
42
43
44
45
46
#include <vlc_codec.h>
#include <vlc_input.h>
#include <vlc_interface.h>
#include <vlc_vout.h>
#include <vlc_playlist.h>
47
#include <vlc_osd.h>
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

#include "vlc_keys.h"

#include "browser_open.h"
#include "history.h"
#include "xstrcat.h"
#include "xurl.h"

#undef  CMML_INTF_USE_TIMED_URIS

#undef  CMML_INTF_DEBUG
#undef  CMML_INTF_HISTORY_DEBUG

/*****************************************************************************
 * intf_sys_t: description and status of interface
 *****************************************************************************/
struct intf_sys_t
{
    decoder_t *         p_cmml_decoder;
    input_thread_t *    p_input;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
69
    int                 i_key_action;
70
71
72
73
74
75
76
77
78
79
80
};

struct navigation_history_t
{
    int i_history_size;
    int i_last_item;
};

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
81

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
82
83
int          OpenIntf               ( vlc_object_t * );
void         CloseIntf              ( vlc_object_t * );
84

85
86
87
88
89
90
91
92
93
94
static int   InitThread                 ( intf_thread_t * );
static int   MouseEvent                 ( vlc_object_t *, char const *,
                                          vlc_value_t, vlc_value_t, void * );
static int   KeyEvent                   ( vlc_object_t *, char const *,
                                          vlc_value_t, vlc_value_t, void * );

static void  FollowAnchor               ( intf_thread_t * );
static void  GoBack                     ( intf_thread_t * );
static void  GoForward                  ( intf_thread_t * );

Andre Pang's avatar
Andre Pang committed
95
96
97
98
99
100
101
static int   FollowAnchorCallback       ( vlc_object_t *, char const *,
                                          vlc_value_t, vlc_value_t, void * );
static int   GoBackCallback             ( vlc_object_t *, char const *,
                                          vlc_value_t, vlc_value_t, void * );
static int   GoForwardCallback          ( vlc_object_t *, char const *,
                                          vlc_value_t, vlc_value_t, void * );

102
103
104
105
106
static char *GetTimedURLFromPlaylistItem( intf_thread_t *, playlist_item_t * );
static char *GetTimedURIFragmentForTime ( int );
static int   GetCurrentTimeInSeconds    ( input_thread_t * );
static int   DisplayAnchor              ( intf_thread_t *, vout_thread_t *,
                                          char *, char * );
Andre Pang's avatar
Andre Pang committed
107
static int   DisplayPendingAnchor       ( intf_thread_t *, vout_thread_t * );
108
109
110
111
112
113
114
115
116
static history_t * GetHistory           ( playlist_t * );
static void  ReplacePlaylistItem        ( playlist_t *, char * );

/* Exported functions */
static void RunIntf        ( intf_thread_t *p_intf );

/*****************************************************************************
 * OpenIntf: initialize CMML interface
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
117
int OpenIntf ( vlc_object_t *p_this )
118
119
120
121
122
123
124
125
126
127
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;

    p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
    if( p_intf->p_sys == NULL )
    {
        return( 1 );
    };

    p_intf->pf_run = RunIntf;
Andre Pang's avatar
Andre Pang committed
128

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
129
    var_AddCallback( p_intf->p_libvlc, "key-action", KeyEvent, p_intf );
130
131
132
    /* we also need to add the callback for "mouse-clicked", but do that later
     * when we've found a p_vout */

133
134
    var_Create( p_intf->p_libvlc, "browse-go-back", VLC_VAR_VOID );
    var_AddCallback( p_intf->p_libvlc, "browse-go-back",
Andre Pang's avatar
Andre Pang committed
135
                     GoBackCallback, p_intf );
136
137
    var_Create( p_intf->p_libvlc, "browse-go-forward", VLC_VAR_VOID );
    var_AddCallback( p_intf->p_libvlc, "browse-go-forward",
Andre Pang's avatar
Andre Pang committed
138
                     GoForwardCallback, p_intf );
139
140
    var_Create( p_intf->p_libvlc, "browse-follow-anchor", VLC_VAR_VOID );
    var_AddCallback( p_intf->p_libvlc, "browse-follow-anchor",
Andre Pang's avatar
Andre Pang committed
141
142
                     FollowAnchorCallback, p_intf );

143
144
145
146
147
148
    return( 0 );
}

/*****************************************************************************
 * CloseIntf: destroy dummy interface
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
149
void CloseIntf ( vlc_object_t *p_this )
150
151
152
153
154
155
156
157
{
    intf_thread_t * p_intf = (intf_thread_t *)p_this;
    vout_thread_t * p_vout;

#ifdef CMML_INTF_DEBUG
    msg_Dbg( p_intf, "freeing CMML interface" );
#endif

zorglub's avatar
zorglub committed
158
    /* erase the anchor text description from the video output if it exists */
159
    p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
160
    if( p_vout )
161
    {
zorglub's avatar
zorglub committed
162
        /* enable CMML as a subtitle track */
163
164
        spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
        vlc_object_release( p_vout );
165
166
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
167
    var_DelCallback( p_intf->p_libvlc, "key-action", KeyEvent, p_intf );
168
169

    vlc_object_release( p_intf->p_sys->p_cmml_decoder );
zorglub's avatar
zorglub committed
170

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    free( p_intf->p_sys );
}


/*****************************************************************************
 * RunIntf: main loop
 *****************************************************************************/
static void RunIntf( intf_thread_t *p_intf )
{
    vout_thread_t * p_vout = NULL;

    if( InitThread( p_intf ) < 0 )
    {
        msg_Err( p_intf, "can't initialize CMML interface" );
        return;
    }
#ifdef CMML_INTF_DEBUG
    msg_Dbg( p_intf, "CMML intf initialized" );
#endif

    /* Main loop */
    while( !p_intf->b_die )
    {
194
195
196
197
198
199
200
        /* if video output is dying, disassociate ourselves from it */
        if( p_vout && p_vout->b_die )
        {
            var_DelCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
            vlc_object_release( p_vout );
            p_vout = NULL;
        }
201

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
        /* find a video output if we currently don't have one */
        if( p_vout == NULL )
        {
            p_vout = vlc_object_find( p_intf->p_sys->p_input,
                                      VLC_OBJECT_VOUT, FIND_CHILD );
            if( p_vout )
            {
#ifdef CMML_INTF_DEBUG
                msg_Dbg( p_intf, "found vout thread" );
#endif
                var_AddCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
            }
        }

        vlc_mutex_lock( &p_intf->change_lock );

        /*
         * keyboard event
         */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
221
        switch( p_intf->p_sys->i_key_action )
222
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
223
224
225
226
227
228
229
230
231
232
233
            case ACTIONID_NAV_ACTIVATE:
                FollowAnchor( p_intf );
                break;
            case ACTIONID_HISTORY_BACK:
                GoBack( p_intf );
                break;
            case ACTIONID_HISTORY_FORWARD:
                GoForward( p_intf );
                break;
            default:
                break;
Andre Pang's avatar
Andre Pang committed
234
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
235
        p_intf->p_sys->i_key_action = 0;
236
237
        vlc_mutex_unlock( &p_intf->change_lock );

Andre Pang's avatar
Andre Pang committed
238
        (void) DisplayPendingAnchor( p_intf, p_vout );
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

        /* Wait a bit */
        msleep( INTF_IDLE_SLEEP );
    }

    /* if we're here, the video output is dying: release the vout object */

    if( p_vout )
    {
        var_DelCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
        vlc_object_release( p_vout );
    }

    vlc_object_release( p_intf->p_sys->p_input );
}

Andre Pang's avatar
Andre Pang committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*****************************************************************************
 * DisplayPendingAnchor: get a pending anchor description/URL from the CMML
 * decoder and display it on screen
 *****************************************************************************/
static int DisplayPendingAnchor( intf_thread_t *p_intf, vout_thread_t *p_vout )
{
    decoder_t *p_cmml_decoder;
    char *psz_description = NULL;
    char *psz_url = NULL;

    intf_thread_t *p_primary_intf;
    vlc_value_t val;

    p_cmml_decoder = p_intf->p_sys->p_cmml_decoder;
    if( var_Get( p_cmml_decoder, "psz-current-anchor-description", &val )
            != VLC_SUCCESS )
    {
272
        return true;
Andre Pang's avatar
Andre Pang committed
273
274
275
    }

    if( !val.p_address )
276
        return true;
Andre Pang's avatar
Andre Pang committed
277
278
279
280
281
282
283
284
285
286
287
288

    psz_description = val.p_address;

    if( var_Get( p_cmml_decoder, "psz-current-anchor-url", &val )
            == VLC_SUCCESS )
    {
        psz_url = val.p_address;
    }

    if( p_vout != NULL )
    {
        /* don't display anchor if main interface can display it */
289
        p_primary_intf = vlc_object_find( p_intf->p_libvlc, VLC_OBJECT_INTF,
Andre Pang's avatar
Andre Pang committed
290
291
292
293
294
295
296
                FIND_CHILD );

        if( p_primary_intf )
        {
            if( var_Get( p_primary_intf, "intf-displays-cmml-description", &val )
                    == VLC_SUCCESS )
            {
297
                if( val.b_bool == true )
298
299
                {
                    vlc_object_release( p_primary_intf );
300
                    return true;
301
                }
Andre Pang's avatar
Andre Pang committed
302
            }
303
304

            vlc_object_release( p_primary_intf );
Andre Pang's avatar
Andre Pang committed
305
306
307
308
309
310
311
        }

        /* display anchor as subtitle on-screen */
        if( DisplayAnchor( p_intf, p_vout, psz_description, psz_url )
                != VLC_SUCCESS )
        {
            /* text render unsuccessful: do nothing */
312
            return false;
Andre Pang's avatar
Andre Pang committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
        }

        /* text render successful: clear description */
        val.p_address = NULL;
        if( var_Set( p_cmml_decoder, "psz-current-anchor-description", val )
                != VLC_SUCCESS )
        {
            msg_Dbg( p_intf,
                     "reset of psz-current-anchor-description failed" );
        }
        free( psz_description );
        psz_url = NULL;
    }

327
    return true;
Andre Pang's avatar
Andre Pang committed
328
329
330
}


331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*****************************************************************************
 * InitThread:
 *****************************************************************************/
static int InitThread( intf_thread_t * p_intf )
{
    /* We might need some locking here */
    if( !p_intf->b_die )
    {
        input_thread_t * p_input;
        decoder_t *p_cmml_decoder;

        p_cmml_decoder = vlc_object_find( p_intf, VLC_OBJECT_DECODER, FIND_PARENT );
        p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );

#ifdef CMML_INTF_DEBUG
        msg_Dbg( p_intf, "cmml decoder at %p, input thread at %p",
                 p_cmml_decoder, p_input );
#endif

        /* Maybe the input just died */
        if( p_input == NULL )
        {
            return VLC_EGENERIC;
        }

        vlc_mutex_lock( &p_intf->change_lock );

        p_intf->p_sys->p_input = p_input;
        p_intf->p_sys->p_cmml_decoder = p_cmml_decoder;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
361
        p_intf->p_sys->i_key_action = 0;
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378

        vlc_mutex_unlock( &p_intf->change_lock );

        return VLC_SUCCESS;
    }
    else
    {
        return VLC_EGENERIC;
    }
}

/*****************************************************************************
 * MouseEvent: callback for mouse events
 *****************************************************************************/
static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
379
380
381
    VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
    VLC_UNUSED(oldval); VLC_UNUSED(newval);
    VLC_UNUSED(p_data);
382
383
384
385
386
387
388
389
390
391
392
    /* TODO: handle mouse clicks on the anchor text */

    return VLC_SUCCESS;
}

/*****************************************************************************
 * KeyEvent: callback for keyboard events
 *****************************************************************************/
static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
393
394
    VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
    VLC_UNUSED(oldval); VLC_UNUSED(newval);
395
396
397
    intf_thread_t *p_intf = (intf_thread_t *)p_data;
    vlc_mutex_lock( &p_intf->change_lock );

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
398
    p_intf->p_sys->i_key_action = newval.i_int;
399

400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
    vlc_mutex_unlock( &p_intf->change_lock );

    return VLC_SUCCESS;
}

/*****************************************************************************
 * FollowAnchor: follow the current anchor being displayed to the user
 *****************************************************************************/
static void FollowAnchor ( intf_thread_t *p_intf )
{
    intf_sys_t *p_sys;
    decoder_t *p_cmml_decoder;
    char *psz_url = NULL;
    vlc_value_t val;

    msg_Dbg( p_intf, "User followed anchor" );

    p_sys = p_intf->p_sys;
    p_cmml_decoder = p_sys->p_cmml_decoder;

    if( var_Get( p_cmml_decoder, "psz-current-anchor-url", &val ) ==
            VLC_SUCCESS )
    {
        if( val.p_address ) psz_url = val.p_address;
    }

#ifdef CMML_INTF_DEBUG
    msg_Dbg( p_intf, "Current URL is \"%s\"", psz_url );
#endif

    if( psz_url )
    {
        playlist_t *p_playlist;
        playlist_item_t *p_current_item;
        char *psz_uri_to_load;
Eric Petit's avatar
Eric Petit committed
435
436
        mtime_t i_seconds;
        vlc_value_t time;
437

438
        p_playlist = (playlist_t *) vlc_object_find( p_intf,
439
440
441
442
443
444
445
446
                VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
        if ( !p_playlist )
        {
            msg_Warn( p_intf, "can't find playlist" );
            return;
        }

        /* Get new URL */
447
        p_current_item = p_playlist->status.p_item;
448
        char *psz_uri = input_item_GetURI( p_current_item->p_input );
449
#ifdef CMML_INTF_DEBUG
450
        msg_Dbg( p_intf, "Current playlist item URL is \"%s\"", psz_uri );
451
452
#endif

453
454
        psz_uri_to_load = XURL_Concat( psz_uri, psz_url );
        free( psz_uri );
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476

#ifdef CMML_INTF_DEBUG
        msg_Dbg( p_intf, "URL to load is \"%s\"", psz_uri_to_load );
#endif

        if( var_Get( p_intf->p_sys->p_input, "time", &time ) )
        {
            msg_Dbg( p_intf, "couldn't get time from current clip" );
            time.i_time = 0;
        }
        i_seconds = time.i_time / 1000000;
#ifdef CMML_INTF_DEBUG
        msg_Dbg( p_intf, "Current time is \"%lld\"", i_seconds );
#endif

        /* TODO: we need a (much) more robust way of detecting whether
         * the file's a media file ... */
        if( strstr( psz_uri_to_load, ".anx" ) != NULL )
        {
            history_t *p_history = NULL;
            history_item_t *p_history_item = NULL;
            char *psz_timed_url;
zorglub's avatar
zorglub committed
477

478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
            p_history = GetHistory( p_playlist );

            /* create history item */
            psz_timed_url = GetTimedURLFromPlaylistItem( p_intf, p_current_item );
            p_history_item = historyItem_New( psz_timed_url, psz_timed_url );
            free( psz_timed_url );

            if( !p_history_item )
            {
                msg_Warn( p_intf, "could not initialise history item" );
            }
            else
            {
#ifdef CMML_INTF_DEBUG
                msg_Dbg( p_intf, "history pre-index %d", p_history->i_index );
#endif
                history_PruneAndInsert( p_history, p_history_item );
#ifdef CMML_INTF_DEBUG
                msg_Dbg( p_intf, "new history item at %p, uri is \"%s\"",
                         p_history_item, p_history_item->psz_uri );
                msg_Dbg( p_intf, "history index now %d", p_history->i_index );
#endif
            }

            /* free current-anchor-url */
            free( psz_url );
            val.p_address = NULL;
            if( var_Set( p_cmml_decoder, "psz-current-anchor-url", val ) !=
                    VLC_SUCCESS )
            {
                msg_Dbg( p_intf, "couldn't reset psz-current-anchor-url" );
            }

            ReplacePlaylistItem( p_playlist, psz_uri_to_load );
        }
        else
        {
Andre Pang's avatar
Andre Pang committed
515
516
517
#ifdef CMML_INTF_DEBUG
            msg_Dbg( p_intf, "calling browser_Open with \"%s\"", psz_url );
#endif
518
            (void) browser_Open( psz_url );
519
            playlist_Control( p_playlist, PLAYLIST_PAUSE, true, 0 );
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
        }

        free( psz_uri_to_load );

        vlc_object_release( p_playlist );
    }
}

static
char *GetTimedURLFromPlaylistItem( intf_thread_t *p_intf,
        playlist_item_t *p_current_item )
{
#ifdef CMML_INTF_USE_TIMED_URIS
    char *psz_url = NULL;
    char *psz_return_value = NULL;
    char *psz_seconds = NULL;
    int i_seconds;
537

538
539
540
    char *psz_uri = input_item_GetURI( p_current_item->p_input );
    psz_url = XURL_GetWithoutFragment( psz_uri );
    free( psz_uri );
541
542

    /* Get current time as a string */
543
    if( XURL_IsFileURL( psz_url ) == true )
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
        psz_url = xstrcat( psz_url, "#" );
    else
        psz_url = xstrcat( psz_url, "?" );

    /* jump back to 2 seconds before where we are now */
    i_seconds = GetCurrentTimeInSeconds( p_intf->p_sys->p_input ) - 2;
    psz_seconds = GetTimedURIFragmentForTime( i_seconds < 0 ? 0 : i_seconds );
    if( psz_seconds )
    {
        psz_url = xstrcat( psz_url, psz_seconds );
        free( psz_seconds );
        psz_return_value = psz_url;
    }

    return psz_return_value;
#else
560
    VLC_UNUSED(p_intf);
561
562
563
564
565
566
    void *p;

    /* Suppress warning messages about unused functions */
    p = GetTimedURIFragmentForTime; /* unused */
    p = GetCurrentTimeInSeconds;    /* unused */

567
    return input_item_GetURI( p_current_item->p_input );
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#endif
}


/*
 * Get the current time, rounded down to the nearest second
 *
 * http://www.ietf.org/internet-drafts/draft-pfeiffer-temporal-fragments-02.txt
 */
static
int GetCurrentTimeInSeconds( input_thread_t *p_input )
{
    vlc_value_t time;
    mtime_t i_seconds;

    var_Get( p_input, "time", &time );
    i_seconds = time.i_time / 1000000;
    return i_seconds;
}

static
char *GetTimedURIFragmentForTime( int seconds )
{
    char *psz_time;

    asprintf( &psz_time, "%d", seconds );
    return psz_time;
}

Andre Pang's avatar
Andre Pang committed
597
598
599
600
static
int GoBackCallback( vlc_object_t *p_this, char const *psz_var,
                    vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
601
602
    VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
    VLC_UNUSED(oldval); VLC_UNUSED(newval);
Andre Pang's avatar
Andre Pang committed
603
604
605
606
607
608
609
610
611
    intf_thread_t *p_intf = (intf_thread_t *) p_data;
    GoBack( p_intf );
    return VLC_SUCCESS;
}

static
int GoForwardCallback( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
612
613
    VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
    VLC_UNUSED(oldval); VLC_UNUSED(newval);
Andre Pang's avatar
Andre Pang committed
614
615
616
617
618
619
620
621
622
623
    intf_thread_t *p_intf = (intf_thread_t *) p_data;
    GoForward( p_intf );
    return VLC_SUCCESS;
}

static
int FollowAnchorCallback( vlc_object_t *p_this, char const *psz_var,
                          vlc_value_t oldval, vlc_value_t newval,
                          void *p_data )
{
624
625
    VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
    VLC_UNUSED(oldval); VLC_UNUSED(newval);
Andre Pang's avatar
Andre Pang committed
626
627
628
629
630
    intf_thread_t *p_intf = (intf_thread_t *) p_data;
    FollowAnchor( p_intf );
    return VLC_SUCCESS;
}

631
632
633
634
635
636
637
638
639
static
void GoBack( intf_thread_t *p_intf )
{
    vlc_value_t history;
    history_t *p_history = NULL;
    history_item_t *p_history_item = NULL;
    history_item_t *p_new_history_item = NULL;
    playlist_t *p_playlist = NULL;
    char *psz_timed_url = NULL;
Eric Petit's avatar
Eric Petit committed
640
    playlist_item_t *p_current_item;
641
642
643
644
645
646

#ifdef CMML_INTF_DEBUG
    msg_Dbg( p_intf, "Going back in navigation history" );
#endif

    /* Find the playlist */
647
    p_playlist = (playlist_t *) vlc_object_find( p_intf,
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
            VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
    if ( !p_playlist )
    {
        msg_Warn( p_intf, "can't find playlist" );
        return;
    }

    /* Retrieve navigation history from playlist */
    if( var_Get( p_playlist, "navigation-history", &history ) != VLC_SUCCESS ||
        !history.p_address )
    {
        /* History doesn't exist yet: ignore user's request */
        msg_Warn( p_intf, "can't go back: no history exists yet" );
        vlc_object_release( p_playlist );
        return;
    }

    p_history = history.p_address;
#ifdef CMML_INTF_DEBUG
    msg_Dbg( p_intf, "back: nav history retrieved from %p", p_history );
    msg_Dbg( p_intf, "nav history index:%d, p_xarray:%p", p_history->i_index,
             p_history->p_xarray );
#endif

    /* Check whether we can go back in the history */
673
    if( history_CanGoBack( p_history ) == false )
674
675
676
677
678
679
    {
        msg_Warn( p_intf, "can't go back: already at beginning of history" );
        vlc_object_release( p_playlist );
        return;
    }

680
    p_current_item = p_playlist->status.p_item;
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717

    /* Save the currently-playing media in a new history item */
    psz_timed_url = GetTimedURLFromPlaylistItem( p_intf, p_current_item );
    p_new_history_item = historyItem_New( psz_timed_url, psz_timed_url );
    free( psz_timed_url );

    if( !p_new_history_item )
    {
#ifdef CMML_INTF_DEBUG
        msg_Dbg( p_intf, "back: could not initialise new history item" );
#endif
        vlc_object_release( p_playlist );
        return;
    }

    /* Go back in the history, saving the currently-playing item */
    (void) history_GoBackSavingCurrentItem( p_history, p_new_history_item );
    p_history_item = history_Item( p_history );

#ifdef CMML_INTF_DEBUG
    msg_Dbg( p_intf, "retrieving item from h index %d", p_history->i_index );
    msg_Dbg( p_intf, "got previous history item: %p", p_history_item );
    msg_Dbg( p_intf, "prev history item URL: \"%s\"", p_history_item->psz_uri );
#endif

    ReplacePlaylistItem( p_playlist, p_history_item->psz_uri );
    vlc_object_release( p_playlist );
}

static
void GoForward( intf_thread_t *p_intf )
{
    vlc_value_t history;
    history_t *p_history = NULL;
    history_item_t *p_history_item = NULL;
    history_item_t *p_new_history_item = NULL;
    playlist_t *p_playlist = NULL;
Eric Petit's avatar
Eric Petit committed
718
    playlist_item_t *p_current_item;
719
720
721
722
723
724

#ifdef CMML_INTF_DEBUG
    msg_Dbg( p_intf, "Going forward in navigation history" );
#endif

    /* Find the playlist */
725
    p_playlist = (playlist_t *) vlc_object_find( p_intf,
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
            VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
    if ( !p_playlist )
    {
        msg_Warn( p_intf, "can't find playlist" );
        return;
    }

    /* Retrieve navigation history from playlist */
    if( var_Get( p_playlist, "navigation-history", &history ) != VLC_SUCCESS ||
        !history.p_address )
    {
        /* History doesn't exist yet: ignore user's request */
        msg_Warn( p_intf, "can't go back: no history exists yet" );
        vlc_object_release( p_playlist );
        return;
    }

    p_history = history.p_address;
#ifdef CMML_INTF_DEBUG
    msg_Dbg( p_intf, "forward: nav history retrieved from %p", p_history );
    msg_Dbg( p_intf, "nav history index:%d, p_xarray:%p", p_history->i_index,
             p_history->p_xarray );
#endif

    /* Check whether we can go forward in the history */
751
    if( history_CanGoForward( p_history ) == false )
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
    {
        msg_Warn( p_intf, "can't go forward: already at end of history" );
        vlc_object_release( p_playlist );
        return;
    }

    /* Save the currently-playing media in a new history item */
    p_new_history_item = malloc( sizeof(history_item_t) );
    if( !p_new_history_item )
    {
#ifdef CMML_INTF_DEBUG
        msg_Dbg( p_intf, "forward: could not initialise new history item" );
#endif
        vlc_object_release( p_playlist );
        return;
    }
768
769
    p_current_item = p_playlist->status.p_item;
    p_new_history_item->psz_uri = GetTimedURLFromPlaylistItem( p_intf,
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
            p_current_item );
    p_new_history_item->psz_name = p_new_history_item->psz_uri;

    /* Go forward in the history, saving the currently-playing item */
    (void) history_GoForwardSavingCurrentItem( p_history, p_new_history_item );
    p_history_item = history_Item( p_history );

#ifdef CMML_INTF_DEBUG
    msg_Dbg( p_intf, "retrieving item from h index %d", p_history->i_index );
    msg_Dbg( p_intf, "got next history item: %p", p_history_item );
    msg_Dbg( p_intf, "next history item URL: \"%s\"", p_history_item->psz_uri );
#endif

    ReplacePlaylistItem( p_playlist, p_history_item->psz_uri );
    vlc_object_release( p_playlist );
}

static void ReplacePlaylistItem( playlist_t *p_playlist, char *psz_uri )
{
    playlist_Stop( p_playlist );
790
    (void) playlist_Add( p_playlist, psz_uri, psz_uri,
791
792
                         PLAYLIST_INSERT /* FIXME: used to be PLAYLIST_REPLACE */, PLAYLIST_END|PLAYLIST_GO, true /* FIXME: p_playlist->status.i_index */,
                         false);
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
}

/****************************************************************************
 * DisplayAnchor: displays an anchor on the given video output
 ****************************************************************************/
static int DisplayAnchor( intf_thread_t *p_intf,
        vout_thread_t *p_vout,
        char *psz_anchor_description,
        char *psz_anchor_url )
{
    int i_margin_h, i_margin_v;
    mtime_t i_now;

    i_margin_h = 0;
    i_margin_v = 10;

    i_now = mdate();

    if( p_vout )
    {
        if( psz_anchor_url )
        {
zorglub's avatar
zorglub committed
815
816
            /* Should display subtitle underlined and in blue, but it looks
             * like VLC doesn't implement any text styles yet.  D'oh! */
hartman's avatar
hartman committed
817
            // p_style = &blue_with_underline;
818
819
820
821
822
823

        }

        /* TODO: p_subpicture doesn't have the proper i_x and i_y
         * coordinates.  Need to look at the subpicture display system to
         * work out why. */
824
        if ( vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
hartman's avatar
hartman committed
825
                psz_anchor_description, NULL, OSD_ALIGN_BOTTOM,
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
                i_margin_h, i_margin_v, i_now, 0 ) == VLC_SUCCESS )
        {
            /* Displayed successfully */
        }
        else
        {
            return VLC_EGENERIC;
        }
    }
    else
    {
        msg_Dbg( p_intf, "DisplayAnchor couldn't find a video output" );
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}

static history_t * GetHistory( playlist_t *p_playlist )
{
    vlc_value_t val;
    history_t *p_history = NULL;

    if( var_Get( p_playlist, "navigation-history", &val ) != VLC_SUCCESS )
    {
        /* history doesn't exist yet: need to create it */
        history_t *new_history = history_New();
        val.p_address = new_history;
        var_Create( p_playlist, "navigation-history",
                VLC_VAR_ADDRESS|VLC_VAR_DOINHERIT );
        if( var_Set( p_playlist, "navigation-history", val ) != VLC_SUCCESS )
        {
            msg_Warn( p_playlist, "could not initialise history" );
        }
        else
        {
            p_history = new_history;
#ifdef CMML_INTF_HISTORY_DEBUG
            msg_Dbg( p_playlist, "nav history created at %p", new_history );
            msg_Dbg( p_playlist, "nav history index:%d, p_xarray:%p",
                     p_history->i_index, p_history->p_xarray );
#endif
        }
    }
    else
    {
        p_history = val.p_address;
#ifdef CMML_INTF_HISTORY_DEBUG
        msg_Dbg( p_playlist, "nav history retrieved from %p", p_history );
#endif
    }

    return p_history;
}