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