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