2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <linux/input.h>
22 #include <cutils/klog.h>
23 #include <utils/SystemClock.h>
24 #include "minui/minui.h"
26 #define NEXT_TIMEOUT_MS 5000
27 #define LAST_TIMEOUT_MS 30000
29 #define LOGE(x...) do { KLOG_ERROR("slideshow", x); } while (0)
31 static int input_cb(int fd, unsigned int epevents, void *data)
33 struct input_event ev;
34 int *key_code = (int *)data;
38 if (ev_get_input(fd, epevents, &ev)) {
42 if (ev.type == EV_KEY && ev.value == 1) {
56 static void draw(const char *resname)
61 if (res_create_display_surface(resname, &surface) < 0) {
62 LOGE("failed to create surface for %s\n", resname);
66 w = gr_get_width(surface);
67 h = gr_get_height(surface);
68 x = (gr_fb_width() - w) / 2;
69 y = (gr_fb_height() - h) / 2;
71 gr_blit(surface, 0, 0, w, h, x, y);
74 res_free_surface(surface);
79 LOGE("usage: slideshow [-t timeout] image.png [image2.png ...] last.png\n");
83 int main(int argc, char **argv)
88 long int timeout = NEXT_TIMEOUT_MS;
91 while ((opt = getopt(argc, argv, "t:")) != -1) {
94 timeout = strtol(optarg, NULL, 0);
96 if (timeout < 0 || timeout >= LONG_MAX) {
97 timeout = NEXT_TIMEOUT_MS;
98 LOGE("invalid timeout %s, defaulting to %ld\n", optarg,
107 if (optind >= argc) {
111 if (gr_init() == -1 || ev_init(input_cb, &key_code) == -1) {
112 LOGE("failed to initialize minui\n");
116 /* display all images except the last one, switch to next image after
117 * timeout or user input */
119 while (optind < argc - 1) {
120 draw(argv[optind++]);
122 start = android::uptimeMillis();
123 long int timeout_remaining = timeout;
125 if (ev_wait(timeout_remaining) == 0) {
128 if (key_code != -1) {
133 timeout_remaining -= android::uptimeMillis() - start;
134 } while (timeout_remaining > 0);
137 /* if there was user input while showing the images, display the last
138 * image and wait until the power button is pressed or LAST_TIMEOUT_MS
142 start = android::uptimeMillis();
147 if (ev_wait(timeout) == 0) {
151 if (android::uptimeMillis() - start >= LAST_TIMEOUT_MS) {
154 } while (key_code != KEY_POWER);