]> git.sesse.net Git - vlc/blob - plugins/aa/aa.c
* ALL: the first libvlc commit.
[vlc] / plugins / aa / aa.c
1 /*****************************************************************************
2  * vout_aa.c: Aa video output display method for testing purposes
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: aa.c,v 1.5 2002/06/01 12:31:58 sam Exp $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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 <errno.h>                                                 /* ENOMEM */
28 #include <stdlib.h>                                                /* free() */
29 #include <string.h>                                            /* strerror() */
30
31 #include <aalib.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc/vout.h>
35 #include <vlc/intf.h>
36
37 /*****************************************************************************
38  * Capabilities defined in the other files.
39  *****************************************************************************/
40 static void vout_getfunctions  ( function_list_t * p_function_list );
41
42 /*****************************************************************************
43  * Build configuration tree.
44  *****************************************************************************/
45 MODULE_CONFIG_START
46 MODULE_CONFIG_STOP
47
48
49 MODULE_INIT_START
50     SET_DESCRIPTION( _("ASCII-art video output module") )
51     ADD_CAPABILITY( VOUT, 10 )
52     ADD_SHORTCUT( "aalib" )
53 MODULE_INIT_STOP
54
55
56 MODULE_ACTIVATE_START
57     vout_getfunctions( &p_module->p_functions->vout );
58 MODULE_ACTIVATE_STOP
59
60
61 MODULE_DEACTIVATE_START
62 MODULE_DEACTIVATE_STOP
63
64 /*****************************************************************************
65  * vout_sys_t: aa video output method descriptor
66  *****************************************************************************
67  * This structure is part of the video output thread descriptor.
68  * It describes the aa specific properties of an output thread.
69  *****************************************************************************/
70 struct vout_sys_s
71 {
72     struct aa_context*  aa_context;
73     aa_palette          palette;
74     int                 i_width;                     /* width of main window */
75     int                 i_height;                   /* height of main window */
76 };
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81 static int  vout_Create    ( vout_thread_t * );
82 static int  vout_Init      ( vout_thread_t * );
83 static void vout_End       ( vout_thread_t * );
84 static void vout_Destroy   ( vout_thread_t * );
85 static int  vout_Manage    ( vout_thread_t * );
86 static void vout_Render    ( vout_thread_t *, picture_t * );
87 static void vout_Display   ( vout_thread_t *, picture_t * );
88
89 static void SetPalette     ( vout_thread_t *, u16 *, u16 *, u16 * );
90
91 /*****************************************************************************
92  * Functions exported as capabilities. They are declared as static so that
93  * we don't pollute the namespace too much.
94  *****************************************************************************/
95 static void vout_getfunctions( function_list_t * p_function_list )
96 {
97     p_function_list->functions.vout.pf_create     = vout_Create;
98     p_function_list->functions.vout.pf_init       = vout_Init;
99     p_function_list->functions.vout.pf_end        = vout_End;
100     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
101     p_function_list->functions.vout.pf_manage     = vout_Manage;
102     p_function_list->functions.vout.pf_render     = vout_Render;
103     p_function_list->functions.vout.pf_display    = vout_Display;
104 }
105
106 /*****************************************************************************
107  * vout_Create: allocates aa video thread output method
108  *****************************************************************************
109  * This function allocates and initializes a aa vout method.
110  *****************************************************************************/
111 static int vout_Create( vout_thread_t *p_vout )
112 {
113     /* Allocate structure */
114     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
115     if( p_vout->p_sys == NULL )
116     {
117         msg_Err( p_vout, "out of memory" );
118         return( 1 );
119     }
120
121     /* Don't parse any options, but take $AAOPTS into account */
122     aa_parseoptions( NULL, NULL, NULL, NULL );
123
124     if (!(p_vout->p_sys->aa_context = aa_autoinit(&aa_defparams)))
125     {
126         msg_Err( p_vout, "cannot initialize aalib" );
127         return( 1 );
128     }
129
130     p_vout->p_sys->i_width = aa_imgwidth(p_vout->p_sys->aa_context);
131     p_vout->p_sys->i_height = aa_imgheight(p_vout->p_sys->aa_context);
132     aa_autoinitkbd( p_vout->p_sys->aa_context, 0 );
133     aa_autoinitmouse( p_vout->p_sys->aa_context, AA_MOUSEPRESSMASK );
134     aa_hidemouse( p_vout->p_sys->aa_context );
135     return( 0 );
136 }
137
138 /*****************************************************************************
139  * vout_Init: initialize aa video thread output method
140  *****************************************************************************/
141 static int vout_Init( vout_thread_t *p_vout )
142 {
143     int i_index;
144     picture_t *p_pic = NULL;
145
146     I_OUTPUTPICTURES = 0;
147
148     p_vout->output.i_chroma = FOURCC_RGB2;
149     p_vout->output.i_width = p_vout->p_sys->i_width;
150     p_vout->output.i_height = p_vout->p_sys->i_height;
151     p_vout->output.i_aspect = p_vout->p_sys->i_width
152                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
153     p_vout->output.pf_setpalette = SetPalette;
154
155     /* Find an empty picture slot */
156     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
157     {
158         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
159         {
160             p_pic = p_vout->p_picture + i_index;
161             break;
162         }
163     }
164
165     if( p_pic == NULL )
166     {
167         return -1;
168     }
169
170     /* Allocate the picture */
171     p_pic->p->p_pixels = aa_image( p_vout->p_sys->aa_context );
172     p_pic->p->i_pixel_bytes = 1;
173     p_pic->p->i_lines = p_vout->p_sys->i_height;
174     p_pic->p->i_pitch = p_vout->p_sys->i_width;
175     p_pic->p->b_margin = 0;
176     p_pic->i_planes = 1;
177
178     p_pic->i_status = DESTROYED_PICTURE;
179     p_pic->i_type   = DIRECT_PICTURE;
180
181     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
182     I_OUTPUTPICTURES++;
183
184     return 0;
185 }
186
187 /*****************************************************************************
188  * vout_End: terminate aa video thread output method
189  *****************************************************************************/
190 static void vout_End( vout_thread_t *p_vout )
191 {
192     ;
193 }
194
195 /*****************************************************************************
196  * vout_Destroy: destroy aa video thread output method
197  *****************************************************************************
198  * Terminate an output method created by AaCreateOutputMethod
199  *****************************************************************************/
200 static void vout_Destroy( vout_thread_t *p_vout )
201 {
202     aa_close( p_vout->p_sys->aa_context );
203     free( p_vout->p_sys );
204 }
205
206 /*****************************************************************************
207  * vout_Manage: handle aa events
208  *****************************************************************************
209  * This function should be called regularly by video output thread. It manages
210  * console events. It returns a non null value on error.
211  *****************************************************************************/
212 static int vout_Manage( vout_thread_t *p_vout )
213 {
214     int event, x, y, b;
215     event = aa_getevent( p_vout->p_sys->aa_context, 0 );
216     switch ( event )
217     {
218     case AA_MOUSE:
219         aa_getmouse( p_vout->p_sys->aa_context, &x, &y, &b );
220         if ( b & AA_BUTTON3 )
221         {
222             intf_thread_t *p_intf;
223             p_intf = vlc_object_find( p_vout->p_vlc, VLC_OBJECT_INTF,
224                                                      FIND_CHILD );
225             if( p_intf )
226             {
227                 p_intf->b_menu_change = 1;
228                 vlc_object_release( p_intf );
229             }
230         }
231         break;
232     case AA_RESIZE:
233         p_vout->i_changes |= VOUT_SIZE_CHANGE;
234         aa_resize( p_vout->p_sys->aa_context );
235         p_vout->p_sys->i_width = aa_imgwidth( p_vout->p_sys->aa_context );
236         p_vout->p_sys->i_height = aa_imgheight( p_vout->p_sys->aa_context );
237         break;
238     default:
239         break;
240     }
241     return( 0 );
242 }
243
244 /*****************************************************************************
245  * vout_Render: render previously calculated output
246  *****************************************************************************/
247 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
248 {
249   aa_fastrender( p_vout->p_sys->aa_context, 0, 0,
250                  aa_imgwidth( p_vout->p_sys->aa_context ),
251                  aa_imgheight( p_vout->p_sys->aa_context ) );
252 }
253
254 /*****************************************************************************
255  * vout_Display: displays previously rendered output
256  *****************************************************************************/
257 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
258 {
259     /* No need to do anything, the fake direct buffers stay as they are */
260     int i_width, i_height, i_x, i_y;
261
262     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
263                        &i_x, &i_y, &i_width, &i_height );
264
265     aa_flush(p_vout->p_sys->aa_context);
266 }
267
268 /*****************************************************************************
269  * SetPalette: set the 8bpp palette
270  *****************************************************************************/
271 static void SetPalette( vout_thread_t *p_vout, u16 *red, u16 *green, u16 *blue )
272 {
273     int i;
274
275     /* Fill colors with color information */
276     for( i = 0; i < 256; i++ )
277     {
278         aa_setpalette( p_vout->p_sys->palette, 256 -i,
279                        red[ i ], green[ i ], blue[ i ] );
280     }
281 }
282