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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c6f00b40 was fd11144, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Make display service argument optional

  • Property mode set to 100644
File size: 6.0 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 <draw/surface.h>
44#include <draw/codec.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
178static void print_syntax(void)
179{
180 printf("Syntax: %s [-d <display>] <image-file>...\n", NAME);
181}
182
183int main(int argc, char *argv[])
184{
185 const char *display_svc = DISPLAY_DEFAULT;
186 window_flags_t flags;
187 surface_t *lsface;
188 bool fullscreen;
189 sysarg_t dwidth;
190 sysarg_t dheight;
191 int i;
192
193 i = 1;
194 while (i < argc && argv[i][0] == '-') {
195 if (str_cmp(argv[i], "-d") == 0) {
196 ++i;
197 if (i >= argc) {
198 printf("Argument missing.\n");
199 print_syntax();
200 return 1;
201 }
202
203 display_svc = argv[i++];
204 } else {
205 printf("Invalid option '%s'.\n", argv[i]);
206 print_syntax();
207 return 1;
208 }
209 }
210
211 if (i >= argc) {
212 printf("No image files specified.\n");
213 print_syntax();
214 return 1;
215 }
216
217 imgs_count = argc - i;
218 imgs = calloc(imgs_count, sizeof(char *));
219 if (imgs == NULL) {
220 printf("Out of memory.\n");
221 return 2;
222 }
223
224 for (int j = 0; j < argc - i; j++) {
225 imgs[j] = str_dup(argv[i + j]);
226 if (imgs[j] == NULL) {
227 printf("Out of memory.\n");
228 return 3;
229 }
230 }
231
232 if (!img_load(imgs[imgs_current], &lsface)) {
233 printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
234 return 4;
235 }
236
237 fullscreen = ((img_width == WINDOW_WIDTH) &&
238 (img_height == WINDOW_HEIGHT));
239
240 flags = WINDOW_MAIN;
241 if (!fullscreen)
242 flags |= WINDOW_DECORATED;
243
244 main_window = window_open(display_svc, NULL, flags, "viewer");
245 if (!main_window) {
246 printf("Cannot open main window.\n");
247 return 5;
248 }
249
250 if (!img_setup(lsface)) {
251 printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
252 return 6;
253 }
254
255 if (!fullscreen) {
256 dwidth = DECORATION_WIDTH;
257 dheight = DECORATION_HEIGHT;
258 } else {
259 dwidth = 0;
260 dheight = 0;
261 }
262
263 window_resize(main_window, 0, 0, img_width + dwidth,
264 img_height + dheight, WINDOW_PLACEMENT_ANY);
265 window_exec(main_window);
266
267 task_retval(0);
268 async_manager();
269
270 return 0;
271}
272
273/** @}
274 */
Note: See TracBrowser for help on using the repository browser.