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