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.46 2001/04/06 09:15:47 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Pierre Baillet <oct@zoy.org>
9 * Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
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.
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.
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 *****************************************************************************/
26 #define MODULE_NAME sdl
27 #include "modules_inner.h"
29 /*****************************************************************************
31 *****************************************************************************/
34 #include <errno.h> /* ENOMEM */
35 #include <stdlib.h> /* free() */
36 #include <string.h> /* strerror() */
48 #include "video_output.h"
51 #include "interface.h"
52 /* FIXME: get rid of this */
53 #include "keystrokes.h"
56 /*****************************************************************************
57 * FIXME: this file is ... *
59 * XXX XXX FIXME XXX XXX XXX XXX *
60 * XXX XXX XXX XXX XXX XXX XXX XXX *
61 * XXX XXX XXX XXX FIXME XXX *
62 * XXX XXX XXX TODO XXX XXX XXX *
63 * XXX XXX XXX XXX XXX XXX *
64 * FIXME FIXME FIXME XXX XXX *
66 *****************************************************************************/
68 /*****************************************************************************
69 * vout_sys_t: video output SDL method descriptor
70 *****************************************************************************
71 * This structure is part of the video output thread descriptor.
72 * It describes the SDL specific properties of an output thread.
73 *****************************************************************************/
74 typedef struct vout_sys_s
79 SDL_Surface * p_display; /* display device */
80 SDL_Overlay * p_overlay; /* overlay device */
82 boolean_t b_fullscreen;
85 boolean_t b_reopen_display;
87 boolean_t b_cursor_autohidden;
90 Uint8 * p_sdl_buf[2]; /* Buffer information */
94 /*****************************************************************************
96 *****************************************************************************/
97 static int vout_Probe ( probedata_t *p_data );
98 static int vout_Create ( struct vout_thread_s * );
99 static int vout_Init ( struct vout_thread_s * );
100 static void vout_End ( struct vout_thread_s * );
101 static void vout_Destroy ( struct vout_thread_s * );
102 static int vout_Manage ( struct vout_thread_s * );
103 static void vout_Display ( struct vout_thread_s * );
104 static void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
105 u16 *blue, u16 *transp );
107 static int SDLOpenDisplay ( vout_thread_t *p_vout );
108 static void SDLCloseDisplay ( vout_thread_t *p_vout );
110 /*****************************************************************************
111 * Functions exported as capabilities. They are declared as static so that
112 * we don't pollute the namespace too much.
113 *****************************************************************************/
114 void _M( vout_getfunctions )( function_list_t * p_function_list )
116 p_function_list->pf_probe = vout_Probe;
117 p_function_list->functions.vout.pf_create = vout_Create;
118 p_function_list->functions.vout.pf_init = vout_Init;
119 p_function_list->functions.vout.pf_end = vout_End;
120 p_function_list->functions.vout.pf_destroy = vout_Destroy;
121 p_function_list->functions.vout.pf_manage = vout_Manage;
122 p_function_list->functions.vout.pf_display = vout_Display;
123 p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
126 /*****************************************************************************
127 * vout_Probe: probe the video driver and return a score
128 *****************************************************************************
129 * This function tries to initialize SDL and returns a score to the
130 * plugin manager so that it can select the best plugin.
131 *****************************************************************************/
132 static int vout_Probe( probedata_t *p_data )
134 if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
142 /*****************************************************************************
143 * vout_Create: allocate SDL video thread output method
144 *****************************************************************************
145 * This function allocate and initialize a SDL vout method. It uses some of the
146 * vout properties to choose the correct mode, and change them according to the
147 * mode actually used.
148 *****************************************************************************/
149 static int vout_Create( vout_thread_t *p_vout )
151 /* Allocate structure */
152 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
153 if( p_vout->p_sys == NULL )
155 intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
159 /* Initialize library */
160 if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE)
163 intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
164 free( p_vout->p_sys );
168 p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
170 p_vout->p_sys->b_cursor_autohidden = 0;
171 p_vout->p_sys->i_lastmoved = mdate();
173 p_vout->p_sys->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
174 VOUT_FULLSCREEN_DEFAULT );
175 p_vout->p_sys->b_overlay = main_GetIntVariable( VOUT_OVERLAY_VAR,
176 VOUT_OVERLAY_DEFAULT );
177 p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
178 VOUT_WIDTH_DEFAULT );
179 p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
180 VOUT_HEIGHT_DEFAULT );
182 p_vout->p_sys->p_display = NULL;
183 p_vout->p_sys->p_overlay = NULL;
185 if( SDLOpenDisplay(p_vout) )
187 intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
188 free( p_vout->p_sys );
192 /* FIXME: get rid of this ASAP, it's FUCKING UGLY */
193 { intf_thread_t * p_intf = p_main->p_intf;
194 intf_AssignKey(p_intf, SDLK_q, INTF_KEY_QUIT, 0);
195 intf_AssignKey(p_intf, SDLK_ESCAPE, INTF_KEY_QUIT, 0);
196 /* intf_AssignKey(p_intf,3,'Q'); */
197 intf_AssignKey(p_intf, SDLK_0, INTF_KEY_SET_CHANNEL,0);
198 intf_AssignKey(p_intf, SDLK_1, INTF_KEY_SET_CHANNEL,1);
199 intf_AssignKey(p_intf, SDLK_2, INTF_KEY_SET_CHANNEL,2);
200 intf_AssignKey(p_intf, SDLK_3, INTF_KEY_SET_CHANNEL,3);
201 intf_AssignKey(p_intf, SDLK_4, INTF_KEY_SET_CHANNEL,4);
202 intf_AssignKey(p_intf, SDLK_5, INTF_KEY_SET_CHANNEL,5);
203 intf_AssignKey(p_intf, SDLK_6, INTF_KEY_SET_CHANNEL,6);
204 intf_AssignKey(p_intf, SDLK_7, INTF_KEY_SET_CHANNEL,7);
205 intf_AssignKey(p_intf, SDLK_8, INTF_KEY_SET_CHANNEL,8);
206 intf_AssignKey(p_intf, SDLK_9, INTF_KEY_SET_CHANNEL,9);
207 intf_AssignKey(p_intf, SDLK_PLUS, INTF_KEY_INC_VOLUME, 0);
208 intf_AssignKey(p_intf, SDLK_MINUS, INTF_KEY_DEC_VOLUME, 0);
209 intf_AssignKey(p_intf, SDLK_m, INTF_KEY_TOGGLE_VOLUME, 0);
210 /* intf_AssignKey(p_intf,'M','M'); */
211 intf_AssignKey(p_intf, SDLK_g, INTF_KEY_DEC_GAMMA, 0);
212 /* intf_AssignKey(p_intf,'G','G'); */
213 intf_AssignKey(p_intf, SDLK_c, INTF_KEY_TOGGLE_GRAYSCALE, 0);
214 intf_AssignKey(p_intf, SDLK_SPACE, INTF_KEY_TOGGLE_INTERFACE, 0);
215 intf_AssignKey(p_intf, SDLK_i, INTF_KEY_TOGGLE_INFO, 0);
216 intf_AssignKey(p_intf, SDLK_s, INTF_KEY_TOGGLE_SCALING, 0);
217 intf_AssignKey(p_intf, SDLK_d, INTF_KEY_DUMP_STREAM, 0); }
222 /*****************************************************************************
223 * vout_Init: initialize SDL video thread output method
224 *****************************************************************************
225 * This function initialize the SDL display device.
226 *****************************************************************************/
227 static int vout_Init( vout_thread_t *p_vout )
229 /* This hack is hugly, but hey, you are, too. */
231 SDL_Overlay * p_overlay;
233 p_overlay = SDL_CreateYUVOverlay(
234 main_GetIntVariable( VOUT_WIDTH_VAR,VOUT_WIDTH_DEFAULT ),
235 main_GetIntVariable( VOUT_HEIGHT_VAR,VOUT_HEIGHT_DEFAULT ),
237 p_vout->p_sys->p_display );
238 intf_Msg( "vout: YUV acceleration %s",
239 p_overlay->hw_overlay ? "activated" : "unavailable !" );
240 p_vout->b_need_render = !p_overlay->hw_overlay;
242 SDL_FreeYUVOverlay( p_overlay );
247 /*****************************************************************************
248 * vout_End: terminate Sys video thread output method
249 *****************************************************************************
250 * Terminate an output method created by vout_SDLCreate
251 *****************************************************************************/
252 static void vout_End( vout_thread_t *p_vout )
254 SDLCloseDisplay( p_vout );
258 /*****************************************************************************
259 * vout_Destroy: destroy Sys video thread output method
260 *****************************************************************************
261 * Terminate an output method created by vout_SDLCreate
262 *****************************************************************************/
263 static void vout_Destroy( vout_thread_t *p_vout )
265 free( p_vout->p_sys );
268 /*****************************************************************************
269 * vout_Manage: handle Sys events
270 *****************************************************************************
271 * This function should be called regularly by video output thread. It returns
272 * a non null value if an error occured.
273 *****************************************************************************/
274 static int vout_Manage( vout_thread_t *p_vout )
276 SDL_Event event; /* SDL event */
280 while( SDL_PollEvent(&event) )
284 case SDL_VIDEORESIZE: /* Resizing of window */
285 p_vout->i_width = event.resize.w;
286 p_vout->i_height = event.resize.h;
287 p_vout->i_changes |= VOUT_SIZE_CHANGE;
290 case SDL_MOUSEMOTION:
291 if( p_vout->p_sys->b_cursor &&
292 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
294 if( p_vout->p_sys->b_cursor_autohidden )
296 p_vout->p_sys->b_cursor_autohidden = 0;
301 p_vout->p_sys->i_lastmoved = mdate();
306 case SDL_MOUSEBUTTONUP:
307 switch( event.button.button )
309 case SDL_BUTTON_RIGHT:
310 p_main->p_intf->b_menu_change = 1;
315 case SDL_MOUSEBUTTONDOWN:
316 switch( event.button.button )
318 case SDL_BUTTON_MIDDLE:
319 p_vout->i_changes |= VOUT_CURSOR_CHANGE;
325 intf_ProcessKey( p_main->p_intf, SDLK_q );
328 case SDL_KEYDOWN: /* if a key is pressed */
329 i_key = event.key.keysym.sym;
333 case SDLK_f: /* switch to fullscreen */
334 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
337 case SDLK_y: /* switch to hard YUV */
338 p_vout->i_changes |= VOUT_YUV_CHANGE;
341 case SDLK_c: /* toggle grayscale */
342 p_vout->b_grayscale = ! p_vout->b_grayscale;
343 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
346 case SDLK_i: /* toggle info */
347 p_vout->b_info = ! p_vout->b_info;
348 p_vout->i_changes |= VOUT_INFO_CHANGE;
351 case SDLK_s: /* toggle scaling */
352 p_vout->b_scale = ! p_vout->b_scale;
353 p_vout->i_changes |= VOUT_SCALE_CHANGE;
356 case SDLK_SPACE: /* toggle interface */
357 p_vout->b_interface = ! p_vout->b_interface;
358 p_vout->i_changes |= VOUT_INTF_CHANGE;
362 if( intf_ProcessKey( p_main->p_intf, (char )i_key ) )
364 intf_DbgMsg( "unhandled key '%c' (%i)", (char)i_key, i_key ); }
377 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
379 p_vout->p_sys->i_width = p_vout->i_width;
380 p_vout->p_sys->i_height = p_vout->i_height;
382 /* Need to reopen display */
383 SDLCloseDisplay( p_vout );
384 if( SDLOpenDisplay( p_vout ) )
386 intf_ErrMsg( "vout error: can't reopen display after resize" );
389 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
395 if( p_vout->i_changes & VOUT_YUV_CHANGE )
397 p_vout->b_need_render = ! p_vout->b_need_render;
399 /* Need to reopen display */
400 SDLCloseDisplay( p_vout );
401 if( SDLOpenDisplay( p_vout ) )
403 intf_ErrMsg( "error: can't reopen display after YUV change" );
406 p_vout->i_changes &= ~VOUT_YUV_CHANGE;
412 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
414 p_vout->p_sys->b_fullscreen = ! p_vout->p_sys->b_fullscreen;
416 SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
418 p_vout->p_sys->b_cursor_autohidden = 0;
419 SDL_ShowCursor( p_vout->p_sys->b_cursor &&
420 ! p_vout->p_sys->b_cursor_autohidden );
422 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
428 if( ! p_vout->p_sys->b_cursor_autohidden &&
429 ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
431 /* Hide the mouse automatically */
432 p_vout->p_sys->b_cursor_autohidden = 1;
436 if( p_vout->i_changes & VOUT_CURSOR_CHANGE )
438 p_vout->p_sys->b_cursor = ! p_vout->p_sys->b_cursor;
440 SDL_ShowCursor( p_vout->p_sys->b_cursor &&
441 ! p_vout->p_sys->b_cursor_autohidden );
443 p_vout->i_changes &= ~VOUT_CURSOR_CHANGE;
449 /*****************************************************************************
450 * vout_SetPalette: sets an 8 bpp palette
451 *****************************************************************************
452 * This function sets the palette given as an argument. It does not return
453 * anything, but could later send information on which colors it was unable
455 *****************************************************************************/
456 static void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
457 u16 *blue, u16 *transp)
459 /* Create a display surface with a grayscale palette */
460 SDL_Color colors[256];
463 /* Fill colors with color information */
464 for( i = 0; i < 256; i++ )
466 colors[ i ].r = red[ i ] >> 8;
467 colors[ i ].g = green[ i ] >> 8;
468 colors[ i ].b = blue[ i ] >> 8;
472 if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
474 intf_ErrMsg( "vout error: failed setting palette" );
479 /*****************************************************************************
480 * vout_Display: displays previously rendered output
481 *****************************************************************************
482 * This function send the currently rendered image to the display, wait until
483 * it is displayed and switch the two rendering buffer, preparing next frame.
484 *****************************************************************************/
485 static void vout_Display( vout_thread_t *p_vout )
488 if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
490 if( p_vout->b_need_render )
492 /* Change display frame */
493 SDL_Flip( p_vout->p_sys->p_display );
498 * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
501 /* TODO: support for streams other than 4:2:0 */
502 /* create the overlay if necessary */
503 if( p_vout->p_sys->p_overlay == NULL )
505 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay(
506 p_vout->p_rendered_pic->i_width,
507 p_vout->p_rendered_pic->i_height,
509 p_vout->p_sys->p_display
511 intf_Msg("vout: YUV acceleration %s",
512 p_vout->p_sys->p_overlay->hw_overlay
513 ? "activated" : "unavailable !" );
516 SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
517 /* copy the data into video buffers */
519 memcpy(p_vout->p_sys->p_overlay->pixels[0],
520 p_vout->p_rendered_pic->p_y,
521 p_vout->p_sys->p_overlay->h *
522 p_vout->p_sys->p_overlay->pitches[0]);
524 memcpy(p_vout->p_sys->p_overlay->pixels[1],
525 p_vout->p_rendered_pic->p_v,
526 p_vout->p_sys->p_overlay->h *
527 p_vout->p_sys->p_overlay->pitches[1] / 2);
529 memcpy(p_vout->p_sys->p_overlay->pixels[2],
530 p_vout->p_rendered_pic->p_u,
531 p_vout->p_sys->p_overlay->h *
532 p_vout->p_sys->p_overlay->pitches[2] / 2);
534 disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
535 disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
536 disp.x = (p_vout->i_width - disp.w)/2;
537 disp.y = (p_vout->i_height - disp.h)/2;
539 SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
540 SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
545 /* following functions are local */
547 /*****************************************************************************
548 * SDLOpenDisplay: open and initialize SDL device
549 *****************************************************************************
550 * Open and initialize display according to preferences specified in the vout
552 *****************************************************************************/
553 static int SDLOpenDisplay( vout_thread_t *p_vout )
555 SDL_Rect clipping_rect;
559 * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
562 /* init flags and cursor */
563 flags = SDL_ANYFORMAT | SDL_HWPALETTE;
565 if( p_vout->p_sys->b_fullscreen )
567 flags |= SDL_FULLSCREEN;
571 flags |= SDL_RESIZABLE;
574 if( p_vout->b_need_render )
576 flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
580 flags |= SDL_SWSURFACE; /* save video memory */
583 bpp = SDL_VideoModeOK( p_vout->p_sys->i_width,
584 p_vout->p_sys->i_height,
585 p_vout->i_screen_depth, flags );
589 intf_ErrMsg( "vout error: no video mode available" );
593 p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
594 p_vout->p_sys->i_height,
597 if( p_vout->p_sys->p_display == NULL )
599 intf_ErrMsg( "vout error: cannot set video mode" );
603 SDL_LockSurface( p_vout->p_sys->p_display );
605 SDL_WM_SetCaption( VOUT_TITLE " (SDL output)",
606 VOUT_TITLE " (SDL output)" );
607 SDL_EventState(SDL_KEYUP , SDL_IGNORE); /* ignore keys up */
609 if( p_vout->b_need_render )
611 p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
612 SDL_Flip(p_vout->p_sys->p_display);
613 p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
614 SDL_Flip(p_vout->p_sys->p_display);
616 /* Set clipping for text */
619 clipping_rect.w = p_vout->p_sys->p_display->w;
620 clipping_rect.h = p_vout->p_sys->p_display->h;
621 SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
623 /* Set thread information */
624 p_vout->i_width = p_vout->p_sys->p_display->w;
625 p_vout->i_height = p_vout->p_sys->p_display->h;
626 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
628 p_vout->i_screen_depth =
629 p_vout->p_sys->p_display->format->BitsPerPixel;
630 p_vout->i_bytes_per_pixel =
631 p_vout->p_sys->p_display->format->BytesPerPixel;
633 p_vout->i_red_mask = p_vout->p_sys->p_display->format->Rmask;
634 p_vout->i_green_mask = p_vout->p_sys->p_display->format->Gmask;
635 p_vout->i_blue_mask = p_vout->p_sys->p_display->format->Bmask;
637 /* FIXME: palette in 8bpp ?? */
638 /* Set and initialize buffers */
639 vout_SetBuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
640 p_vout->p_sys->p_sdl_buf[ 1 ] );
644 p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
645 p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
647 /* Set thread information */
648 p_vout->i_width = p_vout->p_sys->p_display->w;
649 p_vout->i_height = p_vout->p_sys->p_display->h;
650 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
652 vout_SetBuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
653 p_vout->p_sys->p_sdl_buf[ 1 ] );
656 p_vout->p_sys->b_reopen_display = 0;
661 /*****************************************************************************
662 * SDLCloseDisplay: close and reset SDL device
663 *****************************************************************************
664 * This function returns all resources allocated by SDLOpenDisplay and restore
665 * the original state of the device.
666 *****************************************************************************/
667 static void SDLCloseDisplay( vout_thread_t *p_vout )
669 if( p_vout->p_sys->p_display != NULL )
671 if( p_vout->p_sys->p_overlay != NULL )
673 SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
674 p_vout->p_sys->p_overlay = NULL;
677 SDL_UnlockSurface ( p_vout->p_sys->p_display );
678 SDL_FreeSurface( p_vout->p_sys->p_display );
679 p_vout->p_sys->p_display = NULL;