]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
* COMPLETE CVS BREAKAGE !! The MAIN branch is going to be a playground
[vlc] / plugins / sdl / vout_sdl.c
1 /*****************************************************************************
2  * vout_sdl.c: SDL video output display method
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id: vout_sdl.c,v 1.68 2001/12/09 17:01:37 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Pierre Baillet <oct@zoy.org>
9  *          Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
10  *
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.
15  * 
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.
20  *
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  *****************************************************************************/
25
26 #define MODULE_NAME sdl
27 #include "modules_inner.h"
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include "defs.h"
33
34 #include <errno.h>                                                 /* ENOMEM */
35 #include <stdlib.h>                                                /* free() */
36 #include <string.h>                                            /* strerror() */
37
38 #include <sys/types.h>
39 #ifndef WIN32
40 #   include <netinet/in.h>                            /* BSD: struct in_addr */
41 #endif
42
43 #include SDL_INCLUDE_FILE
44
45 #include "common.h"
46 #include "intf_msg.h"
47 #include "threads.h"
48 #include "mtime.h"
49 #include "tests.h"
50
51 #include "video.h"
52 #include "video_output.h"
53
54 #include "interface.h"
55
56 #include "modules.h"
57 #include "modules_export.h"
58
59 #define SDL_MAX_DIRECTBUFFERS 5
60 #define SDL_DEFAULT_BPP 16
61
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
69 {
70     SDL_Surface *   p_display;                             /* display device */
71
72     int i_width;
73     int i_height;
74
75     boolean_t   b_cursor;
76     boolean_t   b_cursor_autohidden;
77     mtime_t     i_lastmoved;
78
79 } vout_sys_t;
80
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
88 {
89     SDL_Overlay *p_overlay;
90
91 } picture_sys_t;
92
93 /*****************************************************************************
94  * Local prototypes.
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 * );
103
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 );
107
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 )
113 {
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;
122 }
123
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 )
131 {
132     if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
133     {
134         return( 999 );
135     }
136
137     return( 100 );
138 }
139
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 )
148 {
149     /* Allocate structure */
150     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
151     if( p_vout->p_sys == NULL )
152     {
153         intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
154         return( 1 );
155     }
156
157     /* Initialize library */
158     if( SDL_Init( SDL_INIT_VIDEO
159 #ifndef WIN32
160     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
161                 | SDL_INIT_EVENTTHREAD
162 #endif
163 #ifdef DEBUG
164     /* In debug mode you may want vlc to dump a core instead of staying
165      * stuck */
166                 | SDL_INIT_NOPARACHUTE
167 #endif
168                 ) < 0 )
169     {
170         intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
171         free( p_vout->p_sys );
172         return( 1 );
173     }
174
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();
178
179     p_vout->p_sys->i_width = p_vout->i_width;
180     p_vout->p_sys->i_height = p_vout->i_height;
181
182     if( SDLOpenDisplay( p_vout ) )
183     {
184         intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
185         free( p_vout->p_sys );
186         return( 1 );
187     }
188
189     return( 0 );
190 }
191
192 /*****************************************************************************
193  * vout_Init: initialize SDL video thread output method
194  *****************************************************************************
195  * This function initialize the SDL display device.
196  *****************************************************************************/
197 static int vout_Init( vout_thread_t *p_vout )
198 {
199     picture_t *p_pic;
200     int        i_index = 0;
201
202     /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
203     while( i_index < SDL_MAX_DIRECTBUFFERS )
204     {
205         p_pic = &p_vout->p_picture[ i_index ];
206
207         if( SDLNewPicture( p_vout, p_pic ) )
208         {
209             break;
210         }
211
212         p_pic->i_status        = DESTROYED_PICTURE;
213
214         p_pic->b_directbuffer  = 1;
215
216         p_pic->i_left_margin   =
217         p_pic->i_right_margin  =
218         p_pic->i_top_margin    =
219         p_pic->i_bottom_margin = 0;
220
221         i_index++;
222     }
223
224     /* How many directbuffers did we create ? */
225     p_vout->i_directbuffers = i_index;
226
227     return( 0 );
228 }
229
230 /*****************************************************************************
231  * vout_End: terminate Sys video thread output method
232  *****************************************************************************
233  * Terminate an output method created by vout_SDLCreate
234  *****************************************************************************/
235 static void vout_End( vout_thread_t *p_vout )
236 {
237     int i_index;
238
239     /* Free the direct buffers we allocated */
240     for( i_index = p_vout->i_directbuffers ; i_index ; )
241     {
242         i_index--;
243         SDL_UnlockYUVOverlay( p_vout->p_picture[ i_index ].p_sys->p_overlay );
244         SDL_FreeYUVOverlay( p_vout->p_picture[ i_index ].p_sys->p_overlay );
245         free( p_vout->p_picture[ i_index ].p_sys );
246     }
247 }
248
249 /*****************************************************************************
250  * vout_Destroy: destroy Sys video thread output method
251  *****************************************************************************
252  * Terminate an output method created by vout_SDLCreate
253  *****************************************************************************/
254 static void vout_Destroy( vout_thread_t *p_vout )
255 {
256     SDLCloseDisplay( p_vout );
257
258     SDL_QuitSubSystem( SDL_INIT_VIDEO );
259
260     free( p_vout->p_sys );
261 }
262
263 /*****************************************************************************
264  * vout_Manage: handle Sys events
265  *****************************************************************************
266  * This function should be called regularly by video output thread. It returns
267  * a non null value if an error occured.
268  *****************************************************************************/
269 static int vout_Manage( vout_thread_t *p_vout )
270 {
271     SDL_Event event;                                            /* SDL event */
272
273     /* Process events */
274     while( SDL_PollEvent(&event) )
275     {
276         switch( event.type )
277         {
278         case SDL_VIDEORESIZE:                          /* Resizing of window */
279             p_vout->p_sys->i_width = event.resize.w;
280             p_vout->p_sys->i_height = event.resize.h;
281             SDLCloseDisplay( p_vout );
282             SDLOpenDisplay( p_vout );
283             break;
284
285         case SDL_MOUSEMOTION:
286             if( p_vout->p_sys->b_cursor &&
287                 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
288             {
289                 if( p_vout->p_sys->b_cursor_autohidden )
290                 {
291                     p_vout->p_sys->b_cursor_autohidden = 0;
292                     SDL_ShowCursor( 1 );
293                 }
294                 else
295                 {
296                     p_vout->p_sys->i_lastmoved = mdate();
297                 }
298             }
299             break;
300
301         case SDL_MOUSEBUTTONUP:
302             switch( event.button.button )
303             {
304             case SDL_BUTTON_RIGHT:
305                 p_main->p_intf->b_menu_change = 1;
306                 break;
307             }
308             break;
309
310         case SDL_MOUSEBUTTONDOWN:
311             switch( event.button.button )
312             {
313             case SDL_BUTTON_LEFT:
314                 /* Handle clicks */
315                 break;
316             }
317             break;
318
319         case SDL_QUIT:
320             p_main->p_intf->b_die = 1;
321             break;
322
323         case SDL_KEYDOWN:                             /* if a key is pressed */
324
325             switch( event.key.keysym.sym )
326             {
327             case SDLK_q:                                             /* quit */
328             case SDLK_ESCAPE:
329                 p_main->p_intf->b_die = 1;
330                 break;
331
332             case SDLK_f:                             /* switch to fullscreen */
333                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
334                 break;
335
336             case SDLK_c:                                 /* toggle grayscale */
337                 p_vout->b_grayscale = ! p_vout->b_grayscale;
338                 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
339                 break;
340
341             case SDLK_i:                                      /* toggle info */
342                 p_vout->b_info = ! p_vout->b_info;
343                 p_vout->i_changes |= VOUT_INFO_CHANGE;
344                 break;
345
346             case SDLK_s:                                   /* toggle scaling */
347                 p_vout->b_scale = ! p_vout->b_scale;
348                 p_vout->i_changes |= VOUT_SCALE_CHANGE;
349                 break;
350
351             case SDLK_SPACE:                             /* toggle interface */
352                 p_vout->b_interface = ! p_vout->b_interface;
353                 p_vout->i_changes |= VOUT_INTF_CHANGE;
354                 break;
355             
356             case SDLK_MENU:
357                 p_main->p_intf->b_menu_change = 1;
358                 break;
359                 
360             case SDLK_F10: network_ChannelJoin( 0 ); break;
361             case SDLK_F1:  network_ChannelJoin( 1 ); break;
362             case SDLK_F2:  network_ChannelJoin( 2 ); break;
363             case SDLK_F3:  network_ChannelJoin( 3 ); break;
364             case SDLK_F4:  network_ChannelJoin( 4 ); break;
365             case SDLK_F5:  network_ChannelJoin( 5 ); break;
366             case SDLK_F6:  network_ChannelJoin( 6 ); break;
367             case SDLK_F7:  network_ChannelJoin( 7 ); break;
368             case SDLK_F8:  network_ChannelJoin( 8 ); break;
369             case SDLK_F9:  network_ChannelJoin( 9 ); break;
370
371             default:
372                 intf_DbgMsg( "unhandled key %i", event.key.keysym.sym );
373                 break;
374             }
375             break;
376
377         default:
378             break;
379         }
380     }
381
382     /* Fullscreen change */
383     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
384     {
385         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
386
387         SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
388
389         p_vout->p_sys->b_cursor_autohidden = 0;
390         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
391                         ! p_vout->p_sys->b_cursor_autohidden );
392
393         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
394     }
395
396     /* Pointer change */
397     if( ! p_vout->p_sys->b_cursor_autohidden &&
398         ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
399     {
400         /* Hide the mouse automatically */
401         p_vout->p_sys->b_cursor_autohidden = 1;
402         SDL_ShowCursor( 0 );
403     }
404
405     return( 0 );
406 }
407
408 /*****************************************************************************
409  * vout_Display: displays previously rendered output
410  *****************************************************************************
411  * This function sends the currently rendered image to the display.
412  *****************************************************************************/
413 static void vout_Display( vout_thread_t *p_vout, picture_t *p_picture )
414 {
415     SDL_Rect disp;
416
417     /* We'll need to deal with aspect ratio later */
418     disp.w = p_vout->p_sys->i_width;
419     disp.h = p_vout->p_sys->i_height;
420     disp.x = 0;
421     disp.y = 0;
422
423     if( p_picture->b_directbuffer )
424     {
425         SDL_UnlockYUVOverlay( p_picture->p_sys->p_overlay);
426         SDL_DisplayYUVOverlay( p_picture->p_sys->p_overlay , &disp );
427         SDL_LockYUVOverlay( p_picture->p_sys->p_overlay);
428     }
429     else
430     {
431         intf_ErrMsg( "vout error: main thread passed a virtual buffer" );
432     }
433 }
434
435 /* following functions are local */
436
437 /*****************************************************************************
438  * SDLOpenDisplay: open and initialize SDL device
439  *****************************************************************************
440  * Open and initialize display according to preferences specified in the vout
441  * thread fields.
442  *****************************************************************************/
443 static int SDLOpenDisplay( vout_thread_t *p_vout )
444 {
445     Uint32 i_flags;
446     int    i_bpp;
447
448     /* Initialize flags and cursor */
449     i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE;
450     i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
451
452     i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
453                              SDL_DEFAULT_BPP, i_flags );
454     if( i_bpp == 0 )
455     {
456         intf_ErrMsg( "vout error: no video mode available" );
457         return( 1 );
458     }
459
460     p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
461                                                  p_vout->p_sys->i_height,
462                                                  i_bpp, i_flags );
463
464     if( p_vout->p_sys->p_display == NULL )
465     {
466         intf_ErrMsg( "vout error: cannot set video mode" );
467         return( 1 );
468     }
469
470     SDL_LockSurface( p_vout->p_sys->p_display );
471
472     SDL_WM_SetCaption( VOUT_TITLE " (SDL output)",
473                        VOUT_TITLE " (SDL output)" );
474     SDL_EventState( SDL_KEYUP, SDL_IGNORE );               /* ignore keys up */
475
476     return( 0 );
477 }
478
479 /*****************************************************************************
480  * SDLCloseDisplay: close and reset SDL device
481  *****************************************************************************
482  * This function returns all resources allocated by SDLOpenDisplay and restore
483  * the original state of the device.
484  *****************************************************************************/
485 static void SDLCloseDisplay( vout_thread_t *p_vout )
486 {
487     SDL_UnlockSurface ( p_vout->p_sys->p_display );
488     SDL_FreeSurface( p_vout->p_sys->p_display );
489 }
490
491 /*****************************************************************************
492  * SDLNewPicture: allocate a picture
493  *****************************************************************************
494  * Returns 0 on success, -1 otherwise
495  *****************************************************************************/
496 static int SDLNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
497 {
498     switch( p_vout->i_chroma )
499     {
500         case YUV_420_PICTURE:
501             /* We know this chroma, allocate a buffer which will be used
502              * directly by the decoder */
503             p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
504
505             if( p_pic->p_sys == NULL )
506             {
507                 return -1;
508             }
509
510             p_pic->p_sys->p_overlay =
511                 SDL_CreateYUVOverlay( p_vout->i_width, p_vout->i_height,
512                                       SDL_YV12_OVERLAY,
513                                       p_vout->p_sys->p_display );
514
515             if( p_pic->p_sys->p_overlay == NULL )
516             {
517                 free( p_pic->p_sys );
518                 return -1;
519             }
520
521             SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
522
523             p_pic->i_chroma = p_vout->i_chroma; /* YUV_420_PICTURE */
524             p_pic->i_width  = p_vout->i_width;
525             p_pic->i_height = p_vout->i_height;
526
527             /* Precalculate some values */
528             p_pic->i_size         = p_vout->i_width * p_vout->i_height;
529             p_pic->i_chroma_width = p_vout->i_width / 2;
530             p_pic->i_chroma_size  = p_vout->i_height * p_pic->i_chroma_width;
531
532             /* FIXME: try to get the right i_bytes value from p_overlay */
533             p_pic->planes[ Y_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[0];
534             p_pic->planes[ Y_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 );
535             p_pic->planes[ U_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[2];
536             p_pic->planes[ U_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 ) / 4;
537             p_pic->planes[ V_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[1];
538             p_pic->planes[ V_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 ) / 4;
539
540             p_pic->i_planes = 3;
541
542             return 0;
543
544         default:
545             /* Unknown chroma, tell the guy to get lost */
546             p_pic->i_planes = 0;
547
548             return 0;
549     }
550 }
551