]> git.sesse.net Git - vlc/blob - modules/video_output/svgalib.c
* ./modules/video_output/svgalib.c: SVGAlib video output module.
[vlc] / modules / video_output / svgalib.c
1 /*****************************************************************************
2  * svgalib.c : SVGAlib plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: svgalib.c,v 1.1 2002/08/21 15:10:33 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *      
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/vout.h>
31
32 #include <vga.h>
33 #include <vgagl.h>
34 #include <vgakeyboard.h>
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Create    ( vlc_object_t * );
40 static void Destroy   ( vlc_object_t * );
41
42 static int  Init      ( vout_thread_t * );
43 static void End       ( vout_thread_t * );
44 static int  Manage    ( vout_thread_t * );
45 static void Display   ( vout_thread_t *, picture_t * );
46
47 static void SetPalette( vout_thread_t *, u16 *, u16 *, u16 * );
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 vlc_module_begin();                                            
53     set_description( _("SVGAlib module") );
54     set_capability( "video output", 0 );
55     set_callbacks( Create, Destroy );
56 vlc_module_end();
57
58 /*****************************************************************************
59  * vout_sys_t: video output descriptor
60  *****************************************************************************
61  * This structure is part of the video output thread descriptor.
62  * It describes the SVGAlib specific properties of an output thread.
63  *****************************************************************************/
64 struct vout_sys_t
65 {
66     int i_vgamode;
67 };
68
69 /*****************************************************************************
70  * Create: allocates video thread
71  *****************************************************************************
72  * This function allocates and initializes a vout method.
73  *****************************************************************************/
74 static int Create( vlc_object_t *p_this )
75 {
76     vout_thread_t *p_vout = (vout_thread_t *)p_this;
77
78     /* Allocate instance and initialize some members */
79     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
80     if( p_vout->p_sys == NULL )
81     {
82         return VLC_ENOMEM;
83     }
84
85     /* FIXME: find a way to test for SVGAlib availability. We cannot call
86      * vga_init from here because it needs to be closed from the same thread
87      * and we might give away the video output to another thread. Bah, it
88      * doesn't matter anyway ... SVGAlib is being an UGLY DIRTY PIG and calls
89      * atexit() and exit() so what can we do? */
90
91     p_vout->pf_init = Init;
92     p_vout->pf_end = End;
93     p_vout->pf_manage = Manage;
94     p_vout->pf_render = NULL;
95     p_vout->pf_display = Display;
96
97     return VLC_SUCCESS;
98 }
99
100 /*****************************************************************************
101  * Init: initialize video thread
102  *****************************************************************************/
103 static int Init( vout_thread_t *p_vout )
104 {
105     int i_index;
106     picture_t *p_pic;
107
108     I_OUTPUTPICTURES = 0;
109
110     /* SVGAlib shut up! */
111     vga_disabledriverreport();
112
113     /* We cannot call vga_init() in Create() because it has to be run
114      * from the same thread as the other vga calls. */
115     vga_init();
116
117     /* Check that we have a 8bpp mode available */
118     p_vout->p_sys->i_vgamode = vga_getdefaultmode();
119     if( p_vout->p_sys->i_vgamode == -1
120          || vga_getmodeinfo(p_vout->p_sys->i_vgamode)->bytesperpixel != 1 )
121     {
122         p_vout->p_sys->i_vgamode = G320x200x256;
123     }
124
125     if( !vga_hasmode( p_vout->p_sys->i_vgamode ) )
126     {
127         msg_Err( p_vout, "mode %i not available", p_vout->p_sys->i_vgamode );
128         return VLC_EGENERIC;
129     }
130
131     vga_setmode( p_vout->p_sys->i_vgamode );
132     gl_setcontextvga( p_vout->p_sys->i_vgamode );
133     gl_enableclipping();
134
135     if( keyboard_init() )
136     {
137         msg_Err( p_vout, "could not initialize keyboard" );
138         vga_setmode( TEXT );
139         return VLC_EGENERIC;
140     }
141
142     /* Just in case */
143     keyboard_translatekeys( TRANSLATE_CURSORKEYS |
144                             TRANSLATE_KEYPADENTER |
145                             TRANSLATE_DIAGONAL );
146
147     /* Initialize the output structure: RGB with square pixels, whatever
148      * the input format is, since it's the only format we know */
149     p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
150     p_vout->output.pf_setpalette = SetPalette;
151     p_vout->output.i_width = vga_getxdim();
152     p_vout->output.i_height = vga_getydim();
153     p_vout->output.i_aspect = p_vout->output.i_width
154                                * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
155
156     /* Try to initialize 1 direct buffer */
157     p_pic = NULL;
158
159     /* Find an empty picture slot */
160     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
161     {
162         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
163         {
164             p_pic = p_vout->p_picture + i_index;
165             break;
166         }
167     }
168     
169     /* Allocate the picture */
170     if( p_pic == NULL )
171     {
172         return VLC_SUCCESS;
173     }
174
175     vout_AllocatePicture( p_vout, p_pic, p_vout->output.i_width,
176                           p_vout->output.i_height,
177                           p_vout->output.i_chroma );
178
179     if( p_pic->i_planes == 0 )
180     {
181         return VLC_SUCCESS;
182     }
183
184     p_pic->i_status = DESTROYED_PICTURE;
185     p_pic->i_type   = DIRECT_PICTURE;
186
187     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
188
189     I_OUTPUTPICTURES++;
190
191     return VLC_SUCCESS;
192 }
193
194 /*****************************************************************************
195  * End: terminate video thread
196  *****************************************************************************/
197 static void End( vout_thread_t *p_vout )
198 {
199     keyboard_close();
200     vga_clear();
201     vga_setmode( TEXT );
202 }
203
204 /*****************************************************************************
205  * Destroy: destroy video thread
206  *****************************************************************************
207  * Terminate an output method created by Create
208  *****************************************************************************/
209 static void Destroy( vlc_object_t *p_this )
210 {
211     vout_thread_t *p_vout = (vout_thread_t *)p_this;
212
213     /* Destroy structure */
214     free( p_vout->p_sys );
215 }
216
217 /*****************************************************************************
218  * Manage: handle SVGAlib events
219  *****************************************************************************
220  * This function should be called regularly by video output thread. It manages
221  * console events. It returns a non null value on error.
222  *****************************************************************************/
223 static int Manage( vout_thread_t *p_vout )
224 {
225     keyboard_update();
226
227     if( keyboard_keypressed(SCANCODE_ESCAPE)
228          || keyboard_keypressed(SCANCODE_Q ) )
229     {
230         p_vout->p_vlc->b_die = VLC_TRUE;
231     }
232
233     return VLC_SUCCESS;
234 }
235
236 /*****************************************************************************
237  * Display: displays previously rendered output
238  *****************************************************************************
239  * This function sends the currently rendered image to the VGA card.
240  *****************************************************************************/
241 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
242 {
243     gl_putbox( 0, 0, p_vout->output.i_width,
244                p_vout->output.i_height, p_pic->p->p_pixels );
245 }
246
247 /*****************************************************************************
248  * SetPalette: set a 8bpp palette
249  *****************************************************************************
250  * TODO: support 8 bits clut (for Mach32 cards and others).
251  *****************************************************************************/
252 static void SetPalette( vout_thread_t *p_vout, u16 *red, u16 *green, u16 *blue )
253 {
254     int i = 256;
255
256     while( i-- )
257     {
258         vga_setpalette( i, red[i]>>10, green[i]>>10, blue[i]>>10 );
259     }
260 }
261