Skip to content

Commit 6d66123

Browse files
author
Philip Withnall
committed
kinetic-scroll-view: Use gint64 for timestamps rather than GTimeVal
GTimeVal is a pain to use and the use of glong means the potential for overflow when summing several timestamps on 32-bit platforms is quite high. Instead, use gint64 and g_get_real_time(). #98
1 parent 41a820c commit 6d66123

1 file changed

Lines changed: 13 additions & 23 deletions

File tree

mx/mx-kinetic-scroll-view.c

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ typedef struct {
6666
/* Units to store the origin of a click when scrolling */
6767
gfloat x;
6868
gfloat y;
69-
GTimeVal time;
69+
gint64 time; /* use g_get_real_time() for this */
7070
} MxKineticScrollViewMotion;
7171

7272
typedef enum {
@@ -758,12 +758,12 @@ add_motion_event (MxKineticScrollView *scroll,
758758
gfloat y)
759759
{
760760
MxKineticScrollViewPrivate *priv = scroll->priv;
761-
GTimeVal tv;
761+
gint64 tv;
762762

763763
LOG_DEBUG (scroll, "%s: x = %f, y = %f, n_motions = %u",
764764
G_STRFUNC, x, y, priv->n_motions);
765765

766-
g_get_current_time (&tv);
766+
tv = g_get_real_time ();
767767

768768
if (priv->n_motions == 0)
769769
{
@@ -786,8 +786,7 @@ add_motion_event (MxKineticScrollView *scroll,
786786

787787
priv->motion_total.x += x;
788788
priv->motion_total.y += y;
789-
priv->motion_total.time.tv_sec += tv.tv_sec;
790-
priv->motion_total.time.tv_usec += tv.tv_usec;
789+
priv->motion_total.time += tv;
791790

792791
/* Avoid overflow by only taking this branch if n_motions will not
793792
* overflow. Subsequent motions are ignored. */
@@ -1359,37 +1358,28 @@ release_event (MxKineticScrollView *scroll,
13591358
gdouble value, lower, upper, step_increment, page_size,
13601359
d, ax, ay, y, nx, ny, n;
13611360
gfloat frac, x_origin, y_origin;
1362-
GTimeVal release_time, motion_time;
1361+
gint64 release_time, motion_time; /* real microseconds */
13631362
MxAdjustment *hadjust, *vadjust;
1364-
glong time_diff;
1363+
gint64 time_diff; /* real microseconds */
13651364
guint duration;
13661365

13671366
/* Get time delta */
1368-
g_get_current_time (&release_time);
1367+
release_time = g_get_real_time ();
13691368

13701369
/* Get average position/time of last x mouse events */
13711370
x_origin = y_origin = 0;
1372-
motion_time = (GTimeVal){ 0, 0 };
1371+
motion_time = 0;
13731372

13741373
g_assert (priv->n_motions > 0);
13751374

13761375
x_origin = priv->motion_total.x / priv->n_motions;
13771376
y_origin = priv->motion_total.y / priv->n_motions;
1378-
motion_time.tv_sec = priv->motion_total.time.tv_sec / priv->n_motions;
1379-
motion_time.tv_usec = priv->motion_total.time.tv_usec / priv->n_motions;
1377+
motion_time = priv->motion_total.time / priv->n_motions;
13801378

1381-
/* Normalise the GTimeVal. */
1382-
motion_time.tv_sec += motion_time.tv_usec / G_USEC_PER_SEC;
1383-
motion_time.tv_usec %= G_USEC_PER_SEC;
1384-
1385-
if (motion_time.tv_sec == release_time.tv_sec)
1386-
time_diff = release_time.tv_usec - motion_time.tv_usec;
1387-
else
1388-
time_diff = release_time.tv_usec +
1389-
(G_USEC_PER_SEC - motion_time.tv_usec);
1379+
time_diff = release_time - motion_time;
13901380

13911381
/* Work out the fraction of 1/60th of a second that has elapsed */
1392-
frac = (time_diff/1000.0) / (1000.0/60.0);
1382+
frac = (time_diff / 1000.0) / (1000.0 / 60.0);
13931383

13941384
/* See how many units to move in 1/60th of a second */
13951385
priv->dx = (x_origin - event_x) / frac * priv->acceleration_factor;
@@ -1421,7 +1411,7 @@ release_event (MxKineticScrollView *scroll,
14211411
LOG_DEBUG (scroll, "%s: checking duration: x_pos = %i, y_pos = %i, "
14221412
"event_x = %f, event_y = %f, y = %f, nx = %f, ny = %f, "
14231413
"n = %f, frac = %f, x_origin = %f, y_origin = %f, "
1424-
"time_diff = %lu, duration = %u, "
1414+
"time_diff = %" G_GINT64_FORMAT ", duration = %u, "
14251415
"priv->n_motions = %u, priv->dx = %f, "
14261416
"priv->dy = %f, priv->decel_rate = %f, "
14271417
"priv->overshoot = %f, priv->accumulated_delta = %f, "
@@ -1538,7 +1528,7 @@ release_event (MxKineticScrollView *scroll,
15381528
"upper = %f, step_increment = %f, page_size = %f, "
15391529
"d = %f, ax = %f, ay = %f, y = %f, nx = %f, ny = %f, "
15401530
"n = %f, frac = %f, x_origin = %f, y_origin = %f, "
1541-
"time_diff = %lu, duration = %u, "
1531+
"time_diff = %" G_GINT64_FORMAT ", duration = %u, "
15421532
"priv->n_motions = %u, priv->dx = %f, "
15431533
"priv->dy = %f, priv->decel_rate = %f, "
15441534
"priv->overshoot = %f, priv->accumulated_delta = %f, "

0 commit comments

Comments
 (0)