]> git.sesse.net Git - vlc/blob - modules/gui/skins2/vars/time.cpp
75bebc5cdd345dd1f80265862fc8f9d204a826ce
[vlc] / modules / gui / skins2 / vars / time.cpp
1 /*****************************************************************************
2  * time.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN (Centrale Réseaux) and its contributors
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include <stdio.h>  // snprintf
26
27 #include "time.hpp"
28 #include <vlc/input.h>
29
30
31 void StreamTime::set( float percentage, bool updateVLC )
32 {
33     VarPercent::set( percentage );
34
35     // Avoid looping forever...
36     if( updateVLC && getIntf()->p_sys->p_input )
37     {
38         vlc_value_t pos;
39         pos.f_float = percentage;
40
41         var_Set( getIntf()->p_sys->p_input, "position", pos );
42     }
43 }
44
45
46 const string StreamTime::getAsStringPercent() const
47 {
48     int value = (int)(100. * get());
49     // 0 <= value <= 100, so we need 4 chars
50     char *str = new char[4];
51     snprintf( str, 4, "%d", value );
52     string ret = str;
53     delete[] str;
54
55     return ret;
56 }
57
58
59 const string StreamTime::getAsStringCurrTime() const
60 {
61     if( getIntf()->p_sys->p_input == NULL )
62     {
63         return "-:--:--";
64     }
65
66     vlc_value_t pos;
67     var_Get( getIntf()->p_sys->p_input, "position", &pos );
68     if( pos.f_float == 0.0 )
69     {
70         return "-:--:--";
71     }
72
73     vlc_value_t time;
74     var_Get( getIntf()->p_sys->p_input, "time", &time );
75
76     return formatTime( time.i_time / 1000000 );
77 }
78
79
80 const string StreamTime::getAsStringTimeLeft() const
81 {
82     if( getIntf()->p_sys->p_input == NULL )
83     {
84         return "-:--:--";
85     }
86
87     vlc_value_t pos;
88     var_Get( getIntf()->p_sys->p_input, "position", &pos );
89     if( pos.f_float == 0.0 )
90     {
91         return "-:--:--";
92     }
93
94     vlc_value_t time, duration;
95     var_Get( getIntf()->p_sys->p_input, "time", &time );
96     var_Get( getIntf()->p_sys->p_input, "length", &duration );
97
98     return formatTime( (duration.i_time - time.i_time) / 1000000 );
99 }
100
101
102 const string StreamTime::getAsStringDuration() const
103 {
104     if( getIntf()->p_sys->p_input == NULL )
105     {
106         return "-:--:--";
107     }
108
109     vlc_value_t pos;
110     var_Get( getIntf()->p_sys->p_input, "position", &pos );
111     if( pos.f_float == 0.0 )
112     {
113         return "-:--:--";
114     }
115
116     vlc_value_t time;
117     var_Get( getIntf()->p_sys->p_input, "length", &time );
118
119     return formatTime( time.i_time / 1000000 );
120 }
121
122
123 const string StreamTime::formatTime( int seconds ) const
124 {
125     char *psz_time = new char[MSTRTIME_MAX_SIZE];
126     snprintf( psz_time, MSTRTIME_MAX_SIZE, "%d:%02d:%02d",
127               (int) (seconds / (60 * 60)),
128               (int) (seconds / 60 % 60),
129               (int) (seconds % 60) );
130
131     string ret = psz_time;
132     delete[] psz_time;
133
134     return ret;
135 }