Ignore:
File:
1 edited

Legend:

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

    rfd11144 r266ec54  
    11/*
     2 * Copyright (c) 2020 Jiri Svoboda
    23 * Copyright (c) 2014 Martin Decky
    34 * All rights reserved.
     
    3334 */
    3435
     36#include <device/led_dev.h>
     37#include <errno.h>
     38#include <fibril_synch.h>
     39#include <gfximage/tga_gz.h>
     40#include <io/pixel.h>
     41#include <loc.h>
     42#include <stats.h>
    3543#include <stdbool.h>
    36 #include <errno.h>
    3744#include <stdio.h>
    3845#include <stdlib.h>
    39 #include <task.h>
    40 #include <loc.h>
    41 #include <stats.h>
    4246#include <str.h>
    43 #include <fibril_synch.h>
    44 #include <io/pixel.h>
    45 #include <device/led_dev.h>
    46 #include <window.h>
    47 #include <canvas.h>
    48 #include <draw/surface.h>
    49 #include <draw/codec.h>
     47#include <ui/ui.h>
     48#include <ui/wdecor.h>
     49#include <ui/window.h>
     50#include <ui/image.h>
    5051#include "images.h"
    5152
     
    7273} led_dev_t;
    7374
    74 static char *winreg = NULL;
     75typedef struct {
     76        ui_t *ui;
     77} barber_t;
    7578
    7679static fibril_timer_t *led_timer = NULL;
     
    8992
    9093static fibril_timer_t *frame_timer = NULL;
    91 static canvas_t *frame_canvas;
    92 static surface_t *frames[FRAMES];
     94static ui_image_t *frame_img;
     95static gfx_bitmap_t *frame_bmp[FRAMES];
    9396
    9497static unsigned int frame = 0;
     
    98101static void frame_timer_callback(void *);
    99102
    100 static bool decode_frames(void)
    101 {
     103static void wnd_close(ui_window_t *, void *);
     104
     105static ui_window_cb_t window_cb = {
     106        .close = wnd_close
     107};
     108
     109/** Window close button was clicked.
     110 *
     111 * @param window Window
     112 * @param arg Argument (launcher)
     113 */
     114static void wnd_close(ui_window_t *window, void *arg)
     115{
     116        barber_t *barber = (barber_t *) arg;
     117
     118        ui_quit(barber->ui);
     119}
     120
     121static bool decode_frames(gfx_context_t *gc)
     122{
     123        gfx_rect_t rect;
     124        errno_t rc;
     125
    102126        for (unsigned int i = 0; i < FRAMES; i++) {
    103                 frames[i] = decode_tga_gz(images[i].addr, images[i].size, 0);
    104                 if (frames[i] == NULL) {
     127                rc = decode_tga_gz(gc, images[i].addr, images[i].size,
     128                    &frame_bmp[i], &rect);
     129                if (rc != EOK) {
    105130                        printf("Unable to decode frame %u.\n", i);
    106131                        return false;
    107132                }
     133
     134                (void) rect;
    108135        }
    109136
    110137        return true;
     138}
     139
     140static void destroy_frames(void)
     141{
     142        unsigned i;
     143
     144        for (i = 0; i < FRAMES; i++) {
     145                gfx_bitmap_destroy(frame_bmp[i]);
     146                frame_bmp[i] = NULL;
     147        }
    111148}
    112149
     
    192229{
    193230        struct timespec prev;
     231        gfx_rect_t rect;
    194232        getuptime(&prev);
    195233
     
    198236                frame = 0;
    199237
    200         update_canvas(frame_canvas, frames[frame]);
     238        rect.p0.x = 0;
     239        rect.p0.y = 0;
     240        rect.p1.x = FRAME_WIDTH;
     241        rect.p1.y = FRAME_HEIGHT;
     242
     243        ui_image_set_bmp(frame_img, frame_bmp[frame], &rect);
     244        (void) ui_image_paint(frame_img);
    201245
    202246        struct timespec cur;
     
    255299int main(int argc, char *argv[])
    256300{
    257         const char *display_svc = DISPLAY_DEFAULT;
     301        const char *display_spec = UI_DISPLAY_DEFAULT;
     302        barber_t barber;
     303        ui_t *ui;
     304        ui_wnd_params_t params;
     305        ui_window_t *window;
     306        ui_resource_t *ui_res;
     307        gfx_rect_t rect;
     308        gfx_rect_t wrect;
     309        gfx_rect_t app_rect;
     310        gfx_context_t *gc;
     311        gfx_coord2_t off;
    258312        int i;
    259313
     
    268322                        }
    269323
    270                         display_svc = argv[i++];
     324                        display_spec = argv[i++];
    271325                } else {
    272326                        printf("Invalid option '%s'.\n", argv[i]);
     
    295349        }
    296350
    297         if (!decode_frames())
    298                 return 1;
    299 
    300         winreg = argv[1];
    301         window_t *main_window = window_open(display_svc, NULL,
    302             WINDOW_MAIN | WINDOW_DECORATED, "barber");
    303         if (!main_window) {
    304                 printf("Cannot open main window.\n");
    305                 return 1;
    306         }
    307 
    308         frame_canvas = create_canvas(window_root(main_window), NULL,
    309             FRAME_WIDTH, FRAME_HEIGHT, frames[frame]);
    310 
    311         if (!frame_canvas) {
    312                 window_close(main_window);
    313                 printf("Cannot create widgets.\n");
    314                 return 1;
    315         }
    316 
    317         window_resize(main_window, 0, 0, FRAME_WIDTH + 8, FRAME_HEIGHT + 28,
    318             WINDOW_PLACEMENT_RIGHT | WINDOW_PLACEMENT_BOTTOM);
    319         window_exec(main_window);
     351        rc = ui_create(display_spec, &ui);
     352        if (rc != EOK) {
     353                printf("Error creating UI on display %s.\n", display_spec);
     354                return 1;
     355        }
     356
     357        rect.p0.x = 0;
     358        rect.p0.y = 0;
     359        rect.p1.x = FRAME_WIDTH;
     360        rect.p1.y = FRAME_HEIGHT;
     361
     362        ui_wnd_params_init(&params);
     363        params.caption = "";
     364        params.placement = ui_wnd_place_bottom_right;
     365        /*
     366         * Compute window rectangle such that application area corresponds
     367         * to rect
     368         */
     369        ui_wdecor_rect_from_app(params.style, &rect, &wrect);
     370        off = wrect.p0;
     371        gfx_rect_rtranslate(&off, &wrect, &params.rect);
     372
     373        barber.ui = ui;
     374
     375        rc = ui_window_create(ui, &params, &window);
     376        if (rc != EOK) {
     377                printf("Error creating window.\n");
     378                return 1;
     379        }
     380
     381        ui_res = ui_window_get_res(window);
     382        gc = ui_window_get_gc(window);
     383        ui_window_get_app_rect(window, &app_rect);
     384        ui_window_set_cb(window, &window_cb, (void *) &barber);
     385
     386        if (!decode_frames(gc))
     387                return 1;
     388
     389        rc = ui_image_create(ui_res, frame_bmp[frame], &rect,
     390            &frame_img);
     391        if (rc != EOK) {
     392                printf("Error creating UI.\n");
     393                return 1;
     394        }
     395
     396        ui_image_set_rect(frame_img, &app_rect);
     397
     398        ui_window_add(window, ui_image_ctl(frame_img));
     399
     400        rc = ui_window_paint(window);
     401        if (rc != EOK) {
     402                printf("Error painting window.\n");
     403                return 1;
     404        }
    320405
    321406        plan_led_timer();
    322407        plan_frame_timer(0);
    323408
    324         task_retval(0);
    325         async_manager();
     409        ui_run(ui);
     410
     411        /* Unlink bitmap from image so it is not destroyed along with it */
     412        ui_image_set_bmp(frame_img, NULL, &rect);
     413
     414        ui_window_destroy(window);
     415        ui_destroy(ui);
     416
     417        destroy_frames();
    326418
    327419        return 0;
Note: See TracChangeset for help on using the changeset viewer.