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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * caca.c: Color ASCII Art video output plugin using libcaca
  3.  *****************************************************************************
  4.  * Copyright (C) 2003, 2004 VideoLAN
  5.  * $Id: caca.c 8551 2004-08-28 17:36:02Z gbazin $
  6.  *
  7.  * Authors: Sam Hocevar <sam@zoy.org>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #include <errno.h>                                                 /* ENOMEM */
  27. #include <stdlib.h>                                                /* free() */
  28. #include <string.h>                                            /* strerror() */
  29. #include <caca.h>
  30. #include <vlc/vlc.h>
  31. #include <vlc/vout.h>
  32. #include <vlc/intf.h>
  33. #include <vlc_keys.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 Render    ( vout_thread_t *, picture_t * );
  43. static void Display   ( vout_thread_t *, picture_t * );
  44. /*****************************************************************************
  45.  * Module descriptor
  46.  *****************************************************************************/
  47. vlc_module_begin();
  48.     set_description( _("color ASCII art video output") );
  49.     set_capability( "video output", 12 );
  50.     set_callbacks( Create, Destroy );
  51. vlc_module_end();
  52. /*****************************************************************************
  53.  * vout_sys_t: libcaca video output method descriptor
  54.  *****************************************************************************
  55.  * This structure is part of the video output thread descriptor.
  56.  * It describes the libcaca specific properties of an output thread.
  57.  *****************************************************************************/
  58. struct vout_sys_t
  59. {
  60.     struct caca_bitmap *p_bitmap;
  61. };
  62. /*****************************************************************************
  63.  * Create: allocates libcaca video output thread
  64.  *****************************************************************************
  65.  * This function initializes libcaca vout method.
  66.  *****************************************************************************/
  67. static int Create( vlc_object_t *p_this )
  68. {
  69.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  70. #if defined( WIN32 ) && !defined( UNDER_CE )
  71.     if( AllocConsole() )
  72.     {
  73.         CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
  74.         SMALL_RECT rect;
  75.         COORD coord;
  76.         HANDLE hstdout =
  77.             CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE,
  78.                                        FILE_SHARE_READ | FILE_SHARE_WRITE,
  79.                                        NULL, CONSOLE_TEXTMODE_BUFFER, NULL );
  80.         if( !hstdout || hstdout == INVALID_HANDLE_VALUE )
  81.         {
  82.             msg_Err( p_vout, "cannot create screen buffer" );
  83.             FreeConsole();
  84.             return VLC_EGENERIC;
  85.         }
  86.         if( !SetConsoleActiveScreenBuffer( hstdout) )
  87.         {
  88.             msg_Err( p_vout, "cannot set active screen buffer" );
  89.             FreeConsole();
  90.             return VLC_EGENERIC;
  91.         }
  92.         coord = GetLargestConsoleWindowSize( hstdout );
  93.         msg_Dbg( p_vout, "SetConsoleWindowInfo: %ix%i", coord.X, coord.Y );
  94.         /* Force size for now */
  95.         coord.X = 100;
  96.         coord.Y = 40;
  97.         if( !SetConsoleScreenBufferSize( hstdout, coord ) )
  98.             msg_Warn( p_vout, "SetConsoleScreenBufferSize %i %i",
  99.                       coord.X, coord.Y );
  100.         /* Get the current screen buffer size and window position. */
  101.         if( GetConsoleScreenBufferInfo( hstdout, &csbiInfo ) )
  102.         {
  103.             rect.Top = 0; rect.Left = 0;
  104.             rect.Right = csbiInfo.dwMaximumWindowSize.X - 1;
  105.             rect.Bottom = csbiInfo.dwMaximumWindowSize.Y - 1;
  106.             if( !SetConsoleWindowInfo( hstdout, TRUE, &rect ) )
  107.                 msg_Dbg( p_vout, "SetConsoleWindowInfo failed: %ix%i",
  108.                          rect.Right, rect.Bottom );
  109.         }
  110.     }
  111.     else
  112.     {
  113.         msg_Err( p_vout, "cannot create console" );
  114.         return VLC_EGENERIC;
  115.     }
  116. #endif
  117.     /* Allocate structure */
  118.     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
  119.     if( p_vout->p_sys == NULL )
  120.     {
  121.         msg_Err( p_vout, "out of memory" );
  122.         return VLC_ENOMEM;
  123.     }
  124.     if( caca_init() )
  125.     {
  126.         msg_Err( p_vout, "cannot initialize libcaca" );
  127.         free( p_vout->p_sys );
  128.         return VLC_EGENERIC;
  129.     }
  130.     caca_set_window_title( VOUT_TITLE " - Colour AsCii Art (caca)" );
  131.     p_vout->pf_init = Init;
  132.     p_vout->pf_end = End;
  133.     p_vout->pf_manage = Manage;
  134.     p_vout->pf_render = Render;
  135.     p_vout->pf_display = Display;
  136.     return VLC_SUCCESS;
  137. }
  138. /*****************************************************************************
  139.  * Init: initialize libcaca video output thread
  140.  *****************************************************************************/
  141. static int Init( vout_thread_t *p_vout )
  142. {
  143.     int i_index;
  144.     picture_t *p_pic = NULL;
  145.     I_OUTPUTPICTURES = 0;
  146.     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
  147.     p_vout->output.i_width = p_vout->render.i_width;
  148.     p_vout->output.i_height = p_vout->render.i_height;
  149.     p_vout->output.i_aspect = p_vout->render.i_aspect;
  150.     p_vout->output.i_rmask = 0x00ff0000;
  151.     p_vout->output.i_gmask = 0x0000ff00;
  152.     p_vout->output.i_bmask = 0x000000ff;
  153.     /* Create the libcaca bitmap */
  154.     p_vout->p_sys->p_bitmap =
  155.         caca_create_bitmap( 32,
  156.                             p_vout->output.i_width,
  157.                             p_vout->output.i_height,
  158.                             4 * ((p_vout->output.i_width + 15) & ~15),
  159.                             p_vout->output.i_rmask,
  160.                             p_vout->output.i_gmask,
  161.                             p_vout->output.i_bmask,
  162.                             0x00000000 );
  163.     if( !p_vout->p_sys->p_bitmap )
  164.     {
  165.         msg_Err( p_vout, "could not create libcaca bitmap" );
  166.         return VLC_EGENERIC;
  167.     }
  168.     /* Find an empty picture slot */
  169.     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
  170.     {
  171.         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
  172.         {
  173.             p_pic = p_vout->p_picture + i_index;
  174.             break;
  175.         }
  176.     }
  177.     if( p_pic == NULL )
  178.     {
  179.         return VLC_EGENERIC;
  180.     }
  181.     /* Allocate the picture */
  182.     p_pic->p->i_lines = p_vout->output.i_height;
  183.     p_pic->p->i_visible_lines = p_vout->output.i_height;
  184.     p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
  185.     p_pic->p->i_pixel_pitch = 4;
  186.     p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
  187.     p_pic->i_planes = 1;
  188.     p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );
  189.     p_pic->i_status = DESTROYED_PICTURE;
  190.     p_pic->i_type   = DIRECT_PICTURE;
  191.     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
  192.     I_OUTPUTPICTURES++;
  193.     return VLC_SUCCESS;
  194. }
  195. /*****************************************************************************
  196.  * End: terminate libcaca video output thread
  197.  *****************************************************************************/
  198. static void End( vout_thread_t *p_vout )
  199. {
  200.     caca_free_bitmap( p_vout->p_sys->p_bitmap );
  201. }
  202. /*****************************************************************************
  203.  * Destroy: destroy libcaca video output thread
  204.  *****************************************************************************
  205.  * Terminate an output method created by AaCreateOutputMethod
  206.  *****************************************************************************/
  207. static void Destroy( vlc_object_t *p_this )
  208. {
  209.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  210.     caca_end();
  211. #if defined( WIN32 ) && !defined( UNDER_CE )
  212.     FreeConsole();
  213. #endif
  214.     free( p_vout->p_sys );
  215. }
  216. /*****************************************************************************
  217.  * Manage: handle libcaca events
  218.  *****************************************************************************
  219.  * This function should be called regularly by video output thread. It manages
  220.  * console events. It returns a non null value on error.
  221.  *****************************************************************************/
  222. static int Manage( vout_thread_t *p_vout )
  223. {
  224.     int event;
  225.     vlc_value_t val;
  226.     while(( event = caca_get_event(CACA_EVENT_KEY_PRESS | CACA_EVENT_RESIZE) ))
  227.     {
  228.         if( event == CACA_EVENT_RESIZE )
  229.         {
  230.             /* Acknowledge the resize */
  231.             caca_refresh();
  232.             continue;
  233.         }
  234.         switch( event & 0x00ffffff )
  235.         {
  236.         case 'q':
  237.             val.i_int = KEY_MODIFIER_CTRL | 'q';
  238.             break;
  239.         case ' ':
  240.             val.i_int = KEY_SPACE;
  241.             break;
  242.         default:
  243.             continue;
  244.         }
  245.         var_Set( p_vout->p_vlc, "key-pressed", val );
  246.     }
  247.     return VLC_SUCCESS;
  248. }
  249. /*****************************************************************************
  250.  * Render: render previously calculated output
  251.  *****************************************************************************/
  252. static void Render( vout_thread_t *p_vout, picture_t *p_pic )
  253. {
  254.     caca_clear();
  255.     caca_draw_bitmap( 0, 0, caca_get_width() - 1, caca_get_height() - 1,
  256.                       p_vout->p_sys->p_bitmap, p_pic->p->p_pixels );
  257. }
  258. /*****************************************************************************
  259.  * Display: displays previously rendered output
  260.  *****************************************************************************/
  261. static void Display( vout_thread_t *p_vout, picture_t *p_pic )
  262. {
  263.     caca_refresh();
  264. }