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