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