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 */
31 #ifndef CACA_API_VERSION_1
32 /* Upward compatibility macros */
33 typedef char cucul_canvas_t;
34 typedef struct caca_bitmap cucul_dither_t;
35 typedef char caca_display_t;
36 # define CUCUL_COLOR_DEFAULT CACA_COLOR_LIGHTGRAY
37 # define CUCUL_COLOR_BLACK CACA_COLOR_BLACK
38 # define cucul_clear_canvas(x) caca_clear()
39 # define cucul_create_canvas(x,y) "" /* kinda hacky */
40 # define cucul_create_dither caca_create_bitmap
41 # define cucul_dither_bitmap(x,y,z,t,u,v,w) caca_draw_bitmap(y,z,t,u,v,w)
42 # define cucul_free_dither caca_free_bitmap
43 # define cucul_free_canvas(x)
44 # define cucul_get_canvas_width(x) caca_get_width()
45 # define cucul_get_canvas_height(x) caca_get_height()
46 # define cucul_set_color(x,y,z) caca_set_color(y,z)
47 # define caca_create_display(x) (caca_init() ? NULL : "") /* hacky, too */
48 # define caca_free_display(x) caca_end()
49 # define caca_get_event(x,y,z,t) *(z) = caca_get_event(y)
50 # define caca_refresh_display(x) caca_refresh()
51 # define caca_set_display_title(x,y) caca_set_window_title(y)
56 #include <vlc_interface.h>
57 #include <vlc_playlist.h>
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Create ( vlc_object_t * );
64 static void Destroy ( vlc_object_t * );
66 static int Init ( vout_thread_t * );
67 static void End ( vout_thread_t * );
68 static int Manage ( vout_thread_t * );
69 static void Render ( vout_thread_t *, picture_t * );
70 static void Display ( vout_thread_t *, picture_t * );
72 /*****************************************************************************
74 *****************************************************************************/
76 set_shortname( "Caca" );
77 set_category( CAT_VIDEO );
78 set_subcategory( SUBCAT_VIDEO_VOUT );
79 set_description( _("Color ASCII art video output") );
80 set_capability( "video output", 12 );
81 set_callbacks( Create, Destroy );
84 /*****************************************************************************
85 * vout_sys_t: libcaca video output method descriptor
86 *****************************************************************************
87 * This structure is part of the video output thread descriptor.
88 * It describes the libcaca specific properties of an output thread.
89 *****************************************************************************/
94 cucul_dither_t *p_dither;
97 /*****************************************************************************
98 * Create: allocates libcaca video output thread
99 *****************************************************************************
100 * This function initializes libcaca vout method.
101 *****************************************************************************/
102 static int Create( vlc_object_t *p_this )
104 vout_thread_t *p_vout = (vout_thread_t *)p_this;
106 #if defined( WIN32 ) && !defined( UNDER_CE )
107 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
112 if( !AllocConsole() )
114 msg_Err( p_vout, "cannot create console" );
119 CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE,
120 FILE_SHARE_READ | FILE_SHARE_WRITE,
121 NULL, CONSOLE_TEXTMODE_BUFFER, NULL );
122 if( !hstdout || hstdout == INVALID_HANDLE_VALUE )
124 msg_Err( p_vout, "cannot create screen buffer" );
129 if( !SetConsoleActiveScreenBuffer( hstdout) )
131 msg_Err( p_vout, "cannot set active screen buffer" );
136 coord = GetLargestConsoleWindowSize( hstdout );
137 msg_Dbg( p_vout, "SetConsoleWindowInfo: %ix%i", coord.X, coord.Y );
139 /* Force size for now */
143 if( !SetConsoleScreenBufferSize( hstdout, coord ) )
144 msg_Warn( p_vout, "SetConsoleScreenBufferSize %i %i",
147 /* Get the current screen buffer size and window position. */
148 if( GetConsoleScreenBufferInfo( hstdout, &csbiInfo ) )
150 rect.Top = 0; rect.Left = 0;
151 rect.Right = csbiInfo.dwMaximumWindowSize.X - 1;
152 rect.Bottom = csbiInfo.dwMaximumWindowSize.Y - 1;
153 if( !SetConsoleWindowInfo( hstdout, TRUE, &rect ) )
154 msg_Dbg( p_vout, "SetConsoleWindowInfo failed: %ix%i",
155 rect.Right, rect.Bottom );
159 /* Allocate structure */
160 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
161 if( p_vout->p_sys == NULL )
163 msg_Err( p_vout, "out of memory" );
167 p_vout->p_sys->p_cv = cucul_create_canvas(0, 0);
168 if( !p_vout->p_sys->p_cv )
170 msg_Err( p_vout, "cannot initialize libcucul" );
171 free( p_vout->p_sys );
175 p_vout->p_sys->p_dp = caca_create_display( p_vout->p_sys->p_cv );
176 if( !p_vout->p_sys->p_dp )
178 msg_Err( p_vout, "cannot initialize libcaca" );
179 cucul_free_canvas( p_vout->p_sys->p_cv );
180 free( p_vout->p_sys );
184 caca_set_display_title( p_vout->p_sys->p_dp,
185 VOUT_TITLE " - Colour AsCii Art (caca)" );
187 p_vout->pf_init = Init;
188 p_vout->pf_end = End;
189 p_vout->pf_manage = Manage;
190 p_vout->pf_render = Render;
191 p_vout->pf_display = Display;
196 /*****************************************************************************
197 * Init: initialize libcaca video output thread
198 *****************************************************************************/
199 static int Init( vout_thread_t *p_vout )
202 picture_t *p_pic = NULL;
204 I_OUTPUTPICTURES = 0;
206 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
207 p_vout->output.i_width = p_vout->render.i_width;
208 p_vout->output.i_height = p_vout->render.i_height;
209 p_vout->output.i_aspect = p_vout->render.i_aspect;
211 p_vout->output.i_rmask = 0x00ff0000;
212 p_vout->output.i_gmask = 0x0000ff00;
213 p_vout->output.i_bmask = 0x000000ff;
215 /* Create the libcaca dither object */
216 p_vout->p_sys->p_dither = cucul_create_dither
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_dither )
224 msg_Err( p_vout, "could not create libcaca dither object" );
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 cucul_free_dither( p_vout->p_sys->p_dither );
269 /*****************************************************************************
270 * Destroy: destroy libcaca video output thread
271 *****************************************************************************
272 * Terminate an output method created by AaCreateOutputMethod
273 *****************************************************************************/
274 static void Destroy( vlc_object_t *p_this )
276 vout_thread_t *p_vout = (vout_thread_t *)p_this;
278 caca_free_display( p_vout->p_sys->p_dp );
279 cucul_free_canvas( p_vout->p_sys->p_cv );
281 #if defined( WIN32 ) && !defined( UNDER_CE )
285 free( p_vout->p_sys );
288 /*****************************************************************************
289 * Manage: handle libcaca events
290 *****************************************************************************
291 * This function should be called regularly by video output thread. It manages
292 * console events. It returns a non null value on error.
293 *****************************************************************************/
294 static int Manage( vout_thread_t *p_vout )
296 #ifdef CACA_API_VERSION_1
297 struct caca_event ev;
302 while( caca_get_event(p_vout->p_sys->p_dp, CACA_EVENT_ANY, &ev, 0) )
304 playlist_t *p_playlist;
307 #ifdef CACA_API_VERSION_1
313 case CACA_EVENT_KEY_RELEASE:
314 #ifdef CACA_API_VERSION_1
315 switch( ev.data.key.ch )
317 switch( ev & 0x00ffffff )
321 val.i_int = KEY_MODIFIER_CTRL | 'q';
324 val.i_int = KEY_SPACE;
330 var_Set( p_vout->p_libvlc, "key-pressed", val );
332 case CACA_EVENT_RESIZE:
333 /* Acknowledge the resize */
334 caca_refresh_display( p_vout->p_sys->p_dp );
336 #ifdef CACA_API_VERSION_1
337 case CACA_EVENT_MOUSE_MOTION:
338 val.i_int = ev.data.mouse.x * p_vout->render.i_width
339 / cucul_get_canvas_width( p_vout->p_sys->p_cv );
340 var_Set( p_vout, "mouse-x", val );
341 val.i_int = ev.data.mouse.y * p_vout->render.i_height
342 / cucul_get_canvas_height( p_vout->p_sys->p_cv );
343 var_Set( p_vout, "mouse-y", val );
344 val.b_bool = VLC_TRUE;
345 var_Set( p_vout, "mouse-moved", val );
347 case CACA_EVENT_MOUSE_RELEASE:
348 val.b_bool = VLC_TRUE;
349 var_Set( p_vout, "mouse-clicked", val );
351 case CACA_EVENT_QUIT:
353 p_playlist = vlc_object_find( p_vout,
354 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
357 playlist_Stop( p_playlist );
358 vlc_object_release( p_playlist );
360 vlc_object_kill( p_vout->p_libvlc );
372 /*****************************************************************************
373 * Render: render previously calculated output
374 *****************************************************************************/
375 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
377 cucul_set_color( p_vout->p_sys->p_cv,
378 CUCUL_COLOR_DEFAULT, CUCUL_COLOR_BLACK );
379 cucul_clear_canvas( p_vout->p_sys->p_cv );
380 cucul_dither_bitmap( p_vout->p_sys->p_cv, 0, 0,
381 cucul_get_canvas_width( p_vout->p_sys->p_cv ) - 1,
382 cucul_get_canvas_height( p_vout->p_sys->p_cv ) - 1,
383 p_vout->p_sys->p_dither, p_pic->p->p_pixels );
386 /*****************************************************************************
387 * Display: displays previously rendered output
388 *****************************************************************************/
389 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
391 caca_refresh_display( p_vout->p_sys->p_dp );