[a047aaa] | 1 | /*
|
---|
| 2 | * Copyright (c) 2014 Martin Decky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup barber
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <unistd.h>
|
---|
| 36 | #include <stdbool.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <stdio.h>
|
---|
| 39 | #include <malloc.h>
|
---|
| 40 | #include <task.h>
|
---|
| 41 | #include <loc.h>
|
---|
[9696b01] | 42 | #include <stats.h>
|
---|
[a047aaa] | 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 <surface.h>
|
---|
| 49 | #include <codec/tga.gz.h>
|
---|
| 50 | #include "images.h"
|
---|
| 51 |
|
---|
| 52 | #define NAME "barber"
|
---|
| 53 |
|
---|
| 54 | #define FRAMES 30
|
---|
| 55 |
|
---|
[9696b01] | 56 | #define MIN_FPS 1
|
---|
| 57 | #define MAX_FPS 25
|
---|
| 58 |
|
---|
| 59 | #define MIN_LOAD (LOAD_UNIT / 4)
|
---|
| 60 | #define MAX_LOAD (LOAD_UNIT / 3)
|
---|
| 61 |
|
---|
[a047aaa] | 62 | #define FRAME_WIDTH 59
|
---|
| 63 | #define FRAME_HEIGHT 192
|
---|
| 64 |
|
---|
[9696b01] | 65 | #define LED_PERIOD 1000000
|
---|
| 66 | #define LED_COLORS 7
|
---|
| 67 |
|
---|
| 68 | typedef struct {
|
---|
| 69 | link_t link;
|
---|
| 70 | service_id_t svc_id;
|
---|
| 71 | async_sess_t *sess;
|
---|
| 72 | } led_dev_t;
|
---|
[a047aaa] | 73 |
|
---|
| 74 | static char *winreg = NULL;
|
---|
| 75 |
|
---|
[9696b01] | 76 | static fibril_timer_t *led_timer = NULL;
|
---|
| 77 | static list_t led_devs;
|
---|
| 78 | static unsigned int led_color = 0;
|
---|
[a047aaa] | 79 |
|
---|
[9696b01] | 80 | static pixel_t led_colors[LED_COLORS] = {
|
---|
[a047aaa] | 81 | PIXEL(0xff, 0xff, 0x00, 0x00),
|
---|
| 82 | PIXEL(0xff, 0x00, 0xff, 0x00),
|
---|
| 83 | PIXEL(0xff, 0x00, 0x00, 0xff),
|
---|
| 84 | PIXEL(0xff, 0xff, 0xff, 0x00),
|
---|
| 85 | PIXEL(0xff, 0xff, 0x00, 0xff),
|
---|
| 86 | PIXEL(0xff, 0x00, 0xff, 0xff),
|
---|
| 87 | PIXEL(0xff, 0xff, 0xff, 0xff)
|
---|
| 88 | };
|
---|
| 89 |
|
---|
[9696b01] | 90 | static fibril_timer_t *frame_timer = NULL;
|
---|
| 91 | static canvas_t *frame_canvas;
|
---|
| 92 | static surface_t *frames[FRAMES];
|
---|
| 93 |
|
---|
[a047aaa] | 94 | static unsigned int frame = 0;
|
---|
[9696b01] | 95 | static unsigned int fps = MIN_FPS;
|
---|
[a047aaa] | 96 |
|
---|
[9696b01] | 97 | static void led_timer_callback(void *);
|
---|
| 98 | static void frame_timer_callback(void *);
|
---|
| 99 |
|
---|
| 100 | static bool decode_frames(void)
|
---|
| 101 | {
|
---|
| 102 | frames[0] = decode_tga_gz((void *) frame01_tga_gz, frame01_tga_gz_size, 0);
|
---|
| 103 | frames[1] = decode_tga_gz((void *) frame02_tga_gz, frame02_tga_gz_size, 0);
|
---|
| 104 | frames[2] = decode_tga_gz((void *) frame03_tga_gz, frame03_tga_gz_size, 0);
|
---|
| 105 | frames[3] = decode_tga_gz((void *) frame04_tga_gz, frame04_tga_gz_size, 0);
|
---|
| 106 | frames[4] = decode_tga_gz((void *) frame05_tga_gz, frame05_tga_gz_size, 0);
|
---|
| 107 | frames[5] = decode_tga_gz((void *) frame06_tga_gz, frame06_tga_gz_size, 0);
|
---|
| 108 | frames[6] = decode_tga_gz((void *) frame07_tga_gz, frame07_tga_gz_size, 0);
|
---|
| 109 | frames[7] = decode_tga_gz((void *) frame08_tga_gz, frame08_tga_gz_size, 0);
|
---|
| 110 | frames[8] = decode_tga_gz((void *) frame09_tga_gz, frame09_tga_gz_size, 0);
|
---|
| 111 | frames[9] = decode_tga_gz((void *) frame10_tga_gz, frame10_tga_gz_size, 0);
|
---|
| 112 | frames[10] = decode_tga_gz((void *) frame11_tga_gz, frame11_tga_gz_size, 0);
|
---|
| 113 | frames[11] = decode_tga_gz((void *) frame12_tga_gz, frame12_tga_gz_size, 0);
|
---|
| 114 | frames[12] = decode_tga_gz((void *) frame13_tga_gz, frame13_tga_gz_size, 0);
|
---|
| 115 | frames[13] = decode_tga_gz((void *) frame14_tga_gz, frame14_tga_gz_size, 0);
|
---|
| 116 | frames[14] = decode_tga_gz((void *) frame15_tga_gz, frame15_tga_gz_size, 0);
|
---|
| 117 | frames[15] = decode_tga_gz((void *) frame16_tga_gz, frame16_tga_gz_size, 0);
|
---|
| 118 | frames[16] = decode_tga_gz((void *) frame17_tga_gz, frame17_tga_gz_size, 0);
|
---|
| 119 | frames[17] = decode_tga_gz((void *) frame18_tga_gz, frame18_tga_gz_size, 0);
|
---|
| 120 | frames[18] = decode_tga_gz((void *) frame19_tga_gz, frame19_tga_gz_size, 0);
|
---|
| 121 | frames[19] = decode_tga_gz((void *) frame20_tga_gz, frame20_tga_gz_size, 0);
|
---|
| 122 | frames[20] = decode_tga_gz((void *) frame21_tga_gz, frame21_tga_gz_size, 0);
|
---|
| 123 | frames[21] = decode_tga_gz((void *) frame22_tga_gz, frame22_tga_gz_size, 0);
|
---|
| 124 | frames[22] = decode_tga_gz((void *) frame23_tga_gz, frame23_tga_gz_size, 0);
|
---|
| 125 | frames[23] = decode_tga_gz((void *) frame24_tga_gz, frame24_tga_gz_size, 0);
|
---|
| 126 | frames[24] = decode_tga_gz((void *) frame25_tga_gz, frame25_tga_gz_size, 0);
|
---|
| 127 | frames[25] = decode_tga_gz((void *) frame26_tga_gz, frame26_tga_gz_size, 0);
|
---|
| 128 | frames[26] = decode_tga_gz((void *) frame27_tga_gz, frame27_tga_gz_size, 0);
|
---|
| 129 | frames[27] = decode_tga_gz((void *) frame28_tga_gz, frame28_tga_gz_size, 0);
|
---|
| 130 | frames[28] = decode_tga_gz((void *) frame29_tga_gz, frame29_tga_gz_size, 0);
|
---|
| 131 | frames[29] = decode_tga_gz((void *) frame30_tga_gz, frame30_tga_gz_size, 0);
|
---|
| 132 |
|
---|
| 133 | for (unsigned int frame = 0; frame < FRAMES; frame++) {
|
---|
| 134 | if (frames[frame] == NULL) {
|
---|
| 135 | printf("Unable to decode frame %u.\n", frame);
|
---|
| 136 | return false;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | return true;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | static void plan_led_timer(void)
|
---|
| 144 | {
|
---|
| 145 | fibril_timer_set(led_timer, LED_PERIOD, led_timer_callback, NULL);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | static load_t get_load(void)
|
---|
| 149 | {
|
---|
| 150 | size_t count;
|
---|
| 151 | load_t *load = stats_get_load(&count);
|
---|
| 152 | load_t load_val;
|
---|
| 153 |
|
---|
| 154 | if ((load != NULL) && (count > 0)) {
|
---|
| 155 | load_val = load[0];
|
---|
| 156 | free(load);
|
---|
| 157 | } else
|
---|
| 158 | load_val = 0;
|
---|
| 159 |
|
---|
| 160 | return load_val;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | static void plan_frame_timer(suseconds_t render_time)
|
---|
| 164 | {
|
---|
| 165 | /*
|
---|
| 166 | * Crank up the FPS unless we lack
|
---|
| 167 | * behind with the rendering and
|
---|
| 168 | * unless the load is not above
|
---|
| 169 | * a lower threshold.
|
---|
| 170 | */
|
---|
| 171 |
|
---|
| 172 | suseconds_t delta = 1000000 / fps;
|
---|
| 173 | load_t load = get_load();
|
---|
| 174 |
|
---|
| 175 | if ((delta >= render_time) && (load < MIN_LOAD))
|
---|
| 176 | fps++;
|
---|
| 177 |
|
---|
| 178 | if (fps > MAX_FPS)
|
---|
| 179 | fps = MAX_FPS;
|
---|
| 180 |
|
---|
| 181 | /*
|
---|
| 182 | * If we lack behind then immediately
|
---|
| 183 | * go to the lowest FPS.
|
---|
| 184 | */
|
---|
| 185 |
|
---|
| 186 | if (delta < render_time)
|
---|
| 187 | fps = MIN_FPS;
|
---|
| 188 |
|
---|
| 189 | /*
|
---|
| 190 | * Crank down the FPS if the current
|
---|
| 191 | * load is above an upper threshold.
|
---|
| 192 | */
|
---|
| 193 |
|
---|
| 194 | if (load > MAX_LOAD)
|
---|
| 195 | fps--;
|
---|
| 196 |
|
---|
| 197 | if (fps < MIN_FPS)
|
---|
| 198 | fps = MIN_FPS;
|
---|
| 199 |
|
---|
| 200 | delta = 1000000 / fps;
|
---|
| 201 |
|
---|
| 202 | fibril_timer_set(frame_timer, delta, frame_timer_callback, NULL);
|
---|
| 203 | }
|
---|
[a047aaa] | 204 |
|
---|
[9696b01] | 205 | static void led_timer_callback(void *data)
|
---|
[a047aaa] | 206 | {
|
---|
[9696b01] | 207 | pixel_t next_led_color = led_colors[led_color];
|
---|
[a047aaa] | 208 |
|
---|
[9696b01] | 209 | led_color++;
|
---|
| 210 | if (led_color >= LED_COLORS)
|
---|
| 211 | led_color = 0;
|
---|
[a047aaa] | 212 |
|
---|
| 213 | list_foreach(led_devs, link, led_dev_t, dev) {
|
---|
| 214 | if (dev->sess)
|
---|
[9696b01] | 215 | led_dev_color_set(dev->sess, next_led_color);
|
---|
[a047aaa] | 216 | }
|
---|
| 217 |
|
---|
[9696b01] | 218 | plan_led_timer();
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | static void frame_timer_callback(void *data)
|
---|
| 222 | {
|
---|
| 223 | struct timeval prev;
|
---|
| 224 | getuptime(&prev);
|
---|
| 225 |
|
---|
[a047aaa] | 226 | frame++;
|
---|
| 227 | if (frame >= FRAMES)
|
---|
| 228 | frame = 0;
|
---|
| 229 |
|
---|
| 230 | update_canvas(frame_canvas, frames[frame]);
|
---|
| 231 |
|
---|
[9696b01] | 232 | struct timeval cur;
|
---|
| 233 | getuptime(&cur);
|
---|
| 234 |
|
---|
[7f9d97f3] | 235 | plan_frame_timer(tv_sub_diff(&cur, &prev));
|
---|
[a047aaa] | 236 | }
|
---|
| 237 |
|
---|
| 238 | static void loc_callback(void)
|
---|
| 239 | {
|
---|
| 240 | category_id_t led_cat;
|
---|
| 241 | int rc = loc_category_get_id("led", &led_cat, IPC_FLAG_BLOCKING);
|
---|
| 242 | if (rc != EOK)
|
---|
| 243 | return;
|
---|
| 244 |
|
---|
| 245 | service_id_t *svcs;
|
---|
| 246 | size_t count;
|
---|
| 247 | rc = loc_category_get_svcs(led_cat, &svcs, &count);
|
---|
| 248 | if (rc != EOK)
|
---|
| 249 | return;
|
---|
| 250 |
|
---|
| 251 | for (size_t i = 0; i < count; i++) {
|
---|
| 252 | bool known = false;
|
---|
| 253 |
|
---|
| 254 | /* Determine whether we already know this device. */
|
---|
| 255 | list_foreach(led_devs, link, led_dev_t, dev) {
|
---|
| 256 | if (dev->svc_id == svcs[i]) {
|
---|
| 257 | known = true;
|
---|
| 258 | break;
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | if (!known) {
|
---|
| 263 | led_dev_t *dev = (led_dev_t *) calloc(1, sizeof(led_dev_t));
|
---|
| 264 | if (!dev)
|
---|
| 265 | continue;
|
---|
| 266 |
|
---|
| 267 | link_initialize(&dev->link);
|
---|
| 268 | dev->svc_id = svcs[i];
|
---|
[f9b2cb4c] | 269 | dev->sess = loc_service_connect(svcs[i], INTERFACE_DDF, 0);
|
---|
[a047aaa] | 270 |
|
---|
| 271 | list_append(&dev->link, &led_devs);
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | // FIXME: Handle LED device removal
|
---|
| 276 |
|
---|
| 277 | free(svcs);
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | int main(int argc, char *argv[])
|
---|
| 281 | {
|
---|
| 282 | if (argc < 2) {
|
---|
| 283 | printf("Compositor server not specified.\n");
|
---|
| 284 | return 1;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | list_initialize(&led_devs);
|
---|
| 288 | int rc = loc_register_cat_change_cb(loc_callback);
|
---|
| 289 | if (rc != EOK) {
|
---|
| 290 | printf("Unable to register callback for device discovery.\n");
|
---|
| 291 | return 1;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[9696b01] | 294 | led_timer = fibril_timer_create(NULL);
|
---|
| 295 | if (!led_timer) {
|
---|
| 296 | printf("Unable to create LED timer.\n");
|
---|
| 297 | return 1;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | frame_timer = fibril_timer_create(NULL);
|
---|
| 301 | if (!frame_timer) {
|
---|
| 302 | printf("Unable to create frame timer.\n");
|
---|
[a047aaa] | 303 | return 1;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | if (!decode_frames())
|
---|
| 307 | return 1;
|
---|
| 308 |
|
---|
| 309 | winreg = argv[1];
|
---|
[10cb47e] | 310 | window_t *main_window = window_open(argv[1], NULL,
|
---|
[2c7fdaa] | 311 | WINDOW_MAIN | WINDOW_DECORATED, "barber");
|
---|
[a047aaa] | 312 | if (!main_window) {
|
---|
| 313 | printf("Cannot open main window.\n");
|
---|
| 314 | return 1;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[10cb47e] | 317 | frame_canvas = create_canvas(window_root(main_window), NULL,
|
---|
[a047aaa] | 318 | FRAME_WIDTH, FRAME_HEIGHT, frames[frame]);
|
---|
| 319 |
|
---|
| 320 | if (!frame_canvas) {
|
---|
| 321 | window_close(main_window);
|
---|
| 322 | printf("Cannot create widgets.\n");
|
---|
| 323 | return 1;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | window_resize(main_window, 0, 0, FRAME_WIDTH + 8, FRAME_HEIGHT + 28,
|
---|
| 327 | WINDOW_PLACEMENT_RIGHT | WINDOW_PLACEMENT_BOTTOM);
|
---|
| 328 | window_exec(main_window);
|
---|
| 329 |
|
---|
[9696b01] | 330 | plan_led_timer();
|
---|
| 331 | plan_frame_timer(0);
|
---|
[a047aaa] | 332 |
|
---|
| 333 | task_retval(0);
|
---|
| 334 | async_manager();
|
---|
| 335 |
|
---|
| 336 | return 0;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | /** @}
|
---|
| 340 | */
|
---|