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