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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * caca.c: Color ASCII Art video output plugin using libcaca
  3.  *****************************************************************************
  4.  * Copyright (C) 2003-2009 the VideoLAN team
  5.  * $Id: 63e437f26379b75a97d17b34263c03502d217d0a $
  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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <vlc_common.h>
  30. #include <vlc_plugin.h>
  31. #include <vlc_vout.h>
  32. #include <vlc_interface.h>
  33. #include <vlc_playlist.h>
  34. #include <vlc_keys.h>
  35. #include <caca.h>
  36. #ifndef CACA_API_VERSION_1
  37.     /* Upward compatibility macros */
  38.     typedef char cucul_canvas_t;
  39.     typedef struct caca_bitmap cucul_dither_t;
  40.     typedef char caca_display_t;
  41. #   define CUCUL_COLOR_DEFAULT CACA_COLOR_LIGHTGRAY
  42. #   define CUCUL_COLOR_BLACK CACA_COLOR_BLACK
  43. #   define cucul_clear_canvas(x) caca_clear()
  44. #   define cucul_create_canvas(x,y) "" /* kinda hacky */
  45. #   define cucul_create_dither caca_create_bitmap
  46. #   define cucul_dither_bitmap(x,y,z,t,u,v,w) caca_draw_bitmap(y,z,t,u,v,w)
  47. #   define cucul_free_dither caca_free_bitmap
  48. #   define cucul_free_canvas(x)
  49. #   define cucul_get_canvas_width(x) caca_get_width()
  50. #   define cucul_get_canvas_height(x) caca_get_height()
  51. #   define cucul_set_color(x,y,z) caca_set_color(y,z)
  52. #   define caca_create_display(x) (caca_init() ? NULL : "") /* hacky, too */
  53. #   define caca_free_display(x) caca_end()
  54. #   define caca_get_event(x,y,z,t) *(z) = caca_get_event(y)
  55. #   define caca_refresh_display(x) caca_refresh()
  56. #   define caca_set_display_title(x,y) caca_set_window_title(y)
  57. #endif
  58. /*****************************************************************************
  59.  * Local prototypes
  60.  *****************************************************************************/
  61. static int  Create    ( vlc_object_t * );
  62. static void Destroy   ( vlc_object_t * );
  63. static int  Init      ( vout_thread_t * );
  64. static void End       ( vout_thread_t * );
  65. static int  Manage    ( vout_thread_t * );
  66. static void Render    ( vout_thread_t *, picture_t * );
  67. static void Display   ( vout_thread_t *, picture_t * );
  68. /*****************************************************************************
  69.  * Module descriptor
  70.  *****************************************************************************/
  71. vlc_module_begin ()
  72.     set_shortname( "Caca" )
  73.     set_category( CAT_VIDEO )
  74.     set_subcategory( SUBCAT_VIDEO_VOUT )
  75.     set_description( N_("Color ASCII art video output") )
  76.     set_capability( "video output", 12 )
  77.     set_callbacks( Create, Destroy )
  78. vlc_module_end ()
  79. /*****************************************************************************
  80.  * vout_sys_t: libcaca video output method descriptor
  81.  *****************************************************************************
  82.  * This structure is part of the video output thread descriptor.
  83.  * It describes the libcaca specific properties of an output thread.
  84.  *****************************************************************************/
  85. struct vout_sys_t
  86. {
  87.     cucul_canvas_t *p_cv;
  88.     caca_display_t *p_dp;
  89.     cucul_dither_t *p_dither;
  90. };
  91. /*****************************************************************************
  92.  * Create: allocates libcaca video output thread
  93.  *****************************************************************************
  94.  * This function initializes libcaca vout method.
  95.  *****************************************************************************/
  96. static int Create( vlc_object_t *p_this )
  97. {
  98.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  99. #if defined( WIN32 ) && !defined( UNDER_CE )
  100.     CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
  101.     SMALL_RECT rect;
  102.     COORD coord;
  103.     HANDLE hstdout;
  104.     if( !AllocConsole() )
  105.     {
  106.         msg_Err( p_vout, "cannot create console" );
  107.         return VLC_EGENERIC;
  108.     }
  109.     hstdout =
  110.         CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE,
  111.                                    FILE_SHARE_READ | FILE_SHARE_WRITE,
  112.                                    NULL, CONSOLE_TEXTMODE_BUFFER, NULL );
  113.     if( !hstdout || hstdout == INVALID_HANDLE_VALUE )
  114.     {
  115.         msg_Err( p_vout, "cannot create screen buffer" );
  116.         FreeConsole();
  117.         return VLC_EGENERIC;
  118.     }
  119.     if( !SetConsoleActiveScreenBuffer( hstdout) )
  120.     {
  121.         msg_Err( p_vout, "cannot set active screen buffer" );
  122.         FreeConsole();
  123.         return VLC_EGENERIC;
  124.     }
  125.     coord = GetLargestConsoleWindowSize( hstdout );
  126.     msg_Dbg( p_vout, "SetConsoleWindowInfo: %ix%i", coord.X, coord.Y );
  127.     /* Force size for now */
  128.     coord.X = 100;
  129.     coord.Y = 40;
  130.     if( !SetConsoleScreenBufferSize( hstdout, coord ) )
  131.         msg_Warn( p_vout, "SetConsoleScreenBufferSize %i %i",
  132.                   coord.X, coord.Y );
  133.     /* Get the current screen buffer size and window position. */
  134.     if( GetConsoleScreenBufferInfo( hstdout, &csbiInfo ) )
  135.     {
  136.         rect.Top = 0; rect.Left = 0;
  137.         rect.Right = csbiInfo.dwMaximumWindowSize.X - 1;
  138.         rect.Bottom = csbiInfo.dwMaximumWindowSize.Y - 1;
  139.         if( !SetConsoleWindowInfo( hstdout, TRUE, &rect ) )
  140.             msg_Dbg( p_vout, "SetConsoleWindowInfo failed: %ix%i",
  141.                      rect.Right, rect.Bottom );
  142.     }
  143. #endif
  144.     /* Allocate structure */
  145.     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
  146.     if( p_vout->p_sys == NULL )
  147.     {
  148. #if defined( WIN32 ) && !defined( UNDER_CE )
  149.         FreeConsole();
  150. #endif
  151.         return VLC_ENOMEM;
  152.     }
  153.     p_vout->p_sys->p_cv = cucul_create_canvas(0, 0);
  154.     if( !p_vout->p_sys->p_cv )
  155.     {
  156.         msg_Err( p_vout, "cannot initialize libcucul" );
  157. #if defined( WIN32 ) && !defined( UNDER_CE )
  158.         FreeConsole();
  159. #endif
  160.         free( p_vout->p_sys );
  161.         return VLC_EGENERIC;
  162.     }
  163.     p_vout->p_sys->p_dp = caca_create_display( p_vout->p_sys->p_cv );
  164.     if( !p_vout->p_sys->p_dp )
  165.     {
  166.         msg_Err( p_vout, "cannot initialize libcaca" );
  167.         cucul_free_canvas( p_vout->p_sys->p_cv );
  168. #if defined( WIN32 ) && !defined( UNDER_CE )
  169.         FreeConsole();
  170. #endif
  171.         free( p_vout->p_sys );
  172.         return VLC_EGENERIC;
  173.     }
  174.     caca_set_display_title( p_vout->p_sys->p_dp,
  175.                             VOUT_TITLE " - Colour AsCii Art (caca)" );
  176.     p_vout->pf_init = Init;
  177.     p_vout->pf_end = End;
  178.     p_vout->pf_manage = Manage;
  179.     p_vout->pf_render = Render;
  180.     p_vout->pf_display = Display;
  181.     return VLC_SUCCESS;
  182. }
  183. /*****************************************************************************
  184.  * Init: initialize libcaca video output thread
  185.  *****************************************************************************/
  186. static int Init( vout_thread_t *p_vout )
  187. {
  188.     int i_index;
  189.     picture_t *p_pic = NULL;
  190.     I_OUTPUTPICTURES = 0;
  191.     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
  192.     p_vout->output.i_width = p_vout->render.i_width;
  193.     p_vout->output.i_height = p_vout->render.i_height;
  194.     p_vout->output.i_aspect = p_vout->render.i_aspect;
  195.     p_vout->output.i_rmask = 0x00ff0000;
  196.     p_vout->output.i_gmask = 0x0000ff00;
  197.     p_vout->output.i_bmask = 0x000000ff;
  198.     /* Create the libcaca dither object */
  199.     p_vout->p_sys->p_dither = cucul_create_dither
  200.                        ( 32, p_vout->output.i_width, p_vout->output.i_height,
  201.                          4 * ((p_vout->output.i_width + 15) & ~15),
  202.                          p_vout->output.i_rmask, p_vout->output.i_gmask,
  203.                          p_vout->output.i_bmask, 0x00000000 );
  204.     if( !p_vout->p_sys->p_dither )
  205.     {
  206.         msg_Err( p_vout, "could not create libcaca dither object" );
  207.         return VLC_EGENERIC;
  208.     }
  209.     /* Find an empty picture slot */
  210.     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
  211.     {
  212.         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
  213.         {
  214.             p_pic = p_vout->p_picture + i_index;
  215.             break;
  216.         }
  217.     }
  218.     if( p_pic == NULL )
  219.     {
  220.         return VLC_EGENERIC;
  221.     }
  222.     /* Allocate the picture */
  223.     p_pic->p->i_lines = p_vout->output.i_height;
  224.     p_pic->p->i_visible_lines = p_vout->output.i_height;
  225.     p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
  226.     p_pic->p->i_pixel_pitch = 4;
  227.     p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
  228.     p_pic->i_planes = 1;
  229.     p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );
  230.     p_pic->i_status = DESTROYED_PICTURE;
  231.     p_pic->i_type   = DIRECT_PICTURE;
  232.     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
  233.     I_OUTPUTPICTURES++;
  234.     return VLC_SUCCESS;
  235. }
  236. /*****************************************************************************
  237.  * End: terminate libcaca video output thread
  238.  *****************************************************************************/
  239. static void End( vout_thread_t *p_vout )
  240. {
  241.     cucul_free_dither( p_vout->p_sys->p_dither );
  242. }
  243. /*****************************************************************************
  244.  * Destroy: destroy libcaca video output thread
  245.  *****************************************************************************
  246.  * Terminate an output method created by AaCreateOutputMethod
  247.  *****************************************************************************/
  248. static void Destroy( vlc_object_t *p_this )
  249. {
  250.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  251.     caca_free_display( p_vout->p_sys->p_dp );
  252.     cucul_free_canvas( p_vout->p_sys->p_cv );
  253. #if defined( WIN32 ) && !defined( UNDER_CE )
  254.     FreeConsole();
  255. #endif
  256.     free( p_vout->p_sys );
  257. }
  258. /*****************************************************************************
  259.  * Manage: handle libcaca events
  260.  *****************************************************************************
  261.  * This function should be called regularly by video output thread. It manages
  262.  * console events. It returns a non null value on error.
  263.  *****************************************************************************/
  264. static int Manage( vout_thread_t *p_vout )
  265. {
  266. #ifdef CACA_API_VERSION_1
  267.     struct caca_event ev;
  268. #else
  269.     int ev;
  270. #endif
  271.     while( caca_get_event(p_vout->p_sys->p_dp, CACA_EVENT_ANY, &ev, 0) )
  272.     {
  273.         playlist_t *p_playlist;
  274.         vlc_value_t val;
  275. #ifdef CACA_API_VERSION_1
  276. #ifdef CACA_EVENT_OPAQUE
  277.         switch( caca_get_event_type( &ev ) )
  278. #else
  279.         switch( ev.type )
  280. #endif /* CACA_EVENT_OPAQUE */
  281. #else
  282.         switch( ev )
  283. #endif
  284.         {
  285.         case CACA_EVENT_KEY_RELEASE:
  286. #ifdef CACA_API_VERSION_1
  287. #ifdef CACA_EVENT_OPAQUE
  288.             switch( caca_get_event_key_ch( &ev ) )
  289. #else
  290.             switch( ev.data.key.ch )
  291. #endif /* CACA_EVENT_OPAQUE */
  292. #else
  293.             switch( ev & 0x00ffffff )
  294. #endif
  295.             {
  296.             case 'q':
  297.                 val.i_int = KEY_MODIFIER_CTRL | 'q';
  298.                 break;
  299.             case ' ':
  300.                 val.i_int = KEY_SPACE;
  301.                 break;
  302.             default:
  303.                 continue;
  304.             }
  305.             var_Set( p_vout->p_libvlc, "key-pressed", val );
  306.             break;
  307.         case CACA_EVENT_RESIZE:
  308.             /* Acknowledge the resize */
  309.             caca_refresh_display( p_vout->p_sys->p_dp );
  310.             break;
  311. #ifdef CACA_API_VERSION_1
  312.         case  CACA_EVENT_MOUSE_MOTION:
  313.             val.i_int =
  314. #ifdef CACA_EVENT_OPAQUE
  315.                 caca_get_event_mouse_x( &ev )
  316. #else
  317.                 ev.data.mouse.x
  318. #endif /* CACA_EVENT_OPAQUE */
  319.                 * p_vout->render.i_width
  320.                          / cucul_get_canvas_width( p_vout->p_sys->p_cv );
  321.             var_Set( p_vout, "mouse-x", val );
  322.             val.i_int =
  323. #ifdef CACA_EVENT_OPAQUE
  324.                 caca_get_event_mouse_y( &ev ) 
  325. #else
  326.                 ev.data.mouse.y
  327. #endif /* CACA_EVENT_OPAQUE */
  328.                 * p_vout->render.i_height
  329.                          / cucul_get_canvas_height( p_vout->p_sys->p_cv );
  330.             var_Set( p_vout, "mouse-y", val );
  331.             var_SetBool( p_vout, "mouse-moved", true );
  332.             break;
  333.         case CACA_EVENT_MOUSE_RELEASE:
  334.             var_SetBool( p_vout, "mouse-clicked", true );
  335.             break;
  336.         case CACA_EVENT_QUIT:
  337.         {
  338.             p_playlist = pl_Hold( p_vout );
  339.             if( p_playlist )
  340.             {
  341.                 playlist_Stop( p_playlist );
  342.                 pl_Release( p_vout );
  343.             }
  344.             libvlc_Quit( p_vout->p_libvlc );
  345.             break;
  346.         }
  347. #endif
  348.         default:
  349.             break;
  350.         }
  351.     }
  352.     return VLC_SUCCESS;
  353. }
  354. /*****************************************************************************
  355.  * Render: render previously calculated output
  356.  *****************************************************************************/
  357. static void Render( vout_thread_t *p_vout, picture_t *p_pic )
  358. {
  359.     cucul_set_color_ansi( p_vout->p_sys->p_cv,
  360.                      CUCUL_COLOR_DEFAULT, CUCUL_COLOR_BLACK );
  361.     cucul_clear_canvas( p_vout->p_sys->p_cv );
  362.     cucul_dither_bitmap( p_vout->p_sys->p_cv, 0, 0,
  363.                          cucul_get_canvas_width( p_vout->p_sys->p_cv ) - 1,
  364.                          cucul_get_canvas_height( p_vout->p_sys->p_cv ) - 1,
  365.                          p_vout->p_sys->p_dither, p_pic->p->p_pixels );
  366. }
  367. /*****************************************************************************
  368.  * Display: displays previously rendered output
  369.  *****************************************************************************/
  370. static void Display( vout_thread_t *p_vout, picture_t *p_pic )
  371. {
  372.     VLC_UNUSED(p_pic);
  373.     caca_refresh_display( p_vout->p_sys->p_dp );
  374. }