]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
* Fixed compilation with dvdcss disabled.
[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.72 2001/12/19 18:14:23 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 "netutils.h"
50 #include "tests.h"
51
52 #include "video.h"
53 #include "video_output.h"
54
55 #include "interface.h"
56
57 #include "modules.h"
58 #include "modules_export.h"
59
60 #define SDL_MAX_DIRECTBUFFERS 5
61 #define SDL_DEFAULT_BPP 16
62
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
70 {
71     SDL_Surface *   p_display;                             /* display device */
72     SDL_Overlay *   p_overlay; /* An overlay we keep to grab the XVideo port */
73
74     int i_width;
75     int i_height;
76
77     boolean_t   b_cursor;
78     boolean_t   b_cursor_autohidden;
79     mtime_t     i_lastmoved;
80
81 } vout_sys_t;
82
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
90 {
91     SDL_Overlay *p_overlay;
92
93 } picture_sys_t;
94
95 /*****************************************************************************
96  * Local prototypes.
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 * );
105
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 );
109
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 )
115 {
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;
124 }
125
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 )
133 {
134     if( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
135     {
136         return( 0 );
137     }
138
139     if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
140     {
141         return( 999 );
142     }
143
144     return( 100 );
145 }
146
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 )
155 {
156     /* Allocate structure */
157     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
158     if( p_vout->p_sys == NULL )
159     {
160         intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
161         return( 1 );
162     }
163
164     /* Initialize library */
165     if( SDL_Init( SDL_INIT_VIDEO
166 #ifndef WIN32
167     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
168                 | SDL_INIT_EVENTTHREAD
169 #endif
170 #ifdef DEBUG
171     /* In debug mode you may want vlc to dump a core instead of staying
172      * stuck */
173                 | SDL_INIT_NOPARACHUTE
174 #endif
175                 ) < 0 )
176     {
177         intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
178         free( p_vout->p_sys );
179         return( 1 );
180     }
181
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();
185
186     if( p_vout->render.i_height * p_vout->render.i_aspect
187          >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
188     {
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;
192     }
193     else
194     {
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;
198     }
199
200     if( p_vout->p_sys->i_width <= 300 && p_vout->p_sys->i_height <= 300 )
201     {
202         p_vout->p_sys->i_width <<= 1;
203         p_vout->p_sys->i_height <<= 1;
204     }
205     else if( p_vout->p_sys->i_width <= 400 && p_vout->p_sys->i_height <= 400 )
206     {
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;
209     }
210
211     if( SDLOpenDisplay( p_vout ) )
212     {
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 );
216         return( 1 );
217     }
218
219     return( 0 );
220 }
221
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 )
228 {
229     int i_index;
230     picture_t *p_pic;
231
232     I_OUTPUTPICTURES = 0;
233
234     /* Initialize the output structure */
235     switch( p_vout->render.i_chroma )
236     {
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;
242             break;
243
244         default:
245             return( 0 );
246     }
247
248     /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
249     while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
250     {
251         p_pic = NULL;
252
253         /* Find an empty picture slot */
254         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
255         {
256             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
257             {
258                 p_pic = p_vout->p_picture + i_index;
259                 break;
260             }
261         }
262
263         /* Allocate the picture if we found one */
264         if( p_pic == NULL || SDLNewPicture( p_vout, p_pic ) )
265         {
266             break;
267         }
268
269         p_pic->i_status        = DESTROYED_PICTURE;
270         p_pic->i_type          = DIRECT_PICTURE;
271
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;
276
277         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
278
279         I_OUTPUTPICTURES++;
280     }
281
282     return( 0 );
283 }
284
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 )
291 {
292     int i_index;
293
294     /* Free the output buffers we allocated */
295     for( i_index = I_OUTPUTPICTURES ; i_index ; )
296     {
297         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 );
301     }
302 }
303
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 )
310 {
311     SDLCloseDisplay( p_vout );
312
313     SDL_QuitSubSystem( SDL_INIT_VIDEO );
314
315     free( p_vout->p_sys );
316 }
317
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 )
325 {
326     SDL_Event event;                                            /* SDL event */
327
328     /* Process events */
329     while( SDL_PollEvent(&event) )
330     {
331         switch( event.type )
332         {
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 );
338             break;
339
340         case SDL_MOUSEMOTION:
341             if( p_vout->p_sys->b_cursor &&
342                 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
343             {
344                 if( p_vout->p_sys->b_cursor_autohidden )
345                 {
346                     p_vout->p_sys->b_cursor_autohidden = 0;
347                     SDL_ShowCursor( 1 );
348                 }
349                 else
350                 {
351                     p_vout->p_sys->i_lastmoved = mdate();
352                 }
353             }
354             break;
355
356         case SDL_MOUSEBUTTONUP:
357             switch( event.button.button )
358             {
359             case SDL_BUTTON_RIGHT:
360                 p_main->p_intf->b_menu_change = 1;
361                 break;
362             }
363             break;
364
365         case SDL_MOUSEBUTTONDOWN:
366             switch( event.button.button )
367             {
368             case SDL_BUTTON_LEFT:
369                 /* Handle clicks */
370                 break;
371             }
372             break;
373
374         case SDL_QUIT:
375             p_main->p_intf->b_die = 1;
376             break;
377
378         case SDL_KEYDOWN:                             /* if a key is pressed */
379
380             switch( event.key.keysym.sym )
381             {
382             case SDLK_q:                                             /* quit */
383             case SDLK_ESCAPE:
384                 p_main->p_intf->b_die = 1;
385                 break;
386
387             case SDLK_f:                             /* switch to fullscreen */
388                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
389                 break;
390
391             case SDLK_c:                                 /* toggle grayscale */
392                 p_vout->b_grayscale = ! p_vout->b_grayscale;
393                 p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
394                 break;
395
396             case SDLK_i:                                      /* toggle info */
397                 p_vout->b_info = ! p_vout->b_info;
398                 p_vout->i_changes |= VOUT_INFO_CHANGE;
399                 break;
400
401             case SDLK_s:                                   /* toggle scaling */
402                 p_vout->b_scale = ! p_vout->b_scale;
403                 p_vout->i_changes |= VOUT_SCALE_CHANGE;
404                 break;
405
406             case SDLK_SPACE:                             /* toggle interface */
407                 p_vout->b_interface = ! p_vout->b_interface;
408                 p_vout->i_changes |= VOUT_INTF_CHANGE;
409                 break;
410             
411             case SDLK_MENU:
412                 p_main->p_intf->b_menu_change = 1;
413                 break;
414                 
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;
425
426             default:
427                 intf_DbgMsg( "unhandled key %i", event.key.keysym.sym );
428                 break;
429             }
430             break;
431
432         default:
433             break;
434         }
435     }
436
437     /* Fullscreen change */
438     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
439     {
440         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
441
442         SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
443
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 );
447
448         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
449     }
450
451     /* Pointer change */
452     if( ! p_vout->p_sys->b_cursor_autohidden &&
453         ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
454     {
455         /* Hide the mouse automatically */
456         p_vout->p_sys->b_cursor_autohidden = 1;
457         SDL_ShowCursor( 0 );
458     }
459
460     return( 0 );
461 }
462
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 )
469 {
470     int x, y, w, h;
471     SDL_Rect disp;
472
473     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
474                        &x, &y, &w, &h );
475     disp.x = x;
476     disp.y = y;
477     disp.w = w;
478     disp.h = h;
479
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);
483 }
484
485 /* following functions are local */
486
487 /*****************************************************************************
488  * SDLOpenDisplay: open and initialize SDL device
489  *****************************************************************************
490  * Open and initialize display according to preferences specified in the vout
491  * thread fields.
492  *****************************************************************************/
493 static int SDLOpenDisplay( vout_thread_t *p_vout )
494 {
495     Uint32 i_flags;
496     int    i_bpp;
497
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;
501
502     i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
503                              SDL_DEFAULT_BPP, i_flags );
504     if( i_bpp == 0 )
505     {
506         intf_ErrMsg( "vout error: no video mode available" );
507         return( 1 );
508     }
509
510     p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
511                                                  p_vout->p_sys->i_height,
512                                                  i_bpp, i_flags );
513
514     if( p_vout->p_sys->p_display == NULL )
515     {
516         intf_ErrMsg( "vout error: cannot set video mode" );
517         return( 1 );
518     }
519
520     SDL_LockSurface( p_vout->p_sys->p_display );
521
522     p_vout->p_sys->p_overlay =
523         SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
524                               p_vout->p_sys->p_display );
525
526     if( p_vout->p_sys->p_overlay == NULL )
527     {
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 );
531         return( 1 );
532     }
533
534     if( p_vout->p_sys->p_overlay->hw_overlay )
535     {
536         SDL_WM_SetCaption( VOUT_TITLE " (hardware SDL output)",
537                            VOUT_TITLE " (hardware SDL output)" );
538     }
539     else
540     {
541         SDL_WM_SetCaption( VOUT_TITLE " (software SDL output)",
542                            VOUT_TITLE " (software SDL output)" );
543     }
544
545     SDL_EventState( SDL_KEYUP, SDL_IGNORE );               /* ignore keys up */
546
547     return( 0 );
548 }
549
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 )
557 {
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 );
561 }
562
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 )
569 {
570     int i_width  = p_vout->output.i_width;
571     int i_height = p_vout->output.i_height;
572
573     switch( p_vout->output.i_chroma )
574     {
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 ) );
579
580             if( p_pic->p_sys == NULL )
581             {
582                 return -1;
583             }
584
585             p_pic->p_sys->p_overlay =
586                 SDL_CreateYUVOverlay( i_width, i_height,
587                                       SDL_YV12_OVERLAY,
588                                       p_vout->p_sys->p_display );
589
590             if( p_pic->p_sys->p_overlay == NULL )
591             {
592                 free( p_pic->p_sys );
593                 return -1;
594             }
595
596             SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
597
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 );
602
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 );
607
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 );
611
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 );
615
616             p_pic->i_planes = 3;
617
618             return 0;
619
620         default:
621             /* Unknown chroma, tell the guy to get lost */
622             p_pic->i_planes = 0;
623
624             return 0;
625     }
626 }
627