source: mainline/uspace/app/viewer/viewer.c@ 38d150e

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

Prefer to get memory allocation functions through the standard stdlib header.

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