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 *****************************************************************************/
30 #include <vlc_interface.h>
31 #include <vlc_playlist.h>
36 #ifndef CACA_API_VERSION_1
37 /* Upward compatibility macros */
38 typedef char cucul_canvas_t;
39 typedef struct caca_bitmap cucul_dither_t;
40 typedef char caca_display_t;
41 # define CUCUL_COLOR_DEFAULT CACA_COLOR_LIGHTGRAY
42 # define CUCUL_COLOR_BLACK CACA_COLOR_BLACK
43 # define cucul_clear_canvas(x) caca_clear()
44 # define cucul_create_canvas(x,y) "" /* kinda hacky */
45 # define cucul_create_dither caca_create_bitmap
46 # define cucul_dither_bitmap(x,y,z,t,u,v,w) caca_draw_bitmap(y,z,t,u,v,w)
47 # define cucul_free_dither caca_free_bitmap
48 # define cucul_free_canvas(x)
49 # define cucul_get_canvas_width(x) caca_get_width()
50 # define cucul_get_canvas_height(x) caca_get_height()
51 # define cucul_set_color(x,y,z) caca_set_color(y,z)
52 # define caca_create_display(x) (caca_init() ? NULL : "") /* hacky, too */
53 # define caca_free_display(x) caca_end()
54 # define caca_get_event(x,y,z,t) *(z) = caca_get_event(y)
55 # define caca_refresh_display(x) caca_refresh()
56 # define caca_set_display_title(x,y) caca_set_window_title(y)
59 /*****************************************************************************
61 *****************************************************************************/
62 static int Create ( vlc_object_t * );
63 static void Destroy ( vlc_object_t * );
65 static int Init ( vout_thread_t * );
66 static void End ( vout_thread_t * );
67 static int Manage ( vout_thread_t * );
68 static void Render ( vout_thread_t *, picture_t * );
69 static void Display ( vout_thread_t *, picture_t * );
71 /*****************************************************************************
73 *****************************************************************************/
75 set_shortname( "Caca" );
76 set_category( CAT_VIDEO );
77 set_subcategory( SUBCAT_VIDEO_VOUT );
78 set_description( _("Color ASCII art video output") );
79 set_capability( "video output", 12 );
80 set_callbacks( Create, Destroy );
83 /*****************************************************************************
84 * vout_sys_t: libcaca video output method descriptor
85 *****************************************************************************
86 * This structure is part of the video output thread descriptor.
87 * It describes the libcaca specific properties of an output thread.
88 *****************************************************************************/
93 cucul_dither_t *p_dither;
96 /*****************************************************************************
97 * Create: allocates libcaca video output thread
98 *****************************************************************************
99 * This function initializes libcaca vout method.
100 *****************************************************************************/
101 static int Create( vlc_object_t *p_this )
103 vout_thread_t *p_vout = (vout_thread_t *)p_this;
105 #if defined( WIN32 ) && !defined( UNDER_CE )
106 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
111 if( !AllocConsole() )
113 msg_Err( p_vout, "cannot create console" );
118 CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE,
119 FILE_SHARE_READ | FILE_SHARE_WRITE,
120 NULL, CONSOLE_TEXTMODE_BUFFER, NULL );
121 if( !hstdout || hstdout == INVALID_HANDLE_VALUE )
123 msg_Err( p_vout, "cannot create screen buffer" );
128 if( !SetConsoleActiveScreenBuffer( hstdout) )
130 msg_Err( p_vout, "cannot set active screen buffer" );
135 coord = GetLargestConsoleWindowSize( hstdout );
136 msg_Dbg( p_vout, "SetConsoleWindowInfo: %ix%i", coord.X, coord.Y );
138 /* Force size for now */
142 if( !SetConsoleScreenBufferSize( hstdout, coord ) )
143 msg_Warn( p_vout, "SetConsoleScreenBufferSize %i %i",
146 /* Get the current screen buffer size and window position. */
147 if( GetConsoleScreenBufferInfo( hstdout, &csbiInfo ) )
149 rect.Top = 0; rect.Left = 0;
150 rect.Right = csbiInfo.dwMaximumWindowSize.X - 1;
151 rect.Bottom = csbiInfo.dwMaximumWindowSize.Y - 1;
152 if( !SetConsoleWindowInfo( hstdout, TRUE, &rect ) )
153 msg_Dbg( p_vout, "SetConsoleWindowInfo failed: %ix%i",
154 rect.Right, rect.Bottom );
158 /* Allocate structure */
159 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
160 if( p_vout->p_sys == NULL )
162 msg_Err( p_vout, "out of memory" );
166 p_vout->p_sys->p_cv = cucul_create_canvas(0, 0);
167 if( !p_vout->p_sys->p_cv )
169 msg_Err( p_vout, "cannot initialize libcucul" );
170 free( p_vout->p_sys );
174 p_vout->p_sys->p_dp = caca_create_display( p_vout->p_sys->p_cv );
175 if( !p_vout->p_sys->p_dp )
177 msg_Err( p_vout, "cannot initialize libcaca" );
178 cucul_free_canvas( p_vout->p_sys->p_cv );
179 free( p_vout->p_sys );
183 caca_set_display_title( p_vout->p_sys->p_dp,
184 VOUT_TITLE " - Colour AsCii Art (caca)" );
186 p_vout->pf_init = Init;
187 p_vout->pf_end = End;
188 p_vout->pf_manage = Manage;
189 p_vout->pf_render = Render;
190 p_vout->pf_display = Display;
195 /*****************************************************************************
196 * Init: initialize libcaca video output thread
197 *****************************************************************************/
198 static int Init( vout_thread_t *p_vout )
201 picture_t *p_pic = NULL;
203 I_OUTPUTPICTURES = 0;
205 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
206 p_vout->output.i_width = p_vout->render.i_width;
207 p_vout->output.i_height = p_vout->render.i_height;
208 p_vout->output.i_aspect = p_vout->render.i_aspect;
210 p_vout->output.i_rmask = 0x00ff0000;
211 p_vout->output.i_gmask = 0x0000ff00;
212 p_vout->output.i_bmask = 0x000000ff;
214 /* Create the libcaca dither object */
215 p_vout->p_sys->p_dither = cucul_create_dither
216 ( 32, p_vout->output.i_width, p_vout->output.i_height,
217 4 * ((p_vout->output.i_width + 15) & ~15),
218 p_vout->output.i_rmask, p_vout->output.i_gmask,
219 p_vout->output.i_bmask, 0x00000000 );
221 if( !p_vout->p_sys->p_dither )
223 msg_Err( p_vout, "could not create libcaca dither object" );
227 /* Find an empty picture slot */
228 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
230 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
232 p_pic = p_vout->p_picture + i_index;
242 /* Allocate the picture */
243 p_pic->p->i_lines = p_vout->output.i_height;
244 p_pic->p->i_visible_lines = p_vout->output.i_height;
245 p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
246 p_pic->p->i_pixel_pitch = 4;
247 p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
249 p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );
251 p_pic->i_status = DESTROYED_PICTURE;
252 p_pic->i_type = DIRECT_PICTURE;
254 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
260 /*****************************************************************************
261 * End: terminate libcaca video output thread
262 *****************************************************************************/
263 static void End( vout_thread_t *p_vout )
265 cucul_free_dither( p_vout->p_sys->p_dither );
268 /*****************************************************************************
269 * Destroy: destroy libcaca video output thread
270 *****************************************************************************
271 * Terminate an output method created by AaCreateOutputMethod
272 *****************************************************************************/
273 static void Destroy( vlc_object_t *p_this )
275 vout_thread_t *p_vout = (vout_thread_t *)p_this;
277 caca_free_display( p_vout->p_sys->p_dp );
278 cucul_free_canvas( p_vout->p_sys->p_cv );
280 #if defined( WIN32 ) && !defined( UNDER_CE )
284 free( p_vout->p_sys );
287 /*****************************************************************************
288 * Manage: handle libcaca events
289 *****************************************************************************
290 * This function should be called regularly by video output thread. It manages
291 * console events. It returns a non null value on error.
292 *****************************************************************************/
293 static int Manage( vout_thread_t *p_vout )
295 #ifdef CACA_API_VERSION_1
296 struct caca_event ev;
301 while( caca_get_event(p_vout->p_sys->p_dp, CACA_EVENT_ANY, &ev, 0) )
303 playlist_t *p_playlist;
306 #ifdef CACA_API_VERSION_1
312 case CACA_EVENT_KEY_RELEASE:
313 #ifdef CACA_API_VERSION_1
314 switch( ev.data.key.ch )
316 switch( ev & 0x00ffffff )
320 val.i_int = KEY_MODIFIER_CTRL | 'q';
323 val.i_int = KEY_SPACE;
329 var_Set( p_vout->p_libvlc, "key-pressed", val );
331 case CACA_EVENT_RESIZE:
332 /* Acknowledge the resize */
333 caca_refresh_display( p_vout->p_sys->p_dp );
335 #ifdef CACA_API_VERSION_1
336 case CACA_EVENT_MOUSE_MOTION:
337 val.i_int = ev.data.mouse.x * p_vout->render.i_width
338 / cucul_get_canvas_width( p_vout->p_sys->p_cv );
339 var_Set( p_vout, "mouse-x", val );
340 val.i_int = ev.data.mouse.y * p_vout->render.i_height
341 / cucul_get_canvas_height( p_vout->p_sys->p_cv );
342 var_Set( p_vout, "mouse-y", val );
343 val.b_bool = VLC_TRUE;
344 var_Set( p_vout, "mouse-moved", val );
346 case CACA_EVENT_MOUSE_RELEASE:
347 val.b_bool = VLC_TRUE;
348 var_Set( p_vout, "mouse-clicked", val );
350 case CACA_EVENT_QUIT:
352 p_playlist = vlc_object_find( p_vout,
353 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
356 playlist_Stop( p_playlist );
357 vlc_object_release( p_playlist );
359 vlc_object_kill( p_vout->p_libvlc );
371 /*****************************************************************************
372 * Render: render previously calculated output
373 *****************************************************************************/
374 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
376 cucul_set_color( p_vout->p_sys->p_cv,
377 CUCUL_COLOR_DEFAULT, CUCUL_COLOR_BLACK );
378 cucul_clear_canvas( p_vout->p_sys->p_cv );
379 cucul_dither_bitmap( p_vout->p_sys->p_cv, 0, 0,
380 cucul_get_canvas_width( p_vout->p_sys->p_cv ) - 1,
381 cucul_get_canvas_height( p_vout->p_sys->p_cv ) - 1,
382 p_vout->p_sys->p_dither, p_pic->p->p_pixels );
385 /*****************************************************************************
386 * Display: displays previously rendered output
387 *****************************************************************************/
388 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
390 caca_refresh_display( p_vout->p_sys->p_dp );