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