1 /*****************************************************************************
2 * vout_sdl.c: SDL video output display method
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Pierre Baillet <oct@zoy.org>
8 * Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <string.h> /* strerror() */
44 #include "video_output.h"
47 #include "interface.h"
48 /* FIXME: get rid of this */
49 #include "keystrokes.h"
52 /*****************************************************************************
53 * FIXME: this file is ... *
55 * XXX XXX FIXME XXX XXX XXX XXX *
56 * XXX XXX XXX XXX XXX XXX XXX XXX *
57 * XXX XXX XXX XXX FIXME XXX *
58 * XXX XXX XXX TODO XXX XXX XXX *
59 * XXX XXX XXX XXX XXX XXX *
60 * FIXME FIXME FIXME XXX XXX *
62 *****************************************************************************/
64 /*****************************************************************************
65 * vout_sys_t: video output SDL method descriptor
66 *****************************************************************************
67 * This structure is part of the video output thread descriptor.
68 * It describes the SDL specific properties of an output thread.
69 *****************************************************************************/
70 typedef struct vout_sys_s
74 SDL_Surface * p_display; /* display device */
75 SDL_Overlay * p_overlay; /* overlay device */
76 boolean_t b_fullscreen;
79 boolean_t b_reopen_display;
80 Uint8 * p_sdl_buf[2]; /* Buffer information */
83 /*****************************************************************************
85 *****************************************************************************/
86 static int vout_Probe ( probedata_t *p_data );
87 static int vout_Create ( struct vout_thread_s * );
88 static int vout_Init ( struct vout_thread_s * );
89 static void vout_End ( struct vout_thread_s * );
90 static void vout_Destroy ( struct vout_thread_s * );
91 static int vout_Manage ( struct vout_thread_s * );
92 static void vout_Display ( struct vout_thread_s * );
94 static int SDLOpenDisplay ( vout_thread_t *p_vout );
95 static void SDLCloseDisplay ( vout_thread_t *p_vout );
97 /*****************************************************************************
98 * Functions exported as capabilities. They are declared as static so that
99 * we don't pollute the namespace too much.
100 *****************************************************************************/
101 void vout_getfunctions( function_list_t * p_function_list )
103 p_function_list->pf_probe = vout_Probe;
104 p_function_list->functions.vout.pf_create = vout_Create;
105 p_function_list->functions.vout.pf_init = vout_Init;
106 p_function_list->functions.vout.pf_end = vout_End;
107 p_function_list->functions.vout.pf_destroy = vout_Destroy;
108 p_function_list->functions.vout.pf_manage = vout_Manage;
109 p_function_list->functions.vout.pf_display = vout_Display;
110 p_function_list->functions.vout.pf_setpalette = NULL;
113 /*****************************************************************************
114 * vout_Probe: probe the video driver and return a score
115 *****************************************************************************
116 * This function tries to initialize SDL and returns a score to the
117 * plugin manager so that it can select the best plugin.
118 *****************************************************************************/
119 static int vout_Probe( probedata_t *p_data )
121 if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
129 /*****************************************************************************
130 * vout_Create: allocate SDL video thread output method
131 *****************************************************************************
132 * This function allocate and initialize a SDL vout method. It uses some of the
133 * vout properties to choose the correct mode, and change them according to the
134 * mode actually used.
135 *****************************************************************************/
136 int vout_Create( vout_thread_t *p_vout )
138 /* Allocate structure */
139 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
140 if( p_vout->p_sys == NULL )
142 intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
146 /* Initialize library */
147 if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE)
150 intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
151 free( p_vout->p_sys );
155 /* Force the software yuv even if it is not used */
156 /* If we don't do this, p_vout is not correctly initialized
157 and it's impossible to switch between soft/hard yuv */
158 /* FIXME: this is a broken way to do !! fix this !! */
159 p_vout->b_need_render = 1;
161 p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
162 p_vout->p_sys->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
163 VOUT_FULLSCREEN_DEFAULT );
164 p_vout->p_sys->b_overlay = main_GetIntVariable( VOUT_OVERLAY_VAR,
165 VOUT_OVERLAY_DEFAULT );
166 p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
167 VOUT_WIDTH_DEFAULT );
168 p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
169 VOUT_HEIGHT_DEFAULT );
171 p_vout->p_sys->p_display = NULL;
172 p_vout->p_sys->p_overlay = NULL;
174 if( SDLOpenDisplay(p_vout) )
176 intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
177 free( p_vout->p_sys );
181 /* FIXME: get rid of this ASAP, it's FUCKING UGLY */
182 { intf_thread_t * p_intf = p_main->p_intf;
183 intf_AssignKey(p_intf, SDLK_q, INTF_KEY_QUIT, 0);
184 intf_AssignKey(p_intf, SDLK_ESCAPE, INTF_KEY_QUIT, 0);
185 /* intf_AssignKey(p_intf,3,'Q'); */
186 intf_AssignKey(p_intf, SDLK_0, INTF_KEY_SET_CHANNEL,0);
187 intf_AssignKey(p_intf, SDLK_1, INTF_KEY_SET_CHANNEL,1);
188 intf_AssignKey(p_intf, SDLK_2, INTF_KEY_SET_CHANNEL,2);
189 intf_AssignKey(p_intf, SDLK_3, INTF_KEY_SET_CHANNEL,3);
190 intf_AssignKey(p_intf, SDLK_4, INTF_KEY_SET_CHANNEL,4);
191 intf_AssignKey(p_intf, SDLK_5, INTF_KEY_SET_CHANNEL,5);
192 intf_AssignKey(p_intf, SDLK_6, INTF_KEY_SET_CHANNEL,6);
193 intf_AssignKey(p_intf, SDLK_7, INTF_KEY_SET_CHANNEL,7);
194 intf_AssignKey(p_intf, SDLK_8, INTF_KEY_SET_CHANNEL,8);
195 intf_AssignKey(p_intf, SDLK_9, INTF_KEY_SET_CHANNEL,9);
196 intf_AssignKey(p_intf, SDLK_PLUS, INTF_KEY_INC_VOLUME, 0);
197 intf_AssignKey(p_intf, SDLK_MINUS, INTF_KEY_DEC_VOLUME, 0);
198 intf_AssignKey(p_intf, SDLK_m, INTF_KEY_TOGGLE_VOLUME, 0);
199 /* intf_AssignKey(p_intf,'M','M'); */
200 intf_AssignKey(p_intf, SDLK_g, INTF_KEY_DEC_GAMMA, 0);
201 /* intf_AssignKey(p_intf,'G','G'); */
202 intf_AssignKey(p_intf, SDLK_c, INTF_KEY_TOGGLE_GRAYSCALE, 0);
203 intf_AssignKey(p_intf, SDLK_SPACE, INTF_KEY_TOGGLE_INTERFACE, 0);
204 intf_AssignKey(p_intf, SDLK_i, INTF_KEY_TOGGLE_INFO, 0);
205 intf_AssignKey(p_intf, SDLK_s, INTF_KEY_TOGGLE_SCALING, 0); }
210 /*****************************************************************************
211 * vout_Init: initialize SDL video thread output method
212 *****************************************************************************
213 * This function initialize the SDL display device.
214 *****************************************************************************/
215 int vout_Init( vout_thread_t *p_vout )
220 /*****************************************************************************
221 * vout_End: terminate Sys video thread output method
222 *****************************************************************************
223 * Terminate an output method created by vout_SDLCreate
224 *****************************************************************************/
225 void vout_End( vout_thread_t *p_vout )
227 SDLCloseDisplay( p_vout );
231 /*****************************************************************************
232 * vout_Destroy: destroy Sys video thread output method
233 *****************************************************************************
234 * Terminate an output method created by vout_SDLCreate
235 *****************************************************************************/
236 void vout_Destroy( vout_thread_t *p_vout )
238 free( p_vout->p_sys );
241 /*****************************************************************************
242 * vout_Manage: handle Sys events
243 *****************************************************************************
244 * This function should be called regularly by video output thread. It returns
245 * a non null value if an error occured.
246 *****************************************************************************/
247 int vout_Manage( vout_thread_t *p_vout )
249 SDL_Event event; /* SDL event */
253 while( SDL_PollEvent(&event) )
257 case SDL_VIDEORESIZE: /* Resizing of window */
258 p_vout->i_width = event.resize.w;
259 p_vout->i_height = event.resize.h;
260 p_vout->i_changes |= VOUT_SIZE_CHANGE;
263 case SDL_MOUSEBUTTONDOWN:
264 switch( event.button.button )
266 case SDL_BUTTON_MIDDLE:
267 p_vout->i_changes |= VOUT_CURSOR_CHANGE;
270 case SDL_BUTTON_RIGHT:
271 p_main->p_intf->b_menu_change = 1;
277 intf_ProcessKey( p_main->p_intf, SDLK_q );
280 case SDL_KEYDOWN: /* if a key is pressed */
281 i_key = event.key.keysym.sym;
285 case SDLK_f: /* switch to fullscreen */
286 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
289 case SDLK_y: /* switch to hard YUV */
290 p_vout->i_changes |= VOUT_YUV_CHANGE;
293 case SDLK_c: /* toggle grayscale */
294 p_vout->b_grayscale = ! p_vout->b_grayscale;
295 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
298 case SDLK_i: /* toggle info */
299 p_vout->b_info = ! p_vout->b_info;
300 p_vout->i_changes |= VOUT_INFO_CHANGE;
303 case SDLK_s: /* toggle scaling */
304 p_vout->b_scale = ! p_vout->b_scale;
305 p_vout->i_changes |= VOUT_SCALE_CHANGE;
308 case SDLK_SPACE: /* toggle interface */
309 p_vout->b_interface = ! p_vout->b_interface;
310 p_vout->i_changes |= VOUT_INTF_CHANGE;
314 if( intf_ProcessKey( p_main->p_intf, (char )i_key ) )
316 intf_DbgMsg( "unhandled key '%c' (%i)", (char)i_key, i_key ); }
329 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
331 p_vout->p_sys->i_width = p_vout->i_width;
332 p_vout->p_sys->i_height = p_vout->i_height;
334 /* Need to reopen display */
335 SDLCloseDisplay( p_vout );
336 if( SDLOpenDisplay( p_vout ) )
338 intf_ErrMsg( "error: can't open DISPLAY default display" );
341 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
347 if( p_vout->i_changes & VOUT_YUV_CHANGE )
349 p_vout->b_need_render = ! p_vout->b_need_render;
351 /* Need to reopen display */
352 SDLCloseDisplay( p_vout );
353 if( SDLOpenDisplay( p_vout ) )
355 intf_ErrMsg( "error: can't open DISPLAY default display" );
358 p_vout->i_changes &= ~VOUT_YUV_CHANGE;
364 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
366 p_vout->p_sys->b_fullscreen = ! p_vout->p_sys->b_fullscreen;
368 if( p_vout->p_sys->b_fullscreen )
370 p_vout->p_sys->b_fullscreen = 0;
371 p_vout->p_sys->b_cursor = 1;
376 p_vout->p_sys->b_fullscreen = 1;
377 p_vout->p_sys->b_cursor = 0;
381 SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
383 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
389 if( p_vout->i_changes & VOUT_CURSOR_CHANGE )
391 if( p_vout->p_sys->b_cursor )
394 p_vout->p_sys->b_cursor = 0;
399 p_vout->p_sys->b_cursor = 1;
401 p_vout->i_changes &= ~VOUT_CURSOR_CHANGE;
407 /*****************************************************************************
408 * vout_SetPalette: sets an 8 bpp palette
409 *****************************************************************************
410 * This function sets the palette given as an argument. It does not return
411 * anything, but could later send information on which colors it was unable
413 *****************************************************************************/
414 void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
415 u16 *blue, u16 *transp)
417 /* Create a display surface with a grayscale palette */
418 SDL_Color colors[256];
421 /* Fill colors with color information */
422 for( i = 0; i < 256; i++ )
424 colors[ i ].r = red[ i ] >> 8;
425 colors[ i ].g = green[ i ] >> 8;
426 colors[ i ].b = blue[ i ] >> 8;
430 if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
432 intf_ErrMsg( "vout error: failed setting palette" );
437 /*****************************************************************************
438 * vout_Display: displays previously rendered output
439 *****************************************************************************
440 * This function send the currently rendered image to the display, wait until
441 * it is displayed and switch the two rendering buffer, preparing next frame.
442 *****************************************************************************/
443 void vout_Display( vout_thread_t *p_vout )
446 if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
448 if( p_vout->b_need_render )
450 /* Change display frame */
451 SDL_Flip( p_vout->p_sys->p_display );
457 * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
460 /* TODO: support for streams other than 4:2:0 */
461 /* create the overlay if necessary */
462 if( p_vout->p_sys->p_overlay == NULL )
464 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay(
465 p_vout->p_rendered_pic->i_width,
466 p_vout->p_rendered_pic->i_height,
468 p_vout->p_sys->p_display
470 intf_Msg("vout: YUV acceleration %s",
471 p_vout->p_sys->p_overlay->hw_overlay
472 ? "activated" : "unavailable !" );
475 SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
476 /* copy the data into video buffers */
478 memcpy(p_vout->p_sys->p_overlay->pixels[0],
479 p_vout->p_rendered_pic->p_y,
480 p_vout->p_sys->p_overlay->h *
481 p_vout->p_sys->p_overlay->pitches[0]);
483 memcpy(p_vout->p_sys->p_overlay->pixels[1],
484 p_vout->p_rendered_pic->p_v,
485 p_vout->p_sys->p_overlay->h *
486 p_vout->p_sys->p_overlay->pitches[1] / 2);
488 memcpy(p_vout->p_sys->p_overlay->pixels[2],
489 p_vout->p_rendered_pic->p_u,
490 p_vout->p_sys->p_overlay->h *
491 p_vout->p_sys->p_overlay->pitches[2] / 2);
493 disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
494 disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
495 disp.x = (p_vout->i_width - disp.w)/2;
496 disp.y = (p_vout->i_height - disp.h)/2;
498 SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
499 SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
504 /* following functions are local */
506 /*****************************************************************************
507 * SDLOpenDisplay: open and initialize SDL device
508 *****************************************************************************
509 * Open and initialize display according to preferences specified in the vout
511 *****************************************************************************/
512 static int SDLOpenDisplay( vout_thread_t *p_vout )
514 SDL_Rect clipping_rect;
518 * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
521 /* init flags and cursor */
522 flags = SDL_ANYFORMAT | SDL_HWPALETTE;
524 if( p_vout->p_sys->b_fullscreen )
525 flags |= SDL_FULLSCREEN;
527 flags |= SDL_RESIZABLE;
529 if( p_vout->b_need_render )
530 flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
532 flags |= SDL_SWSURFACE; /* save video memory */
534 bpp = SDL_VideoModeOK(p_vout->p_sys->i_width,
535 p_vout->p_sys->i_height,
536 p_vout->i_screen_depth, flags);
540 intf_ErrMsg( "error: can't open DISPLAY default display" );
544 p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
545 p_vout->p_sys->i_height,
548 if( p_vout->p_sys->p_display == NULL )
550 intf_ErrMsg( "error: can't open DISPLAY default display" );
554 SDL_LockSurface(p_vout->p_sys->p_display);
556 if( p_vout->p_sys->b_fullscreen )
561 SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
562 SDL_EventState(SDL_KEYUP , SDL_IGNORE); /* ignore keys up */
563 SDL_EventState(SDL_MOUSEBUTTONUP, SDL_IGNORE);
565 if( p_vout->b_need_render )
567 p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
568 SDL_Flip(p_vout->p_sys->p_display);
569 p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
570 SDL_Flip(p_vout->p_sys->p_display);
572 /* Set clipping for text */
575 clipping_rect.w = p_vout->p_sys->p_display->w;
576 clipping_rect.h = p_vout->p_sys->p_display->h;
577 SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
579 /* Set thread information */
580 p_vout->i_width = p_vout->p_sys->p_display->w;
581 p_vout->i_height = p_vout->p_sys->p_display->h;
582 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
584 p_vout->i_screen_depth =
585 p_vout->p_sys->p_display->format->BitsPerPixel;
586 p_vout->i_bytes_per_pixel =
587 p_vout->p_sys->p_display->format->BytesPerPixel;
589 p_vout->i_red_mask = p_vout->p_sys->p_display->format->Rmask;
590 p_vout->i_green_mask = p_vout->p_sys->p_display->format->Gmask;
591 p_vout->i_blue_mask = p_vout->p_sys->p_display->format->Bmask;
593 /* FIXME: palette in 8bpp ?? */
594 /* Set and initialize buffers */
595 vout_SetBuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
596 p_vout->p_sys->p_sdl_buf[ 1 ] );
600 p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
601 p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
603 /* Set thread information */
604 p_vout->i_width = p_vout->p_sys->p_display->w;
605 p_vout->i_height = p_vout->p_sys->p_display->h;
606 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
608 vout_SetBuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
609 p_vout->p_sys->p_sdl_buf[ 1 ] );
612 p_vout->p_sys->b_reopen_display = 0;
617 /*****************************************************************************
618 * SDLCloseDisplay: close and reset SDL device
619 *****************************************************************************
620 * This function returns all resources allocated by SDLOpenDisplay and restore
621 * the original state of the device.
622 *****************************************************************************/
623 static void SDLCloseDisplay( vout_thread_t *p_vout )
625 if( p_vout->p_sys->p_display != NULL )
627 if( p_vout->p_sys->p_overlay != NULL )
629 SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
630 p_vout->p_sys->p_overlay = NULL;
633 SDL_UnlockSurface ( p_vout->p_sys->p_display );
634 SDL_FreeSurface( p_vout->p_sys->p_display );
635 p_vout->p_sys->p_display = NULL;