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