Changeset 2c9fdeed in mainline
- Timestamp:
- 2020-11-12T20:47:57Z (4 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 38f5598
- Parents:
- 12008adf
- Location:
- uspace/app/viewer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/viewer/meson.build
r12008adf r2c9fdeed 27 27 # 28 28 29 deps = [ ' gui' ]29 deps = [ 'ui', 'draw', 'compress' ] 30 30 src = files('viewer.c') 31 31 -
uspace/app/viewer/viewer.c
r12008adf r2c9fdeed 1 1 /* 2 * Copyright (c) 2020 Jiri Svoboda 2 3 * Copyright (c) 2013 Martin Decky 3 4 * All rights reserved. … … 33 34 */ 34 35 36 #include <draw/surface.h> 37 #include <draw/codec.h> 38 #include <errno.h> 39 #include <stdbool.h> 35 40 #include <stdio.h> 36 41 #include <stdlib.h> 42 #include <str.h> 43 #include <ui/image.h> 44 #include <ui/ui.h> 45 #include <ui/wdecor.h> 46 #include <ui/window.h> 37 47 #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 48 49 #define NAME "viewer" 49 50 50 #define WINDOW_WIDTH 1024 51 #define WINDOW_HEIGHT 768 52 53 #define DECORATION_WIDTH 8 54 #define DECORATION_HEIGHT 28 51 typedef struct { 52 ui_t *ui; 53 } viewer_t; 55 54 56 55 static size_t imgs_count; … … 58 57 static char **imgs; 59 58 60 static window_t *main_window;59 static ui_window_t *window; 61 60 static surface_t *surface = NULL; 62 static canvas_t *canvas = NULL; 61 static ui_image_t *image = NULL; 62 static gfx_context_t *window_gc; 63 63 64 64 static surface_coord_t img_width; … … 66 66 67 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; 68 static bool img_setup(gfx_context_t *, surface_t *); 69 70 static void wnd_close(ui_window_t *, void *); 71 static void wnd_kbd_event(ui_window_t *, void *, kbd_event_t *); 72 73 static ui_window_cb_t window_cb = { 74 .close = wnd_close, 75 .kbd = wnd_kbd_event 76 }; 77 78 /** Window close request 79 * 80 * @param window Window 81 * @param arg Argument (calc_t *) 82 */ 83 static void wnd_close(ui_window_t *window, void *arg) 84 { 85 viewer_t *viewer = (viewer_t *) arg; 86 87 ui_quit(viewer->ui); 88 } 89 90 static void wnd_kbd_event(ui_window_t *window, void *arg, 91 kbd_event_t *event) 92 { 73 93 bool update = false; 74 94 … … 101 121 exit(4); 102 122 } 103 if (!img_setup( lsface)) {123 if (!img_setup(window_gc, lsface)) { 104 124 printf("Cannot setup image \"%s\".\n", imgs[imgs_current]); 105 125 exit(6); … … 138 158 vfs_put(fd); 139 159 140 *p_local_surface = decode_tga(tga, stat.size, 0);160 *p_local_surface = decode_tga(tga, stat.size, SURFACE_FLAG_SHARED); 141 161 if (*p_local_surface == NULL) { 142 162 free(tga); … … 151 171 } 152 172 153 static bool img_setup(surface_t *local_surface) 154 { 155 if (canvas != NULL) { 156 if (!update_canvas(canvas, local_surface)) { 173 static bool img_setup(gfx_context_t *gc, surface_t *local_surface) 174 { 175 surface_coord_t w, h; 176 gfx_bitmap_params_t params; 177 gfx_bitmap_alloc_t alloc; 178 gfx_bitmap_t *bmp; 179 gfx_rect_t arect; 180 gfx_rect_t irect; 181 ui_resource_t *ui_res; 182 errno_t rc; 183 184 ui_res = ui_window_get_res(window); 185 186 surface_get_resolution(local_surface, &w, &h); 187 gfx_bitmap_params_init(¶ms); 188 params.rect.p1.x = w; 189 params.rect.p1.y = h; 190 191 ui_window_get_app_rect(window, &arect); 192 gfx_rect_translate(&arect.p0, ¶ms.rect, &irect); 193 194 alloc.pitch = sizeof(uint32_t) * w; 195 alloc.off0 = 0; 196 alloc.pixels = surface_direct_access(local_surface); 197 198 rc = gfx_bitmap_create(gc, ¶ms, &alloc, &bmp); 199 if (rc != EOK) { 200 surface_destroy(local_surface); 201 return false; 202 } 203 204 if (image != NULL) { 205 ui_image_set_bmp(image, bmp, ¶ms.rect); 206 (void) ui_image_paint(image); 207 ui_image_set_rect(image, &irect); 208 } else { 209 rc = ui_image_create(ui_res, bmp, ¶ms.rect, &image); 210 if (rc != EOK) { 211 gfx_bitmap_destroy(bmp); 157 212 surface_destroy(local_surface); 158 213 return false; 159 214 } 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); 215 216 ui_image_set_rect(image, &irect); 217 ui_window_add(window, ui_image_ctl(image)); 169 218 } 170 219 … … 178 227 static void print_syntax(void) 179 228 { 180 printf("Syntax: %s [-d <display>] <image-file>...\n", NAME); 229 printf("Syntax: %s [<options] <image-file>...\n", NAME); 230 printf("\t-d <display-spec> Use the specified display\n"); 231 printf("\t-f Full-screen mode\n"); 181 232 } 182 233 183 234 int main(int argc, char *argv[]) 184 235 { 185 const char *display_svc = DISPLAY_DEFAULT; 186 window_flags_t flags; 236 const char *display_spec = DISPLAY_DEFAULT; 187 237 surface_t *lsface; 188 bool fullscreen; 189 sysarg_t dwidth; 190 sysarg_t dheight; 238 bool fullscreen = false; 239 gfx_rect_t rect; 240 gfx_rect_t wrect; 241 gfx_coord2_t off; 242 ui_t *ui; 243 ui_wnd_params_t params; 244 viewer_t viewer; 245 errno_t rc; 191 246 int i; 192 247 … … 201 256 } 202 257 203 display_svc = argv[i++]; 258 display_spec = argv[i++]; 259 } else if (str_cmp(argv[i], "-f") == 0) { 260 fullscreen = true; 204 261 } else { 205 262 printf("Invalid option '%s'.\n", argv[i]); … … 219 276 if (imgs == NULL) { 220 277 printf("Out of memory.\n"); 221 return 2;278 return 1; 222 279 } 223 280 … … 232 289 if (!img_load(imgs[imgs_current], &lsface)) { 233 290 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)) { 291 return 1; 292 } 293 294 // TODO Fullscreen mode 295 if (fullscreen) { 296 printf("Fullscreen mode not implemented.\n"); 297 return 1; 298 } 299 300 rc = ui_create(display_spec, &ui); 301 if (rc != EOK) { 302 printf("Error creating UI on display %s.\n", display_spec); 303 return 1; 304 } 305 306 viewer.ui = ui; 307 308 rect.p0.x = 0; 309 rect.p0.y = 0; 310 rect.p1.x = img_width; 311 rect.p1.y = img_height; 312 313 ui_wnd_params_init(¶ms); 314 params.caption = "Viewer"; 315 /* 316 * Compute window rectangle such that application area corresponds 317 * to rect 318 */ 319 ui_wdecor_rect_from_app(&rect, &wrect); 320 off = wrect.p0; 321 gfx_rect_rtranslate(&off, &wrect, ¶ms.rect); 322 323 rc = ui_window_create(ui, ¶ms, &window); 324 if (rc != EOK) { 325 printf("Error creating window.\n"); 326 return 1; 327 } 328 329 window_gc = ui_window_get_gc(window); 330 331 ui_window_set_cb(window, &window_cb, (void *) &viewer); 332 333 if (!img_setup(window_gc, lsface)) { 251 334 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(); 335 return 1; 336 } 337 338 rc = ui_window_paint(window); 339 if (rc != EOK) { 340 printf("Error painting window.\n"); 341 return 1; 342 } 343 344 ui_run(ui); 269 345 270 346 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.