Skip to content

Commit

Permalink
slideshow: Switch to android::uptimeMillis
Browse files Browse the repository at this point in the history
time(2) is not guaranteed to always go forward which makes
it a bit dangerous to use in loops like slideshow does.
switch to uptimeMillis which is based on clock_gettime
CLOCK_MONOTONIC which is is safe.

Change-Id: Ica1b7ee50df00fcc7bc849d7eaebe64f62434a47
  • Loading branch information
jredestig committed Oct 12, 2015
1 parent 810ae48 commit c38026b
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions slideshow/slideshow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
#include <time.h>
#include <linux/input.h>
#include <cutils/klog.h>
#include <utils/SystemClock.h>
#include "minui/minui.h"

#define NEXT_TIMEOUT_MS 5000
#define LAST_TIMEOUT_S 30
#define LAST_TIMEOUT_MS 30000

#define LOGE(x...) do { KLOG_ERROR("slideshow", x); } while (0)

Expand Down Expand Up @@ -85,7 +86,7 @@ int main(int argc, char **argv)
int input = false;
int opt;
long int timeout = NEXT_TIMEOUT_MS;
time_t start;
int64_t start;

while ((opt = getopt(argc, argv, "t:")) != -1) {
switch (opt) {
Expand Down Expand Up @@ -118,7 +119,7 @@ int main(int argc, char **argv)
while (optind < argc - 1) {
draw(argv[optind++]);

start = time(NULL);
start = android::uptimeMillis();
long int timeout_remaining = timeout;
do {
if (ev_wait(timeout_remaining) == 0) {
Expand All @@ -129,24 +130,25 @@ int main(int argc, char **argv)
break;
}
}
timeout_remaining -= (long int)(time(NULL) - start) * 1000;
timeout_remaining -= android::uptimeMillis() - start;
} while (timeout_remaining > 0);
};

/* if there was user input while showing the images, display the last
* image and wait until the power button is pressed or LAST_TIMEOUT_S
* image and wait until the power button is pressed or LAST_TIMEOUT_MS
* has elapsed */

if (input) {
start = time(NULL);
start = android::uptimeMillis();

draw(argv[optind]);

do {
if (ev_wait(timeout) == 0) {
ev_dispatch();
}

if (time(NULL) - start >= LAST_TIMEOUT_S) {
if (android::uptimeMillis() - start >= LAST_TIMEOUT_MS) {
break;
}
} while (key_code != KEY_POWER);
Expand Down

0 comments on commit c38026b

Please sign in to comment.