]> git.sesse.net Git - vlc/blob - modules/video_output/caca.c
* modules/misc/dummy/renderer.c:
[vlc] / modules / video_output / caca.c
1 /*****************************************************************************
2  * caca.c: Color ASCII Art video output plugin using libcaca
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: caca.c,v 1.4 2003/12/04 16:49:43 sam Exp $
6  *
7  * Authors: Sam 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., 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 <caca.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc/vout.h>
35 #include <vlc/intf.h>
36 #include <vlc_keys.h>
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static int  Create    ( vlc_object_t * );
42 static void Destroy   ( vlc_object_t * );
43
44 static int  Init      ( vout_thread_t * );
45 static void End       ( vout_thread_t * );
46 static int  Manage    ( vout_thread_t * );
47 static void Render    ( vout_thread_t *, picture_t * );
48 static void Display   ( vout_thread_t *, picture_t * );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 #define MODE_TEXT N_("dithering mode")
54 #define MODE_LONGTEXT N_("Choose the libcaca dithering mode")
55
56 static char *mode_list[] = { "none",
57                              "ordered2",
58                              "ordered4",
59                              "ordered8",
60                              "random" };
61 static char *mode_list_text[] = { N_("No dithering"),
62                                   N_("2x2 ordered dithering"),
63                                   N_("4x4 ordered dithering"),
64                                   N_("8x8 ordered dithering"),
65                                   N_("Random dithering") };
66
67 vlc_module_begin();
68     add_category_hint( N_("Dithering"), NULL, VLC_FALSE );
69     add_string( "caca-dithering", "ordered", NULL, MODE_TEXT,
70                 MODE_LONGTEXT, VLC_FALSE );
71         change_string_list( mode_list, mode_list_text, 0 );
72     set_description( _("colour ASCII art video output") );
73     set_capability( "video output", 12 );
74     set_callbacks( Create, Destroy );
75 vlc_module_end();
76
77 /*****************************************************************************
78  * vout_sys_t: libcaca video output method descriptor
79  *****************************************************************************
80  * This structure is part of the video output thread descriptor.
81  * It describes the libcaca specific properties of an output thread.
82  *****************************************************************************/
83 struct vout_sys_t
84 {
85     struct caca_bitmap *p_bitmap;
86 };
87
88 /*****************************************************************************
89  * Create: allocates libcaca video output thread
90  *****************************************************************************
91  * This function initializes libcaca vout method.
92  *****************************************************************************/
93 static int Create( vlc_object_t *p_this )
94 {
95     vout_thread_t *p_vout = (vout_thread_t *)p_this;
96     enum caca_dithering dither = CACA_DITHERING_ORDERED4;
97     vlc_value_t val;
98
99     /* Allocate structure */
100     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
101     if( p_vout->p_sys == NULL )
102     {
103         msg_Err( p_vout, "out of memory" );
104         return VLC_ENOMEM;
105     }
106
107     if( caca_init() )
108     {
109         msg_Err( p_vout, "cannot initialize libcaca" );
110         free( p_vout->p_sys );
111         return VLC_EGENERIC;
112     }
113
114     var_Create( p_vout, "caca-dithering", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
115     var_Get( p_vout, "caca-dithering", &val );
116     if( val.psz_string )
117     {
118         if( !strcmp( val.psz_string, "none" ) )
119         {
120             dither = CACA_DITHERING_NONE;
121         }
122         else if( !strcmp( val.psz_string, "ordered2" ) )
123         {
124             dither = CACA_DITHERING_ORDERED2;
125         }
126         else if( !strcmp( val.psz_string, "ordered4" ) )
127         {
128             dither = CACA_DITHERING_ORDERED4;
129         }
130         else if( !strcmp( val.psz_string, "random" ) )
131         {
132             dither = CACA_DITHERING_RANDOM;
133         }
134         free( val.psz_string );
135     }
136     caca_set_dithering( dither );
137
138     p_vout->pf_init = Init;
139     p_vout->pf_end = End;
140     p_vout->pf_manage = Manage;
141     p_vout->pf_render = Render;
142     p_vout->pf_display = Display;
143
144     return VLC_SUCCESS;
145 }
146
147 /*****************************************************************************
148  * Init: initialize libcaca video output thread
149  *****************************************************************************/
150 static int Init( vout_thread_t *p_vout )
151 {
152     int i_index;
153     picture_t *p_pic = NULL;
154
155     I_OUTPUTPICTURES = 0;
156
157     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
158     p_vout->output.i_width = p_vout->render.i_width;
159     p_vout->output.i_height = p_vout->render.i_height;
160     p_vout->output.i_aspect = p_vout->render.i_aspect;
161
162     p_vout->output.i_rmask = 0x00ff0000;
163     p_vout->output.i_gmask = 0x0000ff00;
164     p_vout->output.i_bmask = 0x000000ff;
165
166     /* Create the libcaca bitmap */
167     p_vout->p_sys->p_bitmap =
168         caca_create_bitmap( 32,
169                             p_vout->output.i_width,
170                             p_vout->output.i_height,
171                             4 * ((p_vout->output.i_width + 15) & ~15),
172                             p_vout->output.i_rmask,
173                             p_vout->output.i_gmask,
174                             p_vout->output.i_bmask );
175     if( !p_vout->p_sys->p_bitmap )
176     {
177         msg_Err( p_vout, "could not create libcaca bitmap" );
178         return VLC_EGENERIC;
179     }
180
181     /* Find an empty picture slot */
182     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
183     {
184         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
185         {
186             p_pic = p_vout->p_picture + i_index;
187             break;
188         }
189     }
190
191     if( p_pic == NULL )
192     {
193         return VLC_EGENERIC;
194     }
195
196     /* Allocate the picture */
197     p_pic->p->i_lines = p_vout->output.i_height;
198     p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
199     p_pic->p->i_pixel_pitch = 4;
200     p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
201     p_pic->i_planes = 1;
202     p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );
203
204     p_pic->i_status = DESTROYED_PICTURE;
205     p_pic->i_type   = DIRECT_PICTURE;
206
207     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
208     I_OUTPUTPICTURES++;
209
210     return VLC_SUCCESS;
211 }
212
213 /*****************************************************************************
214  * End: terminate libcaca video output thread
215  *****************************************************************************/
216 static void End( vout_thread_t *p_vout )
217 {
218     caca_free_bitmap( p_vout->p_sys->p_bitmap );
219 }
220
221 /*****************************************************************************
222  * Destroy: destroy libcaca video output thread
223  *****************************************************************************
224  * Terminate an output method created by AaCreateOutputMethod
225  *****************************************************************************/
226 static void Destroy( vlc_object_t *p_this )
227 {
228     vout_thread_t *p_vout = (vout_thread_t *)p_this;
229
230     caca_end();
231     free( p_vout->p_sys );
232 }
233
234 /*****************************************************************************
235  * Manage: handle libcaca events
236  *****************************************************************************
237  * This function should be called regularly by video output thread. It manages
238  * console events. It returns a non null value on error.
239  *****************************************************************************/
240 static int Manage( vout_thread_t *p_vout )
241 {
242     int event;
243     vlc_value_t val;
244
245     while(( event = caca_get_event() ))
246     {
247         if( event & CACA_EVENT_KEY_PRESS )
248         {
249             switch( event & 0xffff )
250             {
251             case 'q':
252                 val.i_int = KEY_MODIFIER_CTRL | 'q';
253                 break;
254             case ' ':
255                 val.i_int = KEY_SPACE;
256                 break;
257             default:
258                 continue;
259             }
260         }
261
262         var_Set( p_vout->p_vlc, "key-pressed", val );
263     }
264
265     return VLC_SUCCESS;
266 }
267
268 /*****************************************************************************
269  * Render: render previously calculated output
270  *****************************************************************************/
271 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
272 {
273     caca_clear();
274     caca_draw_bitmap( 0, 0, caca_get_width() - 1, caca_get_height() - 1,
275                       p_vout->p_sys->p_bitmap, p_pic->p->p_pixels );
276 }
277
278 /*****************************************************************************
279  * Display: displays previously rendered output
280  *****************************************************************************/
281 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
282 {
283     caca_refresh();
284 }
285