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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * timer.cpp : wxWindows plugin for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2000-2003 VideoLAN
  5.  * $Id: timer.cpp 8966 2004-10-10 10:08:44Z ipkiss $
  6.  *
  7.  * Authors: Gildas Bazin <gbazin@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 <errno.h>                                                 /* ENOMEM */
  28. #include <string.h>                                            /* strerror() */
  29. #include <stdio.h>
  30. #include <vlc/vlc.h>
  31. #include <vlc/aout.h>
  32. #include <vlc/intf.h>
  33. #include "wxwindows.h"
  34. #include <wx/timer.h>
  35. //void DisplayStreamDate( wxControl *, intf_thread_t *, int );
  36. /* Callback prototypes */
  37. static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
  38.                         vlc_value_t old_val, vlc_value_t new_val, void *param );
  39. static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
  40.                        vlc_value_t old_val, vlc_value_t new_val, void *param );
  41. /*****************************************************************************
  42.  * Constructor.
  43.  *****************************************************************************/
  44. Timer::Timer( intf_thread_t *_p_intf, Interface *_p_main_interface )
  45. {
  46.     p_intf = _p_intf;
  47.     p_main_interface = _p_main_interface;
  48.     b_init = 0;
  49.     i_old_playing_status = PAUSE_S;
  50.     i_old_rate = INPUT_RATE_DEFAULT;
  51.     /* Register callback for the intf-popupmenu variable */
  52.     playlist_t *p_playlist =
  53.         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
  54.                                        FIND_ANYWHERE );
  55.     if( p_playlist != NULL )
  56.     {
  57.         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
  58.         var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
  59.         vlc_object_release( p_playlist );
  60.     }
  61.     Start( 100 /*milliseconds*/, wxTIMER_CONTINUOUS );
  62. }
  63. Timer::~Timer()
  64. {
  65.     /* Unregister callback */
  66.     playlist_t *p_playlist =
  67.         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
  68.                                        FIND_ANYWHERE );
  69.     if( p_playlist != NULL )
  70.     {
  71.         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
  72.         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
  73.         vlc_object_release( p_playlist );
  74.     }
  75. }
  76. /*****************************************************************************
  77.  * Private methods.
  78.  *****************************************************************************/
  79. /*****************************************************************************
  80.  * Manage: manage main thread messages
  81.  *****************************************************************************
  82.  * In this function, called approx. 10 times a second, we check what the
  83.  * main program wanted to tell us.
  84.  *****************************************************************************/
  85. void Timer::Notify()
  86. {
  87. #if defined( __WXMSW__ ) /* Work-around a bug with accelerators */
  88.     if( !b_init )
  89.     {
  90.         p_main_interface->Init();
  91.         b_init = VLC_TRUE;
  92.     }
  93. #endif
  94.     vlc_mutex_lock( &p_intf->change_lock );
  95.     /* Update the input */
  96.     if( p_intf->p_sys->p_input == NULL )
  97.     {
  98.         p_intf->p_sys->p_input =
  99.             (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
  100.                                                FIND_ANYWHERE );
  101.         /* Refresh interface */
  102.         if( p_intf->p_sys->p_input )
  103.         {
  104.             p_main_interface->slider->SetValue( 0 );
  105.             b_old_seekable = VLC_FALSE;
  106.             p_main_interface->statusbar->SetStatusText(
  107.                 wxU(p_intf->p_sys->p_input->input.p_item->psz_name), 2 );
  108.             p_main_interface->TogglePlayButton( PLAYING_S );
  109.             i_old_playing_status = PLAYING_S;
  110.         }
  111.     }
  112.     else if( p_intf->p_sys->p_input->b_dead )
  113.     {
  114.         /* Hide slider */
  115.         p_main_interface->slider_frame->Hide();
  116.         p_main_interface->frame_sizer->Hide(
  117.             p_main_interface->slider_frame );
  118.         p_main_interface->frame_sizer->Layout();
  119.         p_main_interface->frame_sizer->Fit( p_main_interface );
  120.         p_main_interface->TogglePlayButton( PAUSE_S );
  121.         i_old_playing_status = PAUSE_S;
  122.         p_main_interface->statusbar->SetStatusText( wxT(""), 0 );
  123.         p_main_interface->statusbar->SetStatusText( wxT(""), 2 );
  124.         vlc_object_release( p_intf->p_sys->p_input );
  125.         p_intf->p_sys->p_input = NULL;
  126.     }
  127.     if( p_intf->p_sys->p_input )
  128.     {
  129.         input_thread_t *p_input = p_intf->p_sys->p_input;
  130.         vlc_value_t val;
  131.         if( !p_input->b_die )
  132.         {
  133.             vlc_value_t pos;
  134.             /* New input or stream map change */
  135.             p_intf->p_sys->b_playing = 1;
  136.             /* Manage the slider */
  137.             /* FIXME --fenrir */
  138.             /* Change the name of b_old_seekable into b_show_bar or something like that */
  139.             var_Get( p_input, "position", &pos );
  140.             if( !b_old_seekable )
  141.             {
  142.                 if( pos.f_float > 0.0 )
  143.                 {
  144.                     /* Done like this, as it's the only way to know if the slider
  145.                      * has to be displayed */
  146.                     b_old_seekable = VLC_TRUE;
  147.                     p_main_interface->slider_frame->Show();
  148.                     p_main_interface->frame_sizer->Show(
  149.                         p_main_interface->slider_frame );
  150.                     p_main_interface->frame_sizer->Layout();
  151.                     p_main_interface->frame_sizer->Fit( p_main_interface );
  152.                 }
  153.             }
  154.             if( p_intf->p_sys->b_playing && b_old_seekable )
  155.             {
  156.                 /* Update the slider if the user isn't dragging it. */
  157.                 if( p_intf->p_sys->b_slider_free )
  158.                 {
  159.                     char psz_time[ MSTRTIME_MAX_SIZE ];
  160.                     char psz_total[ MSTRTIME_MAX_SIZE ];
  161.                     vlc_value_t time;
  162.                     mtime_t i_seconds;
  163.                     /* Update the value */
  164.                     if( pos.f_float >= 0.0 )
  165.                     {
  166.                         p_intf->p_sys->i_slider_pos =
  167.                             (int)(SLIDER_MAX_POS * pos.f_float);
  168.                         p_main_interface->slider->SetValue(
  169.                             p_intf->p_sys->i_slider_pos );
  170.                         var_Get( p_intf->p_sys->p_input, "time", &time );
  171.                         i_seconds = time.i_time / 1000000;
  172.                         secstotimestr ( psz_time, i_seconds );
  173.                         var_Get( p_intf->p_sys->p_input, "length",  &time );
  174.                         i_seconds = time.i_time / 1000000;
  175.                         secstotimestr ( psz_total, i_seconds );
  176.                         p_main_interface->statusbar->SetStatusText(
  177.                             wxU(psz_time) + wxString(wxT(" / ")) +
  178.                             wxU(psz_total), 0 );
  179.                     }
  180.                 }
  181.             }
  182. #if 0
  183.         vlc_mutex_lock( &p_input->stream.stream_lock );
  184.             if( p_intf->p_sys->p_input->stream.b_seekable && !b_old_seekable )
  185.             {
  186.                 /* Done like this because b_seekable is set slightly after
  187.                  * the new input object is available. */
  188.                 b_old_seekable = VLC_TRUE;
  189.                 p_main_interface->slider_frame->Show();
  190.                 p_main_interface->frame_sizer->Show(
  191.                     p_main_interface->slider_frame );
  192.                 p_main_interface->frame_sizer->Layout();
  193.                 p_main_interface->frame_sizer->Fit( p_main_interface );
  194.             }
  195.             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
  196.             {
  197.                 /* Update the slider if the user isn't dragging it. */
  198.                 if( p_intf->p_sys->b_slider_free )
  199.                 {
  200.                     vlc_value_t pos;
  201.                     char psz_time[ MSTRTIME_MAX_SIZE ];
  202.                     char psz_total[ MSTRTIME_MAX_SIZE ];
  203.                     vlc_value_t time;
  204.                     mtime_t i_seconds;
  205.                     /* Update the value */
  206.                     var_Get( p_input, "position", &pos );
  207.                     if( pos.f_float >= 0.0 )
  208.                     {
  209.                         p_intf->p_sys->i_slider_pos =
  210.                             (int)(SLIDER_MAX_POS * pos.f_float);
  211.                         p_main_interface->slider->SetValue(
  212.                             p_intf->p_sys->i_slider_pos );
  213.                         var_Get( p_intf->p_sys->p_input, "time", &time );
  214.                         i_seconds = time.i_time / 1000000;
  215.                         secstotimestr ( psz_time, i_seconds );
  216.                         var_Get( p_intf->p_sys->p_input, "length",  &time );
  217.                         i_seconds = time.i_time / 1000000;
  218.                         secstotimestr ( psz_total, i_seconds );
  219.                         p_main_interface->statusbar->SetStatusText(
  220.                             wxU(psz_time) + wxString(wxT(" / ")) +
  221.                             wxU(psz_total), 0 );
  222.                     }
  223.                 }
  224.             }
  225.         vlc_mutex_unlock( &p_input->stream.stream_lock );
  226. #endif
  227.             /* Take care of the volume, etc... */
  228.             p_main_interface->Update();
  229.             /* Manage Playing status */
  230.             var_Get( p_input, "state", &val );
  231.             if( i_old_playing_status != val.i_int )
  232.             {
  233.                 if( val.i_int == PAUSE_S )
  234.                 {
  235.                     p_main_interface->TogglePlayButton( PAUSE_S );
  236.                 }
  237.                 else
  238.                 {
  239.                     p_main_interface->TogglePlayButton( PLAYING_S );
  240.                 }
  241.                 i_old_playing_status = val.i_int;
  242.             }
  243.             /* Manage Speed status */
  244.             var_Get( p_input, "rate", &val );
  245.             if( i_old_rate != val.i_int )
  246.             {
  247.                 p_main_interface->statusbar->SetStatusText(
  248.                     wxString::Format(wxT("x%.2f"),
  249.                     (float)INPUT_RATE_DEFAULT / val.i_int ), 1 );
  250.                 i_old_rate = val.i_int;
  251.             }
  252.         }
  253.     }
  254.     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
  255.     {
  256.         p_intf->p_sys->b_playing = 0;
  257.         p_main_interface->TogglePlayButton( PAUSE_S );
  258.         i_old_playing_status = PAUSE_S;
  259.     }
  260.     /* Show the interface, if requested */
  261.     if( p_intf->p_sys->b_intf_show )
  262.     {
  263.         p_main_interface->Raise();
  264.         p_intf->p_sys->b_intf_show = VLC_FALSE;
  265.     }
  266.     if( p_intf->b_die )
  267.     {
  268.         vlc_mutex_unlock( &p_intf->change_lock );
  269.         /* Prepare to die, young Skywalker */
  270.         p_main_interface->Close(TRUE);
  271.         return;
  272.     }
  273.     vlc_mutex_unlock( &p_intf->change_lock );
  274. }
  275. /*****************************************************************************
  276.  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
  277.  *  We don't show the menu directly here because we don't want the
  278.  *  caller to block for a too long time.
  279.  *****************************************************************************/
  280. static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
  281.                         vlc_value_t old_val, vlc_value_t new_val, void *param )
  282. {
  283.     intf_thread_t *p_intf = (intf_thread_t *)param;
  284.     if( p_intf->p_sys->pf_show_dialog )
  285.     {
  286.         p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
  287.                                        new_val.b_bool, 0 );
  288.     }
  289.     return VLC_SUCCESS;
  290. }
  291. /*****************************************************************************
  292.  * IntfShowCB: callback triggered by the intf-show playlist variable.
  293.  *****************************************************************************/
  294. static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
  295.                        vlc_value_t old_val, vlc_value_t new_val, void *param )
  296. {
  297.     intf_thread_t *p_intf = (intf_thread_t *)param;
  298.     p_intf->p_sys->b_intf_show = VLC_TRUE;
  299.     return VLC_SUCCESS;
  300. }