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