Changeset 3f81cdc in mainline
- Timestamp:
- 2024-03-11T10:17:27Z (10 months ago)
- Branches:
- master
- Children:
- c3d4b39b
- Parents:
- 0ffa40a7
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/viewer/viewer.c
r0ffa40a7 r3f81cdc 50 50 typedef struct { 51 51 ui_t *ui; 52 53 size_t imgs_count; 54 size_t imgs_current; 55 char **imgs; 56 57 ui_window_t *window; 58 gfx_bitmap_t *bitmap; 59 ui_image_t *image; 60 gfx_context_t *window_gc; 61 62 gfx_rect_t img_rect; 52 63 } viewer_t; 53 64 54 static size_t imgs_count; 55 static size_t imgs_current = 0; 56 static char **imgs; 57 58 static ui_window_t *window = NULL; 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 **, 65 static bool viewer_img_load(viewer_t *, const char *, gfx_bitmap_t **, 66 66 gfx_rect_t *); 67 static bool img_setup(gfx_context_t *, gfx_bitmap_t *, gfx_rect_t *);67 static bool viewer_img_setup(viewer_t *, gfx_bitmap_t *, gfx_rect_t *); 68 68 69 69 static void wnd_close(ui_window_t *, void *); … … 97 97 98 98 if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_DOWN)) { 99 if ( imgs_current ==imgs_count - 1)100 imgs_current = 0;99 if (viewer->imgs_current == viewer->imgs_count - 1) 100 viewer->imgs_current = 0; 101 101 else 102 imgs_current++;102 viewer->imgs_current++; 103 103 104 104 update = true; … … 106 106 107 107 if ((event->type == KEY_PRESS) && (event->key == KC_PAGE_UP)) { 108 if ( imgs_current == 0)109 imgs_current =imgs_count - 1;108 if (viewer->imgs_current == 0) 109 viewer->imgs_current = viewer->imgs_count - 1; 110 110 else 111 imgs_current--;111 viewer->imgs_current--; 112 112 113 113 update = true; … … 118 118 gfx_rect_t lrect; 119 119 120 if (!img_load(window_gc, imgs[imgs_current], &lbitmap, &lrect)) { 121 printf("Cannot load image \"%s\".\n", imgs[imgs_current]); 120 if (!viewer_img_load(viewer, viewer->imgs[viewer->imgs_current], 121 &lbitmap, &lrect)) { 122 printf("Cannot load image \"%s\".\n", 123 viewer->imgs[viewer->imgs_current]); 122 124 exit(4); 123 125 } 124 if (!img_setup(window_gc, lbitmap, &lrect)) { 125 printf("Cannot setup image \"%s\".\n", imgs[imgs_current]); 126 if (!viewer_img_setup(viewer, lbitmap, &lrect)) { 127 printf("Cannot setup image \"%s\".\n", 128 viewer->imgs[viewer->imgs_current]); 126 129 exit(6); 127 130 } … … 129 132 } 130 133 131 static bool img_load(gfx_context_t *gc, const char *fname,134 static bool viewer_img_load(viewer_t *viewer, const char *fname, 132 135 gfx_bitmap_t **rbitmap, gfx_rect_t *rect) 133 136 { … … 160 163 vfs_put(fd); 161 164 162 rc = decode_tga( gc, tga, stat.size, rbitmap, rect);165 rc = decode_tga(viewer->window_gc, tga, stat.size, rbitmap, rect); 163 166 if (rc != EOK) { 164 167 free(tga); … … 168 171 free(tga); 169 172 170 img_rect = *rect;173 viewer->img_rect = *rect; 171 174 return true; 172 175 } 173 176 174 static bool img_setup(gfx_context_t *gc, gfx_bitmap_t *bmp, gfx_rect_t *rect) 177 static bool viewer_img_setup(viewer_t *viewer, gfx_bitmap_t *bmp, 178 gfx_rect_t *rect) 175 179 { 176 180 gfx_rect_t arect; … … 179 183 errno_t rc; 180 184 181 ui_res = ui_window_get_res( window);182 183 ui_window_get_app_rect( window, &arect);185 ui_res = ui_window_get_res(viewer->window); 186 187 ui_window_get_app_rect(viewer->window, &arect); 184 188 185 189 /* Center image on application area */ 186 190 gfx_rect_ctr_on_rect(rect, &arect, &irect); 187 191 188 if ( image != NULL) {189 ui_image_set_bmp( image, bmp, rect);190 (void) ui_image_paint( image);191 ui_image_set_rect( image, &irect);192 if (viewer->image != NULL) { 193 ui_image_set_bmp(viewer->image, bmp, rect); 194 (void) ui_image_paint(viewer->image); 195 ui_image_set_rect(viewer->image, &irect); 192 196 } else { 193 rc = ui_image_create(ui_res, bmp, rect, & image);197 rc = ui_image_create(ui_res, bmp, rect, &viewer->image); 194 198 if (rc != EOK) { 195 199 gfx_bitmap_destroy(bmp); … … 197 201 } 198 202 199 ui_image_set_rect( image, &irect);200 ui_window_add( window, ui_image_ctl(image));201 } 202 203 if ( bitmap != NULL)204 gfx_bitmap_destroy( bitmap);205 206 bitmap = bmp;203 ui_image_set_rect(viewer->image, &irect); 204 ui_window_add(viewer->window, ui_image_ctl(viewer->image)); 205 } 206 207 if (viewer->bitmap != NULL) 208 gfx_bitmap_destroy(viewer->bitmap); 209 210 viewer->bitmap = bmp; 207 211 return true; 208 212 } … … 224 228 gfx_rect_t wrect; 225 229 gfx_coord2_t off; 226 ui_t *ui ;230 ui_t *ui = NULL; 227 231 ui_wnd_params_t params; 228 viewer_t viewer;232 viewer_t *viewer; 229 233 errno_t rc; 230 234 int i; 235 unsigned u; 236 237 viewer = calloc(1, sizeof(viewer_t)); 238 if (viewer == NULL) { 239 printf("Out of memory.\n"); 240 goto error; 241 } 231 242 232 243 i = 1; … … 237 248 printf("Argument missing.\n"); 238 249 print_syntax(); 239 return 1;250 goto error; 240 251 } 241 252 … … 247 258 printf("Invalid option '%s'.\n", argv[i]); 248 259 print_syntax(); 249 return 1;260 goto error; 250 261 } 251 262 } … … 254 265 printf("No image files specified.\n"); 255 266 print_syntax(); 256 return 1;257 } 258 259 imgs_count = argc - i;260 imgs = calloc(imgs_count, sizeof(char *));261 if ( imgs == NULL) {267 goto error; 268 } 269 270 viewer->imgs_count = argc - i; 271 viewer->imgs = calloc(viewer->imgs_count, sizeof(char *)); 272 if (viewer->imgs == NULL) { 262 273 printf("Out of memory.\n"); 263 return 1;274 goto error; 264 275 } 265 276 266 277 for (int j = 0; j < argc - i; j++) { 267 imgs[j] = str_dup(argv[i + j]);268 if ( imgs[j] == NULL) {278 viewer->imgs[j] = str_dup(argv[i + j]); 279 if (viewer->imgs[j] == NULL) { 269 280 printf("Out of memory.\n"); 270 return 3;281 goto error; 271 282 } 272 283 } … … 275 286 if (rc != EOK) { 276 287 printf("Error creating UI on display %s.\n", display_spec); 277 return 1;288 goto error; 278 289 } 279 290 … … 281 292 fullscreen = true; 282 293 283 viewer .ui = ui;294 viewer->ui = ui; 284 295 285 296 /* … … 299 310 } 300 311 301 rc = ui_window_create(ui, ¶ms, & window);312 rc = ui_window_create(ui, ¶ms, &viewer->window); 302 313 if (rc != EOK) { 303 314 printf("Error creating window.\n"); … … 305 316 } 306 317 307 window_gc = ui_window_get_gc(window); 308 309 ui_window_set_cb(window, &window_cb, (void *) &viewer); 310 311 if (!img_load(window_gc, imgs[imgs_current], &lbitmap, &lrect)) { 312 printf("Cannot load image \"%s\".\n", imgs[imgs_current]); 318 viewer->window_gc = ui_window_get_gc(viewer->window); 319 320 ui_window_set_cb(viewer->window, &window_cb, (void *)viewer); 321 322 if (!viewer_img_load(viewer, viewer->imgs[viewer->imgs_current], 323 &lbitmap, &lrect)) { 324 printf("Cannot load image \"%s\".\n", 325 viewer->imgs[viewer->imgs_current]); 313 326 goto error; 314 327 } … … 323 336 324 337 if (!fullscreen) { 325 rc = ui_window_resize( window, &rect);338 rc = ui_window_resize(viewer->window, &rect); 326 339 if (rc != EOK) { 327 340 printf("Error resizing window.\n"); … … 330 343 } 331 344 332 if (!img_setup(window_gc, lbitmap, &lrect)) { 333 printf("Cannot setup image \"%s\".\n", imgs[imgs_current]); 334 goto error; 335 } 336 337 rc = ui_window_paint(window); 345 if (!viewer_img_setup(viewer, lbitmap, &lrect)) { 346 printf("Cannot setup image \"%s\".\n", 347 viewer->imgs[viewer->imgs_current]); 348 goto error; 349 } 350 351 rc = ui_window_paint(viewer->window); 338 352 if (rc != EOK) { 339 353 printf("Error painting window.\n"); … … 343 357 ui_run(ui); 344 358 345 ui_window_destroy( window);359 ui_window_destroy(viewer->window); 346 360 ui_destroy(ui); 361 free(viewer); 347 362 348 363 return 0; 349 364 error: 350 if (window != NULL) 351 ui_window_destroy(window); 352 ui_destroy(ui); 365 if (viewer != NULL && viewer->imgs != NULL) { 366 for (u = 0; u < viewer->imgs_count; u++) { 367 if (viewer->imgs[i] != NULL) 368 free(viewer->imgs[i]); 369 } 370 free(viewer->imgs); 371 } 372 if (viewer != NULL && viewer->window != NULL) 373 ui_window_destroy(viewer->window); 374 if (ui != NULL) 375 ui_destroy(ui); 376 if (viewer != NULL) 377 free(viewer); 353 378 return 1; 354 379 }
Note:
See TracChangeset
for help on using the changeset viewer.