Skip to content

Commit 50b91be

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 a5610ad commit 50b91be

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

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 {
@@ -757,12 +757,12 @@ add_motion_event (MxKineticScrollView *scroll,
757757
gfloat y)
758758
{
759759
MxKineticScrollViewPrivate *priv = scroll->priv;
760-
GTimeVal tv;
760+
gint64 tv;
761761

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

765-
g_get_current_time (&tv);
765+
tv = g_get_real_time ();
766766

767767
if (priv->n_motions == 0)
768768
{
@@ -785,8 +785,7 @@ add_motion_event (MxKineticScrollView *scroll,
785785

786786
priv->motion_total.x += x;
787787
priv->motion_total.y += y;
788-
priv->motion_total.time.tv_sec += tv.tv_sec;
789-
priv->motion_total.time.tv_usec += tv.tv_usec;
788+
priv->motion_total.time += tv;
790789

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

13661365
/* Get time delta */
1367-
g_get_current_time (&release_time);
1366+
release_time = g_get_real_time ();
13681367

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

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

13751374
x_origin = priv->motion_total.x / priv->n_motions;
13761375
y_origin = priv->motion_total.y / priv->n_motions;
1377-
motion_time.tv_sec = priv->motion_total.time.tv_sec / priv->n_motions;
1378-
motion_time.tv_usec = priv->motion_total.time.tv_usec / priv->n_motions;
1376+
motion_time = priv->motion_total.time / priv->n_motions;
13791377

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

13901380
/* Work out the fraction of 1/60th of a second that has elapsed */
1391-
frac = (time_diff/1000.0) / (1000.0/60.0);
1381+
frac = (time_diff / 1000.0) / (1000.0 / 60.0);
13921382

13931383
/* See how many units to move in 1/60th of a second */
13941384
priv->dx = (x_origin - event_x) / frac * priv->acceleration_factor;
@@ -1420,7 +1410,7 @@ release_event (MxKineticScrollView *scroll,
14201410
LOG_DEBUG (scroll, "%s: checking duration: x_pos = %i, y_pos = %i, "
14211411
"event_x = %f, event_y = %f, y = %f, nx = %f, ny = %f, "
14221412
"n = %f, frac = %f, x_origin = %f, y_origin = %f, "
1423-
"time_diff = %lu, duration = %u, "
1413+
"time_diff = %" G_GINT64_FORMAT ", duration = %u, "
14241414
"priv->n_motions = %u, priv->dx = %f, "
14251415
"priv->dy = %f, priv->decel_rate = %f, "
14261416
"priv->overshoot = %f, priv->accumulated_delta = %f, "
@@ -1537,7 +1527,7 @@ release_event (MxKineticScrollView *scroll,
15371527
"upper = %f, step_increment = %f, page_size = %f, "
15381528
"d = %f, ax = %f, ay = %f, y = %f, nx = %f, ny = %f, "
15391529
"n = %f, frac = %f, x_origin = %f, y_origin = %f, "
1540-
"time_diff = %lu, duration = %u, "
1530+
"time_diff = %" G_GINT64_FORMAT ", duration = %u, "
15411531
"priv->n_motions = %u, priv->dx = %f, "
15421532
"priv->dy = %f, priv->decel_rate = %f, "
15431533
"priv->overshoot = %f, priv->accumulated_delta = %f, "

0 commit comments

Comments
 (0)