ggi.c 18 KB
Newer Older
1 2 3
/*****************************************************************************
 * ggi.c : GGI plugin for vlc
 *****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
4
 * Copyright (C) 2000, 2001 VideoLAN
Gildas Bazin's avatar
 
Gildas Bazin committed
5
 * $Id: ggi.c,v 1.16 2002/03/11 07:23:09 gbazin Exp $
6
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
7 8
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
Sam Hocevar's avatar
 
Sam Hocevar committed
9
 *      
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
29
#include <string.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
30 31 32
#include <errno.h>                                                 /* ENOMEM */

#include <ggi/ggi.h>
33

Sam Hocevar's avatar
 
Sam Hocevar committed
34
#include <videolan/vlc.h>
35 36 37 38

#include "video.h"
#include "video_output.h"

Sam Hocevar's avatar
 
Sam Hocevar committed
39 40 41
#include "intf_msg.h"
#include "interface.h"

Sam Hocevar's avatar
 
Sam Hocevar committed
42
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
43
 * Local prototypes.
Sam Hocevar's avatar
 
Sam Hocevar committed
44
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
45 46 47 48 49 50 51 52 53 54 55 56
static void vout_getfunctions( function_list_t * p_function_list );

static int  vout_Create    ( vout_thread_t * );
static int  vout_Init      ( vout_thread_t * );
static void vout_End       ( vout_thread_t * );
static void vout_Destroy   ( vout_thread_t * );
static int  vout_Manage    ( vout_thread_t * );
static void vout_Render    ( vout_thread_t *, picture_t * );
static void vout_Display   ( vout_thread_t *, picture_t * );

static int  OpenDisplay    ( vout_thread_t * );
static void CloseDisplay   ( vout_thread_t * );
57 58

/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
59
 * Building configuration tree
60
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
61 62
MODULE_CONFIG_START
MODULE_CONFIG_STOP
63

Sam Hocevar's avatar
 
Sam Hocevar committed
64
MODULE_INIT_START
Sam Hocevar's avatar
 
Sam Hocevar committed
65 66 67
    SET_DESCRIPTION( "General Graphics Interface video output" )
    ADD_CAPABILITY( VOUT, 30 )
    ADD_SHORTCUT( "ggi" )
Sam Hocevar's avatar
 
Sam Hocevar committed
68
MODULE_INIT_STOP
69

Sam Hocevar's avatar
 
Sam Hocevar committed
70
MODULE_ACTIVATE_START
Sam Hocevar's avatar
 
Sam Hocevar committed
71
    vout_getfunctions( &p_module->p_functions->vout );
Sam Hocevar's avatar
 
Sam Hocevar committed
72
MODULE_ACTIVATE_STOP
Sam Hocevar's avatar
 
Sam Hocevar committed
73

Sam Hocevar's avatar
 
Sam Hocevar committed
74 75
MODULE_DEACTIVATE_START
MODULE_DEACTIVATE_STOP
76

Sam Hocevar's avatar
 
Sam Hocevar committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/*****************************************************************************
 * vout_sys_t: video output GGI method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the GGI specific properties of an output thread.
 *****************************************************************************/
typedef struct vout_sys_s
{
    /* GGI system informations */
    ggi_visual_t        p_display;                         /* display device */

    ggi_mode            mode;                             /* mode descriptor */
    int                 i_bits_per_pixel;

    /* Buffer information */
Sam Hocevar's avatar
 
Sam Hocevar committed
92
    ggi_directbuffer *  pp_buffer[2];                             /* buffers */
Sam Hocevar's avatar
 
Sam Hocevar committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    int                 i_index;

    boolean_t           b_must_acquire;   /* must be acquired before writing */
} vout_sys_t;

/*****************************************************************************
 * Functions exported as capabilities. They are declared as static so that
 * we don't pollute the namespace too much.
 *****************************************************************************/
static void vout_getfunctions( function_list_t * p_function_list )
{
    p_function_list->functions.vout.pf_create  = vout_Create;
    p_function_list->functions.vout.pf_init    = vout_Init;
    p_function_list->functions.vout.pf_end     = vout_End;
    p_function_list->functions.vout.pf_destroy = vout_Destroy;
    p_function_list->functions.vout.pf_manage  = vout_Manage;
    p_function_list->functions.vout.pf_render  = vout_Render;
    p_function_list->functions.vout.pf_display = vout_Display;
}

/*****************************************************************************
 * vout_Create: allocate GGI video thread output method
 *****************************************************************************
 * This function allocate and initialize a GGI vout method. It uses some of the
 * vout properties to choose the correct mode, and change them according to the
 * mode actually used.
 *****************************************************************************/
int vout_Create( vout_thread_t *p_vout )
{
    /* Allocate structure */
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
    {
        intf_ErrMsg( "vout error: %s", strerror(ENOMEM) );
        return( 1 );
    }

    /* Open and initialize device */
    if( OpenDisplay( p_vout ) )
    {
        intf_ErrMsg( "vout error: can't initialize GGI display" );
        free( p_vout->p_sys );
        return( 1 );
    }

    return( 0 );
}

/*****************************************************************************
 * vout_Init: initialize GGI video thread output method
 *****************************************************************************
 * This function initialize the GGI display device.
 *****************************************************************************/
int vout_Init( vout_thread_t *p_vout )
{
    int i_index;
    picture_t *p_pic;

Sam Hocevar's avatar
 
Sam Hocevar committed
151 152
    I_OUTPUTPICTURES = 0;

Sam Hocevar's avatar
 
Sam Hocevar committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    p_vout->output.i_width  = p_vout->p_sys->mode.visible.x;
    p_vout->output.i_height = p_vout->p_sys->mode.visible.y;
    p_vout->output.i_aspect = p_vout->p_sys->mode.visible.x
                               * VOUT_ASPECT_FACTOR
                               / p_vout->p_sys->mode.visible.y;

    switch( p_vout->p_sys->i_bits_per_pixel )
    {
        case 8: /* FIXME: set the palette */
            p_vout->output.i_chroma = FOURCC_BI_RGB; break;
        case 15:
            p_vout->output.i_chroma = FOURCC_RV15; break;
        case 16:
            p_vout->output.i_chroma = FOURCC_RV16; break;
        case 24:
            p_vout->output.i_chroma = FOURCC_BI_BITFIELDS; break;
        case 32:
            p_vout->output.i_chroma = FOURCC_BI_BITFIELDS; break;
        default:
            intf_ErrMsg( "vout error: unknown screen depth" );
Sam Hocevar's avatar
 
Sam Hocevar committed
173
            return 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
174 175
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
176
    p_pic = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
177

Sam Hocevar's avatar
 
Sam Hocevar committed
178 179
    /* Find an empty picture slot */
    for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
180
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
181
        if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
Sam Hocevar's avatar
 
Sam Hocevar committed
182
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
183
            p_pic = p_vout->p_picture + i_index;
Sam Hocevar's avatar
 
Sam Hocevar committed
184 185
            break;
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
186
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
187

Sam Hocevar's avatar
 
Sam Hocevar committed
188 189 190 191
    if( p_pic == NULL )
    {
        return 0;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
192

Sam Hocevar's avatar
 
Sam Hocevar committed
193 194 195 196 197 198 199
#define p_b p_vout->p_sys->pp_buffer
    /* We know the chroma, allocate a buffer which will be used
     * directly by the decoder */
    p_vout->p_sys->i_index = 0;
    p_pic->p->p_pixels = p_b[ 0 ]->write;
    p_pic->p->i_pixel_bytes = p_b[ 0 ]->buffer.plb.pixelformat->size / 8;
    p_pic->p->i_lines = p_vout->p_sys->mode.visible.y;
Sam Hocevar's avatar
 
Sam Hocevar committed
200

Sam Hocevar's avatar
 
Sam Hocevar committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214
    if( p_b[ 0 ]->buffer.plb.pixelformat->size / 8
         * p_vout->p_sys->mode.visible.x
        != p_b[ 0 ]->buffer.plb.stride )
    {
        p_pic->p->b_margin = 1;
        p_pic->p->b_hidden = 1;
        p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
        p_pic->p->i_visible_bytes = p_b[ 0 ]->buffer.plb.pixelformat->size
                                     / 8 * p_vout->p_sys->mode.visible.x;
    }
    else
    {
        p_pic->p->b_margin = 0;
        p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
Sam Hocevar's avatar
 
Sam Hocevar committed
215 216
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230
    /* Only useful for bits_per_pixel != 8 */
    p_pic->p->i_red_mask =   p_b[ 0 ]->buffer.plb.pixelformat->red_mask;
    p_pic->p->i_green_mask = p_b[ 0 ]->buffer.plb.pixelformat->green_mask;
    p_pic->p->i_blue_mask =  p_b[ 0 ]->buffer.plb.pixelformat->blue_mask;

    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++;

Sam Hocevar's avatar
 
Sam Hocevar committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    /* Acquire first buffer */
    if( p_vout->p_sys->b_must_acquire )
    {
        ggiResourceAcquire( p_b[ 0 ]->resource,
                            GGI_ACTYPE_WRITE );
    }

    /* Listen to the keyboard and the mouse buttons */
    ggiSetEventMask( p_vout->p_sys->p_display,
                     emKeyboard | emPtrButtonPress | emPtrButtonRelease );

    /* Set asynchronous display mode -- usually quite faster */
    ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );

    return( 0 );
#undef p_b
}

/*****************************************************************************
 * vout_End: terminate GGI video thread output method
 *****************************************************************************
 * Terminate an output method created by vout_Create
 *****************************************************************************/
void vout_End( vout_thread_t *p_vout )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
256
#define p_b p_vout->p_sys->pp_buffer
Sam Hocevar's avatar
 
Sam Hocevar committed
257 258 259
    /* Release buffer */
    if( p_vout->p_sys->b_must_acquire )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
260
        ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
Sam Hocevar's avatar
 
Sam Hocevar committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    }
#undef p_b
}

/*****************************************************************************
 * vout_Destroy: destroy GGI video thread output method
 *****************************************************************************
 * Terminate an output method created by vout_Create
 *****************************************************************************/
void vout_Destroy( vout_thread_t *p_vout )
{
    CloseDisplay( p_vout );

    free( p_vout->p_sys );
}

/*****************************************************************************
 * vout_Manage: handle GGI events
 *****************************************************************************
 * This function should be called regularly by video output thread. It returns
 * a non null value if an error occured.
 *****************************************************************************/
int vout_Manage( vout_thread_t *p_vout )
{
    struct timeval tv = { 0, 1000 };                        /* 1 millisecond */
    gii_event_mask mask;
    gii_event      event;

    mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;

    ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
    
    while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
    {
        ggiEventRead( p_vout->p_sys->p_display, &event, mask);

        switch( event.any.type )
        {
            case evKeyRelease:

                switch( event.key.sym )
                {
                    case 'q':
                    case 'Q':
                    case GIIUC_Escape:
                        /* FIXME pass message ! */
                        p_main->p_intf->b_die = 1;
                        break;

                    default:
                        break;
                }
                break;

            case evPtrButtonRelease:

                switch( event.pbutton.button )
                {
                    case GII_PBUTTON_RIGHT:
                        /* FIXME: need locking ! */
                        p_main->p_intf->b_menu_change = 1;
                        break;
                }
                break;

            default:
                break;
        }
    }

    return( 0 );
}

/*****************************************************************************
 * vout_Render: displays previously rendered output
 *****************************************************************************/
void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
{
    ;
}

/*****************************************************************************
 * vout_Display: displays previously rendered output
 *****************************************************************************/
void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
347 348 349
#define p_b p_vout->p_sys->pp_buffer
    p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;

Sam Hocevar's avatar
 
Sam Hocevar committed
350 351 352
    /* Change display frame */
    if( p_vout->p_sys->b_must_acquire )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
353
        ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
Sam Hocevar's avatar
 
Sam Hocevar committed
354 355
    }
    ggiSetDisplayFrame( p_vout->p_sys->p_display,
Sam Hocevar's avatar
 
Sam Hocevar committed
356
                        p_b[ p_vout->p_sys->i_index ]->frame );
Sam Hocevar's avatar
 
Sam Hocevar committed
357 358

    /* Swap buffers and change write frame */
Sam Hocevar's avatar
 
Sam Hocevar committed
359 360 361
    p_vout->p_sys->i_index ^= 1;
    p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;

Sam Hocevar's avatar
 
Sam Hocevar committed
362 363
    if( p_vout->p_sys->b_must_acquire )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
364
        ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
Sam Hocevar's avatar
 
Sam Hocevar committed
365 366 367
                            GGI_ACTYPE_WRITE );
    }
    ggiSetWriteFrame( p_vout->p_sys->p_display,
Sam Hocevar's avatar
 
Sam Hocevar committed
368
                      p_b[ p_vout->p_sys->i_index ]->frame );
Sam Hocevar's avatar
 
Sam Hocevar committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

    /* Flush the output so that it actually displays */
    ggiFlush( p_vout->p_sys->p_display );
#undef p_b
}

/* following functions are local */

/*****************************************************************************
 * OpenDisplay: open and initialize GGI device
 *****************************************************************************
 * Open and initialize display according to preferences specified in the vout
 * thread fields.
 *****************************************************************************/
static int OpenDisplay( vout_thread_t *p_vout )
{
Sam Hocevar's avatar
 
Sam Hocevar committed
385
#define p_b p_vout->p_sys->pp_buffer
Sam Hocevar's avatar
 
Sam Hocevar committed
386 387 388 389 390 391 392 393 394 395 396 397 398
    ggi_color   col_fg;                                  /* foreground color */
    ggi_color   col_bg;                                  /* background color */
    int         i_index;                               /* all purposes index */
    char        *psz_display;

    /* Initialize library */
    if( ggiInit() )
    {
        intf_ErrMsg( "vout error: can't initialize GGI library" );
        return( 1 );
    }

    /* Open display */
Gildas Bazin's avatar
 
Gildas Bazin committed
399
    psz_display = config_GetPszVariable( "display" );
Sam Hocevar's avatar
 
Sam Hocevar committed
400 401

    p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
Gildas Bazin's avatar
 
Gildas Bazin committed
402
    if( psz_display ) free( psz_display );
Sam Hocevar's avatar
 
Sam Hocevar committed
403 404 405 406 407 408 409 410 411 412

    if( p_vout->p_sys->p_display == NULL )
    {
        intf_ErrMsg( "vout error: can't open GGI default display" );
        ggiExit();
        return( 1 );
    }

    /* Find most appropriate mode */
    p_vout->p_sys->mode.frames =    2;                          /* 2 buffers */
Gildas Bazin's avatar
 
Gildas Bazin committed
413 414
    p_vout->p_sys->mode.visible.x = config_GetIntVariable( "width" );
    p_vout->p_sys->mode.visible.y = config_GetIntVariable( "height" );
Sam Hocevar's avatar
 
Sam Hocevar committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    p_vout->p_sys->mode.virt.x =    GGI_AUTO;
    p_vout->p_sys->mode.virt.y =    GGI_AUTO;
    p_vout->p_sys->mode.size.x =    GGI_AUTO;
    p_vout->p_sys->mode.size.y =    GGI_AUTO;
    p_vout->p_sys->mode.graphtype = GT_15BIT;        /* minimum usable depth */
    p_vout->p_sys->mode.dpp.x =     GGI_AUTO;
    p_vout->p_sys->mode.dpp.y =     GGI_AUTO;
    ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );

    /* FIXME: Check that returned mode has some minimum properties */

    /* Set mode */
    if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
    {
        intf_ErrMsg( "vout error: can't set GGI mode" );
        ggiClose( p_vout->p_sys->p_display );
        ggiExit();
        return( 1 );
    }

    /* Check buffers properties */
    p_vout->p_sys->b_must_acquire = 0;
    for( i_index = 0; i_index < 2; i_index++ )
    {
        /* Get buffer address */
Sam Hocevar's avatar
 
Sam Hocevar committed
440
        p_vout->p_sys->pp_buffer[ i_index ] =
Sam Hocevar's avatar
 
Sam Hocevar committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
            (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
                                                i_index );
        if( p_b[ i_index ] == NULL )
        {
            intf_ErrMsg( "vout error: double buffering is not possible" );
            ggiClose( p_vout->p_sys->p_display );
            ggiExit();
            return( 1 );
        }

        /* Check buffer properties */
        if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
           || ( p_b[ i_index ]->page_size != 0 )
           || ( p_b[ i_index ]->write == NULL )
           || ( p_b[ i_index ]->noaccess != 0 )
           || ( p_b[ i_index ]->align != 0 ) )
        {
            intf_ErrMsg( "vout error: incorrect video memory type" );
            ggiClose( p_vout->p_sys->p_display );
            ggiExit();
            return( 1 );
        }

        /* Check if buffer needs to be acquired before write */
        if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
        {
            p_vout->p_sys->b_must_acquire = 1;
        }
    }

    /* Set graphic context colors */
    col_fg.r = col_fg.g = col_fg.b = -1;
    col_bg.r = col_bg.g = col_bg.b = 0;
    if( ggiSetGCForeground(p_vout->p_sys->p_display,
                           ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
        ggiSetGCBackground(p_vout->p_sys->p_display,
                           ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
    {
        intf_ErrMsg( "vout error: can't set colors" );
        ggiClose( p_vout->p_sys->p_display );
        ggiExit();
        return( 1 );
    }

    /* Set clipping for text */
    if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
                          p_vout->p_sys->mode.visible.x,
                          p_vout->p_sys->mode.visible.y ) )
    {
        intf_ErrMsg( "vout error: can't set clipping" );
        ggiClose( p_vout->p_sys->p_display );
        ggiExit();
        return( 1 );
    }

    /* FIXME: set palette in 8bpp */
    p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;

    return( 0 );
#undef p_b
}

/*****************************************************************************
 * CloseDisplay: close and reset GGI device
 *****************************************************************************
 * This function returns all resources allocated by OpenDisplay and restore
 * the original state of the device.
 *****************************************************************************/
static void CloseDisplay( vout_thread_t *p_vout )
{
    /* Restore original mode and close display */
    ggiClose( p_vout->p_sys->p_display );

    /* Exit library */
    ggiExit();
}