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.52 2001/05/25 13:20:09 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() */
39 #include <netinet/in.h> /* BSD: struct in_addr */
52 #include "video_output.h"
55 #include "interface.h"
56 /* FIXME: get rid of this */
57 #include "keystrokes.h"
61 /*****************************************************************************
62 * FIXME: this file is ... *
64 * XXX XXX FIXME XXX XXX XXX XXX *
65 * XXX XXX XXX XXX XXX XXX XXX XXX *
66 * XXX XXX XXX XXX FIXME XXX *
67 * XXX XXX XXX TODO XXX XXX XXX *
68 * XXX XXX XXX XXX XXX XXX *
69 * FIXME FIXME FIXME XXX XXX *
71 *****************************************************************************/
73 /*****************************************************************************
74 * vout_sys_t: video output SDL method descriptor
75 *****************************************************************************
76 * This structure is part of the video output thread descriptor.
77 * It describes the SDL specific properties of an output thread.
78 *****************************************************************************/
79 typedef struct vout_sys_s
84 SDL_Surface * p_display; /* display device */
85 SDL_Overlay * p_overlay; /* overlay device */
89 boolean_t b_reopen_display;
91 boolean_t b_cursor_autohidden;
94 Uint8 * p_sdl_buf[2]; /* Buffer information */
98 /*****************************************************************************
100 *****************************************************************************/
101 static int vout_Probe ( probedata_t *p_data );
102 static int vout_Create ( struct vout_thread_s * );
103 static int vout_Init ( struct vout_thread_s * );
104 static void vout_End ( struct vout_thread_s * );
105 static void vout_Destroy ( struct vout_thread_s * );
106 static int vout_Manage ( struct vout_thread_s * );
107 static void vout_Display ( struct vout_thread_s * );
108 static void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
109 u16 *blue, u16 *transp );
111 static int SDLOpenDisplay ( vout_thread_t *p_vout );
112 static void SDLCloseDisplay ( vout_thread_t *p_vout );
114 /*****************************************************************************
115 * Functions exported as capabilities. They are declared as static so that
116 * we don't pollute the namespace too much.
117 *****************************************************************************/
118 void _M( vout_getfunctions )( function_list_t * p_function_list )
120 p_function_list->pf_probe = vout_Probe;
121 p_function_list->functions.vout.pf_create = vout_Create;
122 p_function_list->functions.vout.pf_init = vout_Init;
123 p_function_list->functions.vout.pf_end = vout_End;
124 p_function_list->functions.vout.pf_destroy = vout_Destroy;
125 p_function_list->functions.vout.pf_manage = vout_Manage;
126 p_function_list->functions.vout.pf_display = vout_Display;
127 p_function_list->functions.vout.pf_setpalette = vout_SetPalette;
130 /*****************************************************************************
131 * vout_Probe: probe the video driver and return a score
132 *****************************************************************************
133 * This function tries to initialize SDL and returns a score to the
134 * plugin manager so that it can select the best plugin.
135 *****************************************************************************/
136 static int vout_Probe( probedata_t *p_data )
138 if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
146 /*****************************************************************************
147 * vout_Create: allocate SDL video thread output method
148 *****************************************************************************
149 * This function allocate and initialize a SDL vout method. It uses some of the
150 * vout properties to choose the correct mode, and change them according to the
151 * mode actually used.
152 *****************************************************************************/
153 static int vout_Create( vout_thread_t *p_vout )
155 /* Allocate structure */
156 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
157 if( p_vout->p_sys == NULL )
159 intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
163 /* Initialize library */
164 if( SDL_Init( SDL_INIT_VIDEO
166 /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
167 | SDL_INIT_EVENTTHREAD
169 | SDL_INIT_NOPARACHUTE ) < 0 )
171 intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
172 free( p_vout->p_sys );
176 p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
178 p_vout->p_sys->b_cursor_autohidden = 0;
179 p_vout->p_sys->i_lastmoved = mdate();
181 p_vout->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
182 VOUT_FULLSCREEN_DEFAULT );
183 p_vout->p_sys->b_overlay = main_GetIntVariable( VOUT_OVERLAY_VAR,
184 VOUT_OVERLAY_DEFAULT );
185 p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
186 VOUT_WIDTH_DEFAULT );
187 p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
188 VOUT_HEIGHT_DEFAULT );
190 p_vout->p_sys->p_display = NULL;
191 p_vout->p_sys->p_overlay = NULL;
193 if( SDLOpenDisplay(p_vout) )
195 intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
196 free( p_vout->p_sys );
200 /* FIXME: get rid of this ASAP, it's FUCKING UGLY */
201 { intf_thread_t * p_intf = p_main->p_intf;
202 intf_AssignKey(p_intf, SDLK_q, INTF_KEY_QUIT, 0);
203 intf_AssignKey(p_intf, SDLK_ESCAPE, INTF_KEY_QUIT, 0);
204 /* intf_AssignKey(p_intf,3,'Q'); */
205 intf_AssignKey(p_intf, SDLK_0, INTF_KEY_SET_CHANNEL,0);
206 intf_AssignKey(p_intf, SDLK_1, INTF_KEY_SET_CHANNEL,1);
207 intf_AssignKey(p_intf, SDLK_2, INTF_KEY_SET_CHANNEL,2);
208 intf_AssignKey(p_intf, SDLK_3, INTF_KEY_SET_CHANNEL,3);
209 intf_AssignKey(p_intf, SDLK_4, INTF_KEY_SET_CHANNEL,4);
210 intf_AssignKey(p_intf, SDLK_5, INTF_KEY_SET_CHANNEL,5);
211 intf_AssignKey(p_intf, SDLK_6, INTF_KEY_SET_CHANNEL,6);
212 intf_AssignKey(p_intf, SDLK_7, INTF_KEY_SET_CHANNEL,7);
213 intf_AssignKey(p_intf, SDLK_8, INTF_KEY_SET_CHANNEL,8);
214 intf_AssignKey(p_intf, SDLK_9, INTF_KEY_SET_CHANNEL,9);
215 intf_AssignKey(p_intf, SDLK_PLUS, INTF_KEY_INC_VOLUME, 0);
216 intf_AssignKey(p_intf, SDLK_MINUS, INTF_KEY_DEC_VOLUME, 0);
217 intf_AssignKey(p_intf, SDLK_m, INTF_KEY_TOGGLE_VOLUME, 0);
218 /* intf_AssignKey(p_intf,'M','M'); */
219 intf_AssignKey(p_intf, SDLK_g, INTF_KEY_DEC_GAMMA, 0);
220 /* intf_AssignKey(p_intf,'G','G'); */
221 intf_AssignKey(p_intf, SDLK_c, INTF_KEY_TOGGLE_GRAYSCALE, 0);
222 intf_AssignKey(p_intf, SDLK_SPACE, INTF_KEY_TOGGLE_INTERFACE, 0);
223 intf_AssignKey(p_intf, SDLK_i, INTF_KEY_TOGGLE_INFO, 0);
224 intf_AssignKey(p_intf, SDLK_s, INTF_KEY_TOGGLE_SCALING, 0);
225 intf_AssignKey(p_intf, SDLK_d, INTF_KEY_DUMP_STREAM, 0); }
230 /*****************************************************************************
231 * vout_Init: initialize SDL video thread output method
232 *****************************************************************************
233 * This function initialize the SDL display device.
234 *****************************************************************************/
235 static int vout_Init( vout_thread_t *p_vout )
237 /* This hack is hugly, but hey, you are, too. */
239 SDL_Overlay * p_overlay;
241 p_overlay = SDL_CreateYUVOverlay(
242 main_GetIntVariable( VOUT_WIDTH_VAR,VOUT_WIDTH_DEFAULT ),
243 main_GetIntVariable( VOUT_HEIGHT_VAR,VOUT_HEIGHT_DEFAULT ),
245 p_vout->p_sys->p_display );
246 intf_WarnMsg( 2, "vout: YUV acceleration %s",
247 p_overlay->hw_overlay ? "activated" : "unavailable !" );
248 p_vout->b_need_render = !p_overlay->hw_overlay;
250 SDL_FreeYUVOverlay( p_overlay );
255 /*****************************************************************************
256 * vout_End: terminate Sys video thread output method
257 *****************************************************************************
258 * Terminate an output method created by vout_SDLCreate
259 *****************************************************************************/
260 static void vout_End( vout_thread_t *p_vout )
262 SDLCloseDisplay( p_vout );
266 /*****************************************************************************
267 * vout_Destroy: destroy Sys video thread output method
268 *****************************************************************************
269 * Terminate an output method created by vout_SDLCreate
270 *****************************************************************************/
271 static void vout_Destroy( vout_thread_t *p_vout )
273 free( p_vout->p_sys );
276 /*****************************************************************************
277 * vout_Manage: handle Sys events
278 *****************************************************************************
279 * This function should be called regularly by video output thread. It returns
280 * a non null value if an error occured.
281 *****************************************************************************/
282 static int vout_Manage( vout_thread_t *p_vout )
284 SDL_Event event; /* SDL event */
288 while( SDL_PollEvent(&event) )
292 case SDL_VIDEORESIZE: /* Resizing of window */
293 p_vout->i_width = event.resize.w;
294 p_vout->i_height = event.resize.h;
295 p_vout->i_changes |= VOUT_SIZE_CHANGE;
298 case SDL_MOUSEMOTION:
299 if( p_vout->p_sys->b_cursor &&
300 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
302 if( p_vout->p_sys->b_cursor_autohidden )
304 p_vout->p_sys->b_cursor_autohidden = 0;
309 p_vout->p_sys->i_lastmoved = mdate();
314 case SDL_MOUSEBUTTONUP:
315 switch( event.button.button )
317 case SDL_BUTTON_RIGHT:
318 p_main->p_intf->b_menu_change = 1;
323 case SDL_MOUSEBUTTONDOWN:
324 switch( event.button.button )
326 case SDL_BUTTON_MIDDLE:
327 p_vout->i_changes |= VOUT_CURSOR_CHANGE;
333 intf_ProcessKey( p_main->p_intf, SDLK_q );
336 case SDL_KEYDOWN: /* if a key is pressed */
338 switch( event.key.keysym.sym )
340 case SDLK_f: /* switch to fullscreen */
341 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
344 case SDLK_y: /* switch to hard YUV */
345 p_vout->i_changes |= VOUT_YUV_CHANGE;
348 case SDLK_c: /* toggle grayscale */
349 p_vout->b_grayscale = ! p_vout->b_grayscale;
350 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
353 case SDLK_i: /* toggle info */
354 p_vout->b_info = ! p_vout->b_info;
355 p_vout->i_changes |= VOUT_INFO_CHANGE;
358 case SDLK_s: /* toggle scaling */
359 p_vout->b_scale = ! p_vout->b_scale;
360 p_vout->i_changes |= VOUT_SCALE_CHANGE;
363 case SDLK_SPACE: /* toggle interface */
364 p_vout->b_interface = ! p_vout->b_interface;
365 p_vout->i_changes |= VOUT_INTF_CHANGE;
369 network_ChannelJoin( 0 );
372 network_ChannelJoin( 1 );
375 network_ChannelJoin( 2 );
378 network_ChannelJoin( 3 );
381 network_ChannelJoin( 4 );
384 network_ChannelJoin( 5 );
387 network_ChannelJoin( 6 );
390 network_ChannelJoin( 7 );
393 network_ChannelJoin( 8 );
396 network_ChannelJoin( 9 );
400 p_main->p_intf->b_menu_change = 1;
404 p_key = SDL_GetKeyName( event.key.keysym.sym ) ;
405 if( intf_ProcessKey( p_main->p_intf,
406 (char )event.key.keysym.sym ) )
408 intf_DbgMsg( "unhandled key '%c' (%i)",
409 (char)event.key.keysym.sym,
410 event.key.keysym.sym );
424 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
426 p_vout->p_sys->i_width = p_vout->i_width;
427 p_vout->p_sys->i_height = p_vout->i_height;
429 /* Need to reopen display */
430 SDLCloseDisplay( p_vout );
431 if( SDLOpenDisplay( p_vout ) )
433 intf_ErrMsg( "vout error: can't reopen display after resize" );
436 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
442 if( p_vout->i_changes & VOUT_YUV_CHANGE )
444 p_vout->b_need_render = ! p_vout->b_need_render;
446 /* Need to reopen display */
447 SDLCloseDisplay( p_vout );
448 if( SDLOpenDisplay( p_vout ) )
450 intf_ErrMsg( "error: can't reopen display after YUV change" );
453 p_vout->i_changes &= ~VOUT_YUV_CHANGE;
459 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
461 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
463 SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
465 p_vout->p_sys->b_cursor_autohidden = 0;
466 SDL_ShowCursor( p_vout->p_sys->b_cursor &&
467 ! p_vout->p_sys->b_cursor_autohidden );
469 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
475 if( ! p_vout->p_sys->b_cursor_autohidden &&
476 ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
478 /* Hide the mouse automatically */
479 p_vout->p_sys->b_cursor_autohidden = 1;
483 if( p_vout->i_changes & VOUT_CURSOR_CHANGE )
485 p_vout->p_sys->b_cursor = ! p_vout->p_sys->b_cursor;
487 SDL_ShowCursor( p_vout->p_sys->b_cursor &&
488 ! p_vout->p_sys->b_cursor_autohidden );
490 p_vout->i_changes &= ~VOUT_CURSOR_CHANGE;
496 /*****************************************************************************
497 * vout_SetPalette: sets an 8 bpp palette
498 *****************************************************************************
499 * This function sets the palette given as an argument. It does not return
500 * anything, but could later send information on which colors it was unable
502 *****************************************************************************/
503 static void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
504 u16 *blue, u16 *transp)
506 /* Create a display surface with a grayscale palette */
507 SDL_Color colors[256];
510 /* Fill colors with color information */
511 for( i = 0; i < 256; i++ )
513 colors[ i ].r = red[ i ] >> 8;
514 colors[ i ].g = green[ i ] >> 8;
515 colors[ i ].b = blue[ i ] >> 8;
519 if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
521 intf_ErrMsg( "vout error: failed setting palette" );
526 /*****************************************************************************
527 * vout_Display: displays previously rendered output
528 *****************************************************************************
529 * This function send the currently rendered image to the display, wait until
530 * it is displayed and switch the two rendering buffer, preparing next frame.
531 *****************************************************************************/
532 static void vout_Display( vout_thread_t *p_vout )
535 if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
537 if( p_vout->b_need_render )
539 /* Change display frame */
540 SDL_Flip( p_vout->p_sys->p_display );
545 * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
548 /* TODO: support for streams other than 4:2:0 */
549 /* create the overlay if necessary */
550 if( p_vout->p_sys->p_overlay == NULL )
552 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay(
553 p_vout->p_rendered_pic->i_width,
554 p_vout->p_rendered_pic->i_height,
556 p_vout->p_sys->p_display
558 intf_WarnMsg( 2, "vout: YUV acceleration %s",
559 p_vout->p_sys->p_overlay->hw_overlay
560 ? "activated" : "unavailable !" );
563 SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
564 /* copy the data into video buffers */
566 memcpy(p_vout->p_sys->p_overlay->pixels[0],
567 p_vout->p_rendered_pic->p_y,
568 p_vout->p_sys->p_overlay->h *
569 p_vout->p_sys->p_overlay->pitches[0]);
571 memcpy(p_vout->p_sys->p_overlay->pixels[1],
572 p_vout->p_rendered_pic->p_v,
573 p_vout->p_sys->p_overlay->h *
574 p_vout->p_sys->p_overlay->pitches[1] / 2);
576 memcpy(p_vout->p_sys->p_overlay->pixels[2],
577 p_vout->p_rendered_pic->p_u,
578 p_vout->p_sys->p_overlay->h *
579 p_vout->p_sys->p_overlay->pitches[2] / 2);
581 disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
582 disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
583 disp.x = (p_vout->i_width - disp.w)/2;
584 disp.y = (p_vout->i_height - disp.h)/2;
586 SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
587 SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
592 /* following functions are local */
594 /*****************************************************************************
595 * SDLOpenDisplay: open and initialize SDL device
596 *****************************************************************************
597 * Open and initialize display according to preferences specified in the vout
599 *****************************************************************************/
600 static int SDLOpenDisplay( vout_thread_t *p_vout )
602 SDL_Rect clipping_rect;
606 * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
609 /* init flags and cursor */
610 flags = SDL_ANYFORMAT | SDL_HWPALETTE;
612 if( p_vout->b_fullscreen )
614 flags |= SDL_FULLSCREEN;
618 flags |= SDL_RESIZABLE;
621 if( p_vout->b_need_render )
623 flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
627 flags |= SDL_SWSURFACE; /* save video memory */
630 bpp = SDL_VideoModeOK( p_vout->p_sys->i_width,
631 p_vout->p_sys->i_height,
632 p_vout->i_screen_depth, flags );
636 intf_ErrMsg( "vout error: no video mode available" );
640 p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
641 p_vout->p_sys->i_height,
644 if( p_vout->p_sys->p_display == NULL )
646 intf_ErrMsg( "vout error: cannot set video mode" );
650 SDL_LockSurface( p_vout->p_sys->p_display );
652 SDL_WM_SetCaption( VOUT_TITLE " (SDL output)",
653 VOUT_TITLE " (SDL output)" );
654 SDL_EventState(SDL_KEYUP , SDL_IGNORE); /* ignore keys up */
656 if( p_vout->b_need_render )
658 p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
659 SDL_Flip(p_vout->p_sys->p_display);
660 p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
661 SDL_Flip(p_vout->p_sys->p_display);
663 /* Set clipping for text */
666 clipping_rect.w = p_vout->p_sys->p_display->w;
667 clipping_rect.h = p_vout->p_sys->p_display->h;
668 SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
670 /* Set thread information */
671 p_vout->i_width = p_vout->p_sys->p_display->w;
672 p_vout->i_height = p_vout->p_sys->p_display->h;
673 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
675 p_vout->i_screen_depth =
676 p_vout->p_sys->p_display->format->BitsPerPixel;
677 p_vout->i_bytes_per_pixel =
678 p_vout->p_sys->p_display->format->BytesPerPixel;
680 p_vout->i_red_mask = p_vout->p_sys->p_display->format->Rmask;
681 p_vout->i_green_mask = p_vout->p_sys->p_display->format->Gmask;
682 p_vout->i_blue_mask = p_vout->p_sys->p_display->format->Bmask;
684 /* FIXME: palette in 8bpp ?? */
685 /* Set and initialize buffers */
686 vout_SetBuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
687 p_vout->p_sys->p_sdl_buf[ 1 ] );
691 p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
692 p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
694 /* Set thread information */
695 p_vout->i_width = p_vout->p_sys->p_display->w;
696 p_vout->i_height = p_vout->p_sys->p_display->h;
697 p_vout->i_bytes_per_line = p_vout->p_sys->p_display->pitch;
699 vout_SetBuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
700 p_vout->p_sys->p_sdl_buf[ 1 ] );
703 p_vout->p_sys->b_reopen_display = 0;
708 /*****************************************************************************
709 * SDLCloseDisplay: close and reset SDL device
710 *****************************************************************************
711 * This function returns all resources allocated by SDLOpenDisplay and restore
712 * the original state of the device.
713 *****************************************************************************/
714 static void SDLCloseDisplay( vout_thread_t *p_vout )
716 if( p_vout->p_sys->p_display != NULL )
718 if( p_vout->p_sys->p_overlay != NULL )
720 SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
721 p_vout->p_sys->p_overlay = NULL;
724 SDL_UnlockSurface ( p_vout->p_sys->p_display );
725 SDL_FreeSurface( p_vout->p_sys->p_display );
726 p_vout->p_sys->p_display = NULL;