]> git.sesse.net Git - vlc/blob - modules/video_output/caca.c
* modules/video_output/caca.c: synced caca output with libcaca-0.99.beta1.
[vlc] / modules / video_output / caca.c
1 /*****************************************************************************
2  * caca.c: Color ASCII Art video output plugin using libcaca
3  *****************************************************************************
4  * Copyright (C) 2003, 2004 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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_shortname( "Caca" );
55     set_category( CAT_VIDEO );
56     set_subcategory( SUBCAT_VIDEO_VOUT );
57     set_description( _("Color ASCII art video output") );
58     set_capability( "video output", 12 );
59     set_callbacks( Create, Destroy );
60 vlc_module_end();
61
62 /*****************************************************************************
63  * vout_sys_t: libcaca video output method descriptor
64  *****************************************************************************
65  * This structure is part of the video output thread descriptor.
66  * It describes the libcaca specific properties of an output thread.
67  *****************************************************************************/
68 struct vout_sys_t
69 {
70 #ifdef CACA_API_VERSION_1
71     cucul_canvas_t *p_cv;
72     caca_display_t *p_dp;
73     cucul_dither_t *p_dither;
74 #else
75     struct caca_bitmap *p_bitmap;
76 #endif
77 };
78
79 /*****************************************************************************
80  * Create: allocates libcaca video output thread
81  *****************************************************************************
82  * This function initializes libcaca vout method.
83  *****************************************************************************/
84 static int Create( vlc_object_t *p_this )
85 {
86     vout_thread_t *p_vout = (vout_thread_t *)p_this;
87
88 #if defined( WIN32 ) && !defined( UNDER_CE )
89     if( AllocConsole() )
90     {
91         CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
92         SMALL_RECT rect;
93         COORD coord;
94
95         HANDLE hstdout =
96             CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE,
97                                        FILE_SHARE_READ | FILE_SHARE_WRITE,
98                                        NULL, CONSOLE_TEXTMODE_BUFFER, NULL );
99         if( !hstdout || hstdout == INVALID_HANDLE_VALUE )
100         {
101             msg_Err( p_vout, "cannot create screen buffer" );
102             FreeConsole();
103             return VLC_EGENERIC;
104         }
105
106         if( !SetConsoleActiveScreenBuffer( hstdout) )
107         {
108             msg_Err( p_vout, "cannot set active screen buffer" );
109             FreeConsole();
110             return VLC_EGENERIC;
111         }
112
113         coord = GetLargestConsoleWindowSize( hstdout );
114         msg_Dbg( p_vout, "SetConsoleWindowInfo: %ix%i", coord.X, coord.Y );
115
116         /* Force size for now */
117         coord.X = 100;
118         coord.Y = 40;
119
120         if( !SetConsoleScreenBufferSize( hstdout, coord ) )
121             msg_Warn( p_vout, "SetConsoleScreenBufferSize %i %i",
122                       coord.X, coord.Y );
123
124         /* Get the current screen buffer size and window position. */
125         if( GetConsoleScreenBufferInfo( hstdout, &csbiInfo ) )
126         {
127             rect.Top = 0; rect.Left = 0;
128             rect.Right = csbiInfo.dwMaximumWindowSize.X - 1;
129             rect.Bottom = csbiInfo.dwMaximumWindowSize.Y - 1;
130             if( !SetConsoleWindowInfo( hstdout, TRUE, &rect ) )
131                 msg_Dbg( p_vout, "SetConsoleWindowInfo failed: %ix%i",
132                          rect.Right, rect.Bottom );
133         }
134     }
135     else
136     {
137         msg_Err( p_vout, "cannot create console" );
138         return VLC_EGENERIC;
139     }
140
141 #endif
142
143     /* Allocate structure */
144     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
145     if( p_vout->p_sys == NULL )
146     {
147         msg_Err( p_vout, "out of memory" );
148         return VLC_ENOMEM;
149     }
150
151 #ifdef CACA_API_VERSION_1
152     p_vout->p_sys->p_cv = cucul_create_canvas(0, 0);
153     if( !p_vout->p_sys->p_cv )
154     {
155         msg_Err( p_vout, "cannot initialize libcucul" );
156         free( p_vout->p_sys );
157         return VLC_EGENERIC;
158     }
159
160     p_vout->p_sys->p_dp = caca_create_display( p_vout->p_sys->p_cv );
161     if( !p_vout->p_sys->p_dp )
162     {
163         msg_Err( p_vout, "cannot initialize libcaca" );
164         cucul_free_canvas( p_vout->p_sys->p_cv );
165         free( p_vout->p_sys );
166         return VLC_EGENERIC;
167     }
168
169     caca_set_display_title( p_vout->p_sys->p_dp,
170                             VOUT_TITLE " - Colour AsCii Art (caca)" );
171 #else
172     if( caca_init() )
173     {
174         msg_Err( p_vout, "cannot initialize libcaca" );
175         free( p_vout->p_sys );
176         return VLC_EGENERIC;
177     }
178
179     caca_set_window_title( VOUT_TITLE " - Colour AsCii Art (caca)" );
180 #endif
181
182     p_vout->pf_init = Init;
183     p_vout->pf_end = End;
184     p_vout->pf_manage = Manage;
185     p_vout->pf_render = Render;
186     p_vout->pf_display = Display;
187
188     return VLC_SUCCESS;
189 }
190
191 /*****************************************************************************
192  * Init: initialize libcaca video output thread
193  *****************************************************************************/
194 static int Init( vout_thread_t *p_vout )
195 {
196     int i_index;
197     picture_t *p_pic = NULL;
198
199     I_OUTPUTPICTURES = 0;
200
201     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
202     p_vout->output.i_width = p_vout->render.i_width;
203     p_vout->output.i_height = p_vout->render.i_height;
204     p_vout->output.i_aspect = p_vout->render.i_aspect;
205
206     p_vout->output.i_rmask = 0x00ff0000;
207     p_vout->output.i_gmask = 0x0000ff00;
208     p_vout->output.i_bmask = 0x000000ff;
209
210     /* Create the libcaca bitmap */
211 #ifdef CACA_API_VERSION_1
212     p_vout->p_sys->p_dither =
213         cucul_create_dither
214 #else
215     p_vout->p_sys->p_bitmap =
216         caca_create_bitmap
217 #endif
218                        ( 32, p_vout->output.i_width, p_vout->output.i_height,
219                          4 * ((p_vout->output.i_width + 15) & ~15),
220                          p_vout->output.i_rmask, p_vout->output.i_gmask,
221                          p_vout->output.i_bmask, 0x00000000 );
222
223 #ifdef CACA_API_VERSION_1
224     if( !p_vout->p_sys->p_dither )
225 #else
226     if( !p_vout->p_sys->p_bitmap )
227 #endif
228     {
229         msg_Err( p_vout, "could not create libcaca bitmap" );
230         return VLC_EGENERIC;
231     }
232
233     /* Find an empty picture slot */
234     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
235     {
236         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
237         {
238             p_pic = p_vout->p_picture + i_index;
239             break;
240         }
241     }
242
243     if( p_pic == NULL )
244     {
245         return VLC_EGENERIC;
246     }
247
248     /* Allocate the picture */
249     p_pic->p->i_lines = p_vout->output.i_height;
250     p_pic->p->i_visible_lines = p_vout->output.i_height;
251     p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
252     p_pic->p->i_pixel_pitch = 4;
253     p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
254     p_pic->i_planes = 1;
255     p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );
256
257     p_pic->i_status = DESTROYED_PICTURE;
258     p_pic->i_type   = DIRECT_PICTURE;
259
260     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
261     I_OUTPUTPICTURES++;
262
263     return VLC_SUCCESS;
264 }
265
266 /*****************************************************************************
267  * End: terminate libcaca video output thread
268  *****************************************************************************/
269 static void End( vout_thread_t *p_vout )
270 {
271 #ifdef CACA_API_VERSION_1
272     cucul_free_dither( p_vout->p_sys->p_dither );
273 #else
274     caca_free_bitmap( p_vout->p_sys->p_bitmap );
275 #endif
276 }
277
278 /*****************************************************************************
279  * Destroy: destroy libcaca video output thread
280  *****************************************************************************
281  * Terminate an output method created by AaCreateOutputMethod
282  *****************************************************************************/
283 static void Destroy( vlc_object_t *p_this )
284 {
285     vout_thread_t *p_vout = (vout_thread_t *)p_this;
286
287 #ifdef CACA_API_VERSION_1
288     caca_free_display( p_vout->p_sys->p_dp );
289     cucul_free_canvas( p_vout->p_sys->p_cv );
290 #else
291     caca_end();
292 #endif
293
294 #if defined( WIN32 ) && !defined( UNDER_CE )
295     FreeConsole();
296 #endif
297
298     free( p_vout->p_sys );
299 }
300
301 /*****************************************************************************
302  * Manage: handle libcaca events
303  *****************************************************************************
304  * This function should be called regularly by video output thread. It manages
305  * console events. It returns a non null value on error.
306  *****************************************************************************/
307 static int Manage( vout_thread_t *p_vout )
308 {
309 #ifdef CACA_API_VERSION_1
310     struct caca_event ev;
311 #else
312     int event;
313 #endif
314     vlc_value_t val;
315
316 #ifdef CACA_API_VERSION_1
317     while(( caca_get_event(p_vout->p_sys->p_dp,
318                            CACA_EVENT_KEY_PRESS | CACA_EVENT_RESIZE, &ev, 0) ))
319 #else
320     while(( event = caca_get_event(CACA_EVENT_KEY_PRESS | CACA_EVENT_RESIZE) ))
321 #endif
322     {
323         /* Acknowledge the resize */
324 #ifdef CACA_API_VERSION_1
325         if( ev.type == CACA_EVENT_RESIZE )
326         {
327             caca_refresh_display( p_vout->p_sys->p_dp );
328             continue;
329         }
330 #else
331         if( event == CACA_EVENT_RESIZE )
332         {
333             caca_refresh();
334             continue;
335         }
336 #endif
337
338 #ifdef CACA_API_VERSION_1
339         switch( ev.data.key.ch )
340 #else
341         switch( event & 0x00ffffff )
342 #endif
343         {
344         case 'q':
345             val.i_int = KEY_MODIFIER_CTRL | 'q';
346             break;
347         case ' ':
348             val.i_int = KEY_SPACE;
349             break;
350         default:
351             continue;
352         }
353
354         var_Set( p_vout->p_vlc, "key-pressed", val );
355     }
356
357     return VLC_SUCCESS;
358 }
359
360 /*****************************************************************************
361  * Render: render previously calculated output
362  *****************************************************************************/
363 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
364 {
365 #ifdef CACA_API_VERSION_1
366     cucul_set_color( p_vout->p_sys->p_cv,
367                      CUCUL_COLOR_DEFAULT, CUCUL_COLOR_BLACK );
368     cucul_clear_canvas( p_vout->p_sys->p_cv );
369     cucul_dither_bitmap( p_vout->p_sys->p_cv, 0, 0,
370                          cucul_get_canvas_width( p_vout->p_sys->p_cv ) - 1,
371                          cucul_get_canvas_height( p_vout->p_sys->p_cv ) - 1,
372                          p_vout->p_sys->p_dither, p_pic->p->p_pixels );
373 #else
374     caca_clear();
375     caca_draw_bitmap( 0, 0, caca_get_width() - 1, caca_get_height() - 1,
376                       p_vout->p_sys->p_bitmap, p_pic->p->p_pixels );
377 #endif
378 }
379
380 /*****************************************************************************
381  * Display: displays previously rendered output
382  *****************************************************************************/
383 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
384 {
385 #ifdef CACA_API_VERSION_1
386     caca_refresh_display( p_vout->p_sys->p_dp );
387 #else
388     caca_refresh();
389 #endif
390 }
391