]> git.sesse.net Git - vlc/blob - modules/video_output/sdl.c
Fix memleak in sdl video output. (string passed to setenv have to be freed after).
[vlc] / modules / video_output / sdl.c
1 /*****************************************************************************
2  * sdl.c: SDL video output display method
3  *****************************************************************************
4  * Copyright (C) 1998-2001 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <errno.h>                                                 /* ENOMEM */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_interface.h>
38 #include <vlc_playlist.h>
39 #include <vlc_vout.h>
40 #include <vlc_keys.h>
41 //#include <vlc_aout.h>
42
43 #include <sys/types.h>
44 #ifndef WIN32
45 #   include <netinet/in.h>                            /* BSD: struct in_addr */
46 #endif
47
48 #include SDL_INCLUDE_FILE
49
50 /* SDL is not able to crop overlays - so use only 1 direct buffer */
51 #define SDL_MAX_DIRECTBUFFERS 1
52 #define SDL_DEFAULT_BPP 16
53
54 /*****************************************************************************
55  * vout_sys_t: video output SDL method descriptor
56  *****************************************************************************
57  * This structure is part of the video output thread descriptor.
58  * It describes the SDL specific properties of an output thread.
59  *****************************************************************************/
60 struct vout_sys_t
61 {
62     SDL_Surface *   p_display;                             /* display device */
63
64     int i_width;
65     int i_height;
66
67 #if SDL_VERSION_ATLEAST(1,2,10)
68     unsigned int i_desktop_width;
69     unsigned int i_desktop_height;
70 #endif
71
72     /* For YUV output */
73     SDL_Overlay * p_overlay;   /* An overlay we keep to grab the XVideo port */
74
75     /* For RGB output */
76     int i_surfaces;
77
78     bool  b_cursor;
79     bool  b_cursor_autohidden;
80     mtime_t     i_lastmoved;
81     mtime_t     i_mouse_hide_timeout;
82     mtime_t     i_lastpressed;                        /* to track dbl-clicks */
83 };
84
85 /*****************************************************************************
86  * picture_sys_t: direct buffer method descriptor
87  *****************************************************************************
88  * This structure is part of the picture descriptor, it describes the
89  * SDL specific properties of a direct buffer.
90  *****************************************************************************/
91 struct picture_sys_t
92 {
93     SDL_Overlay *p_overlay;
94 };
95
96 /*****************************************************************************
97  * Local prototypes
98  *****************************************************************************/
99 static int  Open      ( vlc_object_t * );
100 static void Close     ( vlc_object_t * );
101 static int  Init      ( vout_thread_t * );
102 static void End       ( vout_thread_t * );
103 static int  Manage    ( vout_thread_t * );
104 static void Display   ( vout_thread_t *, picture_t * );
105
106 static int  OpenDisplay     ( vout_thread_t * );
107 static void CloseDisplay    ( vout_thread_t * );
108 static int  NewPicture      ( vout_thread_t *, picture_t * );
109 static void SetPalette      ( vout_thread_t *,
110                               uint16_t *, uint16_t *, uint16_t * );
111
112 static int ConvertKey( SDLKey );
113
114
115 #define CHROMA_TEXT N_("SDL chroma format")
116 #define CHROMA_LONGTEXT N_( \
117     "Force the SDL renderer to use a specific chroma format instead of " \
118     "trying to improve performances by using the most efficient one.")
119
120 /*****************************************************************************
121  * Module descriptor
122  *****************************************************************************/
123 vlc_module_begin();
124     set_shortname( "SDL" );
125     set_category( CAT_VIDEO );
126     set_subcategory( SUBCAT_VIDEO_VOUT );
127     set_description( N_("Simple DirectMedia Layer video output") );
128     set_capability( "video output", 60 );
129     add_shortcut( "sdl" );
130     add_string( "sdl-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true );
131     set_callbacks( Open, Close );
132 #if defined( __i386__ ) || defined( __x86_64__ )
133     /* On i386, SDL is linked against svgalib */
134     linked_with_a_crap_library_which_uses_atexit();
135 #endif
136 vlc_module_end();
137
138 /*****************************************************************************
139  * OpenVideo: allocate SDL video thread output method
140  *****************************************************************************
141  * This function allocate and initialize a SDL vout method. It uses some of the
142  * vout properties to choose the correct mode, and change them according to the
143  * mode actually used.
144  *****************************************************************************/
145 static int Open ( vlc_object_t *p_this )
146 {
147     vout_thread_t * p_vout = (vout_thread_t *)p_this;
148     /* XXX: check for conflicts with the SDL audio output */
149     vlc_mutex_t *lock = var_AcquireMutex( "sdl" );
150
151 #ifdef HAVE_SETENV
152     char *psz_method;
153 #endif
154
155     if( lock == NULL )
156         return VLC_ENOMEM;
157
158     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
159     if( p_vout->p_sys == NULL )
160     {
161         vlc_mutex_unlock( lock );
162         return VLC_ENOMEM;
163     }
164
165     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
166
167     /* Check if SDL video module has been initialized */
168     if( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
169     {
170         vlc_mutex_unlock( lock );
171         free( p_vout->p_sys );
172         return VLC_EGENERIC;
173     }
174
175     /* Allocate structure */
176     p_vout->pf_init = Init;
177     p_vout->pf_end = End;
178     p_vout->pf_manage = Manage;
179     p_vout->pf_render = NULL;
180     p_vout->pf_display = Display;
181     p_vout->pf_control = NULL;
182
183 #ifdef HAVE_SETENV
184     char* psz = psz_method = config_GetPsz( p_vout, "vout" );
185     if( psz_method )
186     {
187         while( *psz_method && *psz_method != ':' )
188         {
189             psz_method++;
190         }
191
192         if( *psz_method )
193         {
194             setenv( "SDL_VIDEODRIVER", psz_method + 1, 1 );
195         }
196     }
197     free( psz );
198 #endif
199
200     /* Initialize library */
201     if( SDL_Init( SDL_INIT_VIDEO
202 #ifndef WIN32
203     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
204                 | SDL_INIT_EVENTTHREAD
205 #endif
206 #ifndef NDEBUG
207     /* In debug mode you may want vlc to dump a core instead of staying
208      * stuck */
209                 | SDL_INIT_NOPARACHUTE
210 #endif
211                 ) < 0 )
212     {
213         msg_Err( p_vout, "cannot initialize SDL (%s)", SDL_GetError() );
214         free( p_vout->p_sys );
215         vlc_mutex_unlock( lock );
216         return VLC_EGENERIC;
217     }
218
219     vlc_mutex_unlock( lock );
220
221     /* Translate keys into unicode */
222     SDL_EnableUNICODE(1);
223
224     /* Get the desktop resolution */
225 #if SDL_VERSION_ATLEAST(1,2,10)
226     /* FIXME: SDL has a problem with virtual desktop */
227     p_vout->p_sys->i_desktop_width = SDL_GetVideoInfo()->current_w;
228     p_vout->p_sys->i_desktop_height = SDL_GetVideoInfo()->current_h;
229 #endif
230
231     /* Create the cursor */
232     p_vout->p_sys->b_cursor = 1;
233     p_vout->p_sys->b_cursor_autohidden = 0;
234     p_vout->p_sys->i_lastmoved = p_vout->p_sys->i_lastpressed = mdate();
235     p_vout->p_sys->i_mouse_hide_timeout =
236         var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
237
238     if( OpenDisplay( p_vout ) )
239     {
240         msg_Err( p_vout, "cannot set up SDL (%s)", SDL_GetError() );
241         SDL_QuitSubSystem( SDL_INIT_VIDEO );
242         free( p_vout->p_sys );
243         return VLC_EGENERIC;
244     }
245
246     return VLC_SUCCESS;
247 }
248
249 /*****************************************************************************
250  * Init: initialize SDL video thread output method
251  *****************************************************************************
252  * This function initialize the SDL display device.
253  *****************************************************************************/
254 static int Init( vout_thread_t *p_vout )
255 {
256     int i_index;
257     picture_t *p_pic;
258
259     p_vout->p_sys->i_surfaces = 0;
260
261     I_OUTPUTPICTURES = 0;
262
263     /* Initialize the output structure */
264     if( p_vout->p_sys->p_overlay == NULL )
265     {
266         /* All we have is an RGB image with square pixels */
267         p_vout->output.i_width  = p_vout->p_sys->i_width;
268         p_vout->output.i_height = p_vout->p_sys->i_height;
269         p_vout->output.i_aspect = p_vout->output.i_width
270                                    * VOUT_ASPECT_FACTOR
271                                    / p_vout->output.i_height;
272     }
273     else
274     {
275         /* We may need to convert the chroma, but at least we keep the
276          * aspect ratio */
277         p_vout->output.i_width  = p_vout->render.i_width;
278         p_vout->output.i_height = p_vout->render.i_height;
279         p_vout->output.i_aspect = p_vout->render.i_aspect;
280     }
281
282     /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
283     while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
284     {
285         p_pic = NULL;
286
287         /* Find an empty picture slot */
288         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
289         {
290             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
291             {
292                 p_pic = p_vout->p_picture + i_index;
293                 break;
294             }
295         }
296
297         /* Allocate the picture if we found one */
298         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
299         {
300             break;
301         }
302
303         p_pic->i_status = DESTROYED_PICTURE;
304         p_pic->i_type   = DIRECT_PICTURE;
305
306         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
307
308         I_OUTPUTPICTURES++;
309     }
310
311     return VLC_SUCCESS;
312 }
313
314 /*****************************************************************************
315  * End: terminate Sys video thread output method
316  *****************************************************************************
317  * Terminate an output method created by OpenVideo
318  *****************************************************************************/
319 static void End( vout_thread_t *p_vout )
320 {
321     int i_index;
322
323     /* Free the output buffers we allocated */
324     for( i_index = I_OUTPUTPICTURES ; i_index ; )
325     {
326         i_index--;
327         if( p_vout->p_sys->p_overlay == NULL )
328         {
329             /* RGB picture */
330         }
331         else
332         {
333             SDL_UnlockYUVOverlay(
334                     PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
335             SDL_FreeYUVOverlay(
336                     PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
337         }
338         free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
339     }
340 }
341
342 /*****************************************************************************
343  * CloseVideo: destroy Sys video thread output method
344  *****************************************************************************
345  * Terminate an output method created by vout_SDLCreate
346  *****************************************************************************/
347 static void Close ( vlc_object_t *p_this )
348 {
349     vout_thread_t * p_vout = (vout_thread_t *)p_this;
350
351     CloseDisplay( p_vout );
352     SDL_QuitSubSystem( SDL_INIT_VIDEO );
353
354     free( p_vout->p_sys );
355 }
356
357 /*****************************************************************************
358  * Manage: handle Sys events
359  *****************************************************************************
360  * This function should be called regularly by video output thread. It returns
361  * a non null value if an error occurred.
362  *****************************************************************************/
363 static int Manage( vout_thread_t *p_vout )
364 {
365     SDL_Event event;                                            /* SDL event */
366     vlc_value_t val;
367     unsigned int i_width, i_height, i_x, i_y;
368
369     /* Process events */
370     while( SDL_PollEvent( &event ) )
371     {
372         switch( event.type )
373         {
374         /* Resizing of window */
375         case SDL_VIDEORESIZE:
376             p_vout->i_changes |= VOUT_SIZE_CHANGE;
377             p_vout->i_window_width = p_vout->p_sys->i_width = event.resize.w;
378             p_vout->i_window_height = p_vout->p_sys->i_height = event.resize.h;
379             break;
380
381         /* Mouse move */
382         case SDL_MOUSEMOTION:
383             vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
384                                p_vout->p_sys->i_height,
385                                &i_x, &i_y, &i_width, &i_height );
386
387             /* Compute the x coordinate and check if the value is
388                in [0,p_vout->fmt_in.i_visible_width] */
389             val.i_int = ( event.motion.x - i_x ) *
390                         p_vout->fmt_in.i_visible_width / i_width +
391                         p_vout->fmt_in.i_x_offset;
392
393             if( (int)(event.motion.x - i_x) < 0 )
394                 val.i_int = 0;
395             else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
396                 val.i_int = p_vout->fmt_in.i_visible_width;
397
398             var_Set( p_vout, "mouse-x", val );
399
400             /* compute the y coordinate and check if the value is
401                in [0,p_vout->fmt_in.i_visible_height] */
402             val.i_int = ( event.motion.y - i_y ) *
403                         p_vout->fmt_in.i_visible_height / i_height +
404                         p_vout->fmt_in.i_y_offset;
405
406             if( (int)(event.motion.y - i_y) < 0 )
407                 val.i_int = 0;
408             else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
409                 val.i_int = p_vout->fmt_in.i_visible_height;
410
411             var_Set( p_vout, "mouse-y", val );
412
413             val.b_bool = true;
414             var_Set( p_vout, "mouse-moved", val );
415
416             if( p_vout->p_sys->b_cursor )
417             {
418                 if( p_vout->p_sys->b_cursor_autohidden )
419                 {
420                     p_vout->p_sys->b_cursor_autohidden = 0;
421                     SDL_ShowCursor( 1 );
422                 }
423                 else
424                 {
425                     p_vout->p_sys->i_lastmoved = mdate();
426                 }
427             }
428             break;
429
430         /* Mouse button released */
431         case SDL_MOUSEBUTTONUP:
432             switch( event.button.button )
433             {
434             case SDL_BUTTON_LEFT:
435                 {
436                     var_Get( p_vout, "mouse-button-down", &val );
437                     val.i_int &= ~1;
438                     var_Set( p_vout, "mouse-button-down", val );
439
440                     val.b_bool = true;
441                     var_Set( p_vout, "mouse-clicked", val );
442
443                     val.b_bool = false;
444                     var_Set( p_vout->p_libvlc, "intf-popupmenu", val );
445                 }
446                 break;
447
448             case SDL_BUTTON_MIDDLE:
449                 {
450                     var_Get( p_vout, "mouse-button-down", &val );
451                     val.i_int &= ~2;
452                     var_Set( p_vout, "mouse-button-down", val );
453
454                     vlc_value_t val;
455                     var_Get( p_vout->p_libvlc, "intf-show", &val );
456                     val.b_bool = !val.b_bool;
457                     var_Set( p_vout->p_libvlc, "intf-show", val );
458                 }
459                 break;
460
461             case SDL_BUTTON_RIGHT:
462                 {
463                     intf_thread_t *p_intf;
464
465                     var_Get( p_vout, "mouse-button-down", &val );
466                     val.i_int &= ~4;
467                     var_Set( p_vout, "mous-button-down", val );
468                     p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
469                                                       FIND_ANYWHERE );
470                     if( p_intf )
471                     {
472                         p_intf->b_menu_change = 1;
473                         vlc_object_release( p_intf );
474                     }
475
476                     val.b_bool = true;
477                     var_Set( p_vout->p_libvlc, "intf-popupmenu", val );
478                 }
479                 break;
480             }
481             break;
482
483         /* Mouse button pressed */
484         case SDL_MOUSEBUTTONDOWN:
485             switch( event.button.button )
486             {
487             case SDL_BUTTON_LEFT:
488                 var_Get( p_vout, "mouse-button-down", &val );
489                 val.i_int |= 1;
490                 var_Set( p_vout, "mouse-button-down", val );
491
492                 /* detect double-clicks */
493                 if( ( mdate() - p_vout->p_sys->i_lastpressed ) < 300000 )
494                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
495
496                 p_vout->p_sys->i_lastpressed = mdate();
497                 break;
498
499             case SDL_BUTTON_MIDDLE:
500                 var_Get( p_vout, "mouse-button-down", &val );
501                 val.i_int |= 2;
502                 var_Set( p_vout, "mouse-button-down", val );
503                 break;
504
505             case SDL_BUTTON_RIGHT:
506                 var_Get( p_vout, "mouse-button-down", &val );
507                 val.i_int |= 4;
508                 var_Set( p_vout, "mouse-button-down", val );
509                 break;
510             }
511             break;
512
513         /* Quit event (close the window) */
514         case SDL_QUIT:
515             {
516 #if 0
517                 playlist_t *p_playlist = pl_Yield( p_vout );
518                 if( p_playlist != NULL )
519                 {
520                     playlist_Stop( p_playlist );
521                     pl_Release( p_vout );
522                 }
523 #else
524 #warning FIXME FIXME ?
525 #endif
526             }
527             break;
528
529         /* Key pressed */
530         case SDL_KEYDOWN:
531             /* convert the key if possible */
532             val.i_int = ConvertKey( event.key.keysym.sym );
533
534             if( !val.i_int )
535             {
536                 /* Find the right caracter */
537                 if( ( event.key.keysym.unicode & 0xff80 ) == 0 )
538                 {
539                     val.i_int = event.key.keysym.unicode & 0x7f;
540                     /* FIXME: find a better solution than this
541                               hack to find the right caracter */
542                     if( val.i_int >= 1 && val.i_int <= 26 )
543                         val.i_int += 96;
544                     else if( val.i_int >= 65 && val.i_int <= 90 )
545                         val.i_int += 32;
546                 }
547             }
548
549             if( val.i_int )
550             {
551                 if( ( event.key.keysym.mod & KMOD_SHIFT ) )
552                 {
553                     val.i_int |= KEY_MODIFIER_SHIFT;
554                 }
555                 if( ( event.key.keysym.mod & KMOD_CTRL ) )
556                 {
557                     val.i_int |= KEY_MODIFIER_CTRL;
558                 }
559                 if( ( event.key.keysym.mod & KMOD_ALT ) )
560                 {
561                     val.i_int |= KEY_MODIFIER_ALT;
562                 }
563                 var_Set( p_vout->p_libvlc, "key-pressed", val );
564             }
565
566         default:
567             break;
568         }
569     }
570
571     /* Fullscreen change */
572     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
573     {
574         vlc_value_t val_fs;
575
576         /* Update the object variable and trigger callback */
577         val_fs.b_bool = !p_vout->b_fullscreen;
578         p_vout->b_fullscreen = !p_vout->b_fullscreen;
579         var_Set( p_vout, "fullscreen", val_fs );
580
581         /*TODO: add the "always on top" code here !*/
582
583         p_vout->p_sys->b_cursor_autohidden = 0;
584         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
585                         ! p_vout->p_sys->b_cursor_autohidden );
586
587         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
588         p_vout->i_changes |= VOUT_SIZE_CHANGE;
589     }
590
591     /* Crop or Aspect Ratio Changes */
592     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
593         p_vout->i_changes & VOUT_ASPECT_CHANGE )
594     {
595         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
596         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
597
598         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
599         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
600         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
601         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
602         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
603         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
604         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
605         p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
606
607         p_vout->i_changes |= VOUT_SIZE_CHANGE;
608     }
609
610     /* Size change */
611     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
612     {
613         msg_Dbg( p_vout, "video display resized (%dx%d)",
614                  p_vout->p_sys->i_width, p_vout->p_sys->i_height );
615
616         CloseDisplay( p_vout );
617         OpenDisplay( p_vout );
618
619         /* We don't need to signal the vout thread about the size change if
620          * we can handle rescaling ourselves */
621         if( p_vout->p_sys->p_overlay != NULL )
622             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
623     }
624
625     /* Pointer change */
626     if( ! p_vout->p_sys->b_cursor_autohidden &&
627         ( mdate() - p_vout->p_sys->i_lastmoved >
628             p_vout->p_sys->i_mouse_hide_timeout ) )
629     {
630         /* Hide the mouse automatically */
631         p_vout->p_sys->b_cursor_autohidden = 1;
632         SDL_ShowCursor( 0 );
633     }
634
635     return VLC_SUCCESS;
636 }
637
638 /*****************************************************************************
639  * Key events handling
640  *****************************************************************************/
641 static struct
642 {
643     SDLKey sdl_key;
644     int i_vlckey;
645 } sdlkeys_to_vlckeys[] =
646 {
647     { SDLK_F1,  KEY_F1 },
648     { SDLK_F2,  KEY_F2 },
649     { SDLK_F3,  KEY_F3 },
650     { SDLK_F4,  KEY_F4 },
651     { SDLK_F5,  KEY_F5 },
652     { SDLK_F6,  KEY_F6 },
653     { SDLK_F7,  KEY_F7 },
654     { SDLK_F8,  KEY_F8 },
655     { SDLK_F9,  KEY_F9 },
656     { SDLK_F10, KEY_F10 },
657     { SDLK_F11, KEY_F11 },
658     { SDLK_F12, KEY_F12 },
659
660     { SDLK_RETURN, KEY_ENTER },
661     { SDLK_KP_ENTER, KEY_ENTER },
662     { SDLK_SPACE, KEY_SPACE },
663     { SDLK_ESCAPE, KEY_ESC },
664
665     { SDLK_MENU, KEY_MENU },
666     { SDLK_LEFT, KEY_LEFT },
667     { SDLK_RIGHT, KEY_RIGHT },
668     { SDLK_UP, KEY_UP },
669     { SDLK_DOWN, KEY_DOWN },
670
671     { SDLK_HOME, KEY_HOME },
672     { SDLK_END, KEY_END },
673     { SDLK_PAGEUP, KEY_PAGEUP },
674     { SDLK_PAGEDOWN,  KEY_PAGEDOWN },
675
676     { SDLK_INSERT, KEY_INSERT },
677     { SDLK_DELETE, KEY_DELETE },
678     /*TODO: find a equivalent for SDL 
679     { , KEY_MEDIA_NEXT_TRACK }
680     { , KEY_MEDIA_PREV_TRACK }
681     { , KEY_VOLUME_MUTE }
682     { , KEY_VOLUME_DOWN }
683     { , KEY_VOLUME_UP }
684     { , KEY_MEDIA_PLAY_PAUSE }
685     { , KEY_MEDIA_PLAY_PAUSE }*/
686
687     { 0, 0 }
688 };
689
690 static int ConvertKey( SDLKey sdl_key )
691 {
692     int i;
693     for( i=0; sdlkeys_to_vlckeys[i].sdl_key != 0; i++ )
694     {
695         if( sdlkeys_to_vlckeys[i].sdl_key == sdl_key )
696         {
697             return sdlkeys_to_vlckeys[i].i_vlckey;
698         }
699     }
700     return 0;
701 }
702
703
704 /*****************************************************************************
705  * Display: displays previously rendered output
706  *****************************************************************************
707  * This function sends the currently rendered image to the display.
708  *****************************************************************************/
709 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
710 {
711     unsigned int x, y, w, h;
712     SDL_Rect disp;
713
714     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
715                        &x, &y, &w, &h );
716     disp.x = x;
717     disp.y = y;
718     disp.w = w;
719     disp.h = h;
720
721     if( p_vout->p_sys->p_overlay == NULL )
722     {
723         /* RGB picture */
724         SDL_Flip( p_vout->p_sys->p_display );
725     }
726     else
727     {
728         /* Overlay picture */
729         SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
730         SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
731         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
732     }
733 }
734
735 /* following functions are local */
736
737 /*****************************************************************************
738  * OpenDisplay: open and initialize SDL device
739  *****************************************************************************
740  * Open and initialize display according to preferences specified in the vout
741  * thread fields.
742  *****************************************************************************/
743 static int OpenDisplay( vout_thread_t *p_vout )
744 {
745     uint32_t i_flags;
746     int i_bpp;
747
748     /* SDL fucked up fourcc definitions on bigendian machines */
749     uint32_t i_sdl_chroma;
750     char *psz_chroma = NULL;
751     uint32_t i_chroma = 0;
752
753     bool b_overlay = config_GetInt( p_vout, "overlay" );
754
755     /* Set main window's size */
756 #if SDL_VERSION_ATLEAST(1,2,10)
757     p_vout->p_sys->i_width = p_vout->b_fullscreen ? p_vout->p_sys->i_desktop_width :
758                                                     p_vout->i_window_width;
759     p_vout->p_sys->i_height = p_vout->b_fullscreen ? p_vout->p_sys->i_desktop_height :
760                                                      p_vout->i_window_height;
761 #else
762     p_vout->p_sys->i_width = p_vout->b_fullscreen ? p_vout->output.i_width :
763                                                     p_vout->i_window_width;
764     p_vout->p_sys->i_height = p_vout->b_fullscreen ? p_vout->output.i_height :
765                                                      p_vout->i_window_height;
766 #endif
767
768     /* Initialize flags and cursor */
769     i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE | SDL_DOUBLEBUF;
770     i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
771
772     i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
773                              SDL_DEFAULT_BPP, i_flags );
774     if( i_bpp == 0 )
775     {
776         msg_Err( p_vout, "no video mode available" );
777         return VLC_EGENERIC;
778     }
779
780     p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
781                                                  p_vout->p_sys->i_height,
782                                                  i_bpp, i_flags );
783
784     if( p_vout->p_sys->p_display == NULL )
785     {
786         msg_Err( p_vout, "cannot set video mode" );
787         return VLC_EGENERIC;
788     }
789
790     SDL_LockSurface( p_vout->p_sys->p_display );
791
792     if( ( psz_chroma = config_GetPsz( p_vout, "sdl-chroma" ) ) )
793     {
794         if( strlen( psz_chroma ) >= 4 )
795         {
796             memcpy(&i_chroma, psz_chroma, 4);
797             msg_Dbg( p_vout, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
798         }
799         else
800         {
801             free( psz_chroma );
802             psz_chroma = NULL;
803         }
804     }
805
806     if( b_overlay )
807     {
808         /* Choose the chroma we will try first. */
809         do
810         {
811             if( !psz_chroma ) i_chroma = 0;
812             switch( i_chroma ? i_chroma : p_vout->render.i_chroma )
813             {
814                 case VLC_FOURCC('Y','U','Y','2'):
815                 case VLC_FOURCC('Y','U','N','V'):
816                     p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
817                     i_sdl_chroma = SDL_YUY2_OVERLAY;
818                     break;
819                 case VLC_FOURCC('U','Y','V','Y'):
820                 case VLC_FOURCC('U','Y','N','V'):
821                 case VLC_FOURCC('Y','4','2','2'):
822                     p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
823                     i_sdl_chroma = SDL_UYVY_OVERLAY;
824                     break;
825                 case VLC_FOURCC('Y','V','Y','U'):
826                     p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
827                     i_sdl_chroma = SDL_YVYU_OVERLAY;
828                     break;
829                 case VLC_FOURCC('Y','V','1','2'):
830                 case VLC_FOURCC('I','4','2','0'):
831                 case VLC_FOURCC('I','Y','U','V'):
832                 default:
833                     p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
834                     i_sdl_chroma = SDL_YV12_OVERLAY;
835                     break;
836             }
837             free( psz_chroma ); psz_chroma = NULL;
838
839             p_vout->p_sys->p_overlay =
840                 SDL_CreateYUVOverlay( 32, 32, i_sdl_chroma,
841                                       p_vout->p_sys->p_display );
842             /* FIXME: if the first overlay we find is software, don't stop,
843              * because we may find a hardware one later ... */
844         }
845         while( i_chroma && !p_vout->p_sys->p_overlay );
846
847
848         /* If this best choice failed, fall back to other chromas */
849         if( p_vout->p_sys->p_overlay == NULL )
850         {
851             p_vout->output.i_chroma = VLC_FOURCC('I','Y','U','V');
852             p_vout->p_sys->p_overlay =
853                 SDL_CreateYUVOverlay( 32, 32, SDL_IYUV_OVERLAY,
854                                       p_vout->p_sys->p_display );
855         }
856
857         if( p_vout->p_sys->p_overlay == NULL )
858         {
859             p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
860             p_vout->p_sys->p_overlay =
861                 SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
862                                       p_vout->p_sys->p_display );
863         }
864
865         if( p_vout->p_sys->p_overlay == NULL )
866         {
867             p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
868             p_vout->p_sys->p_overlay =
869                 SDL_CreateYUVOverlay( 32, 32, SDL_YUY2_OVERLAY,
870                                       p_vout->p_sys->p_display );
871         }
872     }
873
874     if( p_vout->p_sys->p_overlay == NULL )
875     {
876         if( b_overlay )
877             msg_Warn( p_vout, "no SDL overlay for 0x%.8x (%4.4s)",
878                       p_vout->render.i_chroma,
879                       (char*)&p_vout->render.i_chroma );
880         else
881             msg_Warn( p_vout, "SDL overlay disabled by the user" );
882
883         switch( p_vout->p_sys->p_display->format->BitsPerPixel )
884         {
885             case 8:
886                 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
887                 p_vout->output.pf_setpalette = SetPalette;
888                 break;
889             case 15:
890                 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
891                 break;
892             case 16:
893                 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
894                 break;
895             case 24:
896                 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
897                 break;
898             case 32:
899                 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
900                 break;
901             default:
902                 msg_Err( p_vout, "unknown screen depth %i",
903                          p_vout->p_sys->p_display->format->BitsPerPixel );
904                 SDL_UnlockSurface( p_vout->p_sys->p_display );
905                 SDL_FreeSurface( p_vout->p_sys->p_display );
906                 return VLC_EGENERIC;
907         }
908
909         p_vout->output.i_rmask = p_vout->p_sys->p_display->format->Rmask;
910         p_vout->output.i_gmask = p_vout->p_sys->p_display->format->Gmask;
911         p_vout->output.i_bmask = p_vout->p_sys->p_display->format->Bmask;
912
913         SDL_WM_SetCaption( VOUT_TITLE " (software RGB SDL output)",
914                            VOUT_TITLE " (software RGB SDL output)" );
915     }
916     else
917     {
918         if( p_vout->p_sys->p_overlay->hw_overlay )
919         {
920             SDL_WM_SetCaption( VOUT_TITLE " (hardware YUV SDL output)",
921                                VOUT_TITLE " (hardware YUV SDL output)" );
922         }
923         else
924         {
925             SDL_WM_SetCaption( VOUT_TITLE " (software YUV SDL output)",
926                                VOUT_TITLE " (software YUV SDL output)" );
927         }
928     }
929
930     SDL_EventState( SDL_KEYUP, SDL_IGNORE );               /* ignore keys up */
931
932     return VLC_SUCCESS;
933 }
934
935 /*****************************************************************************
936  * CloseDisplay: close and reset SDL device
937  *****************************************************************************
938  * This function returns all resources allocated by OpenDisplay and restore
939  * the original state of the device.
940  *****************************************************************************/
941 static void CloseDisplay( vout_thread_t *p_vout )
942 {
943     SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
944     SDL_UnlockSurface ( p_vout->p_sys->p_display );
945     SDL_FreeSurface( p_vout->p_sys->p_display );
946 }
947
948 /*****************************************************************************
949  * NewPicture: allocate a picture
950  *****************************************************************************
951  * Returns 0 on success, -1 otherwise
952  *****************************************************************************/
953 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
954 {
955     int i_width  = p_vout->output.i_width;
956     int i_height = p_vout->output.i_height;
957
958     if( p_vout->p_sys->p_overlay == NULL )
959     {
960         /* RGB picture */
961         if( p_vout->p_sys->i_surfaces )
962         {
963             /* We already allocated this surface, return */
964             return VLC_EGENERIC;
965         }
966
967         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
968
969         if( p_pic->p_sys == NULL )
970         {
971             return VLC_ENOMEM;
972         }
973
974         switch( p_vout->p_sys->p_display->format->BitsPerPixel )
975         {
976             case 8:
977                 p_pic->p->i_pixel_pitch = 1;
978                 break;
979             case 15:
980             case 16:
981                 p_pic->p->i_pixel_pitch = 2;
982                 break;
983             case 24:
984             case 32:
985                 p_pic->p->i_pixel_pitch = 4;
986                 break;
987             default:
988                 return VLC_EGENERIC;
989         }
990
991         p_pic->p->p_pixels = p_vout->p_sys->p_display->pixels;
992         p_pic->p->i_lines = p_vout->p_sys->p_display->h;
993         p_pic->p->i_visible_lines = p_vout->p_sys->p_display->h;
994         p_pic->p->i_pitch = p_vout->p_sys->p_display->pitch;
995         p_pic->p->i_visible_pitch =
996             p_pic->p->i_pixel_pitch * p_vout->p_sys->p_display->w;
997
998         p_vout->p_sys->i_surfaces++;
999
1000         p_pic->i_planes = 1;
1001     }
1002     else
1003     {
1004         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
1005
1006         if( p_pic->p_sys == NULL )
1007         {
1008             return VLC_ENOMEM;
1009         }
1010
1011         p_pic->p_sys->p_overlay =
1012             SDL_CreateYUVOverlay( i_width, i_height,
1013                                   p_vout->output.i_chroma,
1014                                   p_vout->p_sys->p_display );
1015
1016         if( p_pic->p_sys->p_overlay == NULL )
1017         {
1018             free( p_pic->p_sys );
1019             return VLC_EGENERIC;
1020         }
1021
1022         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
1023
1024         p_pic->Y_PIXELS = p_pic->p_sys->p_overlay->pixels[0];
1025         p_pic->p[Y_PLANE].i_lines = p_pic->p_sys->p_overlay->h;
1026         p_pic->p[Y_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h;
1027         p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[0];
1028
1029         switch( p_vout->output.i_chroma )
1030         {
1031         case SDL_YV12_OVERLAY:
1032             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1033             p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
1034
1035             p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
1036             p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1037             p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1038             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
1039             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1040             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1041
1042             p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
1043             p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1044             p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1045             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
1046             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1047             p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1048
1049             p_pic->i_planes = 3;
1050             break;
1051
1052         case SDL_IYUV_OVERLAY:
1053             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1054             p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
1055
1056             p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
1057             p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1058             p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1059             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
1060             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1061             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1062
1063             p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
1064             p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1065             p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1066             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
1067             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1068             p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1069
1070             p_pic->i_planes = 3;
1071             break;
1072
1073         default:
1074             p_pic->p[Y_PLANE].i_pixel_pitch = 2;
1075             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w * 2;
1076
1077             p_pic->i_planes = 1;
1078             break;
1079         }
1080     }
1081
1082     return VLC_SUCCESS;
1083 }
1084
1085 /*****************************************************************************
1086  * SetPalette: sets an 8 bpp palette
1087  *****************************************************************************/
1088 static void SetPalette( vout_thread_t *p_vout,
1089                         uint16_t *red, uint16_t *green, uint16_t *blue )
1090 {
1091     SDL_Color colors[256];
1092     int i;
1093
1094     /* Fill colors with color information */
1095     for( i = 0; i < 256; i++ )
1096     {
1097         colors[ i ].r = red[ i ] >> 8;
1098         colors[ i ].g = green[ i ] >> 8;
1099         colors[ i ].b = blue[ i ] >> 8;
1100     }
1101
1102     /* Set palette */
1103     if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
1104     {
1105         msg_Err( p_vout, "failed to set palette" );
1106     }
1107 }
1108