]> git.sesse.net Git - vlc/blob - modules/video_output/caca.c
* backport of [11257] to trunk
[vlc] / modules / video_output / caca.c
1 /*****************************************************************************
2  * caca.c: Color ASCII Art video output plugin using libcaca
3  *****************************************************************************
4  * Copyright (C) 2003, 2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 #include <vlc/vlc.h>
34 #include <vlc/vout.h>
35 #include <vlc/intf.h>
36 #include <vlc_keys.h>
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static int  Create    ( vlc_object_t * );
42 static void Destroy   ( vlc_object_t * );
43
44 static int  Init      ( vout_thread_t * );
45 static void End       ( vout_thread_t * );
46 static int  Manage    ( vout_thread_t * );
47 static void Render    ( vout_thread_t *, picture_t * );
48 static void Display   ( vout_thread_t *, picture_t * );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 vlc_module_begin();
54     set_shortname( "Caca" );
55     set_category( CAT_VIDEO );
56     set_subcategory( SUBCAT_VIDEO_VOUT );
57     set_description( _("Color ASCII art video output") );
58     set_capability( "video output", 12 );
59     set_callbacks( Create, Destroy );
60 vlc_module_end();
61
62 /*****************************************************************************
63  * vout_sys_t: libcaca video output method descriptor
64  *****************************************************************************
65  * This structure is part of the video output thread descriptor.
66  * It describes the libcaca specific properties of an output thread.
67  *****************************************************************************/
68 struct vout_sys_t
69 {
70     struct caca_bitmap *p_bitmap;
71 };
72
73 /*****************************************************************************
74  * Create: allocates libcaca video output thread
75  *****************************************************************************
76  * This function initializes libcaca vout method.
77  *****************************************************************************/
78 static int Create( vlc_object_t *p_this )
79 {
80     vout_thread_t *p_vout = (vout_thread_t *)p_this;
81
82 #if defined( WIN32 ) && !defined( UNDER_CE )
83     if( AllocConsole() )
84     {
85         CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
86         SMALL_RECT rect;
87         COORD coord;
88
89         HANDLE hstdout =
90             CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE,
91                                        FILE_SHARE_READ | FILE_SHARE_WRITE,
92                                        NULL, CONSOLE_TEXTMODE_BUFFER, NULL );
93         if( !hstdout || hstdout == INVALID_HANDLE_VALUE )
94         {
95             msg_Err( p_vout, "cannot create screen buffer" );
96             FreeConsole();
97             return VLC_EGENERIC;
98         }
99
100         if( !SetConsoleActiveScreenBuffer( hstdout) )
101         {
102             msg_Err( p_vout, "cannot set active screen buffer" );
103             FreeConsole();
104             return VLC_EGENERIC;
105         }
106
107         coord = GetLargestConsoleWindowSize( hstdout );
108         msg_Dbg( p_vout, "SetConsoleWindowInfo: %ix%i", coord.X, coord.Y );
109
110         /* Force size for now */
111         coord.X = 100;
112         coord.Y = 40;
113
114         if( !SetConsoleScreenBufferSize( hstdout, coord ) )
115             msg_Warn( p_vout, "SetConsoleScreenBufferSize %i %i",
116                       coord.X, coord.Y );
117
118         /* Get the current screen buffer size and window position. */
119         if( GetConsoleScreenBufferInfo( hstdout, &csbiInfo ) )
120         {
121             rect.Top = 0; rect.Left = 0;
122             rect.Right = csbiInfo.dwMaximumWindowSize.X - 1;
123             rect.Bottom = csbiInfo.dwMaximumWindowSize.Y - 1;
124             if( !SetConsoleWindowInfo( hstdout, TRUE, &rect ) )
125                 msg_Dbg( p_vout, "SetConsoleWindowInfo failed: %ix%i",
126                          rect.Right, rect.Bottom );
127         }
128     }
129     else
130     {
131         msg_Err( p_vout, "cannot create console" );
132         return VLC_EGENERIC;
133     }
134
135 #endif
136
137     /* Allocate structure */
138     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
139     if( p_vout->p_sys == NULL )
140     {
141         msg_Err( p_vout, "out of memory" );
142         return VLC_ENOMEM;
143     }
144
145     if( caca_init() )
146     {
147         msg_Err( p_vout, "cannot initialize libcaca" );
148         free( p_vout->p_sys );
149         return VLC_EGENERIC;
150     }
151
152     caca_set_window_title( VOUT_TITLE " - Colour AsCii Art (caca)" );
153
154     p_vout->pf_init = Init;
155     p_vout->pf_end = End;
156     p_vout->pf_manage = Manage;
157     p_vout->pf_render = Render;
158     p_vout->pf_display = Display;
159
160     return VLC_SUCCESS;
161 }
162
163 /*****************************************************************************
164  * Init: initialize libcaca video output thread
165  *****************************************************************************/
166 static int Init( vout_thread_t *p_vout )
167 {
168     int i_index;
169     picture_t *p_pic = NULL;
170
171     I_OUTPUTPICTURES = 0;
172
173     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
174     p_vout->output.i_width = p_vout->render.i_width;
175     p_vout->output.i_height = p_vout->render.i_height;
176     p_vout->output.i_aspect = p_vout->render.i_aspect;
177
178     p_vout->output.i_rmask = 0x00ff0000;
179     p_vout->output.i_gmask = 0x0000ff00;
180     p_vout->output.i_bmask = 0x000000ff;
181
182     /* Create the libcaca bitmap */
183     p_vout->p_sys->p_bitmap =
184         caca_create_bitmap( 32,
185                             p_vout->output.i_width,
186                             p_vout->output.i_height,
187                             4 * ((p_vout->output.i_width + 15) & ~15),
188                             p_vout->output.i_rmask,
189                             p_vout->output.i_gmask,
190                             p_vout->output.i_bmask,
191                             0x00000000 );
192     if( !p_vout->p_sys->p_bitmap )
193     {
194         msg_Err( p_vout, "could not create libcaca bitmap" );
195         return VLC_EGENERIC;
196     }
197
198     /* Find an empty picture slot */
199     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
200     {
201         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
202         {
203             p_pic = p_vout->p_picture + i_index;
204             break;
205         }
206     }
207
208     if( p_pic == NULL )
209     {
210         return VLC_EGENERIC;
211     }
212
213     /* Allocate the picture */
214     p_pic->p->i_lines = p_vout->output.i_height;
215     p_pic->p->i_visible_lines = p_vout->output.i_height;
216     p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
217     p_pic->p->i_pixel_pitch = 4;
218     p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
219     p_pic->i_planes = 1;
220     p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );
221
222     p_pic->i_status = DESTROYED_PICTURE;
223     p_pic->i_type   = DIRECT_PICTURE;
224
225     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
226     I_OUTPUTPICTURES++;
227
228     return VLC_SUCCESS;
229 }
230
231 /*****************************************************************************
232  * End: terminate libcaca video output thread
233  *****************************************************************************/
234 static void End( vout_thread_t *p_vout )
235 {
236     caca_free_bitmap( p_vout->p_sys->p_bitmap );
237 }
238
239 /*****************************************************************************
240  * Destroy: destroy libcaca video output thread
241  *****************************************************************************
242  * Terminate an output method created by AaCreateOutputMethod
243  *****************************************************************************/
244 static void Destroy( vlc_object_t *p_this )
245 {
246     vout_thread_t *p_vout = (vout_thread_t *)p_this;
247
248     caca_end();
249
250 #if defined( WIN32 ) && !defined( UNDER_CE )
251     FreeConsole();
252 #endif
253
254     free( p_vout->p_sys );
255 }
256
257 /*****************************************************************************
258  * Manage: handle libcaca events
259  *****************************************************************************
260  * This function should be called regularly by video output thread. It manages
261  * console events. It returns a non null value on error.
262  *****************************************************************************/
263 static int Manage( vout_thread_t *p_vout )
264 {
265     int event;
266     vlc_value_t val;
267
268     while(( event = caca_get_event(CACA_EVENT_KEY_PRESS | CACA_EVENT_RESIZE) ))
269     {
270         if( event == CACA_EVENT_RESIZE )
271         {
272             /* Acknowledge the resize */
273             caca_refresh();
274             continue;
275         }
276
277         switch( event & 0x00ffffff )
278         {
279         case 'q':
280             val.i_int = KEY_MODIFIER_CTRL | 'q';
281             break;
282         case ' ':
283             val.i_int = KEY_SPACE;
284             break;
285         default:
286             continue;
287         }
288
289         var_Set( p_vout->p_vlc, "key-pressed", val );
290     }
291
292     return VLC_SUCCESS;
293 }
294
295 /*****************************************************************************
296  * Render: render previously calculated output
297  *****************************************************************************/
298 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
299 {
300     caca_clear();
301     caca_draw_bitmap( 0, 0, caca_get_width() - 1, caca_get_height() - 1,
302                       p_vout->p_sys->p_bitmap, p_pic->p->p_pixels );
303 }
304
305 /*****************************************************************************
306  * Display: displays previously rendered output
307  *****************************************************************************/
308 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
309 {
310     caca_refresh();
311 }
312