Ignore:
File:
1 edited

Legend:

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

    r266ec54 rfd11144  
    11/*
    2  * Copyright (c) 2020 Jiri Svoboda
    32 * Copyright (c) 2014 Martin Decky
    43 * All rights reserved.
     
    3433 */
    3534
    36 #include <device/led_dev.h>
     35#include <stdbool.h>
    3736#include <errno.h>
    38 #include <fibril_synch.h>
    39 #include <gfximage/tga_gz.h>
    40 #include <io/pixel.h>
     37#include <stdio.h>
     38#include <stdlib.h>
     39#include <task.h>
    4140#include <loc.h>
    4241#include <stats.h>
    43 #include <stdbool.h>
    44 #include <stdio.h>
    45 #include <stdlib.h>
    4642#include <str.h>
    47 #include <ui/ui.h>
    48 #include <ui/wdecor.h>
    49 #include <ui/window.h>
    50 #include <ui/image.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>
    5150#include "images.h"
    5251
     
    7372} led_dev_t;
    7473
    75 typedef struct {
    76         ui_t *ui;
    77 } barber_t;
     74static char *winreg = NULL;
    7875
    7976static fibril_timer_t *led_timer = NULL;
     
    9289
    9390static fibril_timer_t *frame_timer = NULL;
    94 static ui_image_t *frame_img;
    95 static gfx_bitmap_t *frame_bmp[FRAMES];
     91static canvas_t *frame_canvas;
     92static surface_t *frames[FRAMES];
    9693
    9794static unsigned int frame = 0;
     
    10198static void frame_timer_callback(void *);
    10299
    103 static void wnd_close(ui_window_t *, void *);
    104 
    105 static 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  */
    114 static 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 
    121 static bool decode_frames(gfx_context_t *gc)
    122 {
    123         gfx_rect_t rect;
    124         errno_t rc;
    125 
     100static bool decode_frames(void)
     101{
    126102        for (unsigned int i = 0; i < FRAMES; i++) {
    127                 rc = decode_tga_gz(gc, images[i].addr, images[i].size,
    128                     &frame_bmp[i], &rect);
    129                 if (rc != EOK) {
     103                frames[i] = decode_tga_gz(images[i].addr, images[i].size, 0);
     104                if (frames[i] == NULL) {
    130105                        printf("Unable to decode frame %u.\n", i);
    131106                        return false;
    132107                }
    133 
    134                 (void) rect;
    135108        }
    136109
    137110        return true;
    138 }
    139 
    140 static 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         }
    148111}
    149112
     
    229192{
    230193        struct timespec prev;
    231         gfx_rect_t rect;
    232194        getuptime(&prev);
    233195
     
    236198                frame = 0;
    237199
    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);
     200        update_canvas(frame_canvas, frames[frame]);
    245201
    246202        struct timespec cur;
     
    299255int main(int argc, char *argv[])
    300256{
    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;
     257        const char *display_svc = DISPLAY_DEFAULT;
    312258        int i;
    313259
     
    322268                        }
    323269
    324                         display_spec = argv[i++];
     270                        display_svc = argv[i++];
    325271                } else {
    326272                        printf("Invalid option '%s'.\n", argv[i]);
     
    349295        }
    350296
    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         }
     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);
    405320
    406321        plan_led_timer();
    407322        plan_frame_timer(0);
    408323
    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();
     324        task_retval(0);
     325        async_manager();
    418326
    419327        return 0;
Note: See TracChangeset for help on using the changeset viewer.