caca.c 12.1 KB
Newer Older
1
2
3
/*****************************************************************************
 * caca.c: Color ASCII Art video output plugin using libcaca
 *****************************************************************************
4
 * Copyright (C) 2003, 2004 the VideoLAN team
5
 * $Id$
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * Authors: Sam Hocevar <sam@zoy.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <string.h>                                            /* strerror() */

#include <caca.h>

#include <vlc/vlc.h>
#include <vlc/vout.h>
#include <vlc/intf.h>
#include <vlc_keys.h>

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Create    ( vlc_object_t * );
static void Destroy   ( vlc_object_t * );

static int  Init      ( vout_thread_t * );
static void End       ( vout_thread_t * );
static int  Manage    ( vout_thread_t * );
static void Render    ( vout_thread_t *, picture_t * );
static void Display   ( vout_thread_t *, picture_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
54
    set_shortname( "Caca" );
zorglub's avatar
zorglub committed
55
56
    set_category( CAT_VIDEO );
    set_subcategory( SUBCAT_VIDEO_VOUT );
57
    set_description( _("Color ASCII art video output") );
58
59
60
61
    set_capability( "video output", 12 );
    set_callbacks( Create, Destroy );
vlc_module_end();

Sam Hocevar's avatar
Sam Hocevar committed
62
63
64
65
66
67
68
69
/*****************************************************************************
 * vout_sys_t: libcaca video output method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the libcaca specific properties of an output thread.
 *****************************************************************************/
struct vout_sys_t
{
70
71
72
73
74
#ifdef CACA_API_VERSION_1
    struct cucul_context *p_qq;
    struct caca_context *p_kk;
    struct cucul_bitmap *p_bitmap;
#else
Sam Hocevar's avatar
Sam Hocevar committed
75
    struct caca_bitmap *p_bitmap;
76
#endif
Sam Hocevar's avatar
Sam Hocevar committed
77
78
};

79
80
81
82
83
84
85
86
/*****************************************************************************
 * Create: allocates libcaca video output thread
 *****************************************************************************
 * This function initializes libcaca vout method.
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
Sam Hocevar's avatar
Sam Hocevar committed
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#if defined( WIN32 ) && !defined( UNDER_CE )
    if( AllocConsole() )
    {
        CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
        SMALL_RECT rect;
        COORD coord;

        HANDLE hstdout =
            CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE,
                                       FILE_SHARE_READ | FILE_SHARE_WRITE,
                                       NULL, CONSOLE_TEXTMODE_BUFFER, NULL );
        if( !hstdout || hstdout == INVALID_HANDLE_VALUE )
        {
            msg_Err( p_vout, "cannot create screen buffer" );
            FreeConsole();
            return VLC_EGENERIC;
        }

zorglub's avatar
zorglub committed
106
        if( !SetConsoleActiveScreenBuffer( hstdout) )
107
108
109
110
111
112
113
        {
            msg_Err( p_vout, "cannot set active screen buffer" );
            FreeConsole();
            return VLC_EGENERIC;
        }

        coord = GetLargestConsoleWindowSize( hstdout );
zorglub's avatar
zorglub committed
114
        msg_Dbg( p_vout, "SetConsoleWindowInfo: %ix%i", coord.X, coord.Y );
115
116
117
118
119
120
121

        /* Force size for now */
        coord.X = 100;
        coord.Y = 40;

        if( !SetConsoleScreenBufferSize( hstdout, coord ) )
            msg_Warn( p_vout, "SetConsoleScreenBufferSize %i %i",
zorglub's avatar
zorglub committed
122
                      coord.X, coord.Y );
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

        /* Get the current screen buffer size and window position. */
        if( GetConsoleScreenBufferInfo( hstdout, &csbiInfo ) )
        {
            rect.Top = 0; rect.Left = 0;
            rect.Right = csbiInfo.dwMaximumWindowSize.X - 1;
            rect.Bottom = csbiInfo.dwMaximumWindowSize.Y - 1;
            if( !SetConsoleWindowInfo( hstdout, TRUE, &rect ) )
                msg_Dbg( p_vout, "SetConsoleWindowInfo failed: %ix%i",
                         rect.Right, rect.Bottom );
        }
    }
    else
    {
        msg_Err( p_vout, "cannot create console" );
        return VLC_EGENERIC;
    }

#endif

Sam Hocevar's avatar
Sam Hocevar committed
143
144
145
146
147
148
149
    /* Allocate structure */
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
    {
        msg_Err( p_vout, "out of memory" );
        return VLC_ENOMEM;
    }
150

151
#ifdef CACA_API_VERSION_1
152
    p_vout->p_sys->p_qq = cucul_init(0, 0);
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    if( !p_vout->p_sys->p_qq )
    {
        msg_Err( p_vout, "cannot initialize libcucul" );
        free( p_vout->p_sys );
        return VLC_EGENERIC;
    }

    p_vout->p_sys->p_kk = caca_attach( p_vout->p_sys->p_qq );
    if( !p_vout->p_sys->p_kk )
    {
        msg_Err( p_vout, "cannot initialize libcaca" );
        cucul_end( p_vout->p_sys->p_qq );
        free( p_vout->p_sys );
        return VLC_EGENERIC;
    }

    caca_set_window_title( p_vout->p_sys->p_kk,
                           VOUT_TITLE " - Colour AsCii Art (caca)" );
#else
172
173
174
    if( caca_init() )
    {
        msg_Err( p_vout, "cannot initialize libcaca" );
Sam Hocevar's avatar
Sam Hocevar committed
175
        free( p_vout->p_sys );
176
177
178
        return VLC_EGENERIC;
    }

179
    caca_set_window_title( VOUT_TITLE " - Colour AsCii Art (caca)" );
180
#endif
181

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
    p_vout->pf_init = Init;
    p_vout->pf_end = End;
    p_vout->pf_manage = Manage;
    p_vout->pf_render = Render;
    p_vout->pf_display = Display;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Init: initialize libcaca video output thread
 *****************************************************************************/
static int Init( vout_thread_t *p_vout )
{
    int i_index;
    picture_t *p_pic = NULL;

    I_OUTPUTPICTURES = 0;

Sam Hocevar's avatar
Sam Hocevar committed
201
    p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
202
203
204
205
206
207
208
209
    p_vout->output.i_width = p_vout->render.i_width;
    p_vout->output.i_height = p_vout->render.i_height;
    p_vout->output.i_aspect = p_vout->render.i_aspect;

    p_vout->output.i_rmask = 0x00ff0000;
    p_vout->output.i_gmask = 0x0000ff00;
    p_vout->output.i_bmask = 0x000000ff;

Sam Hocevar's avatar
Sam Hocevar committed
210
211
    /* Create the libcaca bitmap */
    p_vout->p_sys->p_bitmap =
212
#ifdef CACA_API_VERSION_1
213
        cucul_create_bitmap
214
#else
215
        caca_create_bitmap
216
#endif
217
218
219
220
221
                       ( 32, p_vout->output.i_width, p_vout->output.i_height,
                         4 * ((p_vout->output.i_width + 15) & ~15),
                         p_vout->output.i_rmask, p_vout->output.i_gmask,
                         p_vout->output.i_bmask, 0x00000000 );

Sam Hocevar's avatar
Sam Hocevar committed
222
223
224
225
226
227
    if( !p_vout->p_sys->p_bitmap )
    {
        msg_Err( p_vout, "could not create libcaca bitmap" );
        return VLC_EGENERIC;
    }

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
    /* Find an empty picture slot */
    for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
    {
        if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
        {
            p_pic = p_vout->p_picture + i_index;
            break;
        }
    }

    if( p_pic == NULL )
    {
        return VLC_EGENERIC;
    }

    /* Allocate the picture */
    p_pic->p->i_lines = p_vout->output.i_height;
245
    p_pic->p->i_visible_lines = p_vout->output.i_height;
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
    p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
    p_pic->p->i_pixel_pitch = 4;
    p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
    p_pic->i_planes = 1;
    p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );

    p_pic->i_status = DESTROYED_PICTURE;
    p_pic->i_type   = DIRECT_PICTURE;

    PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
    I_OUTPUTPICTURES++;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * End: terminate libcaca video output thread
 *****************************************************************************/
static void End( vout_thread_t *p_vout )
{
266
#ifdef CACA_API_VERSION_1
267
    cucul_free_bitmap( p_vout->p_sys->p_bitmap );
268
#else
Christophe Mutricy's avatar
Christophe Mutricy committed
269
    caca_free_bitmap( p_vout->p_sys->p_bitmap );
270
#endif
271
272
273
274
275
276
277
278
279
}

/*****************************************************************************
 * Destroy: destroy libcaca video output thread
 *****************************************************************************
 * Terminate an output method created by AaCreateOutputMethod
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
Sam Hocevar's avatar
Sam Hocevar committed
280
281
    vout_thread_t *p_vout = (vout_thread_t *)p_this;

282
283
284
285
#ifdef CACA_API_VERSION_1
    caca_detach( p_vout->p_sys->p_kk );
    cucul_end( p_vout->p_sys->p_qq );
#else
286
    caca_end();
287
#endif
288
289
290
291
292

#if defined( WIN32 ) && !defined( UNDER_CE )
    FreeConsole();
#endif

Sam Hocevar's avatar
Sam Hocevar committed
293
    free( p_vout->p_sys );
294
295
296
297
298
299
300
301
302
303
}

/*****************************************************************************
 * Manage: handle libcaca events
 *****************************************************************************
 * This function should be called regularly by video output thread. It manages
 * console events. It returns a non null value on error.
 *****************************************************************************/
static int Manage( vout_thread_t *p_vout )
{
Sam Hocevar's avatar
Sam Hocevar committed
304
    int event;
305
306
    vlc_value_t val;

307
308
309
310
#ifdef CACA_API_VERSION_1
    while(( event = caca_get_event(p_vout->p_sys->p_kk,
                                   CACA_EVENT_KEY_PRESS | CACA_EVENT_RESIZE) ))
#else
311
    while(( event = caca_get_event(CACA_EVENT_KEY_PRESS | CACA_EVENT_RESIZE) ))
312
#endif
313
    {
314
315
316
        if( event == CACA_EVENT_RESIZE )
        {
            /* Acknowledge the resize */
317
318
319
#ifdef CACA_API_VERSION_1
            caca_display( p_vout->p_sys->p_kk );
#else
320
            caca_refresh();
321
#endif
322
323
324
            continue;
        }

325
        switch( event & 0x00ffffff )
326
        {
327
328
329
330
331
332
333
334
        case 'q':
            val.i_int = KEY_MODIFIER_CTRL | 'q';
            break;
        case ' ':
            val.i_int = KEY_SPACE;
            break;
        default:
            continue;
335
        }
336
337

        var_Set( p_vout->p_vlc, "key-pressed", val );
338
339
340
341
342
343
344
345
346
347
    }

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Render: render previously calculated output
 *****************************************************************************/
static void Render( vout_thread_t *p_vout, picture_t *p_pic )
{
348
349
350
351
352
353
354
355
#ifdef CACA_API_VERSION_1
    cucul_clear( p_vout->p_sys->p_qq );
    cucul_draw_bitmap( p_vout->p_sys->p_qq,
                       0, 0,
                       cucul_get_width( p_vout->p_sys->p_qq ) - 1,
                       cucul_get_height( p_vout->p_sys->p_qq ) - 1,
                       p_vout->p_sys->p_bitmap, p_pic->p->p_pixels );
#else
356
    caca_clear();
357
358
    caca_draw_bitmap( 0, 0, caca_get_width() - 1, caca_get_height() - 1,
                      p_vout->p_sys->p_bitmap, p_pic->p->p_pixels );
359
#endif
360
361
362
363
364
365
366
}

/*****************************************************************************
 * Display: displays previously rendered output
 *****************************************************************************/
static void Display( vout_thread_t *p_vout, picture_t *p_pic )
{
367
368
369
#ifdef CACA_API_VERSION_1
    caca_display( p_vout->p_sys->p_kk );
#else
370
    caca_refresh();
371
#endif
372
373
}