]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
* SDL compilation fix for FreeBSD.
[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  * $Id: vout_sdl.c,v 1.55 2001/06/07 22:14:56 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Pierre Baillet <oct@zoy.org>
9  *          Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 #define MODULE_NAME sdl
27 #include "modules_inner.h"
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include "defs.h"
33
34 #include <errno.h>                                                 /* ENOMEM */
35 #include <stdlib.h>                                                /* free() */
36 #include <string.h>                                            /* strerror() */
37
38 #include <sys/types.h>
39 #ifndef WIN32
40 #   include <netinet/in.h>                            /* BSD: struct in_addr */
41 #endif
42
43 #include <SDL/SDL.h>
44
45 #include "config.h"
46 #include "common.h"
47 #include "threads.h"
48 #include "mtime.h"
49 #include "tests.h"
50
51 #include "video.h"
52 #include "video_output.h"
53
54 #include "intf_msg.h"
55 #include "interface.h"
56 /* FIXME: get rid of this */
57 #include "keystrokes.h"
58 #include "main.h"
59 #include "netutils.h"
60
61 #include "modules.h"
62 #include "modules_export.h"
63
64 /*****************************************************************************
65  * FIXME: this file is ...                                                   *
66  *                                                                           *
67  *              XXX   XXX     FIXME     XXX     XXX   XXX   XXX              *
68  *              XXX   XXX   XXX   XXX   XXX     XXX   XXX   XXX              *
69  *              XXX   XXX   XXX         XXX       FIXME     XXX              *
70  *              XXX   XXX   XXX  TODO   XXX        XXX      XXX              *
71  *              XXX   XXX   XXX   XXX   XXX        XXX                       *
72  *                FIXME       FIXME       FIXME    XXX      XXX              *
73  *                                                                           *
74  *****************************************************************************/
75
76 /*****************************************************************************
77  * vout_sys_t: video output SDL method descriptor
78  *****************************************************************************
79  * This structure is part of the video output thread descriptor.
80  * It describes the SDL specific properties of an output thread.
81  *****************************************************************************/
82 typedef struct vout_sys_s
83 {
84     int i_width;
85     int i_height;
86
87     SDL_Surface *   p_display;                             /* display device */
88     SDL_Overlay *   p_overlay;                             /* overlay device */
89
90     boolean_t   b_overlay;
91     boolean_t   b_cursor;
92     boolean_t   b_reopen_display;
93
94     boolean_t   b_cursor_autohidden;
95     mtime_t     i_lastmoved;
96
97     Uint8   *   p_sdl_buf[2];                          /* Buffer information */
98
99 } vout_sys_t;
100
101 /*****************************************************************************
102  * Local prototypes.
103  *****************************************************************************/
104 static int  vout_Probe     ( probedata_t *p_data );
105 static int  vout_Create    ( struct vout_thread_s * );
106 static int  vout_Init      ( struct vout_thread_s * );
107 static void vout_End       ( struct vout_thread_s * );
108 static void vout_Destroy   ( struct vout_thread_s * );
109 static int  vout_Manage    ( struct vout_thread_s * );
110 static void vout_Display   ( struct vout_thread_s * );
111 static void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
112                              u16 *blue, u16 *transp );
113
114 static int  SDLOpenDisplay      ( vout_thread_t *p_vout );
115 static void SDLCloseDisplay     ( vout_thread_t *p_vout );
116
117 /*****************************************************************************
118  * Functions exported as capabilities. They are declared as static so that
119  * we don't pollute the namespace too much.
120  *****************************************************************************/
121 void _M( vout_getfunctions )( function_list_t * p_function_list )
122 {
123     p_function_list->pf_probe = vout_Probe;
124     p_function_list->functions.vout.pf_create     = vout_Create;
125     p_function_list->functions.vout.pf_init       = vout_Init;
126     p_function_list->functions.vout.pf_end        = vout_End;
127     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
128     p_function_list->functions.vout.pf_manage     = vout_Manage;
129     p_function_list->functions.vout.pf_display    = vout_Display;
130     p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
131 }
132
133 /*****************************************************************************
134  * vout_Probe: probe the video driver and return a score
135  *****************************************************************************
136  * This function tries to initialize SDL and returns a score to the
137  * plugin manager so that it can select the best plugin.
138  *****************************************************************************/
139 static int vout_Probe( probedata_t *p_data )
140 {
141     if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
142     {
143         return( 999 );
144     }
145
146     return( 100 );
147 }
148
149 /*****************************************************************************
150  * vout_Create: allocate SDL video thread output method
151  *****************************************************************************
152  * This function allocate and initialize a SDL vout method. It uses some of the
153  * vout properties to choose the correct mode, and change them according to the
154  * mode actually used.
155  *****************************************************************************/
156 static int vout_Create( vout_thread_t *p_vout )
157 {
158     /* Allocate structure */
159     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
160     if( p_vout->p_sys == NULL )
161     {
162         intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
163         return( 1 );
164     }
165
166     /* Initialize library */
167     if( SDL_Init( SDL_INIT_VIDEO
168 #ifndef WIN32
169     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
170                 | SDL_INIT_EVENTTHREAD
171 #endif
172                 | SDL_INIT_NOPARACHUTE ) < 0 )
173     {
174         intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
175         free( p_vout->p_sys );
176         return( 1 );
177     }
178
179     p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
180
181     p_vout->p_sys->b_cursor_autohidden = 0;
182     p_vout->p_sys->i_lastmoved = mdate();
183
184     p_vout->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
185                                 VOUT_FULLSCREEN_DEFAULT );
186     p_vout->p_sys->b_overlay = main_GetIntVariable( VOUT_OVERLAY_VAR,
187                                 VOUT_OVERLAY_DEFAULT );
188     p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR, 
189                                 VOUT_WIDTH_DEFAULT );
190     p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
191                                 VOUT_HEIGHT_DEFAULT );
192
193     p_vout->p_sys->p_display = NULL;
194     p_vout->p_sys->p_overlay = NULL;
195
196     if( SDLOpenDisplay(p_vout) )
197     {
198         intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
199         free( p_vout->p_sys );
200         return( 1 );
201     }
202
203     /* FIXME: get rid of this ASAP, it's FUCKING UGLY */
204     { intf_thread_t * p_intf = p_main->p_intf;
205     intf_AssignKey(p_intf, SDLK_q,      INTF_KEY_QUIT, 0);
206     intf_AssignKey(p_intf, SDLK_ESCAPE, INTF_KEY_QUIT, 0);
207     /* intf_AssignKey(p_intf,3,'Q'); */
208     intf_AssignKey(p_intf, SDLK_0,      INTF_KEY_SET_CHANNEL,0);
209     intf_AssignKey(p_intf, SDLK_1,      INTF_KEY_SET_CHANNEL,1);
210     intf_AssignKey(p_intf, SDLK_2,      INTF_KEY_SET_CHANNEL,2);
211     intf_AssignKey(p_intf, SDLK_3,      INTF_KEY_SET_CHANNEL,3);
212     intf_AssignKey(p_intf, SDLK_4,      INTF_KEY_SET_CHANNEL,4);
213     intf_AssignKey(p_intf, SDLK_5,      INTF_KEY_SET_CHANNEL,5);
214     intf_AssignKey(p_intf, SDLK_6,      INTF_KEY_SET_CHANNEL,6);
215     intf_AssignKey(p_intf, SDLK_7,      INTF_KEY_SET_CHANNEL,7);
216     intf_AssignKey(p_intf, SDLK_8,      INTF_KEY_SET_CHANNEL,8);
217     intf_AssignKey(p_intf, SDLK_9,      INTF_KEY_SET_CHANNEL,9);
218     intf_AssignKey(p_intf, SDLK_PLUS,   INTF_KEY_INC_VOLUME, 0);
219     intf_AssignKey(p_intf, SDLK_MINUS,  INTF_KEY_DEC_VOLUME, 0);
220     intf_AssignKey(p_intf, SDLK_m,      INTF_KEY_TOGGLE_VOLUME, 0);
221     /* intf_AssignKey(p_intf,'M','M'); */
222     intf_AssignKey(p_intf, SDLK_g,      INTF_KEY_DEC_GAMMA, 0);
223     /* intf_AssignKey(p_intf,'G','G'); */
224     intf_AssignKey(p_intf, SDLK_c,      INTF_KEY_TOGGLE_GRAYSCALE, 0);
225     intf_AssignKey(p_intf, SDLK_SPACE,  INTF_KEY_TOGGLE_INTERFACE, 0);
226     intf_AssignKey(p_intf, SDLK_i,      INTF_KEY_TOGGLE_INFO, 0);
227     intf_AssignKey(p_intf, SDLK_s,      INTF_KEY_TOGGLE_SCALING, 0);
228     intf_AssignKey(p_intf, SDLK_d,      INTF_KEY_DUMP_STREAM, 0); }
229
230     return( 0 );
231 }
232
233 /*****************************************************************************
234  * vout_Init: initialize SDL video thread output method
235  *****************************************************************************
236  * This function initialize the SDL display device.
237  *****************************************************************************/
238 static int vout_Init( vout_thread_t *p_vout )
239 {
240     /* This hack is hugly, but hey, you are, too. */
241
242     SDL_Overlay *   p_overlay;
243     
244     p_overlay = SDL_CreateYUVOverlay( 
245            main_GetIntVariable( VOUT_WIDTH_VAR,VOUT_WIDTH_DEFAULT ),
246            main_GetIntVariable( VOUT_HEIGHT_VAR,VOUT_HEIGHT_DEFAULT ),
247                                       SDL_YV12_OVERLAY, 
248                                       p_vout->p_sys->p_display );
249     intf_WarnMsg( 2, "vout: YUV acceleration %s",
250               p_overlay->hw_overlay ? "activated" : "unavailable !" ); 
251     p_vout->b_need_render = !p_overlay->hw_overlay;
252
253     SDL_FreeYUVOverlay( p_overlay );
254
255     return( 0 );
256 }
257
258 /*****************************************************************************
259  * vout_End: terminate Sys video thread output method
260  *****************************************************************************
261  * Terminate an output method created by vout_SDLCreate
262  *****************************************************************************/
263 static void vout_End( vout_thread_t *p_vout )
264 {
265     SDLCloseDisplay( p_vout );
266     SDL_QuitSubSystem( SDL_INIT_VIDEO );
267 }
268
269 /*****************************************************************************
270  * vout_Destroy: destroy Sys video thread output method
271  *****************************************************************************
272  * Terminate an output method created by vout_SDLCreate
273  *****************************************************************************/
274 static void vout_Destroy( vout_thread_t *p_vout )
275 {
276     free( p_vout->p_sys );
277 }
278
279 /*****************************************************************************
280  * vout_Manage: handle Sys events
281  *****************************************************************************
282  * This function should be called regularly by video output thread. It returns
283  * a non null value if an error occured.
284  *****************************************************************************/
285 static int vout_Manage( vout_thread_t *p_vout )
286 {
287     SDL_Event event;                                            /* SDL event */
288     char *    p_key;
289
290     /* Process events */
291     while( SDL_PollEvent(&event) )
292     {
293         switch( event.type )
294         {
295         case SDL_VIDEORESIZE:                          /* Resizing of window */
296             p_vout->i_width = event.resize.w;
297             p_vout->i_height = event.resize.h;
298             p_vout->i_changes |= VOUT_SIZE_CHANGE;
299             break;
300
301         case SDL_MOUSEMOTION:
302             if( p_vout->p_sys->b_cursor &&
303                 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
304             {
305                 if( p_vout->p_sys->b_cursor_autohidden )
306                 {
307                     p_vout->p_sys->b_cursor_autohidden = 0;
308                     SDL_ShowCursor( 1 );
309                 }
310                 else
311                 {
312                     p_vout->p_sys->i_lastmoved = mdate();
313                 }
314             }
315             break;
316
317         case SDL_MOUSEBUTTONUP:
318             switch( event.button.button )
319             {
320             case SDL_BUTTON_RIGHT:
321                 p_main->p_intf->b_menu_change = 1;
322                 break;
323             }
324             break;
325
326         case SDL_MOUSEBUTTONDOWN:
327             switch( event.button.button )
328             {
329             case SDL_BUTTON_MIDDLE:
330                 p_vout->i_changes |= VOUT_CURSOR_CHANGE;
331                 break;
332             }
333             break;
334
335         case SDL_QUIT:
336             intf_ProcessKey( p_main->p_intf, SDLK_q );
337             break;
338
339         case SDL_KEYDOWN:                             /* if a key is pressed */
340
341             switch( event.key.keysym.sym )
342             {
343             case SDLK_f:                             /* switch to fullscreen */
344                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
345                 break;
346
347             case SDLK_y:                               /* switch to hard YUV */
348                 p_vout->i_changes |= VOUT_YUV_CHANGE;
349                 break;
350
351             case SDLK_c:                                 /* toggle grayscale */
352                 p_vout->b_grayscale = ! p_vout->b_grayscale;
353                 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
354                 break;
355
356             case SDLK_i:                                      /* toggle info */
357                 p_vout->b_info = ! p_vout->b_info;
358                 p_vout->i_changes |= VOUT_INFO_CHANGE;
359                 break;
360
361             case SDLK_s:                                   /* toggle scaling */
362                 p_vout->b_scale = ! p_vout->b_scale;
363                 p_vout->i_changes |= VOUT_SCALE_CHANGE;
364                 break;
365
366             case SDLK_SPACE:                             /* toggle interface */
367                 p_vout->b_interface = ! p_vout->b_interface;
368                 p_vout->i_changes |= VOUT_INTF_CHANGE;
369                 break;
370             
371             case SDLK_F10:
372                 network_ChannelJoin( 0 );
373                 break;
374             case SDLK_F1:
375                 network_ChannelJoin( 1 );
376                 break;
377             case SDLK_F2:
378                 network_ChannelJoin( 2 );
379                 break;
380             case SDLK_F3:
381                 network_ChannelJoin( 3 );
382                 break;
383             case SDLK_F4:
384                 network_ChannelJoin( 4 );
385                 break;
386             case SDLK_F5:
387                 network_ChannelJoin( 5 );
388                 break;
389             case SDLK_F6:
390                 network_ChannelJoin( 6 );
391                 break;
392             case SDLK_F7:
393                 network_ChannelJoin( 7 );
394                 break;
395             case SDLK_F8:
396                 network_ChannelJoin( 8 );
397                 break;
398             case SDLK_F9:
399                 network_ChannelJoin( 9 );
400                 break;
401
402             case SDLK_MENU:
403                 p_main->p_intf->b_menu_change = 1;
404                 break;
405                 
406             default:
407                 p_key = SDL_GetKeyName( event.key.keysym.sym ) ;
408                 if( intf_ProcessKey( p_main->p_intf, 
409                                      (char )event.key.keysym.sym ) )
410                 {
411                    intf_DbgMsg( "unhandled key '%c' (%i)", 
412                                 (char)event.key.keysym.sym, 
413                                 event.key.keysym.sym );                
414                 }
415                 break;
416             }
417             break;
418
419         default:
420             break;
421         }
422     }
423
424     /*
425      * Size Change 
426      */
427     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
428     {
429         p_vout->p_sys->i_width = p_vout->i_width;
430         p_vout->p_sys->i_height = p_vout->i_height;
431
432         /* Need to reopen display */
433         SDLCloseDisplay( p_vout );
434         if( SDLOpenDisplay( p_vout ) )
435         {
436           intf_ErrMsg( "vout error: can't reopen display after resize" );
437           return( 1 );
438         }
439         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
440     }
441     
442     /*
443      * YUV Change 
444      */
445     if( p_vout->i_changes & VOUT_YUV_CHANGE )
446     {
447         p_vout->b_need_render = ! p_vout->b_need_render;
448         
449         /* Need to reopen display */
450         SDLCloseDisplay( p_vout );
451         if( SDLOpenDisplay( p_vout ) )
452         {
453           intf_ErrMsg( "error: can't reopen display after YUV change" );
454           return( 1 );
455         }
456         p_vout->i_changes &= ~VOUT_YUV_CHANGE;
457     }
458
459     /*
460      * Fullscreen change
461      */
462     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
463     {
464         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
465
466         SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
467
468         p_vout->p_sys->b_cursor_autohidden = 0;
469         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
470                         ! p_vout->p_sys->b_cursor_autohidden );
471
472         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
473     }
474
475     /*
476      * Pointer change
477      */
478     if( ! p_vout->p_sys->b_cursor_autohidden &&
479         ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
480     {
481         /* Hide the mouse automatically */
482         p_vout->p_sys->b_cursor_autohidden = 1;
483         SDL_ShowCursor( 0 );
484     }
485
486     if( p_vout->i_changes & VOUT_CURSOR_CHANGE )
487     {
488         p_vout->p_sys->b_cursor = ! p_vout->p_sys->b_cursor;
489
490         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
491                         ! p_vout->p_sys->b_cursor_autohidden );
492
493         p_vout->i_changes &= ~VOUT_CURSOR_CHANGE;
494     }
495     
496     return( 0 );
497 }
498
499 /*****************************************************************************
500  * vout_SetPalette: sets an 8 bpp palette
501  *****************************************************************************
502  * This function sets the palette given as an argument. It does not return
503  * anything, but could later send information on which colors it was unable
504  * to set.
505  *****************************************************************************/
506 static void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
507                          u16 *blue, u16 *transp)
508 {
509      /* Create a display surface with a grayscale palette */
510     SDL_Color colors[256];
511     int i;
512   
513     /* Fill colors with color information */
514     for( i = 0; i < 256; i++ )
515     {
516         colors[ i ].r = red[ i ] >> 8;
517         colors[ i ].g = green[ i ] >> 8;
518         colors[ i ].b = blue[ i ] >> 8;
519     }
520     
521     /* Set palette */
522     if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
523     {
524         intf_ErrMsg( "vout error: failed setting palette" );
525     }
526
527 }
528
529 /*****************************************************************************
530  * vout_Display: displays previously rendered output
531  *****************************************************************************
532  * This function send the currently rendered image to the display, wait until
533  * it is displayed and switch the two rendering buffer, preparing next frame.
534  *****************************************************************************/
535 static void vout_Display( vout_thread_t *p_vout )
536 {
537     SDL_Rect    disp;
538     if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
539     {
540         if( p_vout->b_need_render )
541         {  
542             /* Change display frame */
543             SDL_Flip( p_vout->p_sys->p_display );
544         }
545         else
546         {
547             /*
548              * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
549              * render 
550              */
551             /* TODO: support for streams other than 4:2:0 */
552             /* create the overlay if necessary */
553             if( p_vout->p_sys->p_overlay == NULL )
554             {
555                 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay( 
556                                              p_vout->p_rendered_pic->i_width, 
557                                              p_vout->p_rendered_pic->i_height,
558                                              SDL_YV12_OVERLAY, 
559                                              p_vout->p_sys->p_display
560                                            );
561                 intf_WarnMsg( 2, "vout: YUV acceleration %s",
562                               p_vout->p_sys->p_overlay->hw_overlay
563                             ? "activated" : "unavailable !" ); 
564             }
565
566             SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
567             /* copy the data into video buffers */
568             /* Y first */
569             memcpy(p_vout->p_sys->p_overlay->pixels[0],
570                    p_vout->p_rendered_pic->p_y,
571                    p_vout->p_sys->p_overlay->h *
572                    p_vout->p_sys->p_overlay->pitches[0]);
573             /* then V */
574             memcpy(p_vout->p_sys->p_overlay->pixels[1],
575                    p_vout->p_rendered_pic->p_v,
576                    p_vout->p_sys->p_overlay->h *
577                    p_vout->p_sys->p_overlay->pitches[1] / 2);
578             /* and U */
579             memcpy(p_vout->p_sys->p_overlay->pixels[2],
580                    p_vout->p_rendered_pic->p_u,
581                    p_vout->p_sys->p_overlay->h *
582                    p_vout->p_sys->p_overlay->pitches[2] / 2);
583
584             disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
585             disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
586             disp.x = (p_vout->i_width - disp.w)/2;
587             disp.y = (p_vout->i_height - disp.h)/2;
588
589             SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
590             SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
591         }
592     }
593 }
594
595 /* following functions are local */
596
597 /*****************************************************************************
598  * SDLOpenDisplay: open and initialize SDL device
599  *****************************************************************************
600  * Open and initialize display according to preferences specified in the vout
601  * thread fields.
602  *****************************************************************************/
603 static int SDLOpenDisplay( vout_thread_t *p_vout )
604 {
605     SDL_Rect    clipping_rect;
606     Uint32      flags;
607     int bpp;
608     /* Open display 
609      * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
610      */
611
612     /* init flags and cursor */
613     flags = SDL_ANYFORMAT | SDL_HWPALETTE;
614
615     if( p_vout->b_fullscreen )
616     {
617         flags |= SDL_FULLSCREEN;
618     }
619     else
620     {
621         flags |= SDL_RESIZABLE;
622     }
623
624     if( p_vout->b_need_render )
625     {
626         flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
627     }
628     else
629     {
630         flags |= SDL_SWSURFACE; /* save video memory */
631     }
632
633     bpp = SDL_VideoModeOK( p_vout->p_sys->i_width,
634                            p_vout->p_sys->i_height,
635                            p_vout->i_screen_depth, flags );
636
637     if( bpp == 0 )
638     {
639         intf_ErrMsg( "vout error: no video mode available" );
640         return( 1 );
641     }
642
643     p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
644                                                 p_vout->p_sys->i_height,
645                                                 bpp, flags);
646
647     if( p_vout->p_sys->p_display == NULL )
648     {
649         intf_ErrMsg( "vout error: cannot set video mode" );
650         return( 1 );
651     }
652
653     SDL_LockSurface( p_vout->p_sys->p_display );
654
655     SDL_WM_SetCaption( VOUT_TITLE " (SDL output)",
656                        VOUT_TITLE " (SDL output)" );
657     SDL_EventState(SDL_KEYUP , SDL_IGNORE);                /* ignore keys up */
658
659     if( p_vout->b_need_render )
660     {
661         p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
662         SDL_Flip(p_vout->p_sys->p_display);
663         p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
664         SDL_Flip(p_vout->p_sys->p_display);
665
666         /* Set clipping for text */
667         clipping_rect.x = 0;
668         clipping_rect.y = 0;
669         clipping_rect.w = p_vout->p_sys->p_display->w;
670         clipping_rect.h = p_vout->p_sys->p_display->h;
671         SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
672
673         /* Set thread information */
674         p_vout->i_width =           p_vout->p_sys->p_display->w;
675         p_vout->i_height =          p_vout->p_sys->p_display->h;
676         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
677
678         p_vout->i_screen_depth =
679             p_vout->p_sys->p_display->format->BitsPerPixel;
680         p_vout->i_bytes_per_pixel =
681             p_vout->p_sys->p_display->format->BytesPerPixel;
682
683         p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
684         p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
685         p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;
686
687         /* FIXME: palette in 8bpp ?? */
688         /* Set and initialize buffers */
689         p_vout->pf_setbuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
690                                        p_vout->p_sys->p_sdl_buf[ 1 ] );
691     }
692     else
693     {
694         p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
695         p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
696
697         /* Set thread information */
698         p_vout->i_width =           p_vout->p_sys->p_display->w;
699         p_vout->i_height =          p_vout->p_sys->p_display->h;
700         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
701
702         p_vout->pf_setbuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
703                                        p_vout->p_sys->p_sdl_buf[ 1 ] );
704     }
705
706     p_vout->p_sys->b_reopen_display = 0;
707
708     return( 0 );
709 }
710
711 /*****************************************************************************
712  * SDLCloseDisplay: close and reset SDL device
713  *****************************************************************************
714  * This function returns all resources allocated by SDLOpenDisplay and restore
715  * the original state of the device.
716  *****************************************************************************/
717 static void SDLCloseDisplay( vout_thread_t *p_vout )
718 {
719     if( p_vout->p_sys->p_display != NULL )
720     {
721         if( p_vout->p_sys->p_overlay != NULL )
722         {            
723             SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
724             p_vout->p_sys->p_overlay = NULL;
725         }
726
727         SDL_UnlockSurface ( p_vout->p_sys->p_display );
728         SDL_FreeSurface( p_vout->p_sys->p_display );
729         p_vout->p_sys->p_display = NULL;
730     }
731 }
732