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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bc41f3a3 was c064b58, checked in by Jakub Jermar <jakub@…>, 9 years ago

viewer: support for non-full-screen mode

  • Property mode set to 100644
File size: 5.5 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 <unistd.h>
37#include <fcntl.h>
38#include <sys/stat.h>
39#include <errno.h>
40#include <malloc.h>
41#include <stdbool.h>
42#include <window.h>
43#include <canvas.h>
44#include <surface.h>
45#include <codec/tga.h>
46#include <task.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 = open(fname, O_RDONLY);
113 if (fd < 0)
114 return false;
115
116 struct stat stat;
117 int rc = fstat(fd, &stat);
118 if (rc != 0) {
119 close(fd);
120 return false;
121 }
122
123 void *tga = malloc(stat.size);
124 if (tga == NULL) {
125 close(fd);
126 return false;
127 }
128
129 ssize_t rd = read(fd, tga, stat.size);
130 if ((rd < 0) || (rd != (ssize_t) stat.size)) {
131 free(tga);
132 close(fd);
133 return false;
134 }
135
136 close(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),
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], 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.