1 /*****************************************************************************
2 * vout_sdl.c: SDL video output display method
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: vout_sdl.c,v 1.72 2001/12/19 18:14:23 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() */
38 #include <sys/types.h>
40 # include <netinet/in.h> /* BSD: struct in_addr */
43 #include SDL_INCLUDE_FILE
53 #include "video_output.h"
55 #include "interface.h"
58 #include "modules_export.h"
60 #define SDL_MAX_DIRECTBUFFERS 5
61 #define SDL_DEFAULT_BPP 16
63 /*****************************************************************************
64 * vout_sys_t: video output SDL method descriptor
65 *****************************************************************************
66 * This structure is part of the video output thread descriptor.
67 * It describes the SDL specific properties of an output thread.
68 *****************************************************************************/
69 typedef struct vout_sys_s
71 SDL_Surface * p_display; /* display device */
72 SDL_Overlay * p_overlay; /* An overlay we keep to grab the XVideo port */
78 boolean_t b_cursor_autohidden;
83 /*****************************************************************************
84 * picture_sys_t: direct buffer method descriptor
85 *****************************************************************************
86 * This structure is part of the picture descriptor, it describes the
87 * SDL specific properties of a direct buffer.
88 *****************************************************************************/
89 typedef struct picture_sys_s
91 SDL_Overlay *p_overlay;
95 /*****************************************************************************
97 *****************************************************************************/
98 static int vout_Probe ( probedata_t *p_data );
99 static int vout_Create ( struct vout_thread_s * );
100 static int vout_Init ( struct vout_thread_s * );
101 static void vout_End ( struct vout_thread_s * );
102 static void vout_Destroy ( struct vout_thread_s * );
103 static int vout_Manage ( struct vout_thread_s * );
104 static void vout_Display ( struct vout_thread_s *, struct picture_s * );
106 static int SDLOpenDisplay ( vout_thread_t *p_vout );
107 static void SDLCloseDisplay ( vout_thread_t *p_vout );
108 static int SDLNewPicture ( vout_thread_t *p_vout, picture_t *p_pic );
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 = NULL;
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( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
139 if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
147 /*****************************************************************************
148 * vout_Create: allocate SDL video thread output method
149 *****************************************************************************
150 * This function allocate and initialize a SDL vout method. It uses some of the
151 * vout properties to choose the correct mode, and change them according to the
152 * mode actually used.
153 *****************************************************************************/
154 static int vout_Create( vout_thread_t *p_vout )
156 /* Allocate structure */
157 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
158 if( p_vout->p_sys == NULL )
160 intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
164 /* Initialize library */
165 if( SDL_Init( SDL_INIT_VIDEO
167 /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
168 | SDL_INIT_EVENTTHREAD
171 /* In debug mode you may want vlc to dump a core instead of staying
173 | SDL_INIT_NOPARACHUTE
177 intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
178 free( p_vout->p_sys );
182 p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
183 p_vout->p_sys->b_cursor_autohidden = 0;
184 p_vout->p_sys->i_lastmoved = mdate();
186 if( p_vout->render.i_height * p_vout->render.i_aspect
187 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
189 p_vout->p_sys->i_width = p_vout->render.i_height
190 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
191 p_vout->p_sys->i_height = p_vout->render.i_height;
195 p_vout->p_sys->i_width = p_vout->render.i_width;
196 p_vout->p_sys->i_height = p_vout->render.i_width
197 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
200 if( p_vout->p_sys->i_width <= 300 && p_vout->p_sys->i_height <= 300 )
202 p_vout->p_sys->i_width <<= 1;
203 p_vout->p_sys->i_height <<= 1;
205 else if( p_vout->p_sys->i_width <= 400 && p_vout->p_sys->i_height <= 400 )
207 p_vout->p_sys->i_width += p_vout->p_sys->i_width >> 1;
208 p_vout->p_sys->i_height += p_vout->p_sys->i_height >> 1;
211 if( SDLOpenDisplay( p_vout ) )
213 intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
214 SDL_QuitSubSystem( SDL_INIT_VIDEO );
215 free( p_vout->p_sys );
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 )
232 I_OUTPUTPICTURES = 0;
234 /* Initialize the output structure */
235 switch( p_vout->render.i_chroma )
237 case YUV_420_PICTURE:
238 p_vout->output.i_chroma = p_vout->render.i_chroma;
239 p_vout->output.i_width = p_vout->render.i_width;
240 p_vout->output.i_height = p_vout->render.i_height;
241 p_vout->output.i_aspect = p_vout->render.i_aspect;
248 /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
249 while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
253 /* Find an empty picture slot */
254 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
256 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
258 p_pic = p_vout->p_picture + i_index;
263 /* Allocate the picture if we found one */
264 if( p_pic == NULL || SDLNewPicture( p_vout, p_pic ) )
269 p_pic->i_status = DESTROYED_PICTURE;
270 p_pic->i_type = DIRECT_PICTURE;
272 p_pic->i_left_margin =
273 p_pic->i_right_margin =
274 p_pic->i_top_margin =
275 p_pic->i_bottom_margin = 0;
277 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
285 /*****************************************************************************
286 * vout_End: terminate Sys video thread output method
287 *****************************************************************************
288 * Terminate an output method created by vout_SDLCreate
289 *****************************************************************************/
290 static void vout_End( vout_thread_t *p_vout )
294 /* Free the output buffers we allocated */
295 for( i_index = I_OUTPUTPICTURES ; i_index ; )
298 SDL_UnlockYUVOverlay( PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
299 SDL_FreeYUVOverlay( PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
300 free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
304 /*****************************************************************************
305 * vout_Destroy: destroy Sys video thread output method
306 *****************************************************************************
307 * Terminate an output method created by vout_SDLCreate
308 *****************************************************************************/
309 static void vout_Destroy( vout_thread_t *p_vout )
311 SDLCloseDisplay( p_vout );
313 SDL_QuitSubSystem( SDL_INIT_VIDEO );
315 free( p_vout->p_sys );
318 /*****************************************************************************
319 * vout_Manage: handle Sys events
320 *****************************************************************************
321 * This function should be called regularly by video output thread. It returns
322 * a non null value if an error occured.
323 *****************************************************************************/
324 static int vout_Manage( vout_thread_t *p_vout )
326 SDL_Event event; /* SDL event */
329 while( SDL_PollEvent(&event) )
333 case SDL_VIDEORESIZE: /* Resizing of window */
334 p_vout->p_sys->i_width = event.resize.w;
335 p_vout->p_sys->i_height = event.resize.h;
336 SDLCloseDisplay( p_vout );
337 SDLOpenDisplay( p_vout );
340 case SDL_MOUSEMOTION:
341 if( p_vout->p_sys->b_cursor &&
342 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
344 if( p_vout->p_sys->b_cursor_autohidden )
346 p_vout->p_sys->b_cursor_autohidden = 0;
351 p_vout->p_sys->i_lastmoved = mdate();
356 case SDL_MOUSEBUTTONUP:
357 switch( event.button.button )
359 case SDL_BUTTON_RIGHT:
360 p_main->p_intf->b_menu_change = 1;
365 case SDL_MOUSEBUTTONDOWN:
366 switch( event.button.button )
368 case SDL_BUTTON_LEFT:
375 p_main->p_intf->b_die = 1;
378 case SDL_KEYDOWN: /* if a key is pressed */
380 switch( event.key.keysym.sym )
382 case SDLK_q: /* quit */
384 p_main->p_intf->b_die = 1;
387 case SDLK_f: /* switch to fullscreen */
388 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
391 case SDLK_c: /* toggle grayscale */
392 p_vout->b_grayscale = ! p_vout->b_grayscale;
393 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
396 case SDLK_i: /* toggle info */
397 p_vout->b_info = ! p_vout->b_info;
398 p_vout->i_changes |= VOUT_INFO_CHANGE;
401 case SDLK_s: /* toggle scaling */
402 p_vout->b_scale = ! p_vout->b_scale;
403 p_vout->i_changes |= VOUT_SCALE_CHANGE;
406 case SDLK_SPACE: /* toggle interface */
407 p_vout->b_interface = ! p_vout->b_interface;
408 p_vout->i_changes |= VOUT_INTF_CHANGE;
412 p_main->p_intf->b_menu_change = 1;
415 case SDLK_F10: network_ChannelJoin( 0 ); break;
416 case SDLK_F1: network_ChannelJoin( 1 ); break;
417 case SDLK_F2: network_ChannelJoin( 2 ); break;
418 case SDLK_F3: network_ChannelJoin( 3 ); break;
419 case SDLK_F4: network_ChannelJoin( 4 ); break;
420 case SDLK_F5: network_ChannelJoin( 5 ); break;
421 case SDLK_F6: network_ChannelJoin( 6 ); break;
422 case SDLK_F7: network_ChannelJoin( 7 ); break;
423 case SDLK_F8: network_ChannelJoin( 8 ); break;
424 case SDLK_F9: network_ChannelJoin( 9 ); break;
427 intf_DbgMsg( "unhandled key %i", event.key.keysym.sym );
437 /* Fullscreen change */
438 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
440 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
442 SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
444 p_vout->p_sys->b_cursor_autohidden = 0;
445 SDL_ShowCursor( p_vout->p_sys->b_cursor &&
446 ! p_vout->p_sys->b_cursor_autohidden );
448 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
452 if( ! p_vout->p_sys->b_cursor_autohidden &&
453 ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
455 /* Hide the mouse automatically */
456 p_vout->p_sys->b_cursor_autohidden = 1;
463 /*****************************************************************************
464 * vout_Display: displays previously rendered output
465 *****************************************************************************
466 * This function sends the currently rendered image to the display.
467 *****************************************************************************/
468 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
473 vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
480 SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
481 SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
482 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
485 /* following functions are local */
487 /*****************************************************************************
488 * SDLOpenDisplay: open and initialize SDL device
489 *****************************************************************************
490 * Open and initialize display according to preferences specified in the vout
492 *****************************************************************************/
493 static int SDLOpenDisplay( vout_thread_t *p_vout )
498 /* Initialize flags and cursor */
499 i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE;
500 i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
502 i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
503 SDL_DEFAULT_BPP, i_flags );
506 intf_ErrMsg( "vout error: no video mode available" );
510 p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
511 p_vout->p_sys->i_height,
514 if( p_vout->p_sys->p_display == NULL )
516 intf_ErrMsg( "vout error: cannot set video mode" );
520 SDL_LockSurface( p_vout->p_sys->p_display );
522 p_vout->p_sys->p_overlay =
523 SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
524 p_vout->p_sys->p_display );
526 if( p_vout->p_sys->p_overlay == NULL )
528 intf_ErrMsg( "vout error: cannot set overlay" );
529 SDL_UnlockSurface( p_vout->p_sys->p_display );
530 SDL_FreeSurface( p_vout->p_sys->p_display );
534 if( p_vout->p_sys->p_overlay->hw_overlay )
536 SDL_WM_SetCaption( VOUT_TITLE " (hardware SDL output)",
537 VOUT_TITLE " (hardware SDL output)" );
541 SDL_WM_SetCaption( VOUT_TITLE " (software SDL output)",
542 VOUT_TITLE " (software SDL output)" );
545 SDL_EventState( SDL_KEYUP, SDL_IGNORE ); /* ignore keys up */
550 /*****************************************************************************
551 * SDLCloseDisplay: close and reset SDL device
552 *****************************************************************************
553 * This function returns all resources allocated by SDLOpenDisplay and restore
554 * the original state of the device.
555 *****************************************************************************/
556 static void SDLCloseDisplay( vout_thread_t *p_vout )
558 SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
559 SDL_UnlockSurface ( p_vout->p_sys->p_display );
560 SDL_FreeSurface( p_vout->p_sys->p_display );
563 /*****************************************************************************
564 * SDLNewPicture: allocate a picture
565 *****************************************************************************
566 * Returns 0 on success, -1 otherwise
567 *****************************************************************************/
568 static int SDLNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
570 int i_width = p_vout->output.i_width;
571 int i_height = p_vout->output.i_height;
573 switch( p_vout->output.i_chroma )
575 case YUV_420_PICTURE:
576 /* We know this chroma, allocate a buffer which will be used
577 * directly by the decoder */
578 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
580 if( p_pic->p_sys == NULL )
585 p_pic->p_sys->p_overlay =
586 SDL_CreateYUVOverlay( i_width, i_height,
588 p_vout->p_sys->p_display );
590 if( p_pic->p_sys->p_overlay == NULL )
592 free( p_pic->p_sys );
596 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
598 /* Precalculate some values */
599 p_pic->i_size = i_width * i_height;
600 p_pic->i_chroma_width = i_width / 2;
601 p_pic->i_chroma_size = i_height * ( i_width / 2 );
603 /* FIXME: try to get the right i_bytes value from p_overlay */
604 p_pic->planes[ Y_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 0 ];
605 p_pic->planes[ Y_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 );
606 p_pic->planes[ Y_PLANE ].i_line_bytes = i_width * sizeof( u8 );
608 p_pic->planes[ U_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 2 ];
609 p_pic->planes[ U_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 ) / 4;
610 p_pic->planes[ U_PLANE ].i_line_bytes = p_pic->i_chroma_width * sizeof( u8 );
612 p_pic->planes[ V_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 1 ];
613 p_pic->planes[ V_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 ) / 4;
614 p_pic->planes[ V_PLANE ].i_line_bytes = p_pic->i_chroma_width * sizeof( u8 );
621 /* Unknown chroma, tell the guy to get lost */