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