1 /*****************************************************************************
2 * vout_sdl.c: SDL video output display method
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
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.
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.
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 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <stdlib.h> /* free() */
30 #include <string.h> /* strerror() */
41 #include "video_output.h"
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
57 SDL_Surface * p_display; /* display device */
58 SDL_Overlay * p_overlay; /* overlay device */
59 boolean_t b_fullscreen;
62 boolean_t b_reopen_display;
64 /* Buffers informations */
67 /*****************************************************************************
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
80 *****************************************************************************/
81 int vout_SDLCreate( vout_thread_t *p_vout, char *psz_display,
82 int i_root_window, void *p_data )
84 /* Allocate structure */
85 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
86 if( p_vout->p_sys == NULL )
88 intf_ErrMsg( "error: %s", strerror(ENOMEM) );
92 p_vout->p_sys->p_display = NULL;
93 p_vout->p_sys->p_overlay = NULL;
95 /* Initialize library */
96 if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE)
99 intf_ErrMsg( "error: can't initialize SDL library: %s",
101 free( p_vout->p_sys );
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;
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 );
119 p_vout->p_sys->b_cursor = 0 ; // TODO should be done with a main_GetInt..
121 if( SDLOpenDisplay(p_vout) )
123 intf_ErrMsg( "error: can't initialize SDL library: %s",
125 free( p_vout->p_sys );
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 )
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 )
149 SDLCloseDisplay( p_vout );
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 )
160 free( p_vout->p_sys );
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 )
175 if ( p_vout->i_changes & VOUT_SIZE_CHANGE )
177 p_vout->p_sys->i_width = p_vout->i_width;
178 p_vout->p_sys->i_height = p_vout->i_height;
180 /* Need to reopen display */
181 SDLCloseDisplay( p_vout );
182 if ( SDLOpenDisplay( p_vout ) )
184 intf_ErrMsg( "error: can't open DISPLAY default display" );
187 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
193 if ( p_vout->i_changes & VOUT_YUV_CHANGE )
195 p_vout->b_need_render = 1 - p_vout->b_need_render;
197 /* Need to reopen display */
198 SDLCloseDisplay( p_vout );
199 if ( SDLOpenDisplay( p_vout ) )
201 intf_ErrMsg( "error: can't open DISPLAY default display" );
204 p_vout->i_changes &= ~VOUT_YUV_CHANGE;
210 if ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
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;
222 if ( p_vout->i_changes & VOUT_CURSOR_CHANGE )
224 p_vout->p_sys->b_cursor = 1 - p_vout->p_sys->b_cursor;
225 SDLTogglePointer( p_vout );
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
237 *****************************************************************************/
238 void vout_SDLSetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
239 u16 *blue, u16 *transp)
241 /* Create a display surface with a grayscale palette */
242 SDL_Color colors[256];
245 /* Fill colors with color information */
246 for( i = 0; i < 256; i++ )
248 colors[ i ].r = red[ i ] >> 8;
249 colors[ i ].g = green[ i ] >> 8;
250 colors[ i ].b = blue[ i ] >> 8;
254 if( SDL_SetColors(p_vout->p_sys->p_display, colors, 0, 256) == 0 )
256 intf_ErrMsg( "vout error: failed setting palette\n" );
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 )
270 if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
272 if(p_vout->b_need_render)
274 /* Change display frame */
275 SDL_Flip( p_vout->p_sys->p_display );
281 * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
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 )
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,
292 p_vout->p_sys->p_display
294 intf_Msg("vout: YUV acceleration %s",
295 p_vout->p_sys->p_overlay->hw_overlay
296 ? "activated" : "unavailable !" );
299 SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
300 /* copy the data into video buffers */
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]);
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);
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);
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;
322 SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
323 SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
328 /* following functions are local */
330 /*****************************************************************************
331 * SDLOpenDisplay: open and initialize SDL device
332 *****************************************************************************
333 * Open and initialize display according to preferences specified in the vout
335 *****************************************************************************/
336 static int SDLOpenDisplay( vout_thread_t *p_vout )
338 SDL_Rect clipping_rect;
342 * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
345 /* init flags and cursor */
346 flags = SDL_ANYFORMAT | SDL_HWPALETTE;
348 if( p_vout->p_sys->b_fullscreen )
349 flags |= SDL_FULLSCREEN;
351 flags |= SDL_RESIZABLE;
353 if( p_vout->b_need_render )
354 flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
356 flags |= SDL_SWSURFACE; /* save video memory */
358 bpp = SDL_VideoModeOK(p_vout->p_sys->i_width,
359 p_vout->p_sys->i_height,
360 p_vout->i_screen_depth, flags);
364 intf_ErrMsg( "error: can't open DISPLAY default display" );
368 p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
369 p_vout->p_sys->i_height,
372 if( p_vout->p_sys->p_display == NULL )
374 intf_ErrMsg( "error: can't open DISPLAY default display" );
378 SDL_LockSurface(p_vout->p_sys->p_display);
380 if( p_vout->p_sys->b_fullscreen )
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 )
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);
395 /* Set clipping for text */
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);
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;
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;
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;
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 ] );
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;
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;
431 vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
432 p_vout->p_sys->p_buffer[ 1 ] );
435 p_vout->p_sys->b_reopen_display = 0;
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 )
448 if( p_vout->p_sys->p_display != NULL )
450 if( p_vout->p_sys->p_overlay != NULL )
452 SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
453 p_vout->p_sys->p_overlay = NULL;
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;
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 )
470 SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
472 if( p_vout->p_sys->b_fullscreen )
474 p_vout->p_sys->b_cursor=1;
478 p_vout->p_sys->b_cursor=0;
481 SDLTogglePointer( p_vout );
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 )
491 if( p_vout->p_sys->b_cursor==1 )