]> git.sesse.net Git - vlc/blob - plugins/sdl/vout_sdl.c
. Fixed the sdl plugins to match the changes in the version 1.1.5
[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
45 /*****************************************************************************
46  * vout_sys_t: video output SDL method descriptor
47  *****************************************************************************
48  * This structure is part of the video output thread descriptor.
49  * It describes the SDL specific properties of an output thread.
50  *****************************************************************************/
51 typedef struct vout_sys_s
52 {
53     SDL_Surface *   p_display;                             /* display device */
54     Uint8   *   p_buffer[2];
55                                                      /* Buffers informations */
56     boolean_t   b_must_acquire;           /* must be acquired before writing */
57 }   vout_sys_t;
58
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 static int     SDLOpenDisplay   ( vout_thread_t *p_vout, 
63                                   char *psz_display, void *p_data );
64 static void    SDLCloseDisplay  ( vout_thread_t *p_vout );
65
66 /*****************************************************************************
67  * vout_SDLCreate: allocate SDL video thread output method
68  *****************************************************************************
69  * This function allocate and initialize a SDL vout method. It uses some of the
70  * vout properties to choose the correct mode, and change them according to the
71  * mode actually used.
72  *****************************************************************************/
73 int vout_SDLCreate( vout_thread_t *p_vout, char *psz_display,
74                     int i_root_window, void *p_data )
75 {
76     /* Allocate structure */
77     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
78     if( p_vout->p_sys == NULL )
79     {
80         intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
81         return( 1 );
82     }
83
84     /* Open and initialize device */
85     
86     if( SDLOpenDisplay( p_vout, psz_display, p_data ) )
87     {
88         intf_ErrMsg( "error: can't initialize SDL display\n" );
89         free( p_vout->p_sys );
90         return( 1 );
91     }
92     return( 0 );
93 }
94
95 /*****************************************************************************
96  * vout_SDLInit: initialize SDL video thread output method
97  *****************************************************************************
98  * This function initialize the SDL display device.
99  *****************************************************************************/
100 int vout_SDLInit( vout_thread_t *p_vout )
101 {
102     /* Acquire first buffer */
103     if( p_vout->p_sys->b_must_acquire )
104     {
105                 SDL_LockSurface(p_vout->p_sys->p_display);
106     }
107
108     return( 0 );
109 }
110
111 /*****************************************************************************
112  * vout_SDLEnd: terminate Sys video thread output method
113  *****************************************************************************
114  * Terminate an output method created by vout_SDLCreate
115  *****************************************************************************/
116 void vout_SDLEnd( vout_thread_t *p_vout )
117 {
118     /* Release buffer */
119     if( p_vout->p_sys->b_must_acquire )
120     {
121         SDL_UnlockSurface ( p_vout->p_sys->p_display );
122     }
123     free( p_vout->p_sys );
124 }
125
126 /*****************************************************************************
127  * vout_SDLDestroy: destroy Sys video thread output method
128  *****************************************************************************
129  * Terminate an output method created by vout_SDLCreate
130  *****************************************************************************/
131 void vout_SDLDestroy( vout_thread_t *p_vout )
132 {
133     // SDLCloseDisplay( p_vout );
134     free( p_vout->p_sys );
135 }
136
137 /*****************************************************************************
138  * vout_SDLManage: handle Sys events
139  *****************************************************************************
140  * This function should be called regularly by video output thread. It returns
141  * a non null value if an error occured.
142  *****************************************************************************/
143 int vout_SDLManage( vout_thread_t *p_vout )
144 {
145     /* FIXME: 8bpp: change palette ?? */
146     return( 0 );
147 }
148
149 /*****************************************************************************
150  * vout_SDLDisplay: displays previously rendered output
151  *****************************************************************************
152  * This function send the currently rendered image to the display, wait until
153  * it is displayed and switch the two rendering buffer, preparing next frame.
154  *****************************************************************************/
155 void vout_SDLDisplay( vout_thread_t *p_vout )
156 {
157     SDL_Overlay * screen;
158     SDL_Rect    disp;
159     if(1)
160     {
161         /* Change display frame */
162         if( p_vout->p_sys->b_must_acquire )
163         {
164             
165             SDL_Flip( p_vout->p_sys->p_display );
166         }
167         /* Swap buffers and change write frame */
168         if( p_vout->p_sys->b_must_acquire )
169         {
170             SDL_LockSurface ( p_vout->p_sys->p_display );
171         }
172     } 
173     else 
174     {
175         /*
176          * p_vout->yuv.p_buffer contains the YUV buffer to render 
177          */
178         
179         screen = SDL_CreateYUVOverlay( p_vout->i_width, p_vout->i_height , SDL_IYUV_OVERLAY, p_vout->p_sys->p_display );
180         screen->pixels = p_vout->yuv.p_buffer;
181         disp.x = 0;
182         disp.y = 0;
183         disp.w = p_vout->i_width;
184         disp.h = p_vout->i_height;
185         SDL_DisplayYUVOverlay( screen , &disp );
186     }        
187 }
188
189 /* following functions are local */
190
191 /*****************************************************************************
192  * SDLOpenDisplay: open and initialize SDL device
193  *****************************************************************************
194  * Open and initialize display according to preferences specified in the vout
195  * thread fields.
196  *****************************************************************************/
197 static int SDLOpenDisplay( vout_thread_t *p_vout, char *psz_display, void *p_data )
198 {
199     SDL_Rect    clipping_rect;
200     
201     /* Initialize library */
202     if( SDL_Init(SDL_INIT_VIDEO) < 0 )
203     {
204         intf_ErrMsg( "error: can't initialize SDL library: %s\n",
205                      SDL_GetError() );
206         return( 1 );
207     }
208
209     /* Open display 
210      * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
211      */
212     if(psz_display != NULL && strcmp(psz_display,"fullscreen")==0)
213     {
214         p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->i_width, 
215             p_vout->i_height, 
216             15, 
217             SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN );
218         
219     } else {
220         p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->i_width, 
221             p_vout->i_height, 
222             15, 
223             SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF );
224     }
225         
226     if( p_vout->p_sys->p_display == NULL )
227     {
228         intf_ErrMsg( "error: can't open DISPLAY default display\n" );
229         return( 1 );
230     }
231     SDL_EventState(SDL_KEYUP , SDL_IGNORE);     /* ignore keys up */
232
233     /* Check buffers properties */      
234     p_vout->p_sys->b_must_acquire = 1;          /* always acquire */
235         p_vout->p_sys->p_buffer[ 0 ] =
236              p_vout->p_sys->p_display->pixels;
237         
238         SDL_Flip(p_vout->p_sys->p_display);
239         p_vout->p_sys->p_buffer[ 1 ] =
240              p_vout->p_sys->p_display->pixels;
241         SDL_Flip(p_vout->p_sys->p_display);
242
243     /* Set graphic context colors */
244
245 /*
246         col_fg.r = col_fg.g = col_fg.b = -1;
247     col_bg.r = col_bg.g = col_bg.b = 0;
248     if( ggiSetGCForeground(p_vout->p_sys->p_display,
249                            ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
250         ggiSetGCBackground(p_vout->p_sys->p_display,
251                            ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
252     {
253         intf_ErrMsg("error: can't set colors\n");
254         ggiClose( p_vout->p_sys->p_display );
255         ggiExit();
256         return( 1 );
257     }
258 */
259                 
260     /* Set clipping for text */
261     clipping_rect.x = 0;
262     clipping_rect.y = 0;
263     clipping_rect.w = p_vout->p_sys->p_display->w;
264     clipping_rect.h = p_vout->p_sys->p_display->h;
265     SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);
266
267
268         
269     /* Set thread information */
270     p_vout->i_width =           p_vout->p_sys->p_display->w;
271     p_vout->i_height =          p_vout->p_sys->p_display->h;
272
273     p_vout->i_bytes_per_line = p_vout->p_sys->p_display->format->BytesPerPixel *
274                                 p_vout->p_sys->p_display->w ;
275                 
276     p_vout->i_screen_depth =    p_vout->p_sys->p_display->format->BitsPerPixel;
277     p_vout->i_bytes_per_pixel = p_vout->p_sys->p_display->format->BytesPerPixel;
278     p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
279     p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
280     p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;
281
282     /* FIXME: palette in 8bpp ?? */
283     /* Set and initialize buffers */
284     vout_SetBuffers( p_vout, p_vout->p_sys->p_buffer[ 0 ],
285                              p_vout->p_sys->p_buffer[ 1 ] );
286
287     return( 0 );
288 }
289
290 /*****************************************************************************
291  * SDLCloseDisplay: close and reset SDL device
292  *****************************************************************************
293  * This function returns all resources allocated by SDLOpenDisplay and restore
294  * the original state of the device.
295  *****************************************************************************/
296 static void SDLCloseDisplay( vout_thread_t *p_vout )
297 {
298     SDL_FreeSurface( p_vout->p_sys->p_display );
299     SDL_Quit();
300 }
301