Changeset 7e38970d in mainline for uspace/app/viewer/viewer.c


Ignore:
Timestamp:
2020-12-07T00:08:37Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
25f26600
Parents:
7a873f0 (diff), 8596474 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'jxsvoboda-gfx' into master

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/viewer/viewer.c

    r7a873f0 r7e38970d  
    11/*
     2 * Copyright (c) 2020 Jiri Svoboda
    23 * Copyright (c) 2013 Martin Decky
    34 * All rights reserved.
     
    3334 */
    3435
     36#include <errno.h>
     37#include <gfximage/tga.h>
     38#include <stdbool.h>
    3539#include <stdio.h>
    3640#include <stdlib.h>
     41#include <str.h>
     42#include <ui/image.h>
     43#include <ui/ui.h>
     44#include <ui/wdecor.h>
     45#include <ui/window.h>
    3746#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>
    4747
    4848#define NAME  "viewer"
    4949
    50 #define WINDOW_WIDTH   1024
    51 #define WINDOW_HEIGHT  768
    52 
    53 #define DECORATION_WIDTH        8
    54 #define DECORATION_HEIGHT       28
     50typedef struct {
     51        ui_t *ui;
     52} viewer_t;
    5553
    5654static size_t imgs_count;
     
    5856static char **imgs;
    5957
    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;
     58static ui_window_t *window;
     59static gfx_bitmap_t *bitmap = NULL;
     60static ui_image_t *image = NULL;
     61static gfx_context_t *window_gc;
     62
     63static gfx_rect_t img_rect;
     64
     65static bool img_load(gfx_context_t *gc, const char *, gfx_bitmap_t **,
     66    gfx_rect_t *);
     67static bool img_setup(gfx_context_t *, gfx_bitmap_t *, gfx_rect_t *);
     68
     69static void wnd_close(ui_window_t *, void *);
     70static void wnd_kbd_event(ui_window_t *, void *, kbd_event_t *);
     71
     72static 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 */
     82static 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
     89static void wnd_kbd_event(ui_window_t *window, void *arg,
     90    kbd_event_t *event)
     91{
    7392        bool update = false;
    7493
     
    95114
    96115        if (update) {
    97                 surface_t *lsface;
    98 
    99                 if (!img_load(imgs[imgs_current], &lsface)) {
     116                gfx_bitmap_t *lbitmap;
     117                gfx_rect_t lrect;
     118
     119                if (!img_load(window_gc, imgs[imgs_current], &lbitmap, &lrect)) {
    100120                        printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
    101121                        exit(4);
    102122                }
    103                 if (!img_setup(lsface)) {
     123                if (!img_setup(window_gc, lbitmap, &lrect)) {
    104124                        printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
    105125                        exit(6);
     
    108128}
    109129
    110 static bool img_load(const char *fname, surface_t **p_local_surface)
     130static bool img_load(gfx_context_t *gc, const char *fname,
     131    gfx_bitmap_t **rbitmap, gfx_rect_t *rect)
    111132{
    112133        int fd;
     
    138159        vfs_put(fd);
    139160
    140         *p_local_surface = decode_tga(tga, stat.size, 0);
    141         if (*p_local_surface == NULL) {
     161        rc = decode_tga(gc, tga, stat.size, rbitmap, rect);
     162        if (rc != EOK) {
    142163                free(tga);
    143164                return false;
     
    146167        free(tga);
    147168
    148         surface_get_resolution(*p_local_surface, &img_width, &img_height);
    149 
     169        img_rect = *rect;
    150170        return true;
    151171}
    152172
    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);
     173static 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);
     191        } else {
     192                rc = ui_image_create(ui_res, bmp, rect, &image);
     193                if (rc != EOK) {
     194                        gfx_bitmap_destroy(bmp);
    158195                        return false;
    159196                }
    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);
    169         }
    170 
    171         if (surface != NULL)
    172                 surface_destroy(surface);
    173 
    174         surface = local_surface;
     197
     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;
    175206        return true;
    176207}
     
    178209static void print_syntax(void)
    179210{
    180         printf("Syntax: %s [-d <display>] <image-file>...\n", NAME);
     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");
    181214}
    182215
    183216int main(int argc, char *argv[])
    184217{
    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;
     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;
    191229        int i;
    192230
     
    201239                        }
    202240
    203                         display_svc = argv[i++];
     241                        display_spec = argv[i++];
     242                } else if (str_cmp(argv[i], "-f") == 0) {
     243                        ++i;
     244                        fullscreen = true;
    204245                } else {
    205246                        printf("Invalid option '%s'.\n", argv[i]);
     
    219260        if (imgs == NULL) {
    220261                printf("Out of memory.\n");
    221                 return 2;
     262                return 1;
    222263        }
    223264
     
    230271        }
    231272
    232         if (!img_load(imgs[imgs_current], &lsface)) {
     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(&params);
     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, &params, &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)) {
    233308                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)) {
     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);
     319
     320        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)) {
    251329                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();
     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);
    269340
    270341        return 0;
Note: See TracChangeset for help on using the changeset viewer.