Skip to content

Commit a1ea89a

Browse files
author
Philip Withnall
committed
kinetic-scroll-view: Use gint64 for timestamps rather than GTimeVal
GTimeVal is a pain to use, microsecond precision is not necessary for tracking motion events (millisecond precision is fine), 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().
1 parent 06d22bc commit a1ea89a

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

mx/mx-kinetic-scroll-view.c

Lines changed: 11 additions & 21 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;
13631362
MxAdjustment *hadjust, *vadjust;
1364-
glong time_diff;
1363+
gint64 time_diff; /* real milliseconds */
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 = ((gfloat) time_diff) / (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;

0 commit comments

Comments
 (0)