ggi.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:18k
源码类别:

midi

开发平台:

Unix_Linux

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