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