]> git.sesse.net Git - vlc/blob - src/osd/osd_widgets.c
size_t / int confusion
[vlc] / src / osd / osd_widgets.c
1 /*****************************************************************************
2  * osd_widgets.c : OSD widgets manipulation functions
3  *****************************************************************************
4  * Copyright (C) 2004-2007 the VideoLAN team
5  * $Id$
6  *
7  * Author: Yoann Peronneau <yoann@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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc_osd.h>
29 #include <vlc_vout.h>
30 #include <vlc_filter.h>
31
32 #define STYLE_EMPTY 0
33 #define STYLE_FILLED 1
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static void DrawRect( subpicture_t *, int, int, int, int, short );
39 static void DrawTriangle( subpicture_t *, int, int, int, int, short );
40 static int  CreatePicture( spu_t *, subpicture_t *, int, int, int, int );
41 static subpicture_t *osd_CreateWidget( spu_t *, int );
42
43 /*****************************************************************************
44  * Draws a rectangle at the given position in the subpic.
45  * It may be filled (fill == STYLE_FILLED) or empty (fill == STYLE_EMPTY).
46  *****************************************************************************/
47 static void DrawRect( subpicture_t *p_subpic, int i_x1, int i_y1,
48                       int i_x2, int i_y2, short fill )
49 {
50     int x, y;
51     uint8_t *p_a = p_subpic->p_region->picture.A_PIXELS;
52     int i_pitch = p_subpic->p_region->picture.Y_PITCH;
53
54     if( fill == STYLE_FILLED )
55     {
56         for( y = i_y1; y <= i_y2; y++ )
57         {
58             for( x = i_x1; x <= i_x2; x++ )
59             {
60                 p_a[ x + i_pitch * y ] = 0xff;
61             }
62         }
63     }
64     else
65     {
66         for( y = i_y1; y <= i_y2; y++ )
67         {
68             p_a[ i_x1 + i_pitch * y ] = 0xff;
69             p_a[ i_x2 + i_pitch * y ] = 0xff;
70         }
71         for( x = i_x1; x <= i_x2; x++ )
72         {
73             p_a[ x + i_pitch * i_y1 ] = 0xff;
74             p_a[ x + i_pitch * i_y2 ] = 0xff;
75         }
76     }
77 }
78
79 /*****************************************************************************
80  * Draws a triangle at the given position in the subpic.
81  * It may be filled (fill == STYLE_FILLED) or empty (fill == STYLE_EMPTY).
82  *****************************************************************************/
83 static void DrawTriangle( subpicture_t *p_subpic, int i_x1, int i_y1,
84                           int i_x2, int i_y2, short fill )
85 {
86     int x, y, i_mid, h;
87     uint8_t *p_a = p_subpic->p_region->picture.A_PIXELS;
88     int i_pitch = p_subpic->p_region->picture.Y_PITCH;
89
90     i_mid = i_y1 + ( ( i_y2 - i_y1 ) >> 1 );
91
92     if( i_x2 >= i_x1 )
93     {
94         if( fill == STYLE_FILLED )
95         {
96             for( y = i_y1; y <= i_mid; y++ )
97             {
98                 h = y - i_y1;
99                 for( x = i_x1; x <= i_x1 + h && x <= i_x2; x++ )
100                 {
101                     p_a[ x + i_pitch * y ] = 0xff;
102                     p_a[ x + i_pitch * ( i_y2 - h ) ] = 0xff;
103                 }
104             }
105         }
106         else
107         {
108             for( y = i_y1; y <= i_mid; y++ )
109             {
110                 h = y - i_y1;
111                 p_a[ i_x1 + i_pitch * y ] = 0xff;
112                 p_a[ i_x1 + h + i_pitch * y ] = 0xff;
113                 p_a[ i_x1 + i_pitch * ( i_y2 - h ) ] = 0xff;
114                 p_a[ i_x1 + h + i_pitch * ( i_y2 - h ) ] = 0xff;
115             }
116         }
117     }
118     else
119     {
120         if( fill == STYLE_FILLED )
121         {
122             for( y = i_y1; y <= i_mid; y++ )
123             {
124                 h = y - i_y1;
125                 for( x = i_x1; x >= i_x1 - h && x >= i_x2; x-- )
126                 {
127                     p_a[ x + i_pitch * y ] = 0xff;
128                     p_a[ x + i_pitch * ( i_y2 - h ) ] = 0xff;
129                 }
130             }
131         }
132         else
133         {
134             for( y = i_y1; y <= i_mid; y++ )
135             {
136                 h = y - i_y1;
137                 p_a[ i_x1 + i_pitch * y ] = 0xff;
138                 p_a[ i_x1 - h + i_pitch * y ] = 0xff;
139                 p_a[ i_x1 + i_pitch * ( i_y2 - h ) ] = 0xff;
140                 p_a[ i_x1 - h + i_pitch * ( i_y2 - h ) ] = 0xff;
141             }
142         }
143     }
144 }
145
146 /*****************************************************************************
147  * Create Picture: creates subpicture region and picture
148  *****************************************************************************/
149 static int CreatePicture( spu_t *p_spu, subpicture_t *p_subpic,
150                            int i_x, int i_y, int i_width, int i_height )
151 {
152     uint8_t *p_y, *p_u, *p_v, *p_a;
153     video_format_t fmt;
154     int i_pitch;
155
156     /* Create a new subpicture region */
157     memset( &fmt, 0, sizeof(video_format_t) );
158     fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
159     fmt.i_aspect = 0;
160     fmt.i_width = fmt.i_visible_width = i_width;
161     fmt.i_height = fmt.i_visible_height = i_height;
162     fmt.i_x_offset = fmt.i_y_offset = 0;
163     p_subpic->p_region = p_subpic->pf_create_region( VLC_OBJECT(p_spu), &fmt );
164     if( !p_subpic->p_region )
165     {
166         msg_Err( p_spu, "cannot allocate SPU region" );
167         return VLC_EGENERIC;
168     }
169
170     p_subpic->p_region->i_x = i_x;
171     p_subpic->p_region->i_y = i_y;
172     p_y = p_subpic->p_region->picture.Y_PIXELS;
173     p_u = p_subpic->p_region->picture.U_PIXELS;
174     p_v = p_subpic->p_region->picture.V_PIXELS;
175     p_a = p_subpic->p_region->picture.A_PIXELS;
176     i_pitch = p_subpic->p_region->picture.Y_PITCH;
177
178     /* Initialize the region pixels (only the alpha will be changed later) */
179     memset( p_y, 0xff, i_pitch * p_subpic->p_region->fmt.i_height );
180     memset( p_u, 0x80, i_pitch * p_subpic->p_region->fmt.i_height );
181     memset( p_v, 0x80, i_pitch * p_subpic->p_region->fmt.i_height );
182     memset( p_a, 0x00, i_pitch * p_subpic->p_region->fmt.i_height );
183
184     return VLC_SUCCESS;
185 }
186
187 /*****************************************************************************
188  * Creates and initializes an OSD widget.
189  *****************************************************************************/
190 subpicture_t *osd_CreateWidget( spu_t *p_spu, int i_channel )
191 {
192     subpicture_t *p_subpic;
193     mtime_t i_now = mdate();
194
195     /* Create and initialize a subpicture */
196     p_subpic = spu_CreateSubpicture( p_spu );
197     if( p_subpic == NULL ) return NULL;
198
199     p_subpic->i_channel = i_channel;
200     p_subpic->i_start = i_now;
201     p_subpic->i_stop = i_now + 1200000;
202     p_subpic->b_ephemer = VLC_TRUE;
203     p_subpic->b_fade = VLC_TRUE;
204
205     return p_subpic;
206 }
207
208 /*****************************************************************************
209  * Displays an OSD slider.
210  * Types are: OSD_HOR_SLIDER and OSD_VERT_SLIDER.
211  *****************************************************************************/
212 int osd_Slider( vlc_object_t *p_this, spu_t *p_spu,
213     int i_render_width, int i_render_height,
214     int i_margin_left, int i_margin_bottom,
215     int i_channel, int i_position, short i_type )
216 {
217     subpicture_t *p_subpic;
218     int i_x_margin, i_y_margin, i_x, i_y, i_width, i_height;
219     (void)p_this;
220
221     p_subpic = osd_CreateWidget( p_spu, i_channel );
222     if( p_subpic == NULL )
223     {
224         return VLC_EGENERIC;
225     }
226
227     i_y_margin = i_render_height / 10;
228     i_x_margin = i_y_margin + i_margin_left;
229     i_y_margin += i_margin_bottom;
230
231     if( i_type == OSD_HOR_SLIDER )
232     {
233         i_width = i_render_width - 2 * i_x_margin;
234         i_height = i_render_height / 20;
235         i_x = i_x_margin;
236         i_y = i_render_height - i_y_margin - i_height;
237     }
238     else
239     {
240         i_width = i_render_width / 40;
241         i_height = i_render_height - 2 * i_y_margin;
242         i_x = i_render_width - i_x_margin - i_width;
243         i_y = i_y_margin;
244     }
245
246     /* Create subpicture region and picture */
247     CreatePicture( p_spu, p_subpic, i_x, i_y, i_width, i_height );
248
249     if( i_type == OSD_HOR_SLIDER )
250     {
251         int i_x_pos = ( i_width - 2 ) * i_position / 100;
252         DrawRect( p_subpic, i_x_pos - 1, 2, i_x_pos + 1,
253                   i_height - 3, STYLE_FILLED );
254         DrawRect( p_subpic, 0, 0, i_width - 1, i_height - 1, STYLE_EMPTY );
255     }
256     else if( i_type == OSD_VERT_SLIDER )
257     {
258         int i_y_pos = i_height / 2;
259         DrawRect( p_subpic, 2, i_height - ( i_height - 2 ) * i_position / 100,
260                   i_width - 3, i_height - 3, STYLE_FILLED );
261         DrawRect( p_subpic, 1, i_y_pos, 1, i_y_pos, STYLE_FILLED );
262         DrawRect( p_subpic, i_width - 2, i_y_pos,
263                   i_width - 2, i_y_pos, STYLE_FILLED );
264         DrawRect( p_subpic, 0, 0, i_width - 1, i_height - 1, STYLE_EMPTY );
265     }
266
267     spu_DisplaySubpicture( p_spu, p_subpic );
268
269     return VLC_SUCCESS;
270 }
271
272 /*****************************************************************************
273  * Displays an OSD icon.
274  * Types are: OSD_PLAY_ICON, OSD_PAUSE_ICON, OSD_SPEAKER_ICON, OSD_MUTE_ICON
275  *****************************************************************************/
276 int osd_Icon( vlc_object_t *p_this, spu_t *p_spu,
277     int i_render_width, int i_render_height, int i_margin_right,
278     int i_margin_top, int i_channel, short i_type )
279 {
280     subpicture_t *p_subpic;
281     int i_x_margin, i_y_margin, i_x, i_y, i_width, i_height;
282     (void)p_this;
283
284     p_subpic = osd_CreateWidget( p_spu, i_channel );
285     if( p_subpic == NULL )
286     {
287         return VLC_EGENERIC;
288     }
289
290     i_y_margin = i_render_height / 15;
291     i_x_margin = i_y_margin + i_margin_right;
292     i_y_margin += i_margin_top;
293     i_width = i_render_width / 20;
294     i_height = i_width;
295     i_x = i_render_width - i_x_margin - i_width;
296     i_y = i_y_margin;
297
298     /* Create subpicture region and picture */
299     CreatePicture( p_spu, p_subpic, i_x, i_y, i_width, i_height );
300
301     if( i_type == OSD_PAUSE_ICON )
302     {
303         int i_bar_width = i_width / 3;
304         DrawRect( p_subpic, 0, 0, i_bar_width - 1, i_height -1, STYLE_FILLED );
305         DrawRect( p_subpic, i_width - i_bar_width, 0,
306                   i_width - 1, i_height - 1, STYLE_FILLED );
307     }
308     else if( i_type == OSD_PLAY_ICON )
309     {
310         int i_mid = i_height >> 1;
311         int i_delta = ( i_width - i_mid ) >> 1;
312         int i_y2 = ( ( i_height - 1 ) >> 1 ) * 2;
313         DrawTriangle( p_subpic, i_delta, 0, i_width - i_delta, i_y2,
314                       STYLE_FILLED );
315     }
316     else if( i_type == OSD_SPEAKER_ICON || i_type == OSD_MUTE_ICON )
317     {
318         int i_mid = i_height >> 1;
319         int i_delta = ( i_width - i_mid ) >> 1;
320         int i_y2 = ( ( i_height - 1 ) >> 1 ) * 2;
321         DrawRect( p_subpic, i_delta, i_mid / 2, i_width - i_delta,
322                   i_height - 1 - i_mid / 2, STYLE_FILLED );
323         DrawTriangle( p_subpic, i_width - i_delta, 0, i_delta, i_y2,
324                       STYLE_FILLED );
325         if( i_type == OSD_MUTE_ICON )
326         {
327             uint8_t *p_a = p_subpic->p_region->picture.A_PIXELS;
328             int i_pitch = p_subpic->p_region->picture.Y_PITCH;
329             int i;
330             for( i = 1; i < i_pitch; i++ )
331             {
332                 int k = i + ( i_height - i - 1 ) * i_pitch;
333                 p_a[ k ] = 0xff - p_a[ k ];
334             }
335         }
336     }
337
338     spu_DisplaySubpicture( p_spu, p_subpic );
339
340     return VLC_SUCCESS;
341 }