Code

Added compensation factor for time-skew with event latency.
[inkscape.git] / src / debug / gdk-event-latency-tracker.cpp
1 /*
2  * Inkscape::Debug::GdkEventLatencyTracker - tracks backlog of GDK events
3  *
4  * Authors:
5  *   MenTaLguY <mental@rydia.net>
6  *
7  * Copyright (C) 2008 MenTaLguY
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  */
12 #include "debug/gdk-event-latency-tracker.h"
13 #include "prefs-utils.h"
15 namespace Inkscape {
16 namespace Debug {
18 GdkEventLatencyTracker::GdkEventLatencyTracker()
19     : start_seconds(0.0), max_latency(0.0), skew(1.0)
20 {
21     elapsed.stop();
22     elapsed.reset();
23 }
25 boost::optional<double> GdkEventLatencyTracker::process(GdkEvent const *event) {
26     guint32 const timestamp=gdk_event_get_time(const_cast<GdkEvent *>(event));
27     if (timestamp == GDK_CURRENT_TIME) {
28         return boost::optional<double>();
29     }
30     double const timestamp_seconds = timestamp / 1000.0;
32     if (start_seconds == 0.0) {
33         elapsed.start();
34         start_seconds = timestamp_seconds;
35         skew = prefs_get_double_attribute_limited("debug.latency", "skew", 1.0, 0.5, 2.0);
36         return boost::optional<double>(0.0);
37     } else {
38         double const current_seconds = (elapsed.elapsed() * skew) + start_seconds;
39         double delta = current_seconds - timestamp_seconds;
40         if (delta < 0.0) {
41             start_seconds += -delta;
42             delta = 0.0;
43         } else if (delta > max_latency) {
44             max_latency = delta;
45         }
46         return boost::optional<double>(delta);
47     }
48 }
50 GdkEventLatencyTracker &GdkEventLatencyTracker::default_tracker() {
51     static GdkEventLatencyTracker tracker;
52     return tracker;
53 }
55 }
56 }
58 /*
59   Local Variables:
60   mode:c++
61   c-file-style:"stroustrup"
62   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
63   indent-tabs-mode:nil
64   fill-column:99
65   End:
66 */
67 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :