1 /*****************************************************************************
2 * vout_sdl.c: SDL video output display method
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: vout_sdl.c,v 1.70 2001/12/16 16:18:36 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
52 #include "video_output.h"
54 #include "interface.h"
57 #include "modules_export.h"
59 #define SDL_MAX_DIRECTBUFFERS 5
60 #define SDL_DEFAULT_BPP 16
62 /*****************************************************************************
63 * vout_sys_t: video output SDL method descriptor
64 *****************************************************************************
65 * This structure is part of the video output thread descriptor.
66 * It describes the SDL specific properties of an output thread.
67 *****************************************************************************/
68 typedef struct vout_sys_s
70 SDL_Surface * p_display; /* display device */
76 boolean_t b_cursor_autohidden;
81 /*****************************************************************************
82 * picture_sys_t: direct buffer method descriptor
83 *****************************************************************************
84 * This structure is part of the picture descriptor, it describes the
85 * SDL specific properties of a direct buffer.
86 *****************************************************************************/
87 typedef struct picture_sys_s
89 SDL_Overlay *p_overlay;
93 /*****************************************************************************
95 *****************************************************************************/
96 static int vout_Probe ( probedata_t *p_data );
97 static int vout_Create ( struct vout_thread_s * );
98 static int vout_Init ( struct vout_thread_s * );
99 static void vout_End ( struct vout_thread_s * );
100 static void vout_Destroy ( struct vout_thread_s * );
101 static int vout_Manage ( struct vout_thread_s * );
102 static void vout_Display ( struct vout_thread_s *, struct picture_s * );
104 static int SDLOpenDisplay ( vout_thread_t *p_vout );
105 static void SDLCloseDisplay ( vout_thread_t *p_vout );
106 static int SDLNewPicture ( vout_thread_t *p_vout, picture_t *p_pic );
108 /*****************************************************************************
109 * Functions exported as capabilities. They are declared as static so that
110 * we don't pollute the namespace too much.
111 *****************************************************************************/
112 void _M( vout_getfunctions )( function_list_t * p_function_list )
114 p_function_list->pf_probe = vout_Probe;
115 p_function_list->functions.vout.pf_create = vout_Create;
116 p_function_list->functions.vout.pf_init = vout_Init;
117 p_function_list->functions.vout.pf_end = vout_End;
118 p_function_list->functions.vout.pf_destroy = vout_Destroy;
119 p_function_list->functions.vout.pf_manage = vout_Manage;
120 p_function_list->functions.vout.pf_display = vout_Display;
121 p_function_list->functions.vout.pf_setpalette = NULL;
124 /*****************************************************************************
125 * vout_Probe: probe the video driver and return a score
126 *****************************************************************************
127 * This function tries to initialize SDL and returns a score to the
128 * plugin manager so that it can select the best plugin.
129 *****************************************************************************/
130 static int vout_Probe( probedata_t *p_data )
132 if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
140 /*****************************************************************************
141 * vout_Create: allocate SDL video thread output method
142 *****************************************************************************
143 * This function allocate and initialize a SDL vout method. It uses some of the
144 * vout properties to choose the correct mode, and change them according to the
145 * mode actually used.
146 *****************************************************************************/
147 static int vout_Create( vout_thread_t *p_vout )
149 /* Allocate structure */
150 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
151 if( p_vout->p_sys == NULL )
153 intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
157 /* Initialize library */
158 if( SDL_Init( SDL_INIT_VIDEO
160 /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
161 | SDL_INIT_EVENTTHREAD
164 /* In debug mode you may want vlc to dump a core instead of staying
166 | SDL_INIT_NOPARACHUTE
170 intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
171 free( p_vout->p_sys );
175 p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
176 p_vout->p_sys->b_cursor_autohidden = 0;
177 p_vout->p_sys->i_lastmoved = mdate();
179 if( p_vout->render.i_height * p_vout->render.i_aspect
180 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
182 p_vout->p_sys->i_width = p_vout->render.i_height
183 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
184 p_vout->p_sys->i_height = p_vout->render.i_height;
188 p_vout->p_sys->i_width = p_vout->render.i_width;
189 p_vout->p_sys->i_height = p_vout->render.i_width
190 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
193 if( p_vout->p_sys->i_width <= 400 && p_vout->p_sys->i_height <= 300 )
195 p_vout->p_sys->i_width *= 2;
196 p_vout->p_sys->i_height *= 2;
199 if( SDLOpenDisplay( p_vout ) )
201 intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
202 free( p_vout->p_sys );
209 /*****************************************************************************
210 * vout_Init: initialize SDL video thread output method
211 *****************************************************************************
212 * This function initialize the SDL display device.
213 *****************************************************************************/
214 static int vout_Init( vout_thread_t *p_vout )
219 I_OUTPUTPICTURES = 0;
221 /* Initialize the output structure */
222 switch( p_vout->render.i_chroma )
224 case YUV_420_PICTURE:
225 p_vout->output.i_chroma = p_vout->render.i_chroma;
226 p_vout->output.i_width = p_vout->render.i_width;
227 p_vout->output.i_height = p_vout->render.i_height;
228 p_vout->output.i_aspect = p_vout->render.i_aspect;
235 /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
236 while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
240 /* Find an empty picture slot */
241 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
243 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
245 p_pic = p_vout->p_picture + i_index;
250 /* Allocate the picture */
251 if( SDLNewPicture( p_vout, p_pic ) )
256 p_pic->i_status = DESTROYED_PICTURE;
257 p_pic->i_type = DIRECT_PICTURE;
259 p_pic->i_left_margin =
260 p_pic->i_right_margin =
261 p_pic->i_top_margin =
262 p_pic->i_bottom_margin = 0;
264 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
272 /*****************************************************************************
273 * vout_End: terminate Sys video thread output method
274 *****************************************************************************
275 * Terminate an output method created by vout_SDLCreate
276 *****************************************************************************/
277 static void vout_End( vout_thread_t *p_vout )
281 /* Free the output buffers we allocated */
282 for( i_index = I_OUTPUTPICTURES ; i_index ; )
285 SDL_UnlockYUVOverlay( PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
286 SDL_FreeYUVOverlay( PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
287 free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
291 /*****************************************************************************
292 * vout_Destroy: destroy Sys video thread output method
293 *****************************************************************************
294 * Terminate an output method created by vout_SDLCreate
295 *****************************************************************************/
296 static void vout_Destroy( vout_thread_t *p_vout )
298 SDLCloseDisplay( p_vout );
300 SDL_QuitSubSystem( SDL_INIT_VIDEO );
302 free( p_vout->p_sys );
305 /*****************************************************************************
306 * vout_Manage: handle Sys events
307 *****************************************************************************
308 * This function should be called regularly by video output thread. It returns
309 * a non null value if an error occured.
310 *****************************************************************************/
311 static int vout_Manage( vout_thread_t *p_vout )
313 SDL_Event event; /* SDL event */
316 while( SDL_PollEvent(&event) )
320 case SDL_VIDEORESIZE: /* Resizing of window */
321 p_vout->p_sys->i_width = event.resize.w;
322 p_vout->p_sys->i_height = event.resize.h;
323 SDLCloseDisplay( p_vout );
324 SDLOpenDisplay( p_vout );
327 case SDL_MOUSEMOTION:
328 if( p_vout->p_sys->b_cursor &&
329 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
331 if( p_vout->p_sys->b_cursor_autohidden )
333 p_vout->p_sys->b_cursor_autohidden = 0;
338 p_vout->p_sys->i_lastmoved = mdate();
343 case SDL_MOUSEBUTTONUP:
344 switch( event.button.button )
346 case SDL_BUTTON_RIGHT:
347 p_main->p_intf->b_menu_change = 1;
352 case SDL_MOUSEBUTTONDOWN:
353 switch( event.button.button )
355 case SDL_BUTTON_LEFT:
362 p_main->p_intf->b_die = 1;
365 case SDL_KEYDOWN: /* if a key is pressed */
367 switch( event.key.keysym.sym )
369 case SDLK_q: /* quit */
371 p_main->p_intf->b_die = 1;
374 case SDLK_f: /* switch to fullscreen */
375 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
378 case SDLK_c: /* toggle grayscale */
379 p_vout->b_grayscale = ! p_vout->b_grayscale;
380 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
383 case SDLK_i: /* toggle info */
384 p_vout->b_info = ! p_vout->b_info;
385 p_vout->i_changes |= VOUT_INFO_CHANGE;
388 case SDLK_s: /* toggle scaling */
389 p_vout->b_scale = ! p_vout->b_scale;
390 p_vout->i_changes |= VOUT_SCALE_CHANGE;
393 case SDLK_SPACE: /* toggle interface */
394 p_vout->b_interface = ! p_vout->b_interface;
395 p_vout->i_changes |= VOUT_INTF_CHANGE;
399 p_main->p_intf->b_menu_change = 1;
402 case SDLK_F10: network_ChannelJoin( 0 ); break;
403 case SDLK_F1: network_ChannelJoin( 1 ); break;
404 case SDLK_F2: network_ChannelJoin( 2 ); break;
405 case SDLK_F3: network_ChannelJoin( 3 ); break;
406 case SDLK_F4: network_ChannelJoin( 4 ); break;
407 case SDLK_F5: network_ChannelJoin( 5 ); break;
408 case SDLK_F6: network_ChannelJoin( 6 ); break;
409 case SDLK_F7: network_ChannelJoin( 7 ); break;
410 case SDLK_F8: network_ChannelJoin( 8 ); break;
411 case SDLK_F9: network_ChannelJoin( 9 ); break;
414 intf_DbgMsg( "unhandled key %i", event.key.keysym.sym );
424 /* Fullscreen change */
425 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
427 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
429 SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
431 p_vout->p_sys->b_cursor_autohidden = 0;
432 SDL_ShowCursor( p_vout->p_sys->b_cursor &&
433 ! p_vout->p_sys->b_cursor_autohidden );
435 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
439 if( ! p_vout->p_sys->b_cursor_autohidden &&
440 ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
442 /* Hide the mouse automatically */
443 p_vout->p_sys->b_cursor_autohidden = 1;
450 /*****************************************************************************
451 * vout_Display: displays previously rendered output
452 *****************************************************************************
453 * This function sends the currently rendered image to the display.
454 *****************************************************************************/
455 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
460 vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
467 SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
468 SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
469 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
472 /* following functions are local */
474 /*****************************************************************************
475 * SDLOpenDisplay: open and initialize SDL device
476 *****************************************************************************
477 * Open and initialize display according to preferences specified in the vout
479 *****************************************************************************/
480 static int SDLOpenDisplay( vout_thread_t *p_vout )
485 /* Initialize flags and cursor */
486 i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE;
487 i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
489 i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
490 SDL_DEFAULT_BPP, i_flags );
493 intf_ErrMsg( "vout error: no video mode available" );
497 p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
498 p_vout->p_sys->i_height,
501 if( p_vout->p_sys->p_display == NULL )
503 intf_ErrMsg( "vout error: cannot set video mode" );
507 SDL_LockSurface( p_vout->p_sys->p_display );
509 SDL_WM_SetCaption( VOUT_TITLE " (SDL output)",
510 VOUT_TITLE " (SDL output)" );
511 SDL_EventState( SDL_KEYUP, SDL_IGNORE ); /* ignore keys up */
516 /*****************************************************************************
517 * SDLCloseDisplay: close and reset SDL device
518 *****************************************************************************
519 * This function returns all resources allocated by SDLOpenDisplay and restore
520 * the original state of the device.
521 *****************************************************************************/
522 static void SDLCloseDisplay( vout_thread_t *p_vout )
524 SDL_UnlockSurface ( p_vout->p_sys->p_display );
525 SDL_FreeSurface( p_vout->p_sys->p_display );
528 /*****************************************************************************
529 * SDLNewPicture: allocate a picture
530 *****************************************************************************
531 * Returns 0 on success, -1 otherwise
532 *****************************************************************************/
533 static int SDLNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
535 int i_width = p_vout->output.i_width;
536 int i_height = p_vout->output.i_height;
538 switch( p_vout->output.i_chroma )
540 case YUV_420_PICTURE:
541 /* We know this chroma, allocate a buffer which will be used
542 * directly by the decoder */
543 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
545 if( p_pic->p_sys == NULL )
550 p_pic->p_sys->p_overlay =
551 SDL_CreateYUVOverlay( i_width, i_height,
553 p_vout->p_sys->p_display );
555 if( p_pic->p_sys->p_overlay == NULL )
557 free( p_pic->p_sys );
561 SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
563 /* Precalculate some values */
564 p_pic->i_size = i_width * i_height;
565 p_pic->i_chroma_width = i_width / 2;
566 p_pic->i_chroma_size = i_height * ( i_width / 2 );
568 /* FIXME: try to get the right i_bytes value from p_overlay */
569 p_pic->planes[ Y_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 0 ];
570 p_pic->planes[ Y_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 );
571 p_pic->planes[ Y_PLANE ].i_line_bytes = i_width * sizeof( u8 );
573 p_pic->planes[ U_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 2 ];
574 p_pic->planes[ U_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 ) / 4;
575 p_pic->planes[ U_PLANE ].i_line_bytes = p_pic->i_chroma_width * sizeof( u8 );
577 p_pic->planes[ V_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 1 ];
578 p_pic->planes[ V_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 ) / 4;
579 p_pic->planes[ V_PLANE ].i_line_bytes = p_pic->i_chroma_width * sizeof( u8 );
586 /* Unknown chroma, tell the guy to get lost */