]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/rss.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / video_filter / rss.c
index bf63e0867b2afeea65d57f06b110fa15e25cbcf1..4af49d7af50549570bb8e97881c3ddc99ca59ef9 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
-#include <string.h>
 
 #include <vlc/vlc.h>
-#include <vlc/vout.h>
+#include <vlc_vout.h>
 
 #include "vlc_filter.h"
 #include "vlc_block.h"
@@ -42,7 +40,7 @@
 #include "vlc_block.h"
 #include "vlc_stream.h"
 #include "vlc_xml.h"
-#include "charset.h"
+#include <vlc_charset.h>
 
 #include "vlc_image.h"
 
@@ -60,7 +58,7 @@ static int pi_color_values[] = { 0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0,
                0x00FFFFFF, 0x00800000, 0x00FF0000, 0x00FF00FF, 0x00FFFF00,
                0x00808000, 0x00008000, 0x00008080, 0x0000FF00, 0x00800080,
                0x00000080, 0x000000FF, 0x0000FFFF};
-static char *ppsz_color_descriptions[] = { N_("Default"), N_("Black"),
+static const char *ppsz_color_descriptions[] = { N_("Default"), N_("Black"),
                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"),
@@ -112,6 +110,7 @@ struct filter_sys_t
     int i_ttl;
     time_t t_last_update;
     vlc_bool_t b_images;
+    int i_title;
 
     int i_cur_feed;
     int i_cur_item;
@@ -121,7 +120,7 @@ struct filter_sys_t
 #define MSG_TEXT N_("Feed URLs")
 #define MSG_LONGTEXT N_("RSS/Atom feed '|' (pipe) seperated URLs.")
 #define SPEED_TEXT N_("Speed of feeds")
-#define SPEED_LONGTEXT N_("Speed of the RSS/Atom feeds (bigger is slower).")
+#define SPEED_LONGTEXT N_("Speed of the RSS/Atom feeds in microseconds (bigger is slower).")
 #define LENGTH_TEXT N_("Max length")
 #define LENGTH_LONGTEXT N_("Maximum number of characters displayed on the " \
                 "screen." )
@@ -155,46 +154,73 @@ struct filter_sys_t
   "(0=center, 1=left, 2=right, 4=top, 8=bottom; you can " \
   "also use combinations of these values, eg 6 = top-right).")
 
+#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.")
+
 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
-static char *ppsz_pos_descriptions[] =
+static const char *ppsz_pos_descriptions[] =
      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
 
-#define CFG_PREFIX "rrs-"
+enum title_modes {
+    default_title=-1,
+    hide_title,
+    prepend_title,
+    scroll_title };
+
+static int pi_title_modes[] = { default_title, hide_title, prepend_title, scroll_title };
+static const char *ppsz_title_modes[] =
+    { N_("Default"), N_("Don't show"), N_("Always visible"), N_("Scroll with feed") };
+
+#define CFG_PREFIX "rss-"
 
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
 vlc_module_begin();
-    set_capability( "sub filter", 0 );
+    set_capability( "sub filter", 1 );
     set_shortname( "RSS / Atom" );
     set_callbacks( CreateFilter, DestroyFilter );
     set_category( CAT_VIDEO );
     set_subcategory( SUBCAT_VIDEO_SUBPIC );
     add_string( CFG_PREFIX "urls", "rss", NULL, MSG_TEXT, MSG_LONGTEXT, VLC_FALSE );
+        change_safe();
 
     set_section( N_("Position"), NULL );
-    add_integer( CFG_PREFIX "x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_TRUE );
+    add_integer( CFG_PREFIX "x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_TRUE );
+        change_safe();
     add_integer( CFG_PREFIX "y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_TRUE );
-    add_integer( CFG_PREFIX "position", 5, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
+        change_safe();
+    add_integer( CFG_PREFIX "position", -1, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
+        change_safe();
         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
 
     set_section( N_("Font"), NULL );
     /* 5 sets the default to top [1] left [4] */
     add_integer_with_range( CFG_PREFIX "opacity", 255, 0, 255, NULL,
         OPACITY_TEXT, OPACITY_LONGTEXT, VLC_FALSE );
+        change_safe();
     add_integer( CFG_PREFIX "color", 0xFFFFFF, NULL, COLOR_TEXT, COLOR_LONGTEXT,
                   VLC_FALSE );
+        change_safe();
         change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
     add_integer( CFG_PREFIX "size", -1, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE );
+        change_safe();
 
     set_section( N_("Misc"), NULL );
     add_integer( CFG_PREFIX "speed", 100000, NULL, SPEED_TEXT, SPEED_LONGTEXT,
                  VLC_FALSE );
+        change_safe();
     add_integer( CFG_PREFIX "length", 60, NULL, LENGTH_TEXT, LENGTH_LONGTEXT,
                  VLC_FALSE );
+        change_safe();
     add_integer( CFG_PREFIX "ttl", 1800, NULL, TTL_TEXT, TTL_LONGTEXT, VLC_FALSE );
+        change_safe();
     add_bool( CFG_PREFIX "images", 1, NULL, IMAGE_TEXT, IMAGE_LONGTEXT, VLC_FALSE );
+        change_safe();
+    add_integer( CFG_PREFIX "title", default_title, NULL, TITLE_TEXT, TITLE_LONGTEXT, VLC_FALSE );
+        change_safe();
+        change_integer_list( pi_title_modes, ppsz_title_modes, 0 );
 
     set_description( _("RSS and Atom feed display") );
     add_shortcut( "rss" );
@@ -203,7 +229,7 @@ vlc_module_end();
 
 static const char *ppsz_filter_options[] = {
     "urls", "x", "y", "position", "color", "size", "speed", "length",
-    "ttl", "images", NULL
+    "ttl", "images", "title", NULL
 };
 
 /*****************************************************************************
@@ -229,27 +255,47 @@ static int CreateFilter( vlc_object_t *p_this )
     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
                        p_filter->p_cfg );
 
-    p_sys->psz_urls = var_CreateGetString( p_filter, "rss-urls" );
+    p_sys->psz_urls = var_CreateGetString( p_filter, CFG_PREFIX "urls" );
+    p_sys->i_title = var_CreateGetInteger( p_filter, CFG_PREFIX "title" );
     p_sys->i_cur_feed = 0;
-    p_sys->i_cur_item = 0;
+    p_sys->i_cur_item = p_sys->i_title == scroll_title ? -1 : 0;
     p_sys->i_cur_char = 0;
     p_sys->i_feeds = 0;
     p_sys->p_feeds = NULL;
-    p_sys->i_speed = var_CreateGetInteger( p_filter, "rss-speed" );
-    p_sys->i_length = var_CreateGetInteger( p_filter, "rss-length" );
-    p_sys->i_ttl = __MAX( 0, var_CreateGetInteger( p_filter, "rss-ttl" ) );
-    p_sys->b_images = var_CreateGetBool( p_filter, "rss-images" );
-    p_sys->psz_marquee = (char *)malloc( p_sys->i_length );
+    p_sys->i_speed = var_CreateGetInteger( p_filter, CFG_PREFIX "speed" );
+    p_sys->i_length = var_CreateGetInteger( p_filter, CFG_PREFIX "length" );
+    p_sys->i_ttl = __MAX( 0, var_CreateGetInteger( p_filter, CFG_PREFIX "ttl" ) );
+    p_sys->b_images = var_CreateGetBool( p_filter, CFG_PREFIX "images" );
+
+    p_sys->psz_marquee = (char *)malloc( p_sys->i_length + 1 );
+    if( p_sys->psz_marquee == NULL )
+    {
+        msg_Err( p_filter, "out of memory" );
+        vlc_mutex_unlock( &p_sys->lock );
+        vlc_mutex_destroy( &p_sys->lock );
+        free( p_sys );
+        return VLC_ENOMEM;
+    }
+    p_sys->psz_marquee[p_sys->i_length] = '\0';
 
     p_sys->p_style = malloc( sizeof( text_style_t ));
+    if( p_sys->p_style == NULL )
+    {
+        msg_Err( p_filter, "out of memory" );
+        free( p_sys->psz_marquee );
+        vlc_mutex_unlock( &p_sys->lock );
+        vlc_mutex_destroy( &p_sys->lock );
+        free( p_sys );
+        return VLC_ENOMEM;
+    }
     memcpy( p_sys->p_style, &default_text_style, sizeof( text_style_t ));
 
-    p_sys->i_xoff = var_CreateGetInteger( p_filter, "rss-x" );
-    p_sys->i_yoff = var_CreateGetInteger( p_filter, "rss-y" );
-    p_sys->i_pos = var_CreateGetInteger( p_filter, "rss-position" );
-    p_sys->p_style->i_font_alpha = 255 - var_CreateGetInteger( p_filter, "rss-opacity" );
-    p_sys->p_style->i_font_color = var_CreateGetInteger( p_filter, "rss-color" );
-    p_sys->p_style->i_font_size = var_CreateGetInteger( p_filter, "rss-size" );
+    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" );
+    p_sys->p_style->i_font_alpha = 255 - var_CreateGetInteger( p_filter, CFG_PREFIX "opacity" );
+    p_sys->p_style->i_font_color = var_CreateGetInteger( p_filter, CFG_PREFIX "color" );
+    p_sys->p_style->i_font_size = var_CreateGetInteger( p_filter, CFG_PREFIX "size" );
 
     if( p_sys->b_images == VLC_TRUE && p_sys->p_style->i_font_size == -1 )
     {
@@ -259,23 +305,37 @@ static int CreateFilter( vlc_object_t *p_this )
     if( FetchRSS( p_filter ) )
     {
         msg_Err( p_filter, "failed while fetching RSS ... too bad" );
+        free( p_sys->p_style );
+        free( p_sys->psz_marquee );
         vlc_mutex_unlock( &p_sys->lock );
+        vlc_mutex_destroy( &p_sys->lock );
+        free( p_sys );
         return VLC_EGENERIC;
     }
     p_sys->t_last_update = time( NULL );
 
     if( p_sys->i_feeds == 0 )
     {
+        free( p_sys->p_style );
+        free( p_sys->psz_marquee );
         vlc_mutex_unlock( &p_sys->lock );
+        vlc_mutex_destroy( &p_sys->lock );
+        free( p_sys );
         return VLC_EGENERIC;
     }
     for( i_feed=0; i_feed < p_sys->i_feeds; i_feed ++ )
+    {
         if( p_sys->p_feeds[i_feed].i_items == 0 )
         {
+            free( p_sys->p_style );
+            free( p_sys->psz_marquee );
+            FreeRSS( p_filter );
             vlc_mutex_unlock( &p_sys->lock );
+            vlc_mutex_destroy( &p_sys->lock );
+            free( p_sys );
             return VLC_EGENERIC;
         }
-
+    }
     /* Misc init */
     p_filter->pf_sub_filter = Filter;
     p_sys->last_date = (mtime_t)0;
@@ -303,17 +363,18 @@ static void DestroyFilter( vlc_object_t *p_this )
     free( p_sys );
 
     /* Delete the RSS variables */
-    var_Destroy( p_filter, "rss-urls" );
-    var_Destroy( p_filter, "rss-speed" );
-    var_Destroy( p_filter, "rss-length" );
-    var_Destroy( p_filter, "rss-ttl" );
-    var_Destroy( p_filter, "rss-images" );
-    var_Destroy( p_filter, "rss-x" );
-    var_Destroy( p_filter, "rss-y" );
-    var_Destroy( p_filter, "rss-position" );
-    var_Destroy( p_filter, "rss-color");
-    var_Destroy( p_filter, "rss-opacity");
-    var_Destroy( p_filter, "rss-size");
+    var_Destroy( p_filter, CFG_PREFIX "urls" );
+    var_Destroy( p_filter, CFG_PREFIX "speed" );
+    var_Destroy( p_filter, CFG_PREFIX "length" );
+    var_Destroy( p_filter, CFG_PREFIX "ttl" );
+    var_Destroy( p_filter, CFG_PREFIX "images" );
+    var_Destroy( p_filter, CFG_PREFIX "x" );
+    var_Destroy( p_filter, CFG_PREFIX "y" );
+    var_Destroy( p_filter, CFG_PREFIX "position" );
+    var_Destroy( p_filter, CFG_PREFIX "color");
+    var_Destroy( p_filter, CFG_PREFIX "opacity");
+    var_Destroy( p_filter, CFG_PREFIX "size");
+    var_Destroy( p_filter, CFG_PREFIX "title" );
 }
 
 /****************************************************************************
@@ -325,19 +386,20 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
 {
     filter_sys_t *p_sys = p_filter->p_sys;
     subpicture_t *p_spu;
-    video_format_t fmt = {0};
-
+    video_format_t fmt;
     subpicture_region_t *p_region;
 
     int i_feed, i_item;
 
     struct rss_feed_t *p_feed;
 
+    memset( &fmt, 0, sizeof(video_format_t) );
+
     vlc_mutex_lock( &p_sys->lock );
 
     if( p_sys->last_date
-       + ( p_sys->i_cur_char == 0 && p_sys->i_cur_item == 0 ? 5 : 1 )
-           /* ( ... ? 5 : 1 ) means "wait more for the 1st char" */
+       + ( p_sys->i_cur_char == 0 && p_sys->i_cur_item == ( p_sys->i_title == scroll_title ? -1 : 0 ) ? 5 : 1 )
+           /* ( ... ? 5 : 1 ) means "wait 5 times more for the 1st char" */
        * p_sys->i_speed > date )
     {
         vlc_mutex_unlock( &p_sys->lock );
@@ -351,7 +413,7 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
         msg_Dbg( p_filter, "Forcing update of all the RSS feeds" );
         if( FetchRSS( p_filter ) )
         {
-            msg_Err( p_filter, "failed while fetching RSS ... too bad" );
+            msg_Err( p_filter, "Failed while fetching RSS ... too bad" );
             vlc_mutex_unlock( &p_sys->lock );
             return NULL; /* FIXME : we most likely messed up all the data,
                           * so we might need to do something about it */
@@ -361,13 +423,16 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
 
     p_sys->last_date = date;
     p_sys->i_cur_char++;
-    if( p_sys->p_feeds[p_sys->i_cur_feed].p_items[p_sys->i_cur_item].psz_title[p_sys->i_cur_char] == 0 )
+    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 )
     {
         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 )
         {
-            p_sys->i_cur_item = 0;
+            if( p_sys->i_title == scroll_title )
+                p_sys->i_cur_item = -1;
+            else
+                p_sys->i_cur_item = 0;
             p_sys->i_cur_feed = (p_sys->i_cur_feed + 1)%p_sys->i_feeds;
         }
     }
@@ -393,22 +458,35 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
        be p_sys->i_length characters long. */
     i_item = p_sys->i_cur_item;
     i_feed = p_sys->i_cur_feed;
-    p_feed = &p_sys->p_feeds[p_sys->i_cur_feed];
+    p_feed = &p_sys->p_feeds[i_feed];
 
-    if( p_feed->p_pic )
+    if( ( p_feed->p_pic && p_sys->i_title == default_title )
+        || p_sys->i_title == hide_title )
     {
         /* 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 );
     }
-    else
+    else if( ( !p_feed->p_pic && p_sys->i_title == default_title )
+             || p_sys->i_title == prepend_title )
     {
         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 );
     }
+    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 );
+    }
 
     while( strlen( p_sys->psz_marquee ) < (unsigned int)p_sys->i_length )
     {
@@ -437,26 +515,27 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
     }
 
     p_spu->p_region->psz_text = strdup(p_sys->psz_marquee);
+    if( p_sys->p_style->i_font_size > 0 )
+        p_spu->p_region->fmt.i_visible_height = p_sys->p_style->i_font_size;
     p_spu->i_start = date;
     p_spu->i_stop  = 0;
     p_spu->b_ephemer = VLC_TRUE;
 
     /*  where to locate the string: */
-    if( p_sys->i_xoff < 0 || p_sys->i_yoff < 0 )
-    {   /* set to one of the 9 relative locations */
-        p_spu->i_flags = p_sys->i_pos;
-        p_spu->i_x = 0;
-        p_spu->i_y = 0;
-        p_spu->b_absolute = VLC_FALSE;
+    if( p_sys->i_pos < 0 )
+    {   /*  set to an absolute xy */
+        p_spu->p_region->i_align = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
+        p_spu->b_absolute = VLC_TRUE;
     }
     else
-    {   /*  set to an absolute xy, referenced to upper left corner */
-        p_spu->i_flags = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
-        p_spu->i_x = p_sys->i_xoff;
-        p_spu->i_y = p_sys->i_yoff;
-        p_spu->b_absolute = VLC_TRUE;
+    {   /* set to one of the 9 relative locations */
+        p_spu->p_region->i_align = p_sys->i_pos;
+        p_spu->b_absolute = VLC_FALSE;
     }
 
+    p_spu->i_x = p_sys->i_xoff;
+    p_spu->i_y = p_sys->i_yoff;
+
     p_spu->i_height = 1;
     p_spu->p_region->p_style = p_sys->p_style;
 
@@ -464,7 +543,9 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
     {
         /* Display the feed's image */
         picture_t *p_pic = p_feed->p_pic;
-        video_format_t fmt_out = {0};
+        video_format_t fmt_out;
+
+        memset( &fmt_out, 0, sizeof(video_format_t) );
 
         fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
         fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
@@ -505,20 +586,20 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
 /****************************************************************************
  * download and resize image located at psz_url
  ***************************************************************************/
-picture_t *LoadImage( filter_t *p_filter, const char *psz_url )
+static picture_t *LoadImage( filter_t *p_filter, const char *psz_url )
 {
     filter_sys_t *p_sys = p_filter->p_sys;
-
-    video_format_t fmt_in={0}, fmt_out={0};
-    picture_t *p_orig, *p_pic=NULL;
+    video_format_t fmt_in;
+    video_format_t fmt_out;
+    picture_t *p_orig;
+    picture_t *p_pic = NULL;
     image_handler_t *p_handler = image_HandlerCreate( p_filter );
 
-    char *psz_local;
+    memset( &fmt_in, 0, sizeof(video_format_t) );
+    memset( &fmt_out, 0, sizeof(video_format_t) );
 
-    psz_local = ToLocale( psz_url );
     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
-    p_orig = image_ReadUrl( p_handler, psz_local, &fmt_in, &fmt_out );
-    LocaleFree( psz_local );
+    p_orig = image_ReadUrl( p_handler, psz_url, &fmt_in, &fmt_out );
 
     if( !p_orig )
     {
@@ -555,7 +636,7 @@ picture_t *LoadImage( filter_t *p_filter, const char *psz_url )
  * remove all ' ' '\t' '\n' '\r' characters from the begining and end of the
  * string.
  ***************************************************************************/
-char *removeWhiteChars( char *psz_src )
+static char *removeWhiteChars( char *psz_src )
 {
     char *psz_src2 = strdup( psz_src );
     char *psz_clean = strdup( psz_src2 );