Changes in uspace/app/viewer/viewer.c [266ec54:fd11144] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/viewer/viewer.c
r266ec54 rfd11144 1 1 /* 2 * Copyright (c) 2020 Jiri Svoboda3 2 * Copyright (c) 2013 Martin Decky 4 3 * All rights reserved. … … 34 33 */ 35 34 36 #include <errno.h>37 #include <gfximage/tga.h>38 #include <stdbool.h>39 35 #include <stdio.h> 40 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> 41 46 #include <str.h> 42 #include <ui/image.h>43 #include <ui/ui.h>44 #include <ui/wdecor.h>45 #include <ui/window.h>46 #include <vfs/vfs.h>47 47 48 48 #define NAME "viewer" 49 49 50 typedef struct { 51 ui_t *ui; 52 } viewer_t; 50 #define WINDOW_WIDTH 1024 51 #define WINDOW_HEIGHT 768 52 53 #define DECORATION_WIDTH 8 54 #define DECORATION_HEIGHT 28 53 55 54 56 static size_t imgs_count; … … 56 58 static char **imgs; 57 59 58 static ui_window_t *window; 59 static gfx_bitmap_t *bitmap = NULL; 60 static ui_image_t *image = NULL; 61 static gfx_context_t *window_gc; 62 63 static gfx_rect_t img_rect; 64 65 static bool img_load(gfx_context_t *gc, const char *, gfx_bitmap_t **, 66 gfx_rect_t *); 67 static bool img_setup(gfx_context_t *, gfx_bitmap_t *, gfx_rect_t *); 68 69 static void wnd_close(ui_window_t *, void *); 70 static void wnd_kbd_event(ui_window_t *, void *, kbd_event_t *); 71 72 static ui_window_cb_t window_cb = { 73 .close = wnd_close, 74 .kbd = wnd_kbd_event 75 }; 76 77 /** Window close request 78 * 79 * @param window Window 80 * @param arg Argument (calc_t *) 81 */ 82 static void wnd_close(ui_window_t *window, void *arg) 83 { 84 viewer_t *viewer = (viewer_t *) arg; 85 86 ui_quit(viewer->ui); 87 } 88 89 static void wnd_kbd_event(ui_window_t *window, void *arg, 90 kbd_event_t *event) 91 { 60 static window_t *main_window; 61 static surface_t *surface = NULL; 62 static canvas_t *canvas = NULL; 63 64 static surface_coord_t img_width; 65 static surface_coord_t img_height; 66 67 static bool img_load(const char *, surface_t **); 68 static bool img_setup(surface_t *); 69 70 static void on_keyboard_event(widget_t *widget, void *data) 71 { 72 kbd_event_t *event = (kbd_event_t *) data; 92 73 bool update = false; 93 74 … … 114 95 115 96 if (update) { 116 gfx_bitmap_t *lbitmap; 117 gfx_rect_t lrect; 118 119 if (!img_load(window_gc, imgs[imgs_current], &lbitmap, &lrect)) { 97 surface_t *lsface; 98 99 if (!img_load(imgs[imgs_current], &lsface)) { 120 100 printf("Cannot load image \"%s\".\n", imgs[imgs_current]); 121 101 exit(4); 122 102 } 123 if (!img_setup( window_gc, lbitmap, &lrect)) {103 if (!img_setup(lsface)) { 124 104 printf("Cannot setup image \"%s\".\n", imgs[imgs_current]); 125 105 exit(6); … … 128 108 } 129 109 130 static bool img_load(gfx_context_t *gc, const char *fname, 131 gfx_bitmap_t **rbitmap, gfx_rect_t *rect) 110 static bool img_load(const char *fname, surface_t **p_local_surface) 132 111 { 133 112 int fd; … … 159 138 vfs_put(fd); 160 139 161 rc = decode_tga(gc, tga, stat.size, rbitmap, rect);162 if ( rc != EOK) {140 *p_local_surface = decode_tga(tga, stat.size, 0); 141 if (*p_local_surface == NULL) { 163 142 free(tga); 164 143 return false; … … 167 146 free(tga); 168 147 169 img_rect = *rect; 148 surface_get_resolution(*p_local_surface, &img_width, &img_height); 149 170 150 return true; 171 151 } 172 152 173 static bool img_setup(gfx_context_t *gc, gfx_bitmap_t *bmp, gfx_rect_t *rect) 174 { 175 gfx_rect_t arect; 176 gfx_rect_t irect; 177 ui_resource_t *ui_res; 178 errno_t rc; 179 180 ui_res = ui_window_get_res(window); 181 182 ui_window_get_app_rect(window, &arect); 183 184 /* Center image on application area */ 185 gfx_rect_ctr_on_rect(rect, &arect, &irect); 186 187 if (image != NULL) { 188 ui_image_set_bmp(image, bmp, rect); 189 (void) ui_image_paint(image); 190 ui_image_set_rect(image, &irect); 153 static 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 } 191 160 } else { 192 rc = ui_image_create(ui_res, bmp, rect, &image); 193 if (rc != EOK) { 194 gfx_bitmap_destroy(bmp); 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); 195 165 return false; 196 166 } 197 167 198 ui_image_set_rect(image, &irect); 199 ui_window_add(window, ui_image_ctl(image)); 200 } 201 202 if (bitmap != NULL) 203 gfx_bitmap_destroy(bitmap); 204 205 bitmap = bmp; 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; 206 175 return true; 207 176 } … … 209 178 static void print_syntax(void) 210 179 { 211 printf("Syntax: %s [<options] <image-file>...\n", NAME); 212 printf("\t-d <display-spec> Use the specified display\n"); 213 printf("\t-f Full-screen mode\n"); 180 printf("Syntax: %s [-d <display>] <image-file>...\n", NAME); 214 181 } 215 182 216 183 int main(int argc, char *argv[]) 217 184 { 218 const char *display_spec = DISPLAY_DEFAULT; 219 gfx_bitmap_t *lbitmap; 220 gfx_rect_t lrect; 221 bool fullscreen = false; 222 gfx_rect_t rect; 223 gfx_rect_t wrect; 224 gfx_coord2_t off; 225 ui_t *ui; 226 ui_wnd_params_t params; 227 viewer_t viewer; 228 errno_t rc; 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; 229 191 int i; 230 192 … … 239 201 } 240 202 241 display_spec = argv[i++]; 242 } else if (str_cmp(argv[i], "-f") == 0) { 243 ++i; 244 fullscreen = true; 203 display_svc = argv[i++]; 245 204 } else { 246 205 printf("Invalid option '%s'.\n", argv[i]); … … 260 219 if (imgs == NULL) { 261 220 printf("Out of memory.\n"); 262 return 1;221 return 2; 263 222 } 264 223 … … 271 230 } 272 231 273 rc = ui_create(display_spec, &ui); 274 if (rc != EOK) { 275 printf("Error creating UI on display %s.\n", display_spec); 276 return 1; 277 } 278 279 viewer.ui = ui; 280 281 /* 282 * We don't know the image size yet, so create tiny window and resize 283 * later. 284 */ 285 ui_wnd_params_init(¶ms); 286 params.caption = "Viewer"; 287 params.rect.p0.x = 0; 288 params.rect.p0.y = 0; 289 params.rect.p1.x = 1; 290 params.rect.p1.y = 1; 291 292 if (fullscreen) { 293 params.style &= ~ui_wds_decorated; 294 params.placement = ui_wnd_place_full_screen; 295 } 296 297 rc = ui_window_create(ui, ¶ms, &window); 298 if (rc != EOK) { 299 printf("Error creating window.\n"); 300 return 1; 301 } 302 303 window_gc = ui_window_get_gc(window); 304 305 ui_window_set_cb(window, &window_cb, (void *) &viewer); 306 307 if (!img_load(window_gc, imgs[imgs_current], &lbitmap, &lrect)) { 232 if (!img_load(imgs[imgs_current], &lsface)) { 308 233 printf("Cannot load image \"%s\".\n", imgs[imgs_current]); 309 return 1; 310 } 311 312 /* 313 * Compute window rectangle such that application area corresponds 314 * to rect 315 */ 316 ui_wdecor_rect_from_app(params.style, &lrect, &wrect); 317 off = wrect.p0; 318 gfx_rect_rtranslate(&off, &wrect, &rect); 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 } 319 254 320 255 if (!fullscreen) { 321 rc = ui_window_resize(window, &rect); 322 if (rc != EOK) { 323 printf("Error resizing window.\n"); 324 return 1; 325 } 326 } 327 328 if (!img_setup(window_gc, lbitmap, &lrect)) { 329 printf("Cannot setup image \"%s\".\n", imgs[imgs_current]); 330 return 1; 331 } 332 333 rc = ui_window_paint(window); 334 if (rc != EOK) { 335 printf("Error painting window.\n"); 336 return 1; 337 } 338 339 ui_run(ui); 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(); 340 269 341 270 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.