source: mainline/uspace/app/barber/barber.c@ c6f00b40

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c6f00b40 was fd11144, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Make display service argument optional

  • Property mode set to 100644
File size: 7.0 KB
Line 
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 <stdbool.h>
36#include <errno.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <task.h>
40#include <loc.h>
41#include <stats.h>
42#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>
50#include "images.h"
51
52#define NAME "barber"
53
54#define FRAMES IMAGES
55
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
62#define FRAME_WIDTH 59
63#define FRAME_HEIGHT 192
64
65#define LED_PERIOD 1000000
66#define LED_COLORS 7
67
68typedef struct {
69 link_t link;
70 service_id_t svc_id;
71 async_sess_t *sess;
72} led_dev_t;
73
74static char *winreg = NULL;
75
76static fibril_timer_t *led_timer = NULL;
77static list_t led_devs;
78static unsigned int led_color = 0;
79
80static pixel_t led_colors[LED_COLORS] = {
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
90static fibril_timer_t *frame_timer = NULL;
91static canvas_t *frame_canvas;
92static surface_t *frames[FRAMES];
93
94static unsigned int frame = 0;
95static unsigned int fps = MIN_FPS;
96
97static void led_timer_callback(void *);
98static void frame_timer_callback(void *);
99
100static bool decode_frames(void)
101{
102 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) {
105 printf("Unable to decode frame %u.\n", i);
106 return false;
107 }
108 }
109
110 return true;
111}
112
113static void plan_led_timer(void)
114{
115 fibril_timer_set(led_timer, LED_PERIOD, led_timer_callback, NULL);
116}
117
118static load_t get_load(void)
119{
120 size_t count;
121 load_t *load = stats_get_load(&count);
122 load_t load_val;
123
124 if ((load != NULL) && (count > 0)) {
125 load_val = load[0];
126 free(load);
127 } else
128 load_val = 0;
129
130 return load_val;
131}
132
133static void plan_frame_timer(usec_t render_time)
134{
135 /*
136 * Crank up the FPS unless we lack
137 * behind with the rendering and
138 * unless the load is not above
139 * a lower threshold.
140 */
141
142 usec_t delta = 1000000 / fps;
143 load_t load = get_load();
144
145 if ((delta >= render_time) && (load < MIN_LOAD))
146 fps++;
147
148 if (fps > MAX_FPS)
149 fps = MAX_FPS;
150
151 /*
152 * If we lack behind then immediately
153 * go to the lowest FPS.
154 */
155
156 if (delta < render_time)
157 fps = MIN_FPS;
158
159 /*
160 * Crank down the FPS if the current
161 * load is above an upper threshold.
162 */
163
164 if (load > MAX_LOAD)
165 fps--;
166
167 if (fps < MIN_FPS)
168 fps = MIN_FPS;
169
170 delta = 1000000 / fps;
171
172 fibril_timer_set(frame_timer, delta, frame_timer_callback, NULL);
173}
174
175static void led_timer_callback(void *data)
176{
177 pixel_t next_led_color = led_colors[led_color];
178
179 led_color++;
180 if (led_color >= LED_COLORS)
181 led_color = 0;
182
183 list_foreach(led_devs, link, led_dev_t, dev) {
184 if (dev->sess)
185 led_dev_color_set(dev->sess, next_led_color);
186 }
187
188 plan_led_timer();
189}
190
191static void frame_timer_callback(void *data)
192{
193 struct timespec prev;
194 getuptime(&prev);
195
196 frame++;
197 if (frame >= FRAMES)
198 frame = 0;
199
200 update_canvas(frame_canvas, frames[frame]);
201
202 struct timespec cur;
203 getuptime(&cur);
204
205 plan_frame_timer(NSEC2USEC(ts_sub_diff(&cur, &prev)));
206}
207
208static void loc_callback(void *arg)
209{
210 category_id_t led_cat;
211 errno_t rc = loc_category_get_id("led", &led_cat, IPC_FLAG_BLOCKING);
212 if (rc != EOK)
213 return;
214
215 service_id_t *svcs;
216 size_t count;
217 rc = loc_category_get_svcs(led_cat, &svcs, &count);
218 if (rc != EOK)
219 return;
220
221 for (size_t i = 0; i < count; i++) {
222 bool known = false;
223
224 /* Determine whether we already know this device. */
225 list_foreach(led_devs, link, led_dev_t, dev) {
226 if (dev->svc_id == svcs[i]) {
227 known = true;
228 break;
229 }
230 }
231
232 if (!known) {
233 led_dev_t *dev = (led_dev_t *) calloc(1, sizeof(led_dev_t));
234 if (!dev)
235 continue;
236
237 link_initialize(&dev->link);
238 dev->svc_id = svcs[i];
239 dev->sess = loc_service_connect(svcs[i], INTERFACE_DDF, 0);
240
241 list_append(&dev->link, &led_devs);
242 }
243 }
244
245 // FIXME: Handle LED device removal
246
247 free(svcs);
248}
249
250static void print_syntax(void)
251{
252 printf("Syntax: %s [-d <display>]\n", NAME);
253}
254
255int main(int argc, char *argv[])
256{
257 const char *display_svc = DISPLAY_DEFAULT;
258 int i;
259
260 i = 1;
261 while (i < argc) {
262 if (str_cmp(argv[i], "-d") == 0) {
263 ++i;
264 if (i >= argc) {
265 printf("Argument missing.\n");
266 print_syntax();
267 return 1;
268 }
269
270 display_svc = argv[i++];
271 } else {
272 printf("Invalid option '%s'.\n", argv[i]);
273 print_syntax();
274 return 1;
275 }
276 }
277
278 list_initialize(&led_devs);
279 errno_t rc = loc_register_cat_change_cb(loc_callback, NULL);
280 if (rc != EOK) {
281 printf("Unable to register callback for device discovery.\n");
282 return 1;
283 }
284
285 led_timer = fibril_timer_create(NULL);
286 if (!led_timer) {
287 printf("Unable to create LED timer.\n");
288 return 1;
289 }
290
291 frame_timer = fibril_timer_create(NULL);
292 if (!frame_timer) {
293 printf("Unable to create frame timer.\n");
294 return 1;
295 }
296
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);
320
321 plan_led_timer();
322 plan_frame_timer(0);
323
324 task_retval(0);
325 async_manager();
326
327 return 0;
328}
329
330/** @}
331 */
Note: See TracBrowser for help on using the repository browser.