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