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