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