]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
. Mouse pointer hidden/shown with middle button in SDL output
[vlc] / plugins / sdl / vout_sdl.c
1 /*****************************************************************************
2  * vout_sdl.c: SDL video output display method
3  *****************************************************************************
4  * Copyright (C) 1998, 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include "defs.h"
27
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                                /* free() */
30 #include <string.h>                                            /* strerror() */
31
32 #include <SDL/SDL.h>
33
34 #include "config.h"
35 #include "common.h"
36 #include "threads.h"
37 #include "mtime.h"
38 #include "plugins.h"
39
40 #include "video.h"
41 #include "video_output.h"
42
43 #include "intf_msg.h"
44 #include "main.h"
45
46 /*****************************************************************************
47  * vout_sys_t: video output SDL method descriptor
48  *****************************************************************************
49  * This structure is part of the video output thread descriptor.
50  * It describes the SDL specific properties of an output thread.
51  *****************************************************************************/
52 /* FIXME: SOME CLUELESS MORON DEFINED THIS STRUCTURE IN INTF_SDL.C AS WELL */
53 typedef struct vout_sys_s
54 {
55     int i_width;
56     int i_height;
57     SDL_Surface *   p_display;                             /* display device */
58     SDL_Overlay *   p_overlay;                             /* overlay device */
59     boolean_t   b_fullscreen;
60     boolean_t   b_overlay;
61     boolean_t   b_cursor;
62     boolean_t   b_reopen_display;
63     boolean_t   b_toggle_fullscreen;
64     boolean_t   b_hide_cursor;
65     Uint8   *   p_buffer[2];
66                                                      /* Buffers informations */
67 }   vout_sys_t;
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 static int     SDLOpenDisplay       ( vout_thread_t *p_vout );
73 static void    SDLCloseDisplay      ( vout_thread_t *p_vout );
74 static void    SDLToggleFullScreen  ( vout_thread_t *p_vout );
75 static void    SDLHideCursor        ( vout_thread_t *p_vout );
76 /*****************************************************************************
77  * vout_SDLCreate: allocate SDL video thread output method
78  *****************************************************************************
79  * This function allocate and initialize a SDL vout method. It uses some of the
80  * vout properties to choose the correct mode, and change them according to the
81  * mode actually used.
82  *****************************************************************************/
83 int vout_SDLCreate( vout_thread_t *p_vout, char *psz_display,
84                     int i_root_window, void *p_data )
85 {
86     /* Allocate structure */
87     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
88     if( p_vout->p_sys == NULL )
89     {
90         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
91         return( 1 );
92     }
93
94     p_vout->p_sys->p_display = NULL;
95     p_vout->p_sys->p_overlay = NULL;
96
97     /* Initialize library */
98     if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) < 0 )
99     {
100         intf_ErrMsg( "error: can't initialize SDL library: %s",
101                      SDL_GetError() );
102         free( p_vout->p_sys );
103         return( 1 );
104     }
105
106     /* Force the software yuv even if it is not used */
107     /* If we don't do this, p_vout is not correctly initialized
108        and it's impossible to switch between soft/hard yuv */
109     p_vout->b_need_render = 1;
110
111     p_vout->p_sys->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
112                                 VOUT_FULLSCREEN_DEFAULT );
113     p_vout->p_sys->b_overlay = main_GetIntVariable( VOUT_OVERLAY_VAR,
114                                 VOUT_OVERLAY_DEFAULT );
115     p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR, 
116                                 VOUT_WIDTH_DEFAULT );
117     p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
118                                 VOUT_HEIGHT_DEFAULT );
119
120     p_vout->p_sys->b_cursor = 0 ;   // TODO should be done with a main_GetInt..
121     
122     if( SDLOpenDisplay(p_vout) )
123     {
124       intf_ErrMsg( "error: can't initialize SDL library: %s",
125                    SDL_GetError() );
126       return( 1 );
127     }
128
129     p_vout->p_sys->b_toggle_fullscreen = 0;
130     p_vout->p_sys->b_hide_cursor = 0;
131     return( 0 );
132 }
133
134 /*****************************************************************************
135  * vout_SDLInit: initialize SDL video thread output method
136  *****************************************************************************
137  * This function initialize the SDL display device.
138  *****************************************************************************/
139 int vout_SDLInit( vout_thread_t *p_vout )
140 {
141     return( 0 );
142 }
143
144 /*****************************************************************************
145  * vout_SDLEnd: terminate Sys video thread output method
146  *****************************************************************************
147  * Terminate an output method created by vout_SDLCreate
148  *****************************************************************************/
149 void vout_SDLEnd( vout_thread_t *p_vout )
150 {
151     SDLCloseDisplay( p_vout );
152     SDL_Quit();
153 }
154
155 /*****************************************************************************
156  * vout_SDLDestroy: destroy Sys video thread output method
157  *****************************************************************************
158  * Terminate an output method created by vout_SDLCreate
159  *****************************************************************************/
160 void vout_SDLDestroy( vout_thread_t *p_vout )
161 {
162     free( p_vout->p_sys );
163 }
164
165 /*****************************************************************************
166  * vout_SDLManage: handle Sys events
167  *****************************************************************************
168  * This function should be called regularly by video output thread. It returns
169  * a non null value if an error occured.
170  *****************************************************************************/
171 int vout_SDLManage( vout_thread_t *p_vout )
172 {
173     /* If the display has to be reopened we do so */
174     if( p_vout->p_sys->b_reopen_display )
175     {
176         SDLCloseDisplay(p_vout);
177
178         if( SDLOpenDisplay(p_vout) )
179         {
180             intf_ErrMsg( "error: can't open DISPLAY default display" );
181             return( 1 );
182         }
183     }
184
185     /* if fullscreen has to be toggled we do so */
186     if( p_vout->p_sys->b_toggle_fullscreen )
187     {
188         SDLToggleFullScreen(p_vout);
189     }
190     
191     /* if pointer has to be hidden/shown we do so */
192     if( p_vout->p_sys->b_hide_cursor )
193     {
194         SDLHideCursor(p_vout);
195     }
196     
197     return( 0 );
198 }
199
200 /*****************************************************************************
201  * vout_SDLSetPalette: sets an 8 bpp palette
202  *****************************************************************************
203  * This function sets the palette given as an argument. It does not return
204  * anything, but could later send information on which colors it was unable
205  * to set.
206  *****************************************************************************/
207 void vout_SDLSetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green, u16 *blue, u16 *transp)
208 {
209      /* Create a display surface with a grayscale palette */
210     SDL_Color colors[256];
211     int i;
212   
213     /* Fill colors with color information */
214     for( i = 0; i < 256; i++ )
215     {
216         colors[ i ].r = red[ i ] >> 8;
217         colors[ i ].g = green[ i ] >> 8;
218         colors[ i ].b = blue[ i ] >> 8;
219     }
220     
221     /* Set palette */
222     if( SDL_SetColors(p_vout->p_sys->p_display, colors, 0, 256) == 0 )
223     {
224         intf_ErrMsg( "vout error: failed setting palette\n" );
225     }
226
227 }
228
229 /*****************************************************************************
230  * vout_SDLDisplay: displays previously rendered output
231  *****************************************************************************
232  * This function send the currently rendered image to the display, wait until
233  * it is displayed and switch the two rendering buffer, preparing next frame.
234  *****************************************************************************/
235 void vout_SDLDisplay( vout_thread_t *p_vout )
236 {
237     SDL_Rect    disp;
238     if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
239     {
240         if(p_vout->b_need_render)
241         {  
242             /* Change display frame */
243             SDL_Flip( p_vout->p_sys->p_display );
244         }
245         else
246         {
247         
248             /*
249              * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
250              * render 
251              */
252             /* TODO: support for streams other than 4:2:0 */
253             /* create the overlay if necessary */
254             if( p_vout->p_sys->p_overlay == NULL )
255             {
256                 p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay( 
257                                              p_vout->p_rendered_pic->i_width, 
258                                              p_vout->p_rendered_pic->i_height,
259                                              SDL_YV12_OVERLAY, 
260                                              p_vout->p_sys->p_display
261                                            );
262                 intf_Msg("vout: YUV acceleration %s",
263                             p_vout->p_sys->p_overlay->hw_overlay
264                             ? "activated" : "unavailable !" ); 
265             }
266
267             SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
268             /* copy the data into video buffers */
269             /* Y first */
270             memcpy(p_vout->p_sys->p_overlay->pixels[0],
271                    p_vout->p_rendered_pic->p_y,
272                    p_vout->p_sys->p_overlay->h *
273                    p_vout->p_sys->p_overlay->pitches[0]);
274             /* then V */
275             memcpy(p_vout->p_sys->p_overlay->pixels[1],
276                    p_vout->p_rendered_pic->p_v,
277                    p_vout->p_sys->p_overlay->h *
278                    p_vout->p_sys->p_overlay->pitches[1] / 2);
279             /* and U */
280             memcpy(p_vout->p_sys->p_overlay->pixels[2],
281                    p_vout->p_rendered_pic->p_u,
282                    p_vout->p_sys->p_overlay->h *
283                    p_vout->p_sys->p_overlay->pitches[2] / 2);
284
285             disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
286             disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
287             disp.x = (p_vout->i_width - disp.w)/2;
288             disp.y = (p_vout->i_height - disp.h)/2;
289
290             SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
291             SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
292         }
293     }
294 }
295
296 /* following functions are local */
297
298 /*****************************************************************************
299  * SDLOpenDisplay: open and initialize SDL device
300  *****************************************************************************
301  * Open and initialize display according to preferences specified in the vout
302  * thread fields.
303  *****************************************************************************/
304 static int SDLOpenDisplay( vout_thread_t *p_vout )
305 {
306     SDL_Rect    clipping_rect;
307     Uint32      flags;
308     int bpp;
309     /* Open display 
310      * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
311      */
312
313     /* init flags and cursor */
314     flags = SDL_ANYFORMAT | SDL_HWPALETTE;
315
316     if( p_vout->p_sys->b_fullscreen )
317         flags |= SDL_FULLSCREEN;
318     else
319         flags |= SDL_RESIZABLE;
320
321     if( p_vout->b_need_render )
322         flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
323     else
324         flags |= SDL_SWSURFACE; /* save video memory */
325
326     bpp = SDL_VideoModeOK(p_vout->p_sys->i_width,
327                           p_vout->p_sys->i_height,
328                           p_vout->i_screen_depth, flags);
329
330     if(bpp == 0)
331     {
332         intf_ErrMsg( "error: can't open DISPLAY default display" );
333         return( 1 );
334     }
335
336     p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
337                                                 p_vout->p_sys->i_height,
338                                                 bpp, flags);
339
340     if( p_vout->p_sys->p_display == NULL )
341     {
342         intf_ErrMsg( "error: can't open DISPLAY default display" );
343         return( 1 );
344     }
345
346     SDL_LockSurface(p_vout->p_sys->p_display);
347
348     if( p_vout->p_sys->b_fullscreen )
349         SDL_ShowCursor( 0 );
350     else
351         SDL_ShowCursor( 1 );
352
353     SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
354     SDL_EventState(SDL_KEYUP , SDL_IGNORE);                 /* ignore keys up */
355     SDL_EventState(SDL_MOUSEBUTTONUP, SDL_IGNORE);          
356     if( p_vout->b_need_render )
357     {
358         p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
359         SDL_Flip(p_vout->p_sys->p_display);
360         p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
361         SDL_Flip(p_vout->p_sys->p_display);
362
363         /* Set clipping for text */
364         clipping_rect.x = 0;
365         clipping_rect.y = 0;
366         clipping_rect.w = p_vout->p_sys->p_display->w;
367         clipping_rect.h = p_vout->p_sys->p_display->h;
368         SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
369
370         /* Set thread information */
371         p_vout->i_width =           p_vout->p_sys->p_display->w;
372         p_vout->i_height =          p_vout->p_sys->p_display->h;
373         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
374
375         p_vout->i_screen_depth =
376             p_vout->p_sys->p_display->format->BitsPerPixel;
377         p_vout->i_bytes_per_pixel =
378             p_vout->p_sys->p_display->format->BytesPerPixel;
379
380         p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
381         p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
382         p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;
383
384         /* FIXME: palette in 8bpp ?? */
385         /* Set and initialize buffers */
386         vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
387                                  p_vout->p_sys->p_buffer[ 1 ] );
388     }
389     else
390     {
391         p_vout->p_sys->p_buffer[ 0 ] = p_vout->p_sys->p_display->pixels;
392         p_vout->p_sys->p_buffer[ 1 ] = p_vout->p_sys->p_display->pixels;
393
394         /* Set thread information */
395         p_vout->i_width =           p_vout->p_sys->p_display->w;
396         p_vout->i_height =          p_vout->p_sys->p_display->h;
397         p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;
398
399         vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
400                                  p_vout->p_sys->p_buffer[ 1 ] );
401     }
402
403     p_vout->i_changes |= VOUT_YUV_CHANGE;
404
405     p_vout->p_sys->b_reopen_display = 0;
406
407     return( 0 );
408 }
409
410 /*****************************************************************************
411  * SDLCloseDisplay: close and reset SDL device
412  *****************************************************************************
413  * This function returns all resources allocated by SDLOpenDisplay and restore
414  * the original state of the device.
415  *****************************************************************************/
416 static void SDLCloseDisplay( vout_thread_t *p_vout )
417 {
418     if( p_vout->p_sys->p_display != NULL )
419     {
420         if( p_vout->p_sys->p_overlay != NULL )
421         {            
422             SDL_FreeYUVOverlay(p_vout->p_sys->p_overlay);
423             p_vout->p_sys->p_overlay = NULL;
424         }
425         SDL_UnlockSurface ( p_vout->p_sys->p_display );
426         SDL_FreeSurface( p_vout->p_sys->p_display );
427         p_vout->p_sys->p_display = NULL;
428     }
429 }
430
431 /*****************************************************************************
432  * SDLToggleFullScreen: toggle fullscreen
433  *****************************************************************************
434  * This function toggles the fullscreen state of the surface.
435  * And  - hide the pointer if switching to fullscreen
436  *      - show the pointer if leaving fullscreen state
437  *****************************************************************************/
438 static void SDLToggleFullScreen( vout_thread_t *p_vout )
439 {
440     SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);
441
442     if( p_vout->p_sys->b_fullscreen )
443     {
444         p_vout->p_sys->b_cursor=1;
445     }
446     else
447     {
448         p_vout->p_sys->b_cursor=0;
449     }
450     
451     p_vout->p_sys->b_toggle_fullscreen = 0;
452     SDLHideCursor(p_vout);
453 }
454
455 /*****************************************************************************
456  * SDLHideCursor: Hide/Show mouse pointer
457  *****************************************************************************
458  * This function hides/shows the mouse pointer inside the main window.
459  *****************************************************************************/
460 static void SDLHideCursor( vout_thread_t *p_vout )
461 {
462     if( p_vout->p_sys->b_cursor==1 )
463     {
464         SDL_ShowCursor( 0 );
465     }
466     else
467     {
468         SDL_ShowCursor( 1 );
469     }
470     p_vout->p_sys->b_hide_cursor = 0;
471 }