]> git.sesse.net Git - vlc/blob - modules/gui/skins2/vars/time.cpp
A bit of headers cleanup
[vlc] / modules / gui / skins2 / vars / time.cpp
1 /*****************************************************************************
2  * time.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <stdio.h>  // snprintf
26
27 #include "time.hpp"
28 #include <vlc_input.h>
29
30 void StreamTime::set( float percentage, bool updateVLC )
31 {
32     VarPercent::set( percentage );
33
34     // Avoid looping forever...
35     if( updateVLC && getIntf()->p_sys->p_input )
36     {
37         vlc_value_t pos;
38         pos.f_float = percentage;
39
40         var_Set( getIntf()->p_sys->p_input, "position", pos );
41     }
42 }
43
44
45 const string StreamTime::getAsStringPercent() const
46 {
47     int value = (int)(100. * get());
48     // 0 <= value <= 100, so we need 4 chars
49     char *str = new char[4];
50     snprintf( str, 4, "%d", value );
51     string ret = str;
52     delete[] str;
53
54     return ret;
55 }
56
57
58 const string StreamTime::getAsStringCurrTime( bool bShortFormat ) const
59 {
60     if( getIntf()->p_sys->p_input == NULL )
61     {
62         return "-:--:--";
63     }
64
65     vlc_value_t pos; pos.f_float = 0.0;
66     var_Get( getIntf()->p_sys->p_input, "position", &pos );
67     if( pos.f_float == 0.0 )
68     {
69         return "-:--:--";
70     }
71
72     vlc_value_t time; time.i_time = 0L;
73     var_Get( getIntf()->p_sys->p_input, "time", &time );
74
75     return formatTime( time.i_time / 1000000, bShortFormat );
76 }
77
78
79 const string StreamTime::getAsStringTimeLeft( bool bShortFormat ) const
80 {
81     if( getIntf()->p_sys->p_input == NULL )
82     {
83         return "-:--:--";
84     }
85
86     vlc_value_t pos;
87     var_Get( getIntf()->p_sys->p_input, "position", &pos );
88     if( pos.f_float == 0.0 )
89     {
90         return "-:--:--";
91     }
92
93     vlc_value_t time, duration;
94     var_Get( getIntf()->p_sys->p_input, "time", &time );
95     var_Get( getIntf()->p_sys->p_input, "length", &duration );
96
97     return formatTime( (duration.i_time - time.i_time) / 1000000,
98                        bShortFormat );
99 }
100
101
102 const string StreamTime::getAsStringDuration( bool bShortFormat ) const
103 {
104     if( getIntf()->p_sys->p_input == NULL )
105     {
106         return "-:--:--";
107     }
108
109     vlc_value_t pos; pos.f_float = 0.0;
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, bShortFormat );
120 }
121
122
123 const string StreamTime::formatTime( int seconds, bool bShortFormat ) const
124 {
125     char *psz_time = new char[MSTRTIME_MAX_SIZE];
126     if( bShortFormat && (seconds < 60 * 60) )
127     {
128         snprintf( psz_time, MSTRTIME_MAX_SIZE, "%02d:%02d",
129                   (int) (seconds / 60 % 60),
130                   (int) (seconds % 60) );
131     }
132     else
133     {
134         snprintf( psz_time, MSTRTIME_MAX_SIZE, "%d:%02d:%02d",
135                   (int) (seconds / (60 * 60)),
136                   (int) (seconds / 60 % 60),
137                   (int) (seconds % 60) );
138     }
139
140     string ret = psz_time;
141     delete[] psz_time;
142
143     return ret;
144 }