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