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 *****************************************************************************/
33 #include <vlc_plugin.h>
35 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
41 #ifndef CACA_API_VERSION_1
42 /* Upward compatibility macros */
43 typedef char cucul_canvas_t;
44 typedef struct caca_bitmap cucul_dither_t;
45 typedef char caca_display_t;
46 # define CUCUL_COLOR_DEFAULT CACA_COLOR_LIGHTGRAY
47 # define CUCUL_COLOR_BLACK CACA_COLOR_BLACK
48 # define cucul_clear_canvas(x) caca_clear()
49 # define cucul_create_canvas(x,y) "" /* kinda hacky */
50 # define cucul_create_dither caca_create_bitmap
51 # define cucul_dither_bitmap(x,y,z,t,u,v,w) caca_draw_bitmap(y,z,t,u,v,w)
52 # define cucul_free_dither caca_free_bitmap
53 # define cucul_free_canvas(x)
54 # define cucul_get_canvas_width(x) caca_get_width()
55 # define cucul_get_canvas_height(x) caca_get_height()
56 # define cucul_set_color(x,y,z) caca_set_color(y,z)
57 # define caca_create_display(x) (caca_init() ? NULL : "") /* hacky, too */
58 # define caca_free_display(x) caca_end()
59 # define caca_get_event(x,y,z,t) *(z) = caca_get_event(y)
60 # define caca_refresh_display(x) caca_refresh()
61 # define caca_set_display_title(x,y) caca_set_window_title(y)
64 /*****************************************************************************
66 *****************************************************************************/
67 static int Create ( vlc_object_t * );
68 static void Destroy ( vlc_object_t * );
70 static int Init ( vout_thread_t * );
71 static void End ( vout_thread_t * );
72 static int Manage ( vout_thread_t * );
73 static void Render ( vout_thread_t *, picture_t * );
74 static void Display ( vout_thread_t *, picture_t * );
76 /*****************************************************************************
78 *****************************************************************************/
80 set_shortname( "Caca" );
81 set_category( CAT_VIDEO );
82 set_subcategory( SUBCAT_VIDEO_VOUT );
83 set_description( _("Color ASCII art video output") );
84 set_capability( "video output", 12 );
85 set_callbacks( Create, Destroy );
88 /*****************************************************************************
89 * vout_sys_t: libcaca video output method descriptor
90 *****************************************************************************
91 * This structure is part of the video output thread descriptor.
92 * It describes the libcaca specific properties of an output thread.
93 *****************************************************************************/
98 cucul_dither_t *p_dither;
101 /*****************************************************************************
102 * Create: allocates libcaca video output thread
103 *****************************************************************************
104 * This function initializes libcaca vout method.
105 *****************************************************************************/
106 static int Create( vlc_object_t *p_this )
108 vout_thread_t *p_vout = (vout_thread_t *)p_this;
110 #if defined( WIN32 ) && !defined( UNDER_CE )
111 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
116 if( !AllocConsole() )
118 msg_Err( p_vout, "cannot create console" );
123 CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE,
124 FILE_SHARE_READ | FILE_SHARE_WRITE,
125 NULL, CONSOLE_TEXTMODE_BUFFER, NULL );
126 if( !hstdout || hstdout == INVALID_HANDLE_VALUE )
128 msg_Err( p_vout, "cannot create screen buffer" );
133 if( !SetConsoleActiveScreenBuffer( hstdout) )
135 msg_Err( p_vout, "cannot set active screen buffer" );
140 coord = GetLargestConsoleWindowSize( hstdout );
141 msg_Dbg( p_vout, "SetConsoleWindowInfo: %ix%i", coord.X, coord.Y );
143 /* Force size for now */
147 if( !SetConsoleScreenBufferSize( hstdout, coord ) )
148 msg_Warn( p_vout, "SetConsoleScreenBufferSize %i %i",
151 /* Get the current screen buffer size and window position. */
152 if( GetConsoleScreenBufferInfo( hstdout, &csbiInfo ) )
154 rect.Top = 0; rect.Left = 0;
155 rect.Right = csbiInfo.dwMaximumWindowSize.X - 1;
156 rect.Bottom = csbiInfo.dwMaximumWindowSize.Y - 1;
157 if( !SetConsoleWindowInfo( hstdout, TRUE, &rect ) )
158 msg_Dbg( p_vout, "SetConsoleWindowInfo failed: %ix%i",
159 rect.Right, rect.Bottom );
163 /* Allocate structure */
164 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
165 if( p_vout->p_sys == NULL )
167 msg_Err( p_vout, "out of memory" );
171 p_vout->p_sys->p_cv = cucul_create_canvas(0, 0);
172 if( !p_vout->p_sys->p_cv )
174 msg_Err( p_vout, "cannot initialize libcucul" );
175 free( p_vout->p_sys );
179 p_vout->p_sys->p_dp = caca_create_display( p_vout->p_sys->p_cv );
180 if( !p_vout->p_sys->p_dp )
182 msg_Err( p_vout, "cannot initialize libcaca" );
183 cucul_free_canvas( p_vout->p_sys->p_cv );
184 free( p_vout->p_sys );
188 caca_set_display_title( p_vout->p_sys->p_dp,
189 VOUT_TITLE " - Colour AsCii Art (caca)" );
191 p_vout->pf_init = Init;
192 p_vout->pf_end = End;
193 p_vout->pf_manage = Manage;
194 p_vout->pf_render = Render;
195 p_vout->pf_display = Display;
200 /*****************************************************************************
201 * Init: initialize libcaca video output thread
202 *****************************************************************************/
203 static int Init( vout_thread_t *p_vout )
206 picture_t *p_pic = NULL;
208 I_OUTPUTPICTURES = 0;
210 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
211 p_vout->output.i_width = p_vout->render.i_width;
212 p_vout->output.i_height = p_vout->render.i_height;
213 p_vout->output.i_aspect = p_vout->render.i_aspect;
215 p_vout->output.i_rmask = 0x00ff0000;
216 p_vout->output.i_gmask = 0x0000ff00;
217 p_vout->output.i_bmask = 0x000000ff;
219 /* Create the libcaca dither object */
220 p_vout->p_sys->p_dither = cucul_create_dither
221 ( 32, p_vout->output.i_width, p_vout->output.i_height,
222 4 * ((p_vout->output.i_width + 15) & ~15),
223 p_vout->output.i_rmask, p_vout->output.i_gmask,
224 p_vout->output.i_bmask, 0x00000000 );
226 if( !p_vout->p_sys->p_dither )
228 msg_Err( p_vout, "could not create libcaca dither object" );
232 /* Find an empty picture slot */
233 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
235 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
237 p_pic = p_vout->p_picture + i_index;
247 /* Allocate the picture */
248 p_pic->p->i_lines = p_vout->output.i_height;
249 p_pic->p->i_visible_lines = p_vout->output.i_height;
250 p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
251 p_pic->p->i_pixel_pitch = 4;
252 p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
254 p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );
256 p_pic->i_status = DESTROYED_PICTURE;
257 p_pic->i_type = DIRECT_PICTURE;
259 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
265 /*****************************************************************************
266 * End: terminate libcaca video output thread
267 *****************************************************************************/
268 static void End( vout_thread_t *p_vout )
270 cucul_free_dither( p_vout->p_sys->p_dither );
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 caca_free_display( p_vout->p_sys->p_dp );
283 cucul_free_canvas( p_vout->p_sys->p_cv );
285 #if defined( WIN32 ) && !defined( UNDER_CE )
289 free( p_vout->p_sys );
292 /*****************************************************************************
293 * Manage: handle libcaca events
294 *****************************************************************************
295 * This function should be called regularly by video output thread. It manages
296 * console events. It returns a non null value on error.
297 *****************************************************************************/
298 static int Manage( vout_thread_t *p_vout )
300 #ifdef CACA_API_VERSION_1
301 struct caca_event ev;
306 while( caca_get_event(p_vout->p_sys->p_dp, CACA_EVENT_ANY, &ev, 0) )
308 playlist_t *p_playlist;
311 #ifdef CACA_API_VERSION_1
312 #ifdef CACA_EVENT_OPAQUE
313 switch( caca_get_event_type( &ev ) )
316 #endif /* CACA_EVENT_OPAQUE */
321 case CACA_EVENT_KEY_RELEASE:
322 #ifdef CACA_API_VERSION_1
323 #ifdef CACA_EVENT_OPAQUE
324 switch( caca_get_event_key_ch( &ev ) )
326 switch( ev.data.key.ch )
327 #endif /* CACA_EVENT_OPAQUE */
329 switch( ev & 0x00ffffff )
333 val.i_int = KEY_MODIFIER_CTRL | 'q';
336 val.i_int = KEY_SPACE;
342 var_Set( p_vout->p_libvlc, "key-pressed", val );
344 case CACA_EVENT_RESIZE:
345 /* Acknowledge the resize */
346 caca_refresh_display( p_vout->p_sys->p_dp );
348 #ifdef CACA_API_VERSION_1
349 case CACA_EVENT_MOUSE_MOTION:
351 #ifdef CACA_EVENT_OPAQUE
352 caca_get_event_mouse_x( &ev )
355 #endif /* CACA_EVENT_OPAQUE */
356 * p_vout->render.i_width
357 / cucul_get_canvas_width( p_vout->p_sys->p_cv );
358 var_Set( p_vout, "mouse-x", val );
360 #ifdef CACA_EVENT_OPAQUE
361 caca_get_event_mouse_y( &ev )
364 #endif /* CACA_EVENT_OPAQUE */
365 * p_vout->render.i_height
366 / cucul_get_canvas_height( p_vout->p_sys->p_cv );
367 var_Set( p_vout, "mouse-y", val );
369 var_Set( p_vout, "mouse-moved", val );
371 case CACA_EVENT_MOUSE_RELEASE:
373 var_Set( p_vout, "mouse-clicked", val );
375 case CACA_EVENT_QUIT:
377 p_playlist = vlc_object_find( p_vout,
378 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
381 playlist_Stop( p_playlist );
382 vlc_object_release( p_playlist );
384 vlc_object_kill( p_vout->p_libvlc );
396 /*****************************************************************************
397 * Render: render previously calculated output
398 *****************************************************************************/
399 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
401 cucul_set_color( p_vout->p_sys->p_cv,
402 CUCUL_COLOR_DEFAULT, CUCUL_COLOR_BLACK );
403 cucul_clear_canvas( p_vout->p_sys->p_cv );
404 cucul_dither_bitmap( p_vout->p_sys->p_cv, 0, 0,
405 cucul_get_canvas_width( p_vout->p_sys->p_cv ) - 1,
406 cucul_get_canvas_height( p_vout->p_sys->p_cv ) - 1,
407 p_vout->p_sys->p_dither, p_pic->p->p_pixels );
410 /*****************************************************************************
411 * Display: displays previously rendered output
412 *****************************************************************************/
413 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
416 caca_refresh_display( p_vout->p_sys->p_dp );