fix #1387 (update to new libcaca api)
[vlc.git] / 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 #include <vlc/vlc.h>
29 #include <vlc_vout.h>
30 #include <vlc_interface.h>
31 #include <vlc_playlist.h>
32 #include <vlc_keys.h>
33
34 #include <caca.h>
35
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)
57 #endif
58
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 static int  Create    ( vlc_object_t * );
63 static void Destroy   ( vlc_object_t * );
64
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 * );
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74 vlc_module_begin();
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 );
81 vlc_module_end();
82
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  *****************************************************************************/
89 struct vout_sys_t
90 {
91     cucul_canvas_t *p_cv;
92     caca_display_t *p_dp;
93     cucul_dither_t *p_dither;
94 };
95
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 )
102 {
103     vout_thread_t *p_vout = (vout_thread_t *)p_this;
104
105 #if defined( WIN32 ) && !defined( UNDER_CE )
106     CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
107     SMALL_RECT rect;
108     COORD coord;
109     HANDLE hstdout;
110
111     if( !AllocConsole() )
112     {
113         msg_Err( p_vout, "cannot create console" );
114         return VLC_EGENERIC;
115     }
116
117     hstdout =
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 )
122     {
123         msg_Err( p_vout, "cannot create screen buffer" );
124         FreeConsole();
125         return VLC_EGENERIC;
126     }
127
128     if( !SetConsoleActiveScreenBuffer( hstdout) )
129     {
130         msg_Err( p_vout, "cannot set active screen buffer" );
131         FreeConsole();
132         return VLC_EGENERIC;
133     }
134
135     coord = GetLargestConsoleWindowSize( hstdout );
136     msg_Dbg( p_vout, "SetConsoleWindowInfo: %ix%i", coord.X, coord.Y );
137
138     /* Force size for now */
139     coord.X = 100;
140     coord.Y = 40;
141
142     if( !SetConsoleScreenBufferSize( hstdout, coord ) )
143         msg_Warn( p_vout, "SetConsoleScreenBufferSize %i %i",
144                   coord.X, coord.Y );
145
146     /* Get the current screen buffer size and window position. */
147     if( GetConsoleScreenBufferInfo( hstdout, &csbiInfo ) )
148     {
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 );
155     }
156 #endif
157
158     /* Allocate structure */
159     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
160     if( p_vout->p_sys == NULL )
161     {
162         msg_Err( p_vout, "out of memory" );
163         return VLC_ENOMEM;
164     }
165
166     p_vout->p_sys->p_cv = cucul_create_canvas(0, 0);
167     if( !p_vout->p_sys->p_cv )
168     {
169         msg_Err( p_vout, "cannot initialize libcucul" );
170         free( p_vout->p_sys );
171         return VLC_EGENERIC;
172     }
173
174     p_vout->p_sys->p_dp = caca_create_display( p_vout->p_sys->p_cv );
175     if( !p_vout->p_sys->p_dp )
176     {
177         msg_Err( p_vout, "cannot initialize libcaca" );
178         cucul_free_canvas( p_vout->p_sys->p_cv );
179         free( p_vout->p_sys );
180         return VLC_EGENERIC;
181     }
182
183     caca_set_display_title( p_vout->p_sys->p_dp,
184                             VOUT_TITLE " - Colour AsCii Art (caca)" );
185
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;
191
192     return VLC_SUCCESS;
193 }
194
195 /*****************************************************************************
196  * Init: initialize libcaca video output thread
197  *****************************************************************************/
198 static int Init( vout_thread_t *p_vout )
199 {
200     int i_index;
201     picture_t *p_pic = NULL;
202
203     I_OUTPUTPICTURES = 0;
204
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;
209
210     p_vout->output.i_rmask = 0x00ff0000;
211     p_vout->output.i_gmask = 0x0000ff00;
212     p_vout->output.i_bmask = 0x000000ff;
213
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 );
220
221     if( !p_vout->p_sys->p_dither )
222     {
223         msg_Err( p_vout, "could not create libcaca dither object" );
224         return VLC_EGENERIC;
225     }
226
227     /* Find an empty picture slot */
228     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
229     {
230         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
231         {
232             p_pic = p_vout->p_picture + i_index;
233             break;
234         }
235     }
236
237     if( p_pic == NULL )
238     {
239         return VLC_EGENERIC;
240     }
241
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;
248     p_pic->i_planes = 1;
249     p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );
250
251     p_pic->i_status = DESTROYED_PICTURE;
252     p_pic->i_type   = DIRECT_PICTURE;
253
254     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
255     I_OUTPUTPICTURES++;
256
257     return VLC_SUCCESS;
258 }
259
260 /*****************************************************************************
261  * End: terminate libcaca video output thread
262  *****************************************************************************/
263 static void End( vout_thread_t *p_vout )
264 {
265     cucul_free_dither( p_vout->p_sys->p_dither );
266 }
267
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 )
274 {
275     vout_thread_t *p_vout = (vout_thread_t *)p_this;
276
277     caca_free_display( p_vout->p_sys->p_dp );
278     cucul_free_canvas( p_vout->p_sys->p_cv );
279
280 #if defined( WIN32 ) && !defined( UNDER_CE )
281     FreeConsole();
282 #endif
283
284     free( p_vout->p_sys );
285 }
286
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 )
294 {
295 #ifdef CACA_API_VERSION_1
296     struct caca_event ev;
297 #else
298     int ev;
299 #endif
300
301     while( caca_get_event(p_vout->p_sys->p_dp, CACA_EVENT_ANY, &ev, 0) )
302     {
303         playlist_t *p_playlist;
304         vlc_value_t val;
305
306 #ifdef CACA_API_VERSION_1
307 #ifdef CACA_EVENT_OPAQUE
308         switch( caca_get_event_type( &ev ) )
309 #else
310         switch( ev.type )
311 #endif /* CACA_EVENT_OPAQUE */
312 #else
313         switch( ev )
314 #endif
315         {
316         case CACA_EVENT_KEY_RELEASE:
317 #ifdef CACA_API_VERSION_1
318 #ifdef CACA_EVENT_OPAQUE
319             switch( caca_get_event_key_ch( &ev ) )
320 #else
321             switch( ev.data.key.ch )
322 #endif /* CACA_EVENT_OPAQUE */
323 #else
324             switch( ev & 0x00ffffff )
325 #endif
326             {
327             case 'q':
328                 val.i_int = KEY_MODIFIER_CTRL | 'q';
329                 break;
330             case ' ':
331                 val.i_int = KEY_SPACE;
332                 break;
333             default:
334                 continue;
335             }
336
337             var_Set( p_vout->p_libvlc, "key-pressed", val );
338             break;
339         case CACA_EVENT_RESIZE:
340             /* Acknowledge the resize */
341             caca_refresh_display( p_vout->p_sys->p_dp );
342             break;
343 #ifdef CACA_API_VERSION_1
344         case  CACA_EVENT_MOUSE_MOTION:
345             val.i_int =
346 #ifdef CACA_EVENT_OPAQUE
347                 caca_get_event_mouse_x( &ev )
348 #else
349                 ev.data.mouse.x
350 #endif /* CACA_EVENT_OPAQUE */
351                 * p_vout->render.i_width
352                          / cucul_get_canvas_width( p_vout->p_sys->p_cv );
353             var_Set( p_vout, "mouse-x", val );
354             val.i_int =
355 #ifdef CACA_EVENT_OPAQUE
356                 caca_get_event_mouse_y( &ev ) 
357 #else
358                 ev.data.mouse.y
359 #endif /* CACA_EVENT_OPAQUE */
360                 * p_vout->render.i_height
361                          / cucul_get_canvas_height( p_vout->p_sys->p_cv );
362             var_Set( p_vout, "mouse-y", val );
363             val.b_bool = VLC_TRUE;
364             var_Set( p_vout, "mouse-moved", val );
365             break;
366         case CACA_EVENT_MOUSE_RELEASE:
367             val.b_bool = VLC_TRUE;
368             var_Set( p_vout, "mouse-clicked", val );
369             break;
370         case CACA_EVENT_QUIT:
371         {
372             p_playlist = vlc_object_find( p_vout,
373                                           VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
374             if( p_playlist )
375             {
376                 playlist_Stop( p_playlist );
377                 vlc_object_release( p_playlist );
378             }
379             vlc_object_kill( p_vout->p_libvlc );
380             break;
381         }
382 #endif
383         default:
384             break;
385         }
386     }
387
388     return VLC_SUCCESS;
389 }
390
391 /*****************************************************************************
392  * Render: render previously calculated output
393  *****************************************************************************/
394 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
395 {
396     cucul_set_color( p_vout->p_sys->p_cv,
397                      CUCUL_COLOR_DEFAULT, CUCUL_COLOR_BLACK );
398     cucul_clear_canvas( p_vout->p_sys->p_cv );
399     cucul_dither_bitmap( p_vout->p_sys->p_cv, 0, 0,
400                          cucul_get_canvas_width( p_vout->p_sys->p_cv ) - 1,
401                          cucul_get_canvas_height( p_vout->p_sys->p_cv ) - 1,
402                          p_vout->p_sys->p_dither, p_pic->p->p_pixels );
403 }
404
405 /*****************************************************************************
406  * Display: displays previously rendered output
407  *****************************************************************************/
408 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
409 {
410     caca_refresh_display( p_vout->p_sys->p_dp );
411 }
412