]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
. Fixed a FIXME in SDL: p_vout->p_sys is no more used in intf_sdl.c
[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 /* FIXME: SOME CLUELESS MORON DEFINED THIS STRUCTURE IN INTF_SDL.C AS WELL   */
53 typedef struct vout_sys_s
54 {
55     int i_width;
56     int i_height;
57     SDL_Surface *   p_display;                             /* display device */
58     SDL_Overlay *   p_overlay;                             /* overlay device */
59     boolean_t   b_fullscreen;
60     boolean_t   b_overlay;
61     boolean_t   b_cursor;
62     boolean_t   b_reopen_display;
63     Uint8   *   p_buffer[2];
64                                                      /* Buffers informations */
65 }   vout_sys_t;
66
67 /*****************************************************************************
68  * Local prototypes
69  *****************************************************************************/
70 static int     SDLOpenDisplay       ( vout_thread_t *p_vout );
71 static void    SDLCloseDisplay      ( vout_thread_t *p_vout );
72 static void    SDLToggleFullScreen  ( vout_thread_t *p_vout );
73 static void    SDLTogglePointer     ( vout_thread_t *p_vout );
74 /*****************************************************************************
75  * vout_SDLCreate: allocate SDL video thread output method
76  *****************************************************************************
77  * This function allocate and initialize a SDL vout method. It uses some of the
78  * vout properties to choose the correct mode, and change them according to the
79  * mode actually used.
80  *****************************************************************************/
81 int vout_SDLCreate( vout_thread_t *p_vout, char *psz_display,
82                     int i_root_window, void *p_data )
83 {
84     /* Allocate structure */
85     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
86     if( p_vout->p_sys == NULL )
87     {
88         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
89         return( 1 );
90     }
91
92     p_vout->p_sys->p_display = NULL;
93     p_vout->p_sys->p_overlay = NULL;
94
95     /* Initialize library */
96     if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE)
97             < 0 )
98     {
99         intf_ErrMsg( "error: can't initialize SDL library: %s",
100                      SDL_GetError() );
101         free( p_vout->p_sys );
102         return( 1 );
103     }
104
105     /* Force the software yuv even if it is not used */
106     /* If we don't do this, p_vout is not correctly initialized
107        and it's impossible to switch between soft/hard yuv */
108     p_vout->b_need_render = 1;
109
110     p_vout->p_sys->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
111                                 VOUT_FULLSCREEN_DEFAULT );
112     p_vout->p_sys->b_overlay = main_GetIntVariable( VOUT_OVERLAY_VAR,
113                                 VOUT_OVERLAY_DEFAULT );
114     p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR, 
115                                 VOUT_WIDTH_DEFAULT );
116     p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
117                                 VOUT_HEIGHT_DEFAULT );
118
119     p_vout->p_sys->b_cursor = 0 ;   // TODO should be done with a main_GetInt..
120     
121     if( SDLOpenDisplay(p_vout) )
122     {
123       intf_ErrMsg( "error: can't initialize SDL library: %s",
124                    SDL_GetError() );
125       free( p_vout->p_sys );
126       return( 1 );
127     }
128
129     return( 0 );
130 }
131
132 /*****************************************************************************
133  * vout_SDLInit: initialize SDL video thread output method
134  *****************************************************************************
135  * This function initialize the SDL display device.
136  *****************************************************************************/
137 int vout_SDLInit( vout_thread_t *p_vout )
138 {
139     return( 0 );
140 }
141
142 /*****************************************************************************
143  * vout_SDLEnd: terminate Sys video thread output method
144  *****************************************************************************
145  * Terminate an output method created by vout_SDLCreate
146  *****************************************************************************/
147 void vout_SDLEnd( vout_thread_t *p_vout )
148 {
149     SDLCloseDisplay( p_vout );
150     SDL_Quit();
151 }
152
153 /*****************************************************************************
154  * vout_SDLDestroy: destroy Sys video thread output method
155  *****************************************************************************
156  * Terminate an output method created by vout_SDLCreate
157  *****************************************************************************/
158 void vout_SDLDestroy( vout_thread_t *p_vout )
159 {
160     free( p_vout->p_sys );
161 }
162
163 /*****************************************************************************
164  * vout_SDLManage: handle Sys events
165  *****************************************************************************
166  * This function should be called regularly by video output thread. It returns
167  * a non null value if an error occured.
168  *****************************************************************************/
169 int vout_SDLManage( vout_thread_t *p_vout )
170 {
171
172     /*
173      * Size Change 
174      */
175     if ( p_vout->i_changes & VOUT_SIZE_CHANGE )
176     {
177         p_vout->p_sys->i_width = p_vout->i_width;
178         p_vout->p_sys->i_height = p_vout->i_height;
179
180         /* Need to reopen display */
181         SDLCloseDisplay( p_vout );
182         if ( SDLOpenDisplay( p_vout ) )
183         {
184           intf_ErrMsg( "error: can't open DISPLAY default display" );
185           return( 1 );
186         }
187         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
188     }
189     
190     /*
191      * YUV Change 
192      */
193     if ( p_vout->i_changes & VOUT_YUV_CHANGE )
194     {
195         p_vout->b_need_render = 1 - p_vout->b_need_render;
196         
197         /* Need to reopen display */
198         SDLCloseDisplay( p_vout );
199         if ( SDLOpenDisplay( p_vout ) )
200         {
201           intf_ErrMsg( "error: can't open DISPLAY default display" );
202           return( 1 );
203         }
204         p_vout->i_changes &= ~VOUT_YUV_CHANGE;
205     }
206     
207     /*
208      * Fullscreen change
209      */
210     if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
211     {
212         p_vout->p_sys->b_fullscreen = 1 - p_vout->p_sys->b_fullscreen;
213         SDLToggleFullScreen( p_vout );
214         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
215     }
216
217     
218
219     /*
220      * Pointer change
221      */
222     if ( p_vout->i_changes & VOUT_CURSOR_CHANGE )
223     {
224         p_vout->p_sys->b_cursor = 1 - p_vout->p_sys->b_cursor;
225         SDLTogglePointer( p_vout );
226     }
227     
228     return( 0 );
229 }
230
231 /*****************************************************************************
232  * vout_SDLSetPalette: sets an 8 bpp palette
233  *****************************************************************************
234  * This function sets the palette given as an argument. It does not return
235  * anything, but could later send information on which colors it was unable
236  * to set.
237  *****************************************************************************/
238 void vout_SDLSetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
239                          u16 *blue, u16 *transp)
240 {
241      /* Create a display surface with a grayscale palette */
242     SDL_Color colors[256];
243     int i;
244   
245     /* Fill colors with color information */
246     for( i = 0; i < 256; i++ )
247     {
248         colors[ i ].r = red[ i ] >> 8;
249         colors[ i ].g = green[ i ] >> 8;
250         colors[ i ].b = blue[ i ] >> 8;
251     }
252     
253     /* Set palette */
254     if( SDL_SetColors(p_vout->p_sys->p_display, colors, 0, 256) == 0 )
255     {
256         intf_ErrMsg( "vout error: failed setting palette\n" );
257     }
258
259 }
260
261 /*****************************************************************************
262  * vout_SDLDisplay: displays previously rendered output
263  *****************************************************************************
264  * This function send the currently rendered image to the display, wait until
265  * it is displayed and switch the two rendering buffer, preparing next frame.
266  *****************************************************************************/
267 void vout_SDLDisplay( vout_thread_t *p_vout )
268 {
269     SDL_Rect    disp;
270     if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
271     {
272         if(p_vout->b_need_render)
273         {  
274             /* Change display frame */
275             SDL_Flip( p_vout->p_sys->p_display );
276         }
277         else
278         {
279         
280             /*
281              * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
282              * render 
283              */
284             /* TODO: support for streams other than 4:2:0 */
285             /* create the overlay if necessary */
286             if( p_vout->p_sys->p_overlay == NULL )
287             {
288                 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay( 
289                                              p_vout->p_rendered_pic->i_width, 
290                                              p_vout->p_rendered_pic->i_height,
291                                              SDL_YV12_OVERLAY, 
292                                              p_vout->p_sys->p_display
293                                            );
294                 intf_Msg("vout: YUV acceleration %s",
295                             p_vout->p_sys->p_overlay->hw_overlay
296                             ? "activated" : "unavailable !" ); 
297             }
298
299             SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
300             /* copy the data into video buffers */
301             /* Y first */
302             memcpy(p_vout->p_sys->p_overlay->pixels[0],
303                    p_vout->p_rendered_pic->p_y,
304                    p_vout->p_sys->p_overlay->h *
305                    p_vout->p_sys->p_overlay->pitches[0]);
306             /* then V */
307             memcpy(p_vout->p_sys->p_overlay->pixels[1],
308                    p_vout->p_rendered_pic->p_v,
309                    p_vout->p_sys->p_overlay->h *
310                    p_vout->p_sys->p_overlay->pitches[1] / 2);
311             /* and U */
312             memcpy(p_vout->p_sys->p_overlay->pixels[2],
313                    p_vout->p_rendered_pic->p_u,
314                    p_vout->p_sys->p_overlay->h *
315                    p_vout->p_sys->p_overlay->pitches[2] / 2);
316
317             disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
318             disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
319             disp.x = (p_vout->i_width - disp.w)/2;
320             disp.y = (p_vout->i_height - disp.h)/2;
321
322             SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
323             SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
324         }
325     }
326 }
327
328 /* following functions are local */
329
330 /*****************************************************************************
331  * SDLOpenDisplay: open and initialize SDL device
332  *****************************************************************************
333  * Open and initialize display according to preferences specified in the vout
334  * thread fields.
335  *****************************************************************************/
336 static int SDLOpenDisplay( vout_thread_t *p_vout )
337 {
338     SDL_Rect    clipping_rect;
339     Uint32      flags;
340     int bpp;
341     /* Open display 
342      * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
343      */
344
345     /* init flags and cursor */
346     flags = SDL_ANYFORMAT | SDL_HWPALETTE;
347
348     if( p_vout->p_sys->b_fullscreen )
349         flags |= SDL_FULLSCREEN;
350     else
351         flags |= SDL_RESIZABLE;
352
353     if( p_vout->b_need_render )
354         flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
355     else
356         flags |= SDL_SWSURFACE; /* save video memory */
357
358     bpp = SDL_VideoModeOK(p_vout->p_sys->i_width,
359                           p_vout->p_sys->i_height,
360                           p_vout->i_screen_depth, flags);
361
362     if(bpp == 0)
363     {
364         intf_ErrMsg( "error: can't open DISPLAY default display" );
365         return( 1 );
366     }
367
368     p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
369                                                 p_vout->p_sys->i_height,
370                                                 bpp, flags);
371
372     if( p_vout->p_sys->p_display == NULL )
373     {
374         intf_ErrMsg( "error: can't open DISPLAY default display" );
375         return( 1 );
376     }
377
378     SDL_LockSurface(p_vout->p_sys->p_display);
379
380     if( p_vout->p_sys->b_fullscreen )
381         SDL_ShowCursor( 0 );
382     else
383         SDL_ShowCursor( 1 );
384
385     SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
386     SDL_EventState(SDL_KEYUP , SDL_IGNORE);                 /* ignore keys up */
387     SDL_EventState(SDL_MOUSEBUTTONUP, SDL_IGNORE);          
388     if( p_vout->b_need_render )
389     {
390         p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
391         SDL_Flip(p_vout->p_sys->p_display);
392         p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
393         SDL_Flip(p_vout->p_sys->p_display);
394
395         /* Set clipping for text */
396         clipping_rect.x = 0;
397         clipping_rect.y = 0;
398         clipping_rect.w = p_vout->p_sys->p_display->w;
399         clipping_rect.h = p_vout->p_sys->p_display->h;
400         SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
401
402         /* Set thread information */
403         p_vout->i_width =           p_vout->p_sys->p_display->w;
404         p_vout->i_height =          p_vout->p_sys->p_display->h;
405         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
406
407         p_vout->i_screen_depth =
408             p_vout->p_sys->p_display->format->BitsPerPixel;
409         p_vout->i_bytes_per_pixel =
410             p_vout->p_sys->p_display->format->BytesPerPixel;
411
412         p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
413         p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
414         p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;
415
416         /* FIXME: palette in 8bpp ?? */
417         /* Set and initialize buffers */
418         vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
419                                  p_vout->p_sys->p_buffer[ 1 ] );
420     }
421     else
422     {
423         p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
424         p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
425
426         /* Set thread information */
427         p_vout->i_width =           p_vout->p_sys->p_display->w;
428         p_vout->i_height =          p_vout->p_sys->p_display->h;
429         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
430
431         vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
432                                  p_vout->p_sys->p_buffer[ 1 ] );
433     }
434
435     p_vout->p_sys->b_reopen_display = 0;
436
437     return( 0 );
438 }
439
440 /*****************************************************************************
441  * SDLCloseDisplay: close and reset SDL device
442  *****************************************************************************
443  * This function returns all resources allocated by SDLOpenDisplay and restore
444  * the original state of the device.
445  *****************************************************************************/
446 static void SDLCloseDisplay( vout_thread_t *p_vout )
447 {
448     if( p_vout->p_sys->p_display != NULL )
449     {
450         if( p_vout->p_sys->p_overlay != NULL )
451         {            
452             SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
453             p_vout->p_sys->p_overlay = NULL;
454         }
455         SDL_UnlockSurface ( p_vout->p_sys->p_display );
456         SDL_FreeSurface( p_vout->p_sys->p_display );
457         p_vout->p_sys->p_display = NULL;
458     }
459 }
460
461 /*****************************************************************************
462  * SDLToggleFullScreen: toggle fullscreen
463  *****************************************************************************
464  * This function toggles the fullscreen state of the surface.
465  * And  - hide the pointer if switching to fullscreen
466  *      - show the pointer if leaving fullscreen state
467  *****************************************************************************/
468 static void SDLToggleFullScreen( vout_thread_t *p_vout )
469 {
470     SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
471
472     if( p_vout->p_sys->b_fullscreen )
473     {
474         p_vout->p_sys->b_cursor=1;
475     }
476     else
477     {
478         p_vout->p_sys->b_cursor=0;
479     }
480     
481     SDLTogglePointer( p_vout );
482 }
483
484 /*****************************************************************************
485  * SDLTogglePointer: Hide/Show mouse pointer
486  *****************************************************************************
487  * This function hides/shows the mouse pointer inside the main window.
488  *****************************************************************************/
489 static void SDLTogglePointer( vout_thread_t *p_vout )
490 {
491     if( p_vout->p_sys->b_cursor==1 )
492     {
493         SDL_ShowCursor( 0 );
494     }
495     else
496     {
497         SDL_ShowCursor( 1 );
498     }
499 }