rss.c 33.5 KB
Newer Older
1
/*****************************************************************************
2
 * rss.c : rss/atom feed display video plugin for vlc
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2003-2006 VLC authors and VideoLAN
5 6
 * $Id$
 *
7
 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8
 *          Rémi Duraffort <ivoire -at- videolan -dot- org>
9
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24
 *****************************************************************************/

25 26 27 28 29
/*****************************************************************************
 * Atom : http://www.ietf.org/rfc/rfc4287.txt
 * RSS : http://www.rssboard.org/rss-specification
 *****************************************************************************/

30 31 32 33
/*****************************************************************************
 * Preamble
 *****************************************************************************/

34 35 36 37
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

38 39
#include <time.h>

40
#include <vlc_common.h>
41
#include <vlc_plugin.h>
Rémi Duraffort's avatar
Rémi Duraffort committed
42
#include <vlc_filter.h>
43
#include <vlc_subpicture.h>
Rémi Duraffort's avatar
Rémi Duraffort committed
44 45 46
#include <vlc_block.h>
#include <vlc_stream.h>
#include <vlc_xml.h>
47
#include <vlc_charset.h>
Rémi Duraffort's avatar
Rémi Duraffort committed
48
#include <vlc_image.h>
49

50 51 52 53 54 55 56
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  CreateFilter ( vlc_object_t * );
static void DestroyFilter( vlc_object_t * );
static subpicture_t *Filter( filter_t *, mtime_t );

57 58
static struct rss_feed_t *FetchRSS( filter_t * );
static void FreeRSS( struct rss_feed_t *, int );
59
static int ParseUrls( filter_t *, char * );
60

61 62
static void Fetch( void * );

63 64
static const int pi_color_values[] = {
               0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0,
65 66 67
               0x00FFFFFF, 0x00800000, 0x00FF0000, 0x00FF00FF, 0x00FFFF00,
               0x00808000, 0x00008000, 0x00008080, 0x0000FF00, 0x00800080,
               0x00000080, 0x000000FF, 0x0000FFFF};
68 69
static const char *const ppsz_color_descriptions[] = {
               N_("Default"), N_("Black"),
70 71 72 73 74 75 76 77 78
               N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"), N_("Red"),
               N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"),
               N_("Teal"), N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"),
               N_("Aqua") };

/*****************************************************************************
 * filter_sys_t: rss filter descriptor
 *****************************************************************************/

79
typedef struct rss_item_t
80 81 82 83
{
    char *psz_title;
    char *psz_description;
    char *psz_link;
84
} rss_item_t;
85

86
typedef struct rss_feed_t
87
{
88
    char *psz_url;
89 90 91
    char *psz_title;
    char *psz_description;
    char *psz_link;
92 93
    char *psz_image;
    picture_t *p_pic;
94 95

    int i_items;
96
    rss_item_t *p_items;
97
} rss_feed_t;
98 99 100

struct filter_sys_t
{
101
    vlc_mutex_t lock;
102 103
    vlc_timer_t timer;  /* Timer to refresh the rss feeds */
    bool b_fetched;
104

105 106 107 108 109 110 111
    int i_xoff, i_yoff;  /* offsets for the display string in the video window */
    int i_pos; /* permit relative positioning (top, bottom, left, right, center) */
    int i_speed;
    int i_length;

    char *psz_marquee;    /* marquee string */

112
    text_style_t *p_style; /* font control */
113 114 115 116

    mtime_t last_date;

    int i_feeds;
117
    rss_feed_t *p_feeds;
118

119
    bool b_images;
120
    int i_title;
121

122 123 124 125 126
    int i_cur_feed;
    int i_cur_item;
    int i_cur_char;
};

127
#define MSG_TEXT N_("Feed URLs")
Christophe Mutricy's avatar
Christophe Mutricy committed
128
#define MSG_LONGTEXT N_("RSS/Atom feed '|' (pipe) separated URLs.")
129
#define SPEED_TEXT N_("Speed of feeds")
130
#define SPEED_LONGTEXT N_("Speed of the RSS/Atom feeds in microseconds (bigger is slower).")
131 132 133 134 135 136 137 138 139 140 141 142 143
#define LENGTH_TEXT N_("Max length")
#define LENGTH_LONGTEXT N_("Maximum number of characters displayed on the " \
                "screen." )
#define TTL_TEXT N_("Refresh time")
#define TTL_LONGTEXT N_("Number of seconds between each forced refresh " \
        "of the feeds. 0 means that the feeds are never updated." )
#define IMAGE_TEXT N_("Feed images")
#define IMAGE_LONGTEXT N_("Display feed images if available.")

#define POSX_TEXT N_("X offset")
#define POSX_LONGTEXT N_("X offset, from the left screen edge." )
#define POSY_TEXT N_("Y offset")
#define POSY_LONGTEXT N_("Y offset, down from the top." )
144
#define OPACITY_TEXT N_("Opacity")
145
#define OPACITY_LONGTEXT N_("Opacity (inverse of transparency) of " \
146
    "overlay text. 0 = transparent, 255 = totally opaque." )
147

148
#define SIZE_TEXT N_("Font size, pixels")
149
#define SIZE_LONGTEXT N_("Font size, in pixels. Default is 0 (use default " \
150
    "font size)." )
151

152 153 154 155 156
#define COLOR_TEXT N_("Color")
#define COLOR_LONGTEXT N_("Color of the text that will be rendered on "\
    "the video. This must be an hexadecimal (like HTML colors). The first two "\
    "chars are for red, then green, then blue. #000000 = black, #FF0000 = red,"\
    " #00FF00 = green, #FFFF00 = yellow (red + green), #FFFFFF = white" )
157

158
#define POS_TEXT N_("Text position")
159
#define POS_LONGTEXT N_( \
160
  "You can enforce the text position on the video " \
161
  "(0=center, 1=left, 2=right, 4=top, 8=bottom; you can " \
162
  "also use combinations of these values, eg 6 = top-right).")
163

164 165 166
#define TITLE_TEXT N_("Title display mode")
#define TITLE_LONGTEXT N_("Title display mode. Default is 0 (hidden) if the feed has an image and feed images are enabled, 1 otherwise.")

167 168
#define RSS_HELP N_("Display a RSS or ATOM Feed on your video")

169 170
static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
static const char *const ppsz_pos_descriptions[] =
171 172 173
     { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
     N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };

174 175 176 177 178 179
enum title_modes {
    default_title=-1,
    hide_title,
    prepend_title,
    scroll_title };

180 181
static const int pi_title_modes[] = { default_title, hide_title, prepend_title, scroll_title };
static const char *const ppsz_title_modes[] =
182 183 184
    { N_("Default"), N_("Don't show"), N_("Always visible"), N_("Scroll with feed") };

#define CFG_PREFIX "rss-"
185

186 187 188
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
189
vlc_module_begin ()
190
    set_capability( "sub source", 1 )
191
    set_shortname( N_("RSS / Atom") )
192
    set_help(RSS_HELP)
193 194 195
    set_callbacks( CreateFilter, DestroyFilter )
    set_category( CAT_VIDEO )
    set_subcategory( SUBCAT_VIDEO_SUBPIC )
196
    add_string( CFG_PREFIX "urls", NULL, MSG_TEXT, MSG_LONGTEXT, false )
197 198

    set_section( N_("Position"), NULL )
199 200 201
    add_integer( CFG_PREFIX "x", 0, POSX_TEXT, POSX_LONGTEXT, true )
    add_integer( CFG_PREFIX "y", 0, POSY_TEXT, POSY_LONGTEXT, true )
    add_integer( CFG_PREFIX "position", -1, POS_TEXT, POS_LONGTEXT, false )
202
        change_integer_list( pi_pos_values, ppsz_pos_descriptions )
203

204
    set_section( N_("Font"), NULL )
205
    /* 5 sets the default to top [1] left [4] */
206
    add_integer_with_range( CFG_PREFIX "opacity", 255, 0, 255,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
207
        OPACITY_TEXT, OPACITY_LONGTEXT, false )
208
    add_rgb( CFG_PREFIX "color", 0xFFFFFF, COLOR_TEXT, COLOR_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
209
                  false )
210
        change_integer_list( pi_color_values, ppsz_color_descriptions )
211 212
    add_integer( CFG_PREFIX "size", 0, SIZE_TEXT, SIZE_LONGTEXT, false )
        change_integer_range( 0, 4096)
213

214
    set_section( N_("Misc"), NULL )
215
    add_integer( CFG_PREFIX "speed", 100000, SPEED_TEXT, SPEED_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
216
                 false )
217
    add_integer( CFG_PREFIX "length", 60, LENGTH_TEXT, LENGTH_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
218
                 false )
219
    add_integer( CFG_PREFIX "ttl", 1800, TTL_TEXT, TTL_LONGTEXT, false )
220
    add_bool( CFG_PREFIX "images", true, IMAGE_TEXT, IMAGE_LONGTEXT, false )
221 222
    add_integer( CFG_PREFIX "title", default_title, TITLE_TEXT, TITLE_LONGTEXT,
                 false )
223
        change_integer_list( pi_title_modes, ppsz_title_modes )
224

225
    set_description( N_("RSS and Atom feed display") )
226
    add_shortcut( "rss", "atom" )
227
vlc_module_end ()
228

229
static const char *const ppsz_filter_options[] = {
230
    "urls", "x", "y", "position", "opacity", "color", "size", "speed", "length",
231
    "ttl", "images", "title", NULL
232 233
};

234 235 236 237 238 239 240
/*****************************************************************************
 * CreateFilter: allocates RSS video filter
 *****************************************************************************/
static int CreateFilter( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;
    filter_sys_t *p_sys;
241
    char *psz_urls;
242
    int i_ttl;
243 244 245 246 247 248

    /* Allocate structure */
    p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
    if( p_sys == NULL )
        return VLC_ENOMEM;

249 250 251
    config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
                       p_filter->p_cfg );

252 253 254 255 256 257 258 259 260
    /* Get the urls to parse: must be non empty */
    psz_urls = var_CreateGetNonEmptyString( p_filter, CFG_PREFIX "urls" );
    if( !psz_urls )
    {
        msg_Err( p_filter, "The list of urls must not be empty" );
        free( p_sys );
        return VLC_EGENERIC;
    }

261
    /* Fill the p_sys structure with the configuration */
262
    p_sys->i_title = var_CreateGetInteger( p_filter, CFG_PREFIX "title" );
263
    p_sys->i_cur_feed = 0;
264
    p_sys->i_cur_item = p_sys->i_title == scroll_title ? -1 : 0;
265
    p_sys->i_cur_char = 0;
266 267
    p_sys->i_feeds = 0;
    p_sys->p_feeds = NULL;
268 269 270
    p_sys->i_speed = var_CreateGetInteger( p_filter, CFG_PREFIX "speed" );
    p_sys->i_length = var_CreateGetInteger( p_filter, CFG_PREFIX "length" );
    p_sys->b_images = var_CreateGetBool( p_filter, CFG_PREFIX "images" );
271

272 273
    i_ttl = __MAX( 0, var_CreateGetInteger( p_filter, CFG_PREFIX "ttl" ) );

Rémi Duraffort's avatar
Rémi Duraffort committed
274
    p_sys->psz_marquee = malloc( p_sys->i_length + 1 );
275
    if( p_sys->psz_marquee == NULL )
276 277 278 279 280
    {
        free( psz_urls );
        free( p_sys );
        return VLC_ENOMEM;
    }
281
    p_sys->psz_marquee[p_sys->i_length] = '\0';
282

283
    p_sys->p_style = text_style_Create( STYLE_NO_DEFAULTS );
284
    if( p_sys->p_style == NULL )
Rémi Duraffort's avatar
Rémi Duraffort committed
285
        goto error;
286

287 288 289
    p_sys->i_xoff = var_CreateGetInteger( p_filter, CFG_PREFIX "x" );
    p_sys->i_yoff = var_CreateGetInteger( p_filter, CFG_PREFIX "y" );
    p_sys->i_pos = var_CreateGetInteger( p_filter, CFG_PREFIX "position" );
290
    p_sys->p_style->i_font_alpha = var_CreateGetInteger( p_filter, CFG_PREFIX "opacity" );
291
    p_sys->p_style->i_font_color = var_CreateGetInteger( p_filter, CFG_PREFIX "color" );
292
    p_sys->p_style->i_features |= STYLE_HAS_FONT_ALPHA | STYLE_HAS_FONT_COLOR;
293
    p_sys->p_style->i_font_size = var_CreateGetInteger( p_filter, CFG_PREFIX "size" );
294

295
    if( p_sys->b_images && p_sys->p_style->i_font_size == -1 )
296
    {
297 298
        msg_Warn( p_filter, "rss-size wasn't specified. Feed images will thus "
                            "be displayed without being resized" );
299 300
    }

301
    /* Parse the urls */
302 303
    if( ParseUrls( p_filter, psz_urls ) )
        goto error;
304

305
    /* Misc init */
306
    vlc_mutex_init( &p_sys->lock );
307
    p_filter->pf_sub_source = Filter;
308
    p_sys->last_date = (mtime_t)0;
309 310 311 312 313 314 315 316 317
    p_sys->b_fetched = false;

    /* Create and arm the timer */
    if( vlc_timer_create( &p_sys->timer, Fetch, p_filter ) )
    {
        vlc_mutex_destroy( &p_sys->lock );
        goto error;
    }
    vlc_timer_schedule( p_sys->timer, false, 1,
318
                        (mtime_t)(i_ttl)*1000000 );
319

320
    free( psz_urls );
321
    return VLC_SUCCESS;
Rémi Duraffort's avatar
Rémi Duraffort committed
322 323

error:
324 325
    if( p_sys->p_style )
        text_style_Delete( p_sys->p_style );
Rémi Duraffort's avatar
Rémi Duraffort committed
326
    free( p_sys->psz_marquee );
327
    free( psz_urls );
Rémi Duraffort's avatar
Rémi Duraffort committed
328
    free( p_sys );
329
    return VLC_ENOMEM;
330 331 332 333 334 335 336 337 338
}
/*****************************************************************************
 * DestroyFilter: destroy RSS video filter
 *****************************************************************************/
static void DestroyFilter( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;
    filter_sys_t *p_sys = p_filter->p_sys;

339
    vlc_timer_destroy( p_sys->timer );
340
    vlc_mutex_destroy( &p_sys->lock );
341

342
    text_style_Delete( p_sys->p_style );
343
    free( p_sys->psz_marquee );
344
    FreeRSS( p_sys->p_feeds, p_sys->i_feeds );
345 346 347 348 349 350 351 352 353 354 355 356
    free( p_sys );
}

/****************************************************************************
 * Filter: the whole thing
 ****************************************************************************
 * This function outputs subpictures at regular time intervals.
 ****************************************************************************/
static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
{
    filter_sys_t *p_sys = p_filter->p_sys;
    subpicture_t *p_spu;
357
    video_format_t fmt;
358
    subpicture_region_t *p_region;
359 360

    int i_feed, i_item;
361
    rss_feed_t *p_feed;
362

363 364
    memset( &fmt, 0, sizeof(video_format_t) );

365 366
    vlc_mutex_lock( &p_sys->lock );

367 368 369 370 371 372 373 374
    /* Check if the feeds have been fetched and that we have some feeds */
    /* TODO: check that we have items for each feeds */
    if( !p_sys->b_fetched && p_sys->i_feeds > 0 )
    {
        vlc_mutex_unlock( &p_sys->lock );
        return NULL;
    }

375
    if( p_sys->last_date
376 377
       + ( p_sys->i_cur_char == 0 &&
           p_sys->i_cur_item == ( p_sys->i_title == scroll_title ? -1 : 0 ) ? 5 : 1 )
378
           /* ( ... ? 5 : 1 ) means "wait 5 times more for the 1st char" */
379
       * p_sys->i_speed > date )
380 381 382 383
    {
        vlc_mutex_unlock( &p_sys->lock );
        return NULL;
    }
384 385 386

    p_sys->last_date = date;
    p_sys->i_cur_char++;
387 388 389 390

    if( p_sys->i_cur_item == -1 ?
            p_sys->p_feeds[p_sys->i_cur_feed].psz_title[p_sys->i_cur_char] == 0 :
            p_sys->p_feeds[p_sys->i_cur_feed].p_items[p_sys->i_cur_item].psz_title[p_sys->i_cur_char] == 0 )
391 392 393 394 395
    {
        p_sys->i_cur_char = 0;
        p_sys->i_cur_item++;
        if( p_sys->i_cur_item >= p_sys->p_feeds[p_sys->i_cur_feed].i_items )
        {
396 397 398 399
            if( p_sys->i_title == scroll_title )
                p_sys->i_cur_item = -1;
            else
                p_sys->i_cur_item = 0;
400 401 402 403
            p_sys->i_cur_feed = (p_sys->i_cur_feed + 1)%p_sys->i_feeds;
        }
    }

Laurent Aimar's avatar
Laurent Aimar committed
404
    p_spu = filter_NewSubpicture( p_filter );
405
    if( !p_spu )
406
    {
407
        vlc_mutex_unlock( &p_sys->lock );
408
        return NULL;
409
    }
410

411
    fmt.i_chroma = VLC_CODEC_TEXT;
412

413
    p_spu->p_region = subpicture_region_New( &fmt );
414 415
    if( !p_spu->p_region )
    {
416
        subpicture_Delete( p_spu );
417
        vlc_mutex_unlock( &p_sys->lock );
418 419 420
        return NULL;
    }

421 422
    /* Generate the string that will be displayed. This string is supposed to
       be p_sys->i_length characters long. */
423 424
    i_item = p_sys->i_cur_item;
    i_feed = p_sys->i_cur_feed;
425
    p_feed = &p_sys->p_feeds[i_feed];
426

427 428
    if( ( p_feed->p_pic && p_sys->i_title == default_title )
        || p_sys->i_title == hide_title )
429 430 431 432 433 434
    {
        /* Don't display the feed's title if we have an image */
        snprintf( p_sys->psz_marquee, p_sys->i_length, "%s",
                  p_sys->p_feeds[i_feed].p_items[i_item].psz_title
                  +p_sys->i_cur_char );
    }
435 436
    else if( ( !p_feed->p_pic && p_sys->i_title == default_title )
             || p_sys->i_title == prepend_title )
437 438 439 440 441 442
    {
        snprintf( p_sys->psz_marquee, p_sys->i_length, "%s : %s",
                  p_sys->p_feeds[i_feed].psz_title,
                  p_sys->p_feeds[i_feed].p_items[i_item].psz_title
                  +p_sys->i_cur_char );
    }
443 444 445 446 447 448 449 450 451 452 453
    else /* scrolling title */
    {
        if( i_item == -1 )
            snprintf( p_sys->psz_marquee, p_sys->i_length, "%s : %s",
                      p_sys->p_feeds[i_feed].psz_title + p_sys->i_cur_char,
                      p_sys->p_feeds[i_feed].p_items[i_item+1].psz_title );
        else
            snprintf( p_sys->psz_marquee, p_sys->i_length, "%s",
                      p_sys->p_feeds[i_feed].p_items[i_item].psz_title
                      +p_sys->i_cur_char );
    }
454

455 456 457 458
    while( strlen( p_sys->psz_marquee ) < (unsigned int)p_sys->i_length )
    {
        i_item++;
        if( i_item == p_sys->p_feeds[i_feed].i_items ) break;
459 460 461 462 463
        snprintf( strchr( p_sys->psz_marquee, 0 ),
                  p_sys->i_length - strlen( p_sys->psz_marquee ),
                  " - %s",
                  p_sys->p_feeds[i_feed].p_items[i_item].psz_title );
    }
464

465 466 467 468 469 470 471 472 473 474 475 476 477 478
    /* Calls to snprintf might split multibyte UTF8 chars ...
     * which freetype doesn't like. */
    {
        char *a = strdup( p_sys->psz_marquee );
        char *a2 = a;
        char *b = p_sys->psz_marquee;
        EnsureUTF8( p_sys->psz_marquee );
        /* we want to use ' ' instead of '?' for erroneous chars */
        while( *b != '\0' )
        {
            if( *b != *a ) *b = ' ';
            b++;a++;
        }
        free( a2 );
479 480
    }

481
    p_spu->p_region->p_text = text_segment_New(p_sys->psz_marquee);
482 483
    if( p_sys->p_style->i_font_size > 0 )
        p_spu->p_region->fmt.i_visible_height = p_sys->p_style->i_font_size;
484 485
    p_spu->i_start = date;
    p_spu->i_stop  = 0;
486
    p_spu->b_ephemer = true;
487 488

    /*  where to locate the string: */
489 490
    if( p_sys->i_pos < 0 )
    {   /*  set to an absolute xy */
491
        p_spu->p_region->i_align = SUBPICTURE_ALIGN_LEFT | SUBPICTURE_ALIGN_TOP;
492
        p_spu->b_absolute = true;
493 494
    }
    else
495
    {   /* set to one of the 9 relative locations */
496
        p_spu->p_region->i_align = p_sys->i_pos;
497
        p_spu->b_absolute = false;
498
    }
499 500
    p_spu->p_region->i_x = p_sys->i_xoff;
    p_spu->p_region->i_y = p_sys->i_yoff;
501

502
    p_spu->p_region->p_text->style = text_style_Duplicate( p_sys->p_style );
503

504 505 506 507
    if( p_feed->p_pic )
    {
        /* Display the feed's image */
        picture_t *p_pic = p_feed->p_pic;
508 509 510
        video_format_t fmt_out;

        memset( &fmt_out, 0, sizeof(video_format_t) );
511

512
        fmt_out.i_chroma = VLC_CODEC_YUVA;
513 514 515 516 517 518
        fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
        fmt_out.i_width =
            fmt_out.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
        fmt_out.i_height =
            fmt_out.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;

519
        p_region = subpicture_region_New( &fmt_out );
520 521 522 523 524 525
        if( !p_region )
        {
            msg_Err( p_filter, "cannot allocate SPU region" );
        }
        else
        {
526 527
            p_region->i_x = p_spu->p_region->i_x;
            p_region->i_y = p_spu->p_region->i_y;
528
            /* FIXME the copy is probably not needed anymore */
529
            picture_Copy( p_region->p_picture, p_pic );
530 531
            p_spu->p_region->p_next = p_region;

532 533 534
            /* Offset text to display right next to the image */
            p_spu->p_region->i_x += fmt_out.i_visible_width;
        }
535 536
    }

537
    vlc_mutex_unlock( &p_sys->lock );
538 539 540
    return p_spu;
}

541 542 543 544 545 546 547
/****************************************************************************
 * RSS related functions
 ****************************************************************************
 * You should always lock the p_filter mutex before using any of these
 * functions
 ***************************************************************************/

548 549
#undef LoadImage /* do not conflict with Win32 API */

550 551 552
/****************************************************************************
 * download and resize image located at psz_url
 ***************************************************************************/
553
static picture_t *LoadImage( filter_t *p_filter, const char *psz_url )
554 555
{
    filter_sys_t *p_sys = p_filter->p_sys;
556 557 558 559
    video_format_t fmt_in;
    video_format_t fmt_out;
    picture_t *p_orig;
    picture_t *p_pic = NULL;
560 561
    image_handler_t *p_handler = image_HandlerCreate( p_filter );

562 563 564
    memset( &fmt_in, 0, sizeof(video_format_t) );
    memset( &fmt_out, 0, sizeof(video_format_t) );

565
    fmt_out.i_chroma = VLC_CODEC_YUVA;
566
    p_orig = image_ReadUrl( p_handler, psz_url, &fmt_in, &fmt_out );
567 568 569 570 571 572 573 574

    if( !p_orig )
    {
        msg_Warn( p_filter, "Unable to read image %s", psz_url );
    }
    else if( p_sys->p_style->i_font_size > 0 )
    {

575
        fmt_in.i_chroma = VLC_CODEC_YUVA;
576 577 578 579 580 581 582
        fmt_in.i_height = p_orig->p[Y_PLANE].i_visible_lines;
        fmt_in.i_width = p_orig->p[Y_PLANE].i_visible_pitch;
        fmt_out.i_width = p_orig->p[Y_PLANE].i_visible_pitch
            *p_sys->p_style->i_font_size/p_orig->p[Y_PLANE].i_visible_lines;
        fmt_out.i_height = p_sys->p_style->i_font_size;

        p_pic = image_Convert( p_handler, p_orig, &fmt_in, &fmt_out );
583
        picture_Release( p_orig );
584 585 586 587 588 589 590 591 592 593 594 595 596 597
        if( !p_pic )
        {
            msg_Warn( p_filter, "Error while converting %s", psz_url );
        }
    }
    else
    {
        p_pic = p_orig;
    }

    image_HandlerDelete( p_handler );

    return p_pic;
}
598 599 600 601 602

/****************************************************************************
 * remove all ' ' '\t' '\n' '\r' characters from the begining and end of the
 * string.
 ***************************************************************************/
603
static char *removeWhiteChars( const char *psz_src )
604
{
605 606
    char *psz_src2,*psz_clean, *psz_clean2;
    psz_src2 = psz_clean = strdup( psz_src );
607
    int i;
608

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    while( ( *psz_clean == ' ' || *psz_clean == '\t'
           || *psz_clean == '\n' || *psz_clean == '\r' )
           && *psz_clean != '\0' )
    {
        psz_clean++;
    }
    i = strlen( psz_clean );
    while( --i > 0 &&
         ( psz_clean[i] == ' ' || psz_clean[i] == '\t'
        || psz_clean[i] == '\n' || psz_clean[i] == '\r' ) );
    psz_clean[i+1] = '\0';
    psz_clean2 = strdup( psz_clean );
    free( psz_src2 );
    return psz_clean2;
}

625 626

/****************************************************************************
627
 * Parse url list, psz_urls must be non empty (TODO: check it !)
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
 ***************************************************************************/
static int ParseUrls( filter_t *p_filter, char *psz_urls )
{
    filter_sys_t *p_sys = p_filter->p_sys;
    char *psz_urls2 = psz_urls;

    p_sys->i_feeds = 1;

    /* Count the number of feeds */
    while( *psz_urls )
    {
        if( *psz_urls == '|' )
            p_sys->i_feeds++;
        psz_urls++;
    }

    /* Allocate the structure */
645
    p_sys->p_feeds = vlc_alloc( p_sys->i_feeds, sizeof( rss_feed_t ) );
646 647 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 673 674 675 676 677 678 679
    if( !p_sys->p_feeds )
        return VLC_ENOMEM;

    /* Loop on all urls and fill in the struct */
    psz_urls = psz_urls2;
    for( int i = 0; i < p_sys->i_feeds; i++ )
    {
        rss_feed_t* p_feed = p_sys->p_feeds + i;
        char *psz_end;

        if( i < p_sys->i_feeds - 1 )
        {
            psz_end = strchr( psz_urls, '|' );
            *psz_end = '\0';
        }
        else
            psz_end = psz_urls;

        p_feed->i_items = 0;
        p_feed->p_items = NULL;
        p_feed->psz_title = NULL;
        p_feed->psz_link = NULL;
        p_feed->psz_description = NULL;
        p_feed->psz_image = NULL;
        p_feed->p_pic = NULL;
        p_feed->psz_url = strdup( psz_urls );

        psz_urls = psz_end + 1;
    }

    return VLC_SUCCESS;
}


680 681 682 683 684 685 686 687

/****************************************************************************
 * Parse the rss feed
 ***************************************************************************/
static bool ParseFeed( filter_t *p_filter, xml_reader_t *p_xml_reader,
                      rss_feed_t *p_feed )
{
    VLC_UNUSED(p_filter);
688
    const char *node;
689 690 691 692 693 694
    char *psz_eltname = NULL;

    bool b_is_item = false;
    bool b_is_image = false;

    int i_item = 0;
695
    int type;
696

697
    while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
698
    {
699
        switch( type )
700 701 702
        {
        case XML_READER_STARTELEM:
#ifdef RSS_DEBUG
703
            msg_Dbg( p_filter, "element <%s>", node );
704
#endif
705
            free(psz_eltname);
706 707 708 709
            psz_eltname = strdup( node );
            if( unlikely(!psz_eltname) )
                goto end;

710
            /* rss or atom */
711
            if( !strcmp( node, "item" ) || !strcmp( node, "entry" ) )
712 713 714
            {
                b_is_item = true;
                p_feed->i_items++;
715
                p_feed->p_items = xrealloc( p_feed->p_items,
716
                                     p_feed->i_items * sizeof( rss_item_t ) );
717 718 719 720 721
                p_feed->p_items[p_feed->i_items-1].psz_title = NULL;
                p_feed->p_items[p_feed->i_items-1].psz_description = NULL;
                p_feed->p_items[p_feed->i_items-1].psz_link = NULL;
            }
            /* rss */
722
            else if( !strcmp( node, "image" ) )
723 724 725 726
            {
                b_is_image = true;
            }
            /* atom */
727
            else if( !strcmp( node, "link" ) )
728
            {
729
                const char *name, *value;
730 731
                char *psz_href = NULL;
                char *psz_rel = NULL;
732

733
                while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
734
                {
735
                    if( !strcmp( name, "rel" ) )
736 737
                    {
                        free( psz_rel );
738
                        psz_rel = strdup( value );
739
                    }
740
                    else if( !strcmp( name, "href" ) )
741 742
                    {
                        free( psz_href );
743
                        psz_href = strdup( value );
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
                    }
                }

                /* "rel" and "href" must be defined */
                if( psz_rel && psz_href )
                {
                    if( !strcmp( psz_rel, "alternate" ) && !b_is_item &&
                        !b_is_image && !p_feed->psz_link )
                    {
                        p_feed->psz_link = psz_href;
                    }
                    /* this isn't in the rfc but i found some ... */
                    else if( ( !strcmp( psz_rel, "logo" ) ||
                               !strcmp( psz_rel, "icon" ) )
                             && !b_is_item && !b_is_image
                             && !p_feed->psz_image )
                    {
                        p_feed->psz_image = psz_href;
                    }
                    else
                    {
                        free( psz_href );
                    }
                }
                else
                {
                    free( psz_href );
                }
                free( psz_rel );
            }
            break;

        case XML_READER_ENDELEM:
777
            FREENULL( psz_eltname );
778
#ifdef RSS_DEBUG
779
            msg_Dbg( p_filter, "element end </%s>", node );
780 781
#endif
            /* rss or atom */
782
            if( !strcmp( node, "item" ) || !strcmp( node, "entry" ) )
783 784 785 786 787
            {
                b_is_item = false;
                i_item++;
            }
            /* rss */
788
            else if( !strcmp( node, "image" ) )
789 790 791 792 793 794 795 796 797 798
            {
                b_is_image = false;
            }
            break;

        case XML_READER_TEXT:
        {
            if( !psz_eltname )
                break;

799
            char *psz_eltvalue = removeWhiteChars( node );
800 801

#ifdef RSS_DEBUG
802
            msg_Dbg( p_filter, "  text : \"%s\"", psz_eltvalue );
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 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 881
#endif
            /* Is it an item ? */
            if( b_is_item )
            {
                rss_item_t *p_item = p_feed->p_items+i_item;
                /* rss/atom */
                if( !strcmp( psz_eltname, "title" ) && !p_item->psz_title )
                {
                    p_item->psz_title = psz_eltvalue;
                }
                else if( !strcmp( psz_eltname, "link" ) /* rss */
                         && !p_item->psz_link )
                {
                    p_item->psz_link = psz_eltvalue;
                }
                /* rss/atom */
                else if( ( !strcmp( psz_eltname, "description" ) ||
                           !strcmp( psz_eltname, "summary" ) )
                          && !p_item->psz_description )
                {
                    p_item->psz_description = psz_eltvalue;
                }
                else
                {
                    free( psz_eltvalue );
                }
            }
            /* Is it an image ? */
            else if( b_is_image )
            {
                if( !strcmp( psz_eltname, "url" ) && !p_feed->psz_image )
                    p_feed->psz_image = psz_eltvalue;
                else
                    free( psz_eltvalue );
            }
            else
            {
                /* rss/atom */
                if( !strcmp( psz_eltname, "title" ) && !p_feed->psz_title )
                {
                    p_feed->psz_title = psz_eltvalue;
                }
                /* rss */
                else if( !strcmp( psz_eltname, "link" ) && !p_feed->psz_link )
                {
                    p_feed->psz_link = psz_eltvalue;
                }
                /* rss ad atom */
                else if( ( !strcmp( psz_eltname, "description" ) ||
                           !strcmp( psz_eltname, "subtitle" ) )
                         && !p_feed->psz_description )
                {
                    p_feed->psz_description = psz_eltvalue;
                }
                /* rss */
                else if( ( !strcmp( psz_eltname, "logo" ) ||
                           !strcmp( psz_eltname, "icon" ) )
                         && !p_feed->psz_image )
                {
                    p_feed->psz_image = psz_eltvalue;
                }
                else
                {
                    free( psz_eltvalue );
                }
            }
            break;
        }
        }
    }

    free( psz_eltname );
    return true;

end:
    return false;
}


882
/****************************************************************************
883
 * FetchRSS (or Atom) feeds
884
 ***************************************************************************/
885
static rss_feed_t* FetchRSS( filter_t *p_filter )
886 887 888
{
    filter_sys_t *p_sys = p_filter->p_sys;

889 890
    stream_t *p_stream;
    xml_reader_t *p_xml_reader;
891
    int i_feed;
892

893 894 895 896 897 898
    /* These data are not modified after the creation of the module so we don't
       need to hold the lock */
    int i_feeds = p_sys->i_feeds;
    bool b_images = p_sys->b_images;

    /* Allocate a new structure */
899
    rss_feed_t *p_feeds = vlc_alloc( i_feeds, sizeof( rss_feed_t ) );
900 901 902
    if( !p_feeds )
        return NULL;

903
    /* Fetch all feeds and parse them */
904
    for( i_feed = 0; i_feed < i_feeds; i_feed++ )
905
    {
906 907 908 909 910 911 912 913 914
        rss_feed_t *p_feed = p_feeds + i_feed;
        rss_feed_t *p_old_feed = p_sys->p_feeds + i_feed;

        /* Initialize the structure */
        p_feed->psz_title = NULL;
        p_feed->psz_description = NULL;
        p_feed->psz_link = NULL;
        p_feed->psz_image = NULL;
        p_feed->p_pic = NULL;
915
        p_feed->i_items = 0;
916 917 918
        p_feed->p_items = NULL;

        p_feed->psz_url = strdup( p_old_feed->psz_url );
919

920
        /* Fetch the feed */
921
        msg_Dbg( p_filter, "opening %s RSS/Atom feed ...", p_feed->psz_url );
922

923
        p_stream = vlc_stream_NewURL( p_filter, p_feed->psz_url );
924 925
        if( !p_stream )
        {
926
            msg_Err( p_filter, "Failed to open %s for reading", p_feed->psz_url );
927 928
            p_xml_reader = NULL;
            goto error;
929 930
        }

931
        p_xml_reader = xml_ReaderCreate( p_filter, p_stream );
932 933
        if( !p_xml_reader )
        {
934
            msg_Err( p_filter, "Failed to open %s for parsing", p_feed->psz_url );
935
            goto error;
936 937
        }

938 939 940
        /* Parse the feed */
        if( !ParseFeed( p_filter, p_xml_reader, p_feed ) )
            goto error;
941

942
        /* If we have a image: load it if requiere */
943
        if( b_images && p_feed->psz_image && !p_feed->p_pic )
944 945 946 947
        {
            p_feed->p_pic = LoadImage( p_filter, p_feed->psz_image );
        }

948
        msg_Dbg( p_filter, "done with %s RSS/Atom feed", p_feed->psz_url );
949
        xml_ReaderDelete( p_xml_reader );
950
        vlc_stream_Delete( p_stream );
951 952
    }

953
    return p_feeds;
954 955

error:
956
    FreeRSS( p_feeds, i_feed + 1 );
957
    if( p_xml_reader )
958
        xml_ReaderDelete( p_xml_reader );
959
    if( p_stream )
960
        vlc_stream_Delete( p_stream );
961

962
    return NULL;
963
}
964 965 966 967

/****************************************************************************
 * FreeRSS
 ***************************************************************************/
968
static void FreeRSS( rss_feed_t *p_feeds, int i_feeds )
969
{
970
    for( int i_feed = 0; i_feed < i_feeds; i_feed++ )
971
    {
972
        rss_feed_t *p_feed = p_feeds+i_feed;
973
        for( int i_item = 0; i_item < p_feed->i_items; i_item++ )
974
        {
975
            rss_item_t *p_item = p_feed->p_items+i_item;
976 977 978 979 980 981 982 983
            free( p_item->psz_title );
            free( p_item->psz_link );
            free( p_item->psz_description );
        }
        free( p_feed->p_items );
        free( p_feed->psz_title);
        free( p_feed->psz_link );
        free( p_feed->psz_description );
984 985
        free( p_feed->psz_image );
        if( p_feed->p_pic != NULL )
986
            picture_Release( p_feed->p_pic );
987
        free( p_feed->psz_url );
988
    }
989
    free( p_feeds );
990
}
991 992 993 994 995 996

static void Fetch( void *p_data )
{
    filter_t *p_filter = p_data;
    filter_sys_t *p_sys = p_filter->p_sys;

997
    msg_Dbg( p_filter, "Updating the rss feeds" );
998 999
    rss_feed_t *p_feeds = FetchRSS( p_filter );
    if( !p_feeds )
1000 1001
    {
        msg_Err( p_filter, "Unable to fetch the feeds" );
1002
        return;
1003 1004 1005
    }

    rss_feed_t *p_old_feeds = p_sys->p_feeds;
1006

1007
    vlc_mutex_lock( &p_sys->lock );
1008
    /* Update the feeds */
1009
    p_sys->p_feeds = p_feeds;
1010
    p_sys->b_fetched = true;
1011 1012 1013 1014
    /* Set all current info to the original values */
    p_sys->i_cur_feed = 0;
    p_sys->i_cur_item = p_sys->i_title == scroll_title ? -1 : 0;
    p_sys->i_cur_char = 0;
1015
    vlc_mutex_unlock( &p_sys->lock );
1016 1017 1018

    if( p_old_feeds )
        FreeRSS( p_old_feeds, p_sys->i_feeds );
1019
}