]> git.sesse.net Git - vlc/blob - src/video_output/video_text.c
Used a vout object for vout_OSDMessage/OSDSlider/OSDIcon.
[vlc] / src / video_output / video_text.c
1 /*****************************************************************************
2  * video_text.c : text manipulation functions
3  *****************************************************************************
4  * Copyright (C) 1999-2007 the VideoLAN team
5  * $Id$
6  *
7  * Author: Sigmund Augdal Helberg <dnumgis@videolan.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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <assert.h>
28
29 #include <vlc_common.h>
30 #include <vlc_vout.h>
31 #include <vlc_block.h>
32 #include <vlc_filter.h>
33 #include <vlc_osd.h>
34
35 /* TODO remove access to private vout data */
36 #include "vout_internal.h"
37
38 /**
39  * \brief Show text on the video from a given start date to a given end date
40  * \param p_vout pointer to the vout the text is to be showed on
41  * \param i_channel Subpicture channel
42  * \param psz_string The text to be shown
43  * \param p_style Pointer to a struct with text style info (it is duplicated if non NULL)
44  * \param i_flags flags for alignment and such
45  * \param i_hmargin horizontal margin in pixels
46  * \param i_vmargin vertical margin in pixels
47  * \param i_duration Amount of time the text is to be shown.
48  */
49 int vout_ShowTextRelative( vout_thread_t *p_vout, int i_channel,
50                            const char *psz_string, const text_style_t *p_style,
51                            int i_flags, int i_hmargin, int i_vmargin,
52                            mtime_t i_duration )
53 {
54     subpicture_t *p_spu;
55     video_format_t fmt;
56
57     if( !psz_string ) return VLC_EGENERIC;
58
59     p_spu = subpicture_New( NULL );
60     if( !p_spu )
61         return VLC_EGENERIC;
62
63     p_spu->i_channel = i_channel;
64     p_spu->i_start = mdate();
65     p_spu->i_stop  = p_spu->i_start + i_duration;
66     p_spu->b_ephemer = true;
67     p_spu->b_absolute = false;
68     p_spu->b_fade = true;
69
70
71     /* Create a new subpicture region */
72     memset( &fmt, 0, sizeof(video_format_t) );
73     fmt.i_chroma = VLC_CODEC_TEXT;
74     fmt.i_width = fmt.i_height = 0;
75     fmt.i_x_offset = fmt.i_y_offset = 0;
76     p_spu->p_region = subpicture_region_New( &fmt );
77     if( !p_spu->p_region )
78     {
79         msg_Err( p_vout, "cannot allocate SPU region" );
80         subpicture_Delete( p_spu );
81         return VLC_EGENERIC;
82     }
83
84     p_spu->p_region->psz_text = strdup( psz_string );
85     p_spu->p_region->i_align = i_flags & SUBPICTURE_ALIGN_MASK;
86     p_spu->p_region->i_x = i_hmargin;
87     p_spu->p_region->i_y = i_vmargin;
88     if( p_style )
89         p_spu->p_region->p_style = text_style_Duplicate( p_style );
90
91     spu_DisplaySubpicture( vout_GetSpu( p_vout ), p_spu );
92
93     return VLC_SUCCESS;
94 }
95
96 /**
97  * \brief Write an informative message at the default location,
98  *        for the default duration and only if the OSD option is enabled.
99  * \param p_caller The object that called the function.
100  * \param i_channel Subpicture channel
101  * \param psz_format printf style formatting
102  **/
103 void vout_OSDMessage( vout_thread_t *p_vout, int i_channel,
104                       const char *psz_format, ... )
105 {
106     if( !var_InheritBool( p_vout, "osd" ) )
107         return;
108
109     va_list args;
110     va_start( args, psz_format );
111
112     char *psz_string;
113     if( vasprintf( &psz_string, psz_format, args ) != -1 )
114     {
115         vout_ShowTextRelative( p_vout, i_channel, psz_string, NULL,
116                                SUBPICTURE_ALIGN_TOP|SUBPICTURE_ALIGN_RIGHT,
117                                30 + p_vout->p->fmt_in.i_width
118                                   - p_vout->p->fmt_in.i_visible_width
119                                   - p_vout->p->fmt_in.i_x_offset,
120                                20 + p_vout->p->fmt_in.i_y_offset, 1000000 );
121         free( psz_string );
122     }
123     va_end( args );
124 }
125