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