aboutsummaryrefslogtreecommitdiff
path: root/util/fbcompose/TickTracker.cc
blob: ef0f649745b3ee3102778409a7dc0af4e4fe4dad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/** TickTracker.cc file for the fluxbox compositor. */

// Copyright (c) 2011 Gediminas Liktaras (gliktaras at gmail dot com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.


#include "TickTracker.hh"

#include "Logging.hh"

using namespace FbCompositor;


//--- CONSTANTS ----------------------------------------------------------------

// The accuracy of the timer (1.0 = 1 second).
const double EPSILON = 1e-6;


//--- CONSTRUCTORS AND DESTRUCTORS ---------------------------------------------

// Constructor.
TickTracker::TickTracker() {
    m_is_running = false;
    m_tick_size = 1000000;
    m_ticks_per_second = 1.0;
}

// Copy constructor.
TickTracker::TickTracker(const TickTracker &other) :
    m_is_running(other.m_is_running),
    m_startTime(other.m_startTime),
    m_tick_size(other.m_tick_size),
    m_ticks_per_second(other.m_ticks_per_second),
    m_observed_ticks(other.m_observed_ticks) {
}

// Assignment operator.
TickTracker &TickTracker::operator=(const TickTracker &other) {
    if (this != &other) {
        m_is_running = other.m_is_running;
        m_startTime = other.m_startTime;
        m_tick_size = other.m_tick_size;
        m_ticks_per_second = other.m_ticks_per_second;
        m_observed_ticks = other.m_observed_ticks;
    }
    return *this;
}

// Destructor.
TickTracker::~TickTracker() { }


//--- TIMER MANIPULATION -------------------------------------------------------

// Starts the timer.
void TickTracker::start() {
    if (gettimeofday(&m_startTime, NULL)) {
        throw TimeException("Cannot obtain the current time.");
    }
    m_observed_ticks = 0;
    m_is_running = true;
}

/** Stops the timer. */
void TickTracker::stop() {
    m_is_running = false;
}


//--- TIMER QUERIES ------------------------------------------------------------

// Returns the new number of elapsed ticks since last call of this function.
int TickTracker::newElapsedTicks() {
    int total_ticks = totalElapsedTicks();
    int new_ticks = total_ticks - m_observed_ticks;
    m_observed_ticks = total_ticks;

    if (new_ticks < 0) {
        return 0;
    } else {
        return new_ticks;
    }
}

// Returns the total number of elapsed ticks.
int TickTracker::totalElapsedTicks() {
    static timeval now;

    if (gettimeofday(&now, NULL)) {
        throw TimeException("Cannot obtain the current time.");
    }

    return tickDifference(now, m_startTime);
}


// Sets the size of a tick.
void TickTracker::setTickSize(int usec) {
    if (usec < 1) {
        throw TimeException("Invalid tick size.");
    }

    m_tick_size = usec;
    m_ticks_per_second = 1000000.0 / usec;
}


//--- INTERNAL FUNTIONS --------------------------------------------------------

// Returns the difference in time between two timevals.
// Function adapted from http://www.gnu.org/s/libc/manual/html_node/Elapsed-Time.html.
timeval TickTracker::timeDifference(timeval t1, timeval t2) {
    int n_sec;

    if (t1.tv_usec < t2.tv_usec) {
        n_sec = (t2.tv_usec - t1.tv_usec) / 1000000 + 1;
        t2.tv_usec -= 1000000 * n_sec;
        t2.tv_sec += n_sec;
    }
    if (t1.tv_usec - t2.tv_usec > 1000000) {
        n_sec = (t1.tv_usec - t2.tv_usec) / 1000000;
        t2.tv_usec += 1000000 * n_sec;
        t2.tv_sec -= n_sec;
    }

    timeval diff;
    diff.tv_sec = t1.tv_sec - t2.tv_sec;
    diff.tv_usec = t1.tv_usec - t2.tv_usec;
    return diff;
}

// Returns the difference between two timevals in ticks.
int TickTracker::tickDifference(const timeval &t1, const timeval &t2) {
    timeval diff = timeDifference(t1, t2);

    double raw_diff = diff.tv_sec * m_ticks_per_second + (double(diff.tv_usec) / m_tick_size);
    return int(raw_diff + EPSILON);
}