blob: 15824f839a5fe1175e16351270e16934b72e82a0 [file] [log] [blame]
Sami Tolvanena0f13e02015-02-19 10:44:12 +00001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <unistd.h>
18#include <stdlib.h>
19#include <limits.h>
20#include <time.h>
21#include <linux/input.h>
22#include <cutils/klog.h>
Johan Redestigc38026b2015-10-12 10:24:30 +020023#include <utils/SystemClock.h>
Sami Tolvanena0f13e02015-02-19 10:44:12 +000024#include "minui/minui.h"
25
26#define NEXT_TIMEOUT_MS 5000
Johan Redestigc38026b2015-10-12 10:24:30 +020027#define LAST_TIMEOUT_MS 30000
Sami Tolvanena0f13e02015-02-19 10:44:12 +000028
29#define LOGE(x...) do { KLOG_ERROR("slideshow", x); } while (0)
30
31static int input_cb(int fd, unsigned int epevents, void *data)
32{
33 struct input_event ev;
34 int *key_code = (int *)data;
35
36 *key_code = -1;
37
38 if (ev_get_input(fd, epevents, &ev)) {
39 return -1;
40 }
41
Johan Redestig44fec942015-10-09 09:54:57 +020042 if (ev.type == EV_KEY && ev.value == 1) {
Sami Tolvanena0f13e02015-02-19 10:44:12 +000043 *key_code = ev.code;
44 }
45
46 return 0;
47}
48
49static void clear()
50{
51 gr_color(0, 0, 0, 0);
52 gr_clear();
53 gr_flip();
54}
55
56static void draw(const char *resname)
57{
Elliott Hughes99e2aca2015-04-15 10:23:15 -070058 GRSurface* surface;
Sami Tolvanena0f13e02015-02-19 10:44:12 +000059 int w, h, x, y;
60
61 if (res_create_display_surface(resname, &surface) < 0) {
62 LOGE("failed to create surface for %s\n", resname);
63 return;
64 }
65
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;
70
71 gr_blit(surface, 0, 0, w, h, x, y);
72 gr_flip();
73
74 res_free_surface(surface);
75}
76
77int usage()
78{
79 LOGE("usage: slideshow [-t timeout] image.png [image2.png ...] last.png\n");
80 return EXIT_FAILURE;
81}
82
83int main(int argc, char **argv)
84{
85 int key_code = -1;
86 int input = false;
87 int opt;
88 long int timeout = NEXT_TIMEOUT_MS;
Johan Redestigc38026b2015-10-12 10:24:30 +020089 int64_t start;
Sami Tolvanena0f13e02015-02-19 10:44:12 +000090
Johan Redestig4ef1adc2015-10-09 10:37:37 +020091 while ((opt = getopt(argc, argv, "t:")) != -1) {
Sami Tolvanena0f13e02015-02-19 10:44:12 +000092 switch (opt) {
93 case 't':
94 timeout = strtol(optarg, NULL, 0);
95
96 if (timeout < 0 || timeout >= LONG_MAX) {
97 timeout = NEXT_TIMEOUT_MS;
Sami Tolvanen5b97aa62015-04-01 17:53:41 +010098 LOGE("invalid timeout %s, defaulting to %ld\n", optarg,
Sami Tolvanena0f13e02015-02-19 10:44:12 +000099 timeout);
100 }
101 break;
102 default:
103 return usage();
104 }
105 }
106
107 if (optind >= argc) {
108 return usage();
109 }
110
111 if (gr_init() == -1 || ev_init(input_cb, &key_code) == -1) {
112 LOGE("failed to initialize minui\n");
113 return EXIT_FAILURE;
114 }
115
116 /* display all images except the last one, switch to next image after
117 * timeout or user input */
118
119 while (optind < argc - 1) {
120 draw(argv[optind++]);
121
Johan Redestigc38026b2015-10-12 10:24:30 +0200122 start = android::uptimeMillis();
Johan Redestig810ae482015-10-09 13:25:39 +0200123 long int timeout_remaining = timeout;
124 do {
125 if (ev_wait(timeout_remaining) == 0) {
126 ev_dispatch();
Sami Tolvanena0f13e02015-02-19 10:44:12 +0000127
Johan Redestig810ae482015-10-09 13:25:39 +0200128 if (key_code != -1) {
129 input = true;
130 break;
131 }
Sami Tolvanena0f13e02015-02-19 10:44:12 +0000132 }
Johan Redestigc38026b2015-10-12 10:24:30 +0200133 timeout_remaining -= android::uptimeMillis() - start;
Johan Redestig810ae482015-10-09 13:25:39 +0200134 } while (timeout_remaining > 0);
Sami Tolvanena0f13e02015-02-19 10:44:12 +0000135 };
136
137 /* if there was user input while showing the images, display the last
Johan Redestigc38026b2015-10-12 10:24:30 +0200138 * image and wait until the power button is pressed or LAST_TIMEOUT_MS
Sami Tolvanena0f13e02015-02-19 10:44:12 +0000139 * has elapsed */
140
141 if (input) {
Johan Redestigc38026b2015-10-12 10:24:30 +0200142 start = android::uptimeMillis();
143
Sami Tolvanena0f13e02015-02-19 10:44:12 +0000144 draw(argv[optind]);
145
146 do {
147 if (ev_wait(timeout) == 0) {
148 ev_dispatch();
149 }
150
Johan Redestigc38026b2015-10-12 10:24:30 +0200151 if (android::uptimeMillis() - start >= LAST_TIMEOUT_MS) {
Sami Tolvanena0f13e02015-02-19 10:44:12 +0000152 break;
153 }
154 } while (key_code != KEY_POWER);
155 }
156
157 clear();
158 gr_exit();
159 ev_exit();
160
161 return EXIT_SUCCESS;
162}