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