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