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