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