Ignore:
File:
1 edited

Legend:

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

    r266ec54 rfd11144  
    11/*
    2  * Copyright (c) 2020 Jiri Svoboda
    32 * Copyright (c) 2013 Martin Decky
    43 * All rights reserved.
     
    3433 */
    3534
    36 #include <errno.h>
    37 #include <gfximage/tga.h>
    38 #include <stdbool.h>
    3935#include <stdio.h>
    4036#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>
    4146#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>
    4747
    4848#define NAME  "viewer"
    4949
    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
    5355
    5456static size_t imgs_count;
     
    5658static char **imgs;
    5759
    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 {
     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;
    9273        bool update = false;
    9374
     
    11495
    11596        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)) {
    120100                        printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
    121101                        exit(4);
    122102                }
    123                 if (!img_setup(window_gc, lbitmap, &lrect)) {
     103                if (!img_setup(lsface)) {
    124104                        printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
    125105                        exit(6);
     
    128108}
    129109
    130 static bool img_load(gfx_context_t *gc, const char *fname,
    131     gfx_bitmap_t **rbitmap, gfx_rect_t *rect)
     110static bool img_load(const char *fname, surface_t **p_local_surface)
    132111{
    133112        int fd;
     
    159138        vfs_put(fd);
    160139
    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) {
    163142                free(tga);
    164143                return false;
     
    167146        free(tga);
    168147
    169         img_rect = *rect;
     148        surface_get_resolution(*p_local_surface, &img_width, &img_height);
     149
    170150        return true;
    171151}
    172152
    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);
     153static 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                }
    191160        } 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);
    195165                        return false;
    196166                }
    197167
    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;
    206175        return true;
    207176}
     
    209178static void print_syntax(void)
    210179{
    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);
    214181}
    215182
    216183int main(int argc, char *argv[])
    217184{
    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;
    229191        int i;
    230192
     
    239201                        }
    240202
    241                         display_spec = argv[i++];
    242                 } else if (str_cmp(argv[i], "-f") == 0) {
    243                         ++i;
    244                         fullscreen = true;
     203                        display_svc = argv[i++];
    245204                } else {
    246205                        printf("Invalid option '%s'.\n", argv[i]);
     
    260219        if (imgs == NULL) {
    261220                printf("Out of memory.\n");
    262                 return 1;
     221                return 2;
    263222        }
    264223
     
    271230        }
    272231
    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)) {
     232        if (!img_load(imgs[imgs_current], &lsface)) {
    308233                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        }
    319254
    320255        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();
    340269
    341270        return 0;
Note: See TracChangeset for help on using the changeset viewer.