Code

From trunk
[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 "preferences.h"
15 namespace Inkscape {
16 namespace Debug {
18 GdkEventLatencyTracker::GdkEventLatencyTracker()
19     : start_seconds(0.0), max_latency(0.0), skew(1.0), last_elapsed(0.0), last_seconds(0.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         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
36         skew = prefs->getDoubleLimited("/debug/latency/skew", 1.0, 0.5, 2.0);
37         return boost::optional<double>(0.0);
38     } else {
39         last_elapsed = elapsed.elapsed();
40         last_seconds = timestamp_seconds;
41         double const current_seconds = (last_elapsed * skew) + start_seconds;
42         double delta = current_seconds - timestamp_seconds;
43         if (delta < 0.0) {
44             start_seconds += -delta;
45             delta = 0.0;
46         } else if (delta > max_latency) {
47             max_latency = delta;
48         }
49         return boost::optional<double>(delta);
50     }
51 }
53 double GdkEventLatencyTracker::getSkew() {
54     double val = 0.0;
55     if ((last_elapsed > 0.0) && (last_seconds > 0.0)) {
56         val = (last_seconds - start_seconds) / last_elapsed;
57     }
58     return val;
59 }
61 GdkEventLatencyTracker &GdkEventLatencyTracker::default_tracker() {
62     static GdkEventLatencyTracker tracker;
63     return tracker;
64 }
66 }
67 }
69 /*
70   Local Variables:
71   mode:c++
72   c-file-style:"stroustrup"
73   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
74   indent-tabs-mode:nil
75   fill-column:99
76   End:
77 */
78 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :