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