]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
* Fixed close button support in SDL
[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:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include "defs.h"
27
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                                /* free() */
30 #include <string.h>                                            /* strerror() */
31
32 #include <SDL/SDL.h>
33
34 #include "config.h"
35 #include "common.h"
36 #include "threads.h"
37 #include "mtime.h"
38 #include "plugins.h"
39
40 #include "video.h"
41 #include "video_output.h"
42
43 #include "intf_msg.h"
44 #include "main.h"
45
46 /*****************************************************************************
47  * vout_sys_t: video output SDL method descriptor
48  *****************************************************************************
49  * This structure is part of the video output thread descriptor.
50  * It describes the SDL specific properties of an output thread.
51  *****************************************************************************/
52 typedef struct vout_sys_s
53 {
54     int i_width;
55     int i_height;
56     SDL_Surface *   p_display;                             /* display device */
57     SDL_Overlay *   p_overlay;                             /* overlay device */
58     boolean_t   b_fullscreen;
59     boolean_t   b_reopen_display;
60     boolean_t   b_toggle_fullscreen;
61     Uint8   *   p_buffer[2];
62                                                      /* Buffers informations */
63 }   vout_sys_t;
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int     SDLOpenDisplay       ( vout_thread_t *p_vout );
69 static void    SDLCloseDisplay      ( vout_thread_t *p_vout );
70 static void    SDLToggleFullScreen  ( vout_thread_t *p_vout );
71
72 /*****************************************************************************
73  * vout_SDLCreate: allocate SDL video thread output method
74  *****************************************************************************
75  * This function allocate and initialize a SDL vout method. It uses some of the
76  * vout properties to choose the correct mode, and change them according to the
77  * mode actually used.
78  *****************************************************************************/
79 int vout_SDLCreate( vout_thread_t *p_vout, char *psz_display,
80                     int i_root_window, void *p_data )
81 {
82     /* Allocate structure */
83     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
84     if( p_vout->p_sys == NULL )
85     {
86         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
87         return( 1 );
88     }
89
90     p_vout->p_sys->p_display = NULL;
91     p_vout->p_sys->p_overlay = NULL;
92
93     /* Initialize library */
94     if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) < 0 )
95     {
96         intf_ErrMsg( "error: can't initialize SDL library: %s",
97                      SDL_GetError() );
98         free( p_vout->p_sys );
99         return( 1 );
100     }
101
102     /* Force the software yuv even if it is not used */
103     /* If we don't do this, p_vout is not correctly initialized
104        and it's impossible to switch between soft/hard yuv */
105     p_vout->b_need_render = 1;
106
107     if(psz_display != NULL && strcmp(psz_display,"fullscreen")==0)
108     {
109         p_vout->p_sys->b_fullscreen = 1;
110     }
111     else
112     {
113         p_vout->p_sys->b_fullscreen = 0;
114     }
115
116     p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR, 
117                                 VOUT_WIDTH_DEFAULT );
118     p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
119                                 VOUT_HEIGHT_DEFAULT );
120
121     if( SDLOpenDisplay(p_vout) )
122     {
123       intf_ErrMsg( "error: can't initialize SDL library: %s",
124                    SDL_GetError() );
125       return( 1 );
126     }
127
128     p_vout->p_sys->b_toggle_fullscreen = 0;
129
130     return( 0 );
131 }
132
133 /*****************************************************************************
134  * vout_SDLInit: initialize SDL video thread output method
135  *****************************************************************************
136  * This function initialize the SDL display device.
137  *****************************************************************************/
138 int vout_SDLInit( vout_thread_t *p_vout )
139 {
140     return( 0 );
141 }
142
143 /*****************************************************************************
144  * vout_SDLEnd: terminate Sys video thread output method
145  *****************************************************************************
146  * Terminate an output method created by vout_SDLCreate
147  *****************************************************************************/
148 void vout_SDLEnd( vout_thread_t *p_vout )
149 {
150     SDLCloseDisplay( p_vout );
151     SDL_Quit();
152 }
153
154 /*****************************************************************************
155  * vout_SDLDestroy: destroy Sys video thread output method
156  *****************************************************************************
157  * Terminate an output method created by vout_SDLCreate
158  *****************************************************************************/
159 void vout_SDLDestroy( vout_thread_t *p_vout )
160 {
161     free( p_vout->p_sys );
162 }
163
164 /*****************************************************************************
165  * vout_SDLManage: handle Sys events
166  *****************************************************************************
167  * This function should be called regularly by video output thread. It returns
168  * a non null value if an error occured.
169  *****************************************************************************/
170 int vout_SDLManage( vout_thread_t *p_vout )
171 {
172     /* If the display has to be reopened we do so */
173     if( p_vout->p_sys->b_reopen_display )
174     {
175         SDLCloseDisplay(p_vout);
176
177         if( SDLOpenDisplay(p_vout) )
178         {
179             intf_ErrMsg( "error: can't open DISPLAY default display" );
180             return( 1 );
181         }
182     }
183
184     /* if fullscreen has to be toggled we do so */
185     if( p_vout->p_sys->b_toggle_fullscreen )
186     {
187         SDLToggleFullScreen(p_vout);
188     }
189
190     return( 0 );
191 }
192
193 /*****************************************************************************
194  * vout_SDLSetPalette: sets an 8 bpp palette
195  *****************************************************************************
196  * This function sets the palette given as an argument. It does not return
197  * anything, but could later send information on which colors it was unable
198  * to set.
199  *****************************************************************************/
200 void vout_SDLSetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green, u16 *blue, u16 *transp)
201 {
202      /* Create a display surface with a grayscale palette */
203     SDL_Color colors[256];
204     int i;
205   
206     /* Fill colors with color information */
207     for( i = 0; i < 256; i++ )
208     {
209         colors[ i ].r = red[ i ] >> 8;
210         colors[ i ].g = green[ i ] >> 8;
211         colors[ i ].b = blue[ i ] >> 8;
212     }
213     
214     /* Set palette */
215     if( SDL_SetColors(p_vout->p_sys->p_display, colors, 0, 256) == 0 )
216     {
217         intf_ErrMsg( "vout error: failed setting palette\n" );
218     }
219
220 }
221
222 /*****************************************************************************
223  * vout_SDLDisplay: displays previously rendered output
224  *****************************************************************************
225  * This function send the currently rendered image to the display, wait until
226  * it is displayed and switch the two rendering buffer, preparing next frame.
227  *****************************************************************************/
228 void vout_SDLDisplay( vout_thread_t *p_vout )
229 {
230     SDL_Rect    disp;
231     if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
232     {
233         if(p_vout->b_need_render)
234         {  
235             /* Change display frame */
236             SDL_Flip( p_vout->p_sys->p_display );
237         }
238         else
239         {
240         
241             /*
242              * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
243              * render 
244              */
245             /* TODO: support for streams other than 4:2:0 */
246             /* create the overlay if necessary */
247             if( p_vout->p_sys->p_overlay == NULL )
248             {
249                 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay( 
250                                              p_vout->p_rendered_pic->i_width, 
251                                              p_vout->p_rendered_pic->i_height,
252                                              SDL_YV12_OVERLAY, 
253                                              p_vout->p_sys->p_display
254                                            );
255                 intf_Msg("vout: YUV acceleration set to %d,",
256                             p_vout->p_sys->p_overlay->hw_overlay); 
257             }
258
259             SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
260             /* copy the data into video buffers */
261             /* Y first */
262             memcpy(p_vout->p_sys->p_overlay->pixels[0],
263                    p_vout->p_rendered_pic->p_y,
264                    p_vout->p_sys->p_overlay->h *
265                    p_vout->p_sys->p_overlay->pitches[0]);
266             /* then V */
267             memcpy(p_vout->p_sys->p_overlay->pixels[1],
268                    p_vout->p_rendered_pic->p_v,
269                    p_vout->p_sys->p_overlay->h *
270                    p_vout->p_sys->p_overlay->pitches[1] / 2);
271             /* and U */
272             memcpy(p_vout->p_sys->p_overlay->pixels[2],
273                    p_vout->p_rendered_pic->p_u,
274                    p_vout->p_sys->p_overlay->h *
275                    p_vout->p_sys->p_overlay->pitches[2] / 2);
276
277             disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
278             disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
279             disp.x = (p_vout->i_width - disp.w)/2;
280             disp.y = (p_vout->i_height - disp.h)/2;
281
282             SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
283             SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
284         }
285     }
286 }
287
288 /* following functions are local */
289
290 /*****************************************************************************
291  * SDLOpenDisplay: open and initialize SDL device
292  *****************************************************************************
293  * Open and initialize display according to preferences specified in the vout
294  * thread fields.
295  *****************************************************************************/
296 static int SDLOpenDisplay( vout_thread_t *p_vout )
297 {
298     SDL_Rect    clipping_rect;
299     Uint32      flags;
300     int bpp;
301     /* Open display 
302      * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
303      */
304
305     /* init flags and cursor */
306     flags = SDL_ANYFORMAT | SDL_HWPALETTE;
307
308     if( p_vout->p_sys->b_fullscreen )
309         flags |= SDL_FULLSCREEN;
310     else
311         flags |= SDL_RESIZABLE;
312
313     if( p_vout->b_need_render )
314         flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
315     else
316         flags |= SDL_SWSURFACE; /* save video memory */
317
318     bpp = SDL_VideoModeOK(p_vout->p_sys->i_width,
319                           p_vout->p_sys->i_height,
320                           p_vout->i_screen_depth, flags);
321
322     if(bpp == 0)
323     {
324         intf_ErrMsg( "error: can't open DISPLAY default display" );
325         return( 1 );
326     }
327
328     p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
329                                                 p_vout->p_sys->i_height,
330                                                 bpp, flags);
331
332     if( p_vout->p_sys->p_display == NULL )
333     {
334         intf_ErrMsg( "error: can't open DISPLAY default display" );
335         return( 1 );
336     }
337
338     SDL_LockSurface(p_vout->p_sys->p_display);
339
340     if( p_vout->p_sys->b_fullscreen )
341         SDL_ShowCursor( 0 );
342     else
343         SDL_ShowCursor( 1 );
344
345     SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
346     SDL_EventState(SDL_KEYUP , SDL_IGNORE); /* ignore keys up */
347
348     if( p_vout->b_need_render )
349     {
350         p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
351         SDL_Flip(p_vout->p_sys->p_display);
352         p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
353         SDL_Flip(p_vout->p_sys->p_display);
354
355         /* Set clipping for text */
356         clipping_rect.x = 0;
357         clipping_rect.y = 0;
358         clipping_rect.w = p_vout->p_sys->p_display->w;
359         clipping_rect.h = p_vout->p_sys->p_display->h;
360         SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
361
362         /* Set thread information */
363         p_vout->i_width =           p_vout->p_sys->p_display->w;
364         p_vout->i_height =          p_vout->p_sys->p_display->h;
365         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
366
367         p_vout->i_screen_depth =
368             p_vout->p_sys->p_display->format->BitsPerPixel;
369         p_vout->i_bytes_per_pixel =
370             p_vout->p_sys->p_display->format->BytesPerPixel;
371
372         p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
373         p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
374         p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;
375
376         /* FIXME: palette in 8bpp ?? */
377         /* Set and initialize buffers */
378         vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
379                                  p_vout->p_sys->p_buffer[ 1 ] );
380     }
381     else
382     {
383         p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
384         p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
385
386         /* Set thread information */
387         p_vout->i_width =           p_vout->p_sys->p_display->w;
388         p_vout->i_height =          p_vout->p_sys->p_display->h;
389         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
390
391         vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
392                                  p_vout->p_sys->p_buffer[ 1 ] );
393     }
394
395     p_vout->i_changes |= VOUT_YUV_CHANGE;
396
397     p_vout->p_sys->b_reopen_display = 0;
398
399     return( 0 );
400 }
401
402 /*****************************************************************************
403  * SDLCloseDisplay: close and reset SDL device
404  *****************************************************************************
405  * This function returns all resources allocated by SDLOpenDisplay and restore
406  * the original state of the device.
407  *****************************************************************************/
408 static void SDLCloseDisplay( vout_thread_t *p_vout )
409 {
410     if( p_vout->p_sys->p_display != NULL )
411     {
412         if( p_vout->p_sys->p_overlay != NULL )
413         {            
414             SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
415             p_vout->p_sys->p_overlay = NULL;
416         }
417         SDL_UnlockSurface ( p_vout->p_sys->p_display );
418         SDL_FreeSurface( p_vout->p_sys->p_display );
419         p_vout->p_sys->p_display = NULL;
420     }
421 }
422
423 /*****************************************************************************
424  * SDLToggleFullScreen: toggle fullscreen
425  *****************************************************************************
426  * This function toggles the fullscreen state of the surface.
427  *****************************************************************************/
428 static void SDLToggleFullScreen( vout_thread_t *p_vout )
429 {
430     SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
431
432     if( p_vout->p_sys->b_fullscreen )
433         SDL_ShowCursor( 0 );
434     else
435         SDL_ShowCursor( 1 );
436
437     p_vout->p_sys->b_toggle_fullscreen = 0;
438 }
439