ff61d1d6443d4d725d74e4aee91422e921566e07
[vlc.git] / 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     unsigned int i_desktop_width;
63     unsigned int i_desktop_height;
64
65     /* For YUV output */
66     SDL_Overlay * p_overlay;   /* An overlay we keep to grab the XVideo port */
67
68     /* For RGB output */
69     int i_surfaces;
70
71     vlc_bool_t  b_cursor;
72     vlc_bool_t  b_cursor_autohidden;
73     mtime_t     i_lastmoved;
74     mtime_t     i_lastpressed;                        /* to track dbl-clicks */
75
76     vlc_mutex_t lock;
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     vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
160
161     /* Check if SDL video module has been initialized */
162     if( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
163     {
164         vlc_mutex_unlock( lock );
165         free( p_vout->p_sys );
166         return VLC_EGENERIC;
167     }
168
169     /* Allocate structure */
170     p_vout->pf_init = Init;
171     p_vout->pf_end = End;
172     p_vout->pf_manage = Manage;
173     p_vout->pf_render = NULL;
174     p_vout->pf_display = Display;
175     p_vout->pf_control = NULL;
176
177 #ifdef HAVE_SETENV
178     psz_method = config_GetPsz( p_vout, "vout" );
179     if( psz_method )
180     {
181         while( *psz_method && *psz_method != ':' )
182         {
183             psz_method++;
184         }
185
186         if( *psz_method )
187         {
188             setenv( "SDL_VIDEODRIVER", psz_method + 1, 1 );
189         }
190     }
191 #endif
192
193     /* Initialize library */
194     if( SDL_Init( SDL_INIT_VIDEO
195 #ifndef WIN32
196     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
197                 | SDL_INIT_EVENTTHREAD
198 #endif
199 #ifdef DEBUG
200     /* In debug mode you may want vlc to dump a core instead of staying
201      * stuck */
202                 | SDL_INIT_NOPARACHUTE
203 #endif
204                 ) < 0 )
205     {
206         msg_Err( p_vout, "cannot initialize SDL (%s)", SDL_GetError() );
207         free( p_vout->p_sys );
208         vlc_mutex_unlock( lock );
209         return VLC_EGENERIC;
210     }
211
212     vlc_mutex_unlock( lock );
213
214     /* Translate keys into unicode */
215     SDL_EnableUNICODE(1);
216
217     /* Get the desktop resolution */
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
221     /* Create the cursor */
222     p_vout->p_sys->b_cursor = 1;
223     p_vout->p_sys->b_cursor_autohidden = 0;
224     p_vout->p_sys->i_lastmoved = p_vout->p_sys->i_lastpressed = mdate();
225
226     if( OpenDisplay( p_vout ) )
227     {
228         msg_Err( p_vout, "cannot set up SDL (%s)", SDL_GetError() );
229         SDL_QuitSubSystem( SDL_INIT_VIDEO );
230         free( p_vout->p_sys );
231         return VLC_EGENERIC;
232     }
233
234     return VLC_SUCCESS;
235 }
236
237 /*****************************************************************************
238  * Init: initialize SDL video thread output method
239  *****************************************************************************
240  * This function initialize the SDL display device.
241  *****************************************************************************/
242 static int Init( vout_thread_t *p_vout )
243 {
244     int i_index;
245     picture_t *p_pic;
246
247     p_vout->p_sys->i_surfaces = 0;
248
249     I_OUTPUTPICTURES = 0;
250
251     /* Initialize the output structure */
252     if( p_vout->p_sys->p_overlay == NULL )
253     {
254         /* All we have is an RGB image with square pixels */
255         p_vout->output.i_width  = p_vout->p_sys->i_width;
256         p_vout->output.i_height = p_vout->p_sys->i_height;
257         p_vout->output.i_aspect = p_vout->output.i_width
258                                    * VOUT_ASPECT_FACTOR
259                                    / p_vout->output.i_height;
260     }
261     else
262     {
263         /* We may need to convert the chroma, but at least we keep the
264          * aspect ratio */
265         p_vout->output.i_width  = p_vout->render.i_width;
266         p_vout->output.i_height = p_vout->render.i_height;
267         p_vout->output.i_aspect = p_vout->render.i_aspect;
268     }
269
270     /* Try to initialize SDL_MAX_DIRECTBUFFERS direct buffers */
271     while( I_OUTPUTPICTURES < SDL_MAX_DIRECTBUFFERS )
272     {
273         p_pic = NULL;
274
275         /* Find an empty picture slot */
276         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
277         {
278             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
279             {
280                 p_pic = p_vout->p_picture + i_index;
281                 break;
282             }
283         }
284
285         /* Allocate the picture if we found one */
286         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
287         {
288             break;
289         }
290
291         p_pic->i_status = DESTROYED_PICTURE;
292         p_pic->i_type   = DIRECT_PICTURE;
293
294         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
295
296         I_OUTPUTPICTURES++;
297     }
298
299     return VLC_SUCCESS;
300 }
301
302 /*****************************************************************************
303  * End: terminate Sys video thread output method
304  *****************************************************************************
305  * Terminate an output method created by OpenVideo
306  *****************************************************************************/
307 static void End( vout_thread_t *p_vout )
308 {
309     int i_index;
310
311     /* Free the output buffers we allocated */
312     for( i_index = I_OUTPUTPICTURES ; i_index ; )
313     {
314         i_index--;
315         if( p_vout->p_sys->p_overlay == NULL )
316         {
317             /* RGB picture */
318         }
319         else
320         {
321             SDL_UnlockYUVOverlay(
322                     PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
323             SDL_FreeYUVOverlay(
324                     PP_OUTPUTPICTURE[ i_index ]->p_sys->p_overlay );
325         }
326         free( PP_OUTPUTPICTURE[ i_index ]->p_sys );
327     }
328 }
329
330 /*****************************************************************************
331  * CloseVideo: destroy Sys video thread output method
332  *****************************************************************************
333  * Terminate an output method created by vout_SDLCreate
334  *****************************************************************************/
335 static void Close ( vlc_object_t *p_this )
336 {
337     vout_thread_t * p_vout = (vout_thread_t *)p_this;
338
339     CloseDisplay( p_vout );
340     SDL_QuitSubSystem( SDL_INIT_VIDEO );
341
342     vlc_mutex_destroy( &p_vout->p_sys->lock );
343
344     free( p_vout->p_sys );
345 }
346
347 /*****************************************************************************
348  * Manage: handle Sys events
349  *****************************************************************************
350  * This function should be called regularly by video output thread. It returns
351  * a non null value if an error occurred.
352  *****************************************************************************/
353 static int Manage( vout_thread_t *p_vout )
354 {
355     SDL_Event event;                                            /* SDL event */
356     vlc_value_t val;
357     unsigned int i_width, i_height, i_x, i_y;
358
359     vlc_mutex_lock( &p_vout->p_sys->lock );
360
361     /* Process events */
362     while( SDL_PollEvent( &event ) )
363     {
364         switch( event.type )
365         {
366         /* Resizing of window */
367         case SDL_VIDEORESIZE:
368             p_vout->i_changes |= VOUT_SIZE_CHANGE;
369             p_vout->i_window_width = p_vout->p_sys->i_width = event.resize.w;
370             p_vout->i_window_height = p_vout->p_sys->i_height = event.resize.h;
371             break;
372
373         /* Mouse move */
374         case SDL_MOUSEMOTION:
375             vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
376                                p_vout->p_sys->i_height,
377                                &i_x, &i_y, &i_width, &i_height );
378
379             /* Compute the x coordinate and check if the value is
380                in [0,p_vout->fmt_in.i_visible_width] */
381             val.i_int = ( event.motion.x - i_x ) *
382                         p_vout->fmt_in.i_visible_width / i_width +
383                         p_vout->fmt_in.i_x_offset;
384
385             if( (int)(event.motion.x - i_x) < 0 )
386                 val.i_int = 0;
387             else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_width )
388                 val.i_int = p_vout->fmt_in.i_visible_width;
389
390             var_Set( p_vout, "mouse-x", val );
391
392             /* compute the y coordinate and check if the value is
393                in [0,p_vout->fmt_in.i_visible_height] */
394             val.i_int = ( event.motion.y - i_y ) *
395                         p_vout->fmt_in.i_visible_height / i_height +
396                         p_vout->fmt_in.i_y_offset;
397
398             if( (int)(event.motion.y - i_y) < 0 )
399                 val.i_int = 0;
400             else if( (unsigned int)val.i_int > p_vout->fmt_in.i_visible_height )
401                 val.i_int = p_vout->fmt_in.i_visible_height;
402
403             var_Set( p_vout, "mouse-y", val );
404
405             val.b_bool = VLC_TRUE;
406             var_Set( p_vout, "mouse-moved", val );
407
408             if( p_vout->p_sys->b_cursor )
409             {
410                 if( p_vout->p_sys->b_cursor_autohidden )
411                 {
412                     p_vout->p_sys->b_cursor_autohidden = 0;
413                     SDL_ShowCursor( 1 );
414                 }
415                 else
416                 {
417                     p_vout->p_sys->i_lastmoved = mdate();
418                 }
419             }
420             break;
421
422         /* Mouse button released */
423         case SDL_MOUSEBUTTONUP:
424             switch( event.button.button )
425             {
426             case SDL_BUTTON_LEFT:
427                 var_Get( p_vout, "mouse-button-down", &val );
428                 val.i_int &= ~1;
429                 var_Set( p_vout, "mouse-button-down", val );
430
431                 val.b_bool = VLC_TRUE;
432                 var_Set( p_vout, "mouse-clicked", val );
433                 break;
434
435             case SDL_BUTTON_MIDDLE:
436                 {
437                     playlist_t *p_playlist;
438
439                     var_Get( p_vout, "mouse-button-down", &val );
440                     val.i_int &= ~2;
441                     var_Set( p_vout, "mouse-button-down", val );
442
443                     p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
444                                               FIND_ANYWHERE );
445                     if( p_playlist != NULL )
446                     {
447                         vlc_value_t val;
448                         var_Get( p_playlist, "intf-show", &val );
449                         val.b_bool = !val.b_bool;
450                         var_Set( p_playlist, "intf-show", val );
451                         vlc_object_release( p_playlist );
452                     }
453                 }
454                 break;
455
456             case SDL_BUTTON_RIGHT:
457                 {
458                     intf_thread_t *p_intf;
459                     playlist_t *p_playlist;
460
461                     var_Get( p_vout, "mouse-button-down", &val );
462                     val.i_int &= ~4;
463                     var_Set( p_vout, "mous-button-down", val );
464                     p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
465                                                       FIND_ANYWHERE );
466                     if( p_intf )
467                     {
468                         p_intf->b_menu_change = 1;
469                         vlc_object_release( p_intf );
470                     }
471
472                     p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
473                                                 FIND_ANYWHERE );
474
475                     if( p_playlist != NULL )
476                     {
477                         vlc_value_t val;
478                         val.b_bool = VLC_TRUE;
479                         var_Set( p_playlist, "intf-popupmenu", val );
480                         vlc_object_release( p_playlist );
481                     }
482                 }
483                 break;
484             }
485             break;
486
487         /* Mouse button pressed */
488         case SDL_MOUSEBUTTONDOWN:
489             switch( event.button.button )
490             {
491             case SDL_BUTTON_LEFT:
492                 var_Get( p_vout, "mouse-button-down", &val );
493                 val.i_int |= 1;
494                 var_Set( p_vout, "mouse-button-down", val );
495
496                 /* detect double-clicks */
497                 if( ( mdate() - p_vout->p_sys->i_lastpressed ) < 300000 )
498                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
499
500                 p_vout->p_sys->i_lastpressed = mdate();
501                 break;
502
503             case SDL_BUTTON_MIDDLE:
504                 var_Get( p_vout, "mouse-button-down", &val );
505                 val.i_int |= 2;
506                 var_Set( p_vout, "mouse-button-down", val );
507                 break;
508
509             case SDL_BUTTON_RIGHT:
510                 var_Get( p_vout, "mouse-button-down", &val );
511                 val.i_int |= 4;
512                 var_Set( p_vout, "mouse-button-down", val );
513                 break;
514             }
515             break;
516
517         /* Quit event (close the window) */
518         case SDL_QUIT:
519             {
520                 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
521                 if( p_playlist != NULL )
522                 {
523                     playlist_Stop( p_playlist );
524                     vlc_object_release( p_playlist );
525                 }
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 > 2000000 ) )
628     {
629         /* Hide the mouse automatically */
630         p_vout->p_sys->b_cursor_autohidden = 1;
631         SDL_ShowCursor( 0 );
632     }
633
634     vlc_mutex_unlock( &p_vout->p_sys->lock );
635
636     return VLC_SUCCESS;
637 }
638
639 /*****************************************************************************
640  * Key events handling
641  *****************************************************************************/
642 static struct
643 {
644     SDLKey sdl_key;
645     int i_vlckey;
646 } sdlkeys_to_vlckeys[] =
647 {
648     { SDLK_F1,  KEY_F1 },
649     { SDLK_F2,  KEY_F2 },
650     { SDLK_F3,  KEY_F3 },
651     { SDLK_F4,  KEY_F4 },
652     { SDLK_F5,  KEY_F5 },
653     { SDLK_F6,  KEY_F6 },
654     { SDLK_F7,  KEY_F7 },
655     { SDLK_F8,  KEY_F8 },
656     { SDLK_F9,  KEY_F9 },
657     { SDLK_F10, KEY_F10 },
658     { SDLK_F11, KEY_F11 },
659     { SDLK_F12, KEY_F12 },
660
661     { SDLK_RETURN, KEY_ENTER },
662     { SDLK_KP_ENTER, KEY_ENTER },
663     { SDLK_SPACE, KEY_SPACE },
664     { SDLK_ESCAPE, KEY_ESC },
665
666     { SDLK_MENU, KEY_MENU },
667     { SDLK_LEFT, KEY_LEFT },
668     { SDLK_RIGHT, KEY_RIGHT },
669     { SDLK_UP, KEY_UP },
670     { SDLK_DOWN, KEY_DOWN },
671
672     { SDLK_HOME, KEY_HOME },
673     { SDLK_END, KEY_END },
674     { SDLK_PAGEUP, KEY_PAGEUP },
675     { SDLK_PAGEDOWN,  KEY_PAGEDOWN },
676
677     { SDLK_INSERT, KEY_INSERT },
678     { SDLK_DELETE, KEY_DELETE },
679     /*TODO: find a equivalent for SDL 
680     { , KEY_MEDIA_NEXT_TRACK }
681     { , KEY_MEDIA_PREV_TRACK }
682     { , KEY_VOLUME_MUTE }
683     { , KEY_VOLUME_DOWN }
684     { , KEY_VOLUME_UP }
685     { , KEY_MEDIA_PLAY_PAUSE }
686     { , KEY_MEDIA_PLAY_PAUSE }*/
687
688     { 0, 0 }
689 };
690
691 static int ConvertKey( SDLKey sdl_key )
692 {
693     int i;
694     for( i=0; sdlkeys_to_vlckeys[i].sdl_key != 0; i++ )
695     {
696         if( sdlkeys_to_vlckeys[i].sdl_key == sdl_key )
697         {
698             return sdlkeys_to_vlckeys[i].i_vlckey;
699         }
700     }
701     return 0;
702 }
703
704
705 /*****************************************************************************
706  * Display: displays previously rendered output
707  *****************************************************************************
708  * This function sends the currently rendered image to the display.
709  *****************************************************************************/
710 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
711 {
712     unsigned int x, y, w, h;
713     SDL_Rect disp;
714
715     vlc_mutex_lock( &p_vout->p_sys->lock );
716
717     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
718                        &x, &y, &w, &h );
719     disp.x = x;
720     disp.y = y;
721     disp.w = w;
722     disp.h = h;
723
724     if( p_vout->p_sys->p_overlay == NULL )
725     {
726         /* RGB picture */
727         SDL_Flip( p_vout->p_sys->p_display );
728     }
729     else
730     {
731         /* Overlay picture */
732         SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
733         SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
734         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
735     }
736
737     vlc_mutex_unlock( &p_vout->p_sys->lock );
738 }
739
740 /* following functions are local */
741
742 /*****************************************************************************
743  * OpenDisplay: open and initialize SDL device
744  *****************************************************************************
745  * Open and initialize display according to preferences specified in the vout
746  * thread fields.
747  *****************************************************************************/
748 static int OpenDisplay( vout_thread_t *p_vout )
749 {
750     uint32_t i_flags;
751     int i_bpp;
752
753     /* SDL fucked up fourcc definitions on bigendian machines */
754     uint32_t i_sdl_chroma;
755     char *psz_chroma = NULL;
756     uint32_t i_chroma = 0;
757
758     /* Set main window's size */
759     p_vout->p_sys->i_width = p_vout->b_fullscreen ? p_vout->p_sys->i_desktop_width :
760                                                     p_vout->i_window_width;
761     p_vout->p_sys->i_height = p_vout->b_fullscreen ? p_vout->p_sys->i_desktop_height :
762                                                      p_vout->i_window_height;
763
764     /* Initialize flags and cursor */
765     i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE | SDL_DOUBLEBUF;
766     i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
767
768     i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
769                              SDL_DEFAULT_BPP, i_flags );
770     if( i_bpp == 0 )
771     {
772         msg_Err( p_vout, "no video mode available" );
773         return VLC_EGENERIC;
774     }
775
776     p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
777                                                  p_vout->p_sys->i_height,
778                                                  i_bpp, i_flags );
779
780     if( p_vout->p_sys->p_display == NULL )
781     {
782         msg_Err( p_vout, "cannot set video mode" );
783         return VLC_EGENERIC;
784     }
785
786     SDL_LockSurface( p_vout->p_sys->p_display );
787
788     if( ( psz_chroma = config_GetPsz( p_vout, "sdl-chroma" ) ) )
789     {
790         if( strlen( psz_chroma ) >= 4 )
791         {
792             memcpy(&i_chroma, psz_chroma, 4);
793             msg_Dbg( p_vout, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
794         }
795         else
796         {
797             free( psz_chroma );
798             psz_chroma = NULL;
799         }
800     }
801
802     /* Choose the chroma we will try first. */
803     do
804     {
805         if( !psz_chroma ) i_chroma = 0;
806         switch( i_chroma ? i_chroma : p_vout->render.i_chroma )
807         {
808             case VLC_FOURCC('Y','U','Y','2'):
809             case VLC_FOURCC('Y','U','N','V'):
810                 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
811                 i_sdl_chroma = SDL_YUY2_OVERLAY;
812                 break;
813             case VLC_FOURCC('U','Y','V','Y'):
814             case VLC_FOURCC('U','Y','N','V'):
815             case VLC_FOURCC('Y','4','2','2'):
816                 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
817                 i_sdl_chroma = SDL_UYVY_OVERLAY;
818                 break;
819             case VLC_FOURCC('Y','V','Y','U'):
820                 p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
821                 i_sdl_chroma = SDL_YVYU_OVERLAY;
822                 break;
823             case VLC_FOURCC('Y','V','1','2'):
824             case VLC_FOURCC('I','4','2','0'):
825             case VLC_FOURCC('I','Y','U','V'):
826             default:
827                 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
828                 i_sdl_chroma = SDL_YV12_OVERLAY;
829                 break;
830         }
831         free( psz_chroma ); psz_chroma = NULL;
832
833         p_vout->p_sys->p_overlay =
834             SDL_CreateYUVOverlay( 32, 32, i_sdl_chroma,
835                                   p_vout->p_sys->p_display );
836         /* FIXME: if the first overlay we find is software, don't stop,
837          * because we may find a hardware one later ... */
838     }
839     while( i_chroma && !p_vout->p_sys->p_overlay );
840
841
842     /* If this best choice failed, fall back to other chromas */
843     if( p_vout->p_sys->p_overlay == NULL )
844     {
845         p_vout->output.i_chroma = VLC_FOURCC('I','Y','U','V');
846         p_vout->p_sys->p_overlay =
847             SDL_CreateYUVOverlay( 32, 32, SDL_IYUV_OVERLAY,
848                                   p_vout->p_sys->p_display );
849     }
850
851     if( p_vout->p_sys->p_overlay == NULL )
852     {
853         p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
854         p_vout->p_sys->p_overlay =
855             SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
856                                   p_vout->p_sys->p_display );
857     }
858
859     if( p_vout->p_sys->p_overlay == NULL )
860     {
861         p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
862         p_vout->p_sys->p_overlay =
863             SDL_CreateYUVOverlay( 32, 32, SDL_YUY2_OVERLAY,
864                                   p_vout->p_sys->p_display );
865     }
866
867     if( p_vout->p_sys->p_overlay == NULL )
868     {
869         msg_Warn( p_vout, "no SDL overlay for 0x%.8x (%4.4s)",
870                   p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma );
871
872         switch( p_vout->p_sys->p_display->format->BitsPerPixel )
873         {
874             case 8:
875                 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
876                 p_vout->output.pf_setpalette = SetPalette;
877                 break;
878             case 15:
879                 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
880                 break;
881             case 16:
882                 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
883                 break;
884             case 24:
885                 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
886                 break;
887             case 32:
888                 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
889                 break;
890             default:
891                 msg_Err( p_vout, "unknown screen depth %i",
892                          p_vout->p_sys->p_display->format->BitsPerPixel );
893                 SDL_UnlockSurface( p_vout->p_sys->p_display );
894                 SDL_FreeSurface( p_vout->p_sys->p_display );
895                 return VLC_EGENERIC;
896         }
897
898         p_vout->output.i_rmask = p_vout->p_sys->p_display->format->Rmask;
899         p_vout->output.i_gmask = p_vout->p_sys->p_display->format->Gmask;
900         p_vout->output.i_bmask = p_vout->p_sys->p_display->format->Bmask;
901
902         SDL_WM_SetCaption( VOUT_TITLE " (software RGB SDL output)",
903                            VOUT_TITLE " (software RGB SDL output)" );
904     }
905     else
906     {
907         if( p_vout->p_sys->p_overlay->hw_overlay )
908         {
909             SDL_WM_SetCaption( VOUT_TITLE " (hardware YUV SDL output)",
910                                VOUT_TITLE " (hardware YUV SDL output)" );
911         }
912         else
913         {
914             SDL_WM_SetCaption( VOUT_TITLE " (software YUV SDL output)",
915                                VOUT_TITLE " (software YUV SDL output)" );
916         }
917     }
918
919     SDL_EventState( SDL_KEYUP, SDL_IGNORE );               /* ignore keys up */
920
921     return VLC_SUCCESS;
922 }
923
924 /*****************************************************************************
925  * CloseDisplay: close and reset SDL device
926  *****************************************************************************
927  * This function returns all resources allocated by OpenDisplay and restore
928  * the original state of the device.
929  *****************************************************************************/
930 static void CloseDisplay( vout_thread_t *p_vout )
931 {
932     SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
933     SDL_UnlockSurface ( p_vout->p_sys->p_display );
934     SDL_FreeSurface( p_vout->p_sys->p_display );
935 }
936
937 /*****************************************************************************
938  * NewPicture: allocate a picture
939  *****************************************************************************
940  * Returns 0 on success, -1 otherwise
941  *****************************************************************************/
942 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
943 {
944     int i_width  = p_vout->output.i_width;
945     int i_height = p_vout->output.i_height;
946
947     if( p_vout->p_sys->p_overlay == NULL )
948     {
949         /* RGB picture */
950         if( p_vout->p_sys->i_surfaces )
951         {
952             /* We already allocated this surface, return */
953             return VLC_EGENERIC;
954         }
955
956         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
957
958         if( p_pic->p_sys == NULL )
959         {
960             return VLC_ENOMEM;
961         }
962
963         switch( p_vout->p_sys->p_display->format->BitsPerPixel )
964         {
965             case 8:
966                 p_pic->p->i_pixel_pitch = 1;
967                 break;
968             case 15:
969             case 16:
970                 p_pic->p->i_pixel_pitch = 2;
971                 break;
972             case 24:
973             case 32:
974                 p_pic->p->i_pixel_pitch = 4;
975                 break;
976             default:
977                 return VLC_EGENERIC;
978         }
979
980         p_pic->p->p_pixels = p_vout->p_sys->p_display->pixels;
981         p_pic->p->i_lines = p_vout->p_sys->p_display->h;
982         p_pic->p->i_visible_lines = p_vout->p_sys->p_display->h;
983         p_pic->p->i_pitch = p_vout->p_sys->p_display->pitch;
984         p_pic->p->i_visible_pitch =
985             p_pic->p->i_pixel_pitch * p_vout->p_sys->p_display->w;
986
987         p_vout->p_sys->i_surfaces++;
988
989         p_pic->i_planes = 1;
990     }
991     else
992     {
993         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
994
995         if( p_pic->p_sys == NULL )
996         {
997             return VLC_ENOMEM;
998         }
999
1000         p_pic->p_sys->p_overlay =
1001             SDL_CreateYUVOverlay( i_width, i_height,
1002                                   p_vout->output.i_chroma,
1003                                   p_vout->p_sys->p_display );
1004
1005         if( p_pic->p_sys->p_overlay == NULL )
1006         {
1007             free( p_pic->p_sys );
1008             return VLC_EGENERIC;
1009         }
1010
1011         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
1012
1013         p_pic->Y_PIXELS = p_pic->p_sys->p_overlay->pixels[0];
1014         p_pic->p[Y_PLANE].i_lines = p_pic->p_sys->p_overlay->h;
1015         p_pic->p[Y_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h;
1016         p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[0];
1017
1018         switch( p_vout->output.i_chroma )
1019         {
1020         case SDL_YV12_OVERLAY:
1021             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1022             p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
1023
1024             p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
1025             p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1026             p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1027             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
1028             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1029             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1030
1031             p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
1032             p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1033             p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1034             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
1035             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1036             p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1037
1038             p_pic->i_planes = 3;
1039             break;
1040
1041         case SDL_IYUV_OVERLAY:
1042             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1043             p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
1044
1045             p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
1046             p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1047             p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1048             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
1049             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1050             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1051
1052             p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
1053             p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1054             p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1055             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
1056             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1057             p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1058
1059             p_pic->i_planes = 3;
1060             break;
1061
1062         default:
1063             p_pic->p[Y_PLANE].i_pixel_pitch = 2;
1064             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w * 2;
1065
1066             p_pic->i_planes = 1;
1067             break;
1068         }
1069     }
1070
1071     return VLC_SUCCESS;
1072 }
1073
1074 /*****************************************************************************
1075  * SetPalette: sets an 8 bpp palette
1076  *****************************************************************************/
1077 static void SetPalette( vout_thread_t *p_vout,
1078                         uint16_t *red, uint16_t *green, uint16_t *blue )
1079 {
1080     SDL_Color colors[256];
1081     int i;
1082
1083     /* Fill colors with color information */
1084     for( i = 0; i < 256; i++ )
1085     {
1086         colors[ i ].r = red[ i ] >> 8;
1087         colors[ i ].g = green[ i ] >> 8;
1088         colors[ i ].b = blue[ i ] >> 8;
1089     }
1090
1091     /* Set palette */
1092     if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
1093     {
1094         msg_Err( p_vout, "failed to set palette" );
1095     }
1096 }
1097