]> git.sesse.net Git - vlc/blob - modules/video_output/caca.c
Merge branch 'master' of git@git.videolan.org:vlc
[vlc] / modules / video_output / caca.c
1 /*****************************************************************************
2  * caca.c: Color ASCII Art video output plugin using libcaca
3  *****************************************************************************
4  * Copyright (C) 2003, 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_vout.h>
34 #include <vlc_interface.h>
35 #include <vlc_playlist.h>
36 #include <vlc_keys.h>
37
38 #include <caca.h>
39
40 #ifndef CACA_API_VERSION_1
41     /* Upward compatibility macros */
42     typedef char cucul_canvas_t;
43     typedef struct caca_bitmap cucul_dither_t;
44     typedef char caca_display_t;
45 #   define CUCUL_COLOR_DEFAULT CACA_COLOR_LIGHTGRAY
46 #   define CUCUL_COLOR_BLACK CACA_COLOR_BLACK
47 #   define cucul_clear_canvas(x) caca_clear()
48 #   define cucul_create_canvas(x,y) "" /* kinda hacky */
49 #   define cucul_create_dither caca_create_bitmap
50 #   define cucul_dither_bitmap(x,y,z,t,u,v,w) caca_draw_bitmap(y,z,t,u,v,w)
51 #   define cucul_free_dither caca_free_bitmap
52 #   define cucul_free_canvas(x)
53 #   define cucul_get_canvas_width(x) caca_get_width()
54 #   define cucul_get_canvas_height(x) caca_get_height()
55 #   define cucul_set_color(x,y,z) caca_set_color(y,z)
56 #   define caca_create_display(x) (caca_init() ? NULL : "") /* hacky, too */
57 #   define caca_free_display(x) caca_end()
58 #   define caca_get_event(x,y,z,t) *(z) = caca_get_event(y)
59 #   define caca_refresh_display(x) caca_refresh()
60 #   define caca_set_display_title(x,y) caca_set_window_title(y)
61 #endif
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static int  Create    ( vlc_object_t * );
67 static void Destroy   ( vlc_object_t * );
68
69 static int  Init      ( vout_thread_t * );
70 static void End       ( vout_thread_t * );
71 static int  Manage    ( vout_thread_t * );
72 static void Render    ( vout_thread_t *, picture_t * );
73 static void Display   ( vout_thread_t *, picture_t * );
74
75 /*****************************************************************************
76  * Module descriptor
77  *****************************************************************************/
78 vlc_module_begin();
79     set_shortname( "Caca" );
80     set_category( CAT_VIDEO );
81     set_subcategory( SUBCAT_VIDEO_VOUT );
82     set_description( _("Color ASCII art video output") );
83     set_capability( "video output", 12 );
84     set_callbacks( Create, Destroy );
85 vlc_module_end();
86
87 /*****************************************************************************
88  * vout_sys_t: libcaca video output method descriptor
89  *****************************************************************************
90  * This structure is part of the video output thread descriptor.
91  * It describes the libcaca specific properties of an output thread.
92  *****************************************************************************/
93 struct vout_sys_t
94 {
95     cucul_canvas_t *p_cv;
96     caca_display_t *p_dp;
97     cucul_dither_t *p_dither;
98 };
99
100 /*****************************************************************************
101  * Create: allocates libcaca video output thread
102  *****************************************************************************
103  * This function initializes libcaca vout method.
104  *****************************************************************************/
105 static int Create( vlc_object_t *p_this )
106 {
107     vout_thread_t *p_vout = (vout_thread_t *)p_this;
108
109 #if defined( WIN32 ) && !defined( UNDER_CE )
110     CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
111     SMALL_RECT rect;
112     COORD coord;
113     HANDLE hstdout;
114
115     if( !AllocConsole() )
116     {
117         msg_Err( p_vout, "cannot create console" );
118         return VLC_EGENERIC;
119     }
120
121     hstdout =
122         CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE,
123                                    FILE_SHARE_READ | FILE_SHARE_WRITE,
124                                    NULL, CONSOLE_TEXTMODE_BUFFER, NULL );
125     if( !hstdout || hstdout == INVALID_HANDLE_VALUE )
126     {
127         msg_Err( p_vout, "cannot create screen buffer" );
128         FreeConsole();
129         return VLC_EGENERIC;
130     }
131
132     if( !SetConsoleActiveScreenBuffer( hstdout) )
133     {
134         msg_Err( p_vout, "cannot set active screen buffer" );
135         FreeConsole();
136         return VLC_EGENERIC;
137     }
138
139     coord = GetLargestConsoleWindowSize( hstdout );
140     msg_Dbg( p_vout, "SetConsoleWindowInfo: %ix%i", coord.X, coord.Y );
141
142     /* Force size for now */
143     coord.X = 100;
144     coord.Y = 40;
145
146     if( !SetConsoleScreenBufferSize( hstdout, coord ) )
147         msg_Warn( p_vout, "SetConsoleScreenBufferSize %i %i",
148                   coord.X, coord.Y );
149
150     /* Get the current screen buffer size and window position. */
151     if( GetConsoleScreenBufferInfo( hstdout, &csbiInfo ) )
152     {
153         rect.Top = 0; rect.Left = 0;
154         rect.Right = csbiInfo.dwMaximumWindowSize.X - 1;
155         rect.Bottom = csbiInfo.dwMaximumWindowSize.Y - 1;
156         if( !SetConsoleWindowInfo( hstdout, TRUE, &rect ) )
157             msg_Dbg( p_vout, "SetConsoleWindowInfo failed: %ix%i",
158                      rect.Right, rect.Bottom );
159     }
160 #endif
161
162     /* Allocate structure */
163     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
164     if( p_vout->p_sys == NULL )
165     {
166         msg_Err( p_vout, "out of memory" );
167         return VLC_ENOMEM;
168     }
169
170     p_vout->p_sys->p_cv = cucul_create_canvas(0, 0);
171     if( !p_vout->p_sys->p_cv )
172     {
173         msg_Err( p_vout, "cannot initialize libcucul" );
174         free( p_vout->p_sys );
175         return VLC_EGENERIC;
176     }
177
178     p_vout->p_sys->p_dp = caca_create_display( p_vout->p_sys->p_cv );
179     if( !p_vout->p_sys->p_dp )
180     {
181         msg_Err( p_vout, "cannot initialize libcaca" );
182         cucul_free_canvas( p_vout->p_sys->p_cv );
183         free( p_vout->p_sys );
184         return VLC_EGENERIC;
185     }
186
187     caca_set_display_title( p_vout->p_sys->p_dp,
188                             VOUT_TITLE " - Colour AsCii Art (caca)" );
189
190     p_vout->pf_init = Init;
191     p_vout->pf_end = End;
192     p_vout->pf_manage = Manage;
193     p_vout->pf_render = Render;
194     p_vout->pf_display = Display;
195
196     return VLC_SUCCESS;
197 }
198
199 /*****************************************************************************
200  * Init: initialize libcaca video output thread
201  *****************************************************************************/
202 static int Init( vout_thread_t *p_vout )
203 {
204     int i_index;
205     picture_t *p_pic = NULL;
206
207     I_OUTPUTPICTURES = 0;
208
209     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
210     p_vout->output.i_width = p_vout->render.i_width;
211     p_vout->output.i_height = p_vout->render.i_height;
212     p_vout->output.i_aspect = p_vout->render.i_aspect;
213
214     p_vout->output.i_rmask = 0x00ff0000;
215     p_vout->output.i_gmask = 0x0000ff00;
216     p_vout->output.i_bmask = 0x000000ff;
217
218     /* Create the libcaca dither object */
219     p_vout->p_sys->p_dither = cucul_create_dither
220                        ( 32, p_vout->output.i_width, p_vout->output.i_height,
221                          4 * ((p_vout->output.i_width + 15) & ~15),
222                          p_vout->output.i_rmask, p_vout->output.i_gmask,
223                          p_vout->output.i_bmask, 0x00000000 );
224
225     if( !p_vout->p_sys->p_dither )
226     {
227         msg_Err( p_vout, "could not create libcaca dither object" );
228         return VLC_EGENERIC;
229     }
230
231     /* Find an empty picture slot */
232     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
233     {
234         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
235         {
236             p_pic = p_vout->p_picture + i_index;
237             break;
238         }
239     }
240
241     if( p_pic == NULL )
242     {
243         return VLC_EGENERIC;
244     }
245
246     /* Allocate the picture */
247     p_pic->p->i_lines = p_vout->output.i_height;
248     p_pic->p->i_visible_lines = p_vout->output.i_height;
249     p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
250     p_pic->p->i_pixel_pitch = 4;
251     p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
252     p_pic->i_planes = 1;
253     p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );
254
255     p_pic->i_status = DESTROYED_PICTURE;
256     p_pic->i_type   = DIRECT_PICTURE;
257
258     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
259     I_OUTPUTPICTURES++;
260
261     return VLC_SUCCESS;
262 }
263
264 /*****************************************************************************
265  * End: terminate libcaca video output thread
266  *****************************************************************************/
267 static void End( vout_thread_t *p_vout )
268 {
269     cucul_free_dither( p_vout->p_sys->p_dither );
270 }
271
272 /*****************************************************************************
273  * Destroy: destroy libcaca video output thread
274  *****************************************************************************
275  * Terminate an output method created by AaCreateOutputMethod
276  *****************************************************************************/
277 static void Destroy( vlc_object_t *p_this )
278 {
279     vout_thread_t *p_vout = (vout_thread_t *)p_this;
280
281     caca_free_display( p_vout->p_sys->p_dp );
282     cucul_free_canvas( p_vout->p_sys->p_cv );
283
284 #if defined( WIN32 ) && !defined( UNDER_CE )
285     FreeConsole();
286 #endif
287
288     free( p_vout->p_sys );
289 }
290
291 /*****************************************************************************
292  * Manage: handle libcaca events
293  *****************************************************************************
294  * This function should be called regularly by video output thread. It manages
295  * console events. It returns a non null value on error.
296  *****************************************************************************/
297 static int Manage( vout_thread_t *p_vout )
298 {
299 #ifdef CACA_API_VERSION_1
300     struct caca_event ev;
301 #else
302     int ev;
303 #endif
304
305     while( caca_get_event(p_vout->p_sys->p_dp, CACA_EVENT_ANY, &ev, 0) )
306     {
307         playlist_t *p_playlist;
308         vlc_value_t val;
309
310 #ifdef CACA_API_VERSION_1
311 #ifdef CACA_EVENT_OPAQUE
312         switch( caca_get_event_type( &ev ) )
313 #else
314         switch( ev.type )
315 #endif /* CACA_EVENT_OPAQUE */
316 #else
317         switch( ev )
318 #endif
319         {
320         case CACA_EVENT_KEY_RELEASE:
321 #ifdef CACA_API_VERSION_1
322 #ifdef CACA_EVENT_OPAQUE
323             switch( caca_get_event_key_ch( &ev ) )
324 #else
325             switch( ev.data.key.ch )
326 #endif /* CACA_EVENT_OPAQUE */
327 #else
328             switch( ev & 0x00ffffff )
329 #endif
330             {
331             case 'q':
332                 val.i_int = KEY_MODIFIER_CTRL | 'q';
333                 break;
334             case ' ':
335                 val.i_int = KEY_SPACE;
336                 break;
337             default:
338                 continue;
339             }
340
341             var_Set( p_vout->p_libvlc, "key-pressed", val );
342             break;
343         case CACA_EVENT_RESIZE:
344             /* Acknowledge the resize */
345             caca_refresh_display( p_vout->p_sys->p_dp );
346             break;
347 #ifdef CACA_API_VERSION_1
348         case  CACA_EVENT_MOUSE_MOTION:
349             val.i_int =
350 #ifdef CACA_EVENT_OPAQUE
351                 caca_get_event_mouse_x( &ev )
352 #else
353                 ev.data.mouse.x
354 #endif /* CACA_EVENT_OPAQUE */
355                 * p_vout->render.i_width
356                          / cucul_get_canvas_width( p_vout->p_sys->p_cv );
357             var_Set( p_vout, "mouse-x", val );
358             val.i_int =
359 #ifdef CACA_EVENT_OPAQUE
360                 caca_get_event_mouse_y( &ev ) 
361 #else
362                 ev.data.mouse.y
363 #endif /* CACA_EVENT_OPAQUE */
364                 * p_vout->render.i_height
365                          / cucul_get_canvas_height( p_vout->p_sys->p_cv );
366             var_Set( p_vout, "mouse-y", val );
367             val.b_bool = true;
368             var_Set( p_vout, "mouse-moved", val );
369             break;
370         case CACA_EVENT_MOUSE_RELEASE:
371             val.b_bool = true;
372             var_Set( p_vout, "mouse-clicked", val );
373             break;
374         case CACA_EVENT_QUIT:
375         {
376             p_playlist = vlc_object_find( p_vout,
377                                           VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
378             if( p_playlist )
379             {
380                 playlist_Stop( p_playlist );
381                 vlc_object_release( p_playlist );
382             }
383             vlc_object_kill( p_vout->p_libvlc );
384             break;
385         }
386 #endif
387         default:
388             break;
389         }
390     }
391
392     return VLC_SUCCESS;
393 }
394
395 /*****************************************************************************
396  * Render: render previously calculated output
397  *****************************************************************************/
398 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
399 {
400     cucul_set_color( p_vout->p_sys->p_cv,
401                      CUCUL_COLOR_DEFAULT, CUCUL_COLOR_BLACK );
402     cucul_clear_canvas( p_vout->p_sys->p_cv );
403     cucul_dither_bitmap( p_vout->p_sys->p_cv, 0, 0,
404                          cucul_get_canvas_width( p_vout->p_sys->p_cv ) - 1,
405                          cucul_get_canvas_height( p_vout->p_sys->p_cv ) - 1,
406                          p_vout->p_sys->p_dither, p_pic->p->p_pixels );
407 }
408
409 /*****************************************************************************
410  * Display: displays previously rendered output
411  *****************************************************************************/
412 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
413 {
414     VLC_UNUSED(p_pic);
415     caca_refresh_display( p_vout->p_sys->p_dp );
416 }
417