source: mainline/uspace/app/viewer/viewer.c@ 223efc0

Last change on this file since 223efc0 was 8e3498b, checked in by Jiri Svoboda <jiri@…>, 8 years ago

vfs_read/write() should return error code separately from number of bytes transferred.

  • 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
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
55static size_t imgs_count;
56static size_t imgs_current = 0;
57static char **imgs;
58
59static window_t *main_window;
60static surface_t *surface = NULL;
61static canvas_t *canvas = NULL;
62
63static surface_coord_t img_width;
64static surface_coord_t img_height;
65
66static bool img_load(const char *, surface_t **);
67static bool img_setup(surface_t *);
68
69static 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
109static bool img_load(const char *fname, surface_t **p_local_surface)
110{
111 int fd = vfs_lookup_open(fname, WALK_REGULAR, MODE_READ);
112 if (fd < 0)
113 return false;
114
115 struct stat stat;
116 int rc = vfs_stat(fd, &stat);
117 if (rc != EOK) {
118 vfs_put(fd);
119 return false;
120 }
121
122 void *tga = malloc(stat.size);
123 if (tga == NULL) {
124 vfs_put(fd);
125 return false;
126 }
127
128 size_t nread;
129 rc = vfs_read(fd, (aoff64_t []) {0}, tga, stat.size, &nread);
130 if (rc != EOK || nread != stat.size) {
131 free(tga);
132 vfs_put(fd);
133 return false;
134 }
135
136 vfs_put(fd);
137
138 *p_local_surface = decode_tga(tga, stat.size, 0);
139 if (*p_local_surface == NULL) {
140 free(tga);
141 return false;
142 }
143
144 free(tga);
145
146 surface_get_resolution(*p_local_surface, &img_width, &img_height);
147
148 return true;
149}
150
151static bool img_setup(surface_t *local_surface)
152{
153 if (canvas != NULL) {
154 if (!update_canvas(canvas, local_surface)) {
155 surface_destroy(local_surface);
156 return false;
157 }
158 } else {
159 canvas = create_canvas(window_root(main_window), NULL,
160 img_width, img_height, local_surface);
161 if (canvas == NULL) {
162 surface_destroy(local_surface);
163 return false;
164 }
165
166 sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event);
167 }
168
169 if (surface != NULL)
170 surface_destroy(surface);
171
172 surface = local_surface;
173 return true;
174}
175
176int main(int argc, char *argv[])
177{
178 window_flags_t flags;
179 surface_t *lsface;
180 bool fullscreen;
181 sysarg_t dwidth;
182 sysarg_t dheight;
183
184 if (argc < 2) {
185 printf("Compositor server not specified.\n");
186 return 1;
187 }
188
189 if (argc < 3) {
190 printf("No image files specified.\n");
191 return 1;
192 }
193
194 imgs_count = argc - 2;
195 imgs = calloc(imgs_count, sizeof(char *));
196 if (imgs == NULL) {
197 printf("Out of memory.\n");
198 return 2;
199 }
200
201 for (int i = 0; i < argc - 2; i++) {
202 imgs[i] = str_dup(argv[i + 2]);
203 if (imgs[i] == NULL) {
204 printf("Out of memory.\n");
205 return 3;
206 }
207 }
208
209 if (!img_load(imgs[imgs_current], &lsface)) {
210 printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
211 return 4;
212 }
213
214 fullscreen = ((img_width == WINDOW_WIDTH) &&
215 (img_height == WINDOW_HEIGHT));
216
217 flags = WINDOW_MAIN;
218 if (!fullscreen)
219 flags |= WINDOW_DECORATED;
220
221 main_window = window_open(argv[1], NULL, flags, "viewer");
222 if (!main_window) {
223 printf("Cannot open main window.\n");
224 return 5;
225 }
226
227
228 if (!img_setup(lsface)) {
229 printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
230 return 6;
231 }
232
233 if (!fullscreen) {
234 dwidth = DECORATION_WIDTH;
235 dheight = DECORATION_HEIGHT;
236 } else {
237 dwidth = 0;
238 dheight = 0;
239 }
240
241 window_resize(main_window, 0, 0, img_width + dwidth,
242 img_height + dheight, WINDOW_PLACEMENT_ANY);
243 window_exec(main_window);
244
245 task_retval(0);
246 async_manager();
247
248 return 0;
249}
250
251/** @}
252 */
Note: See TracBrowser for help on using the repository browser.