source: mainline/uspace/app/viewer/viewer.c@ 39330200

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 39330200 was 39330200, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Rename struct stat and struct statfs to vfs_stat_t and vfs_statfs_t,
respectively. They are nonstandard vestiges of times when native file API
was modeled after POSIX API.

  • Property mode set to 100644
File size: 5.6 KB
Line 
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#include <str.h>
47
48#define NAME "viewer"
49
50#define WINDOW_WIDTH 1024
51#define WINDOW_HEIGHT 768
52
53#define DECORATION_WIDTH 8
54#define DECORATION_HEIGHT 28
55
56static size_t imgs_count;
57static size_t imgs_current = 0;
58static char **imgs;
59
60static window_t *main_window;
61static surface_t *surface = NULL;
62static canvas_t *canvas = NULL;
63
64static surface_coord_t img_width;
65static surface_coord_t img_height;
66
67static bool img_load(const char *, surface_t **);
68static bool img_setup(surface_t *);
69
70static void on_keyboard_event(widget_t *widget, void *data)
71{
72 kbd_event_t *event = (kbd_event_t *) data;
73 bool update = false;
74
75 if ((event->type == KEY_PRESS) && (event->c == 'q'))
76 exit(0);
77
78 if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_DOWN)) {
79 if (imgs_current == imgs_count - 1)
80 imgs_current = 0;
81 else
82 imgs_current++;
83
84 update = true;
85 }
86
87 if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_UP)) {
88 if (imgs_current == 0)
89 imgs_current = imgs_count - 1;
90 else
91 imgs_current--;
92
93 update = true;
94 }
95
96 if (update) {
97 surface_t *lsface;
98
99 if (!img_load(imgs[imgs_current], &lsface)) {
100 printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
101 exit(4);
102 }
103 if (!img_setup(lsface)) {
104 printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
105 exit(6);
106 }
107 }
108}
109
110static bool img_load(const char *fname, surface_t **p_local_surface)
111{
112 int fd;
113 errno_t rc = vfs_lookup_open(fname, WALK_REGULAR, MODE_READ, &fd);
114 if (rc != EOK)
115 return false;
116
117 vfs_stat_t stat;
118 rc = vfs_stat(fd, &stat);
119 if (rc != EOK) {
120 vfs_put(fd);
121 return false;
122 }
123
124 void *tga = malloc(stat.size);
125 if (tga == NULL) {
126 vfs_put(fd);
127 return false;
128 }
129
130 size_t nread;
131 rc = vfs_read(fd, (aoff64_t []) {0}, tga, stat.size, &nread);
132 if (rc != EOK || nread != stat.size) {
133 free(tga);
134 vfs_put(fd);
135 return false;
136 }
137
138 vfs_put(fd);
139
140 *p_local_surface = decode_tga(tga, stat.size, 0);
141 if (*p_local_surface == NULL) {
142 free(tga);
143 return false;
144 }
145
146 free(tga);
147
148 surface_get_resolution(*p_local_surface, &img_width, &img_height);
149
150 return true;
151}
152
153static bool img_setup(surface_t *local_surface)
154{
155 if (canvas != NULL) {
156 if (!update_canvas(canvas, local_surface)) {
157 surface_destroy(local_surface);
158 return false;
159 }
160 } else {
161 canvas = create_canvas(window_root(main_window), NULL,
162 img_width, img_height, local_surface);
163 if (canvas == NULL) {
164 surface_destroy(local_surface);
165 return false;
166 }
167
168 sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event);
169 }
170
171 if (surface != NULL)
172 surface_destroy(surface);
173
174 surface = local_surface;
175 return true;
176}
177
178int main(int argc, char *argv[])
179{
180 window_flags_t flags;
181 surface_t *lsface;
182 bool fullscreen;
183 sysarg_t dwidth;
184 sysarg_t dheight;
185
186 if (argc < 2) {
187 printf("Compositor server not specified.\n");
188 return 1;
189 }
190
191 if (argc < 3) {
192 printf("No image files specified.\n");
193 return 1;
194 }
195
196 imgs_count = argc - 2;
197 imgs = calloc(imgs_count, sizeof(char *));
198 if (imgs == NULL) {
199 printf("Out of memory.\n");
200 return 2;
201 }
202
203 for (int i = 0; i < argc - 2; i++) {
204 imgs[i] = str_dup(argv[i + 2]);
205 if (imgs[i] == NULL) {
206 printf("Out of memory.\n");
207 return 3;
208 }
209 }
210
211 if (!img_load(imgs[imgs_current], &lsface)) {
212 printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
213 return 4;
214 }
215
216 fullscreen = ((img_width == WINDOW_WIDTH) &&
217 (img_height == WINDOW_HEIGHT));
218
219 flags = WINDOW_MAIN;
220 if (!fullscreen)
221 flags |= WINDOW_DECORATED;
222
223 main_window = window_open(argv[1], NULL, flags, "viewer");
224 if (!main_window) {
225 printf("Cannot open main window.\n");
226 return 5;
227 }
228
229
230 if (!img_setup(lsface)) {
231 printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
232 return 6;
233 }
234
235 if (!fullscreen) {
236 dwidth = DECORATION_WIDTH;
237 dheight = DECORATION_HEIGHT;
238 } else {
239 dwidth = 0;
240 dheight = 0;
241 }
242
243 window_resize(main_window, 0, 0, img_width + dwidth,
244 img_height + dheight, WINDOW_PLACEMENT_ANY);
245 window_exec(main_window);
246
247 task_retval(0);
248 async_manager();
249
250 return 0;
251}
252
253/** @}
254 */
Note: See TracBrowser for help on using the repository browser.