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