]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
* Initialize SDL before opening the SDL audio output.
[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.71 2001/12/19 03:50:22 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     SDL_Overlay *   p_overlay; /* An overlay we keep to grab the XVideo port */
72
73     int i_width;
74     int i_height;
75
76     boolean_t   b_cursor;
77     boolean_t   b_cursor_autohidden;
78     mtime_t     i_lastmoved;
79
80 } vout_sys_t;
81
82 /*****************************************************************************
83  * picture_sys_t: direct buffer method descriptor
84  *****************************************************************************
85  * This structure is part of the picture descriptor, it describes the
86  * SDL specific properties of a direct buffer.
87  *****************************************************************************/
88 typedef struct picture_sys_s
89 {
90     SDL_Overlay *p_overlay;
91
92 } picture_sys_t;
93
94 /*****************************************************************************
95  * Local prototypes.
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 *, struct picture_s * );
104
105 static int  SDLOpenDisplay      ( vout_thread_t *p_vout );
106 static void SDLCloseDisplay     ( vout_thread_t *p_vout );
107 static int  SDLNewPicture       ( vout_thread_t *p_vout, picture_t *p_pic );
108
109 /*****************************************************************************
110  * Functions exported as capabilities. They are declared as static so that
111  * we don't pollute the namespace too much.
112  *****************************************************************************/
113 void _M( vout_getfunctions )( function_list_t * p_function_list )
114 {
115     p_function_list->pf_probe = vout_Probe;
116     p_function_list->functions.vout.pf_create     = vout_Create;
117     p_function_list->functions.vout.pf_init       = vout_Init;
118     p_function_list->functions.vout.pf_end        = vout_End;
119     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
120     p_function_list->functions.vout.pf_manage     = vout_Manage;
121     p_function_list->functions.vout.pf_display    = vout_Display;
122     p_function_list->functions.vout.pf_setpalette = NULL;
123 }
124
125 /*****************************************************************************
126  * vout_Probe: probe the video driver and return a score
127  *****************************************************************************
128  * This function tries to initialize SDL and returns a score to the
129  * plugin manager so that it can select the best plugin.
130  *****************************************************************************/
131 static int vout_Probe( probedata_t *p_data )
132 {
133     if( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
134     {
135         return( 0 );
136     }
137
138     if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
139     {
140         return( 999 );
141     }
142
143     return( 100 );
144 }
145
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 )
154 {
155     /* Allocate structure */
156     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
157     if( p_vout->p_sys == NULL )
158     {
159         intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
160         return( 1 );
161     }
162
163     /* Initialize library */
164     if( SDL_Init( SDL_INIT_VIDEO
165 #ifndef WIN32
166     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
167                 | SDL_INIT_EVENTTHREAD
168 #endif
169 #ifdef DEBUG
170     /* In debug mode you may want vlc to dump a core instead of staying
171      * stuck */
172                 | SDL_INIT_NOPARACHUTE
173 #endif
174                 ) < 0 )
175     {
176         intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
177         free( p_vout->p_sys );
178         return( 1 );
179     }
180
181     p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
182     p_vout->p_sys->b_cursor_autohidden = 0;
183     p_vout->p_sys->i_lastmoved = mdate();
184
185     if( p_vout->render.i_height * p_vout->render.i_aspect
186          >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
187     {
188         p_vout->p_sys->i_width = p_vout->render.i_height
189             * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
190         p_vout->p_sys->i_height = p_vout->render.i_height;
191     }
192     else
193     {
194         p_vout->p_sys->i_width = p_vout->render.i_width;
195         p_vout->p_sys->i_height = p_vout->render.i_width
196             * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
197     }
198
199     if( p_vout->p_sys->i_width <= 300 && p_vout->p_sys->i_height <= 200 )
200     {
201         p_vout->p_sys->i_width <<= 1;
202         p_vout->p_sys->i_height <<= 1;
203     }
204     else if( p_vout->p_sys->i_width <= 400 && p_vout->p_sys->i_height <= 300 )
205     {
206         p_vout->p_sys->i_width += p_vout->p_sys->i_width >> 1;
207         p_vout->p_sys->i_height += p_vout->p_sys->i_height >> 1;
208     }
209
210     if( SDLOpenDisplay( p_vout ) )
211     {
212         intf_ErrMsg( "vout error: can't set up SDL (%s)", SDL_GetError() );
213         SDL_QuitSubSystem( SDL_INIT_VIDEO );
214         free( p_vout->p_sys );
215         return( 1 );
216     }
217
218     return( 0 );
219 }
220
221 /*****************************************************************************
222  * vout_Init: initialize SDL video thread output method
223  *****************************************************************************
224  * This function initialize the SDL display device.
225  *****************************************************************************/
226 static int vout_Init( vout_thread_t *p_vout )
227 {
228     int i_index;
229     picture_t *p_pic;
230
231     I_OUTPUTPICTURES = 0;
232
233     /* Initialize the output structure */
234     switch( p_vout->render.i_chroma )
235     {
236         case YUV_420_PICTURE:
237             p_vout->output.i_chroma = p_vout->render.i_chroma;
238             p_vout->output.i_width  = p_vout->render.i_width;
239             p_vout->output.i_height = p_vout->render.i_height;
240             p_vout->output.i_aspect = p_vout->render.i_aspect;
241             break;
242
243         default:
244             return( 0 );
245     }
246
247     /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
248     while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
249     {
250         p_pic = NULL;
251
252         /* Find an empty picture slot */
253         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
254         {
255             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
256             {
257                 p_pic = p_vout->p_picture + i_index;
258                 break;
259             }
260         }
261
262         /* Allocate the picture if we found one */
263         if( p_pic == NULL || SDLNewPicture( p_vout, p_pic ) )
264         {
265             break;
266         }
267
268         p_pic->i_status        = DESTROYED_PICTURE;
269         p_pic->i_type          = DIRECT_PICTURE;
270
271         p_pic->i_left_margin   =
272         p_pic->i_right_margin  =
273         p_pic->i_top_margin    =
274         p_pic->i_bottom_margin = 0;
275
276         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
277
278         I_OUTPUTPICTURES++;
279     }
280
281     return( 0 );
282 }
283
284 /*****************************************************************************
285  * vout_End: terminate Sys video thread output method
286  *****************************************************************************
287  * Terminate an output method created by vout_SDLCreate
288  *****************************************************************************/
289 static void vout_End( vout_thread_t *p_vout )
290 {
291     int i_index;
292
293     /* Free the output buffers we allocated */
294     for( i_index = I_OUTPUTPICTURES ; i_index ; )
295     {
296         i_index--;
297         SDL_UnlockYUVOverlay( PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
298         SDL_FreeYUVOverlay( PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
299         free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
300     }
301 }
302
303 /*****************************************************************************
304  * vout_Destroy: destroy Sys video thread output method
305  *****************************************************************************
306  * Terminate an output method created by vout_SDLCreate
307  *****************************************************************************/
308 static void vout_Destroy( vout_thread_t *p_vout )
309 {
310     SDLCloseDisplay( p_vout );
311
312     SDL_QuitSubSystem( SDL_INIT_VIDEO );
313
314     free( p_vout->p_sys );
315 }
316
317 /*****************************************************************************
318  * vout_Manage: handle Sys events
319  *****************************************************************************
320  * This function should be called regularly by video output thread. It returns
321  * a non null value if an error occured.
322  *****************************************************************************/
323 static int vout_Manage( vout_thread_t *p_vout )
324 {
325     SDL_Event event;                                            /* SDL event */
326
327     /* Process events */
328     while( SDL_PollEvent(&event) )
329     {
330         switch( event.type )
331         {
332         case SDL_VIDEORESIZE:                          /* Resizing of window */
333             p_vout->p_sys->i_width = event.resize.w;
334             p_vout->p_sys->i_height = event.resize.h;
335             SDLCloseDisplay( p_vout );
336             SDLOpenDisplay( p_vout );
337             break;
338
339         case SDL_MOUSEMOTION:
340             if( p_vout->p_sys->b_cursor &&
341                 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
342             {
343                 if( p_vout->p_sys->b_cursor_autohidden )
344                 {
345                     p_vout->p_sys->b_cursor_autohidden = 0;
346                     SDL_ShowCursor( 1 );
347                 }
348                 else
349                 {
350                     p_vout->p_sys->i_lastmoved = mdate();
351                 }
352             }
353             break;
354
355         case SDL_MOUSEBUTTONUP:
356             switch( event.button.button )
357             {
358             case SDL_BUTTON_RIGHT:
359                 p_main->p_intf->b_menu_change = 1;
360                 break;
361             }
362             break;
363
364         case SDL_MOUSEBUTTONDOWN:
365             switch( event.button.button )
366             {
367             case SDL_BUTTON_LEFT:
368                 /* Handle clicks */
369                 break;
370             }
371             break;
372
373         case SDL_QUIT:
374             p_main->p_intf->b_die = 1;
375             break;
376
377         case SDL_KEYDOWN:                             /* if a key is pressed */
378
379             switch( event.key.keysym.sym )
380             {
381             case SDLK_q:                                             /* quit */
382             case SDLK_ESCAPE:
383                 p_main->p_intf->b_die = 1;
384                 break;
385
386             case SDLK_f:                             /* switch to fullscreen */
387                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
388                 break;
389
390             case SDLK_c:                                 /* toggle grayscale */
391                 p_vout->b_grayscale = ! p_vout->b_grayscale;
392                 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
393                 break;
394
395             case SDLK_i:                                      /* toggle info */
396                 p_vout->b_info = ! p_vout->b_info;
397                 p_vout->i_changes |= VOUT_INFO_CHANGE;
398                 break;
399
400             case SDLK_s:                                   /* toggle scaling */
401                 p_vout->b_scale = ! p_vout->b_scale;
402                 p_vout->i_changes |= VOUT_SCALE_CHANGE;
403                 break;
404
405             case SDLK_SPACE:                             /* toggle interface */
406                 p_vout->b_interface = ! p_vout->b_interface;
407                 p_vout->i_changes |= VOUT_INTF_CHANGE;
408                 break;
409             
410             case SDLK_MENU:
411                 p_main->p_intf->b_menu_change = 1;
412                 break;
413                 
414             case SDLK_F10: network_ChannelJoin( 0 ); break;
415             case SDLK_F1:  network_ChannelJoin( 1 ); break;
416             case SDLK_F2:  network_ChannelJoin( 2 ); break;
417             case SDLK_F3:  network_ChannelJoin( 3 ); break;
418             case SDLK_F4:  network_ChannelJoin( 4 ); break;
419             case SDLK_F5:  network_ChannelJoin( 5 ); break;
420             case SDLK_F6:  network_ChannelJoin( 6 ); break;
421             case SDLK_F7:  network_ChannelJoin( 7 ); break;
422             case SDLK_F8:  network_ChannelJoin( 8 ); break;
423             case SDLK_F9:  network_ChannelJoin( 9 ); break;
424
425             default:
426                 intf_DbgMsg( "unhandled key %i", event.key.keysym.sym );
427                 break;
428             }
429             break;
430
431         default:
432             break;
433         }
434     }
435
436     /* Fullscreen change */
437     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
438     {
439         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
440
441         SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
442
443         p_vout->p_sys->b_cursor_autohidden = 0;
444         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
445                         ! p_vout->p_sys->b_cursor_autohidden );
446
447         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
448     }
449
450     /* Pointer change */
451     if( ! p_vout->p_sys->b_cursor_autohidden &&
452         ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
453     {
454         /* Hide the mouse automatically */
455         p_vout->p_sys->b_cursor_autohidden = 1;
456         SDL_ShowCursor( 0 );
457     }
458
459     return( 0 );
460 }
461
462 /*****************************************************************************
463  * vout_Display: displays previously rendered output
464  *****************************************************************************
465  * This function sends the currently rendered image to the display.
466  *****************************************************************************/
467 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
468 {
469     int x, y, w, h;
470     SDL_Rect disp;
471
472     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
473                        &x, &y, &w, &h );
474     disp.x = x;
475     disp.y = y;
476     disp.w = w;
477     disp.h = h;
478
479     SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
480     SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
481     SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
482 }
483
484 /* following functions are local */
485
486 /*****************************************************************************
487  * SDLOpenDisplay: open and initialize SDL device
488  *****************************************************************************
489  * Open and initialize display according to preferences specified in the vout
490  * thread fields.
491  *****************************************************************************/
492 static int SDLOpenDisplay( vout_thread_t *p_vout )
493 {
494     Uint32 i_flags;
495     int    i_bpp;
496
497     /* Initialize flags and cursor */
498     i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE;
499     i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
500
501     i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
502                              SDL_DEFAULT_BPP, i_flags );
503     if( i_bpp == 0 )
504     {
505         intf_ErrMsg( "vout error: no video mode available" );
506         return( 1 );
507     }
508
509     p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
510                                                  p_vout->p_sys->i_height,
511                                                  i_bpp, i_flags );
512
513     if( p_vout->p_sys->p_display == NULL )
514     {
515         intf_ErrMsg( "vout error: cannot set video mode" );
516         return( 1 );
517     }
518
519     SDL_LockSurface( p_vout->p_sys->p_display );
520
521     p_vout->p_sys->p_overlay =
522         SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
523                               p_vout->p_sys->p_display );
524
525     if( p_vout->p_sys->p_overlay == NULL )
526     {
527         intf_ErrMsg( "vout error: cannot set overlay" );
528         SDL_UnlockSurface( p_vout->p_sys->p_display );
529         SDL_FreeSurface( p_vout->p_sys->p_display );
530         return( 1 );
531     }
532
533     if( p_vout->p_sys->p_overlay->hw_overlay )
534     {
535         SDL_WM_SetCaption( VOUT_TITLE " (hardware SDL output)",
536                            VOUT_TITLE " (hardware SDL output)" );
537     }
538     else
539     {
540         SDL_WM_SetCaption( VOUT_TITLE " (software SDL output)",
541                            VOUT_TITLE " (software SDL output)" );
542     }
543
544     SDL_EventState( SDL_KEYUP, SDL_IGNORE );               /* ignore keys up */
545
546     return( 0 );
547 }
548
549 /*****************************************************************************
550  * SDLCloseDisplay: close and reset SDL device
551  *****************************************************************************
552  * This function returns all resources allocated by SDLOpenDisplay and restore
553  * the original state of the device.
554  *****************************************************************************/
555 static void SDLCloseDisplay( vout_thread_t *p_vout )
556 {
557     SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
558     SDL_UnlockSurface ( p_vout->p_sys->p_display );
559     SDL_FreeSurface( p_vout->p_sys->p_display );
560 }
561
562 /*****************************************************************************
563  * SDLNewPicture: allocate a picture
564  *****************************************************************************
565  * Returns 0 on success, -1 otherwise
566  *****************************************************************************/
567 static int SDLNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
568 {
569     int i_width  = p_vout->output.i_width;
570     int i_height = p_vout->output.i_height;
571
572     switch( p_vout->output.i_chroma )
573     {
574         case YUV_420_PICTURE:
575             /* We know this chroma, allocate a buffer which will be used
576              * directly by the decoder */
577             p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
578
579             if( p_pic->p_sys == NULL )
580             {
581                 return -1;
582             }
583
584             p_pic->p_sys->p_overlay =
585                 SDL_CreateYUVOverlay( i_width, i_height,
586                                       SDL_YV12_OVERLAY,
587                                       p_vout->p_sys->p_display );
588
589             if( p_pic->p_sys->p_overlay == NULL )
590             {
591                 free( p_pic->p_sys );
592                 return -1;
593             }
594
595             SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
596
597             /* Precalculate some values */
598             p_pic->i_size         = i_width * i_height;
599             p_pic->i_chroma_width = i_width / 2;
600             p_pic->i_chroma_size  = i_height * ( i_width / 2 );
601
602             /* FIXME: try to get the right i_bytes value from p_overlay */
603             p_pic->planes[ Y_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 0 ];
604             p_pic->planes[ Y_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 );
605             p_pic->planes[ Y_PLANE ].i_line_bytes = i_width * sizeof( u8 );
606
607             p_pic->planes[ U_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 2 ];
608             p_pic->planes[ U_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 ) / 4;
609             p_pic->planes[ U_PLANE ].i_line_bytes = p_pic->i_chroma_width * sizeof( u8 );
610
611             p_pic->planes[ V_PLANE ].p_data = p_pic->p_sys->p_overlay->pixels[ 1 ];
612             p_pic->planes[ V_PLANE ].i_bytes = p_pic->i_size * sizeof( u8 ) / 4;
613             p_pic->planes[ V_PLANE ].i_line_bytes = p_pic->i_chroma_width * sizeof( u8 );
614
615             p_pic->i_planes = 3;
616
617             return 0;
618
619         default:
620             /* Unknown chroma, tell the guy to get lost */
621             p_pic->i_planes = 0;
622
623             return 0;
624     }
625 }
626