]> git.sesse.net Git - vlc/blob - modules/video_output/caca.c
* modules/video_output/caca.c:
[vlc] / modules / video_output / caca.c
1 /*****************************************************************************
2  * caca.c: Color ASCII Art video output plugin using libcaca
3  *****************************************************************************
4  * Copyright (C) 2003, 2004 VideoLAN
5  * $Id: caca.c,v 1.6 2004/01/08 19:22:10 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 vlc_module_begin();
54     set_description( _("colour ASCII art video output") );
55     set_capability( "video output", 12 );
56     set_callbacks( Create, Destroy );
57 vlc_module_end();
58
59 /*****************************************************************************
60  * vout_sys_t: libcaca video output method descriptor
61  *****************************************************************************
62  * This structure is part of the video output thread descriptor.
63  * It describes the libcaca specific properties of an output thread.
64  *****************************************************************************/
65 struct vout_sys_t
66 {
67     struct caca_bitmap *p_bitmap;
68 };
69
70 /*****************************************************************************
71  * Create: allocates libcaca video output thread
72  *****************************************************************************
73  * This function initializes libcaca vout method.
74  *****************************************************************************/
75 static int Create( vlc_object_t *p_this )
76 {
77     vout_thread_t *p_vout = (vout_thread_t *)p_this;
78
79     /* Allocate structure */
80     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
81     if( p_vout->p_sys == NULL )
82     {
83         msg_Err( p_vout, "out of memory" );
84         return VLC_ENOMEM;
85     }
86
87     if( caca_init() )
88     {
89         msg_Err( p_vout, "cannot initialize libcaca" );
90         free( p_vout->p_sys );
91         return VLC_EGENERIC;
92     }
93
94     p_vout->pf_init = Init;
95     p_vout->pf_end = End;
96     p_vout->pf_manage = Manage;
97     p_vout->pf_render = Render;
98     p_vout->pf_display = Display;
99
100     return VLC_SUCCESS;
101 }
102
103 /*****************************************************************************
104  * Init: initialize libcaca video output thread
105  *****************************************************************************/
106 static int Init( vout_thread_t *p_vout )
107 {
108     int i_index;
109     picture_t *p_pic = NULL;
110
111     I_OUTPUTPICTURES = 0;
112
113     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
114     p_vout->output.i_width = p_vout->render.i_width;
115     p_vout->output.i_height = p_vout->render.i_height;
116     p_vout->output.i_aspect = p_vout->render.i_aspect;
117
118     p_vout->output.i_rmask = 0x00ff0000;
119     p_vout->output.i_gmask = 0x0000ff00;
120     p_vout->output.i_bmask = 0x000000ff;
121
122     /* Create the libcaca bitmap */
123     p_vout->p_sys->p_bitmap =
124         caca_create_bitmap( 32,
125                             p_vout->output.i_width,
126                             p_vout->output.i_height,
127                             4 * ((p_vout->output.i_width + 15) & ~15),
128                             p_vout->output.i_rmask,
129                             p_vout->output.i_gmask,
130                             p_vout->output.i_bmask,
131                             0x00000000 );
132     if( !p_vout->p_sys->p_bitmap )
133     {
134         msg_Err( p_vout, "could not create libcaca bitmap" );
135         return VLC_EGENERIC;
136     }
137
138     /* Find an empty picture slot */
139     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
140     {
141         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
142         {
143             p_pic = p_vout->p_picture + i_index;
144             break;
145         }
146     }
147
148     if( p_pic == NULL )
149     {
150         return VLC_EGENERIC;
151     }
152
153     /* Allocate the picture */
154     p_pic->p->i_lines = p_vout->output.i_height;
155     p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
156     p_pic->p->i_pixel_pitch = 4;
157     p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
158     p_pic->i_planes = 1;
159     p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );
160
161     p_pic->i_status = DESTROYED_PICTURE;
162     p_pic->i_type   = DIRECT_PICTURE;
163
164     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
165     I_OUTPUTPICTURES++;
166
167     return VLC_SUCCESS;
168 }
169
170 /*****************************************************************************
171  * End: terminate libcaca video output thread
172  *****************************************************************************/
173 static void End( vout_thread_t *p_vout )
174 {
175     caca_free_bitmap( p_vout->p_sys->p_bitmap );
176 }
177
178 /*****************************************************************************
179  * Destroy: destroy libcaca video output thread
180  *****************************************************************************
181  * Terminate an output method created by AaCreateOutputMethod
182  *****************************************************************************/
183 static void Destroy( vlc_object_t *p_this )
184 {
185     vout_thread_t *p_vout = (vout_thread_t *)p_this;
186
187     caca_end();
188     free( p_vout->p_sys );
189 }
190
191 /*****************************************************************************
192  * Manage: handle libcaca events
193  *****************************************************************************
194  * This function should be called regularly by video output thread. It manages
195  * console events. It returns a non null value on error.
196  *****************************************************************************/
197 static int Manage( vout_thread_t *p_vout )
198 {
199     int event;
200     vlc_value_t val;
201
202     while(( event = caca_get_event(CACA_EVENT_KEY_PRESS) ))
203     {
204         switch( event & 0x00ffffff )
205         {
206         case 'q':
207             val.i_int = KEY_MODIFIER_CTRL | 'q';
208             break;
209         case ' ':
210             val.i_int = KEY_SPACE;
211             break;
212         default:
213             continue;
214         }
215
216         var_Set( p_vout->p_vlc, "key-pressed", val );
217     }
218
219     return VLC_SUCCESS;
220 }
221
222 /*****************************************************************************
223  * Render: render previously calculated output
224  *****************************************************************************/
225 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
226 {
227     caca_clear();
228     caca_draw_bitmap( 0, 0, caca_get_width() - 1, caca_get_height() - 1,
229                       p_vout->p_sys->p_bitmap, p_pic->p->p_pixels );
230 }
231
232 /*****************************************************************************
233  * Display: displays previously rendered output
234  *****************************************************************************/
235 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
236 {
237     caca_refresh();
238 }
239