]> git.sesse.net Git - vlc/blob - modules/video_output/sdl.c
Fix an error in key handling with sdl
[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     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             val.i_int = ( event.motion.x - i_x )
380                          * p_vout->render.i_width / i_width;
381             var_Set( p_vout, "mouse-x", val );
382             val.i_int = ( event.motion.y - i_y )
383                          * p_vout->render.i_height / i_height;
384             var_Set( p_vout, "mouse-y", val );
385
386             val.b_bool = VLC_TRUE;
387             var_Set( p_vout, "mouse-moved", val );
388
389             if( p_vout->p_sys->b_cursor &&
390                 (abs(event.motion.xrel) > 2 || abs(event.motion.yrel) > 2) )
391             {
392                 if( p_vout->p_sys->b_cursor_autohidden )
393                 {
394                     p_vout->p_sys->b_cursor_autohidden = 0;
395                     SDL_ShowCursor( 1 );
396                 }
397                 else
398                 {
399                     p_vout->p_sys->i_lastmoved = mdate();
400                 }
401             }
402             break;
403
404         /* Mouse button released */
405         case SDL_MOUSEBUTTONUP:
406             switch( event.button.button )
407             {
408             case SDL_BUTTON_LEFT:
409                 var_Get( p_vout, "mouse-button-down", &val );
410                 val.i_int &= ~1;
411                 var_Set( p_vout, "mouse-button-down", val );
412
413                 val.b_bool = VLC_TRUE;
414                 var_Set( p_vout, "mouse-clicked", val );
415                 break;
416
417             case SDL_BUTTON_MIDDLE:
418                 {
419                     playlist_t *p_playlist;
420
421                     var_Get( p_vout, "mouse-button-down", &val );
422                     val.i_int &= ~2;
423                     var_Set( p_vout, "mouse-button-down", val );
424
425                     p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
426                                               FIND_ANYWHERE );
427                     if( p_playlist != NULL )
428                     {
429                         vlc_value_t val;
430                         var_Get( p_playlist, "intf-show", &val );
431                         val.b_bool = !val.b_bool;
432                         var_Set( p_playlist, "intf-show", val );
433                         vlc_object_release( p_playlist );
434                     }
435                 }
436                 break;
437
438             case SDL_BUTTON_RIGHT:
439                 {
440                     intf_thread_t *p_intf;
441                     playlist_t *p_playlist;
442
443                     var_Get( p_vout, "mouse-button-down", &val );
444                     val.i_int &= ~4;
445                     var_Set( p_vout, "mous-button-down", val );
446                     p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
447                                                       FIND_ANYWHERE );
448                     if( p_intf )
449                     {
450                         p_intf->b_menu_change = 1;
451                         vlc_object_release( p_intf );
452                     }
453
454                     p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
455                                                 FIND_ANYWHERE );
456
457                     if( p_playlist != NULL )
458                     {
459                         vlc_value_t val;
460                         val.b_bool = VLC_TRUE;
461                         var_Set( p_playlist, "intf-popupmenu", val );
462                         vlc_object_release( p_playlist );
463                     }
464                 }
465                 break;
466             }
467             break;
468
469         /* Mouse button pressed */
470         case SDL_MOUSEBUTTONDOWN:
471             switch( event.button.button )
472             {
473             case SDL_BUTTON_LEFT:
474                 var_Get( p_vout, "mouse-button-down", &val );
475                 val.i_int |= 1;
476                 var_Set( p_vout, "mouse-button-down", val );
477
478                 /* detect double-clicks */
479                 if( ( mdate() - p_vout->p_sys->i_lastpressed ) < 300000 )
480                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
481
482                 p_vout->p_sys->i_lastpressed = mdate();
483                 break;
484
485             case SDL_BUTTON_MIDDLE:
486                 var_Get( p_vout, "mouse-button-down", &val );
487                 val.i_int |= 2;
488                 var_Set( p_vout, "mouse-button-down", val );
489                 break;
490
491             case SDL_BUTTON_RIGHT:
492                 var_Get( p_vout, "mouse-button-down", &val );
493                 val.i_int |= 4;
494                 var_Set( p_vout, "mouse-button-down", val );
495                 break;
496             }
497             break;
498
499         /* Quit event (close the window) */
500         case SDL_QUIT:
501             {
502                 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
503                 if( p_playlist != NULL )
504                 {
505                     playlist_Stop( p_playlist );
506                     vlc_object_release( p_playlist );
507                 }
508             }
509             break;
510
511         /* Key pressed */
512         case SDL_KEYDOWN:
513             /* convert the key if possible */
514             val.i_int = ConvertKey( event.key.keysym.sym );
515
516             if( !val.i_int )
517             {
518                 /* Find the right caracter */
519                 if( ( event.key.keysym.unicode & 0xff80 ) == 0 )
520                 {
521                     val.i_int = event.key.keysym.unicode & 0x7f;
522                     /* FIXME: find a better solution than this
523                               hack to find the right caracter */
524                     if( val.i_int >= 1 && val.i_int <= 26 )
525                         val.i_int += 96;
526                     else if( val.i_int >= 65 && val.i_int <= 90 )
527                         val.i_int += 32;
528                 }
529             }
530
531             if( val.i_int )
532             {
533                 if( ( event.key.keysym.mod & KMOD_SHIFT ) )
534                 {
535                     val.i_int |= KEY_MODIFIER_SHIFT;
536                 }
537                 if( ( event.key.keysym.mod & KMOD_CTRL ) )
538                 {
539                     val.i_int |= KEY_MODIFIER_CTRL;
540                 }
541                 if( ( event.key.keysym.mod & KMOD_ALT ) )
542                 {
543                     val.i_int |= KEY_MODIFIER_ALT;
544                 }
545                 var_Set( p_vout->p_libvlc, "key-pressed", val );
546             }
547
548         default:
549             break;
550         }
551     }
552
553     /* Fullscreen change */
554     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
555     {
556         vlc_value_t val_fs;
557
558         /* Update the object variable and trigger callback */
559         val_fs.b_bool = !p_vout->b_fullscreen;
560         p_vout->b_fullscreen = !p_vout->b_fullscreen;
561         var_Set( p_vout, "fullscreen", val_fs );
562
563         /*TODO: add the "always on top" code here !*/
564
565         p_vout->p_sys->b_cursor_autohidden = 0;
566         SDL_ShowCursor( p_vout->p_sys->b_cursor &&
567                         ! p_vout->p_sys->b_cursor_autohidden );
568
569         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
570         p_vout->i_changes |= VOUT_SIZE_CHANGE;
571     }
572
573     /* Crop or Aspect Ratio Changes */
574     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
575         p_vout->i_changes & VOUT_ASPECT_CHANGE )
576     {
577         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
578         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
579
580         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
581         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
582         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
583         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
584         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
585         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
586         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
587         p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
588
589         p_vout->i_changes |= VOUT_SIZE_CHANGE;
590     }
591
592     /* Size change */
593     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
594     {
595         msg_Dbg( p_vout, "video display resized (%dx%d)",
596                  p_vout->p_sys->i_width, p_vout->p_sys->i_height );
597
598         CloseDisplay( p_vout );
599         OpenDisplay( p_vout );
600
601         /* We don't need to signal the vout thread about the size change if
602          * we can handle rescaling ourselves */
603         if( p_vout->p_sys->p_overlay != NULL )
604             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
605     }
606
607     /* Pointer change */
608     if( ! p_vout->p_sys->b_cursor_autohidden &&
609         ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
610     {
611         /* Hide the mouse automatically */
612         p_vout->p_sys->b_cursor_autohidden = 1;
613         SDL_ShowCursor( 0 );
614     }
615
616     vlc_mutex_unlock( &p_vout->p_sys->lock );
617
618     return VLC_SUCCESS;
619 }
620
621 /*****************************************************************************
622  * Key events handling
623  *****************************************************************************/
624 static struct
625 {
626     SDLKey sdl_key;
627     int i_vlckey;
628 } sdlkeys_to_vlckeys[] =
629 {
630     { SDLK_F1,  KEY_F1 },
631     { SDLK_F2,  KEY_F2 },
632     { SDLK_F3,  KEY_F3 },
633     { SDLK_F4,  KEY_F4 },
634     { SDLK_F5,  KEY_F5 },
635     { SDLK_F6,  KEY_F6 },
636     { SDLK_F7,  KEY_F7 },
637     { SDLK_F8,  KEY_F8 },
638     { SDLK_F9,  KEY_F9 },
639     { SDLK_F10, KEY_F10 },
640     { SDLK_F11, KEY_F11 },
641     { SDLK_F12, KEY_F12 },
642
643     { SDLK_RETURN, KEY_ENTER },
644     { SDLK_KP_ENTER, KEY_ENTER },
645     { SDLK_SPACE, KEY_SPACE },
646     { SDLK_ESCAPE, KEY_ESC },
647
648     { SDLK_MENU, KEY_MENU },
649     { SDLK_LEFT, KEY_LEFT },
650     { SDLK_RIGHT, KEY_RIGHT },
651     { SDLK_UP, KEY_UP },
652     { SDLK_DOWN, KEY_DOWN },
653
654     { SDLK_HOME, KEY_HOME },
655     { SDLK_END, KEY_END },
656     { SDLK_PAGEUP, KEY_PAGEUP },
657     { SDLK_PAGEDOWN,  KEY_PAGEDOWN },
658
659     { SDLK_INSERT, KEY_INSERT },
660     { SDLK_DELETE, KEY_DELETE },
661     /*TODO: find a equivalent for SDL 
662     { , KEY_MEDIA_NEXT_TRACK }
663     { , KEY_MEDIA_PREV_TRACK }
664     { , KEY_VOLUME_MUTE }
665     { , KEY_VOLUME_DOWN }
666     { , KEY_VOLUME_UP }
667     { , KEY_MEDIA_PLAY_PAUSE }
668     { , KEY_MEDIA_PLAY_PAUSE }*/
669
670     { 0, 0 }
671 };
672
673 static int ConvertKey( SDLKey sdl_key )
674 {
675     int i;
676     for( i=0; sdlkeys_to_vlckeys[i].sdl_key != 0; i++ )
677     {
678         if( sdlkeys_to_vlckeys[i].sdl_key == sdl_key )
679         {
680             return sdlkeys_to_vlckeys[i].i_vlckey;
681         }
682     }
683     return 0;
684 }
685
686
687 /*****************************************************************************
688  * Display: displays previously rendered output
689  *****************************************************************************
690  * This function sends the currently rendered image to the display.
691  *****************************************************************************/
692 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
693 {
694     unsigned int x, y, w, h;
695     SDL_Rect disp;
696
697     vlc_mutex_lock( &p_vout->p_sys->lock );
698
699     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
700                        &x, &y, &w, &h );
701     disp.x = x;
702     disp.y = y;
703     disp.w = w;
704     disp.h = h;
705
706     if( p_vout->p_sys->p_overlay == NULL )
707     {
708         /* RGB picture */
709         SDL_Flip( p_vout->p_sys->p_display );
710     }
711     else
712     {
713         /* Overlay picture */
714         SDL_UnlockYUVOverlay( p_pic->p_sys->p_overlay);
715         SDL_DisplayYUVOverlay( p_pic->p_sys->p_overlay , &disp );
716         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay);
717     }
718
719     vlc_mutex_unlock( &p_vout->p_sys->lock );
720 }
721
722 /* following functions are local */
723
724 /*****************************************************************************
725  * OpenDisplay: open and initialize SDL device
726  *****************************************************************************
727  * Open and initialize display according to preferences specified in the vout
728  * thread fields.
729  *****************************************************************************/
730 static int OpenDisplay( vout_thread_t *p_vout )
731 {
732     uint32_t i_flags;
733     int i_bpp;
734
735     /* SDL fucked up fourcc definitions on bigendian machines */
736     uint32_t i_sdl_chroma;
737     char *psz_chroma = NULL;
738     uint32_t i_chroma = 0;
739
740     /* Set main window's size */
741     p_vout->p_sys->i_width = p_vout->b_fullscreen ? p_vout->p_sys->i_desktop_width :
742                                                     p_vout->i_window_width;
743     p_vout->p_sys->i_height = p_vout->b_fullscreen ? p_vout->p_sys->i_desktop_height :
744                                                      p_vout->i_window_height;
745
746     /* Initialize flags and cursor */
747     i_flags = SDL_ANYFORMAT | SDL_HWPALETTE | SDL_HWSURFACE | SDL_DOUBLEBUF;
748     i_flags |= p_vout->b_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE;
749
750     i_bpp = SDL_VideoModeOK( p_vout->p_sys->i_width, p_vout->p_sys->i_height,
751                              SDL_DEFAULT_BPP, i_flags );
752     if( i_bpp == 0 )
753     {
754         msg_Err( p_vout, "no video mode available" );
755         return VLC_EGENERIC;
756     }
757
758     p_vout->p_sys->p_display = SDL_SetVideoMode( p_vout->p_sys->i_width,
759                                                  p_vout->p_sys->i_height,
760                                                  i_bpp, i_flags );
761
762     if( p_vout->p_sys->p_display == NULL )
763     {
764         msg_Err( p_vout, "cannot set video mode" );
765         return VLC_EGENERIC;
766     }
767
768     SDL_LockSurface( p_vout->p_sys->p_display );
769
770     if( ( psz_chroma = config_GetPsz( p_vout, "sdl-chroma" ) ) )
771     {
772         if( strlen( psz_chroma ) >= 4 )
773         {
774             memcpy(&i_chroma, psz_chroma, 4);
775             msg_Dbg( p_vout, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
776         }
777         else
778         {
779             free( psz_chroma );
780             psz_chroma = NULL;
781         }
782     }
783
784     /* Choose the chroma we will try first. */
785     do
786     {
787         if( !psz_chroma ) i_chroma = 0;
788         switch( i_chroma ? i_chroma : p_vout->render.i_chroma )
789         {
790             case VLC_FOURCC('Y','U','Y','2'):
791             case VLC_FOURCC('Y','U','N','V'):
792                 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
793                 i_sdl_chroma = SDL_YUY2_OVERLAY;
794                 break;
795             case VLC_FOURCC('U','Y','V','Y'):
796             case VLC_FOURCC('U','Y','N','V'):
797             case VLC_FOURCC('Y','4','2','2'):
798                 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
799                 i_sdl_chroma = SDL_UYVY_OVERLAY;
800                 break;
801             case VLC_FOURCC('Y','V','Y','U'):
802                 p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
803                 i_sdl_chroma = SDL_YVYU_OVERLAY;
804                 break;
805             case VLC_FOURCC('Y','V','1','2'):
806             case VLC_FOURCC('I','4','2','0'):
807             case VLC_FOURCC('I','Y','U','V'):
808             default:
809                 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
810                 i_sdl_chroma = SDL_YV12_OVERLAY;
811                 break;
812         }
813         free( psz_chroma ); psz_chroma = NULL;
814
815         p_vout->p_sys->p_overlay =
816             SDL_CreateYUVOverlay( 32, 32, i_sdl_chroma,
817                                   p_vout->p_sys->p_display );
818         /* FIXME: if the first overlay we find is software, don't stop,
819          * because we may find a hardware one later ... */
820     }
821     while( i_chroma && !p_vout->p_sys->p_overlay );
822
823
824     /* If this best choice failed, fall back to other chromas */
825     if( p_vout->p_sys->p_overlay == NULL )
826     {
827         p_vout->output.i_chroma = VLC_FOURCC('I','Y','U','V');
828         p_vout->p_sys->p_overlay =
829             SDL_CreateYUVOverlay( 32, 32, SDL_IYUV_OVERLAY,
830                                   p_vout->p_sys->p_display );
831     }
832
833     if( p_vout->p_sys->p_overlay == NULL )
834     {
835         p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
836         p_vout->p_sys->p_overlay =
837             SDL_CreateYUVOverlay( 32, 32, SDL_YV12_OVERLAY,
838                                   p_vout->p_sys->p_display );
839     }
840
841     if( p_vout->p_sys->p_overlay == NULL )
842     {
843         p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
844         p_vout->p_sys->p_overlay =
845             SDL_CreateYUVOverlay( 32, 32, SDL_YUY2_OVERLAY,
846                                   p_vout->p_sys->p_display );
847     }
848
849     if( p_vout->p_sys->p_overlay == NULL )
850     {
851         msg_Warn( p_vout, "no SDL overlay for 0x%.8x (%4.4s)",
852                   p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma );
853
854         switch( p_vout->p_sys->p_display->format->BitsPerPixel )
855         {
856             case 8:
857                 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
858                 p_vout->output.pf_setpalette = SetPalette;
859                 break;
860             case 15:
861                 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
862                 break;
863             case 16:
864                 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
865                 break;
866             case 24:
867                 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
868                 break;
869             case 32:
870                 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
871                 break;
872             default:
873                 msg_Err( p_vout, "unknown screen depth %i",
874                          p_vout->p_sys->p_display->format->BitsPerPixel );
875                 SDL_UnlockSurface( p_vout->p_sys->p_display );
876                 SDL_FreeSurface( p_vout->p_sys->p_display );
877                 return VLC_EGENERIC;
878         }
879
880         p_vout->output.i_rmask = p_vout->p_sys->p_display->format->Rmask;
881         p_vout->output.i_gmask = p_vout->p_sys->p_display->format->Gmask;
882         p_vout->output.i_bmask = p_vout->p_sys->p_display->format->Bmask;
883
884         SDL_WM_SetCaption( VOUT_TITLE " (software RGB SDL output)",
885                            VOUT_TITLE " (software RGB SDL output)" );
886     }
887     else
888     {
889         if( p_vout->p_sys->p_overlay->hw_overlay )
890         {
891             SDL_WM_SetCaption( VOUT_TITLE " (hardware YUV SDL output)",
892                                VOUT_TITLE " (hardware YUV SDL output)" );
893         }
894         else
895         {
896             SDL_WM_SetCaption( VOUT_TITLE " (software YUV SDL output)",
897                                VOUT_TITLE " (software YUV SDL output)" );
898         }
899     }
900
901     SDL_EventState( SDL_KEYUP, SDL_IGNORE );               /* ignore keys up */
902
903     return VLC_SUCCESS;
904 }
905
906 /*****************************************************************************
907  * CloseDisplay: close and reset SDL device
908  *****************************************************************************
909  * This function returns all resources allocated by OpenDisplay and restore
910  * the original state of the device.
911  *****************************************************************************/
912 static void CloseDisplay( vout_thread_t *p_vout )
913 {
914     SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
915     SDL_UnlockSurface ( p_vout->p_sys->p_display );
916     SDL_FreeSurface( p_vout->p_sys->p_display );
917 }
918
919 /*****************************************************************************
920  * NewPicture: allocate a picture
921  *****************************************************************************
922  * Returns 0 on success, -1 otherwise
923  *****************************************************************************/
924 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
925 {
926     int i_width  = p_vout->output.i_width;
927     int i_height = p_vout->output.i_height;
928
929     if( p_vout->p_sys->p_overlay == NULL )
930     {
931         /* RGB picture */
932         if( p_vout->p_sys->i_surfaces )
933         {
934             /* We already allocated this surface, return */
935             return VLC_EGENERIC;
936         }
937
938         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
939
940         if( p_pic->p_sys == NULL )
941         {
942             return VLC_ENOMEM;
943         }
944
945         switch( p_vout->p_sys->p_display->format->BitsPerPixel )
946         {
947             case 8:
948                 p_pic->p->i_pixel_pitch = 1;
949                 break;
950             case 15:
951             case 16:
952                 p_pic->p->i_pixel_pitch = 2;
953                 break;
954             case 24:
955             case 32:
956                 p_pic->p->i_pixel_pitch = 4;
957                 break;
958             default:
959                 return VLC_EGENERIC;
960         }
961
962         p_pic->p->p_pixels = p_vout->p_sys->p_display->pixels;
963         p_pic->p->i_lines = p_vout->p_sys->p_display->h;
964         p_pic->p->i_visible_lines = p_vout->p_sys->p_display->h;
965         p_pic->p->i_pitch = p_vout->p_sys->p_display->pitch;
966         p_pic->p->i_visible_pitch =
967             p_pic->p->i_pixel_pitch * p_vout->p_sys->p_display->w;
968
969         p_vout->p_sys->i_surfaces++;
970
971         p_pic->i_planes = 1;
972     }
973     else
974     {
975         p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
976
977         if( p_pic->p_sys == NULL )
978         {
979             return VLC_ENOMEM;
980         }
981
982         p_pic->p_sys->p_overlay =
983             SDL_CreateYUVOverlay( i_width, i_height,
984                                   p_vout->output.i_chroma,
985                                   p_vout->p_sys->p_display );
986
987         if( p_pic->p_sys->p_overlay == NULL )
988         {
989             free( p_pic->p_sys );
990             return VLC_EGENERIC;
991         }
992
993         SDL_LockYUVOverlay( p_pic->p_sys->p_overlay );
994
995         p_pic->Y_PIXELS = p_pic->p_sys->p_overlay->pixels[0];
996         p_pic->p[Y_PLANE].i_lines = p_pic->p_sys->p_overlay->h;
997         p_pic->p[Y_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h;
998         p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[0];
999
1000         switch( p_vout->output.i_chroma )
1001         {
1002         case SDL_YV12_OVERLAY:
1003             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1004             p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
1005
1006             p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
1007             p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1008             p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1009             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
1010             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1011             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1012
1013             p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
1014             p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1015             p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1016             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
1017             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1018             p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1019
1020             p_pic->i_planes = 3;
1021             break;
1022
1023         case SDL_IYUV_OVERLAY:
1024             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1025             p_pic->p[Y_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w;
1026
1027             p_pic->U_PIXELS = p_pic->p_sys->p_overlay->pixels[1];
1028             p_pic->p[U_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1029             p_pic->p[U_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1030             p_pic->p[U_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[1];
1031             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1032             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1033
1034             p_pic->V_PIXELS = p_pic->p_sys->p_overlay->pixels[2];
1035             p_pic->p[V_PLANE].i_lines = p_pic->p_sys->p_overlay->h / 2;
1036             p_pic->p[V_PLANE].i_visible_lines = p_pic->p_sys->p_overlay->h / 2;
1037             p_pic->p[V_PLANE].i_pitch = p_pic->p_sys->p_overlay->pitches[2];
1038             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1039             p_pic->p[V_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w / 2;
1040
1041             p_pic->i_planes = 3;
1042             break;
1043
1044         default:
1045             p_pic->p[Y_PLANE].i_pixel_pitch = 2;
1046             p_pic->p[U_PLANE].i_visible_pitch = p_pic->p_sys->p_overlay->w * 2;
1047
1048             p_pic->i_planes = 1;
1049             break;
1050         }
1051     }
1052
1053     return VLC_SUCCESS;
1054 }
1055
1056 /*****************************************************************************
1057  * SetPalette: sets an 8 bpp palette
1058  *****************************************************************************/
1059 static void SetPalette( vout_thread_t *p_vout,
1060                         uint16_t *red, uint16_t *green, uint16_t *blue )
1061 {
1062     SDL_Color colors[256];
1063     int i;
1064
1065     /* Fill colors with color information */
1066     for( i = 0; i < 256; i++ )
1067     {
1068         colors[ i ].r = red[ i ] >> 8;
1069         colors[ i ].g = green[ i ] >> 8;
1070         colors[ i ].b = blue[ i ] >> 8;
1071     }
1072
1073     /* Set palette */
1074     if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
1075     {
1076         msg_Err( p_vout, "failed to set palette" );
1077     }
1078 }
1079