1 | /*
|
---|
2 | * Copyright (c) 2013 Martin Decky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup viewer
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <stdlib.h>
|
---|
37 | #include <vfs/vfs.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <stdbool.h>
|
---|
41 | #include <window.h>
|
---|
42 | #include <canvas.h>
|
---|
43 | #include <surface.h>
|
---|
44 | #include <codec/tga.h>
|
---|
45 | #include <task.h>
|
---|
46 |
|
---|
47 | #define NAME "viewer"
|
---|
48 |
|
---|
49 | #define WINDOW_WIDTH 1024
|
---|
50 | #define WINDOW_HEIGHT 768
|
---|
51 |
|
---|
52 | #define DECORATION_WIDTH 8
|
---|
53 | #define DECORATION_HEIGHT 28
|
---|
54 |
|
---|
55 | static size_t imgs_count;
|
---|
56 | static size_t imgs_current = 0;
|
---|
57 | static char **imgs;
|
---|
58 |
|
---|
59 | static window_t *main_window;
|
---|
60 | static surface_t *surface = NULL;
|
---|
61 | static canvas_t *canvas = NULL;
|
---|
62 |
|
---|
63 | static surface_coord_t img_width;
|
---|
64 | static surface_coord_t img_height;
|
---|
65 |
|
---|
66 | static bool img_load(const char *, surface_t **);
|
---|
67 | static bool img_setup(surface_t *);
|
---|
68 |
|
---|
69 | static void on_keyboard_event(widget_t *widget, void *data)
|
---|
70 | {
|
---|
71 | kbd_event_t *event = (kbd_event_t *) data;
|
---|
72 | bool update = false;
|
---|
73 |
|
---|
74 | if ((event->type == KEY_PRESS) && (event->c == 'q'))
|
---|
75 | exit(0);
|
---|
76 |
|
---|
77 | if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_DOWN)) {
|
---|
78 | if (imgs_current == imgs_count - 1)
|
---|
79 | imgs_current = 0;
|
---|
80 | else
|
---|
81 | imgs_current++;
|
---|
82 |
|
---|
83 | update = true;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_UP)) {
|
---|
87 | if (imgs_current == 0)
|
---|
88 | imgs_current = imgs_count - 1;
|
---|
89 | else
|
---|
90 | imgs_current--;
|
---|
91 |
|
---|
92 | update = true;
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (update) {
|
---|
96 | surface_t *lsface;
|
---|
97 |
|
---|
98 | if (!img_load(imgs[imgs_current], &lsface)) {
|
---|
99 | printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
|
---|
100 | exit(4);
|
---|
101 | }
|
---|
102 | if (!img_setup(lsface)) {
|
---|
103 | printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
|
---|
104 | exit(6);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | static bool img_load(const char *fname, surface_t **p_local_surface)
|
---|
110 | {
|
---|
111 | int fd;
|
---|
112 | int rc = vfs_lookup_open(fname, WALK_REGULAR, MODE_READ, &fd);
|
---|
113 | if (rc != EOK)
|
---|
114 | return false;
|
---|
115 |
|
---|
116 | struct stat stat;
|
---|
117 | rc = vfs_stat(fd, &stat);
|
---|
118 | if (rc != EOK) {
|
---|
119 | vfs_put(fd);
|
---|
120 | return false;
|
---|
121 | }
|
---|
122 |
|
---|
123 | void *tga = malloc(stat.size);
|
---|
124 | if (tga == NULL) {
|
---|
125 | vfs_put(fd);
|
---|
126 | return false;
|
---|
127 | }
|
---|
128 |
|
---|
129 | size_t nread;
|
---|
130 | rc = vfs_read(fd, (aoff64_t []) {0}, tga, stat.size, &nread);
|
---|
131 | if (rc != EOK || nread != stat.size) {
|
---|
132 | free(tga);
|
---|
133 | vfs_put(fd);
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 |
|
---|
137 | vfs_put(fd);
|
---|
138 |
|
---|
139 | *p_local_surface = decode_tga(tga, stat.size, 0);
|
---|
140 | if (*p_local_surface == NULL) {
|
---|
141 | free(tga);
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 |
|
---|
145 | free(tga);
|
---|
146 |
|
---|
147 | surface_get_resolution(*p_local_surface, &img_width, &img_height);
|
---|
148 |
|
---|
149 | return true;
|
---|
150 | }
|
---|
151 |
|
---|
152 | static bool img_setup(surface_t *local_surface)
|
---|
153 | {
|
---|
154 | if (canvas != NULL) {
|
---|
155 | if (!update_canvas(canvas, local_surface)) {
|
---|
156 | surface_destroy(local_surface);
|
---|
157 | return false;
|
---|
158 | }
|
---|
159 | } else {
|
---|
160 | canvas = create_canvas(window_root(main_window), NULL,
|
---|
161 | img_width, img_height, local_surface);
|
---|
162 | if (canvas == NULL) {
|
---|
163 | surface_destroy(local_surface);
|
---|
164 | return false;
|
---|
165 | }
|
---|
166 |
|
---|
167 | sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event);
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (surface != NULL)
|
---|
171 | surface_destroy(surface);
|
---|
172 |
|
---|
173 | surface = local_surface;
|
---|
174 | return true;
|
---|
175 | }
|
---|
176 |
|
---|
177 | int main(int argc, char *argv[])
|
---|
178 | {
|
---|
179 | window_flags_t flags;
|
---|
180 | surface_t *lsface;
|
---|
181 | bool fullscreen;
|
---|
182 | sysarg_t dwidth;
|
---|
183 | sysarg_t dheight;
|
---|
184 |
|
---|
185 | if (argc < 2) {
|
---|
186 | printf("Compositor server not specified.\n");
|
---|
187 | return 1;
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (argc < 3) {
|
---|
191 | printf("No image files specified.\n");
|
---|
192 | return 1;
|
---|
193 | }
|
---|
194 |
|
---|
195 | imgs_count = argc - 2;
|
---|
196 | imgs = calloc(imgs_count, sizeof(char *));
|
---|
197 | if (imgs == NULL) {
|
---|
198 | printf("Out of memory.\n");
|
---|
199 | return 2;
|
---|
200 | }
|
---|
201 |
|
---|
202 | for (int i = 0; i < argc - 2; i++) {
|
---|
203 | imgs[i] = str_dup(argv[i + 2]);
|
---|
204 | if (imgs[i] == NULL) {
|
---|
205 | printf("Out of memory.\n");
|
---|
206 | return 3;
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (!img_load(imgs[imgs_current], &lsface)) {
|
---|
211 | printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
|
---|
212 | return 4;
|
---|
213 | }
|
---|
214 |
|
---|
215 | fullscreen = ((img_width == WINDOW_WIDTH) &&
|
---|
216 | (img_height == WINDOW_HEIGHT));
|
---|
217 |
|
---|
218 | flags = WINDOW_MAIN;
|
---|
219 | if (!fullscreen)
|
---|
220 | flags |= WINDOW_DECORATED;
|
---|
221 |
|
---|
222 | main_window = window_open(argv[1], NULL, flags, "viewer");
|
---|
223 | if (!main_window) {
|
---|
224 | printf("Cannot open main window.\n");
|
---|
225 | return 5;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | if (!img_setup(lsface)) {
|
---|
230 | printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
|
---|
231 | return 6;
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (!fullscreen) {
|
---|
235 | dwidth = DECORATION_WIDTH;
|
---|
236 | dheight = DECORATION_HEIGHT;
|
---|
237 | } else {
|
---|
238 | dwidth = 0;
|
---|
239 | dheight = 0;
|
---|
240 | }
|
---|
241 |
|
---|
242 | window_resize(main_window, 0, 0, img_width + dwidth,
|
---|
243 | img_height + dheight, WINDOW_PLACEMENT_ANY);
|
---|
244 | window_exec(main_window);
|
---|
245 |
|
---|
246 | task_retval(0);
|
---|
247 | async_manager();
|
---|
248 |
|
---|
249 | return 0;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /** @}
|
---|
253 | */
|
---|