aa.c 9.5 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1 2 3 4
/*****************************************************************************
 * vout_aa.c: Aa video output display method for testing purposes
 *****************************************************************************
 * Copyright (C) 2002 VideoLAN
5
 * $Id: aa.c,v 1.9 2002/07/31 20:56:50 sam Exp $
Sam Hocevar's avatar
 
Sam Hocevar committed
6 7 8 9 10 11 12
 *
 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
 *
 * 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.
Sam Hocevar's avatar
 
Sam Hocevar committed
13
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

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

#include <aalib.h>

33 34 35
#include <vlc/vlc.h>
#include <vlc/vout.h>
#include <vlc/intf.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
36 37

/*****************************************************************************
38
 * Local prototypes
Sam Hocevar's avatar
 
Sam Hocevar committed
39
 *****************************************************************************/
40 41
static int  Create    ( vlc_object_t * );
static void Destroy   ( vlc_object_t * );
Sam Hocevar's avatar
 
Sam Hocevar committed
42

43 44 45 46 47
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 * );
Sam Hocevar's avatar
 
Sam Hocevar committed
48

49
static void SetPalette     ( vout_thread_t *, u16 *, u16 *, u16 * );
Sam Hocevar's avatar
 
Sam Hocevar committed
50

51 52 53 54 55 56 57 58 59
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
    set_description( _("ASCII-art video output module") );
    set_capability( "video output", 10 );
    add_shortcut( "aalib" );
    set_callbacks( Create, Destroy );
vlc_module_end();
Sam Hocevar's avatar
 
Sam Hocevar committed
60 61 62 63 64 65 66

/*****************************************************************************
 * vout_sys_t: aa video output method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the aa specific properties of an output thread.
 *****************************************************************************/
67
struct vout_sys_t
Sam Hocevar's avatar
 
Sam Hocevar committed
68 69 70 71 72
{
    struct aa_context*  aa_context;
    aa_palette          palette;
    int                 i_width;                     /* width of main window */
    int                 i_height;                   /* height of main window */
73
};
Sam Hocevar's avatar
 
Sam Hocevar committed
74 75

/*****************************************************************************
76
 * Create: allocates aa video thread output method
Sam Hocevar's avatar
 
Sam Hocevar committed
77 78 79
 *****************************************************************************
 * This function allocates and initializes a aa vout method.
 *****************************************************************************/
80
static int Create( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
81
{
82 83
    vout_thread_t *p_vout = (vout_thread_t *)p_this;

Sam Hocevar's avatar
 
Sam Hocevar committed
84 85 86 87
    /* Allocate structure */
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
    {
88
        msg_Err( p_vout, "out of memory" );
Sam Hocevar's avatar
 
Sam Hocevar committed
89 90 91
        return( 1 );
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
92 93 94 95 96
    /* Don't parse any options, but take $AAOPTS into account */
    aa_parseoptions( NULL, NULL, NULL, NULL );

    if (!(p_vout->p_sys->aa_context = aa_autoinit(&aa_defparams)))
    {
97
        msg_Err( p_vout, "cannot initialize aalib" );
Sam Hocevar's avatar
 
Sam Hocevar committed
98
        return( 1 );
Sam Hocevar's avatar
 
Sam Hocevar committed
99
    }
100

101 102 103 104 105 106
    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;

Sam Hocevar's avatar
 
Sam Hocevar committed
107 108
    p_vout->p_sys->i_width = aa_imgwidth(p_vout->p_sys->aa_context);
    p_vout->p_sys->i_height = aa_imgheight(p_vout->p_sys->aa_context);
109 110 111
    aa_autoinitkbd( p_vout->p_sys->aa_context, 0 );
    aa_autoinitmouse( p_vout->p_sys->aa_context, AA_MOUSEPRESSMASK );
    aa_hidemouse( p_vout->p_sys->aa_context );
Sam Hocevar's avatar
 
Sam Hocevar committed
112 113 114 115
    return( 0 );
}

/*****************************************************************************
116
 * Init: initialize aa video thread output method
Sam Hocevar's avatar
 
Sam Hocevar committed
117
 *****************************************************************************/
118
static int Init( vout_thread_t *p_vout )
Sam Hocevar's avatar
 
Sam Hocevar committed
119 120
{
    int i_index;
Sam Hocevar's avatar
 
Sam Hocevar committed
121 122
    picture_t *p_pic = NULL;

Sam Hocevar's avatar
 
Sam Hocevar committed
123 124
    I_OUTPUTPICTURES = 0;

125
    p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
Sam Hocevar's avatar
 
Sam Hocevar committed
126 127 128 129 130 131
    p_vout->output.i_width = p_vout->p_sys->i_width;
    p_vout->output.i_height = p_vout->p_sys->i_height;
    p_vout->output.i_aspect = p_vout->p_sys->i_width
                               * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
    p_vout->output.pf_setpalette = SetPalette;

Sam Hocevar's avatar
 
Sam Hocevar committed
132 133
    /* Find an empty picture slot */
    for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
134
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
135
        if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
Sam Hocevar's avatar
 
Sam Hocevar committed
136
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
137
            p_pic = p_vout->p_picture + i_index;
Sam Hocevar's avatar
 
Sam Hocevar committed
138 139
            break;
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
140
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
141

Sam Hocevar's avatar
 
Sam Hocevar committed
142 143 144
    if( p_pic == NULL )
    {
        return -1;
Sam Hocevar's avatar
 
Sam Hocevar committed
145 146
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
147 148 149 150
    /* Allocate the picture */
    p_pic->p->p_pixels = aa_image( p_vout->p_sys->aa_context );
    p_pic->p->i_lines = p_vout->p_sys->i_height;
    p_pic->p->i_pitch = p_vout->p_sys->i_width;
151 152
    p_pic->p->i_pixel_pitch = 1;
    p_pic->p->i_visible_pitch = p_vout->p_sys->i_width;
Sam Hocevar's avatar
 
Sam Hocevar committed
153 154 155 156 157 158 159 160 161
    p_pic->i_planes = 1;

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

    PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
    I_OUTPUTPICTURES++;

    return 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
162 163 164
}

/*****************************************************************************
165
 * End: terminate aa video thread output method
Sam Hocevar's avatar
 
Sam Hocevar committed
166
 *****************************************************************************/
167
static void End( vout_thread_t *p_vout )
Sam Hocevar's avatar
 
Sam Hocevar committed
168 169 170 171 172
{
    ;
}

/*****************************************************************************
173
 * Destroy: destroy aa video thread output method
Sam Hocevar's avatar
 
Sam Hocevar committed
174 175 176
 *****************************************************************************
 * Terminate an output method created by AaCreateOutputMethod
 *****************************************************************************/
177
static void Destroy( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
178
{
179 180
    vout_thread_t *p_vout = (vout_thread_t *)p_this;

181
    aa_close( p_vout->p_sys->aa_context );
Sam Hocevar's avatar
 
Sam Hocevar committed
182 183 184 185
    free( p_vout->p_sys );
}

/*****************************************************************************
186
 * Manage: handle aa events
Sam Hocevar's avatar
 
Sam Hocevar committed
187 188 189 190
 *****************************************************************************
 * This function should be called regularly by video output thread. It manages
 * console events. It returns a non null value on error.
 *****************************************************************************/
191
static int Manage( vout_thread_t *p_vout )
Sam Hocevar's avatar
 
Sam Hocevar committed
192
{
193 194 195 196 197 198
    int event, x, y, b;
    event = aa_getevent( p_vout->p_sys->aa_context, 0 );
    switch ( event )
    {
    case AA_MOUSE:
        aa_getmouse( p_vout->p_sys->aa_context, &x, &y, &b );
199 200 201
        if ( b & AA_BUTTON3 )
        {
            intf_thread_t *p_intf;
202
            p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
203 204 205 206 207
            if( p_intf )
            {
                p_intf->b_menu_change = 1;
                vlc_object_release( p_intf );
            }
208 209 210 211 212 213 214 215 216 217 218
        }
        break;
    case AA_RESIZE:
        p_vout->i_changes |= VOUT_SIZE_CHANGE;
        aa_resize( p_vout->p_sys->aa_context );
        p_vout->p_sys->i_width = aa_imgwidth( p_vout->p_sys->aa_context );
        p_vout->p_sys->i_height = aa_imgheight( p_vout->p_sys->aa_context );
        break;
    default:
        break;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
219 220 221 222
    return( 0 );
}

/*****************************************************************************
223
 * Render: render previously calculated output
Sam Hocevar's avatar
 
Sam Hocevar committed
224
 *****************************************************************************/
225
static void Render( vout_thread_t *p_vout, picture_t *p_pic )
Sam Hocevar's avatar
 
Sam Hocevar committed
226
{
227 228 229
  aa_fastrender( p_vout->p_sys->aa_context, 0, 0,
                 aa_imgwidth( p_vout->p_sys->aa_context ),
                 aa_imgheight( p_vout->p_sys->aa_context ) );
Sam Hocevar's avatar
 
Sam Hocevar committed
230 231 232
}

/*****************************************************************************
233
 * Display: displays previously rendered output
Sam Hocevar's avatar
 
Sam Hocevar committed
234
 *****************************************************************************/
235
static void Display( vout_thread_t *p_vout, picture_t *p_pic )
Sam Hocevar's avatar
 
Sam Hocevar committed
236 237 238 239 240 241 242
{
    /* No need to do anything, the fake direct buffers stay as they are */
    int i_width, i_height, i_x, i_y;

    vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
                       &i_x, &i_y, &i_width, &i_height );

Sam Hocevar's avatar
 
Sam Hocevar committed
243
    aa_flush(p_vout->p_sys->aa_context);
Sam Hocevar's avatar
 
Sam Hocevar committed
244 245 246 247 248 249 250 251
}

/*****************************************************************************
 * SetPalette: set the 8bpp palette
 *****************************************************************************/
static void SetPalette( vout_thread_t *p_vout, u16 *red, u16 *green, u16 *blue )
{
    int i;
Sam Hocevar's avatar
 
Sam Hocevar committed
252

Sam Hocevar's avatar
 
Sam Hocevar committed
253 254 255
    /* Fill colors with color information */
    for( i = 0; i < 256; i++ )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
256
        aa_setpalette( p_vout->p_sys->palette, 256 -i,
Sam Hocevar's avatar
 
Sam Hocevar committed
257 258 259 260
                       red[ i ], green[ i ], blue[ i ] );
    }
}