1
/*****************************************************************************
2
* caca.c: Color ASCII Art video output plugin using libcaca
3
*****************************************************************************
4
* Copyright (C) 2003, 2004 VideoLAN
5
* $Id: caca.c 7522 2004-04-27 16:35:15Z sam $
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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_description( _("color ASCII art video output") );
55
set_capability( "video output", 12 );
56
set_callbacks( Create, Destroy );
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
*****************************************************************************/
67
struct caca_bitmap *p_bitmap;
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 )
77
vout_thread_t *p_vout = (vout_thread_t *)p_this;
79
#if defined( WIN32 ) && !defined( UNDER_CE )
82
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
87
CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE,
88
FILE_SHARE_READ | FILE_SHARE_WRITE,
89
NULL, CONSOLE_TEXTMODE_BUFFER, NULL );
90
if( !hstdout || hstdout == INVALID_HANDLE_VALUE )
92
msg_Err( p_vout, "cannot create screen buffer" );
97
if( !SetConsoleActiveScreenBuffer( hstdout) )
99
msg_Err( p_vout, "cannot set active screen buffer" );
104
coord = GetLargestConsoleWindowSize( hstdout );
105
msg_Dbg( p_vout, "SetConsoleWindowInfo: %ix%i", coord.X, coord.Y );
107
/* Force size for now */
111
if( !SetConsoleScreenBufferSize( hstdout, coord ) )
112
msg_Warn( p_vout, "SetConsoleScreenBufferSize %i %i",
115
/* Get the current screen buffer size and window position. */
116
if( GetConsoleScreenBufferInfo( hstdout, &csbiInfo ) )
118
rect.Top = 0; rect.Left = 0;
119
rect.Right = csbiInfo.dwMaximumWindowSize.X - 1;
120
rect.Bottom = csbiInfo.dwMaximumWindowSize.Y - 1;
121
if( !SetConsoleWindowInfo( hstdout, TRUE, &rect ) )
122
msg_Dbg( p_vout, "SetConsoleWindowInfo failed: %ix%i",
123
rect.Right, rect.Bottom );
128
msg_Err( p_vout, "cannot create console" );
134
/* Allocate structure */
135
p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
136
if( p_vout->p_sys == NULL )
138
msg_Err( p_vout, "out of memory" );
144
msg_Err( p_vout, "cannot initialize libcaca" );
145
free( p_vout->p_sys );
149
caca_set_window_title( VOUT_TITLE " - Colour AsCii Art (caca)" );
151
p_vout->pf_init = Init;
152
p_vout->pf_end = End;
153
p_vout->pf_manage = Manage;
154
p_vout->pf_render = Render;
155
p_vout->pf_display = Display;
160
/*****************************************************************************
161
* Init: initialize libcaca video output thread
162
*****************************************************************************/
163
static int Init( vout_thread_t *p_vout )
166
picture_t *p_pic = NULL;
168
I_OUTPUTPICTURES = 0;
170
p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
171
p_vout->output.i_width = p_vout->render.i_width;
172
p_vout->output.i_height = p_vout->render.i_height;
173
p_vout->output.i_aspect = p_vout->render.i_aspect;
175
p_vout->output.i_rmask = 0x00ff0000;
176
p_vout->output.i_gmask = 0x0000ff00;
177
p_vout->output.i_bmask = 0x000000ff;
179
/* Create the libcaca bitmap */
180
p_vout->p_sys->p_bitmap =
181
caca_create_bitmap( 32,
182
p_vout->output.i_width,
183
p_vout->output.i_height,
184
4 * ((p_vout->output.i_width + 15) & ~15),
185
p_vout->output.i_rmask,
186
p_vout->output.i_gmask,
187
p_vout->output.i_bmask,
189
if( !p_vout->p_sys->p_bitmap )
191
msg_Err( p_vout, "could not create libcaca bitmap" );
195
/* Find an empty picture slot */
196
for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
198
if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
200
p_pic = p_vout->p_picture + i_index;
210
/* Allocate the picture */
211
p_pic->p->i_lines = p_vout->output.i_height;
212
p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
213
p_pic->p->i_pixel_pitch = 4;
214
p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
216
p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );
218
p_pic->i_status = DESTROYED_PICTURE;
219
p_pic->i_type = DIRECT_PICTURE;
221
PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
227
/*****************************************************************************
228
* End: terminate libcaca video output thread
229
*****************************************************************************/
230
static void End( vout_thread_t *p_vout )
232
caca_free_bitmap( p_vout->p_sys->p_bitmap );
235
/*****************************************************************************
236
* Destroy: destroy libcaca video output thread
237
*****************************************************************************
238
* Terminate an output method created by AaCreateOutputMethod
239
*****************************************************************************/
240
static void Destroy( vlc_object_t *p_this )
242
vout_thread_t *p_vout = (vout_thread_t *)p_this;
246
#if defined( WIN32 ) && !defined( UNDER_CE )
250
free( p_vout->p_sys );
253
/*****************************************************************************
254
* Manage: handle libcaca events
255
*****************************************************************************
256
* This function should be called regularly by video output thread. It manages
257
* console events. It returns a non null value on error.
258
*****************************************************************************/
259
static int Manage( vout_thread_t *p_vout )
264
while(( event = caca_get_event(CACA_EVENT_KEY_PRESS | CACA_EVENT_RESIZE) ))
266
if( event == CACA_EVENT_RESIZE )
268
/* Acknowledge the resize */
273
switch( event & 0x00ffffff )
276
val.i_int = KEY_MODIFIER_CTRL | 'q';
279
val.i_int = KEY_SPACE;
285
var_Set( p_vout->p_vlc, "key-pressed", val );
291
/*****************************************************************************
292
* Render: render previously calculated output
293
*****************************************************************************/
294
static void Render( vout_thread_t *p_vout, picture_t *p_pic )
297
caca_draw_bitmap( 0, 0, caca_get_width() - 1, caca_get_height() - 1,
298
p_vout->p_sys->p_bitmap, p_pic->p->p_pixels );
301
/*****************************************************************************
302
* Display: displays previously rendered output
303
*****************************************************************************/
304
static void Display( vout_thread_t *p_vout, picture_t *p_pic )