]> git.sesse.net Git - vlc/blob - modules/video_output/svgalib.c
6c4ce8c1b0256e65bce680793f53c1f124ab7bc9
[vlc] / modules / video_output / svgalib.c
1 /*****************************************************************************
2  * svgalib.c : SVGAlib plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
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 *, uint16_t *, uint16_t *, uint16_t * );
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 vlc_module_begin();
53     set_shortname( "SVGAlib" );
54     set_category( CAT_VIDEO );
55     set_subcategory( SUBCAT_VIDEO_VOUT );
56     set_description( _("SVGAlib video output") );
57     set_capability( "video output", 0 );
58     set_callbacks( Create, Destroy );
59     linked_with_a_crap_library_which_uses_atexit();
60 vlc_module_end();
61
62 /*****************************************************************************
63  * vout_sys_t: video output descriptor
64  *****************************************************************************
65  * This structure is part of the video output thread descriptor.
66  * It describes the SVGAlib specific properties of an output thread.
67  *****************************************************************************/
68 struct vout_sys_t
69 {
70     int i_vgamode;
71 };
72
73 /*****************************************************************************
74  * Create: allocates video thread
75  *****************************************************************************
76  * This function allocates and initializes a vout method.
77  *****************************************************************************/
78 static int Create( vlc_object_t *p_this )
79 {
80     vout_thread_t *p_vout = (vout_thread_t *)p_this;
81
82     /* Allocate instance and initialize some members */
83     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
84     if( p_vout->p_sys == NULL )
85     {
86         return VLC_ENOMEM;
87     }
88
89     /* FIXME: find a way to test for SVGAlib availability. We cannot call
90      * vga_init from here because it needs to be closed from the same thread
91      * and we might give away the video output to another thread. Bah, it
92      * doesn't matter anyway ... SVGAlib is being an UGLY DIRTY PIG and calls
93      * atexit() and exit() so what can we do? */
94
95     p_vout->pf_init = Init;
96     p_vout->pf_end = End;
97     p_vout->pf_manage = Manage;
98     p_vout->pf_render = NULL;
99     p_vout->pf_display = Display;
100
101     return VLC_SUCCESS;
102 }
103
104 /*****************************************************************************
105  * Init: initialize video thread
106  *****************************************************************************/
107 static int Init( vout_thread_t *p_vout )
108 {
109     int i_index;
110     picture_t *p_pic;
111
112     I_OUTPUTPICTURES = 0;
113
114     /* SVGAlib shut up! */
115     vga_disabledriverreport();
116
117     /* We cannot call vga_init() in Create() because it has to be run
118      * from the same thread as the other vga calls. */
119     vga_init();
120
121     /* Check that we have a 8bpp mode available */
122     p_vout->p_sys->i_vgamode = vga_getdefaultmode();
123     if( p_vout->p_sys->i_vgamode == -1
124          || vga_getmodeinfo(p_vout->p_sys->i_vgamode)->bytesperpixel != 1 )
125     {
126         p_vout->p_sys->i_vgamode = G320x200x256;
127     }
128
129     if( !vga_hasmode( p_vout->p_sys->i_vgamode ) )
130     {
131         msg_Err( p_vout, "mode %i not available", p_vout->p_sys->i_vgamode );
132         return VLC_EGENERIC;
133     }
134
135     vga_setmode( p_vout->p_sys->i_vgamode );
136     gl_setcontextvga( p_vout->p_sys->i_vgamode );
137     gl_enableclipping();
138
139     if( keyboard_init() )
140     {
141         msg_Err( p_vout, "could not initialize keyboard" );
142         vga_setmode( TEXT );
143         return VLC_EGENERIC;
144     }
145
146     /* Just in case */
147     keyboard_translatekeys( TRANSLATE_CURSORKEYS |
148                             TRANSLATE_KEYPADENTER |
149                             TRANSLATE_DIAGONAL );
150
151     /* Initialize the output structure: RGB with square pixels, whatever
152      * the input format is, since it's the only format we know */
153     p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
154     p_vout->output.pf_setpalette = SetPalette;
155     p_vout->output.i_width = vga_getxdim();
156     p_vout->output.i_height = vga_getydim();
157     p_vout->output.i_aspect = p_vout->output.i_width
158                                * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
159
160     /* Try to initialize 1 direct buffer */
161     p_pic = NULL;
162
163     /* Find an empty picture slot */
164     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
165     {
166         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
167         {
168             p_pic = p_vout->p_picture + i_index;
169             break;
170         }
171     }
172
173     /* Allocate the picture */
174     if( p_pic == NULL )
175     {
176         return VLC_SUCCESS;
177     }
178
179     vout_AllocatePicture( p_vout, p_pic, p_vout->output.i_chroma,
180                           p_vout->output.i_width, p_vout->output.i_height,
181                           p_vout->output.i_aspect );
182
183     if( p_pic->i_planes == 0 )
184     {
185         return VLC_SUCCESS;
186     }
187
188     p_pic->i_status = DESTROYED_PICTURE;
189     p_pic->i_type   = DIRECT_PICTURE;
190
191     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
192
193     I_OUTPUTPICTURES++;
194
195     return VLC_SUCCESS;
196 }
197
198 /*****************************************************************************
199  * End: terminate video thread
200  *****************************************************************************/
201 static void End( vout_thread_t *p_vout )
202 {
203     keyboard_close();
204     vga_clear();
205     vga_setmode( TEXT );
206 }
207
208 /*****************************************************************************
209  * Destroy: destroy video thread
210  *****************************************************************************
211  * Terminate an output method created by Create
212  *****************************************************************************/
213 static void Destroy( vlc_object_t *p_this )
214 {
215     vout_thread_t *p_vout = (vout_thread_t *)p_this;
216
217     /* Destroy structure */
218     free( p_vout->p_sys );
219 }
220
221 /*****************************************************************************
222  * Manage: handle SVGAlib events
223  *****************************************************************************
224  * This function should be called regularly by video output thread. It manages
225  * console events. It returns a non null value on error.
226  *****************************************************************************/
227 static int Manage( vout_thread_t *p_vout )
228 {
229     keyboard_update();
230
231     if( keyboard_keypressed(SCANCODE_ESCAPE)
232          || keyboard_keypressed(SCANCODE_Q ) )
233     {
234         p_vout->p_vlc->b_die = VLC_TRUE;
235     }
236
237     return VLC_SUCCESS;
238 }
239
240 /*****************************************************************************
241  * Display: displays previously rendered output
242  *****************************************************************************
243  * This function sends the currently rendered image to the VGA card.
244  *****************************************************************************/
245 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
246 {
247     gl_putbox( 0, 0, p_vout->output.i_width,
248                p_vout->output.i_height, p_pic->p->p_pixels );
249 }
250
251 /*****************************************************************************
252  * SetPalette: set a 8bpp palette
253  *****************************************************************************
254  * TODO: support 8 bits clut (for Mach32 cards and others).
255  *****************************************************************************/
256 static void SetPalette( vout_thread_t *p_vout,
257                         uint16_t *red, uint16_t *green, uint16_t *blue )
258 {
259     int i = 256;
260
261     while( i-- )
262     {
263         vga_setpalette( i, red[i]>>10, green[i]>>10, blue[i]>>10 );
264     }
265 }
266