rss.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:36k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * rss.c : rss/atom feed display video plugin for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2003-2006 the VideoLAN team
  5.  * $Id: bb5a330c682a4df693c01c8cf76405e8e3b7e609 $
  6.  *
  7.  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Atom : http://www.ietf.org/rfc/rfc4287.txt
  25.  * RSS : http://www.rssboard.org/rss-specification
  26.  *****************************************************************************/
  27. /*****************************************************************************
  28.  * Preamble
  29.  *****************************************************************************/
  30. #ifdef HAVE_CONFIG_H
  31. # include "config.h"
  32. #endif
  33. #include <vlc_common.h>
  34. #include <vlc_plugin.h>
  35. #include <vlc_vout.h>
  36. #include "vlc_filter.h"
  37. #include "vlc_block.h"
  38. #include "vlc_osd.h"
  39. #include "vlc_block.h"
  40. #include "vlc_stream.h"
  41. #include "vlc_xml.h"
  42. #include <vlc_charset.h>
  43. #include <vlc_image.h>
  44. #include <time.h>
  45. /*****************************************************************************
  46.  * Local prototypes
  47.  *****************************************************************************/
  48. static int  CreateFilter ( vlc_object_t * );
  49. static void DestroyFilter( vlc_object_t * );
  50. static subpicture_t *Filter( filter_t *, mtime_t );
  51. static int FetchRSS( filter_t * );
  52. static void FreeRSS( filter_t * );
  53. static const int pi_color_values[] = {
  54.                0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0,
  55.                0x00FFFFFF, 0x00800000, 0x00FF0000, 0x00FF00FF, 0x00FFFF00,
  56.                0x00808000, 0x00008000, 0x00008080, 0x0000FF00, 0x00800080,
  57.                0x00000080, 0x000000FF, 0x0000FFFF};
  58. static const char *const ppsz_color_descriptions[] = {
  59.                N_("Default"), N_("Black"),
  60.                N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"), N_("Red"),
  61.                N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"),
  62.                N_("Teal"), N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"),
  63.                N_("Aqua") };
  64. /*****************************************************************************
  65.  * filter_sys_t: rss filter descriptor
  66.  *****************************************************************************/
  67. struct rss_item_t
  68. {
  69.     char *psz_title;
  70.     char *psz_description;
  71.     char *psz_link;
  72. };
  73. struct rss_feed_t
  74. {
  75.     char *psz_title;
  76.     char *psz_description;
  77.     char *psz_link;
  78.     char *psz_image;
  79.     picture_t *p_pic;
  80.     int i_items;
  81.     struct rss_item_t *p_items;
  82. };
  83. struct filter_sys_t
  84. {
  85.     vlc_mutex_t lock;
  86.     vlc_mutex_t *p_lock;
  87.     int i_xoff, i_yoff;  /* offsets for the display string in the video window */
  88.     int i_pos; /* permit relative positioning (top, bottom, left, right, center) */
  89.     int i_speed;
  90.     int i_length;
  91.     char *psz_marquee;    /* marquee string */
  92.     text_style_t *p_style; /* font control */
  93.     mtime_t last_date;
  94.     char *psz_urls;
  95.     int i_feeds;
  96.     struct rss_feed_t *p_feeds;
  97.     int i_ttl;
  98.     time_t t_last_update;
  99.     bool b_images;
  100.     int i_title;
  101.     int i_cur_feed;
  102.     int i_cur_item;
  103.     int i_cur_char;
  104. };
  105. #define MSG_TEXT N_("Feed URLs")
  106. #define MSG_LONGTEXT N_("RSS/Atom feed '|' (pipe) seperated URLs.")
  107. #define SPEED_TEXT N_("Speed of feeds")
  108. #define SPEED_LONGTEXT N_("Speed of the RSS/Atom feeds in microseconds (bigger is slower).")
  109. #define LENGTH_TEXT N_("Max length")
  110. #define LENGTH_LONGTEXT N_("Maximum number of characters displayed on the " 
  111.                 "screen." )
  112. #define TTL_TEXT N_("Refresh time")
  113. #define TTL_LONGTEXT N_("Number of seconds between each forced refresh " 
  114.         "of the feeds. 0 means that the feeds are never updated." )
  115. #define IMAGE_TEXT N_("Feed images")
  116. #define IMAGE_LONGTEXT N_("Display feed images if available.")
  117. #define POSX_TEXT N_("X offset")
  118. #define POSX_LONGTEXT N_("X offset, from the left screen edge." )
  119. #define POSY_TEXT N_("Y offset")
  120. #define POSY_LONGTEXT N_("Y offset, down from the top." )
  121. #define OPACITY_TEXT N_("Opacity")
  122. #define OPACITY_LONGTEXT N_("Opacity (inverse of transparency) of " 
  123.     "overlay text. 0 = transparent, 255 = totally opaque." )
  124. #define SIZE_TEXT N_("Font size, pixels")
  125. #define SIZE_LONGTEXT N_("Font size, in pixels. Default is -1 (use default " 
  126.     "font size)." )
  127. #define COLOR_TEXT N_("Color")
  128. #define COLOR_LONGTEXT N_("Color of the text that will be rendered on "
  129.     "the video. This must be an hexadecimal (like HTML colors). The first two "
  130.     "chars are for red, then green, then blue. #000000 = black, #FF0000 = red,"
  131.     " #00FF00 = green, #FFFF00 = yellow (red + green), #FFFFFF = white" )
  132. #define POS_TEXT N_("Text position")
  133. #define POS_LONGTEXT N_( 
  134.   "You can enforce the text position on the video " 
  135.   "(0=center, 1=left, 2=right, 4=top, 8=bottom; you can " 
  136.   "also use combinations of these values, eg 6 = top-right).")
  137. #define TITLE_TEXT N_("Title display mode")
  138. #define TITLE_LONGTEXT N_("Title display mode. Default is 0 (hidden) if the feed has an image and feed images are enabled, 1 otherwise.")
  139. static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
  140. static const char *const ppsz_pos_descriptions[] =
  141.      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
  142.      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
  143. enum title_modes {
  144.     default_title=-1,
  145.     hide_title,
  146.     prepend_title,
  147.     scroll_title };
  148. static const int pi_title_modes[] = { default_title, hide_title, prepend_title, scroll_title };
  149. static const char *const ppsz_title_modes[] =
  150.     { N_("Default"), N_("Don't show"), N_("Always visible"), N_("Scroll with feed") };
  151. #define CFG_PREFIX "rss-"
  152. /*****************************************************************************
  153.  * Module descriptor
  154.  *****************************************************************************/
  155. vlc_module_begin ()
  156.     set_capability( "sub filter", 1 )
  157.     set_shortname( "RSS / Atom" )
  158.     set_callbacks( CreateFilter, DestroyFilter )
  159.     set_category( CAT_VIDEO )
  160.     set_subcategory( SUBCAT_VIDEO_SUBPIC )
  161.     add_string( CFG_PREFIX "urls", "rss", NULL, MSG_TEXT, MSG_LONGTEXT, false )
  162.     set_section( N_("Position"), NULL )
  163.     add_integer( CFG_PREFIX "x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, true )
  164.     add_integer( CFG_PREFIX "y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, true )
  165.     add_integer( CFG_PREFIX "position", -1, NULL, POS_TEXT, POS_LONGTEXT, false )
  166.         change_integer_list( pi_pos_values, ppsz_pos_descriptions, NULL )
  167.     set_section( N_("Font"), NULL )
  168.     /* 5 sets the default to top [1] left [4] */
  169.     add_integer_with_range( CFG_PREFIX "opacity", 255, 0, 255, NULL,
  170.         OPACITY_TEXT, OPACITY_LONGTEXT, false )
  171.     add_integer( CFG_PREFIX "color", 0xFFFFFF, NULL, COLOR_TEXT, COLOR_LONGTEXT,
  172.                   false )
  173.         change_integer_list( pi_color_values, ppsz_color_descriptions, NULL )
  174.     add_integer( CFG_PREFIX "size", -1, NULL, SIZE_TEXT, SIZE_LONGTEXT, false )
  175.     set_section( N_("Misc"), NULL )
  176.     add_integer( CFG_PREFIX "speed", 100000, NULL, SPEED_TEXT, SPEED_LONGTEXT,
  177.                  false )
  178.     add_integer( CFG_PREFIX "length", 60, NULL, LENGTH_TEXT, LENGTH_LONGTEXT,
  179.                  false )
  180.     add_integer( CFG_PREFIX "ttl", 1800, NULL, TTL_TEXT, TTL_LONGTEXT, false )
  181.     add_bool( CFG_PREFIX "images", 1, NULL, IMAGE_TEXT, IMAGE_LONGTEXT, false )
  182.     add_integer( CFG_PREFIX "title", default_title, NULL, TITLE_TEXT, TITLE_LONGTEXT, false )
  183.         change_integer_list( pi_title_modes, ppsz_title_modes, NULL )
  184.     set_description( N_("RSS and Atom feed display") )
  185.     add_shortcut( "rss" )
  186.     add_shortcut( "atom" )
  187. vlc_module_end ()
  188. static const char *const ppsz_filter_options[] = {
  189.     "urls", "x", "y", "position", "color", "size", "speed", "length",
  190.     "ttl", "images", "title", NULL
  191. };
  192. /*****************************************************************************
  193.  * CreateFilter: allocates RSS video filter
  194.  *****************************************************************************/
  195. static int CreateFilter( vlc_object_t *p_this )
  196. {
  197.     filter_t *p_filter = (filter_t *)p_this;
  198.     filter_sys_t *p_sys;
  199.     int i_feed;
  200.     /* Allocate structure */
  201.     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
  202.     if( p_sys == NULL )
  203.         return VLC_ENOMEM;
  204.     vlc_mutex_init( &p_sys->lock );
  205.     vlc_mutex_lock( &p_sys->lock );
  206.     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
  207.                        p_filter->p_cfg );
  208.     p_sys->psz_urls = var_CreateGetString( p_filter, CFG_PREFIX "urls" );
  209.     p_sys->i_title = var_CreateGetInteger( p_filter, CFG_PREFIX "title" );
  210.     p_sys->i_cur_feed = 0;
  211.     p_sys->i_cur_item = p_sys->i_title == scroll_title ? -1 : 0;
  212.     p_sys->i_cur_char = 0;
  213.     p_sys->i_feeds = 0;
  214.     p_sys->p_feeds = NULL;
  215.     p_sys->i_speed = var_CreateGetInteger( p_filter, CFG_PREFIX "speed" );
  216.     p_sys->i_length = var_CreateGetInteger( p_filter, CFG_PREFIX "length" );
  217.     p_sys->i_ttl = __MAX( 0, var_CreateGetInteger( p_filter, CFG_PREFIX "ttl" ) );
  218.     p_sys->b_images = var_CreateGetBool( p_filter, CFG_PREFIX "images" );
  219.     p_sys->psz_marquee = (char *)malloc( p_sys->i_length + 1 );
  220.     if( p_sys->psz_marquee == NULL )
  221.     {
  222.         vlc_mutex_unlock( &p_sys->lock );
  223.         vlc_mutex_destroy( &p_sys->lock );
  224.         free( p_sys->psz_urls );
  225.         free( p_sys );
  226.         return VLC_ENOMEM;
  227.     }
  228.     p_sys->psz_marquee[p_sys->i_length] = '';
  229.     p_sys->p_style = malloc( sizeof( text_style_t ));
  230.     if( p_sys->p_style == NULL )
  231.     {
  232.         free( p_sys->psz_marquee );
  233.         vlc_mutex_unlock( &p_sys->lock );
  234.         vlc_mutex_destroy( &p_sys->lock );
  235.         free( p_sys->psz_urls );
  236.         free( p_sys );
  237.         return VLC_ENOMEM;
  238.     }
  239.     memcpy( p_sys->p_style, &default_text_style, sizeof( text_style_t ));
  240.     p_sys->i_xoff = var_CreateGetInteger( p_filter, CFG_PREFIX "x" );
  241.     p_sys->i_yoff = var_CreateGetInteger( p_filter, CFG_PREFIX "y" );
  242.     p_sys->i_pos = var_CreateGetInteger( p_filter, CFG_PREFIX "position" );
  243.     p_sys->p_style->i_font_alpha = 255 - var_CreateGetInteger( p_filter, CFG_PREFIX "opacity" );
  244.     p_sys->p_style->i_font_color = var_CreateGetInteger( p_filter, CFG_PREFIX "color" );
  245.     p_sys->p_style->i_font_size = var_CreateGetInteger( p_filter, CFG_PREFIX "size" );
  246.     if( p_sys->b_images == true && p_sys->p_style->i_font_size == -1 )
  247.     {
  248.         msg_Warn( p_filter, "rss-size wasn't specified. Feed images will thus be displayed without being resized" );
  249.     }
  250.     if( FetchRSS( p_filter ) )
  251.     {
  252.         msg_Err( p_filter, "failed while fetching RSS ... too bad" );
  253.         free( p_sys->p_style );
  254.         free( p_sys->psz_marquee );
  255.         vlc_mutex_unlock( &p_sys->lock );
  256.         vlc_mutex_destroy( &p_sys->lock );
  257.         free( p_sys->psz_urls );
  258.         free( p_sys );
  259.         return VLC_EGENERIC;
  260.     }
  261.     p_sys->t_last_update = time( NULL );
  262.     if( p_sys->i_feeds == 0 )
  263.     {
  264.         free( p_sys->p_style );
  265.         free( p_sys->psz_marquee );
  266.         vlc_mutex_unlock( &p_sys->lock );
  267.         vlc_mutex_destroy( &p_sys->lock );
  268.         free( p_sys->psz_urls );
  269.         free( p_sys );
  270.         return VLC_EGENERIC;
  271.     }
  272.     for( i_feed=0; i_feed < p_sys->i_feeds; i_feed ++ )
  273.     {
  274.         if( p_sys->p_feeds[i_feed].i_items == 0 )
  275.         {
  276.             free( p_sys->p_style );
  277.             free( p_sys->psz_marquee );
  278.             FreeRSS( p_filter );
  279.             vlc_mutex_unlock( &p_sys->lock );
  280.             vlc_mutex_destroy( &p_sys->lock );
  281.             free( p_sys->psz_urls );
  282.             free( p_sys );
  283.             return VLC_EGENERIC;
  284.         }
  285.     }
  286.     /* Misc init */
  287.     p_filter->pf_sub_filter = Filter;
  288.     p_sys->last_date = (mtime_t)0;
  289.     vlc_mutex_unlock( &p_sys->lock );
  290.     return VLC_SUCCESS;
  291. }
  292. /*****************************************************************************
  293.  * DestroyFilter: destroy RSS video filter
  294.  *****************************************************************************/
  295. static void DestroyFilter( vlc_object_t *p_this )
  296. {
  297.     filter_t *p_filter = (filter_t *)p_this;
  298.     filter_sys_t *p_sys = p_filter->p_sys;
  299.     vlc_mutex_lock( &p_sys->lock );
  300.     free( p_sys->p_style );
  301.     free( p_sys->psz_marquee );
  302.     free( p_sys->psz_urls );
  303.     FreeRSS( p_filter );
  304.     vlc_mutex_unlock( &p_sys->lock );
  305.     vlc_mutex_destroy( &p_sys->lock );
  306.     free( p_sys );
  307.     /* Delete the RSS variables */
  308.     var_Destroy( p_filter, CFG_PREFIX "urls" );
  309.     var_Destroy( p_filter, CFG_PREFIX "speed" );
  310.     var_Destroy( p_filter, CFG_PREFIX "length" );
  311.     var_Destroy( p_filter, CFG_PREFIX "ttl" );
  312.     var_Destroy( p_filter, CFG_PREFIX "images" );
  313.     var_Destroy( p_filter, CFG_PREFIX "x" );
  314.     var_Destroy( p_filter, CFG_PREFIX "y" );
  315.     var_Destroy( p_filter, CFG_PREFIX "position" );
  316.     var_Destroy( p_filter, CFG_PREFIX "color");
  317.     var_Destroy( p_filter, CFG_PREFIX "opacity");
  318.     var_Destroy( p_filter, CFG_PREFIX "size");
  319.     var_Destroy( p_filter, CFG_PREFIX "title" );
  320. }
  321. /****************************************************************************
  322.  * Filter: the whole thing
  323.  ****************************************************************************
  324.  * This function outputs subpictures at regular time intervals.
  325.  ****************************************************************************/
  326. static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
  327. {
  328.     filter_sys_t *p_sys = p_filter->p_sys;
  329.     subpicture_t *p_spu;
  330.     video_format_t fmt;
  331.     subpicture_region_t *p_region;
  332.     int i_feed, i_item;
  333.     struct rss_feed_t *p_feed;
  334.     memset( &fmt, 0, sizeof(video_format_t) );
  335.     vlc_mutex_lock( &p_sys->lock );
  336.     if( p_sys->last_date
  337.        + ( p_sys->i_cur_char == 0 && p_sys->i_cur_item == ( p_sys->i_title == scroll_title ? -1 : 0 ) ? 5 : 1 )
  338.            /* ( ... ? 5 : 1 ) means "wait 5 times more for the 1st char" */
  339.        * p_sys->i_speed > date )
  340.     {
  341.         vlc_mutex_unlock( &p_sys->lock );
  342.         return NULL;
  343.     }
  344.     /* Do we need to update the feeds ? */
  345.     if( p_sys->i_ttl
  346.         && time( NULL ) > p_sys->t_last_update + (time_t)p_sys->i_ttl )
  347.     {
  348.         msg_Dbg( p_filter, "Forcing update of all the RSS feeds" );
  349.         if( FetchRSS( p_filter ) )
  350.         {
  351.             msg_Err( p_filter, "Failed while fetching RSS ... too bad" );
  352.             vlc_mutex_unlock( &p_sys->lock );
  353.             return NULL; /* FIXME : we most likely messed up all the data,
  354.                           * so we might need to do something about it */
  355.         }
  356.         p_sys->t_last_update = time( NULL );
  357.     }
  358.     p_sys->last_date = date;
  359.     p_sys->i_cur_char++;
  360.     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 )
  361.     {
  362.         p_sys->i_cur_char = 0;
  363.         p_sys->i_cur_item++;
  364.         if( p_sys->i_cur_item >= p_sys->p_feeds[p_sys->i_cur_feed].i_items )
  365.         {
  366.             if( p_sys->i_title == scroll_title )
  367.                 p_sys->i_cur_item = -1;
  368.             else
  369.                 p_sys->i_cur_item = 0;
  370.             p_sys->i_cur_feed = (p_sys->i_cur_feed + 1)%p_sys->i_feeds;
  371.         }
  372.     }
  373.     p_spu = filter_NewSubpicture( p_filter );
  374.     if( !p_spu )
  375.     {
  376.         vlc_mutex_unlock( &p_sys->lock );
  377.         return NULL;
  378.     }
  379.     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
  380.     p_spu->p_region = subpicture_region_New( &fmt );
  381.     if( !p_spu->p_region )
  382.     {
  383.         p_filter->pf_sub_buffer_del( p_filter, p_spu );
  384.         vlc_mutex_unlock( &p_sys->lock );
  385.         return NULL;
  386.     }
  387.     /* Generate the string that will be displayed. This string is supposed to
  388.        be p_sys->i_length characters long. */
  389.     i_item = p_sys->i_cur_item;
  390.     i_feed = p_sys->i_cur_feed;
  391.     p_feed = &p_sys->p_feeds[i_feed];
  392.     if( ( p_feed->p_pic && p_sys->i_title == default_title )
  393.         || p_sys->i_title == hide_title )
  394.     {
  395.         /* Don't display the feed's title if we have an image */
  396.         snprintf( p_sys->psz_marquee, p_sys->i_length, "%s",
  397.                   p_sys->p_feeds[i_feed].p_items[i_item].psz_title
  398.                   +p_sys->i_cur_char );
  399.     }
  400.     else if( ( !p_feed->p_pic && p_sys->i_title == default_title )
  401.              || p_sys->i_title == prepend_title )
  402.     {
  403.         snprintf( p_sys->psz_marquee, p_sys->i_length, "%s : %s",
  404.                   p_sys->p_feeds[i_feed].psz_title,
  405.                   p_sys->p_feeds[i_feed].p_items[i_item].psz_title
  406.                   +p_sys->i_cur_char );
  407.     }
  408.     else /* scrolling title */
  409.     {
  410.         if( i_item == -1 )
  411.             snprintf( p_sys->psz_marquee, p_sys->i_length, "%s : %s",
  412.                       p_sys->p_feeds[i_feed].psz_title + p_sys->i_cur_char,
  413.                       p_sys->p_feeds[i_feed].p_items[i_item+1].psz_title );
  414.         else
  415.             snprintf( p_sys->psz_marquee, p_sys->i_length, "%s",
  416.                       p_sys->p_feeds[i_feed].p_items[i_item].psz_title
  417.                       +p_sys->i_cur_char );
  418.     }
  419.     while( strlen( p_sys->psz_marquee ) < (unsigned int)p_sys->i_length )
  420.     {
  421.         i_item++;
  422.         if( i_item == p_sys->p_feeds[i_feed].i_items ) break;
  423.         snprintf( strchr( p_sys->psz_marquee, 0 ),
  424.                   p_sys->i_length - strlen( p_sys->psz_marquee ),
  425.                   " - %s",
  426.                   p_sys->p_feeds[i_feed].p_items[i_item].psz_title );
  427.     }
  428.     /* Calls to snprintf might split multibyte UTF8 chars ...
  429.      * which freetype doesn't like. */
  430.     {
  431.         char *a = strdup( p_sys->psz_marquee );
  432.         char *a2 = a;
  433.         char *b = p_sys->psz_marquee;
  434.         EnsureUTF8( p_sys->psz_marquee );
  435.         /* we want to use ' ' instead of '?' for erroneous chars */
  436.         while( *b != '' )
  437.         {
  438.             if( *b != *a ) *b = ' ';
  439.             b++;a++;
  440.         }
  441.         free( a2 );
  442.     }
  443.     p_spu->p_region->psz_text = strdup(p_sys->psz_marquee);
  444.     if( p_sys->p_style->i_font_size > 0 )
  445.         p_spu->p_region->fmt.i_visible_height = p_sys->p_style->i_font_size;
  446.     p_spu->i_start = date;
  447.     p_spu->i_stop  = 0;
  448.     p_spu->b_ephemer = true;
  449.     /*  where to locate the string: */
  450.     if( p_sys->i_pos < 0 )
  451.     {   /*  set to an absolute xy */
  452.         p_spu->p_region->i_align = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
  453.         p_spu->b_absolute = true;
  454.     }
  455.     else
  456.     {   /* set to one of the 9 relative locations */
  457.         p_spu->p_region->i_align = p_sys->i_pos;
  458.         p_spu->b_absolute = false;
  459.     }
  460.     p_spu->p_region->p_style = p_sys->p_style;
  461.     if( p_feed->p_pic )
  462.     {
  463.         /* Display the feed's image */
  464.         picture_t *p_pic = p_feed->p_pic;
  465.         video_format_t fmt_out;
  466.         memset( &fmt_out, 0, sizeof(video_format_t) );
  467.         fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
  468.         fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
  469.         fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
  470.         fmt_out.i_width =
  471.             fmt_out.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
  472.         fmt_out.i_height =
  473.             fmt_out.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
  474.         p_region = subpicture_region_New( &fmt_out );
  475.         if( !p_region )
  476.         {
  477.             msg_Err( p_filter, "cannot allocate SPU region" );
  478.         }
  479.         else
  480.         {
  481.             p_region->i_x = p_sys->i_xoff;
  482.             p_region->i_y = p_sys->i_yoff;
  483.             /* FIXME the copy is probably not needed anymore */
  484.             picture_Copy( p_region->p_picture, p_pic );
  485.             p_spu->p_region->p_next = p_region;
  486.         }
  487.         /* Offset text to display right next to the image */
  488.         p_spu->p_region->i_x = p_pic->p[Y_PLANE].i_visible_pitch;
  489.     }
  490.     vlc_mutex_unlock( &p_sys->lock );
  491.     return p_spu;
  492. }
  493. /****************************************************************************
  494.  * RSS related functions
  495.  ****************************************************************************
  496.  * You should always lock the p_filter mutex before using any of these
  497.  * functions
  498.  ***************************************************************************/
  499. #undef LoadImage /* do not conflict with Win32 API */
  500. /****************************************************************************
  501.  * download and resize image located at psz_url
  502.  ***************************************************************************/
  503. static picture_t *LoadImage( filter_t *p_filter, const char *psz_url )
  504. {
  505.     filter_sys_t *p_sys = p_filter->p_sys;
  506.     video_format_t fmt_in;
  507.     video_format_t fmt_out;
  508.     picture_t *p_orig;
  509.     picture_t *p_pic = NULL;
  510.     image_handler_t *p_handler = image_HandlerCreate( p_filter );
  511.     memset( &fmt_in, 0, sizeof(video_format_t) );
  512.     memset( &fmt_out, 0, sizeof(video_format_t) );
  513.     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
  514.     p_orig = image_ReadUrl( p_handler, psz_url, &fmt_in, &fmt_out );
  515.     if( !p_orig )
  516.     {
  517.         msg_Warn( p_filter, "Unable to read image %s", psz_url );
  518.     }
  519.     else if( p_sys->p_style->i_font_size > 0 )
  520.     {
  521.         fmt_in.i_chroma = VLC_FOURCC('Y','U','V','A');
  522.         fmt_in.i_height = p_orig->p[Y_PLANE].i_visible_lines;
  523.         fmt_in.i_width = p_orig->p[Y_PLANE].i_visible_pitch;
  524.         fmt_out.i_width = p_orig->p[Y_PLANE].i_visible_pitch
  525.             *p_sys->p_style->i_font_size/p_orig->p[Y_PLANE].i_visible_lines;
  526.         fmt_out.i_height = p_sys->p_style->i_font_size;
  527.         p_pic = image_Convert( p_handler, p_orig, &fmt_in, &fmt_out );
  528.         picture_Release( p_orig );
  529.         if( !p_pic )
  530.         {
  531.             msg_Warn( p_filter, "Error while converting %s", psz_url );
  532.         }
  533.     }
  534.     else
  535.     {
  536.         p_pic = p_orig;
  537.     }
  538.     image_HandlerDelete( p_handler );
  539.     return p_pic;
  540. }
  541. /****************************************************************************
  542.  * remove all ' ' 't' 'n' 'r' characters from the begining and end of the
  543.  * string.
  544.  ***************************************************************************/
  545. static char *removeWhiteChars( const char *psz_src )
  546. {
  547.     char *psz_src2,*psz_clean, *psz_clean2;
  548.     psz_src2 = psz_clean = strdup( psz_src );
  549.     int i;
  550.     while( ( *psz_clean == ' ' || *psz_clean == 't'
  551.            || *psz_clean == 'n' || *psz_clean == 'r' )
  552.            && *psz_clean != '' )
  553.     {
  554.         psz_clean++;
  555.     }
  556.     i = strlen( psz_clean );
  557.     while( --i > 0 &&
  558.          ( psz_clean[i] == ' ' || psz_clean[i] == 't'
  559.         || psz_clean[i] == 'n' || psz_clean[i] == 'r' ) );
  560.     psz_clean[i+1] = '';
  561.     psz_clean2 = strdup( psz_clean );
  562.     free( psz_src2 );
  563.     return psz_clean2;
  564. }
  565. /****************************************************************************
  566.  * FetchRSS (or Atom) feeds
  567.  ***************************************************************************/
  568. static int FetchRSS( filter_t *p_filter)
  569. {
  570.     filter_sys_t *p_sys = p_filter->p_sys;
  571.     stream_t *p_stream = NULL;
  572.     xml_t *p_xml = NULL;
  573.     xml_reader_t *p_xml_reader = NULL;
  574.     char *psz_eltname = NULL;
  575.     char *psz_eltvalue = NULL;
  576.     char *psz_feed = NULL;
  577.     char *psz_buffer = NULL;
  578.     char *psz_buffer_2 = NULL;
  579.     int i_feed;
  580.     int i_item;
  581.     bool b_is_item;
  582.     bool b_is_image;
  583.     int i_int;
  584.     FreeRSS( p_filter );
  585.     p_sys->i_feeds = 1;
  586.     i_int = 0;
  587.     while( p_sys->psz_urls[i_int] != 0 )
  588.         if( p_sys->psz_urls[i_int++] == '|' )
  589.             p_sys->i_feeds++;
  590.     p_sys->p_feeds = (struct rss_feed_t *)malloc( p_sys->i_feeds
  591.                                 * sizeof( struct rss_feed_t ) );
  592.     p_xml = xml_Create( p_filter );
  593.     if( !p_xml )
  594.     {
  595.         msg_Err( p_filter, "Failed to open XML parser" );
  596.         return 1;
  597.     }
  598.     psz_buffer = strdup( p_sys->psz_urls );
  599.     psz_buffer_2 = psz_buffer; /* keep track so we can free it */
  600.     for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
  601.     {
  602.         struct rss_feed_t *p_feed = p_sys->p_feeds+i_feed;
  603.         if( psz_buffer == NULL ) break;
  604.         if( psz_buffer[0] == 0 ) psz_buffer++;
  605.         psz_feed = psz_buffer;
  606.         psz_buffer = strchr( psz_buffer, '|' );
  607.         if( psz_buffer != NULL ) psz_buffer[0] = 0;
  608.         p_feed->psz_title = NULL;
  609.         p_feed->psz_description = NULL;
  610.         p_feed->psz_link = NULL;
  611.         p_feed->psz_image = NULL;
  612.         p_feed->p_pic = NULL;
  613.         p_feed->i_items = 0;
  614.         p_feed->p_items = NULL;
  615.         msg_Dbg( p_filter, "opening %s RSS/Atom feed ...", psz_feed );
  616.         p_stream = stream_UrlNew( p_filter, psz_feed );
  617.         if( !p_stream )
  618.         {
  619.             msg_Err( p_filter, "Failed to open %s for reading", psz_feed );
  620.             xml_Delete( p_xml );
  621.             return 1;
  622.         }
  623.         p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
  624.         if( !p_xml_reader )
  625.         {
  626.             msg_Err( p_filter, "Failed to open %s for parsing", psz_feed );
  627.             xml_Delete( p_xml );
  628.             return 1;
  629.         }
  630.         i_item = 0;
  631.         b_is_item = false;
  632.         b_is_image = false;
  633.         while( xml_ReaderRead( p_xml_reader ) == 1 )
  634.         {
  635.             switch( xml_ReaderNodeType( p_xml_reader ) )
  636.             {
  637.                 // Error
  638.                 case -1:
  639.                     return 1;
  640.                 case XML_READER_STARTELEM:
  641.                     free( psz_eltname );
  642.                     psz_eltname = xml_ReaderName( p_xml_reader );
  643.                     if( !psz_eltname )
  644.                     {
  645.                         return 1;
  646.                     }
  647. #                   ifdef RSS_DEBUG
  648.                     msg_Dbg( p_filter, "element name: %s", psz_eltname );
  649. #                   endif
  650.                     if( !strcmp( psz_eltname, "item" ) /* rss */
  651.                      || !strcmp( psz_eltname, "entry" ) ) /* atom */
  652.                     {
  653.                         b_is_item = true;
  654.                         p_feed->i_items++;
  655.                         p_feed->p_items = (struct rss_item_t *)realloc( p_feed->p_items, p_feed->i_items * sizeof( struct rss_item_t ) );
  656.                         p_feed->p_items[p_feed->i_items-1].psz_title = NULL;
  657.                         p_feed->p_items[p_feed->i_items-1].psz_description
  658.                                                                      = NULL;
  659.                         p_feed->p_items[p_feed->i_items-1].psz_link = NULL;
  660.                     }
  661.                     else if( !strcmp( psz_eltname, "image" ) ) /* rss */
  662.                     {
  663.                         b_is_image = true;
  664.                     }
  665.                     else if( !strcmp( psz_eltname, "link" ) ) /* atom */
  666.                     {
  667.                         char *psz_href = NULL;
  668.                         char *psz_rel = NULL;
  669.                         while( xml_ReaderNextAttr( p_xml_reader )
  670.                                == VLC_SUCCESS )
  671.                         {
  672.                             char *psz_name = xml_ReaderName( p_xml_reader );
  673.                             char *psz_value = xml_ReaderValue( p_xml_reader );
  674.                             if( !strcmp( psz_name, "rel" ) )
  675.                             {
  676.                                 if( psz_rel )
  677.                                 {
  678.                                     msg_Dbg( p_filter, ""rel" attribute of link atom duplicated (last value: %s)", psz_value );
  679.                                     free( psz_rel );
  680.                                 }
  681.                                 psz_rel = psz_value;
  682.                             }
  683.                             else if( !strcmp( psz_name, "href" ) )
  684.                             {
  685.                                 if( psz_href )
  686.                                 {
  687.                                     msg_Dbg( p_filter, ""href" attribute of link atom duplicated (last value: %s)", psz_href );
  688.                                     free( psz_href );
  689.                                 }
  690.                                 psz_href = psz_value;
  691.                             }
  692.                             else
  693.                             {
  694.                                 free( psz_value );
  695.                             }
  696.                             free( psz_name );
  697.                         }
  698.                         if( psz_rel && psz_href )
  699.                         {
  700.                             if( !strcmp( psz_rel, "alternate" )
  701.                                 && b_is_item == false
  702.                                 && b_is_image == false
  703.                                 && !p_feed->psz_link )
  704.                             {
  705.                                 p_feed->psz_link = psz_href;
  706.                             }
  707.                             /* this isn't in the rfc but i found some ... */
  708.                             else if( ( !strcmp( psz_rel, "logo" )
  709.                                     || !strcmp( psz_rel, "icon" ) )
  710.                                     && b_is_item == false
  711.                                     && b_is_image == false
  712.                                     && !p_feed->psz_image )
  713.                             {
  714.                                 p_feed->psz_image = psz_href;
  715.                             }
  716.                             else
  717.                             {
  718.                                 free( psz_href );
  719.                             }
  720.                         }
  721.                         else
  722.                         {
  723.                             free( psz_href );
  724.                         }
  725.                         free( psz_rel );
  726.                     }
  727.                     break;
  728.                 case XML_READER_ENDELEM:
  729.                     free( psz_eltname );
  730.                     psz_eltname = NULL;
  731.                     psz_eltname = xml_ReaderName( p_xml_reader );
  732.                     if( !psz_eltname )
  733.                     {
  734.                         return 1;
  735.                     }
  736. #                   ifdef RSS_DEBUG
  737.                     msg_Dbg( p_filter, "element end : %s", psz_eltname );
  738. #                   endif
  739.                     if( !strcmp( psz_eltname, "item" ) /* rss */
  740.                      || !strcmp( psz_eltname, "entry" ) ) /* atom */
  741.                     {
  742.                         b_is_item = false;
  743.                         i_item++;
  744.                     }
  745.                     else if( !strcmp( psz_eltname, "image" ) ) /* rss */
  746.                     {
  747.                         b_is_image = false;
  748.                     }
  749.                     free( psz_eltname );
  750.                     psz_eltname = NULL;
  751.                     break;
  752.                 case XML_READER_TEXT:
  753.                     if( !psz_eltname ) break;
  754.                     psz_eltvalue = xml_ReaderValue( p_xml_reader );
  755.                     if( !psz_eltvalue )
  756.                     {
  757.                         return 1;
  758.                     }
  759.                     else
  760.                     {
  761.                         char *psz_clean;
  762.                         psz_clean = removeWhiteChars( psz_eltvalue );
  763.                         free( psz_eltvalue ); psz_eltvalue = psz_clean;
  764.                     }
  765. #                   ifdef RSS_DEBUG
  766.                     msg_Dbg( p_filter, "  text : <%s>", psz_eltvalue );
  767. #                   endif
  768.                     if( b_is_item == true )
  769.                     {
  770.                         struct rss_item_t *p_item;
  771.                         p_item = p_feed->p_items+i_item;
  772.                         if( !strcmp( psz_eltname, "title" ) /* rss/atom */
  773.                             && !p_item->psz_title )
  774.                         {
  775.                             p_item->psz_title = psz_eltvalue;
  776.                         }
  777.                         else if( !strcmp( psz_eltname, "link" ) /* rss */
  778.                                  && !p_item->psz_link )
  779.                         {
  780.                             p_item->psz_link = psz_eltvalue;
  781.                         }
  782.                         else if((!strcmp( psz_eltname, "description" ) /* rss */
  783.                               || !strcmp( psz_eltname, "summary" ) ) /* atom */
  784.                               && !p_item->psz_description )
  785.                         {
  786.                             p_item->psz_description = psz_eltvalue;
  787.                         }
  788.                         else
  789.                         {
  790.                             free( psz_eltvalue );
  791.                             psz_eltvalue = NULL;
  792.                         }
  793.                     }
  794.                     else if( b_is_image == true )
  795.                     {
  796.                         if( !strcmp( psz_eltname, "url" ) /* rss */
  797.                             && !p_feed->psz_image )
  798.                         {
  799.                             p_feed->psz_image = psz_eltvalue;
  800.                         }
  801.                         else
  802.                         {
  803.                             free( psz_eltvalue );
  804.                             psz_eltvalue = NULL;
  805.                         }
  806.                     }
  807.                     else
  808.                     {
  809.                         if( !strcmp( psz_eltname, "title" ) /* rss/atom */
  810.                             && !p_feed->psz_title )
  811.                         {
  812.                             p_feed->psz_title = psz_eltvalue;
  813.                         }
  814.                         else if( !strcmp( psz_eltname, "link" ) /* rss */
  815.                                  && !p_feed->psz_link )
  816.                         {
  817.                             p_feed->psz_link = psz_eltvalue;
  818.                         }
  819.                         else if((!strcmp( psz_eltname, "description" ) /* rss */
  820.                               || !strcmp( psz_eltname, "subtitle" ) ) /* atom */
  821.                               && !p_feed->psz_description )
  822.                         {
  823.                             p_feed->psz_description = psz_eltvalue;
  824.                         }
  825.                         else if( ( !strcmp( psz_eltname, "logo" ) /* atom */
  826.                               || !strcmp( psz_eltname, "icon" ) ) /* atom */
  827.                               && !p_feed->psz_image )
  828.                         {
  829.                             p_feed->psz_image = psz_eltvalue;
  830.                         }
  831.                         else
  832.                         {
  833.                             free( psz_eltvalue );
  834.                             psz_eltvalue = NULL;
  835.                         }
  836.                     }
  837.                     break;
  838.             }
  839.         }
  840.         if( p_sys->b_images == true
  841.             && p_feed->psz_image && !p_feed->p_pic )
  842.         {
  843.             p_feed->p_pic = LoadImage( p_filter, p_feed->psz_image );
  844.         }
  845.         if( p_xml_reader && p_xml ) xml_ReaderDelete( p_xml, p_xml_reader );
  846.         if( p_stream ) stream_Delete( p_stream );
  847.         msg_Dbg( p_filter, "done with %s RSS/Atom feed", psz_feed );
  848.     }
  849.     free( psz_buffer_2 );
  850.     if( p_xml ) xml_Delete( p_xml );
  851.     return 0;
  852. }
  853. /****************************************************************************
  854.  * FreeRSS
  855.  ***************************************************************************/
  856. static void FreeRSS( filter_t *p_filter)
  857. {
  858.     filter_sys_t *p_sys = p_filter->p_sys;
  859.     struct rss_item_t *p_item;
  860.     struct rss_feed_t *p_feed;
  861.     int i_feed;
  862.     int i_item;
  863.     for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
  864.     {
  865.         p_feed = p_sys->p_feeds+i_feed;
  866.         for( i_item = 0; i_item < p_feed->i_items; i_item++ )
  867.         {
  868.             p_item = p_feed->p_items+i_item;
  869.             free( p_item->psz_title );
  870.             free( p_item->psz_link );
  871.             free( p_item->psz_description );
  872.         }
  873.         free( p_feed->p_items );
  874.         free( p_feed->psz_title);
  875.         free( p_feed->psz_link );
  876.         free( p_feed->psz_description );
  877.         free( p_feed->psz_image );
  878.         if( p_feed->p_pic != NULL )
  879.             picture_Release( p_feed->p_pic );
  880.     }
  881.     free( p_sys->p_feeds );
  882.     p_sys->i_feeds = 0;
  883. }