ggi.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:18k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * ggi.c : GGI plugin for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2000, 2001 VideoLAN
  5.  * $Id: ggi.c 8551 2004-08-28 17:36:02Z gbazin $
  6.  *
  7.  * Authors: Vincent Seguin <seguin@via.ecp.fr>
  8.  *          Samuel Hocevar <sam@zoy.org>
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  23.  *****************************************************************************/
  24. /*****************************************************************************
  25.  * Preamble
  26.  *****************************************************************************/
  27. #include <stdlib.h>                                      /* malloc(), free() */
  28. #include <string.h>
  29. #include <errno.h>                                                 /* ENOMEM */
  30. #include <ggi/ggi.h>
  31. #include <vlc/vlc.h>
  32. #include <vlc/intf.h>
  33. #include <vlc/vout.h>
  34. /*****************************************************************************
  35.  * Local prototypes
  36.  *****************************************************************************/
  37. static int  Create    ( vlc_object_t * );
  38. static void Destroy   ( vlc_object_t * );
  39. static int  Init      ( vout_thread_t * );
  40. static void End       ( vout_thread_t * );
  41. static int  Manage    ( vout_thread_t * );
  42. static void Display   ( vout_thread_t *, picture_t * );
  43. static int  OpenDisplay    ( vout_thread_t * );
  44. static void CloseDisplay   ( vout_thread_t * );
  45. static void SetPalette     ( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
  46. /*****************************************************************************
  47.  * Module descriptor
  48.  *****************************************************************************/
  49. #define DISPLAY_TEXT N_("X11 display name")
  50. #define DISPLAY_LONGTEXT N_( 
  51.             "Specify the X11 hardware display you want to use.n" 
  52.             "By default, VLC will use the value of the DISPLAY " 
  53.             "environment variable.")
  54. vlc_module_begin();
  55.     add_string( "ggi-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, VLC_TRUE );
  56.     set_description( "General Graphics Interface video output" );
  57.     set_capability( "video output", 30 );
  58.     set_callbacks( Create, Destroy );
  59. vlc_module_end();
  60. /*****************************************************************************
  61.  * vout_sys_t: video output GGI method descriptor
  62.  *****************************************************************************
  63.  * This structure is part of the video output thread descriptor.
  64.  * It describes the GGI specific properties of an output thread.
  65.  *****************************************************************************/
  66. struct vout_sys_t
  67. {
  68.     /* GGI system information */
  69.     ggi_visual_t        p_display;                         /* display device */
  70.     ggi_mode            mode;                             /* mode descriptor */
  71.     int                 i_bits_per_pixel;
  72.     /* Buffer information */
  73.     ggi_directbuffer *  pp_buffer[2];                             /* buffers */
  74.     int                 i_index;
  75.     vlc_bool_t          b_must_acquire;   /* must be acquired before writing */
  76. };
  77. /*****************************************************************************
  78.  * Create: allocate GGI video thread output method
  79.  *****************************************************************************
  80.  * This function allocate and initialize a GGI vout method. It uses some of the
  81.  * vout properties to choose the correct mode, and change them according to the
  82.  * mode actually used.
  83.  *****************************************************************************/
  84. static int Create( vlc_object_t *p_this )
  85. {
  86.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  87.     /* Allocate structure */
  88.     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
  89.     if( p_vout->p_sys == NULL )
  90.     {
  91.         msg_Err( p_vout, "out of memory" );
  92.         return( 1 );
  93.     }
  94.     /* Open and initialize device */
  95.     if( OpenDisplay( p_vout ) )
  96.     {
  97.         msg_Err( p_vout, "cannot initialize GGI display" );
  98.         free( p_vout->p_sys );
  99.         return( 1 );
  100.     }
  101.     p_vout->pf_init = Init;
  102.     p_vout->pf_end = End;
  103.     p_vout->pf_manage = Manage;
  104.     p_vout->pf_render = NULL;
  105.     p_vout->pf_display = Display;
  106.     return( 0 );
  107. }
  108. /*****************************************************************************
  109.  * Init: initialize GGI video thread output method
  110.  *****************************************************************************
  111.  * This function initialize the GGI display device.
  112.  *****************************************************************************/
  113. static int Init( vout_thread_t *p_vout )
  114. {
  115. #define p_b p_vout->p_sys->pp_buffer
  116.     int i_index;
  117.     picture_t *p_pic;
  118.     I_OUTPUTPICTURES = 0;
  119.     p_vout->output.i_width  = p_vout->p_sys->mode.visible.x;
  120.     p_vout->output.i_height = p_vout->p_sys->mode.visible.y;
  121.     p_vout->output.i_aspect = p_vout->p_sys->mode.visible.x
  122.                                * VOUT_ASPECT_FACTOR
  123.                                / p_vout->p_sys->mode.visible.y;
  124.     switch( p_vout->p_sys->i_bits_per_pixel )
  125.     {
  126.         case 8:
  127.             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
  128.             p_vout->output.pf_setpalette = SetPalette;
  129.             break;
  130.         case 15:
  131.             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
  132.         case 16:
  133.             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
  134.         case 24:
  135.             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
  136.         case 32:
  137.             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
  138.         default:
  139.             msg_Err( p_vout, "unknown screen depth %i",
  140.                      p_vout->p_sys->i_bits_per_pixel );
  141.             return 0;
  142.     }
  143.     /* Only useful for bits_per_pixel != 8 */
  144.     p_vout->output.i_rmask = p_b[ 0 ]->buffer.plb.pixelformat->red_mask;
  145.     p_vout->output.i_gmask = p_b[ 0 ]->buffer.plb.pixelformat->green_mask;
  146.     p_vout->output.i_bmask = p_b[ 0 ]->buffer.plb.pixelformat->blue_mask;
  147.     p_pic = NULL;
  148.     /* Find an empty picture slot */
  149.     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
  150.     {
  151.         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
  152.         {
  153.             p_pic = p_vout->p_picture + i_index;
  154.             break;
  155.         }
  156.     }
  157.     if( p_pic == NULL )
  158.     {
  159.         return 0;
  160.     }
  161.     /* We know the chroma, allocate a buffer which will be used
  162.      * directly by the decoder */
  163.     p_vout->p_sys->i_index = 0;
  164.     p_pic->p->p_pixels = p_b[ 0 ]->write;
  165.     p_pic->p->i_pixel_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size / 8;
  166.     p_pic->p->i_lines = p_vout->p_sys->mode.visible.y;
  167.     p_pic->p->i_visible_lines = p_vout->p_sys->mode.visible.y;
  168.     p_pic->p->i_pitch = p_b[ 0 ]->buffer.plb.stride;
  169.     if( p_b[ 0 ]->buffer.plb.pixelformat->size / 8
  170.          * p_vout->p_sys->mode.visible.x
  171.         != p_b[ 0 ]->buffer.plb.stride )
  172.     {
  173.         p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.pixelformat->size
  174.                                      / 8 * p_vout->p_sys->mode.visible.x;
  175.     }
  176.     else
  177.     {
  178.         p_pic->p->i_visible_pitch = p_b[ 0 ]->buffer.plb.stride;
  179.     }
  180.     p_pic->i_planes = 1;
  181.     p_pic->i_status = DESTROYED_PICTURE;
  182.     p_pic->i_type   = DIRECT_PICTURE;
  183.     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
  184.     I_OUTPUTPICTURES++;
  185.     /* Acquire first buffer */
  186.     if( p_vout->p_sys->b_must_acquire )
  187.     {
  188.         ggiResourceAcquire( p_b[ 0 ]->resource,
  189.                             GGI_ACTYPE_WRITE );
  190.     }
  191.     /* Listen to the keyboard and the mouse buttons */
  192.     ggiSetEventMask( p_vout->p_sys->p_display,
  193.                      emKeyboard | emPtrButtonPress | emPtrButtonRelease );
  194.     /* Set asynchronous display mode -- usually quite faster */
  195.     ggiAddFlags( p_vout->p_sys->p_display, GGIFLAG_ASYNC );
  196.     return( 0 );
  197. #undef p_b
  198. }
  199. /*****************************************************************************
  200.  * End: terminate GGI video thread output method
  201.  *****************************************************************************
  202.  * Terminate an output method created by Create
  203.  *****************************************************************************/
  204. static void End( vout_thread_t *p_vout )
  205. {
  206. #define p_b p_vout->p_sys->pp_buffer
  207.     /* Release buffer */
  208.     if( p_vout->p_sys->b_must_acquire )
  209.     {
  210.         ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
  211.     }
  212. #undef p_b
  213. }
  214. /*****************************************************************************
  215.  * Destroy: destroy GGI video thread output method
  216.  *****************************************************************************
  217.  * Terminate an output method created by Create
  218.  *****************************************************************************/
  219. static void Destroy( vlc_object_t *p_this )
  220. {
  221.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  222.     CloseDisplay( p_vout );
  223.     free( p_vout->p_sys );
  224. }
  225. /*****************************************************************************
  226.  * Manage: handle GGI events
  227.  *****************************************************************************
  228.  * This function should be called regularly by video output thread. It returns
  229.  * a non null value if an error occurred.
  230.  *****************************************************************************/
  231. static int Manage( vout_thread_t *p_vout )
  232. {
  233.     struct timeval tv = { 0, 1000 };                        /* 1 millisecond */
  234.     gii_event_mask mask;
  235.     gii_event      event;
  236.     vlc_value_t    val;
  237.     mask = emKeyboard | emPtrButtonPress | emPtrButtonRelease;
  238.     ggiEventPoll( p_vout->p_sys->p_display, mask, &tv );
  239.     while( ggiEventsQueued( p_vout->p_sys->p_display, mask) )
  240.     {
  241.         ggiEventRead( p_vout->p_sys->p_display, &event, mask);
  242.         switch( event.any.type )
  243.         {
  244.             case evKeyRelease:
  245.                 switch( event.key.sym )
  246.                 {
  247.                     case 'q':
  248.                     case 'Q':
  249.                     case GIIUC_Escape:
  250.                         p_vout->p_vlc->b_die = 1;
  251.                         break;
  252.                     default:
  253.                         break;
  254.                 }
  255.                 break;
  256.             case evPtrButtonRelease:
  257.                 switch( event.pbutton.button )
  258.                 {
  259.                     case GII_PBUTTON_LEFT:
  260.                         val.b_bool = VLC_TRUE;
  261.                         var_Set( p_vout, "mouse-clicked", val );
  262.                         break;
  263.                     case GII_PBUTTON_RIGHT:
  264.                         {
  265.                             intf_thread_t *p_intf;
  266.                             p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF,
  267.                                                               FIND_ANYWHERE );
  268.                             if( p_intf )
  269.                             {
  270.                                 p_intf->b_menu_change = 1;
  271.                                 vlc_object_release( p_intf );
  272.                             }
  273.                         }
  274.                         break;
  275.                 }
  276.                 break;
  277.             default:
  278.                 break;
  279.         }
  280.     }
  281.     return( 0 );
  282. }
  283. /*****************************************************************************
  284.  * Display: displays previously rendered output
  285.  *****************************************************************************/
  286. static void Display( vout_thread_t *p_vout, picture_t *p_pic )
  287. {
  288. #define p_b p_vout->p_sys->pp_buffer
  289.     p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
  290.     /* Change display frame */
  291.     if( p_vout->p_sys->b_must_acquire )
  292.     {
  293.         ggiResourceRelease( p_b[ p_vout->p_sys->i_index ]->resource );
  294.     }
  295.     ggiSetDisplayFrame( p_vout->p_sys->p_display,
  296.                         p_b[ p_vout->p_sys->i_index ]->frame );
  297.     /* Swap buffers and change write frame */
  298.     p_vout->p_sys->i_index ^= 1;
  299.     p_pic->p->p_pixels = p_b[ p_vout->p_sys->i_index ]->write;
  300.     if( p_vout->p_sys->b_must_acquire )
  301.     {
  302.         ggiResourceAcquire( p_b[ p_vout->p_sys->i_index ]->resource,
  303.                             GGI_ACTYPE_WRITE );
  304.     }
  305.     ggiSetWriteFrame( p_vout->p_sys->p_display,
  306.                       p_b[ p_vout->p_sys->i_index ]->frame );
  307.     /* Flush the output so that it actually displays */
  308.     ggiFlush( p_vout->p_sys->p_display );
  309. #undef p_b
  310. }
  311. /* following functions are local */
  312. /*****************************************************************************
  313.  * OpenDisplay: open and initialize GGI device
  314.  *****************************************************************************
  315.  * Open and initialize display according to preferences specified in the vout
  316.  * thread fields.
  317.  *****************************************************************************/
  318. static int OpenDisplay( vout_thread_t *p_vout )
  319. {
  320. #define p_b p_vout->p_sys->pp_buffer
  321.     ggi_color   col_fg;                                  /* foreground color */
  322.     ggi_color   col_bg;                                  /* background color */
  323.     int         i_index;                               /* all purposes index */
  324.     char        *psz_display;
  325.     /* Initialize library */
  326.     if( ggiInit() )
  327.     {
  328.         msg_Err( p_vout, "cannot initialize GGI library" );
  329.         return( 1 );
  330.     }
  331.     /* Open display */
  332.     psz_display = config_GetPsz( p_vout, "ggi_display" );
  333.     p_vout->p_sys->p_display = ggiOpen( psz_display, NULL );
  334.     if( psz_display ) free( psz_display );
  335.     if( p_vout->p_sys->p_display == NULL )
  336.     {
  337.         msg_Err( p_vout, "cannot open GGI default display" );
  338.         ggiExit();
  339.         return( 1 );
  340.     }
  341.     /* Find most appropriate mode */
  342.     p_vout->p_sys->mode.frames =    2;                          /* 2 buffers */
  343.     p_vout->p_sys->mode.visible.x = config_GetInt( p_vout, "width" );
  344.     p_vout->p_sys->mode.visible.y = config_GetInt( p_vout, "height" );
  345.     p_vout->p_sys->mode.virt.x =    GGI_AUTO;
  346.     p_vout->p_sys->mode.virt.y =    GGI_AUTO;
  347.     p_vout->p_sys->mode.size.x =    GGI_AUTO;
  348.     p_vout->p_sys->mode.size.y =    GGI_AUTO;
  349.     p_vout->p_sys->mode.graphtype = GT_15BIT;        /* minimum usable depth */
  350.     p_vout->p_sys->mode.dpp.x =     GGI_AUTO;
  351.     p_vout->p_sys->mode.dpp.y =     GGI_AUTO;
  352.     ggiCheckMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode );
  353.     /* FIXME: Check that returned mode has some minimum properties */
  354.     /* Set mode */
  355.     if( ggiSetMode( p_vout->p_sys->p_display, &p_vout->p_sys->mode ) )
  356.     {
  357.         msg_Err( p_vout, "cannot set GGI mode" );
  358.         ggiClose( p_vout->p_sys->p_display );
  359.         ggiExit();
  360.         return( 1 );
  361.     }
  362.     /* Check buffers properties */
  363.     p_vout->p_sys->b_must_acquire = 0;
  364.     for( i_index = 0; i_index < 2; i_index++ )
  365.     {
  366.         /* Get buffer address */
  367.         p_vout->p_sys->pp_buffer[ i_index ] =
  368.             (ggi_directbuffer *)ggiDBGetBuffer( p_vout->p_sys->p_display,
  369.                                                 i_index );
  370.         if( p_b[ i_index ] == NULL )
  371.         {
  372.             msg_Err( p_vout, "double buffering is not possible" );
  373.             ggiClose( p_vout->p_sys->p_display );
  374.             ggiExit();
  375.             return( 1 );
  376.         }
  377.         /* Check buffer properties */
  378.         if( ! ( p_b[ i_index ]->type & GGI_DB_SIMPLE_PLB )
  379.            || ( p_b[ i_index ]->page_size != 0 )
  380.            || ( p_b[ i_index ]->write == NULL )
  381.            || ( p_b[ i_index ]->noaccess != 0 )
  382.            || ( p_b[ i_index ]->align != 0 ) )
  383.         {
  384.             msg_Err( p_vout, "incorrect video memory type" );
  385.             ggiClose( p_vout->p_sys->p_display );
  386.             ggiExit();
  387.             return( 1 );
  388.         }
  389.         /* Check if buffer needs to be acquired before write */
  390.         if( ggiResourceMustAcquire( p_b[ i_index ]->resource ) )
  391.         {
  392.             p_vout->p_sys->b_must_acquire = 1;
  393.         }
  394.     }
  395.     /* Set graphic context colors */
  396.     col_fg.r = col_fg.g = col_fg.b = -1;
  397.     col_bg.r = col_bg.g = col_bg.b = 0;
  398.     if( ggiSetGCForeground(p_vout->p_sys->p_display,
  399.                            ggiMapColor(p_vout->p_sys->p_display,&col_fg)) ||
  400.         ggiSetGCBackground(p_vout->p_sys->p_display,
  401.                            ggiMapColor(p_vout->p_sys->p_display,&col_bg)) )
  402.     {
  403.         msg_Err( p_vout, "cannot set colors" );
  404.         ggiClose( p_vout->p_sys->p_display );
  405.         ggiExit();
  406.         return( 1 );
  407.     }
  408.     /* Set clipping for text */
  409.     if( ggiSetGCClipping( p_vout->p_sys->p_display, 0, 0,
  410.                           p_vout->p_sys->mode.visible.x,
  411.                           p_vout->p_sys->mode.visible.y ) )
  412.     {
  413.         msg_Err( p_vout, "cannot set clipping" );
  414.         ggiClose( p_vout->p_sys->p_display );
  415.         ggiExit();
  416.         return( 1 );
  417.     }
  418.     /* FIXME: set palette in 8bpp */
  419.     p_vout->p_sys->i_bits_per_pixel = p_b[ 0 ]->buffer.plb.pixelformat->depth;
  420.     return( 0 );
  421. #undef p_b
  422. }
  423. /*****************************************************************************
  424.  * CloseDisplay: close and reset GGI device
  425.  *****************************************************************************
  426.  * This function returns all resources allocated by OpenDisplay and restore
  427.  * the original state of the device.
  428.  *****************************************************************************/
  429. static void CloseDisplay( vout_thread_t *p_vout )
  430. {
  431.     /* Restore original mode and close display */
  432.     ggiClose( p_vout->p_sys->p_display );
  433.     /* Exit library */
  434.     ggiExit();
  435. }
  436. /*****************************************************************************
  437.  * SetPalette: sets an 8 bpp palette
  438.  *****************************************************************************/
  439. static void SetPalette( vout_thread_t *p_vout,
  440.                         uint16_t *red, uint16_t *green, uint16_t *blue )
  441. {
  442.     ggi_color colors[256];
  443.     int i;
  444.     /* Fill colors with color information */
  445.     for( i = 0; i < 256; i++ )
  446.     {
  447.         colors[ i ].r = red[ i ];
  448.         colors[ i ].g = green[ i ];
  449.         colors[ i ].b = blue[ i ];
  450.         colors[ i ].a = 0;
  451.     }
  452.     /* Set palette */
  453.     if( ggiSetPalette( p_vout->p_sys->p_display, 0, 256, colors ) < 0 )
  454.     {
  455.         msg_Err( p_vout, "failed setting palette" );
  456.     }
  457. }