Get rid of sys/time.h
[helenos.git] / uspace / app / barber / barber.c
blob7476ecd22c1231616a17db567009e9baa746aae3
1 /*
2 * Copyright (c) 2014 Martin Decky
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup barber
30 * @{
32 /** @file
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 <fibril_synch.h>
43 #include <io/pixel.h>
44 #include <device/led_dev.h>
45 #include <window.h>
46 #include <canvas.h>
47 #include <surface.h>
48 #include <codec/tga.gz.h>
49 #include "images.h"
51 #define NAME "barber"
53 #define FRAMES IMAGES
55 #define MIN_FPS 1
56 #define MAX_FPS 25
58 #define MIN_LOAD (LOAD_UNIT / 4)
59 #define MAX_LOAD (LOAD_UNIT / 3)
61 #define FRAME_WIDTH 59
62 #define FRAME_HEIGHT 192
64 #define LED_PERIOD 1000000
65 #define LED_COLORS 7
67 typedef struct {
68 link_t link;
69 service_id_t svc_id;
70 async_sess_t *sess;
71 } led_dev_t;
73 static char *winreg = NULL;
75 static fibril_timer_t *led_timer = NULL;
76 static list_t led_devs;
77 static unsigned int led_color = 0;
79 static pixel_t led_colors[LED_COLORS] = {
80 PIXEL(0xff, 0xff, 0x00, 0x00),
81 PIXEL(0xff, 0x00, 0xff, 0x00),
82 PIXEL(0xff, 0x00, 0x00, 0xff),
83 PIXEL(0xff, 0xff, 0xff, 0x00),
84 PIXEL(0xff, 0xff, 0x00, 0xff),
85 PIXEL(0xff, 0x00, 0xff, 0xff),
86 PIXEL(0xff, 0xff, 0xff, 0xff)
89 static fibril_timer_t *frame_timer = NULL;
90 static canvas_t *frame_canvas;
91 static surface_t *frames[FRAMES];
93 static unsigned int frame = 0;
94 static unsigned int fps = MIN_FPS;
96 static void led_timer_callback(void *);
97 static void frame_timer_callback(void *);
99 static bool decode_frames(void)
101 for (unsigned int i = 0; i < FRAMES; i++) {
102 frames[i] = decode_tga_gz(images[i].addr, images[i].size, 0);
103 if (frames[i] == NULL) {
104 printf("Unable to decode frame %u.\n", i);
105 return false;
109 return true;
112 static void plan_led_timer(void)
114 fibril_timer_set(led_timer, LED_PERIOD, led_timer_callback, NULL);
117 static load_t get_load(void)
119 size_t count;
120 load_t *load = stats_get_load(&count);
121 load_t load_val;
123 if ((load != NULL) && (count > 0)) {
124 load_val = load[0];
125 free(load);
126 } else
127 load_val = 0;
129 return load_val;
132 static void plan_frame_timer(usec_t render_time)
135 * Crank up the FPS unless we lack
136 * behind with the rendering and
137 * unless the load is not above
138 * a lower threshold.
141 usec_t delta = 1000000 / fps;
142 load_t load = get_load();
144 if ((delta >= render_time) && (load < MIN_LOAD))
145 fps++;
147 if (fps > MAX_FPS)
148 fps = MAX_FPS;
151 * If we lack behind then immediately
152 * go to the lowest FPS.
155 if (delta < render_time)
156 fps = MIN_FPS;
159 * Crank down the FPS if the current
160 * load is above an upper threshold.
163 if (load > MAX_LOAD)
164 fps--;
166 if (fps < MIN_FPS)
167 fps = MIN_FPS;
169 delta = 1000000 / fps;
171 fibril_timer_set(frame_timer, delta, frame_timer_callback, NULL);
174 static void led_timer_callback(void *data)
176 pixel_t next_led_color = led_colors[led_color];
178 led_color++;
179 if (led_color >= LED_COLORS)
180 led_color = 0;
182 list_foreach(led_devs, link, led_dev_t, dev) {
183 if (dev->sess)
184 led_dev_color_set(dev->sess, next_led_color);
187 plan_led_timer();
190 static void frame_timer_callback(void *data)
192 struct timespec prev;
193 getuptime(&prev);
195 frame++;
196 if (frame >= FRAMES)
197 frame = 0;
199 update_canvas(frame_canvas, frames[frame]);
201 struct timespec cur;
202 getuptime(&cur);
204 plan_frame_timer(NSEC2USEC(ts_sub_diff(&cur, &prev)));
207 static void loc_callback(void *arg)
209 category_id_t led_cat;
210 errno_t rc = loc_category_get_id("led", &led_cat, IPC_FLAG_BLOCKING);
211 if (rc != EOK)
212 return;
214 service_id_t *svcs;
215 size_t count;
216 rc = loc_category_get_svcs(led_cat, &svcs, &count);
217 if (rc != EOK)
218 return;
220 for (size_t i = 0; i < count; i++) {
221 bool known = false;
223 /* Determine whether we already know this device. */
224 list_foreach(led_devs, link, led_dev_t, dev) {
225 if (dev->svc_id == svcs[i]) {
226 known = true;
227 break;
231 if (!known) {
232 led_dev_t *dev = (led_dev_t *) calloc(1, sizeof(led_dev_t));
233 if (!dev)
234 continue;
236 link_initialize(&dev->link);
237 dev->svc_id = svcs[i];
238 dev->sess = loc_service_connect(svcs[i], INTERFACE_DDF, 0);
240 list_append(&dev->link, &led_devs);
244 // FIXME: Handle LED device removal
246 free(svcs);
249 int main(int argc, char *argv[])
251 if (argc < 2) {
252 printf("Compositor server not specified.\n");
253 return 1;
256 list_initialize(&led_devs);
257 errno_t rc = loc_register_cat_change_cb(loc_callback, NULL);
258 if (rc != EOK) {
259 printf("Unable to register callback for device discovery.\n");
260 return 1;
263 led_timer = fibril_timer_create(NULL);
264 if (!led_timer) {
265 printf("Unable to create LED timer.\n");
266 return 1;
269 frame_timer = fibril_timer_create(NULL);
270 if (!frame_timer) {
271 printf("Unable to create frame timer.\n");
272 return 1;
275 if (!decode_frames())
276 return 1;
278 winreg = argv[1];
279 window_t *main_window = window_open(argv[1], NULL,
280 WINDOW_MAIN | WINDOW_DECORATED, "barber");
281 if (!main_window) {
282 printf("Cannot open main window.\n");
283 return 1;
286 frame_canvas = create_canvas(window_root(main_window), NULL,
287 FRAME_WIDTH, FRAME_HEIGHT, frames[frame]);
289 if (!frame_canvas) {
290 window_close(main_window);
291 printf("Cannot create widgets.\n");
292 return 1;
295 window_resize(main_window, 0, 0, FRAME_WIDTH + 8, FRAME_HEIGHT + 28,
296 WINDOW_PLACEMENT_RIGHT | WINDOW_PLACEMENT_BOTTOM);
297 window_exec(main_window);
299 plan_led_timer();
300 plan_frame_timer(0);
302 task_retval(0);
303 async_manager();
305 return 0;
308 /** @}