]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
. fixed the bug-that-made-the-vlc-segfault-on-exit, which means that
[vlc] / plugins / sdl / vout_sdl.c
1 /*****************************************************************************
2  * vout_sdl.c: SDL video output display method
3  *****************************************************************************
4  * Copyright (C) 1998, 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include "defs.h"
27
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                                /* free() */
30 #include <string.h>                                            /* strerror() */
31
32 #include <SDL/SDL.h>
33
34 #include "config.h"
35 #include "common.h"
36 #include "threads.h"
37 #include "mtime.h"
38 #include "plugins.h"
39
40 #include "video.h"
41 #include "video_output.h"
42
43 #include "intf_msg.h"
44
45 /*****************************************************************************
46  * vout_sys_t: video output SDL method descriptor
47  *****************************************************************************
48  * This structure is part of the video output thread descriptor.
49  * It describes the SDL specific properties of an output thread.
50  *****************************************************************************/
51 typedef struct vout_sys_s
52 {
53     int i_width;
54     int i_height;
55     SDL_Surface *   p_display;                             /* display device */
56     SDL_Overlay *   p_overlay;                             /* overlay device */
57     boolean_t   b_fullscreen;
58     boolean_t   b_reopen_display;
59     boolean_t   b_toggle_fullscreen;
60     Uint8   *   p_buffer[2];
61                                                      /* Buffers informations */
62 }   vout_sys_t;
63
64 /*****************************************************************************
65  * Local prototypes
66  *****************************************************************************/
67 static int     SDLOpenDisplay       ( vout_thread_t *p_vout );
68 static void    SDLCloseDisplay      ( vout_thread_t *p_vout );
69 static void    SDLToggleFullScreen  ( vout_thread_t *p_vout );
70
71 /*****************************************************************************
72  * vout_SDLCreate: allocate SDL video thread output method
73  *****************************************************************************
74  * This function allocate and initialize a SDL vout method. It uses some of the
75  * vout properties to choose the correct mode, and change them according to the
76  * mode actually used.
77  *****************************************************************************/
78 int vout_SDLCreate( vout_thread_t *p_vout, char *psz_display,
79                     int i_root_window, void *p_data )
80 {
81     /* Allocate structure */
82     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
83     if( p_vout->p_sys == NULL )
84     {
85         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
86         return( 1 );
87     }
88
89     p_vout->p_sys->p_display = NULL;
90     p_vout->p_sys->p_overlay = NULL;
91
92     /* Initialize library */
93     if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) < 0 )
94     {
95         intf_ErrMsg( "error: can't initialize SDL library: %s",
96                      SDL_GetError() );
97         free( p_vout->p_sys );
98         return( 1 );
99     }
100
101     /* Force the software yuv even if it is not used */
102     /* If we don't do this, p_vout is not correctly initialized
103        and it's impossible to switch between soft/hard yuv */
104     p_vout->b_need_render = 1;
105
106     if(psz_display != NULL && strcmp(psz_display,"fullscreen")==0)
107     {
108         p_vout->p_sys->b_fullscreen = 1;
109     }
110     else
111     {
112         p_vout->p_sys->b_fullscreen = 0;
113     }
114
115     p_vout->p_sys->i_width = VOUT_WIDTH_DEFAULT;
116     p_vout->p_sys->i_height = VOUT_HEIGHT_DEFAULT;
117
118     if( SDLOpenDisplay(p_vout) )
119     {
120       intf_ErrMsg( "error: can't initialize SDL library: %s",
121                    SDL_GetError() );
122       return( 1 );
123     }
124
125     p_vout->p_sys->b_toggle_fullscreen = 0;
126
127     return( 0 );
128 }
129
130 /*****************************************************************************
131  * vout_SDLInit: initialize SDL video thread output method
132  *****************************************************************************
133  * This function initialize the SDL display device.
134  *****************************************************************************/
135 int vout_SDLInit( vout_thread_t *p_vout )
136 {
137     return( 0 );
138 }
139
140 /*****************************************************************************
141  * vout_SDLEnd: terminate Sys video thread output method
142  *****************************************************************************
143  * Terminate an output method created by vout_SDLCreate
144  *****************************************************************************/
145 void vout_SDLEnd( vout_thread_t *p_vout )
146 {
147     SDLCloseDisplay( p_vout );
148     SDL_Quit();
149 }
150
151 /*****************************************************************************
152  * vout_SDLDestroy: destroy Sys video thread output method
153  *****************************************************************************
154  * Terminate an output method created by vout_SDLCreate
155  *****************************************************************************/
156 void vout_SDLDestroy( vout_thread_t *p_vout )
157 {
158     free( p_vout->p_sys );
159 }
160
161 /*****************************************************************************
162  * vout_SDLManage: handle Sys events
163  *****************************************************************************
164  * This function should be called regularly by video output thread. It returns
165  * a non null value if an error occured.
166  *****************************************************************************/
167 int vout_SDLManage( vout_thread_t *p_vout )
168 {
169     /* If the display has to be reopened we do so */
170     if( p_vout->p_sys->b_reopen_display )
171     {
172         SDLCloseDisplay(p_vout);
173
174         if( SDLOpenDisplay(p_vout) )
175         {
176             intf_ErrMsg( "error: can't open DISPLAY default display" );
177             return( 1 );
178         }
179     }
180
181     /* if fullscreen has to be toggled we do so */
182     if( p_vout->p_sys->b_toggle_fullscreen )
183     {
184         SDLToggleFullScreen(p_vout);
185     }
186
187     return( 0 );
188 }
189
190 /*****************************************************************************
191  * vout_SDLSetPalette: sets an 8 bpp palette
192  *****************************************************************************
193  * This function sets the palette given as an argument. It does not return
194  * anything, but could later send information on which colors it was unable
195  * to set.
196  *****************************************************************************/
197 void vout_SDLSetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green, u16 *blue, u16 *transp)
198 {
199      /* Create a display surface with a grayscale palette */
200     SDL_Color colors[256];
201     int i;
202   
203     /* Fill colors with color information */
204     for( i = 0; i < 256; i++ )
205     {
206         colors[ i ].r = red[ i ] >> 8;
207         colors[ i ].g = green[ i ] >> 8;
208         colors[ i ].b = blue[ i ] >> 8;
209     }
210     
211     /* Set palette */
212     if( SDL_SetColors(p_vout->p_sys->p_display, colors, 0, 256) == 0 )
213     {
214         intf_ErrMsg( "vout error: failed setting palette\n" );
215     }
216
217 }
218
219 /*****************************************************************************
220  * vout_SDLDisplay: displays previously rendered output
221  *****************************************************************************
222  * This function send the currently rendered image to the display, wait until
223  * it is displayed and switch the two rendering buffer, preparing next frame.
224  *****************************************************************************/
225 void vout_SDLDisplay( vout_thread_t *p_vout )
226 {
227     SDL_Rect    disp;
228     if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
229     {
230         if(p_vout->b_need_render)
231         {  
232             /* Change display frame */
233             SDL_Flip( p_vout->p_sys->p_display );
234         }
235         else
236         {
237         
238             /*
239              * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
240              * render 
241              */
242             /* TODO: support for streams other than 4:2:0 */
243             /* create the overlay if necessary */
244             if( p_vout->p_sys->p_overlay == NULL )
245             {
246                 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay( 
247                                              p_vout->p_rendered_pic->i_width, 
248                                              p_vout->p_rendered_pic->i_height,
249                                              SDL_YV12_OVERLAY, 
250                                              p_vout->p_sys->p_display
251                                            );
252                 intf_Msg("vout: YUV acceleration set to %d,",
253                             p_vout->p_sys->p_overlay->hw_overlay); 
254             }
255
256             SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
257             /* copy the data into video buffers */
258             /* Y first */
259             memcpy(p_vout->p_sys->p_overlay->pixels[0],
260                    p_vout->p_rendered_pic->p_y,
261                    p_vout->p_sys->p_overlay->h *
262                    p_vout->p_sys->p_overlay->pitches[0]);
263             /* then V */
264             memcpy(p_vout->p_sys->p_overlay->pixels[1],
265                    p_vout->p_rendered_pic->p_v,
266                    p_vout->p_sys->p_overlay->h *
267                    p_vout->p_sys->p_overlay->pitches[1] / 2);
268             /* and U */
269             memcpy(p_vout->p_sys->p_overlay->pixels[2],
270                    p_vout->p_rendered_pic->p_u,
271                    p_vout->p_sys->p_overlay->h *
272                    p_vout->p_sys->p_overlay->pitches[2] / 2);
273
274             disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
275             disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
276             disp.x = (p_vout->i_width - disp.w)/2;
277             disp.y = (p_vout->i_height - disp.h)/2;
278
279             SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
280             SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
281         }
282     }
283 }
284
285 /* following functions are local */
286
287 /*****************************************************************************
288  * SDLOpenDisplay: open and initialize SDL device
289  *****************************************************************************
290  * Open and initialize display according to preferences specified in the vout
291  * thread fields.
292  *****************************************************************************/
293 static int SDLOpenDisplay( vout_thread_t *p_vout )
294 {
295     SDL_Rect    clipping_rect;
296     Uint32      flags;
297     int bpp;
298     /* Open display 
299      * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
300      */
301
302     /* init flags and cursor */
303     flags = SDL_ANYFORMAT | SDL_HWPALETTE;
304
305     if( p_vout->p_sys->b_fullscreen )
306         flags |= SDL_FULLSCREEN;
307     else
308         flags |= SDL_RESIZABLE;
309
310     if( p_vout->b_need_render )
311         flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
312     else
313         flags |= SDL_SWSURFACE; /* save video memory */
314
315     bpp = SDL_VideoModeOK(p_vout->p_sys->i_width,
316                           p_vout->p_sys->i_height,
317                           p_vout->i_screen_depth, flags);
318
319     if(bpp == 0)
320     {
321         intf_ErrMsg( "error: can't open DISPLAY default display" );
322         return( 1 );
323     }
324
325     p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
326                                                 p_vout->p_sys->i_height,
327                                                 bpp, flags);
328
329     if( p_vout->p_sys->p_display == NULL )
330     {
331         intf_ErrMsg( "error: can't open DISPLAY default display" );
332         return( 1 );
333     }
334
335     SDL_LockSurface(p_vout->p_sys->p_display);
336
337     if( p_vout->p_sys->b_fullscreen )
338         SDL_ShowCursor( 0 );
339     else
340         SDL_ShowCursor( 1 );
341
342     SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
343     SDL_EventState(SDL_KEYUP , SDL_IGNORE); /* ignore keys up */
344
345     if( p_vout->b_need_render )
346     {
347         p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
348         SDL_Flip(p_vout->p_sys->p_display);
349         p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
350         SDL_Flip(p_vout->p_sys->p_display);
351
352         /* Set clipping for text */
353         clipping_rect.x = 0;
354         clipping_rect.y = 0;
355         clipping_rect.w = p_vout->p_sys->p_display->w;
356         clipping_rect.h = p_vout->p_sys->p_display->h;
357         SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
358
359         /* Set thread information */
360         p_vout->i_width =           p_vout->p_sys->p_display->w;
361         p_vout->i_height =          p_vout->p_sys->p_display->h;
362         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
363
364         p_vout->i_screen_depth =
365             p_vout->p_sys->p_display->format->BitsPerPixel;
366         p_vout->i_bytes_per_pixel =
367             p_vout->p_sys->p_display->format->BytesPerPixel;
368
369         p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
370         p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
371         p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;
372
373         /* FIXME: palette in 8bpp ?? */
374         /* Set and initialize buffers */
375         vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
376                                  p_vout->p_sys->p_buffer[ 1 ] );
377     }
378     else
379     {
380         p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
381         p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
382
383         /* Set thread information */
384         p_vout->i_width =           p_vout->p_sys->p_display->w;
385         p_vout->i_height =          p_vout->p_sys->p_display->h;
386         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
387
388         vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
389                                  p_vout->p_sys->p_buffer[ 1 ] );
390     }
391
392     p_vout->i_changes |= VOUT_YUV_CHANGE;
393
394     p_vout->p_sys->b_reopen_display = 0;
395
396     return( 0 );
397 }
398
399 /*****************************************************************************
400  * SDLCloseDisplay: close and reset SDL device
401  *****************************************************************************
402  * This function returns all resources allocated by SDLOpenDisplay and restore
403  * the original state of the device.
404  *****************************************************************************/
405 static void SDLCloseDisplay( vout_thread_t *p_vout )
406 {
407     if( p_vout->p_sys->p_display != NULL )
408     {
409         if( p_vout->p_sys->p_overlay != NULL )
410         {            
411             SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
412             p_vout->p_sys->p_overlay = NULL;
413         }
414         SDL_UnlockSurface ( p_vout->p_sys->p_display );
415         SDL_FreeSurface( p_vout->p_sys->p_display );
416         p_vout->p_sys->p_display = NULL;
417     }
418 }
419
420 /*****************************************************************************
421  * SDLToggleFullScreen: toggle fullscreen
422  *****************************************************************************
423  * This function toggles the fullscreen state of the surface.
424  *****************************************************************************/
425 static void SDLToggleFullScreen( vout_thread_t *p_vout )
426 {
427     SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
428
429     if( p_vout->p_sys->b_fullscreen )
430         SDL_ShowCursor( 0 );
431     else
432         SDL_ShowCursor( 1 );
433
434     p_vout->p_sys->b_toggle_fullscreen = 0;
435 }
436