1 | /*
|
---|
2 | * Copyright (c) 2020 Jiri Svoboda
|
---|
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 |
|
---|
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>
|
---|
43 | #include <stdbool.h>
|
---|
44 | #include <stdio.h>
|
---|
45 | #include <stdlib.h>
|
---|
46 | #include <str.h>
|
---|
47 | #include <ui/ui.h>
|
---|
48 | #include <ui/wdecor.h>
|
---|
49 | #include <ui/window.h>
|
---|
50 | #include <ui/image.h>
|
---|
51 | #include "images.h"
|
---|
52 |
|
---|
53 | #define NAME "barber"
|
---|
54 |
|
---|
55 | #define FRAMES IMAGES
|
---|
56 |
|
---|
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 |
|
---|
63 | #define FRAME_WIDTH 59
|
---|
64 | #define FRAME_HEIGHT 192
|
---|
65 |
|
---|
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;
|
---|
74 |
|
---|
75 | typedef struct {
|
---|
76 | ui_t *ui;
|
---|
77 | } barber_t;
|
---|
78 |
|
---|
79 | static fibril_timer_t *led_timer = NULL;
|
---|
80 | static list_t led_devs;
|
---|
81 | static unsigned int led_color = 0;
|
---|
82 |
|
---|
83 | static pixel_t led_colors[LED_COLORS] = {
|
---|
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 |
|
---|
93 | static fibril_timer_t *frame_timer = NULL;
|
---|
94 | static ui_image_t *frame_img;
|
---|
95 | static gfx_bitmap_t *frame_bmp[FRAMES];
|
---|
96 |
|
---|
97 | static unsigned int frame = 0;
|
---|
98 | static unsigned int fps = MIN_FPS;
|
---|
99 |
|
---|
100 | static void led_timer_callback(void *);
|
---|
101 | static void frame_timer_callback(void *);
|
---|
102 |
|
---|
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 |
|
---|
126 | 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) {
|
---|
130 | printf("Unable to decode frame %u.\n", i);
|
---|
131 | return false;
|
---|
132 | }
|
---|
133 |
|
---|
134 | (void) rect;
|
---|
135 | }
|
---|
136 |
|
---|
137 | 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 | }
|
---|
148 | }
|
---|
149 |
|
---|
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;
|
---|
160 |
|
---|
161 | if ((load != NULL) && (count > 0)) {
|
---|
162 | load_val = load[0];
|
---|
163 | free(load);
|
---|
164 | } else
|
---|
165 | load_val = 0;
|
---|
166 |
|
---|
167 | return load_val;
|
---|
168 | }
|
---|
169 |
|
---|
170 | static void plan_frame_timer(usec_t render_time)
|
---|
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 | */
|
---|
178 |
|
---|
179 | usec_t delta = 1000000 / fps;
|
---|
180 | load_t load = get_load();
|
---|
181 |
|
---|
182 | if ((delta >= render_time) && (load < MIN_LOAD))
|
---|
183 | fps++;
|
---|
184 |
|
---|
185 | if (fps > MAX_FPS)
|
---|
186 | fps = MAX_FPS;
|
---|
187 |
|
---|
188 | /*
|
---|
189 | * If we lack behind then immediately
|
---|
190 | * go to the lowest FPS.
|
---|
191 | */
|
---|
192 |
|
---|
193 | if (delta < render_time)
|
---|
194 | fps = MIN_FPS;
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * Crank down the FPS if the current
|
---|
198 | * load is above an upper threshold.
|
---|
199 | */
|
---|
200 |
|
---|
201 | if (load > MAX_LOAD)
|
---|
202 | fps--;
|
---|
203 |
|
---|
204 | if (fps < MIN_FPS)
|
---|
205 | fps = MIN_FPS;
|
---|
206 |
|
---|
207 | delta = 1000000 / fps;
|
---|
208 |
|
---|
209 | fibril_timer_set(frame_timer, delta, frame_timer_callback, NULL);
|
---|
210 | }
|
---|
211 |
|
---|
212 | static void led_timer_callback(void *data)
|
---|
213 | {
|
---|
214 | pixel_t next_led_color = led_colors[led_color];
|
---|
215 |
|
---|
216 | led_color++;
|
---|
217 | if (led_color >= LED_COLORS)
|
---|
218 | led_color = 0;
|
---|
219 |
|
---|
220 | list_foreach(led_devs, link, led_dev_t, dev) {
|
---|
221 | if (dev->sess)
|
---|
222 | led_dev_color_set(dev->sess, next_led_color);
|
---|
223 | }
|
---|
224 |
|
---|
225 | plan_led_timer();
|
---|
226 | }
|
---|
227 |
|
---|
228 | static void frame_timer_callback(void *data)
|
---|
229 | {
|
---|
230 | struct timespec prev;
|
---|
231 | gfx_rect_t rect;
|
---|
232 | getuptime(&prev);
|
---|
233 |
|
---|
234 | frame++;
|
---|
235 | if (frame >= FRAMES)
|
---|
236 | frame = 0;
|
---|
237 |
|
---|
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);
|
---|
245 |
|
---|
246 | struct timespec cur;
|
---|
247 | getuptime(&cur);
|
---|
248 |
|
---|
249 | plan_frame_timer(NSEC2USEC(ts_sub_diff(&cur, &prev)));
|
---|
250 | }
|
---|
251 |
|
---|
252 | static void loc_callback(void *arg)
|
---|
253 | {
|
---|
254 | category_id_t led_cat;
|
---|
255 | errno_t rc = loc_category_get_id("led", &led_cat, IPC_FLAG_BLOCKING);
|
---|
256 | if (rc != EOK)
|
---|
257 | return;
|
---|
258 |
|
---|
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;
|
---|
264 |
|
---|
265 | for (size_t i = 0; i < count; i++) {
|
---|
266 | bool known = false;
|
---|
267 |
|
---|
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 | }
|
---|
275 |
|
---|
276 | if (!known) {
|
---|
277 | led_dev_t *dev = (led_dev_t *) calloc(1, sizeof(led_dev_t));
|
---|
278 | if (!dev)
|
---|
279 | continue;
|
---|
280 |
|
---|
281 | link_initialize(&dev->link);
|
---|
282 | dev->svc_id = svcs[i];
|
---|
283 | dev->sess = loc_service_connect(svcs[i], INTERFACE_DDF, 0);
|
---|
284 |
|
---|
285 | list_append(&dev->link, &led_devs);
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | // FIXME: Handle LED device removal
|
---|
290 |
|
---|
291 | free(svcs);
|
---|
292 | }
|
---|
293 |
|
---|
294 | static void print_syntax(void)
|
---|
295 | {
|
---|
296 | printf("Syntax: %s [-d <display>]\n", NAME);
|
---|
297 | }
|
---|
298 |
|
---|
299 | int main(int argc, char *argv[])
|
---|
300 | {
|
---|
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;
|
---|
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 |
|
---|
324 | display_spec = argv[i++];
|
---|
325 | } else {
|
---|
326 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
327 | print_syntax();
|
---|
328 | return 1;
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | list_initialize(&led_devs);
|
---|
333 | errno_t rc = loc_register_cat_change_cb(loc_callback, NULL);
|
---|
334 | if (rc != EOK) {
|
---|
335 | printf("Unable to register callback for device discovery.\n");
|
---|
336 | return 1;
|
---|
337 | }
|
---|
338 |
|
---|
339 | led_timer = fibril_timer_create(NULL);
|
---|
340 | if (!led_timer) {
|
---|
341 | printf("Unable to create LED timer.\n");
|
---|
342 | return 1;
|
---|
343 | }
|
---|
344 |
|
---|
345 | frame_timer = fibril_timer_create(NULL);
|
---|
346 | if (!frame_timer) {
|
---|
347 | printf("Unable to create frame timer.\n");
|
---|
348 | return 1;
|
---|
349 | }
|
---|
350 |
|
---|
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(¶ms);
|
---|
363 | params.caption = "Barber Pole";
|
---|
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, ¶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");
|
---|
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 | }
|
---|
405 |
|
---|
406 | plan_led_timer();
|
---|
407 | plan_frame_timer(0);
|
---|
408 |
|
---|
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 | destroy_frames();
|
---|
415 |
|
---|
416 | ui_window_destroy(window);
|
---|
417 | ui_destroy(ui);
|
---|
418 |
|
---|
419 | return 0;
|
---|
420 | }
|
---|
421 |
|
---|
422 | /** @}
|
---|
423 | */
|
---|