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