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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * xosd.c : X On Screen Display interface
  3.  *****************************************************************************
  4.  * Copyright (C) 2001 VideoLAN
  5.  * $Id: xosd.c 7542 2004-04-28 18:22:31Z zorglub $
  6.  *
  7.  * Authors: Lo颿 Minier <lool@videolan.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 <stdlib.h>                                      /* malloc(), free() */
  27. #include <string.h>
  28. #include <xosd.h>
  29. #include <vlc/intf.h>
  30. #ifdef HAVE_UNISTD_H
  31. #    include <unistd.h>
  32. #endif
  33. /*****************************************************************************
  34.  * intf_sys_t: description and status of rc interface
  35.  *****************************************************************************/
  36. struct intf_sys_t
  37. {
  38.     xosd * p_osd;               /* libxosd handle */
  39.     vlc_bool_t  b_need_update;   /* Update display ? */
  40. };
  41. #define MAX_LINE_LENGTH 256
  42. /*****************************************************************************
  43.  * Local prototypes.
  44.  *****************************************************************************/
  45. static int  Open         ( vlc_object_t * );
  46. static void Close        ( vlc_object_t * );
  47. static void Run          ( intf_thread_t * );
  48. static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
  49.                 vlc_value_t oval, vlc_value_t nval, void *param );
  50. /*****************************************************************************
  51.  * Module descriptor
  52.  *****************************************************************************/
  53. #define POSITION_TEXT N_("Flip vertical position")
  54. #define POSITION_LONGTEXT N_("Display xosd output on the bottom of the " 
  55.                              "screen instead of the top")
  56. #define TXT_OFS_TEXT N_("Vertical offset")
  57. #define TXT_OFS_LONGTEXT N_("Vertical offset in pixels of the displayed text")
  58. #define SHD_OFS_TEXT N_("Shadow offset")
  59. #define SHD_OFS_LONGTEXT N_("Offset in pixels of the shadow")
  60. #define FONT_TEXT N_("Font")
  61. #define FONT_LONGTEXT N_("Font used to display text in the xosd output")
  62. /* FIXME FIXME FIXME: Gettextize */
  63. #define COLOUR_TEXT ("Colour")
  64. #define COLOUR_LONGTEXT ("Colour used to display text in the xosd output")
  65. vlc_module_begin();
  66.     set_description( _("XOSD interface") );
  67.     add_bool( "xosd-position", 1, NULL, POSITION_TEXT, POSITION_LONGTEXT, VLC_TRUE );
  68.     add_integer( "xosd-text-offset", 30, NULL, TXT_OFS_TEXT, TXT_OFS_LONGTEXT, VLC_TRUE );
  69.     add_integer( "xosd-shadow-offset", 2, NULL,
  70.                  SHD_OFS_TEXT, SHD_OFS_LONGTEXT, VLC_TRUE );
  71.     add_string( "xosd-font",
  72.                 "-adobe-helvetica-bold-r-normal-*-*-160-*-*-p-*-iso8859-1",
  73.                 NULL, FONT_TEXT, FONT_LONGTEXT, VLC_TRUE );
  74.     add_string( "xosd-colour", "LawnGreen",
  75.                     NULL, COLOUR_TEXT, COLOUR_LONGTEXT, VLC_TRUE );
  76.     set_capability( "interface", 10 );
  77.     set_callbacks( Open, Close );
  78. vlc_module_end();
  79. /*****************************************************************************
  80.  * Open: initialize and create stuff
  81.  *****************************************************************************/
  82. static int Open( vlc_object_t *p_this )
  83. {
  84.     intf_thread_t *p_intf = (intf_thread_t *)p_this;
  85.     /* Allocate instance and initialize some members */
  86.     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
  87.     if( p_intf->p_sys == NULL )
  88.     {
  89.         msg_Err( p_intf, "out of memory" );
  90.         return VLC_ENOMEM;
  91.     }
  92.     if( getenv( "DISPLAY" ) == NULL )
  93.     {
  94.         msg_Err( p_intf, "no display, please set the DISPLAY variable" );
  95.         return VLC_EGENERIC;
  96.     }
  97.     /* Initialize library */
  98.     p_intf->p_sys->p_osd =
  99. #if defined(HAVE_XOSD_VERSION_0) || defined(HAVE_XOSD_VERSION_1)
  100.         xosd_init( config_GetPsz( p_intf, "xosd-font" ),
  101.                    config_GetPsz( p_intf,"xosd-colour" ), 3,
  102.                    XOSD_top, 0, 1 );
  103. #else
  104.         xosd_init( config_GetPsz( p_intf, "xosd-font" ),
  105.                    config_GetPsz( p_intf,"xosd-colour" ), 3,
  106.                     XOSD_top, 0, 0, 1 );
  107. #endif
  108.     if( p_intf->p_sys->p_osd == NULL )
  109.     {
  110.         msg_Err( p_intf, "couldn't initialize libxosd" );
  111.         return VLC_EGENERIC;
  112.     }
  113.     playlist_t *p_playlist =
  114.             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
  115.                                            FIND_ANYWHERE );
  116.     if( p_playlist == NULL )
  117.     {
  118.         return VLC_EGENERIC;
  119.     }
  120.     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, p_this );
  121.     var_AddCallback( p_playlist, "item-change", PlaylistNext, p_this );
  122.     vlc_object_release( p_playlist );
  123.     /* Set user preferences */
  124.     xosd_set_font( p_intf->p_sys->p_osd,
  125.                     config_GetPsz( p_intf, "xosd-font" ) );
  126.     xosd_set_outline_colour( p_intf->p_sys->p_osd,"black" );
  127. #ifdef HAVE_XOSD_VERSION_2
  128.     xosd_set_horizontal_offset( p_intf->p_sys->p_osd,
  129.                     config_GetInt( p_intf, "xosd-text-offset" ) );
  130.     xosd_set_vertical_offset( p_intf->p_sys->p_osd,
  131.                     config_GetInt( p_intf, "xosd-text-offset" ) );
  132. #else
  133.     xosd_set_offset( p_intf->p_sys->p_osd,
  134.                     config_GetInt( p_intf, "xosd-text-offset" ) );
  135. #endif
  136.     xosd_set_shadow_offset( p_intf->p_sys->p_osd,
  137.                     config_GetInt( p_intf, "xosd-shadow-offset" ));
  138.     xosd_set_pos( p_intf->p_sys->p_osd,
  139.                     config_GetInt( p_intf, "xosd-position" ) ?
  140.                                          XOSD_bottom: XOSD_top );
  141.     /* Initialize to NULL */
  142.     xosd_display( p_intf->p_sys->p_osd,
  143.                   0,
  144.                   XOSD_string,
  145.                   "xosd interface initialized" );
  146.     p_intf->pf_run = Run;
  147.     p_intf->p_sys->b_need_update = VLC_TRUE;
  148.     return VLC_SUCCESS;
  149. }
  150. /*****************************************************************************
  151.  * Close: destroy interface stuff
  152.  *****************************************************************************/
  153. static void Close( vlc_object_t *p_this )
  154. {
  155.     intf_thread_t *p_intf = (intf_thread_t *)p_this;
  156.     /* Uninitialize library */
  157.     xosd_destroy( p_intf->p_sys->p_osd );
  158.     /* Destroy structure */
  159.     free( p_intf->p_sys );
  160. }
  161. /*****************************************************************************
  162.  * Run: xosd thread
  163.  *****************************************************************************
  164.  * This part of the interface runs in a separate thread
  165.  *****************************************************************************/
  166. static void Run( intf_thread_t *p_intf )
  167. {
  168.     int i_size,i_index;
  169.     playlist_t *p_playlist;
  170.     playlist_item_t *p_item = NULL;
  171.     input_item_t item;
  172.     char psz_duration[MSTRTIME_MAX_SIZE+2];
  173.     char *psz_display = NULL;
  174.     while( !p_intf->b_die )
  175.     {
  176.         if( p_intf->p_sys->b_need_update == VLC_TRUE )
  177.         {
  178.             p_intf->p_sys->b_need_update = VLC_FALSE;
  179.             p_playlist = (playlist_t *)vlc_object_find( p_intf,
  180.                                       VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
  181.             if( !p_playlist )
  182.             {
  183.                 continue;
  184.             }
  185.             if( p_playlist->i_size < 0 || p_playlist->i_index < 0 )
  186.             {
  187.                 vlc_object_release( p_playlist );
  188.                 continue;
  189.             }
  190.             if( psz_display )
  191.             {
  192.                 free( psz_display );
  193.                 psz_display = NULL;
  194.             }
  195.             if( p_playlist->i_status == PLAYLIST_STOPPED )
  196.             {
  197.                 psz_display = (char *)malloc( sizeof(char )*strlen(_("Stop")));
  198.                 sprintf( psz_display,_("Stop") );
  199.                 vlc_object_release( p_playlist );
  200.             }
  201.             else if( p_playlist->i_status == PLAYLIST_PAUSED )
  202.             {
  203.                 psz_display = (char *)malloc( sizeof(char )*strlen(_("Pause")));
  204.                 sprintf( psz_display,_("Pause") );
  205.                 vlc_object_release( p_playlist );
  206.             }
  207.             else
  208.             {
  209.     //           vlc_mutex_lock(&p_playlist->object_lock );
  210.                  p_item = playlist_ItemGetByPos( p_playlist,
  211.                                  p_playlist->i_index );
  212.                 item = p_item->input;
  213.                 if( !p_item )
  214.                 {
  215.                     vlc_object_release( p_playlist );
  216.      //            vlc_mutex_unlock(&p_playlist->object_lock );
  217.                     continue;
  218.                 }
  219.                 i_size = p_playlist->i_size;
  220.                 i_index = p_playlist->i_index+1;
  221.     //            vlc_mutex_unlock(&p_playlist->object_lock );
  222.                 vlc_object_release( p_playlist );
  223.                 if( item.i_duration != -1 )
  224.                 {
  225.                     char psz_durationstr[MSTRTIME_MAX_SIZE];
  226.                     secstotimestr( psz_durationstr, item.i_duration/1000000 );
  227.                     sprintf( psz_duration, "(%s)", psz_durationstr );
  228.                 }
  229.                 else
  230.                 {
  231.                     sprintf( psz_duration," " );
  232.                 }
  233.                 psz_display = (char *)malloc( sizeof(char )*
  234.                                           (strlen( item.psz_name ) +
  235.                                           MSTRTIME_MAX_SIZE + 2+6 + 10 +10 ));
  236.                 sprintf( psz_display," %i/%i - %s %s",
  237.                          i_index,i_size, item.psz_name, psz_duration);
  238.             }
  239.             /* Display */
  240.             xosd_display( p_intf->p_sys->p_osd,
  241.                             0,                               /* first line */
  242.                             XOSD_string,
  243.                             psz_display );
  244.         }
  245.         msleep( INTF_IDLE_SLEEP );
  246.     }
  247. }
  248. static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
  249.                 vlc_value_t oval, vlc_value_t nval, void *param )
  250. {
  251.     intf_thread_t *p_intf = (intf_thread_t *)param;
  252.     p_intf->p_sys->b_need_update = VLC_TRUE;
  253.     return VLC_SUCCESS;
  254. }