• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
23 #include "minui/minui.h"
24 
25 #define NEXT_TIMEOUT_MS 5000
26 #define LAST_TIMEOUT_S  30
27 
28 #define LOGE(x...) do { KLOG_ERROR("slideshow", x); } while (0)
29 
input_cb(int fd,unsigned int epevents,void * data)30 static int input_cb(int fd, unsigned int epevents, void *data)
31 {
32     struct input_event ev;
33     int *key_code = (int *)data;
34 
35     *key_code = -1;
36 
37     if (ev_get_input(fd, epevents, &ev)) {
38         return -1;
39     }
40 
41     if (ev.type == EV_KEY) {
42         *key_code = ev.code;
43     }
44 
45     return 0;
46 }
47 
clear()48 static void clear()
49 {
50     gr_color(0, 0, 0, 0);
51     gr_clear();
52     gr_flip();
53 }
54 
draw(const char * resname)55 static void draw(const char *resname)
56 {
57     GRSurface* surface;
58     int w, h, x, y;
59 
60     if (res_create_display_surface(resname, &surface) < 0) {
61         LOGE("failed to create surface for %s\n", resname);
62         return;
63     }
64 
65     w = gr_get_width(surface);
66     h = gr_get_height(surface);
67     x = (gr_fb_width() - w) / 2;
68     y = (gr_fb_height() - h) / 2;
69 
70     gr_blit(surface, 0, 0, w, h, x, y);
71     gr_flip();
72 
73     res_free_surface(surface);
74 }
75 
usage()76 int usage()
77 {
78     LOGE("usage: slideshow [-t timeout] image.png [image2.png ...] last.png\n");
79     return EXIT_FAILURE;
80 }
81 
main(int argc,char ** argv)82 int main(int argc, char **argv)
83 {
84     int key_code = -1;
85     int input = false;
86     int opt;
87     long int timeout = NEXT_TIMEOUT_MS;
88     time_t start;
89 
90     while ((opt = getopt(argc, argv, "t")) != -1) {
91         switch (opt) {
92         case 't':
93             timeout = strtol(optarg, NULL, 0);
94 
95             if (timeout < 0 || timeout >= LONG_MAX) {
96                 timeout = NEXT_TIMEOUT_MS;
97                 LOGE("invalid timeout %s, defaulting to %ld\n", optarg,
98                     timeout);
99             }
100             break;
101         default:
102             return usage();
103         }
104     }
105 
106     if (optind >= argc) {
107         return usage();
108     }
109 
110     if (gr_init() == -1 || ev_init(input_cb, &key_code) == -1) {
111         LOGE("failed to initialize minui\n");
112         return EXIT_FAILURE;
113     }
114 
115     /* display all images except the last one, switch to next image after
116      * timeout or user input */
117 
118     while (optind < argc - 1) {
119         draw(argv[optind++]);
120 
121         if (ev_wait(timeout) == 0) {
122             ev_dispatch();
123 
124             if (key_code != -1) {
125                 input = true;
126             }
127         }
128     };
129 
130     /* if there was user input while showing the images, display the last
131      * image and wait until the power button is pressed or LAST_TIMEOUT_S
132      * has elapsed */
133 
134     if (input) {
135         start = time(NULL);
136         draw(argv[optind]);
137 
138         do {
139             if (ev_wait(timeout) == 0) {
140                 ev_dispatch();
141             }
142 
143             if (time(NULL) - start >= LAST_TIMEOUT_S) {
144                 break;
145             }
146         } while (key_code != KEY_POWER);
147     }
148 
149     clear();
150     gr_exit();
151     ev_exit();
152 
153     return EXIT_SUCCESS;
154 }
155