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