1 /*****************************************************************************
2 * caca.c: Color ASCII Art video output plugin using libcaca
3 *****************************************************************************
4 * Copyright (C) 2003, 2004 the VideoLAN team
7 * Authors: Sam Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <errno.h> /* ENOMEM */
28 #include <stdlib.h> /* free() */
29 #include <string.h> /* strerror() */
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Create ( vlc_object_t * );
42 static void Destroy ( vlc_object_t * );
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 * );
50 /*****************************************************************************
52 *****************************************************************************/
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 );
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 *****************************************************************************/
70 #ifdef CACA_API_VERSION_1
71 struct cucul_context *p_qq;
72 struct caca_context *p_kk;
73 struct cucul_bitmap *p_bitmap;
75 struct caca_bitmap *p_bitmap;
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 )
86 vout_thread_t *p_vout = (vout_thread_t *)p_this;
88 #if defined( WIN32 ) && !defined( UNDER_CE )
91 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
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 )
101 msg_Err( p_vout, "cannot create screen buffer" );
106 if( !SetConsoleActiveScreenBuffer( hstdout) )
108 msg_Err( p_vout, "cannot set active screen buffer" );
113 coord = GetLargestConsoleWindowSize( hstdout );
114 msg_Dbg( p_vout, "SetConsoleWindowInfo: %ix%i", coord.X, coord.Y );
116 /* Force size for now */
120 if( !SetConsoleScreenBufferSize( hstdout, coord ) )
121 msg_Warn( p_vout, "SetConsoleScreenBufferSize %i %i",
124 /* Get the current screen buffer size and window position. */
125 if( GetConsoleScreenBufferInfo( hstdout, &csbiInfo ) )
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 );
137 msg_Err( p_vout, "cannot create console" );
143 /* Allocate structure */
144 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
145 if( p_vout->p_sys == NULL )
147 msg_Err( p_vout, "out of memory" );
151 #ifdef CACA_API_VERSION_1
152 p_vout->p_sys->p_qq = cucul_init(0, 0);
153 if( !p_vout->p_sys->p_qq )
155 msg_Err( p_vout, "cannot initialize libcucul" );
156 free( p_vout->p_sys );
160 p_vout->p_sys->p_kk = caca_attach( p_vout->p_sys->p_qq );
161 if( !p_vout->p_sys->p_kk )
163 msg_Err( p_vout, "cannot initialize libcaca" );
164 cucul_end( p_vout->p_sys->p_qq );
165 free( p_vout->p_sys );
169 caca_set_window_title( p_vout->p_sys->p_kk,
170 VOUT_TITLE " - Colour AsCii Art (caca)" );
174 msg_Err( p_vout, "cannot initialize libcaca" );
175 free( p_vout->p_sys );
179 caca_set_window_title( VOUT_TITLE " - Colour AsCii Art (caca)" );
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;
191 /*****************************************************************************
192 * Init: initialize libcaca video output thread
193 *****************************************************************************/
194 static int Init( vout_thread_t *p_vout )
197 picture_t *p_pic = NULL;
199 I_OUTPUTPICTURES = 0;
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;
206 p_vout->output.i_rmask = 0x00ff0000;
207 p_vout->output.i_gmask = 0x0000ff00;
208 p_vout->output.i_bmask = 0x000000ff;
210 /* Create the libcaca bitmap */
211 p_vout->p_sys->p_bitmap =
212 #ifdef CACA_API_VERSION_1
217 ( 32, p_vout->output.i_width, p_vout->output.i_height,
218 4 * ((p_vout->output.i_width + 15) & ~15),
219 p_vout->output.i_rmask, p_vout->output.i_gmask,
220 p_vout->output.i_bmask, 0x00000000 );
222 if( !p_vout->p_sys->p_bitmap )
224 msg_Err( p_vout, "could not create libcaca bitmap" );
228 /* Find an empty picture slot */
229 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
231 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
233 p_pic = p_vout->p_picture + i_index;
243 /* Allocate the picture */
244 p_pic->p->i_lines = p_vout->output.i_height;
245 p_pic->p->i_visible_lines = p_vout->output.i_height;
246 p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
247 p_pic->p->i_pixel_pitch = 4;
248 p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
250 p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );
252 p_pic->i_status = DESTROYED_PICTURE;
253 p_pic->i_type = DIRECT_PICTURE;
255 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
261 /*****************************************************************************
262 * End: terminate libcaca video output thread
263 *****************************************************************************/
264 static void End( vout_thread_t *p_vout )
266 #ifdef CACA_API_VERSION_1
267 cucul_free_bitmap( p_vout->p_sys->p_bitmap );
269 caca_free_bitmap( p_vout->p_sys->p_bitmap );
273 /*****************************************************************************
274 * Destroy: destroy libcaca video output thread
275 *****************************************************************************
276 * Terminate an output method created by AaCreateOutputMethod
277 *****************************************************************************/
278 static void Destroy( vlc_object_t *p_this )
280 vout_thread_t *p_vout = (vout_thread_t *)p_this;
282 #ifdef CACA_API_VERSION_1
283 caca_detach( p_vout->p_sys->p_kk );
284 cucul_end( p_vout->p_sys->p_qq );
289 #if defined( WIN32 ) && !defined( UNDER_CE )
293 free( p_vout->p_sys );
296 /*****************************************************************************
297 * Manage: handle libcaca events
298 *****************************************************************************
299 * This function should be called regularly by video output thread. It manages
300 * console events. It returns a non null value on error.
301 *****************************************************************************/
302 static int Manage( vout_thread_t *p_vout )
307 #ifdef CACA_API_VERSION_1
308 while(( event = caca_get_event(p_vout->p_sys->p_kk,
309 CACA_EVENT_KEY_PRESS | CACA_EVENT_RESIZE) ))
311 while(( event = caca_get_event(CACA_EVENT_KEY_PRESS | CACA_EVENT_RESIZE) ))
314 if( event == CACA_EVENT_RESIZE )
316 /* Acknowledge the resize */
317 #ifdef CACA_API_VERSION_1
318 caca_display( p_vout->p_sys->p_kk );
325 switch( event & 0x00ffffff )
328 val.i_int = KEY_MODIFIER_CTRL | 'q';
331 val.i_int = KEY_SPACE;
337 var_Set( p_vout->p_vlc, "key-pressed", val );
343 /*****************************************************************************
344 * Render: render previously calculated output
345 *****************************************************************************/
346 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
348 #ifdef CACA_API_VERSION_1
349 cucul_clear( p_vout->p_sys->p_qq );
350 cucul_draw_bitmap( p_vout->p_sys->p_qq,
352 cucul_get_width( p_vout->p_sys->p_qq ) - 1,
353 cucul_get_height( p_vout->p_sys->p_qq ) - 1,
354 p_vout->p_sys->p_bitmap, p_pic->p->p_pixels );
357 caca_draw_bitmap( 0, 0, caca_get_width() - 1, caca_get_height() - 1,
358 p_vout->p_sys->p_bitmap, p_pic->p->p_pixels );
362 /*****************************************************************************
363 * Display: displays previously rendered output
364 *****************************************************************************/
365 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
367 #ifdef CACA_API_VERSION_1
368 caca_display( p_vout->p_sys->p_kk );