]> git.sesse.net Git - vlc/blob - modules/video_output/aa.c
Remove _GNU_SOURCE and string.h too
[vlc] / modules / video_output / aa.c
1 /*****************************************************************************
2  * vout_aa.c: Aa video output display method for testing purposes
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.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 #include <errno.h>                                                 /* ENOMEM */
28
29 #include <aalib.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc_vout.h>
33 #include <vlc_interface.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 Render    ( vout_thread_t *, picture_t * );
45 static void Display   ( vout_thread_t *, picture_t * );
46
47 static void SetPalette     ( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 vlc_module_begin();
53     set_shortname( _("ASCII Art"));
54     set_category( CAT_VIDEO );
55     set_subcategory( SUBCAT_VIDEO_VOUT );
56     set_description( _("ASCII-art video output") );
57     set_capability( "video output", 10 );
58     add_shortcut( "aalib" );
59     set_callbacks( Create, Destroy );
60 vlc_module_end();
61
62 /*****************************************************************************
63  * vout_sys_t: aa video output method descriptor
64  *****************************************************************************
65  * This structure is part of the video output thread descriptor.
66  * It describes the aa specific properties of an output thread.
67  *****************************************************************************/
68 struct vout_sys_t
69 {
70     struct aa_context*  aa_context;
71     aa_palette          palette;
72     int                 i_width;                     /* width of main window */
73     int                 i_height;                   /* height of main window */
74 };
75
76 /*****************************************************************************
77  * Create: allocates aa video thread output method
78  *****************************************************************************
79  * This function allocates and initializes a aa 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 structure */
86     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
87     if( p_vout->p_sys == NULL )
88     {
89         msg_Err( p_vout, "out of memory" );
90         return( 1 );
91     }
92
93     /* Don't parse any options, but take $AAOPTS into account */
94     aa_parseoptions( NULL, NULL, NULL, NULL );
95
96     if (!(p_vout->p_sys->aa_context = aa_autoinit(&aa_defparams)))
97     {
98         msg_Err( p_vout, "cannot initialize aalib" );
99         return( 1 );
100     }
101
102     p_vout->pf_init = Init;
103     p_vout->pf_end = End;
104     p_vout->pf_manage = Manage;
105     p_vout->pf_render = Render;
106     p_vout->pf_display = Display;
107
108     p_vout->p_sys->i_width = aa_imgwidth(p_vout->p_sys->aa_context);
109     p_vout->p_sys->i_height = aa_imgheight(p_vout->p_sys->aa_context);
110     aa_autoinitkbd( p_vout->p_sys->aa_context, 0 );
111     aa_autoinitmouse( p_vout->p_sys->aa_context, AA_MOUSEPRESSMASK );
112     aa_hidemouse( p_vout->p_sys->aa_context );
113     return( 0 );
114 }
115
116 /*****************************************************************************
117  * Init: initialize aa video thread output method
118  *****************************************************************************/
119 static int Init( vout_thread_t *p_vout )
120 {
121     int i_index;
122     picture_t *p_pic = NULL;
123
124     I_OUTPUTPICTURES = 0;
125
126     p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
127     p_vout->output.i_width = p_vout->p_sys->i_width;
128     p_vout->output.i_height = p_vout->p_sys->i_height;
129     p_vout->output.i_aspect = p_vout->p_sys->i_width
130                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
131     p_vout->output.pf_setpalette = SetPalette;
132
133     /* Find an empty picture slot */
134     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
135     {
136         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
137         {
138             p_pic = p_vout->p_picture + i_index;
139             break;
140         }
141     }
142
143     if( p_pic == NULL )
144     {
145         return -1;
146     }
147
148     /* Allocate the picture */
149     p_pic->p->p_pixels = aa_image( p_vout->p_sys->aa_context );
150     p_pic->p->i_lines = p_vout->p_sys->i_height;
151     p_pic->p->i_visible_lines = p_vout->p_sys->i_height;
152     p_pic->p->i_pitch = p_vout->p_sys->i_width;
153     p_pic->p->i_pixel_pitch = 1;
154     p_pic->p->i_visible_pitch = p_vout->p_sys->i_width;
155     p_pic->i_planes = 1;
156
157     p_pic->i_status = DESTROYED_PICTURE;
158     p_pic->i_type   = DIRECT_PICTURE;
159
160     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
161     I_OUTPUTPICTURES++;
162
163     return 0;
164 }
165
166 /*****************************************************************************
167  * End: terminate aa video thread output method
168  *****************************************************************************/
169 static void End( vout_thread_t *p_vout )
170 {
171     ;
172 }
173
174 /*****************************************************************************
175  * Destroy: destroy aa video thread output method
176  *****************************************************************************
177  * Terminate an output method created by AaCreateOutputMethod
178  *****************************************************************************/
179 static void Destroy( vlc_object_t *p_this )
180 {
181     vout_thread_t *p_vout = (vout_thread_t *)p_this;
182
183     aa_close( p_vout->p_sys->aa_context );
184     free( p_vout->p_sys );
185 }
186
187 /*****************************************************************************
188  * Manage: handle aa events
189  *****************************************************************************
190  * This function should be called regularly by video output thread. It manages
191  * console events. It returns a non null value on error.
192  *****************************************************************************/
193 static int Manage( vout_thread_t *p_vout )
194 {
195     int event, x, y, b;
196     event = aa_getevent( p_vout->p_sys->aa_context, 0 );
197     switch ( event )
198     {
199     case AA_MOUSE:
200         aa_getmouse( p_vout->p_sys->aa_context, &x, &y, &b );
201         if ( b & AA_BUTTON3 )
202         {
203             intf_thread_t *p_intf;
204             p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
205             if( p_intf )
206             {
207                 p_intf->b_menu_change = 1;
208                 vlc_object_release( p_intf );
209             }
210         }
211         break;
212     case AA_RESIZE:
213         p_vout->i_changes |= VOUT_SIZE_CHANGE;
214         aa_resize( p_vout->p_sys->aa_context );
215         p_vout->p_sys->i_width = aa_imgwidth( p_vout->p_sys->aa_context );
216         p_vout->p_sys->i_height = aa_imgheight( p_vout->p_sys->aa_context );
217         break;
218     default:
219         break;
220     }
221     return( 0 );
222 }
223
224 /*****************************************************************************
225  * Render: render previously calculated output
226  *****************************************************************************/
227 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
228 {
229   aa_fastrender( p_vout->p_sys->aa_context, 0, 0,
230                  aa_imgwidth( p_vout->p_sys->aa_context ),
231                  aa_imgheight( p_vout->p_sys->aa_context ) );
232 }
233
234 /*****************************************************************************
235  * Display: displays previously rendered output
236  *****************************************************************************/
237 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
238 {
239     /* No need to do anything, the fake direct buffers stay as they are */
240     int i_width, i_height, i_x, i_y;
241
242     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
243                        &i_x, &i_y, &i_width, &i_height );
244
245     aa_flush(p_vout->p_sys->aa_context);
246 }
247
248 /*****************************************************************************
249  * SetPalette: set the 8bpp palette
250  *****************************************************************************/
251 static void SetPalette( vout_thread_t *p_vout,
252                         uint16_t *red, uint16_t *green, uint16_t *blue )
253 {
254     int i;
255
256     /* Fill colors with color information */
257     for( i = 0; i < 256; i++ )
258     {
259         aa_setpalette( p_vout->p_sys->palette, 256 -i,
260                        red[ i ], green[ i ], blue[ i ] );
261     }
262 }
263