[a047aaa] | 1 | /*
|
---|
[12008adf] | 2 | * Copyright (c) 2020 Jiri Svoboda
|
---|
[a047aaa] | 3 | * Copyright (c) 2014 Martin Decky
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup barber
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[12008adf] | 36 | #include <device/led_dev.h>
|
---|
[a047aaa] | 37 | #include <errno.h>
|
---|
[12008adf] | 38 | #include <fibril_synch.h>
|
---|
[0576df9] | 39 | #include <gfximage/tga_gz.h>
|
---|
[12008adf] | 40 | #include <io/pixel.h>
|
---|
[a047aaa] | 41 | #include <loc.h>
|
---|
[9696b01] | 42 | #include <stats.h>
|
---|
[12008adf] | 43 | #include <stdbool.h>
|
---|
| 44 | #include <stdio.h>
|
---|
| 45 | #include <stdlib.h>
|
---|
[fd11144] | 46 | #include <str.h>
|
---|
[12008adf] | 47 | #include <ui/ui.h>
|
---|
| 48 | #include <ui/wdecor.h>
|
---|
| 49 | #include <ui/window.h>
|
---|
| 50 | #include <ui/image.h>
|
---|
[a047aaa] | 51 | #include "images.h"
|
---|
| 52 |
|
---|
| 53 | #define NAME "barber"
|
---|
| 54 |
|
---|
[9ce911d] | 55 | #define FRAMES IMAGES
|
---|
[a047aaa] | 56 |
|
---|
[9696b01] | 57 | #define MIN_FPS 1
|
---|
| 58 | #define MAX_FPS 25
|
---|
| 59 |
|
---|
| 60 | #define MIN_LOAD (LOAD_UNIT / 4)
|
---|
| 61 | #define MAX_LOAD (LOAD_UNIT / 3)
|
---|
| 62 |
|
---|
[a047aaa] | 63 | #define FRAME_WIDTH 59
|
---|
| 64 | #define FRAME_HEIGHT 192
|
---|
| 65 |
|
---|
[9696b01] | 66 | #define LED_PERIOD 1000000
|
---|
| 67 | #define LED_COLORS 7
|
---|
| 68 |
|
---|
| 69 | typedef struct {
|
---|
| 70 | link_t link;
|
---|
| 71 | service_id_t svc_id;
|
---|
| 72 | async_sess_t *sess;
|
---|
| 73 | } led_dev_t;
|
---|
[a047aaa] | 74 |
|
---|
[12008adf] | 75 | typedef struct {
|
---|
| 76 | ui_t *ui;
|
---|
| 77 | } barber_t;
|
---|
[a047aaa] | 78 |
|
---|
[9696b01] | 79 | static fibril_timer_t *led_timer = NULL;
|
---|
| 80 | static list_t led_devs;
|
---|
| 81 | static unsigned int led_color = 0;
|
---|
[a047aaa] | 82 |
|
---|
[9696b01] | 83 | static pixel_t led_colors[LED_COLORS] = {
|
---|
[a047aaa] | 84 | PIXEL(0xff, 0xff, 0x00, 0x00),
|
---|
| 85 | PIXEL(0xff, 0x00, 0xff, 0x00),
|
---|
| 86 | PIXEL(0xff, 0x00, 0x00, 0xff),
|
---|
| 87 | PIXEL(0xff, 0xff, 0xff, 0x00),
|
---|
| 88 | PIXEL(0xff, 0xff, 0x00, 0xff),
|
---|
| 89 | PIXEL(0xff, 0x00, 0xff, 0xff),
|
---|
| 90 | PIXEL(0xff, 0xff, 0xff, 0xff)
|
---|
| 91 | };
|
---|
| 92 |
|
---|
[9696b01] | 93 | static fibril_timer_t *frame_timer = NULL;
|
---|
[12008adf] | 94 | static ui_image_t *frame_img;
|
---|
| 95 | static gfx_bitmap_t *frame_bmp[FRAMES];
|
---|
[9696b01] | 96 |
|
---|
[a047aaa] | 97 | static unsigned int frame = 0;
|
---|
[9696b01] | 98 | static unsigned int fps = MIN_FPS;
|
---|
[a047aaa] | 99 |
|
---|
[9696b01] | 100 | static void led_timer_callback(void *);
|
---|
| 101 | static void frame_timer_callback(void *);
|
---|
| 102 |
|
---|
[12008adf] | 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)
|
---|
[9696b01] | 122 | {
|
---|
[0576df9] | 123 | gfx_rect_t rect;
|
---|
[12008adf] | 124 | errno_t rc;
|
---|
| 125 |
|
---|
[9ce911d] | 126 | for (unsigned int i = 0; i < FRAMES; i++) {
|
---|
[0576df9] | 127 | rc = decode_tga_gz(gc, images[i].addr, images[i].size,
|
---|
| 128 | &frame_bmp[i], &rect);
|
---|
| 129 | if (rc != EOK) {
|
---|
[9ce911d] | 130 | printf("Unable to decode frame %u.\n", i);
|
---|
[9696b01] | 131 | return false;
|
---|
| 132 | }
|
---|
[12008adf] | 133 |
|
---|
[0576df9] | 134 | (void) rect;
|
---|
[9696b01] | 135 | }
|
---|
[a35b458] | 136 |
|
---|
[9696b01] | 137 | return true;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[12008adf] | 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 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[9696b01] | 150 | static void plan_led_timer(void)
|
---|
| 151 | {
|
---|
| 152 | fibril_timer_set(led_timer, LED_PERIOD, led_timer_callback, NULL);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | static load_t get_load(void)
|
---|
| 156 | {
|
---|
| 157 | size_t count;
|
---|
| 158 | load_t *load = stats_get_load(&count);
|
---|
| 159 | load_t load_val;
|
---|
[a35b458] | 160 |
|
---|
[9696b01] | 161 | if ((load != NULL) && (count > 0)) {
|
---|
| 162 | load_val = load[0];
|
---|
| 163 | free(load);
|
---|
| 164 | } else
|
---|
| 165 | load_val = 0;
|
---|
[a35b458] | 166 |
|
---|
[9696b01] | 167 | return load_val;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[bd41ac52] | 170 | static void plan_frame_timer(usec_t render_time)
|
---|
[9696b01] | 171 | {
|
---|
| 172 | /*
|
---|
| 173 | * Crank up the FPS unless we lack
|
---|
| 174 | * behind with the rendering and
|
---|
| 175 | * unless the load is not above
|
---|
| 176 | * a lower threshold.
|
---|
| 177 | */
|
---|
[a35b458] | 178 |
|
---|
[bd41ac52] | 179 | usec_t delta = 1000000 / fps;
|
---|
[9696b01] | 180 | load_t load = get_load();
|
---|
[a35b458] | 181 |
|
---|
[9696b01] | 182 | if ((delta >= render_time) && (load < MIN_LOAD))
|
---|
| 183 | fps++;
|
---|
[a35b458] | 184 |
|
---|
[9696b01] | 185 | if (fps > MAX_FPS)
|
---|
| 186 | fps = MAX_FPS;
|
---|
[a35b458] | 187 |
|
---|
[9696b01] | 188 | /*
|
---|
| 189 | * If we lack behind then immediately
|
---|
| 190 | * go to the lowest FPS.
|
---|
| 191 | */
|
---|
[a35b458] | 192 |
|
---|
[9696b01] | 193 | if (delta < render_time)
|
---|
| 194 | fps = MIN_FPS;
|
---|
[a35b458] | 195 |
|
---|
[9696b01] | 196 | /*
|
---|
| 197 | * Crank down the FPS if the current
|
---|
| 198 | * load is above an upper threshold.
|
---|
| 199 | */
|
---|
[a35b458] | 200 |
|
---|
[9696b01] | 201 | if (load > MAX_LOAD)
|
---|
| 202 | fps--;
|
---|
[a35b458] | 203 |
|
---|
[9696b01] | 204 | if (fps < MIN_FPS)
|
---|
| 205 | fps = MIN_FPS;
|
---|
[a35b458] | 206 |
|
---|
[9696b01] | 207 | delta = 1000000 / fps;
|
---|
[a35b458] | 208 |
|
---|
[9696b01] | 209 | fibril_timer_set(frame_timer, delta, frame_timer_callback, NULL);
|
---|
| 210 | }
|
---|
[a047aaa] | 211 |
|
---|
[9696b01] | 212 | static void led_timer_callback(void *data)
|
---|
[a047aaa] | 213 | {
|
---|
[9696b01] | 214 | pixel_t next_led_color = led_colors[led_color];
|
---|
[a35b458] | 215 |
|
---|
[9696b01] | 216 | led_color++;
|
---|
| 217 | if (led_color >= LED_COLORS)
|
---|
| 218 | led_color = 0;
|
---|
[a35b458] | 219 |
|
---|
[a047aaa] | 220 | list_foreach(led_devs, link, led_dev_t, dev) {
|
---|
| 221 | if (dev->sess)
|
---|
[9696b01] | 222 | led_dev_color_set(dev->sess, next_led_color);
|
---|
[a047aaa] | 223 | }
|
---|
[a35b458] | 224 |
|
---|
[9696b01] | 225 | plan_led_timer();
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | static void frame_timer_callback(void *data)
|
---|
| 229 | {
|
---|
[bd41ac52] | 230 | struct timespec prev;
|
---|
[12008adf] | 231 | gfx_rect_t rect;
|
---|
[9696b01] | 232 | getuptime(&prev);
|
---|
[a35b458] | 233 |
|
---|
[a047aaa] | 234 | frame++;
|
---|
| 235 | if (frame >= FRAMES)
|
---|
| 236 | frame = 0;
|
---|
[a35b458] | 237 |
|
---|
[12008adf] | 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);
|
---|
[a35b458] | 245 |
|
---|
[bd41ac52] | 246 | struct timespec cur;
|
---|
[9696b01] | 247 | getuptime(&cur);
|
---|
[a35b458] | 248 |
|
---|
[bd41ac52] | 249 | plan_frame_timer(NSEC2USEC(ts_sub_diff(&cur, &prev)));
|
---|
[a047aaa] | 250 | }
|
---|
| 251 |
|
---|
[e89a06a] | 252 | static void loc_callback(void *arg)
|
---|
[a047aaa] | 253 | {
|
---|
| 254 | category_id_t led_cat;
|
---|
[b7fd2a0] | 255 | errno_t rc = loc_category_get_id("led", &led_cat, IPC_FLAG_BLOCKING);
|
---|
[a047aaa] | 256 | if (rc != EOK)
|
---|
| 257 | return;
|
---|
[a35b458] | 258 |
|
---|
[a047aaa] | 259 | service_id_t *svcs;
|
---|
| 260 | size_t count;
|
---|
| 261 | rc = loc_category_get_svcs(led_cat, &svcs, &count);
|
---|
| 262 | if (rc != EOK)
|
---|
| 263 | return;
|
---|
[a35b458] | 264 |
|
---|
[a047aaa] | 265 | for (size_t i = 0; i < count; i++) {
|
---|
| 266 | bool known = false;
|
---|
[a35b458] | 267 |
|
---|
[a047aaa] | 268 | /* Determine whether we already know this device. */
|
---|
| 269 | list_foreach(led_devs, link, led_dev_t, dev) {
|
---|
| 270 | if (dev->svc_id == svcs[i]) {
|
---|
| 271 | known = true;
|
---|
| 272 | break;
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
[a35b458] | 275 |
|
---|
[a047aaa] | 276 | if (!known) {
|
---|
| 277 | led_dev_t *dev = (led_dev_t *) calloc(1, sizeof(led_dev_t));
|
---|
| 278 | if (!dev)
|
---|
| 279 | continue;
|
---|
[a35b458] | 280 |
|
---|
[a047aaa] | 281 | link_initialize(&dev->link);
|
---|
| 282 | dev->svc_id = svcs[i];
|
---|
[f9b2cb4c] | 283 | dev->sess = loc_service_connect(svcs[i], INTERFACE_DDF, 0);
|
---|
[a35b458] | 284 |
|
---|
[a047aaa] | 285 | list_append(&dev->link, &led_devs);
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
[a35b458] | 288 |
|
---|
[a047aaa] | 289 | // FIXME: Handle LED device removal
|
---|
[a35b458] | 290 |
|
---|
[a047aaa] | 291 | free(svcs);
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[fd11144] | 294 | static void print_syntax(void)
|
---|
| 295 | {
|
---|
| 296 | printf("Syntax: %s [-d <display>]\n", NAME);
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[a047aaa] | 299 | int main(int argc, char *argv[])
|
---|
| 300 | {
|
---|
[12008adf] | 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;
|
---|
[fd11144] | 312 | int i;
|
---|
| 313 |
|
---|
| 314 | i = 1;
|
---|
| 315 | while (i < argc) {
|
---|
| 316 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
| 317 | ++i;
|
---|
| 318 | if (i >= argc) {
|
---|
| 319 | printf("Argument missing.\n");
|
---|
| 320 | print_syntax();
|
---|
| 321 | return 1;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[12008adf] | 324 | display_spec = argv[i++];
|
---|
[fd11144] | 325 | } else {
|
---|
| 326 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
| 327 | print_syntax();
|
---|
| 328 | return 1;
|
---|
| 329 | }
|
---|
[a047aaa] | 330 | }
|
---|
[a35b458] | 331 |
|
---|
[a047aaa] | 332 | list_initialize(&led_devs);
|
---|
[e89a06a] | 333 | errno_t rc = loc_register_cat_change_cb(loc_callback, NULL);
|
---|
[a047aaa] | 334 | if (rc != EOK) {
|
---|
| 335 | printf("Unable to register callback for device discovery.\n");
|
---|
| 336 | return 1;
|
---|
| 337 | }
|
---|
[a35b458] | 338 |
|
---|
[9696b01] | 339 | led_timer = fibril_timer_create(NULL);
|
---|
| 340 | if (!led_timer) {
|
---|
| 341 | printf("Unable to create LED timer.\n");
|
---|
| 342 | return 1;
|
---|
| 343 | }
|
---|
[a35b458] | 344 |
|
---|
[9696b01] | 345 | frame_timer = fibril_timer_create(NULL);
|
---|
| 346 | if (!frame_timer) {
|
---|
| 347 | printf("Unable to create frame timer.\n");
|
---|
[a047aaa] | 348 | return 1;
|
---|
| 349 | }
|
---|
[a35b458] | 350 |
|
---|
[12008adf] | 351 | rc = ui_create(display_spec, &ui);
|
---|
| 352 | if (rc != EOK) {
|
---|
| 353 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
[a047aaa] | 354 | return 1;
|
---|
[12008adf] | 355 | }
|
---|
[a35b458] | 356 |
|
---|
[12008adf] | 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(¶ms);
|
---|
| 363 | params.caption = "";
|
---|
[06d0c81] | 364 | params.placement = ui_wnd_place_bottom_right;
|
---|
[12008adf] | 365 | /*
|
---|
| 366 | * Compute window rectangle such that application area corresponds
|
---|
| 367 | * to rect
|
---|
| 368 | */
|
---|
[266ec54] | 369 | ui_wdecor_rect_from_app(params.style, &rect, &wrect);
|
---|
[12008adf] | 370 | off = wrect.p0;
|
---|
| 371 | gfx_rect_rtranslate(&off, &wrect, ¶ms.rect);
|
---|
| 372 |
|
---|
| 373 | barber.ui = ui;
|
---|
| 374 |
|
---|
| 375 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 376 | if (rc != EOK) {
|
---|
| 377 | printf("Error creating window.\n");
|
---|
[a047aaa] | 378 | return 1;
|
---|
| 379 | }
|
---|
[a35b458] | 380 |
|
---|
[12008adf] | 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);
|
---|
[a35b458] | 385 |
|
---|
[12008adf] | 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");
|
---|
[a047aaa] | 393 | return 1;
|
---|
| 394 | }
|
---|
[a35b458] | 395 |
|
---|
[12008adf] | 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 | }
|
---|
[a35b458] | 405 |
|
---|
[9696b01] | 406 | plan_led_timer();
|
---|
| 407 | plan_frame_timer(0);
|
---|
[a35b458] | 408 |
|
---|
[12008adf] | 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 |
|
---|
[442210e] | 414 | destroy_frames();
|
---|
| 415 |
|
---|
[12008adf] | 416 | ui_window_destroy(window);
|
---|
| 417 | ui_destroy(ui);
|
---|
| 418 |
|
---|
[a047aaa] | 419 | return 0;
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | /** @}
|
---|
| 423 | */
|
---|